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

Updating the values of a while loop coming from another node

asked 2017-12-24 17:46:29 -0500

koboy gravatar image

updated 2018-01-05 08:34:46 -0500

So, when I try to run this particular part of the script:

if data.buttons[4]:
    while not data.buttons[5]:
        motor_msg.data=int(70)
        pub_motor0.publish(motor_msg)
        if data.buttons[6]:
            while not data.buttons[7]:
                    motor_msg.data=int(60)
                    pub_motor0.publish(motor_msg)

data.buttons come from a Joy node. When I press button 5 or 6 after button 4 being pressed and the loop initialized it doesn't update the value of button5 or 6 in eyes of the loop. Why is it? Thanks a lot guys ^^


Edit: I wanted to command my robot to walk in a straight line on the activation on button4 indefinetly unless i pressed button 5. The point is that on activation of button5 the data stays at 0, annd I don't know how to force the re-read of variables and as you said, nothing updates the variable's value. I put this part in the callback function.


reedit: Yeah! I was writing a whole new node and looking for a way to make it republish a different velocity message everytime but you are right, it is likely that you are totally right and I don't need to re-update the value of velocity. It is rpetty much what you said! Thanks a lot. When I get to the lab I'll try the new code ^^

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2017-12-29 11:37:44 -0500

updated 2018-01-05 08:27:11 -0500

Can you explain a bit more what you're expecting your code to do.

You seem to have several potential infinite loops in this code and nothing that is attempting to update the values of the data.buttons array.

At a glance this is what I think you're code will be doing.

If button 4 is pressed and button 5 isn't then it will loop forever sending a motor_message with a value of 70 Also within this loop it will check if button 6 is pressed and button 7 isn't if this is the case it will then loop forever sending motor_messages with values of 70 forever and never return to the outer loop.

Unless you're running multi-threaded code and 'data' is a shared variable then it's value isn't going to change while these loops are being executed. Do you have a callback function for joy messages somewhere in your code which handles incoming joystick messages?

Update: Okay, I understand what you're trying to achieve now. This really depends on the behaviour of your motor controller, when you send it a velocity message does it continue to drive with that velocity until it receives a different velocity message? That would seem likely to me, but you'll need to check to make sure.

In this case it's actually fairly simple, the ROS joystick node publishes new messages every time it's state changes so all you need to do is find out if the motor needs starting or stopping based on the buttons pressed at this point in time and send the appropriate message. You only need to do this once in your callback function because future callbacks will be called automatically when the joystick buttons change state.

The pseudo code for your callback will look something like this:

if button 4 is pressed and button 5 isn't pressed then
      send a motor message with a value of 70
if button 5 is pressed then
      send a motor message with a value of 60

Hope this helps.

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2017-12-24 17:46:29 -0500

Seen: 514 times

Last updated: Jan 05 '18