Using the code from rosserial_arduino it is possible to use an arduino to as a ROS Node. Matlab initially complicates the issue by using the USB for its external mode, which prevents ROS publishing or subscribing over rosserial.
This is circumvented using the #define USE_STM32_HW_SERIAL to make ROS use the Arduino MEGA Serial Port 1. Note that an additional Serial Port Cable such as a DELOCK 83116 is needed to provide the second connection.
Due to the memory requirements, this currently only works on Arduino MEGA 2560.
Prior to deploying software to Arduino, the sfun_ros_interface.mexw64 needs to be made. In the Matlab command window run:
Assuming you already have ROS installed in either WSL or Linux, (I use ROS in WSL without any problems at all), start up three terminals with your ROS installation and catkin workspace sourced, and then in the three terminals run:
"sudo chmod 666 /dev/ttyS11"
"rosrun rosserial_python serial_node.py /dev/ttyS11"
where /dev/ttyS11 needs to be modified for the serial port your computer is using for connecting with Serial Port 1 of the Arduino MEGA
When everything is working correctly
"rostopic echo adc"
To subscribe to and view the values of the 6 adc channels being published to ROS
"rostopic pub toggle_led std_msgs/Empty --once"
To activate the LED on the arduino and enable the Servo Outputs
" rostopic pub robot_link_1 std_msgs/UInt16 300 --once"
To set the output to servo 1 to 300.
Note 1: This is only a demo program to show how ROS can be used to link with other parts of Simulink such as a state machine.
Note 2: The Software is prepared with ROS Kinetic. If you want to use it with any other ROS Version, or add in custom messages, you will need to patch the files with those you generate in ROS through:
"rosrun rosserial_arduino make_libraries.py ." as demonstrated in the rosserial_arduino Tutorial.
Note that these files cannot necessarily be directly copied across but will need to be modified, for instance to remove all Serial.print calls, to prevent ROS trying to use the same serial port as MATLAB.
This Submission includes code from rosserial_arduino, written by Michael Ferguson, Adam Stambler and published under BSD.
SMEAC (2023). ROS Deployed on Arduino running in External Mode in Simulink (https://www.mathworks.com/matlabcentral/fileexchange/69554-ros-deployed-on-arduino-running-in-external-mode-in-simulink), MATLAB Central File Exchange. Retrieved .
MATLAB Release Compatibility
Platform CompatibilityWindows macOS Linux
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!
Compiled MEX File removed, and comment added on how to compile mex file.