ROS Learning-015 learning_tf(编程) 编写一个监听器程序 (Python版)

本文介绍了在ROS Indigo环境下,如何编写一个Python版的TF监听器程序。程序通过监听广播员发布的坐标信息,使一只小海龟跟随另一只小海龟运动。详细步骤包括创建Python脚本,解释代码,赋予执行权限,编写启动脚本并运行。在终端中运行程序,可以观察到小海龟的跟随效果。

ROS Indigo learning_tf-02 编写一个 监听器 程序 (Python版)

我使用的虚拟机软件:VMware Workstation 11
使用的Ubuntu系统:Ubuntu 14.04.4 LTS
ROS 版本:ROS Indigo


1. 这一节要做的事情:

上一节我们已经编写了一个广播员的程序,下面我们编写一个监听器程序来监听广播员发布的小海龟的坐标系信息:

监听器程序:利用广播员程序广播的坐标信息,在同一个窗口中,有2只小海龟,我们控制 小海龟1 ,让 小海龟2 自动跟随 小海龟1 运行。

2. 程序如何实现:

这个监听器,监听广播员发出的广播(turtle1turtle2 之间坐标系的变化信息),然在同个运算,把结果发布给 turtle2 订阅的 turtle2/cmd_vel 话题上,从而控制 turtle2 运动。

废话少说,开始编程。

2.1. 将路径切换到 learning_tf 软件包,在 nodes 文件夹里创建一个 turtle_tf_listener.py 文件:

$ roscd learning_tf
$ gedit  nodes/turtle_tf_listener.py 

2.2. 将下面的代码添加进去:

#!/usr/bin/env python 

import roslib
roslib.load_manifest('learning_tf')
import rospy
import math
import tf
import geometry_msgs.msg
import turtlesim.srv

if __name__ == '__main__':
   
roslaunch turtle_tf turtle_tf_demo.launch ... logging to /home/jtg/.ros/log/212760ca-8cbd-11f0-bfae-f30f66dfa32a/roslaunch-jtg-niubi-3763.log Checking log directory for disk usage. This may take a while. Press Ctrl-C to interrupt Done checking log file disk usage. Usage is <1GB. started roslaunch server http://jtg-niubi:34237/ SUMMARY ======== PARAMETERS * /rosdistro: noetic * /rosversion: 1.17.4 * /scale_angular: 2.0 * /scale_linear: 2.0 * /turtle1_tf_broadcaster/turtle: turtle1 * /turtle2_tf_broadcaster/turtle: turtle2 NODES / sim (turtlesim/turtlesim_node) teleop (turtlesim/turtle_teleop_key) turtle1_tf_broadcaster (turtle_tf/turtle_tf_broadcaster.py) turtle2_tf_broadcaster (turtle_tf/turtle_tf_broadcaster.py) turtle_pointer (turtle_tf/turtle_tf_listener.py) auto-starting new master process[master]: started with pid [3771] ROS_MASTER_URI=http://localhost:11311 setting /run_id to 212760ca-8cbd-11f0-bfae-f30f66dfa32a process[rosout-1]: started with pid [3781] started core service [/rosout] process[sim-2]: started with pid [3788] process[teleop-3]: started with pid [3789] process[turtle1_tf_broadcaster-4]: started with pid [3790] process[turtle2_tf_broadcaster-5]: started with pid [3792] process[turtle_pointer-6]: started with pid [3796] Reading from keyboard --------------------------- Use arrow keys to move the turtle. 'q' to quit. Traceback (most recent call last): File "/opt/ros/noetic/lib/turtle_tf/turtle_tf_broadcaster.py", line 35, in <module> import rospy File "/opt/ros/noetic/lib/python3/dist-packages/rospy/__init__.py", line 47, in <module> from std_msgs.msg import Header File "/opt/ros/noetic/lib/python3/dist-packages/std_msgs/msg/__init__.py", line 1, in <module> from ._Bool import * File "/opt/ros/noetic/lib/python3/dist-packages/std_msgs/msg/_Bool.py", line 6, in <module> import genpy File "/opt/ros/noetic/lib/python3/dist-packages/genpy/__init__.py", line 34, in <module> from . message i
最新发布
09-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值