当前位置:   article > 正文

ROS学习笔记-用脚本发布cmd_vel话题_ros python 发布cmd

ros python 发布cmd

 创建工作空间

  1. cd ~
  2. mkdir helloworld
  3. touch goforward.py

 python脚本

  1. #!/usr/bin/env python
  2. '''
  3. Copyright (c) 2015, Mark Silliman
  4. All rights reserved.
  5. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
  6. 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  7. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  8. 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
  9. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  10. '''
  11. # A very basic TurtleBot script that moves TurtleBot forward indefinitely. Press CTRL + C to stop. To run:
  12. # On TurtleBot:
  13. # roslaunch turtlebot_bringup minimal.launch
  14. # On work station:
  15. # python goforward.py
  16. import rospy
  17. from geometry_msgs.msg import Twist
  18. class GoForward():
  19. def __init__(self):
  20. # initiliaze
  21. rospy.init_node('GoForward', anonymous=False)
  22. # tell user how to stop TurtleBot
  23. rospy.loginfo("To stop TurtleBot CTRL + C")
  24. # What function to call when you ctrl + c
  25. rospy.on_shutdown(self.shutdown)
  26. # Create a publisher which can "talk" to TurtleBot and tell it to move
  27. # Tip: You may need to change cmd_vel_mux/input/navi to /cmd_vel if you're not using TurtleBot2
  28. self.cmd_vel = rospy.Publisher('cmd_vel_mux/input/navi', Twist, queue_size=10)
  29. #TurtleBot will stop if we don't keep telling it to move. How often should we tell it to move? 10 HZ
  30. r = rospy.Rate(10);
  31. # Twist is a datatype for velocity
  32. move_cmd = Twist()
  33. # let's go forward at 0.2 m/s
  34. move_cmd.linear.x = 0.2
  35. # let's turn at 0 radians/s
  36. move_cmd.angular.z = 0
  37. # as long as you haven't ctrl + c keeping doing...
  38. while not rospy.is_shutdown():
  39. # publish the velocity
  40. self.cmd_vel.publish(move_cmd)
  41. # wait for 0.1 seconds (10 HZ) and publish again
  42. r.sleep()
  43. def shutdown(self):
  44. # stop turtlebot
  45. rospy.loginfo("Stop TurtleBot")
  46. # a default Twist has linear.x of 0 and angular.z of 0. So it'll stop TurtleBot
  47. self.cmd_vel.publish(Twist())
  48. # sleep just makes sure TurtleBot receives the stop command prior to shutting down the script
  49. rospy.sleep(1)
  50. if __name__ == '__main__':
  51. try:
  52. GoForward()
  53. except:
  54. rospy.loginfo("GoForward node terminated.")

启动脚本

打开一个终端

roscore

打开另一个终端 

  1. cd ~/helloworld
  2. python goforward.py

查看cmd_vel话题内容

rostopic echo cmd_vel

参考资料

https://github.com/markwsilliman/turtlebot/blob/master/goforward.py

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/674747
推荐阅读
相关标签
  

闽ICP备14008679号