Programming - Autonomous Code
Once our OpMode is selected, we're ready to actually hit play and let the robot run! For this example, the TeleOp portion of the code has remained the same so we won't repeat looking at it here.
Instead let's break down our autonomous code options. For both Blue and Red Alliance versions of the code, the robot is intended to start against the goal while touching the launch line. It will automatically fire the pre-loaded balls for 10 seconds before backing up from the goal, turning, and driving straight back off the line.


The two versions of the code are nearly identical with the difference being which direction the robot needs to turn before backing up.


Let's take a closer look at how our auto code works.
Running Auto
Launching Balls

Our entire auto is contained to an if/else statement checking if opModeIsActive is true, meaning the "play" button has been pressed on the Driver Hub. Once this occurs, it'll read out that the OpMode is running.
To start launching balls into the target, the associated timer will first be reset. Elapsedtimers begin counting at their creation so it is important to reset them when they're actually going to be used to get a correct time.
With the timer running, the "bankShotAuto", the same used in TeleOp, will begin running for 10 seconds. This time can be adjusted, but was set to 10 seconds in the example as a safe window for teams to begin testing with and observing how their robot acts.

A telemetry readout to the Driver Hub will show how much time has passed according to the "autoLaunchTimer" to aid with making adjustments.

Once our timer is up, all our actuators will be set back to 0 power or velocity.

Driving in Auto
There are two parts to our program for the robot driving in autonomous. In our if/else statement, we have a portion of the "autoDrive" function taking in inputs to calculate the robot's movements. Let's looking at the function itself first.

Here our elapsed timer will reset to be ready for use with the robot's movements. Then the drivetrain motors are given a target position. This target position is calculated using the current position of the motor, the inputted distance in inches from the main function, and our conversion set up during initialization.

Next, we're change our motors to "RUN_TO_POSITION" mode for this autonomous. The power to be provided is set by the "speed" given in the main function.

With all that information gathered, our robot will now moved the specified direction and distance stopping when either the motors halt or the timer runs out. Using "call idle" allows our program to progress between the two functions.

Once our movement is complete, our drivetrain motors are set to turn to 0 power and reset back to "RUN_WITHOUT_ENCODER" in preparation for TeleOp.

Now let's look at the remainder of our main function of "doAuto".

For each portion of movement we have inputted:
speed- The set power for the motors
leftDistanceInch/rightDistanceInch- The target distance we want our robot to move to be calculated with our equation. This will be different for each motor when the robot is turning.
timeout_ms- this is the maximum time the elapsedtimer can count up to in milliseconds.
Let's take a look at each step of the robot's movement
Back Up
The robot will first back away from the goal, at half power, roughly 12 inches. If something happens where the robot cannot complete this, such as colliding with another robot, this step will timeout after 5 seconds.

Turn
Next the robot will turn at half power about 6 inches making a 90 degree angle. If something happens where the robot cannot complete this, such as colliding with another robot, this step will timeout after 5 seconds.

Drive Off Line
Finally the robot will back up at full power 50 inches to be off the line. This is much further than needed to be off the line so we encourage teams to adjust this value to get the robot positioned where they prefer.
If something happens where the robot cannot complete this, such as colliding with another robot, this step will timeout after 5 seconds.

Once the robot has finished moving, the code will stop and return to the initialization option.
Last updated
Was this helpful?