What does IoT need?
It’s clear that in the future we will have many devices that needs Internet to their work in our regular live. While devices are becoming more intelligent, the degree of automatisation is increasing. Today devices use Internet to find or storage 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 the Robot Operating Systems 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.
Why ROS?
Firstly, ROS is the most popular education platform in 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 physical side of your device and just write code to it or even your own linux core. Linux is the best OS for embedded systems.
ROS Installation
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. Set up your keys
sudo apt-key adv --keyserver hkp://ha.pool.sks-keyservers.net:80 --recv-key 421C365BD9FF1F717815A3895523BAEEB01FA116
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 commandline tools.
After that, lets instal turtlesim. This package illustrate 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 command:
rosdep
The goal of this command is to initialize 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.
Account Configuration 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 own account, not using sudo.
Turtlesim example Lets start ROS programming with a simple example. This exercise serve a a few purposes: it provides opportunity to check that ROS is installed correctly, introduce turtlesim simulator and it is used in a lot of tutorials published on web.
Starting turtlesim
roscore rosrun turtlesim 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. (Note, the appearance of your turtle may be different. It depends from the version of ROS). 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 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 to 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 severel independent workspaces. Now, lets set up workspace. Create a 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.
Hello, ROS! 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!"); }
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, publish 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.
Setting up the software 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
Installing from Source onto the ROS workstation
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 releases. 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.
Note: currently you HAVE to run catkin_make install, otherwise portions of the ros_lib directory will be missing. This will hopefully be fixed soon.
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.
=====ros_lib installing to arduino Environment 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.
Ros_lib installation instructions are different for 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 sytem's sketchbook/libraries folder (typically found in My Documents).
*Note: Currently you can install the Arduino libaries directly in the Arduino IDE. Just open the Library Manager from the IDE menu in Sketch → Include Library → Manage Library. Then search for “rosserial”. This is useful if you need to work on an Arduino sketch but don't want to setup a full ROS workstation.
After restarting your IDE, you should see ros_lib listed under examples.
Publisher example We'll start our exploration into rosserial by creating a “hello world” program for our Arduino. (Note: the Arduino community often calls source code for programs a “sketch”, we will use the same convention below). 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.
This 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 }
The code compiling and running To compile the code, use the compile function within the Arduino IDE. This is no different from uploading 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. This 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
This 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
The iot_bridge provides a connection between ROS and any smart device of the control system. Since 2017, the package provides bridging with openHAB, one of the most actively developed and used framework for home automation.
How can we use it? *
Using iot-bridge technology any OpenHAB device can be easily setup to publish updates to the iot_updates topic in ROS, giving a ROS robot knowledge of any Home Automation device. ROS can publish to the iot_set topic (or iot_command) and the device in OpenHAB will be set to the new value (or act on the specified command).
Installing iot_bridge
At first, we should install and configure openHAB. For details and guides see the OpenHAB oficial website. Many people find that the simplest way to experiment with openHAB is to get a Raspberry Pi and install openHABian - the “hassle-free openHAB setup”. While openHABian offers a streamlined and simplified way to get up and running quickly, it is a complete openHAB home automation system capable of automating your entire home [4].
Just in few words, lets consider the OpenHAB installation. Configure the repository in your Ubuntu OS:
wget -qO - 'https://bintray.com/user/downloadSubjectPublicKey?username=openhab' | sudo apt-key add - echo "deb http://dl.bintray.com/openhab/apt-repo stable main" | sudo tee /etc/apt/sources.list.d/openhab.list sudo apt-get update
After this, we can install the runtime:
sudo apt-get install openhab-runtime
After installation, you should configure the runtime. See the following guide Configuring-the-openHAB-runtime.
Now, we should install iot-bridge on the ROS system:
cd catkin_ws/src git clone address-from-above cd .. catkin_make
3. Edit iot_bridge/config/items.yaml * Unordered List Itemupdate host address and port to match your OpenHAB server). - Ordered List ItemEdit Openhab's item file * Create the ROS group: Group ROS (All) * Add the (ROS) group to each item that should send status updates to ROS.
add ROS_Status to openhab's item file
. Sample Open_HAB item definition:
NOTE: you must have two or more items defined in the ROS group.
Group ROS (All) String ROS_Status "ROS [%s]" Switch Light_GF_Corridor_Ceiling "Ceiling" (GF_Corridor, Lights, ROS) Switch Light_GF_Bathroom (GF_Bathroom, Lights, ROS)
Running
1. Run the iot-bridge by
roslaunch iot_bridge iot.launch
2. Testing.
Follow steps to check receiving from OpenHAB.
rostopic echo /iot_updates
Follow next steps to check sending to OpenHAB:
cd catkin_ws/src/iot_bridge/scripts ./iot_test item_name item_value
Text item=ROS_Status label="ROS [%s]"
Set status for an OpenHAB Item ROS topic subscribed by iot_bridge:
//iot_set (diagnostic_msgs/KeyValue)//
When the iot_bridge receives a name/value pair from the ROS iot_set topic, it publishes those to OpenHAB and OpenHAB updates the status for the item specified (e.g. indicate that a switch is now ON). For instance: A ROS program running Facial Detection detects that Sarah is present. It publishes the following to the iot_set topic:
This will set the facedetection item in OpenHAB to Sarah, indicating Sarah has been detected. See sample code in iot_test. Receive item updates from OpenHAB ROS topic published by iot_bridge: iot_updates (diagnostic_msgs/KeyValue) The IoT bridge receives updates from OpenHAB and publishes those as name/value pairs to the iot_updates ROS topic.
To see updates from the command line, type:
rostopic echo iot_updates
For example: A motion detector is triggered in OpenHAB. The openhab bridge will publish the following to the iot_updates topic in ROS
openhab will send the current status of every item in the (ROS) group to the iot_updates topic. Subsequent status will continue to send only when they change.