This domain is now depreciated and is no longer updated!
Optimizing control of modules
Last updated
Optimizing control of modules
Last updated
This domain is now depreciated and is no longer updated! Please visit this domain for the new Tech toolbox!!!
Resources
GM0's Section on State Machines - Must Read!
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.
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:
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:
Here are some notes on the provided code:
Some of the branches do not make use of timers, instead making use of encoder measurements to check when the lift is finished raising. However, the getPosition
method can sometimes be buggy so if you find that your state machine doesn't work well, try only making use of timers.
You cannot make use of getPosition
with servos since it always returns the target position of the servo rather than its true position. As such, you must use timers when if your task is dependent on servo movement.
The following code is from GM0's Page on State Machines (also listed as a resource).
Take a look at the code file which controls the robot from the video above:
Please note that our state machine does not make use of enums, so we recommend that you use this code as a reference but still maintain the cleaner structure shown in the example above.