Setup Matlab and the ROS Toolbox for Ned/Ned2
V.2.1
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_stackThe 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
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.
What is the Matlab Ros Toolbox?
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
git clone https://github.com/NiryoRobotics/ned_ros.git
Or download as a ZIP
Setup pyenv in Matlab
In the Matlab Command Window, type:
pyenv
If you get this answer:
You have to install python3.9.
Note
Don’t forget to check the box “Add Python 3.9 to PATH”
Now, in the Matlab Command Window, type:
pyenv('Version'),('YOUR_PATH/python.exe')
Install Cmake version 3.19
You can now test this command in a prompt:
cmake --version
In Matlab, try to compile these lines:
folderpath = "/YOUR_PATH/ned_ros"
rosgenmsg(folderpath)
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).
Then, in the Matlab Command Window, type:
mex -setup
And click on:
mex -setup C++
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.
folderpath = "/YOUR_PATH/ned_ros"
rosgenmsg(folderpath)
You should then have the message below
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
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
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
rosmsg list
Make sure that you have all messages available on Matlab and more especially all the messages of niryo_robot.
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.
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
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.
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.
Add the line below on your Matlab script:
rosinit(ipaddress)
Before disconnecting or turning off the robot, don’t forget to type “rosshutdown” in the Matlab Command Window to cut the ROS connection.
Note
You are now ready to use Matlab with the ROS Toolbox and control Ned/Ned2 by following the tutorial below: