File Exchange

image thumbnail

ROS Deployed on Arduino running in External Mode in Simulink

version 1.0.3 (221 KB) by SMEAC
This Simulink Model with s-Function demonstrates how to deploy a ROS Node to an Arduino while still maintaining full use of External Mode


Updated 11 Dec 2018

View License

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.

For compiling:
Prior to deploying software to Arduino, the sfun_ros_interface.mexw64 needs to be made. In the Matlab command window run:
"mex sfun_ros_interface.cpp"

For Testing:
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:
In 1.
In 2.
"sudo chmod 666 /dev/ttyS11"
"rosrun rosserial_python /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
In 3.
"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 ." 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.

Cite As

SMEAC (2021). ROS Deployed on Arduino running in External Mode in Simulink (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (2)


@Mohamed Khaled, If you want your board to have a Simulink model running as well, you could use this model. If you just want an Arduino program and have it respond to the messages, use rosserial directly. The advantage from this program, is that you have debug possibilities, where you can look at the variables on the board in real time using displays or scopes.

Mohamed Khaled

I working on a project for prototype of new robotic. I have a raspberry pi and i know already there is a siumlink box for ros on raspberry pi, but my question is : if i have another board ( ardunio or teensy ) connected with raspberry pi with serial port, and i want to publish a message from this board and make raspberry pi subscribe this message, is it possible to do that with your example, or shouldl i publish the teensy message through ros kinetic on raspberry pi and use the ros simulink to subscribe the message ?
I assumed that i install ros kinetic on raspberry pi also i installed rosserial_arduino or rosserial_teensy depending on which board i will use.

MATLAB Release Compatibility
Created with R2018b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!