This domain is now depreciated and is no longer updated!

Optimizing control of modules

Resources

A state machine is a structure used that allows your robot to execute tasks automatically and sequentially. In the driver-controlled period of your matches, state machines allow you to control several modules of your robot with the press of a button.

Here is an example of a robot that uses a state machine to control 2 motors and 6 servos to score a game element with the press of a single button:

As you can see in the video, the scoring mechanism used is very complex with multiple moving parts. Assigning every individual motor and servo to its own button would overwhelm the drivers. To avoid these issues, programmers use state machines to automate these processes.

Timers

An important aspect of state machines is that they can automatically wait for one task to be completed before starting another one. Take, for example, waiting for your claw servo to fully grab onto a game element before making your linear slide motors lift the mechanism.

Waiting for tasks becomes easy by using timers, provided by Java's ElapsedTime class.

You can initialize a timer like this:

Check how much time has passed like this:

And reset the timer like this:

Implementation

We can make a state machine through the following process:

  • Name each process through Java's Enum utility

  • Initialize a timer

  • Create a switch statement branch containing the name of each Enum as a statement

  • Initialize a variable denoting which step the state system should be executing.

  • Within the actual branches of the switch statement:

    • Execute the task, and reset the timer used to measure the amount of time the task has taken

    • If the timer says that enough time has passed such that the task is guaranteed to be completed, move on to the next step.

The following code should make this easier to understand:

The following code is from GM0's Page on State Machines (also listed as a resource).

Case Study

Take a look at the code file which controls the robot from the video above:

Last updated