ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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)

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.

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.