Setup MATLAB and the ROS Toolbox for Ned

ROS Logo Ned Ned

Note

If you are using a Niryo One, please refer to this tutorial.

Objectives - MATLAB ROS Toolbox

  • Prepare the MATLAB work environment in order to use it for all of our future MATLAB tutorials

  • Be able to use Ned with MATLAB

  • Be able to create content with Ned and MATLAB using ROS

Requirements - MATLAB ROS Toolbox

  • Basic knowledge of MATLAB

  • Basic knowledge in robotics and ROS

  • Being able to use Ned

What you will need - MATLAB ROS Toolbox

Add-ons installation

It is necessary to install the Matlab add-ons by downloading them from the MATLAB website:

MATLAB ros toolbox

MATLAB ros toolbox

MATLAB ros toolbox for ROS messages

MATLAB ros toolbox for ROS messages

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.

Note

We advise you to have a first look to the MATLAB tutorial concerning the ROS Toolbox: https://fr.mathworks.com/help/ros/ug/get-started-with-ros.html.

To be able to use this add-on from MATLAB we will need to set up correctly the connection between Ned and the host computer (the computer with using MATLAB). It means we will need to modifiy the bashrc of the Raspberry Pi of Ned and the one of the host computer.

Install Cmake version 3.19

Matlab will need the version 3.19 of Cmake. For this, you should first download the last version of CMake using this link: https://cmake.org/download/.

After that you can follow the command below:

sudo apt remove --purge --auto-remove cmake
version=3.19
build=1
mkdir ~/temp
cd ~/temp
wget https://cmake.org/files/v$version/cmake-$version.$build.tar.gz
tar -xzvf cmake-$version.$build.tar.gz
cd cmake-$version.$build/
./bootstrap
make -j$(nproc)
sudo make install

Setup the working environment, download the “ned_ros” package from Github

In order to control Ned with MATLAB, it is essential to generate Ned’s messages on MATLAB. For this, 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 and extract it to the folder of your choice

Ned ros repository

Ned ROS stack github repository

Setup the working environment, generating Ned messages and services from the ROS package

Now that you have the ned_ros package, you will have to link messages and services from Ned 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.
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.
[9/9] Generating MATLAB interfaces for custom message packages... Done.
Running catkin build in folder '/YOUR_PATH/ned_ros/matlab_msg_gen_ros1/glnxa64'.
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/glnxa64/install/m')
savepath

2. Refresh all messages class definitions, which requires clearing the workspace, by executing:

clear classes
rehash toolboxcache

3. 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/glnxa64/install/m')
savepath

Note

If you have a warning after this step it’s normal, you can continue to step 2.

For the step 2 you should use the command below on the command window

clear classes
rehash toolboxcache

Note

If you have a warning after this step it’s normal, you can continue to step 3.

For the step 3 you should use the command below on the command window

rosmsg list

Normally you should have all messages available on MATLAB and more especially all the messages of Ned.

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. But everytime you connect a new robot you will have to do this step again.

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, connect it to internet and configure the communication between it and MATLAB.

Connect Ned 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 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 and connect it to internet to get his IP address.

The easiest way to get the IP address of Ned is to use Niryo Studio. You can have all information here: https://docs.niryo.com/product/ned/en/source/software/niryo_studio.html#connecting-ned-to-niryo-studio.

Note

For the rest of this tutorial, the IP address of Ned will be IP_OF_NED.
You can use Ned in simulation with the IP addres 127.0.0.1.

Configure the communication between Ned and MATLAB

Now that Ned is connected to internet, you can configure the communication between Ned and MATLAB.

To be able to configure the communication between MATLAB and Ned using the ROS Toolbox, you will first need to properly set Ned’s environment and your computer’s.
For this, you will need to set the good IP addresses on the bashrc of Ned and your computer. Fortunately, the MATLAB ROS Toolbox is doing it for us but you can see more in detail how the communication bewteen Ned and MATLAB is working on the schematics below:
Ned

Note

  • The host computer is your computer with MATLAB, with the IP_HOST_COMPUTER IP address

  • The robot is Ned 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 two lines below on your MATLAB script.

setenv('ROS_MASTER_URI','http://IP_OF_NED:11311')
setenv('ROS_IP','IP_HOST_COMPUTER')

Run the communication between Ned and MATLAB

It’s now time to use MATLAB and the ROS Toolbox and communicate with Ned from MATLAB.

To communicate with Ned 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 to ‘rosinit’.

Add the line below on your MATLAB script:

ipaddress = "http://IP_0F_NED:11311";
rosinit(ipaddress)
Normally, if everything is working properly, you should have this on the command window:
Ned
If you write ‘rostopic list’ on the command window, you should be able to see all the topics of Ned.
You are now ready to use MATLAB with the ROS Toolbox and control Ned.