ROS Twist和Odometry消息类型使用(Python)

论坛 期权论坛     
匿名技术用户   2021-1-11 21:54   247   0
<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;">&#39;&#39;&#39;</span><span style="color:#800000;">
Author: xushangnjlh at gmail dot com
Date: 2017/05/22

&#64;package forward_and_back
</span><span style="color:#800000;">&#39;&#39;&#39;</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;">&#39;</span><span style="color:#800000;">forward_and_back</span><span style="color:#800000;">&#39;</span>, anonymous&#61;<span style="color:#000000;">False)
    rospy.on_shutdown(self.shutdown)
    </span><span style="color:#008000;">#</span><span style="color:#008000;"> this &#34;forward_and_back&#34; 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 &#61; rospy.Publisher(<span style="color:#800000;">&#39;</span><span style="color:#800000;">/cmd_vel</span><span style="color:#800000;">&#39;</span>, Twist, queue_size&#61;5<span style="color:#000000;">)
   
    </span><span style="color:#008000;">#</span><span style="color:#008000;"> parameters</span>
    rate &#61; 50<span style="color:#000000;">
    r </span>&#61;<span style="color:#000000;"> rospy.Rate(rate)
   
    linear_speed </span>&#61; 0.2<span style="color:#000000;">
    goal_distance </span>&#61; 5<span style="color:#000000;">
    linear_duration </span>&#61; goal_distance/<span style="color:#000000;">linear_speed
   
    angular_speed </span>&#61; 1.0<span style="color:#000000;">
    goal_angular </span>&#61;<span style="color:#000000;"> pi
    angular_duration </span>&#61; goal_angular/<span style="color:#000000;">angular_speed
   
    </span><span style="color:#008000;">#</span><span style="color:#008000;"> forward-&gt;rotate-&gt;back-&gt;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 &#61;<span style="color:#000000;"> Twist()

      move_cmd.linear.x </span>&#61; linear_speed <span style="color:#008000;">#</span>
      <span style="color:#008000;">#</span><span style="color:#008000;"> should keep publishing</span>
      ticks &#61; 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 &#61;<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>&#61;<span style="color:#000000;"> angular_speed
      ticks </span>&#61; 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;">&#34;</span><span style="color:#800000;">Stopping the robot</span><span style="color:#800000;">&#
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP