Ask Your Question
0

Unable to stop publishing on a topic (Resolved)

asked 2014-04-06 08:25:51 -0600

Warrior gravatar image

updated 2014-04-17 04:22:34 -0600

Hi,

I want to stop publishing a message to a topic after 20 iterations. For that I have written the following piece of code, but however the counter reaches to 20, the program is still publishing the message to the topic and it seems that it is disregarding the counter variable's value. How can I resolve this issue?

class node():



def __init__(self):

    rosInstance = rospy
    pub = rosInstance.Publisher('cmd_vel', Twist)
    r = rosInstance.Rate(1)
    ctrlArgs = Twist()
    iteration = 0
    speed = -0.1


    while not rospy.is_shutdown():
        self.iterate()
        if iteration < 20:
            cntrlArgs.linear.x = 0.2
            pub.publish(ctrlArgs)
        r.sleep()


def iterate(self):
    iteration = iteration + 1

And then I run the program:

if __name__ == '__main__':

    rospy.init_node('iterator', anonymous=False)
    try:
        nn = node()
    except rospy.ROSInterruptException: pass

Thank you

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

answered 2014-04-15 07:41:28 -0600

Warrior gravatar image

The issue is resolved now. I had to add "self." as prefix to all of the variables that I wanted to consider as "global" in a specific class.

Here's the modified code:

class node():



def __init__(self):

    rosInstance = rospy
    self.pub = rosInstance.Publisher('cmd_vel', Twist)
    r = rosInstance.Rate(1)
    self.ctrlArgs = Twist()
    self.iteration = 0
    speed = -0.1


    while not rospy.is_shutdown():
        self.iterate()
        if iteration < 20:
            self.cntrlArgs.linear.x = 0.2
            self.pub.publish(ctrlArgs)
        r.sleep()


def iterate(self):
    self.iteration = self.iteration + 1
edit flag offensive delete link more
2

answered 2014-04-06 15:46:57 -0600

jbinney gravatar image

iteration here is a local variable, not a member variable of your class. So the variable you iterate in the iterate() function is completely different than the variable you check in your loop. Use self.iteration instead of iteration.

edit flag offensive delete link more

Comments

I just did. I added the self. prefix to the iteration variable in the iterate() function. Now the program doesn't show me an error, but still doesn't stop the robot moving ... . I really need to resolve this issue.

Warrior gravatar imageWarrior ( 2014-04-06 17:27:24 -0600 )edit

You'll have to post the updated code if you want help finding your bug

jbinney gravatar imagejbinney ( 2014-04-06 17:52:45 -0600 )edit

Sorry for the delay. I found the solution to the problem and answered my question.

Warrior gravatar imageWarrior ( 2014-04-15 07:52:07 -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: 2014-04-06 08:25:51 -0600

Seen: 1,099 times

Last updated: Apr 17 '14