原创博客:转载请表明出处:http://www.cnblogs.com/zxouxuewei/

1.到目前为止,我们已经从命令行移动机器人,但大多数时间你将依靠一个ros节点发布适当的Twist消息。作为一个简单的例子,假设你想让你的机器人向前移动一个1米大约180度,然后回到起点。我们将尝试完成这项任务,这将很好地说明不同层次的ros运动控制。

启动tulterbot机器人:

  1. roslaunch rbx1_bringup fake_turtlebot.launch

2.在rviz视图窗口查看机器人:

  1. rosrun rviz rviz -d `rospack find rbx1_nav`/sim.rviz

3.运行timed_out_and_back.py节点:

  1. rosrun rbx1_nav timed_out_and_back.py

4.通过rqt_graph查看消息订阅的框图:

  1. rosrun rqt_graph rqt_graph

5.分析timed_out_and_back.py节点代码:

  1. #!/usr/bin/env python
  2.  
  3. import rospy
  4. from geometry_msgs.msg import Twist
  5. from math import pi
  6.  
  7. class OutAndBack():
  8. def __init__(self):
  9. # Give the node a name
  10. rospy.init_node('out_and_back', anonymous=False)
  11. # Set rospy to execute a shutdown function when exiting
  12. rospy.on_shutdown(self.shutdown)
  13.  
  14. # Publisher to control the robot's speed
  15. self.cmd_vel = rospy.Publisher('/cmd_vel', Twist, queue_size=)
  16.  
  17. # How fast will we update the robot's movement?
  18. rate =
  19.  
  20. # Set the equivalent ROS rate variable
  21. r = rospy.Rate(rate)
  22.  
  23. # Set the forward linear speed to 0.2 meters per second
  24. linear_speed = 0.2
  25.  
  26. # Set the travel distance to 1.0 meters
  27. goal_distance = 1.0
  28.  
  29. # How long should it take us to get there?
  30. linear_duration = goal_distance / linear_speed
  31.  
  32. # Set the rotation speed to 1.0 radians per second
  33. angular_speed = 1.0
  34.  
  35. # Set the rotation angle to Pi radians ( degrees)
  36. goal_angle = pi
  37.  
  38. # How long should it take to rotate?
  39. angular_duration = goal_angle / angular_speed
  40.  
  41. # Loop through the two legs of the trip
  42. for i in range():
  43. # Initialize the movement command
  44. move_cmd = Twist()
  45.  
  46. # Set the forward speed
  47. move_cmd.linear.x = linear_speed
  48.  
  49. # Move forward for a time to go the desired distance
  50. ticks = int(linear_duration * rate)
  51.  
  52. for t in range(ticks):
  53. self.cmd_vel.publish(move_cmd)
  54. r.sleep()
  55.  
  56. # Stop the robot before the rotation
  57. move_cmd = Twist()
  58. self.cmd_vel.publish(move_cmd)
  59. rospy.sleep()
  60.  
  61. # Now rotate left roughly degrees
  62.  
  63. # Set the angular speed
  64. move_cmd.angular.z = angular_speed
  65.  
  66. # Rotate for a time to go degrees
  67. ticks = int(goal_angle * rate)
  68.  
  69. for t in range(ticks):
  70. self.cmd_vel.publish(move_cmd)
  71. r.sleep()
  72.  
  73. # Stop the robot before the next leg
  74. move_cmd = Twist()
  75. self.cmd_vel.publish(move_cmd)
  76. rospy.sleep()
  77.  
  78. # Stop the robot
  79. self.cmd_vel.publish(Twist())
  80.  
  81. def shutdown(self):
  82. # Always stop the robot when shutting down the node.
  83. rospy.loginfo("Stopping the robot...")
  84. self.cmd_vel.publish(Twist())
  85. rospy.sleep()
  86.  
  87. if __name__ == '__main__':
  88. try:
  89. OutAndBack()
  90. except:
  91. rospy.loginfo("Out-and-Back node terminated.")

6.等以上节点运行完成后。可以运行下一个节点;

  1. rosrun rbx1_nav nav_square.py

查看节点订阅框图:

7.分析nav_square.py节点的源码:

  1. #!/usr/bin/env python
  2.  
  3. import rospy
  4. from geometry_msgs.msg import Twist, Point, Quaternion
  5. import tf
  6. from rbx1_nav.transform_utils import quat_to_angle, normalize_angle
  7. from math import radians, copysign, sqrt, pow, pi
  8.  
  9. class NavSquare():
  10. def __init__(self):
  11. # Give the node a name
  12. rospy.init_node('nav_square', anonymous=False)
  13.  
  14. # Set rospy to execute a shutdown function when terminating the script
  15. rospy.on_shutdown(self.shutdown)
  16.  
  17. # How fast will we check the odometry values?
  18. rate =
  19.  
  20. # Set the equivalent ROS rate variable
  21. r = rospy.Rate(rate)
  22.  
  23. # Set the parameters for the target square
  24. goal_distance = rospy.get_param("~goal_distance", 1.0) # meters
  25. goal_angle = rospy.get_param("~goal_angle", radians()) # degrees converted to radians
  26. linear_speed = rospy.get_param("~linear_speed", 0.2) # meters per second
  27. angular_speed = rospy.get_param("~angular_speed", 0.7) # radians per second
  28. angular_tolerance = rospy.get_param("~angular_tolerance", radians()) # degrees to radians
  29.  
  30. # Publisher to control the robot's speed
  31. self.cmd_vel = rospy.Publisher('/cmd_vel', Twist, queue_size=)
  32.  
  33. # The base frame is base_footprint for the TurtleBot but base_link for Pi Robot
  34. self.base_frame = rospy.get_param('~base_frame', '/base_link')
  35.  
  36. # The odom frame is usually just /odom
  37. self.odom_frame = rospy.get_param('~odom_frame', '/odom')
  38.  
  39. # Initialize the tf listener
  40. self.tf_listener = tf.TransformListener()
  41.  
  42. # Give tf some time to fill its buffer
  43. rospy.sleep()
  44.  
  45. # Set the odom frame
  46. self.odom_frame = '/odom'
  47.  
  48. # Find out if the robot uses /base_link or /base_footprint
  49. try:
  50. self.tf_listener.waitForTransform(self.odom_frame, '/base_footprint', rospy.Time(), rospy.Duration(1.0))
  51. self.base_frame = '/base_footprint'
  52. except (tf.Exception, tf.ConnectivityException, tf.LookupException):
  53. try:
  54. self.tf_listener.waitForTransform(self.odom_frame, '/base_link', rospy.Time(), rospy.Duration(1.0))
  55. self.base_frame = '/base_link'
  56. except (tf.Exception, tf.ConnectivityException, tf.LookupException):
  57. rospy.loginfo("Cannot find transform between /odom and /base_link or /base_footprint")
  58. rospy.signal_shutdown("tf Exception")
  59.  
  60. # Initialize the position variable as a Point type
  61. position = Point()
  62.  
  63. # Cycle through the four sides of the square
  64. for i in range():
  65. # Initialize the movement command
  66. move_cmd = Twist()
  67.  
  68. # Set the movement command to forward motion
  69. move_cmd.linear.x = linear_speed
  70.  
  71. # Get the starting position values
  72. (position, rotation) = self.get_odom()
  73.  
  74. x_start = position.x
  75. y_start = position.y
  76.  
  77. # Keep track of the distance traveled
  78. distance =
  79.  
  80. # Enter the loop to move along a side
  81. while distance < goal_distance and not rospy.is_shutdown():
  82. # Publish the Twist message and sleep cycle
  83. self.cmd_vel.publish(move_cmd)
  84.  
  85. r.sleep()
  86.  
  87. # Get the current position
  88. (position, rotation) = self.get_odom()
  89.  
  90. # Compute the Euclidean distance from the start
  91. distance = sqrt(pow((position.x - x_start), ) +
  92. pow((position.y - y_start), ))
  93.  
  94. # Stop the robot before rotating
  95. move_cmd = Twist()
  96. self.cmd_vel.publish(move_cmd)
  97. rospy.sleep(1.0)
  98.  
  99. # Set the movement command to a rotation
  100. move_cmd.angular.z = angular_speed
  101.  
  102. # Track the last angle measured
  103. last_angle = rotation
  104.  
  105. # Track how far we have turned
  106. turn_angle =
  107.  
  108. # Begin the rotation
  109. while abs(turn_angle + angular_tolerance) < abs(goal_angle) and not rospy.is_shutdown():
  110. # Publish the Twist message and sleep cycle
  111. self.cmd_vel.publish(move_cmd)
  112.  
  113. r.sleep()
  114.  
  115. # Get the current rotation
  116. (position, rotation) = self.get_odom()
  117.  
  118. # Compute the amount of rotation since the last lopp
  119. delta_angle = normalize_angle(rotation - last_angle)
  120.  
  121. turn_angle += delta_angle
  122. last_angle = rotation
  123.  
  124. move_cmd = Twist()
  125. self.cmd_vel.publish(move_cmd)
  126. rospy.sleep(1.0)
  127.  
  128. # Stop the robot when we are done
  129. self.cmd_vel.publish(Twist())
  130.  
  131. def get_odom(self):
  132. # Get the current transform between the odom and base frames
  133. try:
  134. (trans, rot) = self.tf_listener.lookupTransform(self.odom_frame, self.base_frame, rospy.Time())
  135. except (tf.Exception, tf.ConnectivityException, tf.LookupException):
  136. rospy.loginfo("TF Exception")
  137. return
  138.  
  139. return (Point(*trans), quat_to_angle(Quaternion(*rot)))
  140.  
  141. def shutdown(self):
  142. # Always stop the robot when shutting down the node
  143. rospy.loginfo("Stopping the robot...")
  144. self.cmd_vel.publish(Twist())
  145. rospy.sleep()
  146.  
  147. if __name__ == '__main__':
  148. try:
  149. NavSquare()
  150. except rospy.ROSInterruptException:
  151. rospy.loginfo("Navigation terminated.")

通过ros节点发布Twist Messages控制机器人--10的更多相关文章

  1. ROS 多台计算机联网控制机器人

    0. 时间同步 sudo apt-get install chrony 1. ubuntu自带的有openssh-client 可以通过如下指令 ssh username@host 来连接同一局域网内 ...

  2. ROS主题发布订阅控制真实的机器人下位机

    先模拟控制小乌龟 新建cmd_node.ccpp文件: #include"ros/ros.h" #include"geometry_msgs/Twist.h" ...

  3. ROS中发布激光扫描消息

    激光雷达工作时会先在当前位置发出激光并接收反射光束,解析得到距离信息,而后激光发射器会转过一个角度分辨率对应的角度再次重复这个过程.限于物理及机械方面的限制,激光雷达通常会有一部分“盲区”.使用激光雷 ...

  4. ROS学习笔记三(理解ROS节点)

    要求已经在Linux系统中安装一个学习用的ros软件包例子: sudo apt-get install ros-indigo-ros-tutorials ROS图形概念概述 nodes:节点,一个节点 ...

  5. ROS节点理解--5

    理解 ROS节点(原创博文,转载请标明出处--周学伟http://www.cnblogs.com/zxouxuewei/) Description: 本教程主要介绍 ROS 图(graph)概念 并讨 ...

  6. 通过joystick遥感和按键控制机器人--11

    原创博客:转载请表明出处:http://www.cnblogs.com/zxouxuewei/ 1.首先安装joystick遥控器驱动: sudo apt-get install ros-indigo ...

  7. (五)ROS节点

    一. 理解ROS 节点: ROS的节点: 可以说是一个可运行的程序.当然这个程序可不简单.因为它可以接受来自ROS网络上其他可运行程序的输出信息,也可以发送信息给ROS网络,被其他 ROS 可运行程序 ...

  8. ROS学习(六)—— 理解ROS节点

    一.准备工作 下载一个轻量级的模拟器 sudo apt-get install ros-kinetic-ros-tutorials 二.图概念的理解 1.Nodes:一个节点就是一个可执行文件,用来与 ...

  9. ROS Learning-007 beginner_Tutorials ROS节点

    ROS Indigo beginner_Tutorials-06 ROS节点 我使用的虚拟机软件:VMware Workstation 11 使用的Ubuntu系统:Ubuntu 14.04.4 LT ...

随机推荐

  1. 【C语言学习】-03 循环结构

    本文目录 循环结构的特点 while循环 do...while循环 for循环 回到顶部 一.循环结构的特点 程序的三种结构: 顺序结构:顺序执行语句 分支结构:通过进行一个判断在两个可选的语句序列之 ...

  2. Program C 暴力求解

    Description   A ring is composed of n (even number) circles as shown in diagram. Put natural numbers ...

  3. JS中 window的用法

    1.window.location.reload();作用是刷新当前页面

  4. AutoCAD ObjectARX(VC)开发基础与实例教程2014版光盘镜像

    AutoCAD ObjectARX(VC)开发基础与实例教程2014,最新版,光盘镜像 作者:张帆 朱文俊 编著 出版社:中国电力出版社 出版时间:2014年6月 点击一下

  5. automationOperationsWithPython

    1.psutil 系统性能信息模块,可获取系统运行的进程和系统利用率(包括CPU.内存.磁盘.网络等)信息.它主要应用于系统监控,分析和限制系统资源及进程的管理.该模块需要单独安装. 示例代码 imp ...

  6. php大力力 [030节] php设计系统后台菜单

    php大力力 [030节] php设计系统后台菜单 2015-08-28 00:11 开始设计: 2015-08-28 01:29 设计完毕. php大力力 [030节] php设计系统后台菜单 1. ...

  7. 在国内时,更新ADT时需要配置的

    RT

  8. PHP中的文件上传

    文件上传:    1.单个文件上传    2.多个文件上传    一.PHP配置文件中和上传有关的选项    file_uploads=on    upload_max_filesize=    最大 ...

  9. [转]BEHAVOUR TREE

    自从开博以来,每天都会关心一下博客的访问情况,看到一些朋友的订阅或者访问,不胜欣喜,也促使我去写一些更好的博文,来和大家分享和交流,从访问 统计来看,有相当一部分是来自于搜索引擎的流量,关键字以“行为 ...

  10. 怎么在手机浏览器上访问电脑本地的文件,局域网内,自建WiFi也可以

    首先,电脑要有Mysql+Apache+PHP环境,我直接用Wampsever,开启环境后手机和电脑要再同一个局域网内,然后电脑上打开win+R,输入cmd,再输入ipconfig,就可以看着这台的电 ...