Autonomous Usage
How to use Pedro in autonomous
We now have everything needed to build an autonomous: a follower, Poses, paths, and a way to follow them.
If you need to revisit a step, see Setting Up Your Autonomous, Creating a Pose, Creating a Path, or Following a Path.
We'll look at how to build an autonomous both without Ivy and with Ivy.
With Ivy
Ivy lets us organize the same autonomous as commands instead of manually keeping track of states.
Add the Ivy imports:
import com.pedropathing.ivy.Command;
import com.pedropathing.ivy.Scheduler;
import static com.pedropathing.ivy.Scheduler.schedule;
import static com.pedropathing.ivy.groups.Groups.sequential;
import static com.pedropathing.ivy.pedro.PedroCommands.follow;For a larger autonomous, create the Poses the robot needs to visit:
private final PoseFactory poseFactory = PoseFactory.degrees();
private final Pose startPose = poseFactory.of(24, 24, 0);
private final Pose scorePose = poseFactory.of(48, 48, 90);
private final Pose parkPose = poseFactory.of(72, 48, 90);Creating the Paths
Create the paths inside of their own methods, calling each method should return a Path object:
private Path startToScore() {
return line(startPose, scorePose).linear(startPose, scorePose);
}
private Path park(){
return line(scorePose, parkPose).linear(scorePose, parkPose);
}Creating the Routine
sequential() runs each command one after another:
private Command autoRoutine() {
return sequential(
follow(follower, startToScore()),
// Add mechanism commands here.
follow(follower, park())
);
}The second path begins after the first follow() command finishes.
Other Ivy commands can be placed between the paths or run alongside them using different command groups.
Running the Routine
Reset Ivy and create the follower inside init():
@Override
public void init() {
Scheduler.reset();
follower = Constants.create(hardwareMap);
follower.setPose(startPose);
follower.update();
}Schedule the routine inside start():
@Override
public void start() {
schedule(autoRoutine());
}Then update both systems inside loop():
@Override
public void loop() {
follower.update();
Scheduler.execute();
// add your other methods needed in the loop here
}Tip
See the Ivy documentation to learn about commands, sequential groups, parallel groups, and other ways to organize an autonomous.
Complete example
And that is how you make an Autonomous with Ivy! Here is a completed version for reference, with some added extras.
import com.pedropathing.ivy.Command;
import com.pedropathing.ivy.Scheduler;
import static com.pedropathing.ivy.Scheduler.schedule;
import static com.pedropathing.ivy.groups.Groups.sequential;
import static com.pedropathing.ivy.pedro.PedroCommands.follow;
@Autonomous
public class ExampleAuto extends OpMode {
private Follower follower;
private final PoseFactory poseFactory = PoseFactory.degrees();
// Poses
private final Pose startPose = poseFactory.of(24, 24, 0);
private final Pose startToScore = poseFactory.of(48, 48, 90);
private final Pose park = poseFactory.of(72, 48, 90);
// Path methods
private Path startToScore() {
return line(startPose, scorePose).linear(startPose, scorePose);
}
private Path park(){
return line(scorePose, parkPose).linear(scorePose, parkPose);
}
private Command autoRoutine() {
return sequential(
follow(follower, startToScore()),
// Add mechanism commands here.
follow(follower, park())
);
}
@Override
public void init() {
Scheduler.reset();
follower = Constants.create(hardwareMap);
follower.setPose(startPose);
follower.update();
}
@Override
public void start() {
schedule(autoRoutine());
}
@Override
public void loop() {
follower.update();
Scheduler.execute();
// add your other methods needed in the loop here
telemetryData.addData("X", follower.pose().x());
telemetryData.addData("Y", follower.pose().y());
telemetryData.addData("Heading", Math.toDegrees(follower.pose().heading()));
telemetry.addData("Follower Mode", follower.mode());
telemetry.update();
}
}Using other command libraries
We strongly recommend Ivy because it has builtin integration with PedroPathing. In addition, it is very simple and neat to create your own commands for your subystems with. If you want to use other command libraries, refer to Following a path on how to make your own follow command.
Last updated on