This will cover the boilerplate stuff that you need to set up. The rest of your OpMode will pretty much consist of RoadRunner TrajectorySequenceBuilder methods. Learn more about those on learnroadrunner.com.
Preview:
package org.firstinspires.ftc.teamcode.fy23;
// some imports here
@Autonomous()
public class NewAutonomous extends LinearOpMode {
Robot robot;
public void runOpMode() {
// This one line prepares everything we need for RoadRunner-powered autonomous.
// Use a different robot if necessary.
robot = new Robot(RobotRoundhouse.getRobotAParameters(hardwareMap));
waitForStart();
// The TrajectorySequenceBuilder needs to know our initial pose. We'll assume it's 0 since we haven't moved yet.
Pose2d startPose = new Pose2d(x:0, y:0, heading:0);
TrajectorySequence trajSeq = robot.drive.trajectorySequenceBuilder(startPose)
// Put your other TSB methods here.
.build()
robot.drive.followTrajectory(trajSeq);
telemetry.addLine("Finished.");
telemetry.update();
}
}
robot = RobotRoundhouse.getRobotAParameters(hardwareMap);
The Robot class does all of the initialization work for us. It does all the stuff with the HardwareMap, sets up all the subsystems, and sets up RoadRunner. This one line is all the initialization we need for Autonomous.
Pose2d startPose = new Pose2d(x:0, y:0, heading:0);
TrajectorySequence trajSeq = robot.drive.trajectorySequenceBuilder(startPose)
// Put your other TSB methods here.
.build()
This line uses the TrajectorySequenceBuilder to build a TrajectorySequence that it can follow. We will store that TrajectorySequence into a variable named trajSeq.
The TrajectorySequence is where all of the actual Autonomous navigation and actions are programmed.
All of the navigation and the structure of the sequence is handled by RoadRunner's TrajectorySequenceBuilder.
You can learn more about it at learnroadrunner.com,
but for now we will continue with a brief example.
To make other things happen, such as moving the manipulator, add markers to call other methods.
For example, let's make a trajectory where we go forward and then go right, and we want the arm to move to a 60
degree angle while we're moving right. Here's our TSB methods:
.forward(5)
.addTemporalMarker(() -> robot.arm.setPivotAngle(AngleUnit.DEGREES, 60));
.strafeRight(5)
The action specified by the marker will run in the background, so we're doing two things - strafing right and raising the arm - simultaneously, saving valuable time. To perform more complicated sequences or create state machines, you will want to create additional methods (outside of runOpMode()) and have your markers call them. This will keep your list of TSB methods clean and readable.
robot.drive.followTrajectory(trajSeq);
This tells the drive subsystem to carry out the trajectory we created.
telemetry.addLine("Finished.");
telemetry.update();
Here we just let the drivers know that the OpMode is done and they can now queue up TeleOp. We never started a loop, so we're done. The OpMode will end here.