View Single Post
  #4   Spotlight this post!  
Unread 17-02-2012, 22:28
Ginto8's Avatar
Ginto8 Ginto8 is offline
Programming Lead
AKA: Joe Doyle
FRC #2729 (Storm)
Team Role: Programmer
 
Join Date: Oct 2010
Rookie Year: 2010
Location: Marlton, NJ
Posts: 174
Ginto8 is a glorious beacon of lightGinto8 is a glorious beacon of lightGinto8 is a glorious beacon of lightGinto8 is a glorious beacon of lightGinto8 is a glorious beacon of light
Re: Need some quick advice

Sounds to me like this is a job for... a Command with a state machine!
Since you want it to work easily with a .get() (presumably without hanging your robot or killing the watchdog, the poor beast), I'd use a StartCommand to initialize a Command in .get(). This command should have some sort of member determining which "state" it's in (raising belt, extending piston, retracting piston, etc.), and each execute() it should act upon this state (by continuing to do something like raising the belt, or possibly doing nothing), then check to see if it's complete (enough time has gone by, another sensor has been tripped, etc.). If the current state is complete, it advances to the next state, and if there are no more states, it either starts over (returning to its initial state) or finishes (which I think is the better choice in this situation). If you set up your command like a state machine, it can perform actions in sequence without getting in the way of the rest of the robot.

Just as a possible example of a state machine, I have some code I wrote early in the season which runs a sequence of Commands (feel free to use it if it suits your needs):
Code:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package commands;

import edu.wpi.first.wpilibj.command.Command;

/**
 *
 * @author Ginto8
 */
public class CommandSequence extends Command {
    Command[] commands_;
    int current_;

    public CommandSequence(Command[] commands) {
        commands_ = commands;
    }

    protected void initialize() {
        if(commands_ == null || commands_.length == 0)
            current_ = -1;
        else
            current_ = 0;
    }

    protected void execute() {
        if(!commands_[current_].isRunning()) {
            ++current_;
            if(!isFinished())
                commands_[current_].start();
        }
    }

    protected boolean isFinished() {
        return current_ >= 0 && current_ <= commands_.length;
    }

    protected void end() {
        current_ = -1;
    }

    protected void interrupted() {
        end();
    }

}
In this case, the member variable current_ is my state, selecting which Command should be run.
Reply With Quote