ROS学习笔记(十一)——ROS试用练习(二)

ROS学习笔记(十一)——ROS试用练习

六、如何使用主题与节点交互
要进行交互并获得主题的信息,可以使用rostopic工具
在terminal里输入

rostopic

你将获得如下输出:

rostopic is a command-line tool for printing information about ROS Topics.

Commands:
    rostopic bw display bandwidth used by topic   
    rostopic delay  display delay of topic from timestamp in header
    rostopic echo   print messages to screen
    rostopic find   find topics by type
    rostopic hz display publishing rate of topic    
    rostopic info   print information about active topic
    rostopic list   list active topics
    rostopic pub    publish data to topic
    rostopic type   print topic or field type

Type rostopic <command> -h for more detailed usage, e.g. 'rostopic echo -h'

现在我们在启动键盘控制“小乌龟”来运动

rosrun turtlesim turtle_teleop_key

这时候你使用rosnode list命令,你将会看到有三个节点在运行:

/rosout
/teleop_turtle
/turtlesim

其中/teleop_turtle节点是键盘控制小乌龟运动的节点
我们来看看/teleop_turtle节点的详细信息

rosnode info /teleop_turtle

你将看到如下输出信息:

Node [/teleop_turtle]
Publications: 
 * /rosout [rosgraph_msgs/Log]
 * /turtle1/cmd_vel [geometry_msgs/Twist]

Subscriptions: None

Services: 
 * /teleop_turtle/get_loggers
 * /teleop_turtle/set_logger_level


contacting node http://your pc name:XXXXX/ ...
Pid: XXXXX
Connections:
 * topic: /rosout
    * to: /rosout
    * direction: outbound
    * transport: TCPROS
 * topic: /turtle1/cmd_vel
    * to: /turtlesim
    * direction: outbound
    * transport: TCPROS

我们可以看到teleop_turtle节点发布一个主题:/turtle1/cmd_vel [geometry_msgs/Twist]
同样,我们再去看看turtlesim节点的详细信息

rosnode info /turtlesim

在输出信息中,你将会看到有这么一行:

Subscriptions: 
 * /turtle1/cmd_vel [geometry_msgs/Twist]

表示teleop_turtle节点把消息发布到/turtle1/cmd_vel [geometry_msgs/Twist]主题,而turtlesim节点订阅了该主题,从而我们可以通过键盘来控制“小乌龟”的运动。

rostopic list
/rosout
/rosout_agg
/turtle1/cmd_vel
/turtle1/color_sensor
/turtle1/pose

通过echo可以查看节点发出的信息:

rostopic echo /turtle1/cmd_vel

此时切换到键盘控制“小乌龟”移动得terminal下,按下控制键
你将会看到类似如下的输出信息:

linear: 
  x: 2.0
  y: 0.0
  z: 0.0
angular: 
  x: 0.0
  y: 0.0
  z: 0.0

通过rostopic type 可以查看主题发送的消息类型:

rostopic type /turtle1/cmd_vel

你将获得如下输出信息:

geometry_msgs/Twist

如果你想查看消息字段,可以使用如下命令:

rosmsg show geometry_msgs/Twist

你将获得如下输出信息:

geometry_msgs/Vector3 linear
  float64 x
  float64 y
  float64 z
geometry_msgs/Vector3 angular
  float64 x
  float64 y
  float64 z

现在我们已经了解了什么是ROS的消息,接下来我们开始结合消息来使用rostopic。
rostopic pub可以把数据发布到当前某个正在广播的话题上。

用法:rostopic pub [topic] [msg_type] [args]
 rostopic pub -1 /turtle1/cmd_vel geometry_msgs/Twist -- '[2.0, 0.0, 0.0]' '[0.0, 0.0, 1.8]'

至于这句话什么意思,详见http://wiki.ros.org/cn/ROS/Tutorials/UnderstandingTopics

未完待续!
2018年3月16日
于杭州
hangzhou

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值