#################################################
Setup Matlab and the ROS Toolbox for Ned/Ned2
#################################################
**V.2.1**
.. image:: /images/setup_matlab/matlab_ros_toolbox.png
:alt: set up matlab ros toolbox
:width: 100%
:align: center
Difficulty: ``medium``
Time: ``~1h`` with all the installations (depending on your internet connection).
.. note::
This tutorial is working from:
| The version v4.1.0 of the ned_ros_stack
| The version v4.1.0 of Niryo Studio
If you are using a Niryo One, please refer to
`this tutorial `_.
Objectives
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- Prepare the Matlab work environment in order to use it for all of our future Matlab tutorials
- Be able to use Ned/Ned2 with Matlab
- Be able to create content with Ned/Ned2 and Matlab using ROS
Requirements
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- Basic knowledge of Matlab
- Basic knowledge in robotics and ROS
- Being able to use Ned/Ned2
.. note::
| Documentation of Ned: https://archive-docs.niryo.com/product/ned/index.html
| Documentation of Ned2: https://archive-docs.niryo.com/product/ned2/index.html
| Matlab documentation: https://fr.mathworks.com/help/matlab/
| ROS documentation: http://wiki.ros.org/Documentation
What you will need
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- A Ned or a Ned2
- Ubuntu or Windows
- Matlab installed on your computer (2014 version or more)
- Matlab ROS Toolbox: https://fr.mathworks.com/products/ros.html
- A Wi-Fi communication
.. note::
To do this tutorial we used Windows10, Matlab2022a and a Ned2.
Add-ons installation
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
It is necessary to install the
Matlab add-ons by downloading them from `the Matlab website `__.
.. figure:: /images/setup_matlab/Ros_Toolbox.png
:alt: Matlab ros toolbox
:align: center
Matlab ROS Toolbox
What is the Matlab Ros Toolbox?
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ROS Toolbox provides an interface that connects MatlabĀ® and SimulinkĀ® to the Robot Operating System (ROS and ROS 2), allowing you to create a network of ROS nodes.
| The toolbox includes Matlab functions and Simulink blocks for importing, analyzing, and reading ROS data stored in rosbag files. In addition, you can establish a connection to a live ROS network to access ROS messages.
.. tip::
We advise you to have a first look to the Matlab `tutorial `__ concerning the ROS Toolbox.
Download the "ned_ros" package from Github
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
In order to control Ned/Ned2 with Matlab, it is essential to generate Ned's messages on Matlab. To do so, you will first need to download the "ned_ros" package
available on Niryo's Github.
You can clone the "ned_ros" package from github
--------------------------------------------------------------
.. code-block:: matlab
git clone https://github.com/NiryoRobotics/ned_ros.git
Or download as a `ZIP `__
--------------------------------------------------------------------------------------------------------------------------------------------
| Extract it in an easy-to-access file (e.g. D:/ned_ros) and check that the file has the name "ned_ros" (not "ned_ros_master").
.. figure:: /images/setup_matlab/ned_ros_repo.png
:alt: Ned ros repository
:width: 800px
:align: center
Ned ROS stack github repository
Setup pyenv in Matlab
^^^^^^^^^^^^^^^^^^^^^^^^^^^
In the Matlab Command Window, type:
.. code-block:: matlab
pyenv
If you get this answer:
.. figure:: /images/setup_matlab/Command_Window_Answer.png
:alt: Command Window answer
:align: center
Command Window answer
You have to install `python3.9 `__.
.. figure:: /images/setup_matlab/Python_Installer.png
:alt: Python installer
:width: 600px
:align: center
Python installer
.. note::
Don't forget to check the box "Add Python 3.9 to PATH"
Now, in the Matlab Command Window, type:
.. code-block:: matlab
pyenv('Version'),('YOUR_PATH/python.exe')
.. figure:: /images/setup_matlab/Command_Window_With_Pyenv.png
:alt: Command Window with pyenv setup
:align: center
Command Window with pyenv setup
Install Cmake version 3.19
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| Matlab will need the version 3.19 of Cmake. For this, you should first download CMake using this `link `__, download a .zip file and extract it.
| Copy the path of the /bin file, then go to the advanced settings of the computer and add a new variable to paste the path.
.. figure:: /images/setup_matlab/Advanced_Settings.png
:alt: Advanced settings
:width: 800px
:align: center
CMake setup in advanced settings
You can now test this command in a prompt:
.. code-block:: matlab
cmake --version
.. figure:: /images/setup_matlab/Prompt_CMake.png
:alt: Command prompt
:width: 800px
:align: center
Command prompt after CMake setup
In Matlab, try to compile these lines:
.. code-block:: matlab
folderpath = "/YOUR_PATH/ned_ros"
rosgenmsg(folderpath)
If CMake is not found, type in the Matlab Command Window:
.. code-block:: matlab
>> oldPath = getenv('PATH');
>> newPath = strcat(oldPath,pathsep,'YOUR_PATH\bin'); %YOUR_PATH of the /bin folder
>> setenv('PATH',newPath);
>> system('cmake --version')
Now, retry the compilation of the two lines.
.. admonition:: Possible error : "No C++ Compiler"
:class: error, dropdown
In this case, you have to install Visual Studio 2019 (only C++ compiler that ROS can support).
.. figure:: /images/setup_matlab/VS19_Installer.png
:alt: VS2019 installation
:width: 800px
:align: center
Virtual Studio 2019 installation
Then, in the Matlab Command Window, type:
.. code-block:: matlab
mex -setup
And click on:
.. code-block:: matlab
mex -setup C++
.. figure:: /images/setup_matlab/CPP_Compiler_Setup.png
:alt: C++ Compiler in Matlab
:align: center
C++ compiler setup in Matlab
Setup the working environment, generating Ned messages and services from the ROS package
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Now, you will have to link messages and services from Ned/Ned2 to Matlab.
| For this, you have to create a Matlab script on the repository you want. Then, write on the Matlab script you just created the code below. It will specify the path of the ned_ros package which contains all messages and services and generate them.
.. code-block:: matlab
folderpath = "/YOUR_PATH/ned_ros"
rosgenmsg(folderpath)
You should then have the message below
-----------------------------------------------------
.. code-block:: matlab
Identifying message files in folder '/YOUR_PATH/ned_ros'..Done.
Validating message files in folder '/YOUR_PATH/ned_ros'..Done.
[12/12] Generating MATLAB interfaces for custom message packages... Done.
Running catkin build in folder '/YOUR_PATH/ned_ros/matlab_msg_gen_ros1/win64'.
Build in progress. This may take several minutes...
Build succeeded.build log
To use the custom messages, follow these steps:
1. Add the custom message folder to the MATLAB path by executing:
addpath('/YOUR_PATH/ned_ros/matlab_msg_gen_ros1/win64/install/m')
savepath
1. Refresh all messages class definitions, which requires clearing the workspace, by executing:
clear classes
rehash toolboxcache
1. Verify that you can use the custom messages.
Enter "rosmsg list" and ensure that the output contains the generated
custom message types.
For the step 1, you should use the command below on the command window
------------------------------------------------------------------------------------
.. code-block:: matlab
addpath('/YOUR_PATH/ned_ros/matlab_msg_gen_ros1/win64/install/m')
savepath
.. note::
Getting a warning after this step is normal. Continue to step 2.
For the step 2, you should use the command below on the command window
-----------------------------------------------------------------------------
.. code-block:: matlab
clear classes
rehash toolboxcache
.. note::
Getting a warning after this step is normal. Continue to step 3.
For the step 3, you should use the command below on the command window
------------------------------------------------------------------------------
.. code-block:: matlab
rosmsg list
Make sure that you have all messages available on Matlab and more especially all the messages of niryo_robot.
.. code-block:: matlab
niryo_robot_commander/ArmMoveCommand
niryo_robot_commander/GetFKRequest
niryo_robot_commander/GetFKResponse
niryo_robot_commander/GetIKRequest
niryo_robot_commander/GetIKResponse
niryo_robot_commander/JogShiftRequest
niryo_robot_commander/JogShiftResponse
niryo_robot_commander/PausePlanExecution
niryo_robot_commander/RobotCommand
niryo_robot_commander/ShiftPose
(...)
niryo_robot_vision/DebugColorDetectionRequest
niryo_robot_vision/DebugColorDetectionResponse
niryo_robot_vision/DebugMarkersRequest
niryo_robot_vision/DebugMarkersResponse
niryo_robot_vision/ObjDetectionRequest
niryo_robot_vision/ObjDetectionResponse
niryo_robot_vision/TakePictureRequest
niryo_robot_vision/TakePictureResponse
You can now comment those two lines. Thus, you'll have to repeat this step again each time you connect a new robot.
.. note::
You can also see all services available on Matlab using "rosservice list" on the command window.
Well done! Matlab can now use Ned's ROS messages and services! You are now ready to turn on Ned/Ned2, connect it to internet and configure the communication between it and MATLAB.
Connect Ned/Ned2 to internet and get its IP address
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Now that you have all Ned's messages and services from the "ned_ros" package available on Matlab, you can connect Ned/Ned2 to internet in order to configure the connexion between it and Matlab using the ROS Toolbox.
| For this, you will first need to power Ned/Ned2 on and connect it to internet to get its IP address.
The easiest way to get Ned/Ned2's IP address is to use Niryo Studio. You can have all information `here `__.
.. note::
You have to connect your Ned/Ned2 with the `Wi-Fi mode `_ (not Hotspot).
.. note::
| For the rest of this tutorial, the IP address of Ned/Ned2 will be IP_OF_NED.
Configure the communication between Ned/Ned2 and Matlab
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Now that Ned/Ned2 is connected to internet, you can configure the communication between your robot and Matlab.
.. note::
- The host computer is your computer with Matlab, with the IP_HOST_COMPUTER IP address
- The robot is Ned/Ned2 with IP_OF_NED IP address
To define that we will use the 'setenv' function from the Matlab ROS Toolbox, you have to add the lines below on your Matlab script.
.. code-block:: matlab
ipaddress = "http://IP_0F_NED:11311";
setenv('ROS_MASTER_URI','ipaddress')
setenv('ROS_IP','IP_HOST_COMPUTER')
Run the communication between Ned/Ned2 and Matlab
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
It's now time to use Matlab and the ROS Toolbox and communicate with Ned/Ned2 from Matlab.
| To communicate with Ned/Ned2 from Matlab with ROS, we will need to initialize ROS. To do that with the ROS Toolbox you need to use 'rosinit'.
| By default, rosinit creates a ROS master in Matlab and starts a global node that is connected to the master. In our case we will directly give the IP address of Ned/Ned2 to 'rosinit'.
Add the line below on your Matlab script:
.. code-block:: matlab
rosinit(ipaddress)
| Normally, if everything is working properly, you should have this on the command window:
.. figure:: /images/setup_matlab/Rosinit.png
:alt: Rosinit Setup
:width: 900px
:align: center
Rosinit setup
Before disconnecting or turning off the robot, don't forget to type "rosshutdown" in the Matlab Command Window to cut the ROS connection.
| If you write 'rostopic list' on the command window, you should be able to see all the topics of Ned/Ned2.
.. note::
You are now ready to use Matlab with the ROS Toolbox and control Ned/Ned2 by following the tutorial below:
- :doc:`/source/tutorials/Use_ned_with_matlab`