how to use Twist with Timer()
I'd like to make the robot moves a certain time toward x-direction using Twist with Timer
full code:
#!/usr/bin/env python
import rospy
from geometry_msgs.msg import Twist
class GoForward():
def __init__(self):
rospy.init_node('GoForward', anonymous=False)
rospy.on_shutdown(self.shutdown)
self.cmd_vel = rospy.Publisher('/cmd_vel', Twist, queue_size=10)
rospy.Timer(rospy.Duration(2), self.my_callback)
def my_callback(self, event):
move_cmd = Twist()
move_cmd.linear.x = 0.2
move_cmd.angular.z = 0
self.cmd_vel.publish(move_cmd)
def shutdown(self):
rospy.loginfo("Stop TurtleBot")
self.cmd_vel.publish(Twist())
rospy.sleep(1)
if __name__ == '__main__':
try:
GoForward()
except:
rospy.loginfo("GoForward node terminated.")
What is wrong, the code stops without continue.
any help or suggestions, it would be appreciated.
UPDATE
I modified the code to:
#!/usr/bin/env python
import rospy
from geometry_msgs.msg import Twist
class GoForward():
def __init__(self):
rospy.init_node('GoForward', anonymous=False)
rospy.on_shutdown(self.shutdown)
self.cmd_vel = rospy.Publisher('/cmd_vel', Twist, queue_size=10)
event_period = rospy.get_param('~event_period', 0.02)
rospy.Timer(rospy.Duration(event_period), self.my_callback)
def my_callback(self, event):
move_cmd = Twist()
move_cmd.linear.x = 0.2
move_cmd.angular.z = 0
self.cmd_vel.publish(move_cmd)
def shutdown(self):
rospy.loginfo("Stop TurtleBot")
self.cmd_vel.publish(Twist())
rospy.sleep(1)
if __name__ == '__main__':
try:
GoForward()
rospy.spin()
except:
rospy.loginfo("GoForward node terminated.")
the output:
the robot moved without stopping.