ROS with Matlab

Note

This section is covered only for ELE306 students.

Note

Matlab has already been installed in the VM copy you have but it is not activated. Please follow the instructions on the ~/Desktop to activate Matlab using your own account.

We have seen that ROS is a very powefull tool which allows us to use many predefined packages including various robots’ packages, various algorithms, sensor drivers etc. On the other hand, we have another powerful in our hands for which mostly we use signal processing, modelling, in control process. It would be cool if we used them together.

Recently Matlab included ROS under its Robotics Toolbox which enables the ROS environment accessible via Matlab. Now, we are going to set our Matlab to control turtlesim which we started from our ROS.

Beneficial tutorials:

  1. ROS Toolbox

As we have roscore on ROS, there is an equivalent on Matlab as rosinit to start the ROS master in the network. There is a slight difference of starting the ROS master between using roscore and rosinit. If you type rosnode list, you will see a global node which was started by Matlab but you won’t see rosout (as we are used to see). In usage, there is not a significant difference right now since we are not going to connect a real robot. Just keep in mind that if you need to include more modules/robots/PC in the same ROS network, they should be connected to the same ROS master. If you want to initialize your ROS master on a specific address you should specify it rosinit('192.168.17.157').

Since we successfully ran the ros master, we can create the first publisher. There are two ways of doing it:

  1. Via Simulink

  2. Via Matlab Script

Via Matlab Script

Note

You can write this code directly on the Command Window also.

On turtlesim: Copy this piece of code into a new script which you created under the work directory of Matlab.

% rosinit;
[pub,msg] = rospublisher('/turtle1/cmd_vel','geometry_msgs/Twist');
msg.Linear.X = 0.5;
msg.Angular.Z = -0.5;
rate = robotics.Rate(10);
while rate.TotalElapsedTime < 100
   send(pub,msg)
   waitfor(rate);
end

%rosshutdown

On ExampleHelperSimulinkRobotROS:

  • Chance the /turtle1/cmd_vel into /mobile_base/commands/velocity.