It’s clear that in the future we will have many devices that need the Internet for their work in our regular life. While devices are becoming more intelligent, the degree of automatisation is increasing. Today devices use the Internet to find or store some data. However, frequently it's hard to integrate different IoT-devices in one system. The market needs standardisation.
In this chapter, we are going to introduce you to the Robot Operating Systems (ROS) libraries, which could be used in IoT tasks. The ROS is not just the most popular platform for academic researches in robotics, but a powerful open-source set of software libraries and tools not only for robotics but also autonomous systems.
Firstly, the ROS is the most popular education platform for world universities. ROS is widely used in different researches. Secondly, ROS is based on Linux, so you can use all libraries from ROS to stop worrying about the physical side of your device and write code to it or even your own Linux core. So far, Linux is the best OS for embedded systems.
The latest version of ROS is Kinetic Kame, which is available for Ubuntu Wily (15.10) and Ubuntu Xenial (16.04 LTS). If you have older Ubuntu version install ROS Indigo Igloo.
Instructions for installing ROS Kinetic Kame:
1. Configure your Ubuntu repositories to allow restricted
, universe
, and multiverse
2. Setup your computer to accept software from packages.ros.org
.
sudo sh -c 'echo "deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list'
3. Make sure your Debian package is up-to-date
sudo apt-get update
4. Install ROS Desktop version. If you want to work with navigation and 2D/3D perception packages, make Desktop-full install
sudo apt-get install ros-kinetic-desktop
apt-cache search ros-kinetic
5. To make all system dependencies for source you want to compile, run rosdep
. Also it is required to run some core components in ROS.
sudo rosdep init
rosdep update
6. Environment setup
echo "source /opt/ros/kinetic/setup.bash" >> ~/.bashrc source ~/.bashrc
7. To create and manage your own ROS workspaces, there are various tools and requirements that are distributed separately. To install this tool and other dependencies for building ROS packages, run:
sudo apt-get install python-rosinstall python-rosinstall-generator | python-wstool build-essential
To be good at ROS programming, you need to understand ROS filesystem concepts and be able to use
roscd
, rosls
, and rospack
command line tools.
After that, let's instal turtlesim
. This package illustrates how different things work.
Use the following command:
sudo apt-get install ros-kinetic-turtlesim
Now, there is a need to configure rosdep
systemwide. Just execute the command:
rosdep
The goal of this command is to initialise rosdep
, which
is a tool for checking and installing package dependencies in an OS-independent way. On Ubuntu, for instance, rosdep
acts as a front end to apt-get.
Doesn't matter you install ROS yourself of you're using a computer with pre-installed ROS, the are two important configuration steps that must be done within the account of every user that is going to ROS using.
At start, initialize the rosdep system in your account:
rosdep update
This command stores some files in your home directory, in a sub-directory named .ros
. This action must be done one time.
Before using this command be sure you are acting into your account, not using sudo
.
Let's start ROS programming with a simple example. This exercise serves a few purposes: it provides an opportunity to check that ROS is installed correctly, introduce turtlesim
simulator and it is used in a lot of tutorials published on the web.
Starting turtlesim
roscore rosrun turtlesim_node rosrun turtlesim turtle_teleop_key
By running separate command terminals, we can run three commands simultaneously. If the configuration is OK you can see a graphical window:
This window shows a simulated turtle robot that operates in a squad.
If you choose the terminal one which is executing turtle_teleop_key
and give the inputs by pressing Left, Right, Up, Down, the turtle should move to your commands.
You should keep these three terminals open because of the examples in the following sections will show some additional ways to interact with this system.
Before we start to write programs, we should create a workspace to save there our packages, and then create the package.
Creating workspace
Packages we are creating should be situated together in a directory called a workspace. For instance: /home/$username$/ros
. You can name your workspace ever you want and save the directory anywhere in your account. To create a directory, use the mkdir
command. If you are working on many projects, we recommend you to create several independent workspaces.
Now, lets set up the workspace. Create an src
subdirectory inside the workspace directory. This directory will contain the source code of your packages.
A package creation To create a new ROS package use the following command:
catkin_create_pkg //package-name//
This command should be run from the src
directory of initial workspace.
This command a directory to hold the package and creates two configuration files inside that directory.
package.xml
is the manifest. This file defines some details about the package, such as its name, version, maintainer, and dependencies. The directory containing package.xml is called the package directory
.CMakeLists.txt
is a script for an industrial-strength cross-platform build system CMake. It contains a list of build instructions including what executables should be created, what source files to use to build each of them, and where to find the include files and libraries needed for those executables. CMake is used internally by catkin.Now, after the workspace configuration we can create ROS programs. Let us consider a version of the “Hello”.
#include <ros / ros<h> int main(int agrc, char **argv) { ros::init(argc, argv, "hello_ros"); ros::NodeHandle nh; ROS_INFO_STREAM("Hello, ROS!"); }
ros/ros.h
is declaration of the ROS classes. It is necessary to connect it in every ROS program you are creating.ros::init
It is a function which initialises the library of the ROS client.ros::NodeHandle
object is the main mechanism that your program will use to interact with ROS system. By creating this object provides registration with your program as a node to the ROS master. ROS_INFO_STREAM
this command generates an information message. This message is going to be sent to several various locations, including the console.
The Arduino and Arduino IDE are great tools for quickly and easily programming hardware. Using the rosserial_arduino package, you can use ROS directly with the Arduino IDE. rosserial
provides a ROS communication protocol that works over your Arduino's UART. It allows your Arduino to be a full-fledged ROS node which can directly publish and subscribe to ROS messages, publishes TF transforms, and get the ROS system time.
Our ROS bindings are implemented as an Arduino library. Like all Arduino libraries, ros_lib works by putting its library implementation into the libraries folder of your sketchbook. If there is not already a libraries folder in your sketchbook, make one. You can then install the library using the instructions below.
In order to use the rosserial libraries in your own code, you must first put:
#include <ros.h>
First, should be done.
#include <std_msgs/String.h>
Otherwise the software will not be able to locate header files.
At first we should install rosserial
for Arduino by the following command
sudo apt-get install ros-kinetic-rosserial-arduino sudo apt-get install ros-kinetic-rosserial
If you use another ROS version just replace kinetic to your release, e.g. hydro
Source build instructions are different for groovy+ (catkin) than for earlier (rosbuild) releases. Select the build system based on your release to see appropriate instructions. Rosserial has been catkin-ized since the groovy release, and the workflow is a bit different from Fuerte and earlier versions. Rather than running the library generator over each package you want to use, you run it once and generate libraries for all installed messages. In the instructions below, <ws> represents your catkin workspace.
cd <ws>/src git clone https://github.com/ros-drivers/rosserial.git cd <ws> catkin_make
These commands clone rosserial from the github repository, generate the rosserial_msgs
needed for communication, and make the ros_lib
library in the <ws>/install
directory.
catkin_make install
. Otherwise, portions of the ros_lib
directory will be missing. It will hopefully be fixed in the future release.
In case if you use previous ROS versions, use:
hg clone https://kforge.ros.org/rosserial/hg rosserial rosmake rosserial_arduino
These commands clone rosserial
from the kforge repository using mercurial, generate the rosserial_msgs
needed for communication and make the ros_lib
library.
The preceding installation steps created ros_lib
, which must be copied into the Arduino build environment to enable Arduino programs to interact with ROS.
In the steps below, <sketchbook> is the directory where the Linux Arduino environment saves your sketches. Typically this is a directory called sketchbook in your home directory. Alternately, you can install into a Windows Arduino environment.
The ros_lib
installation instructions are different for the groovy source (catkin) than for earlier (rosbuild) or binary releases. Be sure you've selected the correct build system above to see appropriate instructions – catkin for a groovy source build, rosbuild otherwise.
Now that you've installed either from source or debs, all you have to do is to copy the rosserial_arduino/libraries
directory into your Arduino sketchbook:
roscd rosserial_arduino/src cp -r ros_lib <sketchbook>/libraries/ros_lib
If you are building Arduino on Windows, copy the ros_lib
directory from Linux to the Windows system's sketchbook/libraries
folder (typically found in My Documents).
After restarting your IDE, you should see ros_lib
listed under examples.
We'll start our exploration into rosserial
by creating a “hello world” program for our Arduino.
If you have followed the Arduino IDE Setup tutorial, you'll be able to open the sketch below by choosing ros_lib → HelloWorld from the Arduino examples menu.
It should open the following code in your IDE:
1 /* 2 * rosserial Publisher Example 3 * Prints "hello world!" 4 */ 5 6 // Use the following line if you have a Leonardo or MKR1000 7 //#define USE_USBCON 8 9 #include <ros.h> 10 #include <std_msgs/String.h> 11 12 ros::NodeHandle nh; 13 14 std_msgs::String str_msg; 15 ros::Publisher chatter("chatter", &str_msg); 16 17 char hello[13] = "hello, ROS!"; 18 19 void setup() 20 { 21 nh.initNode(); 22 nh.advertise(chatter); 23 } 24 25 void loop() 26 { 27 str_msg.data = hello; 28 chatter.publish( &str_msg ); 29 nh.spinOnce(); 30 delay(1000); 31 }
To compile the code, use the compile function within the Arduino IDE. It is no different from compiling any other sketch. Once the compilation is completed, you will receive a message about program storage space and dynamic memory usage. To upload the code to your Arduino, use the upload function within the Arduino IDE. It is no different from uploading any other sketch.
Now, launch roscore
:
roscore
Next, run the rosserial client application that forwards your Arduino messages to the rest of ROS. Make sure to use the correct serial port:
rosrun rosserial_python serial_node.py /dev/ttyUSB0
Alternatively, if you want the ability to programmatically reset your Arduino, run using:
rosrun rosserial_arduino serial_node.py _port:=/dev/ttyUSB0
It will automatically provide a service endpoint at ~reset_arduino that you can call which will have the same effect as pressing the Arduino's reset button.
Finally, watch the greetings come in from your Arduino by launching a new terminal window and entering:
rostopic echo chatter