5. Hello World ROS - Python

Lập trình các node Talker(publisher) và Listener(subscriber) bằng Python.

Ở đây, chúng tôi tạo một node publisher với tên là “talker”, node này sẽ phát ra message và node subscriber với tên “người nghe”) để nhận message qua topic là “/ chatter”

Talker Node

Chắc chắn rằng bạn đã tạo catkin_ws và thử catkin_make

Bước 1: Tạo hello_world package

$ catkin_create_pkg hello_world rospy std_msgs

Như bạn có thể thấy, chương trình tự động tạo một số tệp bên dưới:

Created file hello_world/package.xml
Created file hello_world/CMakeLists.txt
Created folder hello_world/include/hello_world
Created folder hello_world/src
Successfully created files in /home/<your_name>/catkin_ws/src/hello_world.
Please adjust the values in package.xml.

Sau đó ta cd vào thư mục catkin_ws và compile code bằng lệnh catkin_make

$ cd ~/catkin_ws/
$ catkin_make

Bước 2. Vào thư mục hello_world bằng roscd và tạo file talker.py

roscd hello_world
mkdir scripts
cd scripts
gedit talker.py

Code Python talker.py

#!/usr/bin/env python
# Hello world ROS code from Robotlab.vn 
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

Bước 3. Cho phép file talker.py hoạt động (Permission file talker.py)

$ roscd hello_world 
$ cd script 
$ chmod +x talker.py

Listener Node

Tương tự như vậy các bạn hãy bắt đầu code file listener.py

#!/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
    # name 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()

Nào các bạn chạy các ROS Node thôi nào.

Chạy các Node bằng Python

Bước đầu tiên bạn phải make các package trong catkin_ws

$ cd ~/catkin_ws
$ catkin_make

Mở terminal 1 chạy roscore

roscore

Terminal 2 chạy talker.py

 rosrun hello_world talker.py

Bạn có thể thấy node talker.py bắt đầu publish data

[INFO] [WallTime: 1521785268.935330] hello world 1521785268.94
[INFO] [WallTime: 1521785269.035358] hello world 1521785269.04
[INFO] [WallTime: 1521785269.135298] hello world 1521785269.14

Terminal 3 chạy talker.py

rosrun hello_world listener.py

Node listener bắt đầu nhận dữ liệu từ node talker

[INFO] [WallTime: 1521785358.586366] /listener_14367_1521785357161I heard hello
world 1521785358.58
[INFO] [WallTime: 1521785358.686434] /listener_14367_1521785357161I heard hello
world 1521785358.69
[INFO] [WallTime: 1521785358.786199] /listener_14367_1521785357161I heard hello
world 1521785358.78

Terminal 4 : Mở Sơ đồ các node (Open Node graph)

rqt_graph

Bạn có thể tham khảo giải thích chi tiết code ở link sau:

Last updated