Publish and Subscribe on Ned with MATLAB ROS Toolbox

ROS Logo Ned Ned

Objectives

  • Be able to use Ned with MATLAB

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

  • Have access to the different topics of Ned with MATLAB using ROS

  • Subscribe to a topic of Ned from MATLAB

  • Publish to a topic of Ned from MATLAB

Requirements

  • Basic knowledge of MATLAB

  • Basic knowledge in robotics and ROS

  • Being able to use Ned

  • Having done the “How to setup Ned for MATLAB and the ROS Toolbox” tutorial

Note

How to setup Ned for MATLAB and the ROS Toolbox tutorial: Setup MATLAB and the ROS Toolbox for Ned

What you will need

Warning

Make sure that you already followed the “How to setup Ned for MATLAB and the ROS Toolbox” tutorial before doing this one.
You should now be able to communicate with Ned from MATLAB via ROS Toolbox and the ‘rosinit’ function.
In this tutorial you will have to use the code you created on the Setup MATLAB and the ROS Toolbox for Ned tutorial or use the code on the github at this link: Matlab setup ros toolbox.

Start a new roscore on Ned to make sure to have access to the topics.

It is important to start a new roscore and launch the ‘niryo_robot_bringup niryo_ned_robot.launch’ launcher. Indeed, when Ned starts, it automatically launches a roscore and all the packages it needs to run. Unfortunately, MATLAB doesn’t manage to have access to this roscore and needs it to have it launched an other time.

For this, you need to connect via ssh to Ned:

ssh niryo@IP_OF_NED

Then, you have to stop all services of Ned and launch them again.

sudo service niryo_robot_ros stop
roslaunch niryo_robot_bringup niryo_ned_robot.launch

Warning

If you have the error below, you will have to generate again Ned’s messages and services from the ‘ned_ros’ package.

Ned

To do that, you have to uncomment the two following lines:

folderpath = "/YOUR_PATH/ned_ros"
rosgenmsg(folderpath)

See on the “How to setup Ned for MATLAB and the ROS Toolbox” tutorial if you don’t remember all the steps to follow.

You are now able to publish and subscribe from MATLAB on Ned with the ROS Toolbox.

Use the MATLAB ROS Toolbox: Subscribe to a topic of Ned from MATLAB

To subscribe to a topic from MATLAB with the ROS Toolbox, you will need to create a subscriber and use the ‘receive()’ function to get the datas.
If we take the example of the /joint__states topics we should add the code below to our previous MATLAB script.
joint_states = rossubscriber('/joint_states');
pause(2)
scandata = receive(joint_states,10);

We create a subscriber called joint_states, then we wait for 2 seconds to let time for the subscriber to be created (but it’s not necessary) and then we receive the datas on the ‘scandata’ variable.

Before running the script, don’t forget to add this line at the end of your script to shut down the ROS network in MATLAB.
rosshutdown
You are now ready to run the code and subscribe to your first Ned’s topic with MATLAB.

If everything is working, on the workspace you should have these three variables:

  • The IP address

  • The subscriber we created

Ned
  • The scandata variable

Ned

Note

Don’t hesitate to manually move Ned and run the script again to see that the joints datas changed. See the example below:

Ned

Use the MATLAB ROS Toolbox: Publish to a topic of Ned from MATLAB

To subscribe to a topic from MATLAB with the ROS Toolbox, you will need to create a publisher and use the ‘send()’ function to send the datas.
If we take the example of the /niryo_robot/learning_mode/state topics we should add the code below to our previous MATLAB script.
robotCmd = rospublisher("/niryo_robot/learning_mode/state") ;
velMsg = rosmessage(robotCmd);

velMsg.Data = true;
send(robotCmd,velMsg)
First create a publisher called robotCmd. This publisher will need to send a message to publish. That’s why we created a message called velMsg just after. This message will need to be the correct type for the publisher.
Now that we created a message with the good type, we can modify this message and put the value we want. In our case it’s a Bool so we change it from false to true.
Finally we use the ROS Toolbox function called ‘send’ to publish the new message.
Before running the script, don’t forget to add this line at the end of your script to shut down the ROS network in MATLAB:
rosshutdown
You are now ready to run the code and publish to your first Ned’s topic with MATLAB.

If everything is working, on the workspace you should have these three variables:

  • The IP address

  • The publisher we created

Ned
  • The velMsg variable

Ned

Note

You can open a terminal on the catkin_ws of the robot and do a rostopic echo of the topic /niryo_robot/learning_mode/state to see that your message has been sent.

Ned