Setup Matlab and the ROS Toolbox for Ned/Ned2

V.2.1

set up matlab ros toolbox

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

What you will need

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.

Matlab ros toolbox

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

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”).
Ned ros repository

Ned ROS stack github repository

Setup pyenv in Matlab

In the Matlab Command Window, type:

pyenv

If you get this answer:

Command Window answer

Command Window answer

You have to install python3.9.

Python installer

Python installer

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')
Command Window with pyenv setup

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.
Advanced settings

CMake setup in advanced settings

You can now test this command in a prompt:

cmake --version
Command prompt

Command prompt after CMake setup

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.

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.
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.

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.

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:

rosinit(ipaddress)
Normally, if everything is working properly, you should have this on the command window:
Rosinit Setup

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: