<div class="blogpost-body" id="cnblogs_post_body">
<p>消息类型:</p>
<h3>1. Twist - 线速度角速度</h3>
<p>通常被用于发送到/cmd_vel话题,被base controller节点监听,控制机器人运动</p>
<div class="cnblogs_code">
<pre class="blockcode">geometry_msgs/<span style="color:#000000;">Twist
geometry_msgs</span>/<span style="color:#000000;">Vector3 linear
float64 x
float64 y
float64 z
geometry_msgs</span>/<span style="color:#000000;">Vector3 angular
float64 x
float64 y
float64 z</span></pre>
</div>
<p>linear.x指向机器人前方,linear.y指向左方,linear.z垂直向上满足右手系,平面移动机器人常常linear.y和linear.z均为0</p>
<p>angular.z代表平面机器人的角速度,因为此时z轴为旋转轴</p>
<p>示例:</p>
<div class="cnblogs_code">
<pre class="blockcode"><span style="color:#008000;">#</span><span style="color:#008000;">! /usr/bin/env python</span>
<span style="color:#800000;">'''</span><span style="color:#800000;">
Author: xushangnjlh at gmail dot com
Date: 2017/05/22
@package forward_and_back
</span><span style="color:#800000;">'''</span>
<span style="color:#0000ff;">import</span><span style="color:#000000;"> rospy
</span><span style="color:#0000ff;">from</span> geometry_msgs.msg <span style="color:#0000ff;">import</span><span style="color:#000000;"> Twist
</span><span style="color:#0000ff;">from</span> math <span style="color:#0000ff;">import</span><span style="color:#000000;"> pi
</span><span style="color:#0000ff;">class</span><span style="color:#000000;"> ForwardAndBack():
</span><span style="color:#0000ff;">def</span> <span style="color:#800080;">__init__</span><span style="color:#000000;">(self):
rospy.init_node(</span><span style="color:#800000;">'</span><span style="color:#800000;">forward_and_back</span><span style="color:#800000;">'</span>, anonymous=<span style="color:#000000;">False)
rospy.on_shutdown(self.shutdown)
</span><span style="color:#008000;">#</span><span style="color:#008000;"> this "forward_and_back" node will publish Twist type msgs to /cmd_vel </span>
<span style="color:#008000;">#</span><span style="color:#008000;"> topic, where this node act like a Publisher </span>
self.cmd_vel = rospy.Publisher(<span style="color:#800000;">'</span><span style="color:#800000;">/cmd_vel</span><span style="color:#800000;">'</span>, Twist, queue_size=5<span style="color:#000000;">)
</span><span style="color:#008000;">#</span><span style="color:#008000;"> parameters</span>
rate = 50<span style="color:#000000;">
r </span>=<span style="color:#000000;"> rospy.Rate(rate)
linear_speed </span>= 0.2<span style="color:#000000;">
goal_distance </span>= 5<span style="color:#000000;">
linear_duration </span>= goal_distance/<span style="color:#000000;">linear_speed
angular_speed </span>= 1.0<span style="color:#000000;">
goal_angular </span>=<span style="color:#000000;"> pi
angular_duration </span>= goal_angular/<span style="color:#000000;">angular_speed
</span><span style="color:#008000;">#</span><span style="color:#008000;"> forward->rotate->back->rotate</span>
<span style="color:#0000ff;">for</span> i <span style="color:#0000ff;">in</span> range(2<span style="color:#000000;">):
</span><span style="color:#008000;">#</span><span style="color:#008000;"> this is the msgs variant, has Twist type, no data now</span>
move_cmd =<span style="color:#000000;"> Twist()
move_cmd.linear.x </span>= linear_speed <span style="color:#008000;">#</span>
<span style="color:#008000;">#</span><span style="color:#008000;"> should keep publishing</span>
ticks = int(linear_duration*<span style="color:#000000;">rate)
</span><span style="color:#0000ff;">for</span> t <span style="color:#0000ff;">in</span><span style="color:#000000;"> range(ticks):
</span><span style="color:#008000;">#</span><span style="color:#008000;"> one node can publish msgs to different topics, here only publish</span>
<span style="color:#008000;">#</span><span style="color:#008000;"> to /cmd_vel</span>
<span style="color:#000000;"> self.cmd_vel.publish(move_cmd)
r.sleep </span><span style="color:#008000;">#</span><span style="color:#008000;"> sleep according to the rate</span>
<span style="color:#008000;">#</span><span style="color:#008000;"> stop 1 ms before ratate</span>
move_cmd =<span style="color:#000000;"> Twist()
self.cmd_vel.publish(move_cmd)
rospy.sleep(</span>1<span style="color:#000000;">)
move_cmd.angular_speed.z </span>=<span style="color:#000000;"> angular_speed
ticks </span>= int(angular_duration*<span style="color:#000000;">rate)
</span><span style="color:#0000ff;">for</span> t <span style="color:#0000ff;">in</span><span style="color:#000000;"> range(ticks):
self.cmd_vel.publish(move_cmd)
r.sleep()
self.cmd_vel.publish(Twist())
rospy.sleep(</span>1<span style="color:#000000;">)
</span><span style="color:#0000ff;">def</span><span style="color:#000000;"> shutdown(self):
rospy.loginfo(</span><span style="color:#800000;">"</span><span style="color:#800000;">Stopping the robot</span><span style="color:#800000;">&# |
|