Chapter 8. ROS on MATLAB and Android
As you all know, MATLAB is one of the powerful numerical computation tools available for research, education, and commercial applications. Also, we don't need any more explanation of the Android OS, which is one of the most popular mobile operating systems. In this chapter, we will mainly work with the ROS interface of MATLAB and Android.
By combining the capabilities of MATLAB and Android in ROS, we can create powerful robotic projects. Here are the main topics we will discuss in this chapter:
Getting started with the ROS-MATLAB interface
Communicating from MATLAB to a ROS network
Controlling a ROS robot from MATLAB
Getting started with Android and its ROS interface
Installing the ROS-Android interface
Playing with ROS-Android applications
ROS-Android code walk through
Creating a basic application using the ROS-Android interface
Getting started with the ROS-MATLAB interface
The ROS-MATLAB interface is a useful interface for researchers and students for prototyping their robot algorithms in MATLAB and testing it on ROS-compatible robots. The robotics system toolbox in MATLAB provides the interface between MATLAB and ROS. We can prototype our algorithm and test it on a ROS-enabled robot or in robot simulators such as Gazebo and V-REP (http://www.coppeliarobotics.com/downloads.html). From MATLAB, we can publish or subscribe to a topic, such as a ROS node, and we can make it a ROS master. The MATLAB-ROS interface has most of the ROS functionalities that we need.
Here is a block diagram shows how MATLAB is communicating with a robot which is running on ROS.
Figure 1: The MATLAB - Robot communication diagram
From the preceding figure, you can understand, MATLAB is equipped with powerful toolboxes such as computer vision, control system and signal processing. We can fetch the data from robot through ROS interface and process...
Setting Robotics Toolbox in MATLAB
Here is the link to download a trial or purchase the Robotics Toolbox in MATLAB (https://in.mathworks.com/products/robotics.html). This toolbox is compatible with MATLAB version 2013 onward. If you don't have MATLAB, you can test the chapter's code using a trial version; if you have it, buy or download a trial version of Robotic Toolbox.
Basic ROS functions in MATLAB
After setting up Robotics Toolbox in MATLAB, we can start working on the important functions of MATLAB that are used to interact with a ROS network. Let's look at them with examples.
Initializing a ROS network
Before running a ROS node, we have to run the roscore
command, right? The roscore
command will start a ROS master, and other ROS nodes can find each other through it. In MATLAB, instead of the roscore
command, we can use the rosinit
function to start a ROS master.
Figure 2 : The rosinit function in MATLAB
The rosinit
function can start a ROS master and a global node that is connected to the...
Communicating from MATLAB to a ROS network
We have worked with some MATLAB commands and we've understood that we can communicate with ROS from MATLAB. But the previous commands were executed in a MATLAB terminal by making MATLAB the ROS master. But what do we do when we need to communicate with a ROS network or a ROS-based robot? The method is simple.
Assuming your PC has MATLAB and the ROS PC/robot is connected to the same network. It can be connected either through LAN or Wi-Fi. If the PC and robot are connected to the same network, both should have identical IP addresses. The first step is to find each device's IP address.
If your MATLAB installation is in Windows, you can open Command Prompt window by simply searching for cmd
in the search window; then, enter the ipconfig
command. This will list the network adapters and their details:
Figure 6: Wi-Fi adapter details and its IP in a MATLAB system
Here you can see that the PC running MATLAB and the ROS system are connected through Wi-Fi...
Controlling a ROS robot from MATLAB
Here is an interesting MATLAB GUI application that uses ROS APIs to remotely control a robot. The final application will look like the following:
Figure 13: MATLAB-ROS application GUI
In this application, we can put in the ROS master IP, port, and the teleop topic of the robot in its GUI itself. When we press the connect button, the MATLAB application will connect to the ROS network. Now, we can move the robot by pressing the Forward, Backward, Left, and Right buttons.
Here is the design block diagram of this application:
Figure 14: MATLAB-ROS application design block diagram
So let's look at how we can build an application like this.
Here are some of the frequently asking questions in ROS-MATLAB interface
How to run multiple ROS nodes in MATLAB?
Yes, we can run multiple ROS nodes in MATLAB. The following command in MATLAB will give you an example to do it.
>>>openExample('robotics/RunMultipleROSNodesToPerformDifferentTasksExample')
Does MATLAB support...
Getting started with Android and its ROS interface
There exists a cool interface between ROS and Android. As you know, Android is one of most popular operating systems in mobile devices. Just imagine: if we can access all features of a mobile devices on the ROS network, we can build robots using it, right? We can build Android apps with ROS capabilities and can make any kind of robot using it, its scope is unlimited.
The following shows how the communication between android device and ROS robot is happening. The figure shows an example Android-ROS application which can teleoperate robot from an android device. Each android application should inherit from RosActivity which is getting from Android-ROS interface, then only we can access ROS API's in our application. We can see more about the API's after this section.
Figure 22: Android - ROS teleop interface
The core backend of the Android ROS library is RosJava (http://wiki.ros.org/rosjava), which is an implementation of ROS in Java. There...
Installing the ROS-Android interface
If all the dependencies are satisfied, you can easily build the ROS-Android interface and build a bunch of Android-ROS applications. Here is how we can do that:
Initially, we have to create a workspace folder for the Android interface. We can name it android_core
:
$ mkdir -p ~/android_core
After creating this folder, you can initialize the workspace using the following command:
wstool init -j4 ~/android_core/src https://raw.github.com/rosjava/rosjava/indigo/android_core.rosinstall
Now switch to the workspace and build the workspace using catkin_make
:
$ cd ~/android_core
$ catkin_make
After building the workspace successfully, you can source it by adding it to .bashrc
:
$ echo 'source ~/android_core /devel/setup.bash' >> ~/.bashrc
You are now done with setting up the android_core
package in ROS. So what do you get after building this workspace? You will get a bunch of Android-ROS applications that can be installed on your Android device. You will also...
Playing with ROS-Android applications
In this section, we will see how to install the ROS-Android application generated from the preceding build process on your Android phone.
Let's take the android_core
folder and search for .apk
files; you may get a bunch of applications, as shown in the following figure:
Figure 25: List of generated APK files
You can copy the APK files and install them on your phone.
You may get errors while installing these APK files. One of the errors is shown in the following screenshot:
Figure 26: Parse error during installation of APK
Here are the tips to solve this issue:
The first step is to enable installation from Unknown sources, as shown in section 1 of the following figure:
Figure 27: Tips to solve parse error
Install an Android app called APK Editor, which can be downloaded from following the link:
https://play.google.com/store/apps/details?id=com.gmail.heagoo.apkeditor&hl=en
You can also buy the Pro version, which you may require in the future...
Let's check out the Android-ROS application code for the basic publisher-subscriber app. You can get it from ~/android_core/android_tutorial_pubsub/src
. You'll see a file called MainActiviy.java
, and now I'll explain the code.
In the beginning of the code, you can see the package name and required Android modules for this application. The important modules are RosActivity
and NodeConfiguration
. These will help us create a new ROS node in an Android activity (https://developer.android.com/guide/components/activities.html).
package org.ros.android.android_tutorial_pubsub;
import android.os.Bundle;
import org.ros.android.MessageCallable;
import org.ros.android.RosActivity;
import org.ros.android.view.RosTextView;
import org.ros.node.NodeConfiguration;
import org.ros.node.NodeMainExecutor;
import org.ros.rosjava_tutorial_pubsub.Talker;
Here is where the Android MainActivity
starts, which is...
Creating basic applications using the ROS-Android interface
We have covered Android - ROS applications provided from the ROS repository. So how can we create our own application using it? Let's take a look.
First, we have to create a separate workspace for our application. Here, it is named myandroid
:
$ mkdir -p ~/myandroid/src
Switch to the workspace's src
folder:
$ cd ~/myandroid/src
Create a package called android_foo
that depends on android_core
, rosjava_core
, and std_msg
:
$ catkin_create_android_pkg android_foo android_core rosjava_core std_msgs
Switch into android_foo
and add sample libraries to check whether the project is building properly:
$ cd android_foo
$ catkin_create_android_project -t 10 -p com.github.ros_java.android_foo.bar bar
$ catkin_create_android_library_project -t 13 -p com.github.ros_java.android_foo.barlib barlib
$ cd ../..
And finally, you can build the empty project using catkin_make
:
$ catkin_make
If it is building properly, you can add a custom project, such as...
In this chapter, we mainly discussed two important interfaces of ROS: MATLAB and Android. Both are very popular platforms, and this chapter will be very useful if you are working on the interfacing of ROS with MATLAB and Android. In MATLAB interfacing, we covered Robotics Toolbox and APIs to connect to ROS networks. Using these APIs, we built a GUI application to teleoperate a ROS robot. In the Android-ROS interfacing section, we saw how to set up and build Android-ROS applications from a Linux PC. After that, we successfully built ROS-Android applications and saw demos of important applications. We also saw the Android-ROS application code and its functions, and finally, we saw how to build a custom Android-ROS application.