Publish and Subscribe on Ned with MATLAB ROS Toolbox
Note
- This tutorial is working from:
- The version v3.0.0 of the ned_ros_stackThe version v3.0.0 of Niryo Studio
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
What you will need
A Ned
MATLAB
MATLAB ROS Toolbox https://fr.mathworks.com/products/ros.html
A Wi-Fi communication
Warning
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.
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
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.
rosshutdown
If everything is working, on the workspace you should have these three variables:
The IP address
The subscriber we created
The scandata variable
Use the MATLAB ROS Toolbox: Publish to a topic of Ned from MATLAB
robotCmd = rospublisher("/niryo_robot/learning_mode/state") ;
velMsg = rosmessage(robotCmd);
velMsg.Data = true;
send(robotCmd,velMsg)
rosshutdown
If everything is working, on the workspace you should have these three variables:
The IP address
The publisher we created
The velMsg variable