0
$\begingroup$

Rosanswers logo

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 ^^


Originally posted by koboy on ROS Answers with karma: 5 on 2017-12-24

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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.


Originally posted by PeteBlackerThe3rd with karma: 9529 on 2017-12-29

This answer was ACCEPTED on the original site

Post score: 2

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.