Programming - Autonomous Initialization
The autonomous version of the sample program includes the option for both a Blue and Red Alliance Auto, along with TeleOp, all in one code.
Why combine everything into one program?
Combining the Auto codes and TeleOp all together does make the Blocks program appear chunky and intimidating at first glance. However, the goal is for this to prevent issues with updates not being reflected across multiple programs.
For example, say your team decides to make changes to the flywheel that leads to changes in what one of the target velocities should be set to. This change is then updated in TeleOp, perhaps the program the team runs most often, but neither of the auto options. This can lead to frustration and confusion later.
But since everything is combined here, when that one variable is update, it now is automatically reflect in all three options!
Let's take a look first at how we select which OpMode should run when we press "play" on the Driver Hub.
Initialization with Auto

First, you may notice compared to our "default" code that the variables and motor modes are gone. These have been combined into a function, "initRobot", for clarity. We'll look at that function more closely below.
Next, rather than checking if our opModeIsActive as we usually do we are instead checking that the program has only been initialized. In this time frame is when we can select which OpMode will run.

Our "operationSelected" variable will ultimately set which OpMode will run after receiving input from the function "selectOperation". By default, it is set to run TeleOp if the PS/Home button is never pressed.
The "selectOperation" function will output the end state to change this variable. The PS/Home button on the gamepad will progress the if/else statement used for determining this state. We'll break this down further below!

After "waitForStart" is called, the robot will execute the appropriate function containing the Auto Blue, Auto Red, or TeleOp code based on what was selected.

Initializing Variables and Settings

Let's focus on the new variables added in this version of the code. To learn about the motor settings and existing variables check out Programming - Initialization.
First, we need to set up the variables used while selecting our state to mean something. In this case, rather than the variables being set to a number, we will set them to a name.

Additionally, we need our "operationSelected" to have a default program to run.

For autonomous we'll be using elapsed timers and encoders. Because of this we need to set up some needed information to be referenced later, starting with our timers.
autoLaunchTimer - controls how long the robot will launch balls during autonomous
autoDriveTimer - controls how long the robot can drive when backing away from the launch line

For drivetrain encoders, we can go ahead and do our math here needed to convert wheel size into appropriate ticks.

selectOperation Function

Unlike functions we've used previously, the "selectOperation" function is set up to take in an input then return an output after being completed. In this instance, "state" and "cycleNext" are being inputted then the output will be the variable "state".
Keep in mind the function will repeatedly run while the specified condition is true, in this case so long as the program is set to initialization. Therefore state will be an input and output between each cycle.
Functions such as this are created in Blocks by clicking the gear icon and adding desired inputs.

Cycling OpModes

As mentioned above, our cycleNext is tied to our PS/Home button and if it was pressed (not being held). Each time it is pressed, "state" will progress one step in our if/else statement moving through each option.
In short, our if/else is checking what "state" is currently set to then proceeding to the next option if the button is pressed again. In the event the input is not received properly, a warning will be returned instead as our final "else".
Telemetry Outputs

Based on which "state" or OpMode has been selected, the Driver Hub will showcase different telemetry reads. First is the telemetry to provide the directions for cycling OpModes and the current selection.

If an autonomouse mode has been selected, then the Driver Hub will read out a prompt to turn on the "Auto timer" built into the Driver Station App for autonomous. This will prevent the robot from continuing to run after the 30 second auto period.

Lastly is another instruction for starting the program when ready. You'll see at the bottom of our function the return of "state" to be used.

Last updated
Was this helpful?