View Single Post
  #1   Spotlight this post!  
Unread 09-05-2011, 17:24
Dave Scheck's Avatar
Dave Scheck Dave Scheck is offline
Registered User
FRC #0111 (WildStang)
Team Role: Engineer
 
Join Date: Feb 2003
Rookie Year: 2002
Location: Arlington Heights, IL
Posts: 574
Dave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond repute
Re: Wait(); function

TofuRama replied while I was composing this, but I'll post everything for completeness. One thing to be careful of with the code that was suggested is that the timer variable can roll over when it hits 2147483647. This is probably larger than you'll ever hit with an FRC robot, but I wanted to point it out so you understood that there could be a potential problem (think about if you did that in an airplane that had its computers running for hours at a time).

Based on your code, I assume you want this behavior

When the trigger is pressed, run the motor for one second then turn off.

There are multiple reasons that your code doesn't work.
1. Your timer variable is reset every time you run this code because your variable declaration is within the IF block. To do what you're trying to do you need some form of persistent memory that holds its value between loops. You can accomplish this with a global variable, a static variable within your function, or a member variable in your class depending on how you want to code it up.

2. Your timer and motor control code only get run when the trigger is pressed.

Here's some quick code that should do what you're looking for. I didn't compile/run/test it, but the concepts are there.
Code:
// Declare some global variables at the top of the cpp file
unsigned int timerCounter = 0;
bool processingTriggerPress = false;

....

// Store the trigger value into a local variable so it can be used multiple times
bool triggerPressed = Thirdstick.GetTrigger();

// Create a motor speed variable and default it to 0 (off)
float motorSpeed = 0.0;

// This checks to see if the button is pressed but we're not
// processing the press yet.  This condition causes the timer
// counter to clear and the processing state to change.  The
// reason that it is needed is to protect against the timer from
// clearing when the trigger is held down.
if(triggerPressed == true &&
   processingTriggerPress == false)
{
    timerCounter = 0;
    processingTriggerPress = true;
}

//Check to see if we're in a processing state
if(processingTriggerPress == true)
{   
    if(timerCounter < 1000)
    {
        // The timer counter is within range, so assign a motor speed
        motorSpeed = 1.0;
    }
    else
    {
        // The timer has expired.  Clear the processing state
        processingTriggerPress = false;
    }
}

// Assign the calculated motor speed to the motor.
motor->Set(motorSpeed);
This code illustrates a few important concepts that I teach to our students every year.

1. You have a giant loop around your code. You can use this to your advantage by setting states and using them in the next loop to make decisions

2. Read your inputs into variables at the top of your code, calculate the output values based on the inputs and store the values in local variables, then write your outputs once at the end. This really makes your life easier and makes your code cleaner. For one, this method protects against inputs changing from read to read within the same loop iteration. Second, as your code gets more and more complex, if you have motor assignments (calls to motor->Set()) strewn throughout your code, you could potentially be writing multiple values to your speed controllers within the same loop. This could cause some strange PWM output if you're setting your motors to 0, then to 1 every loop.
Reply With Quote