Search…
Programming Autonomous

Basic Autonomous Strategy

When we were planning for this years kickoff concept one thing we really wanted to address was the accessibility of autonomous scoring achievements. Certain barriers of entry to autonomous programming keep teams from scoring points during the autonomous period. What are these barriers? In our experience as FTC alumni and mentors, time and programming knowledge are the major reasons teams don't create autonomous programs. If a team feels like they don't have the time or skills, programming an autonomous can seem overwhelming. However, many autonomous scoring achievements, like navigation, are easily obtainable objectives that do not require a deep knowledge of programming.
After identifying this as a goal, analyzing the game, and laying out what a starter bot would look like; we decide to explore what a basic autonomous would be for the Freight Frenzy season. There are two major task we feel are achievable.
  • Placing the Pre-Load Box on level 1 of the Alliance Shipping Hub
  • Navigating to the Warehouse
As you may recall from the Game Breakdown, each robot must start with a pre-loaded box. For this season's randomization element, a robot determines what position the barcode is in and places the pre-loaded box on the corresponding level of the shipping hub. Using a vision component, like a webcam, to determine the position of the barcode is a more advanced programming concept. However, since placing freight on any level of the alliance shipping hub is worth 6 points (regardless of level), we think that placing the block on the 1st level of the shipping hub is an attainable task. This objective also comes with a 33% chance of getting the autonomous bonus.
Once we decided on placing the pre-loaded box the next decision to make was where to navigate: the warehouse or the storage unit. We chose the warehouse for a few different reasons. Parking in or completely in the warehouse is worth more points that parking in or completely in the storage unit. The warehouse also has a larger surface area than the storage unit, which means that less tuning is needed to get completely in.
We want to start with these two tasks for ease of access reasons. The important thing with programming is to work incrementally. Setting starting tasks allows us to focus on one section of the autonomous at a time, rather than get overwhelmed attempting to solve all the pieces. After tuning and working our autonomous to the point that we want, we can look at other tasks, like adding duck delivery to our lists of tasks.

Autonomous Path

Once we identified what objectives we wanted to achieve in autonomous we created a a basic path. We decided to have the robot start on the red alliance side of the field near the carousel. From there we identified the following basic elements of the path:
  1. 1.
    Go to shipping hub and drop off the pre-loaded box
  2. 2.
    Head towards Warehouse and straighten out
  3. 3.
    Drive into warehouse and park
This path design is for a red alliance autonomous. Adjustments will need to be made to create blue alliance autonomous.
We know from our mockups for the basic starter kit robot that we want a differential drivetrain, single jointed arm, and roller intake. A differential drivetrain combined with limited the amount of space between the shipping hub and the field wall, makes turning in that space difficult. There is also a risk of the arm knocking into the shipping hub. Taking into consideration what the basic robot design was we knew we needed to make some adjustments to our path.
  • Path 1
    • Drive towards shipping hub
    • Turn slightly
  • Path 2
    • Lift arm to level 1 height
    • Get closer to shipping hub while arm is still lifted
    • Outtake pre-loaded box
  • Path 3
    • Reverse towards towards storage unit
    • Turn slight to angle towards warehouse
  • Path 4
    • Drive into warehouse
    • Park

Programming Autonomous - Blocks

This section makes the assumption that you have learned some of the FTC programming basics by going through the Hello Robot guide. If you have not gone through this guide please walk through it before proceeding.
In the Hello Robot - Encoder Navigation, we covered how to useRUN_TO_POSITION to create paths for the Class bot. We decided to use the same basic concepts to create the path for the Freight Frenzy autonomous. To start we created the code for the first path.
Like with the Encoder Navigation guide, we created several constant variables to convert from encoder ticks per rotation of the HD Hex Motors (drivetrain motors) to ticks per millimeter traveled. We further converted this value to ticks per inch traveled. TheDRIVE_GEAR_REDUCTION variable was created by following the same guidelines from the Total Gear Reduction formula; using our planned gear ratios for the motor gearboxes and for transmitting motion to the wheels.
Another change we made was to howrightTarget and leftTarget are calculated. To reduce the need to useSTOP_AND_RESET_ENCODER mode before each path, we instead take the current position of the motor encoders and add the calculate ticks needed to go to target to the current position.
Once we had a general idea of the first path down and tested, we knew we were going to have to repeat the following segment of code for each path that requires the robot to drive.
This is a rather large segment of code, while duplicating it would allow us to repeat it, we decided instead to move it into a function. Functions in programming, much like mathematical functions, are a set of instructions that are bundled together to reach a particular goal. Since we are using the same (or similar) set of instructions to create each drive path, we can reduce clutter by creating a function that allows us to repeat these actions over and over again.
Lets start by creating a function called drive.
Before creating our function we looked at what information in our instructions needs to be editable. Each path will have a different target distance and we may want to change the speed the robot is running. Knowing this we create three parameters:power, leftInches, andrightInches. By adding parameters we are requiring that each instance of the function must be given a value for power (or speed) and the number of inches we want each motor to move.
Now that we have the basics of our function we can add in the motor code.
The parameter that we pass through the function act similar to variables. We used the rightInches and leftInches parameters to help us calculate the target position. We also use power to set speed.
With the function created we removed the motor code from our main op mode and reference the function instead. To confirm the function worked as expected we passed the parameter we knew lined up with the first path.
The leftInches and rightInches parameters are set to different values. We did this so that the robot would move in a relatively straight line and then turn slightly. For more information on the part speed and direction play in how drivetrains move, check out our Robot Navigation guide.
From here we can work to create our other paths. We know the next action we need to make is to lift the arm and hold that position. We know that RUN_TO_POSITION alone will not hold the arm for the amount duration needed to move forward and outtake the pre-loaded box. So we used the elapsed time function to do path 2.
We did some testing with the elapsed time solution. While it was able to score consistently, there are some flaws with this solution. Since path two is included in a loop the reference to our function, repeats itself; meaning that the drivetrain moves forward twice before the condition of the loop is met. In general, a flaw of using elapsed time, is that basing movement on time isn't a guarantee for consistency. That being said, in testing and refining our code to our robot we were able to score the pre-loaded box about 80% of the time. This met our basic standard for our autonomous.
Once we had path two figured out, we were able to do some basic testing to create paths 3 and 4 using our function.
This basic code allowed us to achieve our basic autonomous strategy. Bare in mind that we generated the power, rightInches, leftInches, and elapsed time numbers doing basic testing with our Class Bot and then made some assumptions from the other mechanisms we made. You will need to make adjustments based on your own mechanisms and hardware components To fine tune this code for your robot, test the code and adjust the number accordingly for the best results. Good luck!

Programming Autonomous - OnBot Java

This section makes the assumption that you have learned some of the FTC programming basics by going through the Hello Robot guide. If you have not gone through this guide please walk through it before proceeding.
In the Hello Robot - Encoder Navigation, we covered how to useRUN_TO_POSITION to create paths for the Class bot. We decided to use the same basic concepts to create the path for the Freight Frenzy autonomous. To start we created the code for the first path.
1
@Autonomous
2
public class FreightFenzy_REDAuton1 extends LinearOpMode {
3
4
private DcMotor RightDrive;
5
private DcMotor LeftDrive;
6
private DcMotor Arm;
7
private DcMotor Intake;
8
9
//Convert from the counts per revolution of the encoder to counts per inch
10
static final double HD_COUNTS_PER_REV = 28;
11
static final double DRIVE_GEAR_REDUCTION = 20.15293;
12
static final double WHEEL_CIRCUMFERENCE_MM = 90 * Math.PI;
13
static final double DRIVE_COUNTS_PER_MM = (HD_COUNTS_PER_REV * DRIVE_GEAR_REDUCTION) / WHEEL_CIRCUMFERENCE_MM;
14
static final double DRIVE_COUNTS_PER_IN = DRIVE_COUNTS_PER_MM * 25.4;
15
16
@Override
17
public void runOpMode() {
18
19
RightDrive = hardwareMap.get(DcMotor.class, "RightDrive");
20
LeftDrive = hardwareMap.get(DcMotor.class, "LeftDrive");
21
Arm = hardwareMap.get(DcMotor.class, "Arm");
22
Intake = hardwareMap.get(DcMotor.class, "Intake");
23
24
// reverse left drive motor direciton
25
LeftDrive.setDirection(DcMotorSimple.Direction.REVERSE);
26
27
waitForStart();
28
if (opModeIsActive()) {
29
// Create target positions
30
rightTarget = RightDrive.getCurrentPosition() + (int)(30 * DRIVE_COUNTS_PER_IN);
31
leftTarget = LeftDrive.getCurrentPosition() + (int)(15 * DRIVE_COUNTS_PER_IN);
32
33
// set target position
34
LeftDrive.setTargetPosition(leftTarget);
35
RightDrive.setTargetPosition(rightTarget);
36
37
//switch to run to position mode
38
LeftDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
39
RightDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
40
41
//run to position at the desiginated power
42
LeftDrive.setPower(0.7);
43
RightDrive.setPower(0.7);
44
45
// wait until both motors are no longer busy running to position
46
while (opModeIsActive() && (LeftDrive.isBusy() || RightDrive.isBusy())) {
47
}
48
49
// set motor power back to 0
50
LeftDrive.setPower(0);
51
RightDrive.setPower(0);
52
}
53
}
54
}
Copied!
Like with the Encoder Navigation guide, we created several constant variables to convert from encoder ticks per rotation of the HD Hex Motors (drivetrain motors) to ticks per millimeter traveled. We further converted this value to ticks per inch traveled. TheDRIVE_GEAR_REDUCTION variable was created by following the same guidelines from the Total Gear Reduction formula; using our planned gear ratios for the motor gearboxes and for transmitting motion to the wheels.
Another change we made was to howrightTarget and leftTarget are calculated. To reduce the need to useSTOP_AND_RESET_ENCODER mode before each path,; we instead take the current position of the motor encoders and add the calculate ticks needed to go to target to the current position.
Once we had a general idea of the first path down and tested, we knew we were going to have to repeat the following segment of code for each path that requires the robot to drive.
1
if (opModeIsActive()) {
2
// Create target positions
3
rightTarget = RightDrive.getCurrentPosition() + (int)(30 * DRIVE_COUNTS_PER_IN);
4
leftTarget = LeftDrive.getCurrentPosition() + (int)(15 * DRIVE_COUNTS_PER_IN);
5
6
// set target position
7
LeftDrive.setTargetPosition(leftTarget);
8
RightDrive.setTargetPosition(rightTarget);
9
10
//switch to run to position mode
11
LeftDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
12
RightDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
13
14
//run to position at the desiginated power
15
LeftDrive.setPower(0.7);
16
RightDrive.setPower(0.7);
17
18
// wait until both motors are no longer busy running to position
19
while (opModeIsActive() && (LeftDrive.isBusy() || RightDrive.isBusy())) {
20
}
21
22
// set motor power back to 0
23
LeftDrive.setPower(0);
24
RightDrive.setPower(0);
25
}
26
Copied!
This is a rather large segment of code, while duplicating it would allow us to repeat it, we decided instead to move it into a function. Functions in programming, much like mathematical functions, are a set of instructions that are bundled together to reach a particular goal. Since we are using the same (or similar) set of instructions to create each drive path, we can reduce clutter by creating a function that allows us to repeat these actions over and over again.
Lets start by creating a function called drive.
1
//drive function
2
private void drive() {
3
4
}
Copied!
Before creating our function we looked at what information in our instructions needs to be editable. Each path will have a different target distance and we may want to change the speed the robot is running. Knowing this we create three parameters:power, leftInches, andrightInches. By adding parameters we are requiring that each instance of the function must be given a value for power (or speed) and the number of inches we want each motor to move.
1
// drive function intakes 3 parameters
2
private void drive(double power, double leftInches, double rightInches) {
3
4
}
Copied!
Now that we have the basics of our function we can add in the motor code.
1
private void drive(double power, double leftInches, double rightInches) {
2
int rightTarget;
3
int leftTarget;
4
5
if (opModeIsActive()) {
6
// Create target positions
7
rightTarget = RightDrive.getCurrentPosition() + (int)(rightInches * DRIVE_COUNTS_PER_IN);
8
leftTarget = LeftDrive.getCurrentPosition() + (int)(leftInches * DRIVE_COUNTS_PER_IN);
9
10
// set target position
11
LeftDrive.setTargetPosition(leftTarget);
12
RightDrive.setTargetPosition(rightTarget);
13
14
//switch to run to position mode
15
LeftDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
16
RightDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
17
18
//run to position at the desiginated power
19
LeftDrive.setPower(power);
20
RightDrive.setPower(power);
21
22
// wait until both motors are no longer busy running to position
23
while (opModeIsActive() && (LeftDrive.isBusy() || RightDrive.isBusy())) {
24
}
25
26
// set motor power back to 0
27
LeftDrive.setPower(0);
28
RightDrive.setPower(0);
29
}
30
}
Copied!
The parameter that we pass through the function act similar to variables. We used the rightInches and leftInchesparameters to help us calculate the target position. We also use power to set speed.
With the function created we can remove the motor code from our main op mode and reference the function instead. To confirm the function worked as expected we passed the parameter we knew lined up with the first segment.
1
@Override
2
public void runOpMode() {
3
4
RightDrive = hardwareMap.get(DcMotor.class, "RightDrive");
5
LeftDrive = hardwareMap.get(DcMotor.class, "LeftDrive");
6
Arm = hardwareMap.get(DcMotor.class, "Arm");
7
Intake = hardwareMap.get(DcMotor.class, "Intake");
8
9
// reverse left drive motor direciton
10
LeftDrive.setDirection(DcMotorSimple.Direction.REVERSE);
11
12
waitForStart();
13
if (opModeIsActive()) {
14
15
// segment 1
16
drive(0.7, 30, 15);
17
}
18
}
Copied!
The leftInches and rightInches parameters are set to different values. We did this so that the robot would move in a relatively straight line and then turn slightly. For more information on the part speed and direction play in how drivetrains move, check out our Robot Navigation guide.
From here we can work to create our other paths. We know the next action we need to make is to lift the arm and hold that position. We know that RUN_TO_POSITION alone will not hold the arm for the amount duration needed to move forward and outtake the pre-loaded box. So we used the elapsed time function to do segment 2.
1
static final double HD_COUNTS_PER_REV = 28;
2
static final double DRIVE_GEAR_REDUCTION = 20.15293;
3
static final double WHEEL_CIRCUMFERENCE_MM = 90 * Math.PI;
4
static final double DRIVE_COUNTS_PER_MM = (HD_COUNTS_PER_REV * DRIVE_GEAR_REDUCTION) / WHEEL_CIRCUMFERENCE_MM;
5
static final double DRIVE_COUNTS_PER_IN = DRIVE_COUNTS_PER_MM * 25.4;
6
7
//Create elapsed time variable and an instance of elapsed time
8
private ElapsedTime runtime = new ElapsedTime();
Copied!
1
waitForStart();
2
if (opModeIsActive()) {
3
4
//segment 1
5
drive(0.7, 30, 15);
6
7
runtime.reset(); // reset elapsed time timer
8
9
//segment 2 - lift arm, drive to shipping hub, outtake freight
10
while (opModeIsActive() && runtime.seconds() <= 7) {
11
12
//lift arm and hold
13
Arm.setTargetPosition(120);
14
Arm.setMode(DcMotor.RunMode.RUN_TO_POSITION);
15
Arm.setPower(0.3);
16
17
//drive forward for 1 second
18
while (runtime.seconds() > 2 && runtime.seconds() <= 3) {
19
drive(0.4, 4, 4);
20
}
21
22
//run intake
23
while (runtime.seconds() > 4 && runtime.seconds() <= 7) {
24
Intake.setPower(-0.6);
25
}
26
27
// turn off arm and intake
28
Arm.setPower(0);
29
Intake.setPower(0);
30
31
}
Copied!
We did some testing with the elapsed time solution. While it was able to score consistently, there are some flaws with this solution. Since path two is included in a loop the reference to our function, repeats itself; meaning that the drivetrain moves forward twice before the condition of the loop is met. In general, a flaw of using elapsed time, is that basing movement on time isn't a guarantee for consistency. That being said, in testing and refining our code to our robot we were able to score the pre-loaded box about 80% of the time. This met our basic standard for our autonomous.
Once we had path two figured out, we were able to do some basic testing to create segments 3 and 4 using our function.
1
package org.firstinspires.ftc.teamcode;
2
3
import com.qualcomm.robotcore.eventloop.opmode.Autonomous;
4
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
5
import com.qualcomm.robotcore.hardware.DcMotor;
6
import com.qualcomm.robotcore.hardware.DcMotorSimple;
7
import com.qualcomm.robotcore.util.ElapsedTime;
8
9
@Autonomous(name = "FreightFenzy_REDAuton1 (Java)")
10
public class FreightFenzy_REDAuton1 extends LinearOpMode {
11
12
private DcMotor RightDrive;
13
private DcMotor LeftDrive;
14
private DcMotor Arm;
15
private DcMotor Intake;
16
17
//Convert from the counts per revolution of the encoder to counts per inch
18
static final double HD_COUNTS_PER_REV = 28;
19
static final double DRIVE_GEAR_REDUCTION = 20.15293;
20
static final double WHEEL_CIRCUMFERENCE_MM = 90 * Math.PI;
21
static final double DRIVE_COUNTS_PER_MM = (HD_COUNTS_PER_REV * DRIVE_GEAR_REDUCTION) / WHEEL_CIRCUMFERENCE_MM;
22
static final double DRIVE_COUNTS_PER_IN = DRIVE_COUNTS_PER_MM * 25.4;
23
24
//Create elapsed time variable and an instance of elapsed time
25
private ElapsedTime runtime = new ElapsedTime();
26
27
// Drive function with 3 parameters
28
private void drive(double power, double leftInches, double rightInches) {
29
int rightTarget;
30
int leftTarget;
31
32
if (opModeIsActive()) {
33
// Create target positions
34
rightTarget = RightDrive.getCurrentPosition() + (int)(rightInches * DRIVE_COUNTS_PER_IN);
35
leftTarget = LeftDrive.getCurrentPosition() + (int)(leftInches * DRIVE_COUNTS_PER_IN);
36
37
// set target position
38
LeftDrive.setTargetPosition(leftTarget);
39
RightDrive.setTargetPosition(rightTarget);
40
41
//switch to run to position mode
42
LeftDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
43
RightDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
44
45
//run to position at the desiginated power
46
LeftDrive.setPower(power);
47
RightDrive.setPower(power);
48
49
// wait until both motors are no longer busy running to position
50
while (opModeIsActive() && (LeftDrive.isBusy() || RightDrive.isBusy())) {
51
}
52
53
// set motor power back to 0
54
LeftDrive.setPower(0);
55
RightDrive.setPower(0);
56
}
57
}
58
59
60
@Override
61
public void runOpMode() {
62
63
RightDrive = hardwareMap.get(DcMotor.class, "RightDrive");
64
LeftDrive = hardwareMap.get(DcMotor.class, "LeftDrive");
65
Arm = hardwareMap.get(DcMotor.class, "Arm");
66
Intake = hardwareMap.get(DcMotor.class, "Intake");
67
68
69
LeftDrive.setDirection(DcMotorSimple.Direction.REVERSE);
70
71
waitForStart();
72
if (opModeIsActive()) {
73
74
//segment 1
75
drive(0.7, 30, 15);
76
77
runtime.reset(); // reset elapsed time timer
78
79
//segment 2 - lift arm, drive to shipping hub, outtake freight
80
while (opModeIsActive() && runtime.seconds() <= 7) {
81
82
//lift arm and hold
83
Arm.setTargetPosition(120);
84
Arm.setMode(DcMotor.RunMode.RUN_TO_POSITION);
85
Arm.setPower(0.3);
86
87
//drive forward for 1 second
88
while (runtime.seconds() > 2 && runtime.seconds() <= 3) {
89
drive(0.4, 4, 4);
90
}
91
92
//run intake
93
while (runtime.seconds() > 4 && runtime.seconds() <= 7) {
94
Intake.setPower(-0.6);
95
}
96
97
// turn off arm and intake
98
Arm.setPower(0);
99
Intake.setPower(0);
100
101
//segment 3 - reverse to get better angle
102
drive(0.7, -15, -30);
103
104
//segment 4 - drive into warehouse
105
drive(1, 90, 90);
106
}
107
}
108
}
Copied!
This basic code allowed us to achieve our basic autonomous strategy. Bare in mind that we generated the power, rightInches, leftInches, and elapsed time numbers doing basic testing with our Class Bot and then made some assumptions from the other mechanisms we made. You will need to make adjustments based on your own mechanisms and hardware components. To fine tune this code for your robot, test the code and adjust the number accordingly for the best results.

Autonomous Wrap Up

The above walkthrough should give you the basic information to get started with programming your own autonomous. We recommend testing and tuning your code to make sure everything works consistently. Once you have a functioning code, explore different ways you can add on this code to elevate it. The starting position for the path puts you in a great place to deliver the duck, which can easily be reworked into your path. Also it is worth considering what your alliance partner may do in a match to better optimize the code.
If you are new to FTC programming check out our Hello Robot programming guide to learn the basics of programming within the FTC SDK.
Last modified 3mo ago