Ask Your Question
-1

how to use Twist with Timer()

asked 2019-12-07 07:13:17 -0600

Redhwan gravatar image

updated 2019-12-09 19:03:23 -0600

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.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2019-12-07 07:52:35 -0600

gvdhoorn gravatar image

updated 2019-12-10 03:33:10 -0600

Don't know about the rest, but we see this:

class GoForward():
    def __init__(self):
        ...
        rospy.Timer(rospy.Duration(2), self.my_callback)

you're not assigning the Timer to anything, causing the temporary object to go out of scope immediately and being destroyed.

So the Timer never really does anything.

You'll probably wan to do something like this:

self.my_timer = rospy.Timer(rospy.Duration(2), self.my_callback)

And the same issue here:

if __name__ == '__main__':
    try:
        GoForward()

GoForward is not a function, but a class.

You're calling the constructor of the class, but don't store the returned object anywhere.

So your program creates an instance of GoForward, which is then immediately destroyed as it's not assigned to a variable in scope.

In the end, your program does nothing as it terminates immediately after calling the GoForward constructor and destroying the object.

You'll probably want to do something like this:

obj = GoForward()

And then also add this afterwards:

rospy.spin()

Because without it -- or something similar -- your program again exits immediately.


Edit: in your updated code, you:

  1. still don't store the Timer in a member variable
  2. still don't store the GoForward instance in a variable

Your script still has the same problems as the original version.

edit flag offensive delete link more

Comments

1

Note: this is purely a Python problem. Not something ROS-specific.

gvdhoorn gravatar imagegvdhoorn ( 2019-12-07 07:53:13 -0600 )edit

Thanks for your help, it is not working yet.

Redhwan gravatar imageRedhwan ( 2019-12-08 19:08:41 -0600 )edit
1

As always: if you want our help, you'll have to be much more specific.

We cannot help you if you just write: "it does not work" ..

gvdhoorn gravatar imagegvdhoorn ( 2019-12-09 01:44:32 -0600 )edit

sorry, @gvdhoorn I will update in my question what I modified in the code.

Redhwan gravatar imageRedhwan ( 2019-12-09 19:01:08 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2019-12-07 07:13:17 -0600

Seen: 58 times

Last updated: Dec 10 '19