how to use Twist with Duration()
I'd like to make the robot moves a certain distance toward x-direction using Twist with Duration() without while loop or rospy.get_rostime().to_sec()
,
ex: I'd choose 60 sec
like Duration(60)
and move_cmd.linear.x = 0.2. it will move the distance = 0.2 * 60 = 12 m
this idea will solve many issues for me which forced with move_base
and converted the coordinates with Odometry
this full code:
the robot moves without stops, I'd also add Duration and remove while loop.
#!/usr/bin/env python
import rospy
from geometry_msgs.msg import Twist
class GoForward():
def __init__(self):
rospy.init_node('GoForward', anonymous=False)
rospy.loginfo("To stop TurtleBot CTRL + C")
rospy.on_shutdown(self.shutdown)
self.cmd_vel = rospy.Publisher('/cmd_vel', Twist, queue_size=10)
r = rospy.Rate(50)
move_cmd = Twist()
move_cmd.linear.x = 0.2
move_cmd.angular.z = 0
while not rospy.is_shutdown():
self.cmd_vel.publish(move_cmd)
r.sleep()
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.")
I spent more time on it, it seems easy but it is difficult for me.
please help me or any suggestions.
Edit:
I tried with Timer but it didn't work with me, it ran without continue. It seems to while loop or starts time.
#!/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.")
Could you please explain where in this your actual problem is? What have you tried, what didn't work and what did you expect vs. what did you actually see?
Also, please note that this approach sounds nice in theory, but due to wheel slip, timing issues, acceleration/deceleration, and many more factors, you'll (almost) never end up with the expected distance...
Also, I'm not quite sure if this isn't an xy-problem. Could you explain a little bit what your use case is and why this doesn't work e.g. with
move_base
?my issue in this my previous question, it wrote before few days here . how to stop this line
move_cmd.linear.x = 0.2
after60 sec
? second thing when usingrospy.get_rostime().to_sec()
which need to addwhile loop
lead to freeze the video,I'm not sure I understand you correctly... What about a while loop and a video? There is neither some kind of video mentioned here nor in the other question you link to...
I still don't think that this is the way you should proceed, but to answer your question about stopping the publishing of the velocity, why not try something like this:
First of all, thank you a lot for your help.
sorry, I have a long code but I use a part of it here, I mentioned there 'the human following with OpenCV '.
while loop changes in a number of the frame per second if it used, as a result, the video freezes. So, I wonder if there is any method to solve the issue smoothly, similar this it used
success = self.move_base.wait_for_result(rospy.Duration(60))
withmove_base
because I have the velocity is a known. theoretically, we can calculate the distance if using a Duration(xxxx).my background is good with ROS but not enough.
I think while loop is the simplest way but with OpenCV is bad. second thing use
Odometry
like these codes here , here. I already used Odometry bI already used Odometry but when the robot moves a long time, it becomes inaccurate. So, I post this question here.
One workaround would be using a separate thread or a ros timer. You can publish the twist within the timer callback, update the time and handle the publishing logic there.
@pavel92 thank you for your help. I tried to implement it with
Timer
but failure. So, I edited my question and added the code.could you help me
I don't fully understand "ran without continue. It seems to while loop or starts time." Does it run once and stop? You need something to keep the node alive, e.g., a
while not rospy.is_shutdown()
loop orrospy.spin()
; otherwise, the node will finish before the timer triggers the callback. Also, for troubleshooting, I would shorten the duration and use logging or a print statement to test the callback functionality before issuing velocity commands.