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.
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.
If CMake is not found, type in the Matlab Command Window:
>>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.
Possible error : “No C++ Compiler”
In this case, you have to install Visual Studio 2019 (only C++ compiler that ROS can support).
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.
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.
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:
rosinit(ipaddress)
Normally, if everything is working properly, you should have this on the command window: