#################################################################### Publish and Subscribe on Ned with MATLAB ROS Toolbox #################################################################### .. image:: /images/Sub_Pub_Matlab/Sub_Pub_Matlab.png :alt: control ned matlab ros publish subscribe :width: 100% :align: center .. note:: This tutorial is working from: | The version v3.0.0 of the ned_ros_stack | The 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:: | Documentation of Ned: https://docs.niryo.com/product/ned/index.html | MATLAB documentation: https://fr.mathworks.com/help/matlab/ | ROS documentation: http://wiki.ros.org/Documentation | Tutorial: :doc:`/source/tutorials/setup_ned_matlab_ros_toolbox` What you will need ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - A Ned - MATLAB - MATLAB ROS Toolbox https://fr.mathworks.com/products/ros.html - A Wi-Fi communication .. 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 :doc:`/source/tutorials/setup_ned_matlab_ros_toolbox` 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: .. code-block:: matlab ssh niryo@IP_OF_NED Then, you have to stop all services of Ned and launch them again. .. code-block:: matlab 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. .. image:: /images/services_matlab/error.png :alt: Ned :width: 600px :align: center To do that, you have to uncomment the two following lines: .. code-block:: matlab 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. .. code-block:: 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. | 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. .. code-block:: 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 .. image:: /images/Sub_Pub_Matlab/subscriber.png :alt: Ned :width: 650px :align: center - The scandata variable .. image:: /images/Sub_Pub_Matlab/mouv1.png :alt: Ned :width: 650px :align: center .. note:: Don't hesitate to manually move Ned and run the script again to see that the joints datas changed. See the example below: .. image:: /images/Sub_Pub_Matlab/mouv2.png :alt: Ned :width: 650px :align: center 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. .. code-block:: matlab 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: .. code-block:: 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 .. image:: /images/Sub_Pub_Matlab/robotCmd.png :alt: Ned :width: 750px :align: center - The velMsg variable .. image:: /images/Sub_Pub_Matlab/velMsg.png :alt: Ned :width: 750px :align: center .. 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. .. image:: /images/Sub_Pub_Matlab/true.png :alt: Ned :width: 550px :align: center