View Single Post
  #2   Spotlight this post!  
Unread 30-03-2011, 16:58
Jared Russell's Avatar
Jared Russell Jared Russell is offline
Taking a year (mostly) off
FRC #0254 (The Cheesy Poofs), FRC #0341 (Miss Daisy)
Team Role: Engineer
 
Join Date: Nov 2002
Rookie Year: 2001
Location: San Francisco, CA
Posts: 3,077
Jared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond repute
Re: Autonomous State Machine: Abstract Inner Classes?

We have an abstract class called "State" that gets extended by our various states (DriveForDistance, TurnToAngle, GoToArmPosition, ScoreTube, Wait, etc.)

I would suggest making your abstract inner class into its own "outer" class in its own file. Then, rather than anonymous inheritance, create a new concrete class for each distinct state. The advantage of this, IMO, is twofold:

1. It makes debugging and tracing your code easier.

2. It facilitates easier/more elegant version control (less likelihood of conficts, and more obvious where the changes were between versions)

My team did something very similar for our solution to autonomous mode this year. Our state machine is built around the IterativeRobot philosophy - writing code that is designed to be executed in periodic loops. Here is what our State class looks like:

Code:
public abstract class State
{
    private String mName;

    protected State(String aName)
    {
        mName = aName;
    }

    public String toString()
    {
        return mName;
    }

    public void enter()
    {

    }
    
    public abstract void running();

    public abstract boolean isDone();

    public void exit()
    {

    }
}
And here is the code that calls the various periodic functions of the current State:

Code:
public class StateMachine
{
    State[] mStates;
    int mCurrentState;
    boolean mStarted;

    public StateMachine(State[] aStates)
    {
        mStates = aStates;
        mCurrentState = 0;
        mStarted = false;
    }

    public void run()
    {
        if( mCurrentState < mStates.length )
        {
            if( !mStarted )
            {
                mStates[mCurrentState].enter();
                mStarted = true;
            }
            else if( mStates[mCurrentState].isDone() )
            {
                mStates[mCurrentState].exit();
                System.out.println("Exiting state: " + mStates[mCurrentState]);
                mCurrentState++;
                if( mCurrentState < mStates.length )
                {
                    mStates[mCurrentState].enter();
                    System.out.println("Entering state: " + mStates[mCurrentState]);
                }
                else
                {
                    System.out.println("Finished state machine.");
                }
            }
            else
            {
                mStates[mCurrentState].running();
            }
        }
    }
}
StateMachine.run() is called by the autonomousPeriodic() function that our main class overrides from IterativeRobot.

The last nifty feature is that when autonomous mode begins, our robot looks on its filesystem for a file called "autonomous.txt". This is a list of states (and their arguments, such as speeds or positions) that we edit over FTP from our driver station (using any standard text editor - we like Notepad++ for the built-in FTP integration). This is a nice feature to have once each state works correctly, but you need to tweak things like distances or angles - we can edit our autonomous code *without* re-compiling.
Reply With Quote