View Single Post
  #6   Spotlight this post!  
Unread 25-03-2010, 23:17
Kyledoo Kyledoo is offline
Registered User
FRC #2603
 
Join Date: Jan 2010
Location: Ohio
Posts: 24
Kyledoo is an unknown quantity at this point
Re: switching turning direction using arcade drive?

Quote:
Originally Posted by Tom Bottiglieri View Post
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.
Hey. I know it has been awhile since I actually asked this question, but we are now at our second regional(Buckeye) and it has been quite hectic. I just wanted to thank you for all the advice, I used that same format you suggested and we were even able to put in a penalty preventer so that the kicker will not fire more than once every 2 seconds.