Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[examples] Call resetOdometry() when controller command is executed #5905

Merged
merged 17 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,7 @@ frc2::CommandPtr RobotContainer::GetAutonomousCommand() {
// Pass the config
config);

// Reset odometry to the starting pose of the trajectory.
m_drive.ResetOdometry(exampleTrajectory.InitialPose());

frc2::CommandPtr mecanumDriveCommand =
frc2::CommandPtr mecanumControllerCommand =
frc2::MecanumControllerCommand(
exampleTrajectory, [this]() { return m_drive.GetPose(); },

Expand Down Expand Up @@ -110,8 +107,16 @@ frc2::CommandPtr RobotContainer::GetAutonomousCommand() {
{&m_drive})
.ToPtr();

rzblue marked this conversation as resolved.
Show resolved Hide resolved
// Reset odometry to the initial pose of the trajectory, run path following
// command, then stop at the end.
return frc2::cmd::Sequence(
std::move(mecanumDriveCommand),
frc2::InstantCommand([this] { m_drive.Drive(0, 0, 0, false); }, {})
frc2::InstantCommand(
[this, &exampleTrajectory]() {
m_drive.ResetOdometry(exampleTrajectory.InitialPose());
},
{})
.ToPtr(),
std::move(mecanumControllerCommand),
frc2::InstantCommand([this]() { m_drive.Drive(0, 0, 0, false); }, {})
.ToPtr());
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,14 @@ frc2::CommandPtr RobotContainer::GetAutonomousCommand() {
[this](auto left, auto right) { m_drive.TankDriveVolts(left, right); },
{&m_drive})};

// Reset odometry to the starting pose of the trajectory.
m_drive.ResetOdometry(exampleTrajectory.InitialPose());

return std::move(ramseteCommand)
.BeforeStarting(
// Reset odometry to the initial pose of the trajectory, run path following
// command, then stop at the end.
return frc2::cmd::RunOnce(
[this, &exampleTrajectory] {
m_drive.ResetOdometry(exampleTrajectory.InitialPose());
},
{})
.AndThen(std::move(ramseteCommand))
.AndThen(
frc2::cmd::RunOnce([this] { m_drive.TankDriveVolts(0_V, 0_V); }, {}));
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,7 @@ frc2::CommandPtr RobotContainer::GetAutonomousCommand() {
thetaController.EnableContinuousInput(units::radian_t{-std::numbers::pi},
units::radian_t{std::numbers::pi});

// Reset odometry to the starting pose of the trajectory.
m_drive.ResetOdometry(exampleTrajectory.InitialPose());

frc2::CommandPtr swerveDriveCommand =
frc2::CommandPtr swerveControllerCommand =
frc2::SwerveControllerCommand<4>(
exampleTrajectory, [this]() { return m_drive.GetPose(); },

Expand All @@ -92,8 +89,16 @@ frc2::CommandPtr RobotContainer::GetAutonomousCommand() {
{&m_drive})
.ToPtr();

// Reset odometry to the initial pose of the trajectory, run path following
// command, then stop at the end.
return frc2::cmd::Sequence(
std::move(swerveDriveCommand),
frc2::InstantCommand(
[this, &exampleTrajectory]() {
m_drive.ResetOdometry(exampleTrajectory.InitialPose());
},
{})
.ToPtr(),
std::move(swerveControllerCommand),
frc2::InstantCommand(
[this] { m_drive.Drive(0_mps, 0_mps, 0_rad_per_s, false); }, {})
.ToPtr());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import edu.wpi.first.wpilibj.examples.mecanumcontrollercommand.Constants.OIConstants;
import edu.wpi.first.wpilibj.examples.mecanumcontrollercommand.subsystems.DriveSubsystem;
import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.Commands;
import edu.wpi.first.wpilibj2.command.InstantCommand;
import edu.wpi.first.wpilibj2.command.MecanumControllerCommand;
import edu.wpi.first.wpilibj2.command.RunCommand;
Expand Down Expand Up @@ -85,7 +86,7 @@ public Command getAutonomousCommand() {
// Add kinematics to ensure max speed is actually obeyed
.setKinematics(DriveConstants.kDriveKinematics);

// An example trajectory to follow. All units in meters.
// An example trajectory to follow. All units in meters.
Trajectory exampleTrajectory =
TrajectoryGenerator.generateTrajectory(
// Start at the origin facing the +X direction
Expand Down Expand Up @@ -121,10 +122,11 @@ public Command getAutonomousCommand() {
m_robotDrive::setDriveMotorControllersVolts, // Consumer for the output motor voltages
m_robotDrive);

// Reset odometry to the starting pose of the trajectory.
m_robotDrive.resetOdometry(exampleTrajectory.getInitialPose());

// Run path following command, then stop at the end.
return mecanumControllerCommand.andThen(() -> m_robotDrive.drive(0, 0, 0, false));
// Reset odometry to the initial pose of the trajectory, run path following
// command, then stop at the end.
return Commands.sequence(
new InstantCommand(() -> m_robotDrive.resetOdometry(exampleTrajectory.getInitialPose())),
mecanumControllerCommand,
new InstantCommand(() -> m_robotDrive.drive(0, 0, 0, false)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

package edu.wpi.first.wpilibj.examples.ramsetecommand;

import static edu.wpi.first.wpilibj.XboxController.Button;

import edu.wpi.first.math.controller.PIDController;
import edu.wpi.first.math.controller.RamseteController;
import edu.wpi.first.math.controller.SimpleMotorFeedforward;
Expand All @@ -17,11 +15,13 @@
import edu.wpi.first.math.trajectory.TrajectoryGenerator;
import edu.wpi.first.math.trajectory.constraint.DifferentialDriveVoltageConstraint;
import edu.wpi.first.wpilibj.XboxController;
import edu.wpi.first.wpilibj.XboxController.Button;
import edu.wpi.first.wpilibj.examples.ramsetecommand.Constants.AutoConstants;
import edu.wpi.first.wpilibj.examples.ramsetecommand.Constants.DriveConstants;
import edu.wpi.first.wpilibj.examples.ramsetecommand.Constants.OIConstants;
import edu.wpi.first.wpilibj.examples.ramsetecommand.subsystems.DriveSubsystem;
import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.Commands;
import edu.wpi.first.wpilibj2.command.InstantCommand;
import edu.wpi.first.wpilibj2.command.RamseteCommand;
import edu.wpi.first.wpilibj2.command.RunCommand;
Expand Down Expand Up @@ -97,7 +97,7 @@ public Command getAutonomousCommand() {
// Apply the voltage constraint
.addConstraint(autoVoltageConstraint);

// An example trajectory to follow. All units in meters.
// An example trajectory to follow. All units in meters.
Trajectory exampleTrajectory =
TrajectoryGenerator.generateTrajectory(
// Start at the origin facing the +X direction
Expand Down Expand Up @@ -126,10 +126,10 @@ public Command getAutonomousCommand() {
m_robotDrive::tankDriveVolts,
m_robotDrive);

// Reset odometry to the starting pose of the trajectory.
m_robotDrive.resetOdometry(exampleTrajectory.getInitialPose());

// Run path following command, then stop at the end.
return ramseteCommand.andThen(() -> m_robotDrive.tankDriveVolts(0, 0));
// Reset odometry to the initial pose of the trajectory, run path following
// command, then stop at the end.
return Commands.runOnce(() -> m_robotDrive.resetOdometry(exampleTrajectory.getInitialPose()))
.andThen(ramseteCommand)
.andThen(Commands.runOnce(() -> m_robotDrive.tankDriveVolts(0, 0)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import edu.wpi.first.wpilibj.examples.swervecontrollercommand.Constants.OIConstants;
import edu.wpi.first.wpilibj.examples.swervecontrollercommand.subsystems.DriveSubsystem;
import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.Commands;
import edu.wpi.first.wpilibj2.command.InstantCommand;
import edu.wpi.first.wpilibj2.command.RunCommand;
import edu.wpi.first.wpilibj2.command.SwerveControllerCommand;
import edu.wpi.first.wpilibj2.command.button.JoystickButton;
Expand Down Expand Up @@ -82,7 +84,7 @@ public Command getAutonomousCommand() {
// Add kinematics to ensure max speed is actually obeyed
.setKinematics(DriveConstants.kDriveKinematics);

// An example trajectory to follow. All units in meters.
// An example trajectory to follow. All units in meters.
Trajectory exampleTrajectory =
TrajectoryGenerator.generateTrajectory(
// Start at the origin facing the +X direction
Expand Down Expand Up @@ -111,10 +113,11 @@ public Command getAutonomousCommand() {
m_robotDrive::setModuleStates,
m_robotDrive);

// Reset odometry to the starting pose of the trajectory.
m_robotDrive.resetOdometry(exampleTrajectory.getInitialPose());

// Run path following command, then stop at the end.
return swerveControllerCommand.andThen(() -> m_robotDrive.drive(0, 0, 0, false));
// Reset odometry to the initial pose of the trajectory, run path following
// command, then stop at the end.
return Commands.sequence(
new InstantCommand(() -> m_robotDrive.resetOdometry(exampleTrajectory.getInitialPose())),
swerveControllerCommand,
new InstantCommand(() -> m_robotDrive.drive(0, 0, 0, false)));
}
}
Loading