Sorry, for the earlier post (I posted my answer to a different thread to this thread by mistake).
Here are some suggestions to avoid the long boolean array pattern:
- Try transitioning between "states" to keep things more manageable in your code.
- I would also recommend that your team spend some time evaluating the Command based framework after this season ends to see if it might be a better fit for next year (it addresses this type of issue with an entirely different pattern). Or, if you have a lot of time waiting for a turn on the robot, you might want to investigate it now in a new project to see if it would be a better fit for what you want to accomplish.
As far as using a state based iterative method, there are probably many approaches. Here is a rough framework that include a timer to get you started:
Code:
// Keeps track of time state was entered
private Timer autonStateTimer;
// Keeps track of current state
private int autonState;
// List of possible states
private final static int AUTON_STATE_DRIVE_FORWARD = 1;
private final static int AUTON_STATE_STOP = 2;
private final static int AUTON_STATE_SHOOT = 3;
private final static int AUTON_STATE_FINISHED = 4;
// Helper method to change to new state and reset timer so
// states can keep track of how long they have been running.
private void changeAutonState(int nextState) {
if (nextState != autonState) {
autonState = nextState;
autonStateTimer.reset();
}
}
public void autonomousInit() {
// Reset auton state to initial drive forward and reset the timer
autonState = AUTON_STATE_DRIVE_FORWARD;
autonStateTimer = new Timer();
// Not sure if start() is required anymore, but it shouldn't hurt
autonStateTimer.start();
}
public void autonomousPeriodic() {
switch (autonState) {
case AUTON_STATE_DRIVE_FORWARD: {
// Drive forward at half power for 3 seconds
setDrivePower(0.5, 0.5);
if (autonStateTimer.hasPeriodPassed(3.0)) {
changeAutonState(AUTON_STATE_STOP);
}
break;
}
case AUTON_STATE_STOP: {
// Turn off drive motors
setDrivePower(0.0, 0.0);
// After 1/2 elapses (time to stop) transition to
// shooter state
if (autonStateTimer.hasPeriodPassed(.5)) {
changeAutonState(AUTON_STATE_SHOOT);
}
break;
}
case AUTON_STATE_SHOOT: {
// Some auton method that fires a boulder
fireBoulder();
if (autonStateTimer.hasPeriodPassed(1.5)) {
changeAutonState(AUTON_STATE_FINISHED);
}
break;
}
case AUTON_STATE_FINISHED: {
stopShooter();
break;
}
}
}
The switch/case code can get pretty long when using this pattern. It can be easier to manage (read) if you have each case call a separate method. You will need to implement the methods you want to perform in each state - the calls above are just pseudo code.
Hope that helps.