[examples] Ensure right side motors are inverted (#3836)

Fixes #3827
Adds MotorController inversion for right side, removes inversion in
setVoltage methods.

Also fixes various XboxController negations (was inconsistent throughout examples).
This commit is contained in:
sciencewhiz
2021-12-26 19:25:26 -08:00
committed by GitHub
parent baacbc8e24
commit dceb5364f4
42 changed files with 155 additions and 52 deletions

View File

@@ -18,6 +18,13 @@ class Robot : public frc::TimedRobot {
frc::Joystick m_stick{0};
public:
void RobotInit() override {
// We need to invert one side of the drivetrain so that positive voltages
// result in both sides moving forward. Depending on how your robot's
// gearbox is constructed, you might have to invert the left side instead.
m_rightMotor.SetInverted(true);
}
void TeleopPeriodic() override {
// Drive with arcade style
m_robotDrive.ArcadeDrive(m_stick.GetY(), m_stick.GetX());

View File

@@ -18,12 +18,19 @@ class Robot : public frc::TimedRobot {
frc::XboxController m_driverController{0};
public:
void RobotInit() override {
// We need to invert one side of the drivetrain so that positive voltages
// result in both sides moving forward. Depending on how your robot's
// gearbox is constructed, you might have to invert the left side instead.
m_rightMotor.SetInverted(true);
}
void TeleopPeriodic() override {
// Drive with split arcade style
// That means that the Y axis of the left stick moves forward
// and backward, and the X of the right stick turns left and right.
m_robotDrive.ArcadeDrive(m_driverController.GetLeftY(),
m_driverController.GetRightX());
m_robotDrive.ArcadeDrive(-m_driverController.GetLeftY(),
-m_driverController.GetRightX());
}
};

View File

@@ -17,8 +17,8 @@ RobotContainer::RobotContainer() {
// Set up default drive command
m_drive.SetDefaultCommand(frc2::RunCommand(
[this] {
m_drive.ArcadeDrive(m_driverController.GetLeftY(),
m_driverController.GetRightX());
m_drive.ArcadeDrive(-m_driverController.GetLeftY(),
-m_driverController.GetRightX());
},
{&m_drive}));
}

View File

@@ -16,6 +16,10 @@ DriveSubsystem::DriveSubsystem()
// Set the distance per pulse for the encoders
m_leftEncoder.SetDistancePerPulse(kEncoderDistancePerPulse);
m_rightEncoder.SetDistancePerPulse(kEncoderDistancePerPulse);
// We need to invert one side of the drivetrain so that positive voltages
// result in both sides moving forward. Depending on how your robot's
// gearbox is constructed, you might have to invert the left side instead.
m_rightMotors.SetInverted(true);
}
void DriveSubsystem::Periodic() {

View File

@@ -17,8 +17,8 @@ RobotContainer::RobotContainer() {
// Set up default drive command
m_drive.SetDefaultCommand(frc2::RunCommand(
[this] {
m_drive.ArcadeDrive(m_driverController.GetLeftY(),
m_driverController.GetRightX());
m_drive.ArcadeDrive(-m_driverController.GetLeftY(),
-m_driverController.GetRightX());
},
{&m_drive}));
}

View File

@@ -16,6 +16,10 @@ DriveSubsystem::DriveSubsystem()
// Set the distance per pulse for the encoders
m_leftEncoder.SetDistancePerPulse(kEncoderDistancePerPulse);
m_rightEncoder.SetDistancePerPulse(kEncoderDistancePerPulse);
// We need to invert one side of the drivetrain so that positive voltages
// result in both sides moving forward. Depending on how your robot's
// gearbox is constructed, you might have to invert the left side instead.
m_rightMotors.SetInverted(true);
}
void DriveSubsystem::Periodic() {

View File

@@ -18,8 +18,8 @@ RobotContainer::RobotContainer() {
// Set up default drive command
m_drive.SetDefaultCommand(frc2::RunCommand(
[this] {
m_drive.ArcadeDrive(m_driverController.GetLeftY(),
m_driverController.GetRightX());
m_drive.ArcadeDrive(-m_driverController.GetLeftY(),
-m_driverController.GetRightX());
},
{&m_drive}));
}

View File

@@ -16,8 +16,8 @@ RobotContainer::RobotContainer() {
// Set up default drive command
m_drive.SetDefaultCommand(frc2::RunCommand(
[this] {
m_drive.ArcadeDrive(m_driverController.GetLeftY(),
m_driverController.GetRightX());
m_drive.ArcadeDrive(-m_driverController.GetLeftY(),
-m_driverController.GetRightX());
},
{&m_drive}));
}

View File

@@ -11,7 +11,11 @@
class Robot : public frc::TimedRobot {
public:
Robot() {
m_right.SetInverted(true);
m_robotDrive.SetExpiration(100_ms);
// We need to invert one side of the drivetrain so that positive voltages
// result in both sides moving forward. Depending on how your robot's
// gearbox is constructed, you might have to invert the left side instead.
m_timer.Start();
}

View File

@@ -17,7 +17,13 @@
*/
class Robot : public frc::TimedRobot {
public:
void RobotInit() override { m_gyro.SetSensitivity(kVoltsPerDegreePerSecond); }
void RobotInit() override {
m_gyro.SetSensitivity(kVoltsPerDegreePerSecond);
// We need to invert one side of the drivetrain so that positive voltages
// result in both sides moving forward. Depending on how your robot's
// gearbox is constructed, you might have to invert the left side instead.
m_right.SetInverted(true);
}
/**
* The motor speed is set from the joystick while the DifferentialDrive

View File

@@ -16,10 +16,10 @@
class Robot : public frc::TimedRobot {
public:
void RobotInit() override {
// Invert the left side motors. You may need to change or remove this to
// Invert the right side motors. You may need to change or remove this to
// match your robot.
m_frontLeft.SetInverted(true);
m_rearLeft.SetInverted(true);
m_frontRight.SetInverted(true);
m_rearRight.SetInverted(true);
m_gyro.SetSensitivity(kVoltsPerDegreePerSecond);
}

View File

@@ -27,8 +27,8 @@ RobotContainer::RobotContainer() {
// Set up default drive command
m_drive.SetDefaultCommand(DefaultDrive(
&m_drive, [this] { return m_driverController.GetLeftY(); },
[this] { return m_driverController.GetRightX(); }));
&m_drive, [this] { return -m_driverController.GetLeftY(); },
[this] { return -m_driverController.GetRightX(); }));
}
void RobotContainer::ConfigureButtonBindings() {

View File

@@ -20,7 +20,14 @@
*/
class Drivetrain {
public:
Drivetrain() { m_gyro.Reset(); }
Drivetrain() {
m_gyro.Reset();
// We need to invert one side of the drivetrain so that positive voltages
// result in both sides moving forward. Depending on how your robot's
// gearbox is constructed, you might have to invert the left side instead.
m_frontRightMotor.SetInverted(true);
m_backRightMotor.SetInverted(true);
}
frc::MecanumDriveWheelSpeeds GetCurrentState() const;
void SetSpeeds(const frc::MecanumDriveWheelSpeeds& wheelSpeeds);

View File

@@ -34,6 +34,11 @@ DriveSubsystem::DriveSubsystem()
m_rearLeftEncoder.SetDistancePerPulse(kEncoderDistancePerPulse);
m_frontRightEncoder.SetDistancePerPulse(kEncoderDistancePerPulse);
m_rearRightEncoder.SetDistancePerPulse(kEncoderDistancePerPulse);
// We need to invert one side of the drivetrain so that positive voltages
// result in both sides moving forward. Depending on how your robot's
// gearbox is constructed, you might have to invert the left side instead.
m_frontRight.SetInverted(true);
m_rearRight.SetInverted(true);
}
void DriveSubsystem::Periodic() {

View File

@@ -14,10 +14,10 @@
class Robot : public frc::TimedRobot {
public:
void RobotInit() override {
// Invert the left side motors. You may need to change or remove this to
// Invert the right side motors. You may need to change or remove this to
// match your robot.
m_frontLeft.SetInverted(true);
m_rearLeft.SetInverted(true);
m_frontRight.SetInverted(true);
m_rearRight.SetInverted(true);
}
void TeleopPeriodic() override {

View File

@@ -21,7 +21,14 @@
*/
class Drivetrain {
public:
Drivetrain() { m_gyro.Reset(); }
Drivetrain() {
m_gyro.Reset();
// We need to invert one side of the drivetrain so that positive voltages
// result in both sides moving forward. Depending on how your robot's
// gearbox is constructed, you might have to invert the left side instead.
m_frontRightMotor.SetInverted(true);
m_backRightMotor.SetInverted(true);
}
frc::MecanumDriveWheelSpeeds GetCurrentState() const;
void SetSpeeds(const frc::MecanumDriveWheelSpeeds& wheelSpeeds);

View File

@@ -42,7 +42,7 @@ void DriveSubsystem::ArcadeDrive(double fwd, double rot) {
void DriveSubsystem::TankDriveVolts(units::volt_t left, units::volt_t right) {
m_leftMotors.SetVoltage(left);
m_rightMotors.SetVoltage(-right);
m_rightMotors.SetVoltage(right);
m_drive.Feed();
}

View File

@@ -43,7 +43,7 @@ void Drivetrain::SimulationPeriodic() {
// voltages make the right side move forward.
m_drivetrainSimulator.SetInputs(units::volt_t{m_leftLeader.Get()} *
frc::RobotController::GetInputVoltage(),
units::volt_t{-m_rightLeader.Get()} *
units::volt_t{m_rightLeader.Get()} *
frc::RobotController::GetInputVoltage());
m_drivetrainSimulator.Update(20_ms);

View File

@@ -28,8 +28,8 @@ RobotContainer::RobotContainer() {
// Set up default drive command
m_drive.SetDefaultCommand(frc2::RunCommand(
[this] {
m_drive.ArcadeDrive(m_driverController.GetLeftY(),
m_driverController.GetRightX());
m_drive.ArcadeDrive(-m_driverController.GetLeftY(),
-m_driverController.GetRightX());
},
{&m_drive}));
}

View File

@@ -42,7 +42,7 @@ void DriveSubsystem::SimulationPeriodic() {
// voltages make the right side move forward.
m_drivetrainSimulator.SetInputs(units::volt_t{m_leftMotors.Get()} *
frc::RobotController::GetInputVoltage(),
units::volt_t{-m_rightMotors.Get()} *
units::volt_t{m_rightMotors.Get()} *
frc::RobotController::GetInputVoltage());
m_drivetrainSimulator.Update(20_ms);
@@ -64,7 +64,7 @@ void DriveSubsystem::ArcadeDrive(double fwd, double rot) {
void DriveSubsystem::TankDriveVolts(units::volt_t left, units::volt_t right) {
m_leftMotors.SetVoltage(left);
m_rightMotors.SetVoltage(-right);
m_rightMotors.SetVoltage(right);
m_drive.Feed();
}

View File

@@ -27,8 +27,8 @@ class Robot : public frc::TimedRobot {
void TeleopPeriodic() override {
// Drive with tank style
m_robotDrive.TankDrive(m_driverController.GetLeftY(),
m_driverController.GetRightY());
m_robotDrive.TankDrive(-m_driverController.GetLeftY(),
-m_driverController.GetRightY());
}
};

View File

@@ -19,6 +19,14 @@ public class Robot extends TimedRobot {
private final DifferentialDrive m_robotDrive = new DifferentialDrive(m_leftMotor, m_rightMotor);
private final Joystick m_stick = new Joystick(0);
@Override
public void robotInit() {
// We need to invert one side of the drivetrain so that positive voltages
// result in both sides moving forward. Depending on how your robot's
// gearbox is constructed, you might have to invert the left side instead.
m_rightMotor.setInverted(true);
}
@Override
public void teleopPeriodic() {
// Drive with arcade drive.

View File

@@ -19,11 +19,19 @@ public class Robot extends TimedRobot {
private final DifferentialDrive m_robotDrive = new DifferentialDrive(m_leftMotor, m_rightMotor);
private final XboxController m_driverController = new XboxController(0);
@Override
public void robotInit() {
// We need to invert one side of the drivetrain so that positive voltages
// result in both sides moving forward. Depending on how your robot's
// gearbox is constructed, you might have to invert the left side instead.
m_rightMotor.setInverted(true);
}
@Override
public void teleopPeriodic() {
// Drive with split arcade drive.
// That means that the Y axis of the left stick moves forward
// and backward, and the X of the right stick turns left and right.
m_robotDrive.arcadeDrive(m_driverController.getLeftY(), m_driverController.getRightX());
m_robotDrive.arcadeDrive(-m_driverController.getLeftY(), -m_driverController.getRightX());
}
}

View File

@@ -42,7 +42,7 @@ public class RobotContainer {
new RunCommand(
() ->
m_robotDrive.arcadeDrive(
m_driverController.getLeftY(), m_driverController.getRightX()),
-m_driverController.getLeftY(), -m_driverController.getRightX()),
m_robotDrive));
}

View File

@@ -46,6 +46,11 @@ public class DriveSubsystem extends SubsystemBase {
// Sets the distance per pulse for the encoders
m_leftEncoder.setDistancePerPulse(DriveConstants.kEncoderDistancePerPulse);
m_rightEncoder.setDistancePerPulse(DriveConstants.kEncoderDistancePerPulse);
// We need to invert one side of the drivetrain so that positive voltages
// result in both sides moving forward. Depending on how your robot's
// gearbox is constructed, you might have to invert the left side instead.
m_rightMotors.setInverted(true);
}
/**

View File

@@ -42,7 +42,7 @@ public class RobotContainer {
new RunCommand(
() ->
m_robotDrive.arcadeDrive(
m_driverController.getLeftY(), m_driverController.getRightX()),
-m_driverController.getLeftY(), -m_driverController.getRightX()),
m_robotDrive));
}

View File

@@ -46,6 +46,11 @@ public class DriveSubsystem extends SubsystemBase {
// Sets the distance per pulse for the encoders
m_leftEncoder.setDistancePerPulse(DriveConstants.kEncoderDistancePerPulse);
m_rightEncoder.setDistancePerPulse(DriveConstants.kEncoderDistancePerPulse);
// We need to invert one side of the drivetrain so that positive voltages
// result in both sides moving forward. Depending on how your robot's
// gearbox is constructed, you might have to invert the left side instead.
m_rightMotors.setInverted(true);
}
/**

View File

@@ -44,7 +44,7 @@ public class RobotContainer {
new RunCommand(
() ->
m_robotDrive.arcadeDrive(
m_driverController.getLeftY(), m_driverController.getRightX()),
-m_driverController.getLeftY(), -m_driverController.getRightX()),
m_robotDrive));
}

View File

@@ -67,7 +67,7 @@ public class RobotContainer {
new RunCommand(
() ->
m_robotDrive.arcadeDrive(
m_driverController.getLeftY(), m_driverController.getRightX()),
-m_driverController.getLeftY(), -m_driverController.getRightX()),
m_robotDrive));
}

View File

@@ -17,8 +17,9 @@ import edu.wpi.first.wpilibj.motorcontrol.PWMSparkMax;
* directory.
*/
public class Robot extends TimedRobot {
private final DifferentialDrive m_robotDrive =
new DifferentialDrive(new PWMSparkMax(0), new PWMSparkMax(1));
private final PWMSparkMax m_leftDrive = new PWMSparkMax(0);
private final PWMSparkMax m_rightDrive = new PWMSparkMax(1);
private final DifferentialDrive m_robotDrive = new DifferentialDrive(m_leftDrive, m_rightDrive);
private final Joystick m_stick = new Joystick(0);
private final Timer m_timer = new Timer();
@@ -27,7 +28,12 @@ public class Robot extends TimedRobot {
* initialization code.
*/
@Override
public void robotInit() {}
public void robotInit() {
// We need to invert one side of the drivetrain so that positive voltages
// result in both sides moving forward. Depending on how your robot's
// gearbox is constructed, you might have to invert the left side instead.
m_rightDrive.setInverted(true);
}
/** This function is run once each time the robot enters autonomous mode. */
@Override

View File

@@ -28,14 +28,19 @@ public class Robot extends TimedRobot {
private static final int kGyroPort = 0;
private static final int kJoystickPort = 0;
private final DifferentialDrive m_myRobot =
new DifferentialDrive(new PWMSparkMax(kLeftMotorPort), new PWMSparkMax(kRightMotorPort));
private final PWMSparkMax m_leftDrive = new PWMSparkMax(kLeftMotorPort);
private final PWMSparkMax m_rightDrive = new PWMSparkMax(kRightMotorPort);
private final DifferentialDrive m_myRobot = new DifferentialDrive(m_leftDrive, m_rightDrive);
private final AnalogGyro m_gyro = new AnalogGyro(kGyroPort);
private final Joystick m_joystick = new Joystick(kJoystickPort);
@Override
public void robotInit() {
m_gyro.setSensitivity(kVoltsPerDegreePerSecond);
// We need to invert one side of the drivetrain so that positive voltages
// result in both sides moving forward. Depending on how your robot's
// gearbox is constructed, you might have to invert the left side instead.
m_rightDrive.setInverted(true);
}
/**

View File

@@ -37,10 +37,10 @@ public class Robot extends TimedRobot {
PWMSparkMax frontRight = new PWMSparkMax(kFrontRightChannel);
PWMSparkMax rearRight = new PWMSparkMax(kRearRightChannel);
// Invert the left side motors.
// Invert the right side motors.
// You may need to change or remove this to match your robot.
frontLeft.setInverted(true);
rearLeft.setInverted(true);
frontRight.setInverted(true);
rearRight.setInverted(true);
m_robotDrive = new MecanumDrive(frontLeft, rearLeft, frontRight, rearRight);

View File

@@ -56,6 +56,11 @@ public class Drivetrain {
/** Constructs a MecanumDrive and resets the gyro. */
public Drivetrain() {
m_gyro.reset();
// We need to invert one side of the drivetrain so that positive voltages
// result in both sides moving forward. Depending on how your robot's
// gearbox is constructed, you might have to invert the left side instead.
m_frontRightMotor.setInverted(true);
m_backRightMotor.setInverted(true);
}
/**

View File

@@ -67,6 +67,11 @@ public class DriveSubsystem extends SubsystemBase {
m_rearLeftEncoder.setDistancePerPulse(DriveConstants.kEncoderDistancePerPulse);
m_frontRightEncoder.setDistancePerPulse(DriveConstants.kEncoderDistancePerPulse);
m_rearRightEncoder.setDistancePerPulse(DriveConstants.kEncoderDistancePerPulse);
// We need to invert one side of the drivetrain so that positive voltages
// result in both sides moving forward. Depending on how your robot's
// gearbox is constructed, you might have to invert the left side instead.
m_frontRight.setInverted(true);
m_rearRight.setInverted(true);
}
@Override

View File

@@ -28,10 +28,10 @@ public class Robot extends TimedRobot {
PWMSparkMax frontRight = new PWMSparkMax(kFrontRightChannel);
PWMSparkMax rearRight = new PWMSparkMax(kRearRightChannel);
// Invert the left side motors.
// Invert the right side motors.
// You may need to change or remove this to match your robot.
frontLeft.setInverted(true);
rearLeft.setInverted(true);
frontRight.setInverted(true);
rearRight.setInverted(true);
m_robotDrive = new MecanumDrive(frontLeft, rearLeft, frontRight, rearRight);

View File

@@ -68,6 +68,11 @@ public class Drivetrain {
/** Constructs a MecanumDrive and resets the gyro. */
public Drivetrain() {
m_gyro.reset();
// We need to invert one side of the drivetrain so that positive voltages
// result in both sides moving forward. Depending on how your robot's
// gearbox is constructed, you might have to invert the left side instead.
m_frontRightMotor.setInverted(true);
m_backRightMotor.setInverted(true);
}
/**

View File

@@ -53,7 +53,7 @@ public class RobotContainer {
new RunCommand(
() ->
m_robotDrive.arcadeDrive(
m_driverController.getLeftY(), m_driverController.getRightX()),
-m_driverController.getLeftY(), -m_driverController.getRightX()),
m_robotDrive));
}

View File

@@ -120,7 +120,7 @@ public class DriveSubsystem extends SubsystemBase {
*/
public void tankDriveVolts(double leftVolts, double rightVolts) {
m_leftMotors.setVoltage(leftVolts);
m_rightMotors.setVoltage(-rightVolts);
m_rightMotors.setVoltage(rightVolts);
m_drive.feed();
}

View File

@@ -145,7 +145,7 @@ public class Drivetrain {
// voltages make the right side move forward.
m_drivetrainSimulator.setInputs(
m_leftLeader.get() * RobotController.getInputVoltage(),
-m_rightLeader.get() * RobotController.getInputVoltage());
m_rightLeader.get() * RobotController.getInputVoltage());
m_drivetrainSimulator.update(0.02);
m_leftEncoderSim.setDistance(m_drivetrainSimulator.getLeftPositionMeters());

View File

@@ -50,7 +50,7 @@ public class RobotContainer {
new RunCommand(
() ->
m_robotDrive.arcadeDrive(
-m_driverController.getLeftY(), m_driverController.getRightX()),
-m_driverController.getLeftY(), -m_driverController.getRightX()),
m_robotDrive));
}

View File

@@ -122,7 +122,7 @@ public class DriveSubsystem extends SubsystemBase {
// move forward.
m_drivetrainSimulator.setInputs(
m_leftMotors.get() * RobotController.getBatteryVoltage(),
-m_rightMotors.get() * RobotController.getBatteryVoltage());
m_rightMotors.get() * RobotController.getBatteryVoltage());
m_drivetrainSimulator.update(0.020);
m_leftEncoderSim.setDistance(m_drivetrainSimulator.getLeftPositionMeters());
@@ -194,7 +194,7 @@ public class DriveSubsystem extends SubsystemBase {
rightVolts *= batteryVoltage / 12.0;
}
m_leftMotors.setVoltage(leftVolts);
m_rightMotors.setVoltage(-rightVolts);
m_rightMotors.setVoltage(rightVolts);
m_drive.feed();
}

View File

@@ -33,6 +33,6 @@ public class Robot extends TimedRobot {
// That means that the Y axis of the left stick moves the left side
// of the robot forward and backward, and the Y axis of the right stick
// moves the right side of the robot forward and backward.
m_robotDrive.tankDrive(m_driverController.getLeftY(), m_driverController.getRightY());
m_robotDrive.tankDrive(-m_driverController.getLeftY(), -m_driverController.getRightY());
}
}