View Single Post
  #3   Spotlight this post!  
Unread 22-02-2010, 08:48
SuperBK's Avatar
SuperBK SuperBK is offline
Registered User
AKA: BrianK
FRC #1225 (Amperage Robotics)
Team Role: Mentor
 
Join Date: Jan 2007
Rookie Year: 2006
Location: Henersonville, NC
Posts: 357
SuperBK is just really niceSuperBK is just really niceSuperBK is just really niceSuperBK is just really nice
Re: Running a Relay for a Set Time

This is a C++ example. Its a state machine that starts with a trigger on a joystick. It even includes the 2 second lockout before the kicker can be fired again.

Code:
enum Kicker_State {
Kicker_off, 
Kicker_going_out, 
Kicker_out, 
Kicker_coming_back, 
Kicker_reload};

Timer kicker_timer;
Kicker_State kicker_state = Kicker_off;

while (IsOperatorControl())
{
    GetWatchdog().Feed();
    myRobot.TankDrive(left_stick, right_stick);		// drive with two sticks
    Wait(0.005);				// wait for a motor update time
    
    // kicker out-in actuator
    switch (kicker_state)
    {
    case Kicker_off:
        // see if go button is pressed
        if (right_stick.GetRawButton(KICKER_BUTTON))
        {
            kicker->Set(Relay::kForward);	// turn on
            kicker_timer.Reset();
            kicker_timer.Start();
            kicker_state = Kicker_going_out;
        }
        break;
    case Kicker_going_out:
        // see if enough time elasped
        if (kicker_timer.Get() >= .25)
        {
            kicker->Set(Relay::kOff);
            kicker_timer.Reset();
            kicker_state = Kicker_out;
        }
        break;
    case Kicker_out:
        // see if enough time elasped
        if (kicker_timer.Get() >= .5)
        {
            kicker->Set(Relay::kReverse);
            kicker_timer.Reset();
            kicker_state = Kicker_coming_back;
        }
        break;
    case Kicker_coming_back:
        // see if enough time elasped
        if (kicker_timer.Get() >= .25)
        {
            kicker->Set(Relay::kOff);
            kicker_timer.Reset();
            kicker_state = Kicker_reload;
        }
        break;
    case Kicker_reload:
        // see if enough time elasped
        if (kicker_timer.Get() >= 2.0)
        {
            kicker_state = Kicker_off;
            kicker_timer.Stop();
        }
        break;
    }
    
}	// end operator control loop
__________________
Brian K
Team 1225 Robotics Mentor
Reply With Quote