View unanswered posts | View active topics It is currently Mon Sep 01, 2014 3:31 am






Reply to topic  [ 6 posts ] 
Can't get servo to work with two different buttons 
Author Message
Rookie

Joined: Tue Nov 18, 2008 5:13 pm
Posts: 9
Post Can't get servo to work with two different buttons
My team is having a problem with programing a servo to toggled. We would like our buttons to sort of "toggle" the servo, so that we press it and it moves to the position and stays and then when we press it again it goes back to 0. But we don't know how. Please help!!!


Tue Nov 18, 2008 5:23 pm
Profile
Rookie

Joined: Tue Nov 18, 2008 5:13 pm
Posts: 9
Post Re: Can't get servo to work with two different buttons
I forgot to change the subject title.


Tue Nov 18, 2008 5:31 pm
Profile
Professor

Joined: Fri Sep 19, 2008 1:22 am
Posts: 200
Post Re: Can't get servo to work with two different buttons
This code will toggle the target position of a servo every time game controller button 3 is pressed.

Code:
#define servoTarget1  50  // Minimum position of servo
#define servoTarget2  99  // Maximum position of servo
...
    int servoTarget = -1;  // Start out with servo in an unknown state.
...
    if (joy1Btn(3))   // Toggle the position of the servo...
    {
        if (servoTarget == servoTarget1)
            servoTarget = servoTarget2;
        else
            servoTarget = servoTarget1;

        servo[servo1] = servoTarget;
    }
...
}

_________________
Jeff McBride
Benson Robotics Club


Tue Nov 18, 2008 7:05 pm
Profile
Rookie

Joined: Sun Nov 30, 2008 3:42 pm
Posts: 1
Post Re: Can't get servo to work with two different buttons
Actually, the code posted above probably won't work (assuming that it's within a scan sequence) because we continually scan the joystick, so the code would actually activate more than once, and thus the result would be indeterminate.

In order to correctly handle a toggle, you have to make a one-shot notifier as well...

I took the code and added in the "b3Pressed" boolean variable to handle that one-shot

Code:
Code:
#define servoTarget1  50  // Minimum position of servo
#define servoTarget2  99  // Maximum position of servo
...
    int servoTarget = -1;  // Start out with servo in an unknown state.

    bool b3Pressed = false; // This is the new piece -- This is a holder to let us know the state of the last press of the button
...
 /* Section within bluetooth scan loop */

    if (joy1Btn(3))   // Toggle the position of the servo...
    {
        if ( ! b3Pressed) // only deal with this if the previous scan was off!
        {         
            if (servoTarget == servoTarget1)
                servoTarget = servoTarget2;
            else
                servoTarget = servoTarget1;

            servo[servo1] = servoTarget;
       }
       b3Pressed = true;    // indicate that we're already pressed for the next scan through
    } else
      b3Pressed = false; // clear the "history" so we can tell that we've got a new button press
...
}


Try that (or something similar). The biggest problem with the button pushes on this system is that the bluetooth is slower than the actual scan speed of the controller. To that end, you can't push the button fast enough to make it only scan through once.


Sun Nov 30, 2008 3:57 pm
Profile
Professor

Joined: Fri Sep 19, 2008 1:22 am
Posts: 200
Post Re: Can't get servo to work with two different buttons
There is another way to handle the button changes. Keep track of the previous state of the entire joystick.joy1_Buttons field and only fall into (or call) the code that processes the button clicks when the field changes:

Code:
        buttonsPrev = 0;
        while (true)
        {
             getjoysticksettings(joystick);

             if (joystick.joy1_Buttons != buttonsPrev)
             {
                  buttonsPrev = joystick.joy1_Buttons;
                  if (joy1Btn(3))
                  {
                     // Do work
                  }

                  if (joy1Btn(2))
                  ....
             }
         }


This avoids having to track separate "pressed" flags for each button.

_________________
Jeff McBride
Benson Robotics Club


Sun Nov 30, 2008 4:52 pm
Profile
Rookie

Joined: Tue Nov 18, 2008 5:13 pm
Posts: 9
Post Re: Can't get servo to work with two different buttons
Thank you all, we got the program to work out and are having no problems with our TeleOp program. Now for the Autonomous...... :programmer:


Sun Nov 30, 2008 9:03 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 6 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.