[text] wanderbot

Viewer

copydownloadembedprintName: wanderbot
  1. #!/usr/bin/env python3
  2. import rospy
  3. from geometry_msgs.msg import Twist
  4. cmd_vel_pub = rospy.Publisher('cmd_vel', Twist, queue_size=1)
  5. rospy.init_node('red_light_green_light')
  6. rospy.loginfo("started")
  7. red_light_twist = Twist()
  8. green_light_twist = Twist()
  9. green_light_twist.linear.x = 0.5
  10. driving_forward = False
  11. light_change_time = rospy.Time.now()
  12. rate = rospy.Rate(10)
  13. while not rospy.is_shutdown():
  14.     if driving_forward:
  15.         cmd_vel_pub.publish(green_light_twist)
  16.     else:
  17.         cmd_vel_pub.publish(red_light_twist)
  18.     if light_change_time < rospy.Time.now():
  19.         driving_forward = not driving_forward
  20.         light_change_time = rospy.Time.now() + rospy.Duration(3)
  21.     rate.sleep()
  22.  

Editor

You can edit this paste and save as new:


File Description
  • wanderbot
  • Paste Code
  • 17 Apr-2024
  • 745 Bytes
You can Share it: