View Single Post
  #5   Spotlight this post!  
Unread 12-03-2010, 02:26
Tom Bottiglieri Tom Bottiglieri is offline
Registered User
FRC #0254 (The Cheesy Poofs)
Team Role: Engineer
 
Join Date: Jan 2004
Rookie Year: 2003
Location: San Francisco, CA
Posts: 3,186
Tom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond repute
Re: switching turning direction using arcade drive?

You probably dont want to have any kind of nested loops in your main program loop, unless you are certain it will execute fast enough. A for loop will block the execution of the code after it, and will fire the watch dog, or worse, cause your bot to "freeze" its output values while the loop is executing.

Instead, you could use a Timer object.
Code:
class MyRobot : public IterativeRobot
{
   // Declare the timer
   Timer * kickTimer;
 
   // Then down in the constructor...
   MyRobot(void){
      // ...
      kickTimer = new Timer();
   }

   void TeleopPeriodic(void){
      if( somethingHappened ){
         kickTimer->Start();
         mySolenoid->Set(true);
      }

      if( kickTimer->Get() >= 1.0){
         mySolenoid->Set(false);
         kickTimer->Stop();
         kickTimer->Reset();
      }
}
Or you could just increment a counter every time you loop around
Code:
static unsigned int solenoidCounts = 0;
static bool kickerFired = false;

if(somethingHappened){
   solenoid->Set(true);
   kickerFired = true;
   solenoidCounts = 0;
}

if(kickerFired){
   solenoidCounts++;
   if(solenoidCounts > 1 * 40){ // 40Hz(ish)
      solenoid->Set(false);
      kickerFired = false;
      solenoidCounts = 0; // just to be safe
   }
}
It's not the prettiest thing in the world, but it gets the job done and its pretty easy to understand/implement/change.