ROS installation and setup for Raspberry Pi - mktk1117/six_wheel_robot GitHub Wiki

How to use ROS on Raspberry Pi

Here, we install ROS(Robot Operating System) to Raspberry Pi and make a simple publisher and subscriber on catkin_ws.

ROS (Robot Operating System) provides libraries and tools to help software developers create robot applications. It provides hardware abstraction, device drivers, libraries, visualizers, message-passing, package management, and more. ROS is licensed under an open source, BSD license.

If you are not familiar with ROS, please read the Start guide and Tutorials first.

Prerequisite

Installation

It can be installed by following Ubuntu ARM install of ROS Indigo

Setup catkin

After you installed ROS, next step is setting up catkin workspace.
Let's make catkin workspace by following Creating a workspace for catkin.

Write a simple code to send and receive messages

Creating a catkin Package

Then, let's make a new package on catkin_ws. (Creating a ROS Package)

After creating a catkin_ws, move to src folder.

# You should have created this in the Creating a Workspace Tutorial
$ cd ~/catkin_ws/src

Now use the catkin_create_pkg script to create a new package called 'beginner_tutorials' which depends on std_msgs, roscpp, and rospy:

$ catkin_create_pkg beginner_tutorials std_msgs rospy roscpp

This will create a beginner_tutorials folder which contains a package.xml and a CMakeLists.txt, which have been partially filled out with the information you gave catkin_create_pkg.

Building a catkin workspace and sourcing the setup file

Now you need to build the packages in the catkin workspace:

$ cd ~/catkin_ws
$ catkin_make

To add the workspace to your ROS environment you need to source the generated setup file:

$ . ~/catkin_ws/devel/setup.bash

It would be convenient to add this command to your ~/.bashrc

$ echo 'source ~/catkin_ws/devel/setup.bash' >> ~/.bashrc

Now, you have made a new package 'beginer_tutorial'
Please see Creating a ROS Package for more detailed information.

Write a Simple Publisher and Subscriber(Python)

Next step is to make a simple program that send messages and receive messages. Writing a Simple Publisher and Subscriber (Python)

Writing the Publisher Node

"Node" is the ROS term for an executable that is connected to the ROS network. Here we'll create the publisher ("talker") node which will continually broadcast a message.

Change directory into the beginner_tutorials package

$ roscd beginner_tutorials

The Code

First lets create a 'scripts' folder to store our Python scripts in:

$ mkdir scripts
$ cd scripts

Then download the example script talker.py to your new scripts directory and make it executable:

$ wget https://raw.github.com/ros/ros_tutorials/kinetic-devel/rospy_tutorials/001_talker_listener/talker.py
$ chmod +x talker.py

You can view and edit the file with $ rosed beginner_tutorials talker.py or just look below.

#!/usr/bin/env python
# license removed for brevity
import rospy
from std_msgs.msg import String

def talker():
    pub = rospy.Publisher('chatter', String, queue_size=10)
    rospy.init_node('talker', anonymous=True)
    rate = rospy.Rate(10) # 10hz
    while not rospy.is_shutdown():
        hello_str = "hello world %s" % rospy.get_time()
        rospy.loginfo(hello_str)
        pub.publish(hello_str)
        rate.sleep()

if __name__ == '__main__':
    try:
        talker()
    except rospy.ROSInterruptException:
        pass

See the explanation of this code here

Writing the Subscriber Node

The Code

Download the listener.py file into your scripts directory:

$ roscd beginner_tutorials/scripts/
$ wget https://raw.github.com/ros/ros_tutorials/kinetic-devel/rospy_tutorials/001_talker_listener/listener.py

Don't forget to make the node executable:

$ chmod +x listener.py

The code looks like

#!/usr/bin/env python
import rospy
from std_msgs.msg import String

def callback(data):
    rospy.loginfo(rospy.get_caller_id() + "I heard %s", data.data)
    
def listener():

    # In ROS, nodes are uniquely named. If two nodes with the same
    # node are launched, the previous one is kicked off. The
    # anonymous=True flag means that rospy will choose a unique
    # name for our 'listener' node so that multiple listeners can
    # run simultaneously.
    rospy.init_node('listener', anonymous=True)

    rospy.Subscriber("chatter", String, callback)

    # spin() simply keeps python from exiting until this node is stopped
    rospy.spin()

if __name__ == '__main__':
    listener()

See the explanation of this code here again.

Building your nodes

We use CMake as our build system and, yes, you have to use it even for Python nodes. This is to make sure that the autogenerated Python code for messages and services is created.

Go to your catkin workspace and run catkin_make:

$ cd ~/catkin_ws
$ catkin_make

#Examining the Simple Publisher and Subscriber

Running the Publisher

Make sure that a roscore is up and running:

$ roscore

In the last tutorial we made a publisher called "talker". Let's run it:

$ rosrun beginner_tutorials talker.py

You will see something similar to:

[INFO] [WallTime: 1314931831.774057] hello world 1314931831.77
[INFO] [WallTime: 1314931832.775497] hello world 1314931832.77
[INFO] [WallTime: 1314931833.778937] hello world 1314931833.78
[INFO] [WallTime: 1314931834.782059] hello world 1314931834.78
[INFO] [WallTime: 1314931835.784853] hello world 1314931835.78
[INFO] [WallTime: 1314931836.788106] hello world 1314931836.79

Running the Subscriber

In the last tutorial we made a subscriber called "listener". Let's run it:

$ rosrun beginner_tutorials listener.py

You will see something similar to:

[INFO] [WallTime: 1314931969.258941] /listener_17657_1314931968795I heard hello world 1314931969.26
[INFO] [WallTime: 1314931970.262246] /listener_17657_1314931968795I heard hello world 1314931970.26
[INFO] [WallTime: 1314931971.266348] /listener_17657_1314931968795I heard hello world 1314931971.26
[INFO] [WallTime: 1314931972.270429] /listener_17657_1314931968795I heard hello world 1314931972.27
[INFO] [WallTime: 1314931973.274382] /listener_17657_1314931968795I heard hello world 1314931973.27
[INFO] [WallTime: 1314931974.277694] /listener_17657_1314931968795I heard hello world 1314931974.28
[INFO] [WallTime: 1314931975.283708] /listener_17657_1314931968795I heard hello world 1314931975.28

Now that you have examined the simple publisher and subscriber