View unanswered posts | View active topics It is currently Mon Apr 23, 2018 7:27 am






Reply to topic  [ 32 posts ]  Go to page Previous  1, 2, 3
Question for MHTS 
Author Message
Novice

Joined: Tue Dec 16, 2014 10:25 am
Posts: 81
Post Re: Question for MHTS
Ok Thanks!, I should be able to figure it out from here then!
Also, thanks Synergists! I might be able to use that in the Grabbers for the lift!

_________________
Thanks!


Thu Mar 05, 2015 6:32 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1523
Post Re: Question for MHTS
Synergists wrote:
Just for your reference, this is how we did a toggle button. Initially the button was meant to switch between three different positions, but now we just need two but the functionality is still there if we need three or more positions later:
Code:
#ifndef ROBOTDEFINITIONS
#include "..\RobotDefintions 150131.c"
#define ROBOTDEFINITIONS 1
#endif

int nextServoCommand = 1;
bool bButtonPressed = false;

void autoServoBall()
{
   clearDebugStream();
   while(true)
   {
      getJoystickSettings(joystick);
      if(joy2Btn(9) == true && bButtonPressed == false)
      {
         switch((int) nextServoCommand)
         {
         case 1: servo[autoBallDispenser] = HoldSmallBall;
            nextServoCommand++;
            bButtonPressed = true;
            //writeDebugStreamLine("Small Ball");
            break;
         case 2: servo[autoBallDispenser] = HoldNoBalls;
            nextServoCommand = 1;
            bButtonPressed = true;
            //writeDebugStreamLine("No Balls");
            break;
         //case 3: servo[autoBallDispenser] = HoldBothBalls;
         //   nextServoCommand = 1;
         //   bButtonPressed = true;
         //   //writeDebugStreamLine("Both Balls");
         //   break;
         default:
            break;
         }
      }
      if(joy2Btn(9) == true != true)
      {
         bButtonPressed = false;
      }
   }
}

BTW, don't know if you realize it, the above code is actually implementing a toggling button using a state machine. I have renamed the variable "nextServoCommand" to "servoBallState" and optimized the code a little bit to show you the equivalence. This code allows any number of states. Each time button 9 is pressed, it can go to a different state.
Code:
int servoBallState = 1;

void autoServoBall()
{
    clearDebugStream();
    while(true)
    {
        getJoystickSettings(joystick);
        //
        // This basically said if button 9 is pressed.
        // It is detecting an edge event (i.e. a transistion
        // from button 9 not pressed to being pressed.
        //
        if(joy2Btn(9) == true && bButtonPressed == false)
        {
            bButtonPressed = true;
            switch (servoBallState)
            {
            case 1:
                servo[autoBallDispenser] = HoldSmallBall;
                servoBallState++;  //advance to next state (the toggled ON state)
                //writeDebugStreamLine("Small Ball");
                break;
            case 2:
                servo[autoBallDispenser] = HoldNoBalls;
                servoBallState = 1;  // go back to previous state (the toggled OFF state)
                //writeDebugStreamLine("No Balls");
                break;
//            case 3: servo[autoBallDispenser] = HoldBothBalls;
//               nextServoCommand = 1;
//               //writeDebugStreamLine("Both Balls");
//               break;
            default:
                break;
            }
        }
        else if(!joy2Btn(9))
        {
            bButtonPressed = false;
        }
    }
}

A toggle button has only two states (ON and OFF), you can do it with a boolean instead of an integer state such as the code below.
Code:
bool bButtonPressed = false;
bool toggledOn = false;

while (true)
{
    getJoystickSettings(joystick);
    if (!bButtonPressed and joy2Btn(9))
    {
        bButtonPressed = true;
        toggledOn = !toggledOn;
    }
    else if (!joy2Btn(9))
    {
        bButonPressed = false;
    }

    if (toggledOn)
    {
        servo[autoBallDispenser] = HoldSmallBall;
        //writeDebugStreamLine("Small Ball");
    }
    else
    {
        servo[autoBallDispenser] = HoldNoBalls;
        //writeDebugStreamLine("No Balls");
    }
}


Mon Mar 30, 2015 5:23 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 32 posts ]  Go to page Previous  1, 2, 3

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.