Run multiple ROS nodes on a connected ROS device. ROS nodes can be generated using Simulink® models to perform different tasks on the ROS network. These nodes are then deployed on a ROS device and can be run independently of Simulink®.
This example uses two different Simulink models that have been deployed as ROS nodes. See Generate a Standalone ROS Node from Simulink and follow the instructions to generate and deploy a ROS node. Do this twice and name them 'robotcontroller'
and 'robotcontroller2'
. The 'robotcontroller'
node sends velocity commands to a robot to navigate it to a given point. The 'robotcontroller2'
node uses the same model, but doubles the linear velocity to drive the robot faster.
Create a connection to a ROS device. Specify the address, user name, and password of your specific ROS device. The device contains information about the ROS device, including the available ROS nodes that can be run using runNode
.
d =
rosdevice with properties:
DeviceAddress: '192.168.203.129'
Username: 'user'
ROSFolder: '/opt/ros/indigo'
CatkinWorkspace: '~/catkin_ws'
AvailableNodes: {0×1 cell}
d =
rosdevice with properties:
DeviceAddress: '192.168.203.129'
Username: 'user'
ROSFolder: '/opt/ros/indigo'
CatkinWorkspace: '~/catkin_ws_test'
AvailableNodes: {'robotcontroller' 'robotcontroller2'}
Run a ROS core. The ROS Core is the master enables you to run ROS nodes on your ROS device. Connect MATLAB® to the ROS master using rosinit
. For this example, the port is set to 11311. rosinit
can automatically select a port for you without specifying this input.
Initializing global node /matlab_global_node_66434 with NodeURI http://192.168.203.1:59395/
Check the available ROS nodes on the connected ROS device. The nodes listed in this example were generated from Simulink® models following the process in the Generate a Standalone ROS Node from Simulink example. Two separate nodes are generated, 'robotcontroller'
and 'robotcontroller2'
, which have the linear velocity set to 1 and 2 in the model respectively.
ans = 1×2 cell
{'robotcontroller'} {'robotcontroller2'}
Start up the Robot Simulator using ExampleHelperSimulinkRobotROS
. This simulator automatically connects to the ROS master on the ROS device. You will use this simulator to run a ROS node and control the robot.
Run a ROS node, specifying the node name. The 'robotcontroller'
node commands the robot to a specific location ([-10 10]
). Wait to see the robot drive.
Reset the Robot Simulator to reset the robot position. Alternatively, click Reset Simulation. Because the node is still running, the robot continues back to the specific location. To stop sending commands, stop the node.
Run the 'robotcontroller2'
node. This model drives the robot with twice the linear velocity. Reset the robot position. Wait to see the robot drive. You should see a wider turn due to the increased velocity.
Close the simulator. Stop the ROS node. Disconnect from the ROS network and stop the ROS core.
Shutting down global node /matlab_global_node_66434 with NodeURI http://192.168.203.1:59395/