Skip to content

2.2 Course Notes

Mehmet Efe Tiryaki edited this page Nov 21, 2018 · 1 revision

ROS Warm up

Launch a terminal and type

roscore

Then open two more terminals and type followings in each

rosrun  turtlesim turtlesim_node
rosrun turtlesim turtle_teleop_key

Once you click on the terminal with "turtle_teleop_key", you should be able to move the turtle using arrow keys.

rosnode

Launch another terminal and check the existing nodes using

rosnode list

for detailed overview of the node

rosnode info {node_name}

above command displays the topics(msgs) published by this node, the topics subscribed by this node, the services owned by this node and this node's connection with other nodes. This command is very helpful to check if your node is publishing/subscribing a certain topic.

Rqt graph

Finally you can also use rqt UI to see these information

rosrun rqt_graph rqt_graph 

ROS topic/service

rostopic

Let's check the topics and services currently in use. Launch another terminal while "roscore" and "turtlesim" still running in other terminals, and type

rostopic list

This command will show you the topics registered to your ROS master by any currently running publisher or subscriber node. You can check which node publishes or which node is subscribes any of topics by using

rostopic info {topic_name}

Beside publishers and subscribers, this command also shows you the type of the topic(msg). If you want to further investigate the published topic, you can use

rostopic hz {topic_name}

to check the publication frequency of the topic, and you can use

rostopic echo {topic_name}

to screen the published topic real time in your terminal. The "echo" option is especially useful when it comes to debugging you code. Finally you can also publish any topic through terminal with

rostopic pub {topic_name} {msg_type} {msg_content}

You don't need to know msg_type and msg_content by heart to use this command. After you type in topic_name, if you press "tab" key two times, the {msg_type} and {msg_content} will automatically appear in your terminal. Then you can change the {msg_content} as you wish. You can try to publish "/turtle1/cmd_vel" and observer how the "turtlesim" react your msg.

rosservice

rosservice list

rosmsg

rossrv

Catkin tools and Git

If you haven't installed "catkin tools" and "git" yet. Please go to catkin and git installation pages.

After you install catkin and git. First create a catkin workspace.

 cd ~
 mkdir catkin_ws
 mkdir catkin_ws/src

Second, initialize your workspace. It is very important that you initialize your workspace when you are in side of the your "catkin_ws". If you initialize when you are not in your "catkin_ws" this will cause a major problem later.

cd ~/catkin_ws
catkin init 

Clone our git repository into your "src" directory

cd ~/catkin_ws/src
git clone https://github.com/Sadetra/robotic_tutorial.git

Then build it

catkin build robotic_tutorial

Writing your own node

Publisher/Subscriber

Following code is a simple publisher node example (sender.py).

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

def send():
    rospy.init_node('sender',anonymous=True)
    publisher = rospy.Publisher('message',String,queue_size=10)
    #publisher = rospy.Publisher('message',Float64,queue_size=10)
    rate = rospy.Rate(10)
    while not rospy.is_shutdown():
        publisher.publish("[sender] : time is " + str(rospy.get_time()))
        #publisher.publish(rospy.get_time())
        rate.sleep()

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

In this code, we only have a send method which publishes current time in string format at every 0.1 sec. Let's break the code in pieces and study the line by line.

  1. When we run this code python will first import "rospy"(ros interface for python) and some msg types included in "std_msg".
  2. Then it will run the if statement at the bottom of the code, in which we have a try/except block. We need this block to catch errors which might cause during the communication(you don't really need it)
  3. In side of the try/except block, we call our send method. We first initialize our node using
rospy.init_node('sender',anonymous=True)

after running this line, you should see your node "senderXXXXX" in terminal, when you type "rosnode list". The numbers at the end are because of "anonymous" flag. If you make it "False", then numbers disappear. 4. Afterward, we initialize our "publisher" object with message name "message" and msg type "String" and queue_size 10 (if you don't send thousands of message per second, keep the queue_size small)

publisher = rospy.Publisher('message',String,queue_size=10)
  1. Initialize the rate object which will be responsible for loop timing of you node. In this example, we choose the rate as 10Hz (10 loop per sec). Note that since python is not really a multi thread programming language if you increase the rate too much, rate will fail to reach the loop periods in real time applications (if real time performance is important for you, go for c++ and roscpp).
rate = rospy.Rate(10)
  1. In the while loop, we get the time from rospy using
rospy.get_time())

then we publish it with

publisher.publish("[sender] : time is " + str(rospy.get_time()))

in "String" format. Later, we complete the loop using rate object

rate.sleep()

Following code is a simple subscriber node example (receiver.py).

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

def callback(data):
    rospy.loginfo(data.data)

def receiver():
    rospy.init_node('receiver', anonymous=True)
    rospy.Subscriber("message", String, callback)
    #rospy.Subscriber("message", Float64, callback)
    rospy.spin()

if __name__ == '__main__':
    receiver()

Service/Client

rosparam

Building a catkin package

CMakelist

Package

setup.py

roscd

rosrun

roslaunch

arg

rosparam

if/unless

remap

namespace

Custom Msg/Srv

custom msg

custom srv

Exercise

Clone this wiki locally