FRC2706 / 2024-2706-Robot-Code

Code for the 2024 game.
Other
3 stars 0 forks source link

StateBased Programming #57

Open LuisFerArredondo opened 4 months ago

LuisFerArredondo commented 4 months ago

This file will explain my concept of integration state machines into the robots code and the advantages it can bring.

-First of all, to fully understand this file you must go and take a look at the "StateBased Coding style".

Now, First of all, what's a state machine?. A state machine is defined literally as a machine that has defined states. The best example I can think of this is:

Lets use the example defined before in the "StateBased Coding style", now that we have all of our files, lets make the "ArmSuperstructure" a state machine.

The way I plan to make the integration of a state machine is by having "Modes" and "States". The "Modes" are basically what we command the Superstructure to do, and the "Sates" are basically the robots output. This is a bit tricky to understand so here is an actual Code Example:

Following up with the code example given in "StateBased coding style", inside the "SuperStructures" folder, lets create a file specifically for state control. This file will be named "ArmSuperStructureStates".

This state file will only store the following data; "Modes", "States", each one with their respective "getters" and "Setters" methods, an "UpdateState" method, and any other method required by the "UpdateState" method. Now lets dive more into how the Modes are implemented:

public static enum Arm_Modes{
        PICK_ITEMS_POS(new Translation2d(0,0)),
        SCORE_HIGH_GOAL(new Translation2d(0,0)),
        SCORE_MID_GOAL(new Translation2d(0,0)),
        SCORE_LOW_GOAL(new Translation2d(0,0)),
        IDLE(new Translation2d());

        Translation2d pose2d;
        Arm_Modes(Translation2d pose){
            pose2d = pose;
        }

        public Translation2d getDesiredPose(){
            return pose2d;
        }

      }

The Modes are basically enums. This enums are meant to store data that will be needed by the Superstructure. In this case the Modes store the position of the arm depending on what we want the arm to do.

    private Arm_Modes action = Arm_Modes.IDLE;

Of curse then we have to create an object of the Modes.

    //Mode methods 
    public void setAction(Arm_Modes wantedMode){
        this.wantedMode = wantedMode;
    }

    public Arm_Modes getWantedMode(){
        return wantedMode;
    }    

This are the "getters" and "Setters" of the attribute, each one named by the purpose they have, and the future implementation.

Now lets begin with the "States", as its name says, the State is literally all of the possible states of the machine;

    public static enum Arm_States{
        MOVING_TO_HIGH,
        MOVING_TO_MID,
        MOVING_TO_LOW,
        MOVING_TO_PICK,
        IN_HIGH_POS,
        IN_MID_POS,
        IN_LOW_POS,
        IN_PICK_POS,
        IDLE
    }

In this specific example, this are all of the possible states of the arm. Now lets create an object of the "Arm_States" enum and the "getters" and "setters" of the state object.

    private Arm_States state = Arm_States.IDLE;
    //State Methods
    public ArmSuperStructureState isDesiredState(){
        isTheDesiredState = true;
        return this;
    }

    public Arm_States getCurrentState(){
        return state;
    }

//TODO: keep working on this cause now im too tired to continue writting jeje

Overall, the mechanisms are just the hardware layer, these provide the software layer the tools to control the hardware and also stores the high level control algorithms that will make more efficient the mechanisms actions and then the control layer (the commands) will use all of those tools to transform code into real life movements.

https://github.com/LuisFerArredondo/2706_2024.git