用Python实现ROS节点(编写简单的消息发布器和订阅器)

https://blog.csdn.net/sk950425/article/details/81737170

编写一个简单的发布器和订阅器

1.创建工作空间

建立文件夹hello_rospy,再在该目录下建立子目录src,cd到该src目录,运行如下命令创建工作包

catkin_create_pkg beginner_tutorials std_msgs rospy roscpp

2.编写发布器和订阅器

进入之前创建的beginner_tutorials包

roscd beginner_tutorials

创建scripts目录存放Python代码:

mkdir scripts
cd scripts

下载例子脚本talker.py到scripts目录,并修改权限为可执行(发布节点):

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

下载listener.py到scripts目录,并修改权限为可执行(订阅节点):

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

构建节点:

cd ~/hello_rospy
catkin_make

3.测试发布器和订阅器

启动发布器:

打开终端,输入:

roscore

确保roscore可用。

打开新的终端,输入:

如果使用catkin,确保你在调用catkin_make后,在运行你自己的程序前,已经source了catkin工作空间下的setup.sh文件

cd ~/hello_rospy
source ./devel/setup.bash
rosrun beginner_tutorials talker.py

此时你会看到如下信息:

[INFO] [1534388269.223655]: hello world 1534388269.2233906
[INFO] [1534388269.323618]: hello world 1534388269.3233504
[INFO] [1534388269.423586]: hello world 1534388269.4233208
[INFO] [1534388269.523613]: hello world 1534388269.5233488
[INFO] [1534388269.623685]: hello world 1534388269.6233852
[INFO] [1534388269.723714]: hello world 1534388269.7234194
[INFO] [1534388269.823649]: hello world 1534388269.8233697
[INFO] [1534388269.923647]: hello world 1534388269.9233446
[INFO] [1534388270.023622]: hello world 1534388270.023337
[INFO] [1534388270.123665]: hello world 1534388270.1233444
[INFO] [1534388270.223655]: hello world 1534388270.2233653
[INFO] [1534388270.323668]: hello world 1534388270.3233645
[INFO] [1534388270.423637]: hello world 1534388270.423357
[INFO] [1534388270.523678]: hello world 1534388270.5233543
[INFO] [1534388270.623646]: hello world 1534388270.6233633
[INFO] [1534388270.723670]: hello world 1534388270.723355
[INFO] [1534388270.823638]: hello world 1534388270.8233557
[INFO] [1534388270.923635]: hello world 1534388270.9233434
[INFO] [1534388271.023646]: hello world 1534388271.0233815

发布器节点已经启动运行。现在需要一个订阅器节点来接受发布的消息。

启动订阅器:

打开新的终端,输入:

cd ~/hello_rospy
source ./devel/setup.bash
rosrun beginner_tutorials listener.py

此时你会看到如下信息:

[INFO] [1534388451.225271]: /listener_32238_1534388451115I heard hello world 1534388451.2233224
[INFO] [1534388451.325214]: /listener_32238_1534388451115I heard hello world 1534388451.3234394
[INFO] [1534388451.425159]: /listener_32238_1534388451115I heard hello world 1534388451.4234073
[INFO] [1534388451.525082]: /listener_32238_1534388451115I heard hello world 1534388451.5234268
[INFO] [1534388451.624949]: /listener_32238_1534388451115I heard hello world 1534388451.6233602
[INFO] [1534388451.724969]: /listener_32238_1534388451115I heard hello world 1534388451.723355
[INFO] [1534388451.825018]: /listener_32238_1534388451115I heard hello world 1534388451.823384
[INFO] [1534388451.925162]: /listener_32238_1534388451115I heard hello world 1534388451.923369
[INFO] [1534388452.025092]: /listener_32238_1534388451115I heard hello world 1534388452.0234113
[INFO] [1534388452.125054]: /listener_32238_1534388451115I heard hello world 1534388452.1233687

至此,全部完成。

你可能感兴趣的:(ubuntu,ROS)