From 45d48d6b5a810055fdb7bab9ed8244aae002e917 Mon Sep 17 00:00:00 2001 From: Tyler Veness Date: Fri, 3 Nov 2017 13:22:56 -0700 Subject: [PATCH] Cleaned up C++ examples (#672) --- .../cpp/examples/ArcadeDrive/src/Robot.cpp | 9 ++- .../main/cpp/examples/Encoder/src/Robot.cpp | 41 ++++++------ .../GearsBot/src/Commands/Autonomous.cpp | 2 - .../GearsBot/src/Commands/CloseClaw.cpp | 8 +-- .../GearsBot/src/Commands/DriveStraight.cpp | 30 ++++----- .../GearsBot/src/Commands/DriveStraight.h | 33 +++++---- .../GearsBot/src/Commands/OpenClaw.cpp | 6 +- .../src/Commands/SetDistanceToBox.cpp | 28 ++++---- .../GearsBot/src/Commands/SetDistanceToBox.h | 33 +++++---- .../src/Commands/SetElevatorSetpoint.cpp | 10 +-- .../src/Commands/SetElevatorSetpoint.h | 2 +- .../src/Commands/SetWristSetpoint.cpp | 10 +-- .../GearsBot/src/Commands/SetWristSetpoint.h | 2 +- .../src/Commands/TankDriveWithJoystick.cpp | 7 +- .../src/main/cpp/examples/GearsBot/src/OI.cpp | 20 +++--- .../src/main/cpp/examples/GearsBot/src/OI.h | 20 +++--- .../main/cpp/examples/GearsBot/src/Robot.cpp | 24 +++---- .../main/cpp/examples/GearsBot/src/Robot.h | 16 ++--- .../examples/GearsBot/src/Subsystems/Claw.cpp | 8 +-- .../examples/GearsBot/src/Subsystems/Claw.h | 6 +- .../GearsBot/src/Subsystems/DriveTrain.cpp | 53 +++++++-------- .../GearsBot/src/Subsystems/DriveTrain.h | 33 ++++----- .../GearsBot/src/Subsystems/Elevator.cpp | 10 +-- .../GearsBot/src/Subsystems/Elevator.h | 8 +-- .../GearsBot/src/Subsystems/Wrist.cpp | 6 +- .../examples/GearsBot/src/Subsystems/Wrist.h | 8 +-- .../cpp/examples/GettingStarted/src/Robot.cpp | 39 ++++++----- .../src/main/cpp/examples/Gyro/src/Robot.cpp | 20 +++--- .../cpp/examples/GyroMecanum/src/Robot.cpp | 31 +++++---- .../cpp/examples/MecanumDrive/src/Robot.cpp | 29 ++++---- .../PacGoat/src/Commands/CheckForHotGoal.cpp | 2 +- .../PacGoat/src/Commands/CloseClaw.cpp | 4 +- .../PacGoat/src/Commands/DriveForward.cpp | 26 +++---- .../PacGoat/src/Commands/DriveForward.h | 6 +- .../src/Commands/DriveWithJoystick.cpp | 7 +- .../PacGoat/src/Commands/ExtendShooter.cpp | 6 +- .../PacGoat/src/Commands/OpenClaw.cpp | 6 +- .../src/Commands/SetCollectionSpeed.cpp | 6 +- .../PacGoat/src/Commands/SetCollectionSpeed.h | 2 +- .../PacGoat/src/Commands/SetPivotSetpoint.cpp | 10 +-- .../PacGoat/src/Commands/SetPivotSetpoint.h | 2 +- .../PacGoat/src/Commands/WaitForBall.cpp | 4 +- .../PacGoat/src/Commands/WaitForPressure.cpp | 4 +- .../src/main/cpp/examples/PacGoat/src/OI.cpp | 14 ++-- .../src/main/cpp/examples/PacGoat/src/OI.h | 14 ++-- .../main/cpp/examples/PacGoat/src/Robot.cpp | 48 ++++++------- .../src/main/cpp/examples/PacGoat/src/Robot.h | 23 +++---- .../PacGoat/src/Subsystems/Collector.cpp | 21 +++--- .../PacGoat/src/Subsystems/Collector.h | 10 +-- .../PacGoat/src/Subsystems/DriveTrain.cpp | 59 +++++++--------- .../PacGoat/src/Subsystems/DriveTrain.h | 42 +++++------- .../examples/PacGoat/src/Subsystems/Pivot.cpp | 23 ++++--- .../examples/PacGoat/src/Subsystems/Pivot.h | 10 +-- .../PacGoat/src/Subsystems/Pneumatics.cpp | 9 +-- .../PacGoat/src/Subsystems/Pneumatics.h | 4 +- .../PacGoat/src/Subsystems/Shooter.cpp | 45 +++++++------ .../examples/PacGoat/src/Subsystems/Shooter.h | 12 ++-- .../PacGoat/src/Triggers/DoubleButton.cpp | 10 +-- .../PacGoat/src/Triggers/DoubleButton.h | 6 +- .../examples/PotentiometerPID/src/Robot.cpp | 28 ++++---- .../cpp/examples/Ultrasonic/src/Robot.cpp | 15 +++-- .../cpp/examples/UltrasonicPID/src/Robot.cpp | 39 ++++++----- .../commandbased/Commands/ExampleCommand.cpp | 2 +- .../commandbased/Commands/MyAutoCommand.cpp | 31 +++++++++ .../commandbased/Commands/MyAutoCommand.h | 20 ++++++ .../main/cpp/templates/commandbased/Robot.cpp | 42 ++++++------ .../cpp/templates/commandbased/RobotMap.h | 8 +-- .../main/cpp/templates/iterative/Robot.cpp | 31 +++++---- .../src/main/cpp/templates/sample/Robot.cpp | 67 +++++++++++-------- 69 files changed, 672 insertions(+), 598 deletions(-) create mode 100644 wpilibcExamples/src/main/cpp/templates/commandbased/Commands/MyAutoCommand.cpp create mode 100644 wpilibcExamples/src/main/cpp/templates/commandbased/Commands/MyAutoCommand.h diff --git a/wpilibcExamples/src/main/cpp/examples/ArcadeDrive/src/Robot.cpp b/wpilibcExamples/src/main/cpp/examples/ArcadeDrive/src/Robot.cpp index 8004026312..a2838369c0 100644 --- a/wpilibcExamples/src/main/cpp/examples/ArcadeDrive/src/Robot.cpp +++ b/wpilibcExamples/src/main/cpp/examples/ArcadeDrive/src/Robot.cpp @@ -12,12 +12,15 @@ * Runs the motors with arcade steering. */ class Robot : public frc::IterativeRobot { - frc::RobotDrive myRobot{0, 1}; - frc::Joystick stick{0}; + frc::Spark m_left{0}; + frc::Spark m_right{1}; + frc::RobotDrive m_robotDrive{m_left, m_right}; + frc::Joystick m_stick{0}; public: void TeleopPeriodic() { - myRobot.ArcadeDrive(stick); // drive with arcade style + // drive with arcade style + m_robotDrive.ArcadeDrive(m_stick.GetY(), m_stick.GetX()); } }; diff --git a/wpilibcExamples/src/main/cpp/examples/Encoder/src/Robot.cpp b/wpilibcExamples/src/main/cpp/examples/Encoder/src/Robot.cpp index f0eec6a6d5..9dfd337570 100644 --- a/wpilibcExamples/src/main/cpp/examples/Encoder/src/Robot.cpp +++ b/wpilibcExamples/src/main/cpp/examples/Encoder/src/Robot.cpp @@ -26,26 +26,6 @@ * autonomous mode. */ class Robot : public frc::IterativeRobot { - /** - * The Encoder object is constructed with 4 parameters, the last two - * being - * optional. - * The first two parameters (1, 2 in this case) refer to the ports on - * the - * roboRIO which the encoder uses. Because a quadrature encoder has - * two signal wires, the signal from two DIO ports on the roboRIO are - * used. - * The third (optional) parameter is a boolean which defaults to false. - * If you set this parameter to true, the direction of the encoder - * will - * be reversed, in case it makes more sense mechanically. - * The final (optional) parameter specifies encoding rate (k1X, k2X, or - * k4X) - * and defaults to k4X. Faster (k4X) encoding gives greater positional - * precision but more noise in the rate. - */ - frc::Encoder m_encoder{1, 2, false, Encoder::k4X}; - public: Robot() { /* Defines the number of samples to average when determining the @@ -88,6 +68,27 @@ public: frc::SmartDashboard::PutNumber( "Encoder Rate", m_encoder.GetRate()); } + +private: + /** + * The Encoder object is constructed with 4 parameters, the last two + * being + * optional. + * The first two parameters (1, 2 in this case) refer to the ports on + * the + * roboRIO which the encoder uses. Because a quadrature encoder has + * two signal wires, the signal from two DIO ports on the roboRIO are + * used. + * The third (optional) parameter is a boolean which defaults to false. + * If you set this parameter to true, the direction of the encoder + * will + * be reversed, in case it makes more sense mechanically. + * The final (optional) parameter specifies encoding rate (k1X, k2X, or + * k4X) + * and defaults to k4X. Faster (k4X) encoding gives greater positional + * precision but more noise in the rate. + */ + frc::Encoder m_encoder{1, 2, false, Encoder::k4X}; }; START_ROBOT_CLASS(Robot) diff --git a/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Commands/Autonomous.cpp b/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Commands/Autonomous.cpp index 347f549df0..6209839646 100644 --- a/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Commands/Autonomous.cpp +++ b/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Commands/Autonomous.cpp @@ -7,8 +7,6 @@ #include "Autonomous.h" -#include - #include "CloseClaw.h" #include "DriveStraight.h" #include "Pickup.h" diff --git a/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Commands/CloseClaw.cpp b/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Commands/CloseClaw.cpp index 2ee4d381d2..ddd641fdd1 100644 --- a/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Commands/CloseClaw.cpp +++ b/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Commands/CloseClaw.cpp @@ -11,17 +11,17 @@ CloseClaw::CloseClaw() : frc::Command("CloseClaw") { - Requires(Robot::claw.get()); + Requires(&Robot::claw); } // Called just before this Command runs the first time void CloseClaw::Initialize() { - Robot::claw->Close(); + Robot::claw.Close(); } // Make this return true when this Command no longer needs to run execute() bool CloseClaw::IsFinished() { - return Robot::claw->IsGripping(); + return Robot::claw.IsGripping(); } // Called once after isFinished returns true @@ -30,6 +30,6 @@ void CloseClaw::End() { // fall out // + there is no need to worry about stalling the motor or crushing the can. #ifndef SIMULATION - Robot::claw->Stop(); + Robot::claw.Stop(); #endif } diff --git a/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Commands/DriveStraight.cpp b/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Commands/DriveStraight.cpp index 92a49b4f12..467a7db91f 100644 --- a/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Commands/DriveStraight.cpp +++ b/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Commands/DriveStraight.cpp @@ -7,42 +7,38 @@ #include "DriveStraight.h" -#include - #include "../Robot.h" DriveStraight::DriveStraight(double distance) { - Requires(Robot::drivetrain.get()); - pid = new frc::PIDController(4, 0, 0, new DriveStraightPIDSource(), - new DriveStraightPIDOutput()); - pid->SetAbsoluteTolerance(0.01); - pid->SetSetpoint(distance); + Requires(&Robot::drivetrain); + m_pid.SetAbsoluteTolerance(0.01); + m_pid.SetSetpoint(distance); } // Called just before this Command runs the first time void DriveStraight::Initialize() { // Get everything in a safe starting state. - Robot::drivetrain->Reset(); - pid->Reset(); - pid->Enable(); + Robot::drivetrain.Reset(); + m_pid.Reset(); + m_pid.Enable(); } // Make this return true when this Command no longer needs to run execute() bool DriveStraight::IsFinished() { - return pid->OnTarget(); + return m_pid.OnTarget(); } // Called once after isFinished returns true void DriveStraight::End() { // Stop PID and the wheels - pid->Disable(); - Robot::drivetrain->Drive(0, 0); + m_pid.Disable(); + Robot::drivetrain.Drive(0, 0); } -double DriveStraightPIDSource::PIDGet() { - return Robot::drivetrain->GetDistance(); +double DriveStraight::DriveStraightPIDSource::PIDGet() { + return Robot::drivetrain.GetDistance(); } -void DriveStraightPIDOutput::PIDWrite(double d) { - Robot::drivetrain->Drive(d, d); +void DriveStraight::DriveStraightPIDOutput::PIDWrite(double d) { + Robot::drivetrain.Drive(d, d); } diff --git a/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Commands/DriveStraight.h b/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Commands/DriveStraight.h index 3cb3b174e0..f602aa3c39 100644 --- a/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Commands/DriveStraight.h +++ b/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Commands/DriveStraight.h @@ -8,13 +8,10 @@ #pragma once #include +#include #include #include -namespace frc { -class PIDController; -} // namespace frc - /** * Drive the given distance straight (negative values go backwards). * Uses a local PID controller to run a simple PID loop that is only @@ -28,18 +25,20 @@ public: bool IsFinished() override; void End() override; + class DriveStraightPIDSource : public frc::PIDSource { + public: + virtual ~DriveStraightPIDSource() = default; + double PIDGet() override; + }; + + class DriveStraightPIDOutput : public frc::PIDOutput { + public: + virtual ~DriveStraightPIDOutput() = default; + void PIDWrite(double d) override; + }; + private: - frc::PIDController* pid; -}; - -class DriveStraightPIDSource : public PIDSource { -public: - virtual ~DriveStraightPIDSource() = default; - double PIDGet() override; -}; - -class DriveStraightPIDOutput : public PIDOutput { -public: - virtual ~DriveStraightPIDOutput() = default; - void PIDWrite(double d) override; + DriveStraightPIDSource m_source; + DriveStraightPIDOutput m_output; + frc::PIDController m_pid{4, 0, 0, &m_source, &m_output}; }; diff --git a/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Commands/OpenClaw.cpp b/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Commands/OpenClaw.cpp index c5ad540010..542196c655 100644 --- a/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Commands/OpenClaw.cpp +++ b/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Commands/OpenClaw.cpp @@ -11,13 +11,13 @@ OpenClaw::OpenClaw() : frc::Command("OpenClaw") { - Requires(Robot::claw.get()); + Requires(&Robot::claw); SetTimeout(1); } // Called just before this Command runs the first time void OpenClaw::Initialize() { - Robot::claw->Open(); + Robot::claw.Open(); } // Make this return true when this Command no longer needs to run execute() @@ -27,5 +27,5 @@ bool OpenClaw::IsFinished() { // Called once after isFinished returns true void OpenClaw::End() { - Robot::claw->Stop(); + Robot::claw.Stop(); } diff --git a/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Commands/SetDistanceToBox.cpp b/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Commands/SetDistanceToBox.cpp index a66a70b918..9b7e264f40 100644 --- a/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Commands/SetDistanceToBox.cpp +++ b/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Commands/SetDistanceToBox.cpp @@ -12,37 +12,35 @@ #include "../Robot.h" SetDistanceToBox::SetDistanceToBox(double distance) { - Requires(Robot::drivetrain.get()); - pid = new frc::PIDController(-2, 0, 0, new SetDistanceToBoxPIDSource(), - new SetDistanceToBoxPIDOutput()); - pid->SetAbsoluteTolerance(0.01); - pid->SetSetpoint(distance); + Requires(&Robot::drivetrain); + m_pid.SetAbsoluteTolerance(0.01); + m_pid.SetSetpoint(distance); } // Called just before this Command runs the first time void SetDistanceToBox::Initialize() { // Get everything in a safe starting state. - Robot::drivetrain->Reset(); - pid->Reset(); - pid->Enable(); + Robot::drivetrain.Reset(); + m_pid.Reset(); + m_pid.Enable(); } // Make this return true when this Command no longer needs to run execute() bool SetDistanceToBox::IsFinished() { - return pid->OnTarget(); + return m_pid.OnTarget(); } // Called once after isFinished returns true void SetDistanceToBox::End() { // Stop PID and the wheels - pid->Disable(); - Robot::drivetrain->Drive(0, 0); + m_pid.Disable(); + Robot::drivetrain.Drive(0, 0); } -double SetDistanceToBoxPIDSource::PIDGet() { - return Robot::drivetrain->GetDistanceToObstacle(); +double SetDistanceToBox::SetDistanceToBoxPIDSource::PIDGet() { + return Robot::drivetrain.GetDistanceToObstacle(); } -void SetDistanceToBoxPIDOutput::PIDWrite(double d) { - Robot::drivetrain->Drive(d, d); +void SetDistanceToBox::SetDistanceToBoxPIDOutput::PIDWrite(double d) { + Robot::drivetrain.Drive(d, d); } diff --git a/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Commands/SetDistanceToBox.h b/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Commands/SetDistanceToBox.h index 1751457f40..a032d760d2 100644 --- a/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Commands/SetDistanceToBox.h +++ b/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Commands/SetDistanceToBox.h @@ -8,13 +8,10 @@ #pragma once #include +#include #include #include -namespace frc { -class PIDController; -} // namespace frc - /** * Drive until the robot is the given distance away from the box. Uses a local * PID controller to run a simple PID loop that is only enabled while this @@ -28,18 +25,20 @@ public: bool IsFinished() override; void End() override; + class SetDistanceToBoxPIDSource : public frc::PIDSource { + public: + virtual ~SetDistanceToBoxPIDSource() = default; + double PIDGet() override; + }; + + class SetDistanceToBoxPIDOutput : public frc::PIDOutput { + public: + virtual ~SetDistanceToBoxPIDOutput() = default; + void PIDWrite(double d) override; + }; + private: - frc::PIDController* pid; -}; - -class SetDistanceToBoxPIDSource : public frc::PIDSource { -public: - virtual ~SetDistanceToBoxPIDSource() = default; - double PIDGet() override; -}; - -class SetDistanceToBoxPIDOutput : public frc::PIDOutput { -public: - virtual ~SetDistanceToBoxPIDOutput() = default; - void PIDWrite(double d) override; + SetDistanceToBoxPIDSource m_source; + SetDistanceToBoxPIDOutput m_output; + frc::PIDController m_pid{-2, 0, 0, &m_source, &m_output}; }; diff --git a/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Commands/SetElevatorSetpoint.cpp b/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Commands/SetElevatorSetpoint.cpp index 555989c39f..d056a39d8b 100644 --- a/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Commands/SetElevatorSetpoint.cpp +++ b/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Commands/SetElevatorSetpoint.cpp @@ -13,17 +13,17 @@ SetElevatorSetpoint::SetElevatorSetpoint(double setpoint) : frc::Command("SetElevatorSetpoint") { - this->setpoint = setpoint; - Requires(Robot::elevator.get()); + m_setpoint = setpoint; + Requires(&Robot::elevator); } // Called just before this Command runs the first time void SetElevatorSetpoint::Initialize() { - Robot::elevator->SetSetpoint(setpoint); - Robot::elevator->Enable(); + Robot::elevator.SetSetpoint(m_setpoint); + Robot::elevator.Enable(); } // Make this return true when this Command no longer needs to run execute() bool SetElevatorSetpoint::IsFinished() { - return Robot::elevator->OnTarget(); + return Robot::elevator.OnTarget(); } diff --git a/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Commands/SetElevatorSetpoint.h b/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Commands/SetElevatorSetpoint.h index 283a72aef7..0c0cbb1a41 100644 --- a/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Commands/SetElevatorSetpoint.h +++ b/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Commands/SetElevatorSetpoint.h @@ -23,5 +23,5 @@ public: bool IsFinished() override; private: - double setpoint; + double m_setpoint; }; diff --git a/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Commands/SetWristSetpoint.cpp b/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Commands/SetWristSetpoint.cpp index b03ef0f4ff..e1b45a92c8 100644 --- a/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Commands/SetWristSetpoint.cpp +++ b/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Commands/SetWristSetpoint.cpp @@ -11,17 +11,17 @@ SetWristSetpoint::SetWristSetpoint(double setpoint) : frc::Command("SetWristSetpoint") { - this->setpoint = setpoint; - Requires(Robot::wrist.get()); + m_setpoint = setpoint; + Requires(&Robot::wrist); } // Called just before this Command runs the first time void SetWristSetpoint::Initialize() { - Robot::wrist->SetSetpoint(setpoint); - Robot::wrist->Enable(); + Robot::wrist.SetSetpoint(m_setpoint); + Robot::wrist.Enable(); } // Make this return true when this Command no longer needs to run execute() bool SetWristSetpoint::IsFinished() { - return Robot::wrist->OnTarget(); + return Robot::wrist.OnTarget(); } diff --git a/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Commands/SetWristSetpoint.h b/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Commands/SetWristSetpoint.h index 94a0ab86d9..4f0bdd842d 100644 --- a/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Commands/SetWristSetpoint.h +++ b/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Commands/SetWristSetpoint.h @@ -21,5 +21,5 @@ public: bool IsFinished() override; private: - double setpoint; + double m_setpoint; }; diff --git a/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Commands/TankDriveWithJoystick.cpp b/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Commands/TankDriveWithJoystick.cpp index 2c954df3d1..86277fd8ae 100644 --- a/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Commands/TankDriveWithJoystick.cpp +++ b/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Commands/TankDriveWithJoystick.cpp @@ -11,12 +11,13 @@ TankDriveWithJoystick::TankDriveWithJoystick() : frc::Command("TankDriveWithJoystick") { - Requires(Robot::drivetrain.get()); + Requires(&Robot::drivetrain); } // Called repeatedly when this Command is scheduled to run void TankDriveWithJoystick::Execute() { - Robot::drivetrain->Drive(Robot::oi->GetJoystick()); + auto& joystick = Robot::oi.GetJoystick(); + Robot::drivetrain.Drive(-joystick.GetY(), -joystick.GetRawAxis(4)); } // Make this return true when this Command no longer needs to run execute() @@ -26,5 +27,5 @@ bool TankDriveWithJoystick::IsFinished() { // Called once after isFinished returns true void TankDriveWithJoystick::End() { - Robot::drivetrain->Drive(0, 0); + Robot::drivetrain.Drive(0, 0); } diff --git a/wpilibcExamples/src/main/cpp/examples/GearsBot/src/OI.cpp b/wpilibcExamples/src/main/cpp/examples/GearsBot/src/OI.cpp index 0ccf662143..d38bc9de3c 100644 --- a/wpilibcExamples/src/main/cpp/examples/GearsBot/src/OI.cpp +++ b/wpilibcExamples/src/main/cpp/examples/GearsBot/src/OI.cpp @@ -22,17 +22,17 @@ OI::OI() { frc::SmartDashboard::PutData("Close Claw", new CloseClaw()); // Connect the buttons to commands - d_up.WhenPressed(new SetElevatorSetpoint(0.2)); - d_down.WhenPressed(new SetElevatorSetpoint(-0.2)); - d_right.WhenPressed(new CloseClaw()); - d_left.WhenPressed(new OpenClaw()); + m_dUp.WhenPressed(new SetElevatorSetpoint(0.2)); + m_dDown.WhenPressed(new SetElevatorSetpoint(-0.2)); + m_dRight.WhenPressed(new CloseClaw()); + m_dLeft.WhenPressed(new OpenClaw()); - r1.WhenPressed(new PrepareToPickup()); - r2.WhenPressed(new Pickup()); - l1.WhenPressed(new Place()); - l2.WhenPressed(new Autonomous()); + m_r1.WhenPressed(new PrepareToPickup()); + m_r2.WhenPressed(new Pickup()); + m_l1.WhenPressed(new Place()); + m_l2.WhenPressed(new Autonomous()); } -frc::Joystick* OI::GetJoystick() { - return &joy; +frc::Joystick& OI::GetJoystick() { + return m_joy; } diff --git a/wpilibcExamples/src/main/cpp/examples/GearsBot/src/OI.h b/wpilibcExamples/src/main/cpp/examples/GearsBot/src/OI.h index 6827147681..60cb95881a 100644 --- a/wpilibcExamples/src/main/cpp/examples/GearsBot/src/OI.h +++ b/wpilibcExamples/src/main/cpp/examples/GearsBot/src/OI.h @@ -13,18 +13,18 @@ class OI { public: OI(); - frc::Joystick* GetJoystick(); + frc::Joystick& GetJoystick(); private: - frc::Joystick joy{0}; + frc::Joystick m_joy{0}; // Create some buttons - frc::JoystickButton d_up{&joy, 5}; - frc::JoystickButton d_right{&joy, 6}; - frc::JoystickButton d_down{&joy, 7}; - frc::JoystickButton d_left{&joy, 8}; - frc::JoystickButton l2{&joy, 9}; - frc::JoystickButton r2{&joy, 10}; - frc::JoystickButton l1{&joy, 11}; - frc::JoystickButton r1{&joy, 12}; + frc::JoystickButton m_dUp{&m_joy, 5}; + frc::JoystickButton m_dRight{&m_joy, 6}; + frc::JoystickButton m_dDown{&m_joy, 7}; + frc::JoystickButton m_dLeft{&m_joy, 8}; + frc::JoystickButton m_l2{&m_joy, 9}; + frc::JoystickButton m_r2{&m_joy, 10}; + frc::JoystickButton m_l1{&m_joy, 11}; + frc::JoystickButton m_r1{&m_joy, 12}; }; diff --git a/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Robot.cpp b/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Robot.cpp index d8523bbd29..1db76e154b 100644 --- a/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Robot.cpp +++ b/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Robot.cpp @@ -9,22 +9,22 @@ #include -std::shared_ptr Robot::drivetrain = std::make_shared(); -std::shared_ptr Robot::elevator = std::make_shared(); -std::shared_ptr Robot::wrist = std::make_shared(); -std::shared_ptr Robot::claw = std::make_shared(); -std::unique_ptr Robot::oi = std::make_unique(); +DriveTrain Robot::drivetrain; +Elevator Robot::elevator; +Wrist Robot::wrist; +Claw Robot::claw; +OI Robot::oi; void Robot::RobotInit() { // Show what command your subsystem is running on the SmartDashboard - frc::SmartDashboard::PutData(drivetrain.get()); - frc::SmartDashboard::PutData(elevator.get()); - frc::SmartDashboard::PutData(wrist.get()); - frc::SmartDashboard::PutData(claw.get()); + frc::SmartDashboard::PutData(&drivetrain); + frc::SmartDashboard::PutData(&elevator); + frc::SmartDashboard::PutData(&wrist); + frc::SmartDashboard::PutData(&claw); } void Robot::AutonomousInit() { - autonomousCommand.Start(); + m_autonomousCommand.Start(); std::cout << "Starting Auto" << std::endl; } @@ -37,7 +37,7 @@ void Robot::TeleopInit() { // teleop starts running. If you want the autonomous to // continue until interrupted by another command, remove // this line or comment it out. - autonomousCommand.Cancel(); + m_autonomousCommand.Cancel(); std::cout << "Starting Teleop" << std::endl; } @@ -46,7 +46,7 @@ void Robot::TeleopPeriodic() { } void Robot::TestPeriodic() { - lw->Run(); + m_lw.Run(); } START_ROBOT_CLASS(Robot) diff --git a/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Robot.h b/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Robot.h index 267c2023fb..c201c8ab3c 100644 --- a/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Robot.h +++ b/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Robot.h @@ -7,8 +7,6 @@ #pragma once -#include - #include #include #include @@ -22,15 +20,15 @@ class Robot : public frc::IterativeRobot { public: - static std::shared_ptr drivetrain; - static std::shared_ptr elevator; - static std::shared_ptr wrist; - static std::shared_ptr claw; - static std::unique_ptr oi; + static DriveTrain drivetrain; + static Elevator elevator; + static Wrist wrist; + static Claw claw; + static OI oi; private: - Autonomous autonomousCommand; - frc::LiveWindow* lw = frc::LiveWindow::GetInstance(); + Autonomous m_autonomousCommand; + frc::LiveWindow& m_lw = *frc::LiveWindow::GetInstance(); void RobotInit() override; void AutonomousInit() override; diff --git a/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Subsystems/Claw.cpp b/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Subsystems/Claw.cpp index ba322711c4..99cb350256 100644 --- a/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Subsystems/Claw.cpp +++ b/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Subsystems/Claw.cpp @@ -18,19 +18,19 @@ Claw::Claw() void Claw::InitDefaultCommand() {} void Claw::Open() { - motor.Set(-1); + m_motor.Set(-1); } void Claw::Close() { - motor.Set(1); + m_motor.Set(1); } void Claw::Stop() { - motor.Set(0); + m_motor.Set(0); } bool Claw::IsGripping() { - return contact.Get(); + return m_contact.Get(); } void Claw::Log() {} diff --git a/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Subsystems/Claw.h b/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Subsystems/Claw.h index 5c216e56e1..ee178167fd 100644 --- a/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Subsystems/Claw.h +++ b/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Subsystems/Claw.h @@ -9,7 +9,7 @@ #include #include -#include +#include /** * The claw subsystem is a simple system with a motor for opening and closing. @@ -46,6 +46,6 @@ public: void Log(); private: - frc::Victor motor{7}; - frc::DigitalInput contact{5}; + frc::Spark m_motor{7}; + frc::DigitalInput m_contact{5}; }; diff --git a/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Subsystems/DriveTrain.cpp b/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Subsystems/DriveTrain.cpp index fef5bb5349..abcbeae94c 100644 --- a/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Subsystems/DriveTrain.cpp +++ b/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Subsystems/DriveTrain.cpp @@ -20,36 +20,36 @@ DriveTrain::DriveTrain() // simulate 360 tick encoders. This if statement allows for the // real robot to handle this difference in devices. #ifndef SIMULATION - leftEncoder.SetDistancePerPulse(0.042); - rightEncoder.SetDistancePerPulse(0.042); + m_leftEncoder.SetDistancePerPulse(0.042); + m_rightEncoder.SetDistancePerPulse(0.042); #else // Circumference in ft = 4in/12(in/ft)*PI - leftEncoder.SetDistancePerPulse( + m_leftEncoder.SetDistancePerPulse( static_cast(4.0 / 12.0 * M_PI) / 360.0); - rightEncoder.SetDistancePerPulse( + m_rightEncoder.SetDistancePerPulse( static_cast(4.0 / 12.0 * M_PI) / 360.0); #endif // Let's show everything on the LiveWindow // frc::LiveWindow::GetInstance()->AddActuator("Drive Train", - // "Front_Left Motor", &frontLeft); + // "Front_Left Motor", &m_frontLeft); // frc::LiveWindow::GetInstance()->AddActuator("Drive Train", - // "Rear Left Motor", &rearLeft); + // "Rear Left Motor", &m_rearLeft); // frc::LiveWindow::GetInstance()->AddActuator("Drive Train", - // "Front Right Motor", &frontRight); + // "Front Right Motor", &m_frontRight); // frc::LiveWindow::GetInstance()->AddActuator("Drive Train", - // "Rear Right Motor", &rearRight); + // "Rear Right Motor", &m_rearRight); // frc::LiveWindow::GetInstance()->AddSensor("Drive Train", "Left // Encoder", - // &leftEncoder); + // &m_leftEncoder); // frc::LiveWindow::GetInstance()->AddSensor("Drive Train", "Right // Encoder", - // &rightEncoder); + // &m_rightEncoder); // frc::LiveWindow::GetInstance()->AddSensor("Drive Train", // "Rangefinder", - // &rangefinder); + // &m_rangefinder); // frc::LiveWindow::GetInstance()->AddSensor("Drive Train", "Gyro", - // &gyro); + // &m_gyro); } /** @@ -65,37 +65,34 @@ void DriveTrain::InitDefaultCommand() { */ void DriveTrain::Log() { frc::SmartDashboard::PutNumber( - "Left Distance", leftEncoder.GetDistance()); + "Left Distance", m_leftEncoder.GetDistance()); frc::SmartDashboard::PutNumber( - "Right Distance", rightEncoder.GetDistance()); - frc::SmartDashboard::PutNumber("Left Speed", leftEncoder.GetRate()); - frc::SmartDashboard::PutNumber("Right Speed", rightEncoder.GetRate()); - frc::SmartDashboard::PutNumber("Gyro", gyro.GetAngle()); + "Right Distance", m_rightEncoder.GetDistance()); + frc::SmartDashboard::PutNumber("Left Speed", m_leftEncoder.GetRate()); + frc::SmartDashboard::PutNumber("Right Speed", m_rightEncoder.GetRate()); + frc::SmartDashboard::PutNumber("Gyro", m_gyro.GetAngle()); } void DriveTrain::Drive(double left, double right) { - drive.TankDrive(left, right); -} - -void DriveTrain::Drive(frc::Joystick* joy) { - Drive(-joy->GetY(), -joy->GetRawAxis(4)); + m_robotDrive.TankDrive(left, right); } double DriveTrain::GetHeading() { - return gyro.GetAngle(); + return m_gyro.GetAngle(); } void DriveTrain::Reset() { - gyro.Reset(); - leftEncoder.Reset(); - rightEncoder.Reset(); + m_gyro.Reset(); + m_leftEncoder.Reset(); + m_rightEncoder.Reset(); } double DriveTrain::GetDistance() { - return (leftEncoder.GetDistance() + rightEncoder.GetDistance()) / 2; + return (m_leftEncoder.GetDistance() + m_rightEncoder.GetDistance()) + / 2.0; } double DriveTrain::GetDistanceToObstacle() { // Really meters in simulation since it's a rangefinder... - return rangefinder.GetAverageVoltage(); + return m_rangefinder.GetAverageVoltage(); } diff --git a/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Subsystems/DriveTrain.h b/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Subsystems/DriveTrain.h index 586136d613..752367ffdc 100644 --- a/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Subsystems/DriveTrain.h +++ b/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Subsystems/DriveTrain.h @@ -10,9 +10,10 @@ #include #include #include +#include #include -#include -#include +#include +#include namespace frc { class Joystick; @@ -45,11 +46,6 @@ public: */ void Drive(double left, double right); - /** - * @param joy The ps3 style joystick to use to drive tank style. - */ - void Drive(frc::Joystick* joy); - /** * @return The robots heading in degrees. */ @@ -71,13 +67,18 @@ public: double GetDistanceToObstacle(); private: - frc::Talon frontLeft{1}; - frc::Talon rearLeft{2}; - frc::Talon frontRight{3}; - frc::Talon rearRight{4}; - frc::RobotDrive drive{frontLeft, rearLeft, frontRight, rearRight}; - frc::Encoder leftEncoder{1, 2}; - frc::Encoder rightEncoder{3, 4}; - frc::AnalogInput rangefinder{6}; - frc::AnalogGyro gyro{1}; + frc::Spark m_frontLeft{1}; + frc::Spark m_rearLeft{2}; + frc::SpeedControllerGroup m_left{m_frontLeft, m_rearLeft}; + + frc::Spark m_frontRight{3}; + frc::Spark m_rearRight{4}; + frc::SpeedControllerGroup m_right{m_frontRight, m_rearRight}; + + frc::DifferentialDrive m_robotDrive{m_left, m_right}; + + frc::Encoder m_leftEncoder{1, 2}; + frc::Encoder m_rightEncoder{3, 4}; + frc::AnalogInput m_rangefinder{6}; + frc::AnalogGyro m_gyro{1}; }; diff --git a/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Subsystems/Elevator.cpp b/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Subsystems/Elevator.cpp index 4ccc52e19e..d07d0828ff 100644 --- a/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Subsystems/Elevator.cpp +++ b/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Subsystems/Elevator.cpp @@ -19,8 +19,8 @@ Elevator::Elevator() // Let's show everything on the LiveWindow // frc::LiveWindow::GetInstance()->AddActuator("Elevator", "Motor", - // &motor); - // frc::LiveWindow::GetInstance()->AddSensor("Elevator", "Pot", &pot); + // &m_motor); + // frc::LiveWindow::GetInstance()->AddSensor("Elevator", "Pot", &m_pot); // frc::LiveWindow::GetInstance()->AddActuator("Elevator", "PID", // GetPIDController()); } @@ -28,13 +28,13 @@ Elevator::Elevator() void Elevator::InitDefaultCommand() {} void Elevator::Log() { - // frc::SmartDashboard::PutData("Wrist Pot", &pot); + // frc::SmartDashboard::PutData("Wrist Pot", &m_pot); } double Elevator::ReturnPIDInput() { - return pot.Get(); + return m_pot.Get(); } void Elevator::UsePIDOutput(double d) { - motor.Set(d); + m_motor.Set(d); } diff --git a/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Subsystems/Elevator.h b/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Subsystems/Elevator.h index 8c871d1ec6..ee0a4be164 100644 --- a/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Subsystems/Elevator.h +++ b/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Subsystems/Elevator.h @@ -9,7 +9,7 @@ #include #include -#include +#include /** * The elevator subsystem uses PID to go to a given height. Unfortunately, in @@ -42,14 +42,14 @@ public: void UsePIDOutput(double d); private: - frc::Victor motor{5}; + frc::Spark m_motor{5}; // Conversion value of potentiometer varies between the real world and // simulation #ifndef SIMULATION - frc::AnalogPotentiometer pot{2, -2.0 / 5}; + frc::AnalogPotentiometer m_pot{2, -2.0 / 5}; #else - frc::AnalogPotentiometer pot{2}; // Defaults to meters + frc::AnalogPotentiometer m_pot{2}; // Defaults to meters #endif static constexpr double kP_real = 4; diff --git a/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Subsystems/Wrist.cpp b/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Subsystems/Wrist.cpp index 4556457824..447a4313cf 100644 --- a/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Subsystems/Wrist.cpp +++ b/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Subsystems/Wrist.cpp @@ -28,13 +28,13 @@ Wrist::Wrist() void Wrist::InitDefaultCommand() {} void Wrist::Log() { - // frc::SmartDashboard::PutData("Wrist Angle", &pot); + // frc::SmartDashboard::PutData("Wrist Angle", &m_pot); } double Wrist::ReturnPIDInput() { - return pot.Get(); + return m_pot.Get(); } void Wrist::UsePIDOutput(double d) { - motor.Set(d); + m_motor.Set(d); } diff --git a/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Subsystems/Wrist.h b/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Subsystems/Wrist.h index efcd394ffa..33b1f4e3a3 100644 --- a/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Subsystems/Wrist.h +++ b/wpilibcExamples/src/main/cpp/examples/GearsBot/src/Subsystems/Wrist.h @@ -9,7 +9,7 @@ #include #include -#include +#include /** * The wrist subsystem is like the elevator, but with a rotational joint instead @@ -40,14 +40,14 @@ public: void UsePIDOutput(double d) override; private: - frc::Victor motor{6}; + frc::Spark m_motor{6}; // Conversion value of potentiometer varies between the real world and // simulation #ifndef SIMULATION - frc::AnalogPotentiometer pot{3, -270.0 / 5}; + frc::AnalogPotentiometer m_pot{3, -270.0 / 5}; #else - frc::AnalogPotentiometer pot{3}; // Defaults to degrees + frc::AnalogPotentiometer m_pot{3}; // Defaults to degrees #endif static constexpr double kP_real = 1; diff --git a/wpilibcExamples/src/main/cpp/examples/GettingStarted/src/Robot.cpp b/wpilibcExamples/src/main/cpp/examples/GettingStarted/src/Robot.cpp index 6516dfa66e..c1894a4fc7 100644 --- a/wpilibcExamples/src/main/cpp/examples/GettingStarted/src/Robot.cpp +++ b/wpilibcExamples/src/main/cpp/examples/GettingStarted/src/Robot.cpp @@ -5,36 +5,33 @@ /* the project. */ /*----------------------------------------------------------------------------*/ +#include #include #include #include -#include +#include #include class Robot : public frc::IterativeRobot { public: Robot() { - myRobot.SetExpiration(0.1); - timer.Start(); + m_robotDrive.SetExpiration(0.1); + m_timer.Start(); } -private: - frc::RobotDrive myRobot{0, 1}; // Robot drive system - frc::Joystick stick{0}; // Only joystick - frc::LiveWindow* lw = frc::LiveWindow::GetInstance(); - frc::Timer timer; - void AutonomousInit() override { - timer.Reset(); - timer.Start(); + m_timer.Reset(); + m_timer.Start(); } void AutonomousPeriodic() override { // Drive for 2 seconds - if (timer.Get() < 2.0) { - myRobot.Drive(-0.5, 0.0); // Drive forwards half speed + if (m_timer.Get() < 2.0) { + // Drive forwards half speed + m_robotDrive.ArcadeDrive(-0.5, 0.0); } else { - myRobot.Drive(0.0, 0.0); // Stop robot + // Stop robot + m_robotDrive.ArcadeDrive(0.0, 0.0); } } @@ -42,10 +39,20 @@ private: void TeleopPeriodic() override { // Drive with arcade style (use right stick) - myRobot.ArcadeDrive(stick); + m_robotDrive.ArcadeDrive(m_stick.GetY(), m_stick.GetX()); } - void TestPeriodic() override { lw->Run(); } + void TestPeriodic() override { m_lw.Run(); } + +private: + // Robot drive system + frc::Spark m_left{0}; + frc::Spark m_right{1}; + frc::DifferentialDrive m_robotDrive{m_left, m_right}; + + frc::Joystick m_stick{0}; + frc::LiveWindow& m_lw = *frc::LiveWindow::GetInstance(); + frc::Timer m_timer; }; START_ROBOT_CLASS(Robot) diff --git a/wpilibcExamples/src/main/cpp/examples/Gyro/src/Robot.cpp b/wpilibcExamples/src/main/cpp/examples/Gyro/src/Robot.cpp index 8d7c2c0859..021b17e4b1 100644 --- a/wpilibcExamples/src/main/cpp/examples/Gyro/src/Robot.cpp +++ b/wpilibcExamples/src/main/cpp/examples/Gyro/src/Robot.cpp @@ -8,9 +8,10 @@ #include #include +#include #include #include -#include +#include /** * This is a sample program to demonstrate how to use a gyro sensor to make a @@ -22,7 +23,7 @@ class Robot : public frc::IterativeRobot { public: void RobotInit() override { - gyro.SetSensitivity(kVoltsPerDegreePerSecond); + m_gyro.SetSensitivity(kVoltsPerDegreePerSecond); } /** @@ -31,10 +32,10 @@ public: * angle. */ void TeleopPeriodic() override { - double turningValue = (kAngleSetpoint - gyro.GetAngle()) * kP; + double turningValue = (kAngleSetpoint - m_gyro.GetAngle()) * kP; // Invert the direction of the turn if we are going backwards - turningValue = std::copysign(turningValue, joystick.GetY()); - myRobot.Drive(joystick.GetY(), turningValue); + turningValue = std::copysign(turningValue, m_joystick.GetY()); + m_robotDrive.ArcadeDrive(m_joystick.GetY(), turningValue); } private: @@ -50,9 +51,12 @@ private: static constexpr int kGyroPort = 0; static constexpr int kJoystickPort = 0; - frc::RobotDrive myRobot{kLeftMotorPort, kRightMotorPort}; - frc::AnalogGyro gyro{kGyroPort}; - frc::Joystick joystick{kJoystickPort}; + frc::Spark m_left{kLeftMotorPort}; + frc::Spark m_right{kRightMotorPort}; + frc::DifferentialDrive m_robotDrive{m_left, m_right}; + + frc::AnalogGyro m_gyro{kGyroPort}; + frc::Joystick m_joystick{kJoystickPort}; }; START_ROBOT_CLASS(Robot) diff --git a/wpilibcExamples/src/main/cpp/examples/GyroMecanum/src/Robot.cpp b/wpilibcExamples/src/main/cpp/examples/GyroMecanum/src/Robot.cpp index 07f6cbbc1f..e839fa804d 100644 --- a/wpilibcExamples/src/main/cpp/examples/GyroMecanum/src/Robot.cpp +++ b/wpilibcExamples/src/main/cpp/examples/GyroMecanum/src/Robot.cpp @@ -6,9 +6,10 @@ /*----------------------------------------------------------------------------*/ #include +#include #include #include -#include +#include /** * This is a sample program that uses mecanum drive with a gyro sensor to @@ -20,18 +21,19 @@ public: void RobotInit() override { // invert the left side motors // you may need to change or remove this to match your robot - myRobot.SetInvertedMotor(RobotDrive::kFrontLeftMotor, true); - myRobot.SetInvertedMotor(RobotDrive::kRearLeftMotor, true); + m_frontLeft.SetInverted(true); + m_rearLeft.SetInverted(true); - gyro.SetSensitivity(kVoltsPerDegreePerSecond); + m_gyro.SetSensitivity(kVoltsPerDegreePerSecond); } /** * Mecanum drive is used with the gyro angle as an input. */ void TeleopPeriodic() override { - myRobot.MecanumDrive_Cartesian(joystick.GetX(), joystick.GetY(), - joystick.GetZ(), gyro.GetAngle()); + m_robotDrive.DriveCartesian(m_joystick.GetX(), + m_joystick.GetY(), m_joystick.GetZ(), + m_gyro.GetAngle()); } private: @@ -40,16 +42,21 @@ private: static constexpr double kVoltsPerDegreePerSecond = 0.0128; static constexpr int kFrontLeftMotorPort = 0; - static constexpr int kFrontRightMotorPort = 1; - static constexpr int kRearLeftMotorPort = 2; + static constexpr int kRearLeftMotorPort = 1; + static constexpr int kFrontRightMotorPort = 2; static constexpr int kRearRightMotorPort = 3; static constexpr int kGyroPort = 0; static constexpr int kJoystickPort = 0; - frc::RobotDrive myRobot{kFrontLeftMotorPort, kFrontRightMotorPort, - kRearLeftMotorPort, kRearRightMotorPort}; - frc::AnalogGyro gyro{kGyroPort}; - frc::Joystick joystick{kJoystickPort}; + frc::Spark m_frontLeft{kFrontLeftMotorPort}; + frc::Spark m_rearLeft{kRearLeftMotorPort}; + frc::Spark m_frontRight{kFrontRightMotorPort}; + frc::Spark m_rearRight{kRearRightMotorPort}; + frc::MecanumDrive m_robotDrive{ + m_frontLeft, m_rearLeft, m_frontRight, m_rearRight}; + + frc::AnalogGyro m_gyro{kGyroPort}; + frc::Joystick m_joystick{kJoystickPort}; }; START_ROBOT_CLASS(Robot) diff --git a/wpilibcExamples/src/main/cpp/examples/MecanumDrive/src/Robot.cpp b/wpilibcExamples/src/main/cpp/examples/MecanumDrive/src/Robot.cpp index 6aa4c03283..3183dd4382 100644 --- a/wpilibcExamples/src/main/cpp/examples/MecanumDrive/src/Robot.cpp +++ b/wpilibcExamples/src/main/cpp/examples/MecanumDrive/src/Robot.cpp @@ -5,9 +5,10 @@ /* the project. */ /*----------------------------------------------------------------------------*/ +#include #include #include -#include +#include /** * This is a demo program showing how to use Mecanum control with the RobotDrive @@ -18,8 +19,8 @@ public: void RobotInit() { // Invert the left side motors // You may need to change or remove this to match your robot - robotDrive.SetInvertedMotor(RobotDrive::kFrontLeftMotor, true); - robotDrive.SetInvertedMotor(RobotDrive::kRearLeftMotor, true); + m_frontLeft.SetInverted(true); + m_rearLeft.SetInverted(true); } void TeleopPeriodic() override { @@ -27,22 +28,26 @@ public: * forward * movement, and Z axis for rotation. */ - robotDrive.MecanumDrive_Cartesian( - stick.GetX(), stick.GetY(), stick.GetZ()); + m_robotDrive.DriveCartesian( + m_stick.GetX(), m_stick.GetY(), m_stick.GetZ()); } private: - static constexpr int kFrontLeftChannel = 2; - static constexpr int kRearLeftChannel = 3; - static constexpr int kFrontRightChannel = 1; - static constexpr int kRearRightChannel = 0; + static constexpr int kFrontLeftChannel = 0; + static constexpr int kRearLeftChannel = 1; + static constexpr int kFrontRightChannel = 2; + static constexpr int kRearRightChannel = 3; static constexpr int kJoystickChannel = 0; - frc::RobotDrive robotDrive{kFrontLeftChannel, kRearLeftChannel, - kFrontRightChannel, kRearRightChannel}; + frc::Spark m_frontLeft{kFrontLeftChannel}; + frc::Spark m_rearLeft{kRearLeftChannel}; + frc::Spark m_frontRight{kFrontRightChannel}; + frc::Spark m_rearRight{kRearRightChannel}; + frc::MecanumDrive m_robotDrive{ + m_frontLeft, m_rearLeft, m_frontRight, m_rearRight}; - frc::Joystick stick{kJoystickChannel}; + frc::Joystick m_stick{kJoystickChannel}; }; START_ROBOT_CLASS(Robot) diff --git a/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Commands/CheckForHotGoal.cpp b/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Commands/CheckForHotGoal.cpp index 3c68a979b6..62f4130bb5 100644 --- a/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Commands/CheckForHotGoal.cpp +++ b/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Commands/CheckForHotGoal.cpp @@ -15,5 +15,5 @@ CheckForHotGoal::CheckForHotGoal(double time) { // Make this return true when this Command no longer needs to run execute() bool CheckForHotGoal::IsFinished() { - return IsTimedOut() || Robot::shooter->GoalIsHot(); + return IsTimedOut() || Robot::shooter.GoalIsHot(); } diff --git a/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Commands/CloseClaw.cpp b/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Commands/CloseClaw.cpp index c8b7095f83..ee26e1834f 100644 --- a/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Commands/CloseClaw.cpp +++ b/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Commands/CloseClaw.cpp @@ -10,10 +10,10 @@ #include "../Robot.h" CloseClaw::CloseClaw() { - Requires(Robot::collector.get()); + Requires(&Robot::collector); } // Called just before this Command runs the first time void CloseClaw::Initialize() { - Robot::collector->Close(); + Robot::collector.Close(); } diff --git a/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Commands/DriveForward.cpp b/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Commands/DriveForward.cpp index fd39d8b195..c1b2061f93 100644 --- a/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Commands/DriveForward.cpp +++ b/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Commands/DriveForward.cpp @@ -12,9 +12,9 @@ #include "../Robot.h" void DriveForward::init(double dist, double maxSpeed) { - Requires(Robot::drivetrain.get()); - distance = dist; - driveForwardSpeed = maxSpeed; + Requires(&Robot::drivetrain); + m_distance = dist; + m_driveForwardSpeed = maxSpeed; } DriveForward::DriveForward() { @@ -31,29 +31,29 @@ DriveForward::DriveForward(double dist, double maxSpeed) { // Called just before this Command runs the first time void DriveForward::Initialize() { - Robot::drivetrain->GetRightEncoder()->Reset(); + Robot::drivetrain.GetRightEncoder().Reset(); SetTimeout(2); } // Called repeatedly when this Command is scheduled to run void DriveForward::Execute() { - error = (distance - - Robot::drivetrain->GetRightEncoder()->GetDistance()); - if (driveForwardSpeed * kP * error >= driveForwardSpeed) { - Robot::drivetrain->TankDrive( - driveForwardSpeed, driveForwardSpeed); + m_error = (m_distance + - Robot::drivetrain.GetRightEncoder().GetDistance()); + if (m_driveForwardSpeed * kP * m_error >= m_driveForwardSpeed) { + Robot::drivetrain.TankDrive( + m_driveForwardSpeed, m_driveForwardSpeed); } else { - Robot::drivetrain->TankDrive(driveForwardSpeed * kP * error, - driveForwardSpeed * kP * error); + Robot::drivetrain.TankDrive(m_driveForwardSpeed * kP * m_error, + m_driveForwardSpeed * kP * m_error); } } // Make this return true when this Command no longer needs to run execute() bool DriveForward::IsFinished() { - return (std::fabs(error) <= kTolerance) || IsTimedOut(); + return (std::fabs(m_error) <= kTolerance) || IsTimedOut(); } // Called once after isFinished returns true void DriveForward::End() { - Robot::drivetrain->Stop(); + Robot::drivetrain.Stop(); } diff --git a/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Commands/DriveForward.h b/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Commands/DriveForward.h index 8fd6b4f9bb..a22fe49dd0 100644 --- a/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Commands/DriveForward.h +++ b/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Commands/DriveForward.h @@ -24,9 +24,9 @@ public: void End() override; private: - double driveForwardSpeed; - double distance; - double error = 0; + double m_driveForwardSpeed; + double m_distance; + double m_error = 0; static constexpr double kTolerance = 0.1; static constexpr double kP = -1.0 / 5.0; diff --git a/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Commands/DriveWithJoystick.cpp b/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Commands/DriveWithJoystick.cpp index 1a982c0034..8d75aedb45 100644 --- a/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Commands/DriveWithJoystick.cpp +++ b/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Commands/DriveWithJoystick.cpp @@ -10,12 +10,13 @@ #include "../Robot.h" DriveWithJoystick::DriveWithJoystick() { - Requires(Robot::drivetrain.get()); + Requires(&Robot::drivetrain); } // Called repeatedly when this Command is scheduled to run void DriveWithJoystick::Execute() { - Robot::drivetrain->TankDrive(Robot::oi->GetJoystick()); + auto& joystick = Robot::oi.GetJoystick(); + Robot::drivetrain.TankDrive(joystick.GetY(), joystick.GetRawAxis(4)); } // Make this return true when this Command no longer needs to run execute() @@ -25,5 +26,5 @@ bool DriveWithJoystick::IsFinished() { // Called once after isFinished returns true void DriveWithJoystick::End() { - Robot::drivetrain->Stop(); + Robot::drivetrain.Stop(); } diff --git a/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Commands/ExtendShooter.cpp b/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Commands/ExtendShooter.cpp index 9b232b99cf..3c312d01f9 100644 --- a/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Commands/ExtendShooter.cpp +++ b/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Commands/ExtendShooter.cpp @@ -11,15 +11,15 @@ ExtendShooter::ExtendShooter() : frc::TimedCommand(1.0) { - Requires(Robot::shooter.get()); + Requires(&Robot::shooter); } // Called just before this Command runs the first time void ExtendShooter::Initialize() { - Robot::shooter->ExtendBoth(); + Robot::shooter.ExtendBoth(); } // Called once after isFinished returns true void ExtendShooter::End() { - Robot::shooter->RetractBoth(); + Robot::shooter.RetractBoth(); } diff --git a/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Commands/OpenClaw.cpp b/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Commands/OpenClaw.cpp index 652fc1fcbc..fc9dad9228 100644 --- a/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Commands/OpenClaw.cpp +++ b/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Commands/OpenClaw.cpp @@ -10,15 +10,15 @@ #include "../Robot.h" OpenClaw::OpenClaw() { - Requires(Robot::collector.get()); + Requires(&Robot::collector); } // Called just before this Command runs the first time void OpenClaw::Initialize() { - Robot::collector->Open(); + Robot::collector.Open(); } // Make this return true when this Command no longer needs to run execute() bool OpenClaw::IsFinished() { - return Robot::collector->IsOpen(); + return Robot::collector.IsOpen(); } diff --git a/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Commands/SetCollectionSpeed.cpp b/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Commands/SetCollectionSpeed.cpp index d1f279d046..a1466fc753 100644 --- a/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Commands/SetCollectionSpeed.cpp +++ b/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Commands/SetCollectionSpeed.cpp @@ -10,11 +10,11 @@ #include "../Robot.h" SetCollectionSpeed::SetCollectionSpeed(double speed) { - Requires(Robot::collector.get()); - this->speed = speed; + Requires(&Robot::collector); + m_speed = speed; } // Called just before this Command runs the first time void SetCollectionSpeed::Initialize() { - Robot::collector->SetSpeed(speed); + Robot::collector.SetSpeed(m_speed); } diff --git a/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Commands/SetCollectionSpeed.h b/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Commands/SetCollectionSpeed.h index 656d490524..f1b7c26f37 100644 --- a/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Commands/SetCollectionSpeed.h +++ b/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Commands/SetCollectionSpeed.h @@ -20,5 +20,5 @@ public: void Initialize() override; private: - double speed; + double m_speed; }; diff --git a/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Commands/SetPivotSetpoint.cpp b/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Commands/SetPivotSetpoint.cpp index 9d60639cff..bd7435a3e2 100644 --- a/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Commands/SetPivotSetpoint.cpp +++ b/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Commands/SetPivotSetpoint.cpp @@ -10,17 +10,17 @@ #include "../Robot.h" SetPivotSetpoint::SetPivotSetpoint(double setpoint) { - this->setpoint = setpoint; - Requires(Robot::pivot.get()); + m_setpoint = setpoint; + Requires(&Robot::pivot); } // Called just before this Command runs the first time void SetPivotSetpoint::Initialize() { - Robot::pivot->Enable(); - Robot::pivot->SetSetpoint(setpoint); + Robot::pivot.Enable(); + Robot::pivot.SetSetpoint(m_setpoint); } // Make this return true when this Command no longer needs to run execute() bool SetPivotSetpoint::IsFinished() { - return Robot::pivot->OnTarget(); + return Robot::pivot.OnTarget(); } diff --git a/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Commands/SetPivotSetpoint.h b/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Commands/SetPivotSetpoint.h index 6d5030cac8..ca93f26180 100644 --- a/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Commands/SetPivotSetpoint.h +++ b/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Commands/SetPivotSetpoint.h @@ -21,5 +21,5 @@ public: bool IsFinished() override; private: - double setpoint; + double m_setpoint; }; diff --git a/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Commands/WaitForBall.cpp b/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Commands/WaitForBall.cpp index f8382b6969..8f78d0fc7c 100644 --- a/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Commands/WaitForBall.cpp +++ b/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Commands/WaitForBall.cpp @@ -10,10 +10,10 @@ #include "../Robot.h" WaitForBall::WaitForBall() { - Requires(Robot::collector.get()); + Requires(&Robot::collector); } // Make this return true when this Command no longer needs to run execute() bool WaitForBall::IsFinished() { - return Robot::collector->HasBall(); + return Robot::collector.HasBall(); } diff --git a/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Commands/WaitForPressure.cpp b/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Commands/WaitForPressure.cpp index adf76f1e20..be3a432c8a 100644 --- a/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Commands/WaitForPressure.cpp +++ b/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Commands/WaitForPressure.cpp @@ -10,10 +10,10 @@ #include "../Robot.h" WaitForPressure::WaitForPressure() { - Requires(Robot::pneumatics.get()); + Requires(&Robot::pneumatics); } // Make this return true when this Command no longer needs to run execute() bool WaitForPressure::IsFinished() { - return Robot::pneumatics->IsPressurized(); + return Robot::pneumatics.IsPressurized(); } diff --git a/wpilibcExamples/src/main/cpp/examples/PacGoat/src/OI.cpp b/wpilibcExamples/src/main/cpp/examples/PacGoat/src/OI.cpp index b542cadb4c..2963ad4812 100644 --- a/wpilibcExamples/src/main/cpp/examples/PacGoat/src/OI.cpp +++ b/wpilibcExamples/src/main/cpp/examples/PacGoat/src/OI.cpp @@ -17,13 +17,13 @@ #include "Subsystems/Pivot.h" OI::OI() { - R1.WhenPressed(new LowGoal()); - R2.WhenPressed(new Collect()); + m_r1.WhenPressed(new LowGoal()); + m_r2.WhenPressed(new Collect()); - L1.WhenPressed(new SetPivotSetpoint(Pivot::kShoot)); - L2.WhenPressed(new SetPivotSetpoint(Pivot::kShootNear)); + m_l1.WhenPressed(new SetPivotSetpoint(Pivot::kShoot)); + m_l2.WhenPressed(new SetPivotSetpoint(Pivot::kShootNear)); - sticks.WhenActive(new Shoot()); + m_sticks.WhenActive(new Shoot()); // SmartDashboard Buttons frc::SmartDashboard::PutData("Drive Forward", new DriveForward(2.25)); @@ -36,6 +36,6 @@ OI::OI() { new SetCollectionSpeed(Collector::kReverse)); } -frc::Joystick* OI::GetJoystick() { - return &joystick; +frc::Joystick& OI::GetJoystick() { + return m_joystick; } diff --git a/wpilibcExamples/src/main/cpp/examples/PacGoat/src/OI.h b/wpilibcExamples/src/main/cpp/examples/PacGoat/src/OI.h index ad1a3e96de..2e927b4625 100644 --- a/wpilibcExamples/src/main/cpp/examples/PacGoat/src/OI.h +++ b/wpilibcExamples/src/main/cpp/examples/PacGoat/src/OI.h @@ -15,15 +15,15 @@ class OI { public: OI(); - frc::Joystick* GetJoystick(); + frc::Joystick& GetJoystick(); private: - frc::Joystick joystick{0}; + frc::Joystick m_joystick{0}; - frc::JoystickButton L1{&joystick, 11}; - frc::JoystickButton L2{&joystick, 9}; - frc::JoystickButton R1{&joystick, 12}; - frc::JoystickButton R2{&joystick, 10}; + frc::JoystickButton m_l1{&m_joystick, 11}; + frc::JoystickButton m_l2{&m_joystick, 9}; + frc::JoystickButton m_r1{&m_joystick, 12}; + frc::JoystickButton m_r2{&m_joystick, 10}; - DoubleButton sticks{&joystick, 2, 3}; + DoubleButton m_sticks{&m_joystick, 2, 3}; }; diff --git a/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Robot.cpp b/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Robot.cpp index 9502d8922f..f2f7c8034b 100644 --- a/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Robot.cpp +++ b/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Robot.cpp @@ -11,32 +11,32 @@ #include -std::shared_ptr Robot::drivetrain = std::make_shared(); -std::shared_ptr Robot::pivot = std::make_shared(); -std::shared_ptr Robot::collector = std::make_shared(); -std::shared_ptr Robot::shooter = std::make_shared(); -std::shared_ptr Robot::pneumatics = std::make_shared(); -std::unique_ptr Robot::oi = std::make_unique(); +DriveTrain Robot::drivetrain; +Pivot Robot::pivot; +Collector Robot::collector; +Shooter Robot::shooter; +Pneumatics Robot::pneumatics; +OI Robot::oi; void Robot::RobotInit() { // Show what command your subsystem is running on the SmartDashboard - frc::SmartDashboard::PutData(drivetrain.get()); - frc::SmartDashboard::PutData(pivot.get()); - frc::SmartDashboard::PutData(collector.get()); - frc::SmartDashboard::PutData(shooter.get()); - frc::SmartDashboard::PutData(pneumatics.get()); + frc::SmartDashboard::PutData(&drivetrain); + frc::SmartDashboard::PutData(&pivot); + frc::SmartDashboard::PutData(&collector); + frc::SmartDashboard::PutData(&shooter); + frc::SmartDashboard::PutData(&pneumatics); // instantiate the command used for the autonomous period - autoChooser.AddDefault("Drive and Shoot", driveAndShootAuto.get()); - autoChooser.AddObject("Drive Forward", driveForwardAuto.get()); - frc::SmartDashboard::PutData("Auto Mode", &autoChooser); + m_autoChooser.AddDefault("Drive and Shoot", &m_driveAndShootAuto); + m_autoChooser.AddObject("Drive Forward", &m_driveForwardAuto); + frc::SmartDashboard::PutData("Auto Mode", &m_autoChooser); - pneumatics->Start(); // Pressurize the pneumatics. + pneumatics.Start(); // Pressurize the pneumatics. } void Robot::AutonomousInit() { - autonomousCommand = autoChooser.GetSelected(); - autonomousCommand->Start(); + m_autonomousCommand = m_autoChooser.GetSelected(); + m_autonomousCommand->Start(); } void Robot::AutonomousPeriodic() { @@ -49,8 +49,8 @@ void Robot::TeleopInit() { // teleop starts running. If you want the autonomous to // continue until interrupted by another command, remove // this line or comment it out. - if (autonomousCommand != nullptr) { - autonomousCommand->Cancel(); + if (m_autonomousCommand != nullptr) { + m_autonomousCommand->Cancel(); } std::cout << "Starting Teleop" << std::endl; } @@ -65,7 +65,7 @@ void Robot::TestPeriodic() { } void Robot::DisabledInit() { - shooter->Unlatch(); + shooter.Unlatch(); } void Robot::DisabledPeriodic() { @@ -76,12 +76,12 @@ void Robot::DisabledPeriodic() { * Log interesting values to the SmartDashboard. */ void Robot::Log() { - Robot::pneumatics->WritePressure(); - frc::SmartDashboard::PutNumber("Pivot Pot Value", pivot->GetAngle()); + Robot::pneumatics.WritePressure(); + frc::SmartDashboard::PutNumber("Pivot Pot Value", pivot.GetAngle()); frc::SmartDashboard::PutNumber("Left Distance", - drivetrain->GetLeftEncoder()->GetDistance()); + drivetrain.GetLeftEncoder().GetDistance()); frc::SmartDashboard::PutNumber("Right Distance", - drivetrain->GetRightEncoder()->GetDistance()); + drivetrain.GetRightEncoder().GetDistance()); } START_ROBOT_CLASS(Robot) diff --git a/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Robot.h b/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Robot.h index 92104c549e..ba63e39a2c 100644 --- a/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Robot.h +++ b/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Robot.h @@ -7,8 +7,6 @@ #pragma once -#include - #include #include #include @@ -24,19 +22,18 @@ class Robot : public IterativeRobot { public: - static std::shared_ptr drivetrain; - static std::shared_ptr pivot; - static std::shared_ptr collector; - static std::shared_ptr shooter; - static std::shared_ptr pneumatics; - static std::unique_ptr oi; + static DriveTrain drivetrain; + static Pivot pivot; + static Collector collector; + static Shooter shooter; + static Pneumatics pneumatics; + static OI oi; private: - frc::Command* autonomousCommand = nullptr; - std::unique_ptr driveAndShootAuto{ - new DriveAndShootAutonomous()}; - std::unique_ptr driveForwardAuto{new DriveForward()}; - SendableChooser autoChooser; + frc::Command* m_autonomousCommand = nullptr; + DriveAndShootAutonomous m_driveAndShootAuto; + DriveForward m_driveForwardAuto; + SendableChooser m_autoChooser; void RobotInit() override; void AutonomousInit() override; diff --git a/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Subsystems/Collector.cpp b/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Subsystems/Collector.cpp index c3afc8f91e..d5e5233fe9 100644 --- a/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Subsystems/Collector.cpp +++ b/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Subsystems/Collector.cpp @@ -13,36 +13,37 @@ Collector::Collector() : frc::Subsystem("Collector") { // Put everything to the LiveWindow for testing. // XXX: LiveWindow::GetInstance()->AddActuator("Collector", "Roller - // Motor", &rollerMotor); + // Motor", &m_rollerMotor); LiveWindow::GetInstance()->AddSensor( - "Collector", "Ball Detector", &ballDetector); + "Collector", "Ball Detector", &m_ballDetector); LiveWindow::GetInstance()->AddSensor( - "Collector", "Claw Open Detector", &openDetector); - LiveWindow::GetInstance()->AddActuator("Collector", "Piston", &piston); + "Collector", "Claw Open Detector", &m_openDetector); + LiveWindow::GetInstance()->AddActuator( + "Collector", "Piston", &m_piston); } bool Collector::HasBall() { - return ballDetector.Get(); // TODO: prepend ! to reflect real robot + return m_ballDetector.Get(); // TODO: prepend ! to reflect real robot } void Collector::SetSpeed(double speed) { - rollerMotor.Set(-speed); + m_rollerMotor.Set(-speed); } void Collector::Stop() { - rollerMotor.Set(0); + m_rollerMotor.Set(0); } bool Collector::IsOpen() { - return openDetector.Get(); // TODO: prepend ! to reflect real robot + return m_openDetector.Get(); // TODO: prepend ! to reflect real robot } void Collector::Open() { - piston.Set(true); + m_piston.Set(true); } void Collector::Close() { - piston.Set(false); + m_piston.Set(false); } void Collector::InitDefaultCommand() {} diff --git a/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Subsystems/Collector.h b/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Subsystems/Collector.h index a090452999..9d015d4de0 100644 --- a/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Subsystems/Collector.h +++ b/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Subsystems/Collector.h @@ -10,7 +10,7 @@ #include #include #include -#include +#include /** * The Collector subsystem has one motor for the rollers, a limit switch for @@ -69,8 +69,8 @@ public: private: // Subsystem devices - frc::Victor rollerMotor{6}; - frc::DigitalInput ballDetector{10}; - frc::Solenoid piston{1}; - frc::DigitalInput openDetector{6}; + frc::Spark m_rollerMotor{6}; + frc::DigitalInput m_ballDetector{10}; + frc::Solenoid m_piston{1}; + frc::DigitalInput m_openDetector{6}; }; diff --git a/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Subsystems/DriveTrain.cpp b/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Subsystems/DriveTrain.cpp index bc4735ebbd..02f70cbb27 100644 --- a/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Subsystems/DriveTrain.cpp +++ b/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Subsystems/DriveTrain.cpp @@ -17,77 +17,70 @@ DriveTrain::DriveTrain() : frc::Subsystem("DriveTrain") { // frc::LiveWindow::GetInstance()->AddActuator("DriveTrain", "Front Left - // CIM", &frontLeftCIM); + // CIM", &m_frontLeftCIM); // frc::LiveWindow::GetInstance()->AddActuator("DriveTrain", "Front - // Right CIM", &frontRightCIM); + // Right CIM", &m_frontRightCIM); // frc::LiveWindow::GetInstance()->AddActuator("DriveTrain", "Back Left - // CIM", &backLeftCIM); + // CIM", &m_backLeftCIM); // frc::LiveWindow::GetInstance()->AddActuator("DriveTrain", "Back Right - // CIM", &backRightCIM); + // CIM", &m_backRightCIM); // Configure the RobotDrive to reflect the fact that all our motors are // wired backwards and our drivers sensitivity preferences. - drive.SetSafetyEnabled(false); - drive.SetExpiration(0.1); - drive.SetSensitivity(0.5); - drive.SetMaxOutput(1.0); - drive.SetInvertedMotor(RobotDrive::kFrontLeftMotor, true); - drive.SetInvertedMotor(RobotDrive::kRearLeftMotor, true); - drive.SetInvertedMotor(RobotDrive::kFrontRightMotor, true); - drive.SetInvertedMotor(RobotDrive::kRearRightMotor, true); + m_robotDrive.SetSafetyEnabled(false); + m_robotDrive.SetExpiration(0.1); + m_robotDrive.SetMaxOutput(1.0); + m_leftCIMs.SetInverted(true); + m_rightCIMs.SetInverted(true); // Configure encoders - rightEncoder->SetPIDSourceType(PIDSourceType::kDisplacement); - leftEncoder->SetPIDSourceType(PIDSourceType::kDisplacement); + m_rightEncoder.SetPIDSourceType(PIDSourceType::kDisplacement); + m_leftEncoder.SetPIDSourceType(PIDSourceType::kDisplacement); #ifndef SIMULATION // Converts to feet - rightEncoder->SetDistancePerPulse(0.0785398); - leftEncoder->SetDistancePerPulse(0.0785398); + m_rightEncoder.SetDistancePerPulse(0.0785398); + m_leftEncoder.SetDistancePerPulse(0.0785398); #else // Convert to feet 4in diameter wheels with 360 tick simulated encoders - rightEncoder->SetDistancePerPulse( + m_rightEncoder.SetDistancePerPulse( (4.0 /*in*/ * M_PI) / (360.0 * 12.0 /*in/ft*/)); - leftEncoder->SetDistancePerPulse( + m_leftEncoder.SetDistancePerPulse( (4.0 /*in*/ * M_PI) / (360.0 * 12.0 /*in/ft*/)); #endif LiveWindow::GetInstance()->AddSensor( - "DriveTrain", "Right Encoder", rightEncoder); + "DriveTrain", "Right Encoder", m_rightEncoder); LiveWindow::GetInstance()->AddSensor( - "DriveTrain", "Left Encoder", leftEncoder); + "DriveTrain", "Left Encoder", m_leftEncoder); // Configure gyro #ifndef SIMULATION - gyro.SetSensitivity(0.007); // TODO: Handle more gracefully? + m_gyro.SetSensitivity(0.007); // TODO: Handle more gracefully? #endif - LiveWindow::GetInstance()->AddSensor("DriveTrain", "Gyro", &gyro); + LiveWindow::GetInstance()->AddSensor("DriveTrain", "Gyro", &m_gyro); } void DriveTrain::InitDefaultCommand() { SetDefaultCommand(new DriveWithJoystick()); } -void DriveTrain::TankDrive(Joystick* joy) { - drive.TankDrive(joy->GetY(), joy->GetRawAxis(4)); -} - void DriveTrain::TankDrive(double leftAxis, double rightAxis) { - drive.TankDrive(leftAxis, rightAxis); + m_robotDrive.TankDrive(leftAxis, rightAxis); } void DriveTrain::Stop() { - drive.TankDrive(0.0, 0.0); + m_robotDrive.TankDrive(0.0, 0.0); } -std::shared_ptr DriveTrain::GetLeftEncoder() { - return leftEncoder; +Encoder& DriveTrain::GetLeftEncoder() { + return m_leftEncoder; } -std::shared_ptr DriveTrain::GetRightEncoder() { - return rightEncoder; +Encoder& DriveTrain::GetRightEncoder() { + return m_rightEncoder; } double DriveTrain::GetAngle() { - return gyro.GetAngle(); + return m_gyro.GetAngle(); } diff --git a/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Subsystems/DriveTrain.h b/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Subsystems/DriveTrain.h index 62bc24c5ab..28d612bd9a 100644 --- a/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Subsystems/DriveTrain.h +++ b/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Subsystems/DriveTrain.h @@ -7,13 +7,12 @@ #pragma once -#include - #include #include +#include #include -#include -#include +#include +#include namespace frc { class Joystick; @@ -34,11 +33,6 @@ public: */ void InitDefaultCommand(); - /** - * @param joy PS3 style joystick to use as the input for tank drive. - */ - void TankDrive(frc::Joystick* joy); - /** * @param leftAxis Left sides value * @param rightAxis Right sides value @@ -54,13 +48,13 @@ public: * @return The encoder getting the distance and speed of left side of * the drivetrain. */ - std::shared_ptr GetLeftEncoder(); + Encoder& GetLeftEncoder(); /** * @return The encoder getting the distance and speed of right side of * the drivetrain. */ - std::shared_ptr GetRightEncoder(); + Encoder& GetRightEncoder(); /** * @return The current angle of the drivetrain. @@ -69,17 +63,17 @@ public: private: // Subsystem devices - frc::Victor frontLeftCIM{1}; - frc::Victor rearLeftCIM{2}; - frc::Victor frontRightCIM{3}; - frc::Victor rearRightCIM{4}; - frc::RobotDrive drive{frontRightCIM, rearLeftCIM, frontRightCIM, - rearRightCIM}; - std::shared_ptr rightEncoder = - std::make_shared( - 1, 2, true, Encoder::k4X); - std::shared_ptr leftEncoder = - std::make_shared( - 3, 4, false, Encoder::k4X); - frc::AnalogGyro gyro{0}; + frc::Spark m_frontLeftCIM{1}; + frc::Spark m_rearLeftCIM{2}; + frc::SpeedControllerGroup m_leftCIMs{m_frontLeftCIM, m_rearLeftCIM}; + + frc::Spark m_frontRightCIM{3}; + frc::Spark m_rearRightCIM{4}; + frc::SpeedControllerGroup m_rightCIMs{m_frontRightCIM, m_rearRightCIM}; + + frc::DifferentialDrive m_robotDrive{m_leftCIMs, m_rightCIMs}; + + frc::Encoder m_rightEncoder{1, 2, true, Encoder::k4X}; + frc::Encoder m_leftEncoder{3, 4, false, Encoder::k4X}; + frc::AnalogGyro m_gyro{0}; }; diff --git a/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Subsystems/Pivot.cpp b/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Subsystems/Pivot.cpp index 9ddb99872c..59d4e153c4 100644 --- a/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Subsystems/Pivot.cpp +++ b/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Subsystems/Pivot.cpp @@ -21,12 +21,13 @@ Pivot::Pivot() // Put everything to the LiveWindow for testing. frc::LiveWindow::GetInstance()->AddSensor( - "Pivot", "Upper Limit Switch", &upperLimitSwitch); + "Pivot", "Upper Limit Switch", &m_upperLimitSwitch); frc::LiveWindow::GetInstance()->AddSensor( - "Pivot", "Lower Limit Switch", &lowerLimitSwitch); - // XXX: frc::LiveWindow::GetInstance()->AddSensor("Pivot", "Pot", &pot); + "Pivot", "Lower Limit Switch", &m_lowerLimitSwitch); + // XXX: frc::LiveWindow::GetInstance()->AddSensor("Pivot", "Pot", + // &m_pot); // XXX: frc::LiveWindow::GetInstance()->AddActuator("Pivot", "Motor", - // &motor); + // &m_motor); frc::LiveWindow::GetInstance()->AddActuator( "Pivot", "PIDSubsystem Controller", GetPIDController()); } @@ -34,23 +35,23 @@ Pivot::Pivot() void InitDefaultCommand() {} double Pivot::ReturnPIDInput() { - return pot.Get(); + return m_pot.Get(); } void Pivot::UsePIDOutput(double output) { - motor.PIDWrite(output); + m_motor.PIDWrite(output); } bool Pivot::IsAtUpperLimit() { - return upperLimitSwitch.Get(); // TODO: inverted from real robot - // (prefix with !) + return m_upperLimitSwitch.Get(); // TODO: inverted from real robot + // (prefix with !) } bool Pivot::IsAtLowerLimit() { - return lowerLimitSwitch.Get(); // TODO: inverted from real robot - // (prefix with !) + return m_lowerLimitSwitch.Get(); // TODO: inverted from real robot + // (prefix with !) } double Pivot::GetAngle() { - return pot.Get(); + return m_pot.Get(); } diff --git a/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Subsystems/Pivot.h b/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Subsystems/Pivot.h index d7671b250a..a3c490bcfa 100644 --- a/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Subsystems/Pivot.h +++ b/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Subsystems/Pivot.h @@ -10,7 +10,7 @@ #include #include #include -#include +#include /** * The Pivot subsystem contains the Van-door motor and the pot for PID control @@ -61,14 +61,14 @@ private: // Subsystem devices // Sensors for measuring the position of the pivot - frc::DigitalInput upperLimitSwitch{13}; - frc::DigitalInput lowerLimitSwitch{12}; + frc::DigitalInput m_upperLimitSwitch{13}; + frc::DigitalInput m_lowerLimitSwitch{12}; /* 0 degrees is vertical facing up. * Angle increases the more forward the pivot goes. */ - frc::AnalogPotentiometer pot{1}; + frc::AnalogPotentiometer m_pot{1}; // Motor to move the pivot - frc::Victor motor{5}; + frc::Spark m_motor{5}; }; diff --git a/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Subsystems/Pneumatics.cpp b/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Subsystems/Pneumatics.cpp index e1f6ddd056..c84f636365 100644 --- a/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Subsystems/Pneumatics.cpp +++ b/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Subsystems/Pneumatics.cpp @@ -12,7 +12,7 @@ Pneumatics::Pneumatics() : frc::Subsystem("Pneumatics") { frc::LiveWindow::GetInstance()->AddSensor( - "Pneumatics", "Pressure Sensor", pressureSensor); + "Pneumatics", "Pressure Sensor", m_pressureSensor); } /** @@ -26,7 +26,7 @@ void Pneumatics::InitDefaultCommand() {} */ void Pneumatics::Start() { #ifndef SIMULATION - compressor.Start(); + m_compressor.Start(); #endif } @@ -35,7 +35,7 @@ void Pneumatics::Start() { */ bool Pneumatics::IsPressurized() { #ifndef SIMULATION - return kMaxPressure <= pressureSensor.GetVoltage(); + return kMaxPressure <= m_pressureSensor.GetVoltage(); #else return true; // NOTE: Simulation always has full pressure #endif @@ -45,5 +45,6 @@ bool Pneumatics::IsPressurized() { * Puts the pressure on the SmartDashboard. */ void Pneumatics::WritePressure() { - frc::SmartDashboard::PutNumber("Pressure", pressureSensor.GetVoltage()); + frc::SmartDashboard::PutNumber( + "Pressure", m_pressureSensor.GetVoltage()); } diff --git a/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Subsystems/Pneumatics.h b/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Subsystems/Pneumatics.h index ae8538d7ee..fe33e8edff 100644 --- a/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Subsystems/Pneumatics.h +++ b/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Subsystems/Pneumatics.h @@ -43,10 +43,10 @@ public: void WritePressure(); private: - frc::AnalogInput pressureSensor{3}; + frc::AnalogInput m_pressureSensor{3}; #ifndef SIMULATION - frc::Compressor compressor{1}; // TODO: (1, 14, 1, 8); + frc::Compressor m_compressor{1}; // TODO: (1, 14, 1, 8); #endif static constexpr double kMaxPressure = 2.55; diff --git a/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Subsystems/Shooter.cpp b/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Subsystems/Shooter.cpp index 03b3479a00..432b2ed76b 100644 --- a/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Subsystems/Shooter.cpp +++ b/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Subsystems/Shooter.cpp @@ -13,13 +13,14 @@ Shooter::Shooter() : Subsystem("Shooter") { // Put everything to the LiveWindow for testing. frc::LiveWindow::GetInstance()->AddSensor( - "Shooter", "Hot Goal Sensor", &hotGoalSensor); + "Shooter", "Hot Goal Sensor", &m_hotGoalSensor); frc::LiveWindow::GetInstance()->AddSensor("Shooter", - "Piston1 Reed Switch Front ", &piston1ReedSwitchFront); + "Piston1 Reed Switch Front ", + &m_piston1ReedSwitchFront); frc::LiveWindow::GetInstance()->AddSensor("Shooter", - "Piston1 Reed Switch Back ", &piston1ReedSwitchBack); + "Piston1 Reed Switch Back ", &m_piston1ReedSwitchBack); frc::LiveWindow::GetInstance()->AddActuator( - "Shooter", "Latch Piston", &latchPiston); + "Shooter", "Latch Piston", &m_latchPiston); } void Shooter::InitDefaultCommand() { @@ -28,64 +29,64 @@ void Shooter::InitDefaultCommand() { } void Shooter::ExtendBoth() { - piston1.Set(frc::DoubleSolenoid::kForward); - piston2.Set(frc::DoubleSolenoid::kForward); + m_piston1.Set(frc::DoubleSolenoid::kForward); + m_piston2.Set(frc::DoubleSolenoid::kForward); } void Shooter::RetractBoth() { - piston1.Set(frc::DoubleSolenoid::kReverse); - piston2.Set(frc::DoubleSolenoid::kReverse); + m_piston1.Set(frc::DoubleSolenoid::kReverse); + m_piston2.Set(frc::DoubleSolenoid::kReverse); } void Shooter::Extend1() { - piston1.Set(frc::DoubleSolenoid::kForward); + m_piston1.Set(frc::DoubleSolenoid::kForward); } void Shooter::Retract1() { - piston1.Set(frc::DoubleSolenoid::kReverse); + m_piston1.Set(frc::DoubleSolenoid::kReverse); } void Shooter::Extend2() { - piston2.Set(frc::DoubleSolenoid::kReverse); + m_piston2.Set(frc::DoubleSolenoid::kReverse); } void Shooter::Retract2() { - piston2.Set(frc::DoubleSolenoid::kForward); + m_piston2.Set(frc::DoubleSolenoid::kForward); } void Shooter::Off1() { - piston1.Set(frc::DoubleSolenoid::kOff); + m_piston1.Set(frc::DoubleSolenoid::kOff); } void Shooter::Off2() { - piston2.Set(frc::DoubleSolenoid::kOff); + m_piston2.Set(frc::DoubleSolenoid::kOff); } void Shooter::Unlatch() { - latchPiston.Set(true); + m_latchPiston.Set(true); } void Shooter::Latch() { - latchPiston.Set(false); + m_latchPiston.Set(false); } void Shooter::ToggleLatchPosition() { - latchPiston.Set(!latchPiston.Get()); + m_latchPiston.Set(!m_latchPiston.Get()); } bool Shooter::Piston1IsExtended() { - return !piston1ReedSwitchFront.Get(); + return !m_piston1ReedSwitchFront.Get(); } bool Shooter::Piston1IsRetracted() { - return !piston1ReedSwitchBack.Get(); + return !m_piston1ReedSwitchBack.Get(); } void Shooter::OffBoth() { - piston1.Set(frc::DoubleSolenoid::kOff); - piston2.Set(frc::DoubleSolenoid::kOff); + m_piston1.Set(frc::DoubleSolenoid::kOff); + m_piston2.Set(frc::DoubleSolenoid::kOff); } bool Shooter::GoalIsHot() { - return hotGoalSensor.Get(); + return m_hotGoalSensor.Get(); } diff --git a/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Subsystems/Shooter.h b/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Subsystems/Shooter.h index 7b0e7cd213..a77676e599 100644 --- a/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Subsystems/Shooter.h +++ b/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Subsystems/Shooter.h @@ -117,11 +117,11 @@ public: private: // Devices - frc::DoubleSolenoid piston1{3, 4}; - frc::DoubleSolenoid piston2{5, 6}; - frc::Solenoid latchPiston{1, 2}; - frc::DigitalInput piston1ReedSwitchFront{9}; - frc::DigitalInput piston1ReedSwitchBack{11}; - frc::DigitalInput hotGoalSensor{ + frc::DoubleSolenoid m_piston1{3, 4}; + frc::DoubleSolenoid m_piston2{5, 6}; + frc::Solenoid m_latchPiston{1, 2}; + frc::DigitalInput m_piston1ReedSwitchFront{9}; + frc::DigitalInput m_piston1ReedSwitchBack{11}; + frc::DigitalInput m_hotGoalSensor{ 7}; // NOTE: Currently ignored in simulation }; diff --git a/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Triggers/DoubleButton.cpp b/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Triggers/DoubleButton.cpp index 2bae9c657d..0fa28f1d3e 100644 --- a/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Triggers/DoubleButton.cpp +++ b/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Triggers/DoubleButton.cpp @@ -9,12 +9,12 @@ #include -DoubleButton::DoubleButton(frc::Joystick* joy, int button1, int button2) { - this->joy = joy; - this->button1 = button1; - this->button2 = button2; +DoubleButton::DoubleButton(frc::Joystick* joy, int button1, int button2) + : m_joy(*joy) { + m_button1 = button1; + m_button2 = button2; } bool DoubleButton::Get() { - return joy->GetRawButton(button1) && joy->GetRawButton(button2); + return m_joy.GetRawButton(m_button1) && m_joy.GetRawButton(m_button2); } diff --git a/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Triggers/DoubleButton.h b/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Triggers/DoubleButton.h index b3c074e360..2370c97702 100644 --- a/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Triggers/DoubleButton.h +++ b/wpilibcExamples/src/main/cpp/examples/PacGoat/src/Triggers/DoubleButton.h @@ -20,7 +20,7 @@ public: bool Get(); private: - frc::Joystick* joy; - int button1; - int button2; + frc::Joystick& m_joy; + int m_button1; + int m_button2; }; diff --git a/wpilibcExamples/src/main/cpp/examples/PotentiometerPID/src/Robot.cpp b/wpilibcExamples/src/main/cpp/examples/PotentiometerPID/src/Robot.cpp index dccad779e4..db4938cbb5 100644 --- a/wpilibcExamples/src/main/cpp/examples/PotentiometerPID/src/Robot.cpp +++ b/wpilibcExamples/src/main/cpp/examples/PotentiometerPID/src/Robot.cpp @@ -20,22 +20,22 @@ */ class Robot : public frc::IterativeRobot { public: - void RobotInit() override { pidController.SetInputRange(0, 5); } + void RobotInit() override { m_pidController.SetInputRange(0, 5); } - void TeleopInit() override { pidController.Enable(); } + void TeleopInit() override { m_pidController.Enable(); } void TeleopPeriodic() override { // when the button is pressed once, the selected elevator // setpoint // is incremented - bool currentButtonValue = joystick.GetTrigger(); - if (currentButtonValue && !previousButtonValue) { + bool currentButtonValue = m_joystick.GetTrigger(); + if (currentButtonValue && !m_previousButtonValue) { // index of the elevator setpoint wraps around. - index = (index + 1) % (sizeof(kSetPoints) / 8); + m_index = (m_index + 1) % (sizeof(kSetPoints) / 8); } - previousButtonValue = currentButtonValue; + m_previousButtonValue = currentButtonValue; - pidController.SetSetpoint(kSetPoints[index]); + m_pidController.SetSetpoint(kSetPoints[m_index]); } private: @@ -59,12 +59,12 @@ private: static constexpr double kI = -0.02; static constexpr double kD = -2.0; - int index = 0; - bool previousButtonValue = false; + int m_index = 0; + bool m_previousButtonValue = false; - frc::AnalogInput potentiometer{kPotChannel}; - frc::Joystick joystick{kJoystickChannel}; - frc::Spark elevatorMotor{kMotorChannel}; + frc::AnalogInput m_potentiometer{kPotChannel}; + frc::Joystick m_joystick{kJoystickChannel}; + frc::Spark m_elevatorMotor{kMotorChannel}; /* potentiometer (AnalogInput) and elevatorMotor (Victor) can be used as * a @@ -73,8 +73,8 @@ private: * to the PIDSource and PIDOutput, so you must use &potentiometer and * &elevatorMotor to get their pointers. */ - frc::PIDController pidController{ - kP, kI, kD, &potentiometer, &elevatorMotor}; + frc::PIDController m_pidController{ + kP, kI, kD, &m_potentiometer, &m_elevatorMotor}; }; constexpr std::array Robot::kSetPoints; diff --git a/wpilibcExamples/src/main/cpp/examples/Ultrasonic/src/Robot.cpp b/wpilibcExamples/src/main/cpp/examples/Ultrasonic/src/Robot.cpp index a81807aaba..a3e495a02f 100644 --- a/wpilibcExamples/src/main/cpp/examples/Ultrasonic/src/Robot.cpp +++ b/wpilibcExamples/src/main/cpp/examples/Ultrasonic/src/Robot.cpp @@ -6,8 +6,9 @@ /*----------------------------------------------------------------------------*/ #include +#include #include -#include +#include /** * This is a sample program demonstrating how to use an ultrasonic sensor and @@ -21,11 +22,12 @@ public: */ void TeleopPeriodic() override { // sensor returns a value from 0-4095 that is scaled to inches - double currentDistance = ultrasonic.GetValue() * kValueToInches; + double currentDistance = + m_ultrasonic.GetValue() * kValueToInches; // convert distance error to a motor speed double currentSpeed = (kHoldDistance - currentDistance) * kP; // drive robot - myRobot.Drive(currentSpeed, 0); + m_robotDrive.ArcadeDrive(currentSpeed, 0); } private: @@ -42,8 +44,11 @@ private: static constexpr int kRightMotorPort = 1; static constexpr int kUltrasonicPort = 0; - frc::AnalogInput ultrasonic{kUltrasonicPort}; - frc::RobotDrive myRobot{kLeftMotorPort, kRightMotorPort}; + frc::AnalogInput m_ultrasonic{kUltrasonicPort}; + + frc::Spark m_left{kLeftMotorPort}; + frc::Spark m_right{kRightMotorPort}; + frc::DifferentialDrive m_robotDrive{m_left, m_right}; }; START_ROBOT_CLASS(Robot) diff --git a/wpilibcExamples/src/main/cpp/examples/UltrasonicPID/src/Robot.cpp b/wpilibcExamples/src/main/cpp/examples/UltrasonicPID/src/Robot.cpp index 819066640c..550ba8539b 100644 --- a/wpilibcExamples/src/main/cpp/examples/UltrasonicPID/src/Robot.cpp +++ b/wpilibcExamples/src/main/cpp/examples/UltrasonicPID/src/Robot.cpp @@ -6,10 +6,11 @@ /*----------------------------------------------------------------------------*/ #include +#include #include #include #include -#include +#include /** * This is a sample program demonstrating how to use an ultrasonic sensor and @@ -26,29 +27,31 @@ public: // Set expected range to 0-24 inches; e.g. at 24 inches from // object go // full forward, at 0 inches from object go full backward. - pidController.SetInputRange(0, 24 * kValueToInches); + m_pidController.SetInputRange(0, 24 * kValueToInches); + // Set setpoint of the pidController - pidController.SetSetpoint(kHoldDistance * kValueToInches); - pidController.Enable(); // begin PID control + m_pidController.SetSetpoint(kHoldDistance * kValueToInches); + + // Begin PID control + m_pidController.Enable(); } private: - // internal class to write to myRobot (a RobotDrive object) using a - // PIDOutput + // Internal class to write to robot drive using a PIDOutput class MyPIDOutput : public frc::PIDOutput { public: - explicit MyPIDOutput(frc::RobotDrive& r) - : rd(r) { - rd.SetSafetyEnabled(false); + explicit MyPIDOutput(frc::DifferentialDrive& r) + : m_rd(r) { + m_rd.SetSafetyEnabled(false); } void PIDWrite(double output) override { - rd.Drive(output, 0); // write to myRobot (RobotDrive) - // by reference + // Write to robot drive by reference + m_rd.ArcadeDrive(output, 0); } private: - frc::RobotDrive& rd; + frc::DifferentialDrive& m_rd; }; // Distance in inches the robot wants to stay from an object @@ -70,10 +73,14 @@ private: static constexpr int kRightMotorPort = 1; static constexpr int kUltrasonicPort = 0; - frc::AnalogInput ultrasonic{kUltrasonicPort}; - frc::RobotDrive myRobot{kLeftMotorPort, kRightMotorPort}; - frc::PIDController pidController{ - kP, kI, kD, &ultrasonic, new MyPIDOutput(myRobot)}; + frc::AnalogInput m_ultrasonic{kUltrasonicPort}; + + frc::Spark m_left{kLeftMotorPort}; + frc::Spark m_right{kRightMotorPort}; + frc::DifferentialDrive m_robotDrive{m_left, m_right}; + + frc::PIDController m_pidController{kP, kI, kD, &m_ultrasonic, + new MyPIDOutput(m_robotDrive)}; }; START_ROBOT_CLASS(Robot) diff --git a/wpilibcExamples/src/main/cpp/templates/commandbased/Commands/ExampleCommand.cpp b/wpilibcExamples/src/main/cpp/templates/commandbased/Commands/ExampleCommand.cpp index 52a600b749..efbf96ef14 100644 --- a/wpilibcExamples/src/main/cpp/templates/commandbased/Commands/ExampleCommand.cpp +++ b/wpilibcExamples/src/main/cpp/templates/commandbased/Commands/ExampleCommand.cpp @@ -9,7 +9,7 @@ ExampleCommand::ExampleCommand() { // Use Requires() here to declare subsystem dependencies - // eg. Requires(Robot::chassis.get()); + // eg. Requires(&Robot::chassis); } // Called just before this Command runs the first time diff --git a/wpilibcExamples/src/main/cpp/templates/commandbased/Commands/MyAutoCommand.cpp b/wpilibcExamples/src/main/cpp/templates/commandbased/Commands/MyAutoCommand.cpp new file mode 100644 index 0000000000..71642826e8 --- /dev/null +++ b/wpilibcExamples/src/main/cpp/templates/commandbased/Commands/MyAutoCommand.cpp @@ -0,0 +1,31 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) 2017 FIRST. All Rights Reserved. */ +/* Open Source Software - may be modified and shared by FRC teams. The code */ +/* must be accompanied by the FIRST BSD license file in the root directory of */ +/* the project. */ +/*----------------------------------------------------------------------------*/ + +#include "MyAutoCommand.h" + +MyAutoCommand::MyAutoCommand() { + // Use Requires() here to declare subsystem dependencies + // eg. Requires(&Robot::chassis); +} + +// Called just before this Command runs the first time +void MyAutoCommand::Initialize() {} + +// Called repeatedly when this Command is scheduled to run +void MyAutoCommand::Execute() {} + +// Make this return true when this Command no longer needs to run execute() +bool MyAutoCommand::IsFinished() { + return false; +} + +// Called once after isFinished returns true +void MyAutoCommand::End() {} + +// Called when another command which requires one or more of the same +// subsystems is scheduled to run +void MyAutoCommand::Interrupted() {} diff --git a/wpilibcExamples/src/main/cpp/templates/commandbased/Commands/MyAutoCommand.h b/wpilibcExamples/src/main/cpp/templates/commandbased/Commands/MyAutoCommand.h new file mode 100644 index 0000000000..fa6d57dcf7 --- /dev/null +++ b/wpilibcExamples/src/main/cpp/templates/commandbased/Commands/MyAutoCommand.h @@ -0,0 +1,20 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) 2017 FIRST. All Rights Reserved. */ +/* Open Source Software - may be modified and shared by FRC teams. The code */ +/* must be accompanied by the FIRST BSD license file in the root directory of */ +/* the project. */ +/*----------------------------------------------------------------------------*/ + +#pragma once + +#include + +class MyAutoCommand : public frc::Command { +public: + MyAutoCommand(); + void Initialize() override; + void Execute() override; + bool IsFinished() override; + void End() override; + void Interrupted() override; +}; diff --git a/wpilibcExamples/src/main/cpp/templates/commandbased/Robot.cpp b/wpilibcExamples/src/main/cpp/templates/commandbased/Robot.cpp index 07ca4ad6e4..1c5ee1d0cc 100644 --- a/wpilibcExamples/src/main/cpp/templates/commandbased/Robot.cpp +++ b/wpilibcExamples/src/main/cpp/templates/commandbased/Robot.cpp @@ -5,8 +5,6 @@ /* the project. */ /*----------------------------------------------------------------------------*/ -#include - #include #include #include @@ -15,15 +13,14 @@ #include #include "Commands/ExampleCommand.h" +#include "Commands/MyAutoCommand.h" class Robot : public frc::IterativeRobot { public: void RobotInit() override { - defaultAuto.reset(new ExampleCommand()); - chooser.AddDefault("Default Auto", defaultAuto.get()); - // myAuto.reset(new MyAutoCommand()); - // chooser.AddObject("My Auto", myAuto.get()); - frc::SmartDashboard::PutData("Auto Modes", &chooser); + m_chooser.AddDefault("Default Auto", &m_defaultAuto); + m_chooser.AddObject("My Auto", &m_myAuto); + frc::SmartDashboard::PutData("Auto Modes", &m_chooser); } /** @@ -54,19 +51,18 @@ public: * to the if-else structure below with additional strings & commands. */ void AutonomousInit() override { - /* std::string autoSelected = - frc::SmartDashboard::GetString("Auto Selector", "Default"); + std::string autoSelected = frc::SmartDashboard::GetString( + "Auto Selector", "Default"); if (autoSelected == "My Auto") { - autonomousCommand.reset(new MyAutoCommand()); + m_autonomousCommand = &m_myAuto; + } else { + m_autonomousCommand = &m_defaultAuto; } - else { - autonomousCommand.reset(new ExampleCommand()); - } */ - autonomousCommand = chooser.GetSelected(); + m_autonomousCommand = m_chooser.GetSelected(); - if (autonomousCommand != nullptr) { - autonomousCommand->Start(); + if (m_autonomousCommand != nullptr) { + m_autonomousCommand->Start(); } } @@ -79,9 +75,9 @@ public: // teleop starts running. If you want the autonomous to // continue until interrupted by another command, remove // this line or comment it out. - if (autonomousCommand != nullptr) { - autonomousCommand->Cancel(); - autonomousCommand = nullptr; + if (m_autonomousCommand != nullptr) { + m_autonomousCommand->Cancel(); + m_autonomousCommand = nullptr; } } @@ -92,10 +88,10 @@ public: private: // Have it null by default so that if testing teleop it // doesn't have undefined behavior and potentially crash. - frc::Command* autonomousCommand = nullptr; - std::unique_ptr defaultAuto; - // std::unique_ptr myAuto; - frc::SendableChooser chooser; + frc::Command* m_autonomousCommand = nullptr; + ExampleCommand m_defaultAuto; + MyAutoCommand m_myAuto; + frc::SendableChooser m_chooser; }; START_ROBOT_CLASS(Robot) diff --git a/wpilibcExamples/src/main/cpp/templates/commandbased/RobotMap.h b/wpilibcExamples/src/main/cpp/templates/commandbased/RobotMap.h index c87578e966..e3e501acef 100644 --- a/wpilibcExamples/src/main/cpp/templates/commandbased/RobotMap.h +++ b/wpilibcExamples/src/main/cpp/templates/commandbased/RobotMap.h @@ -16,10 +16,10 @@ // For example to map the left and right motors, you could define the // following variables to use with your drivetrain subsystem. -// constexpr int LEFTMOTOR = 1; -// constexpr int RIGHTMOTOR = 2; +// constexpr int kLeftMotor = 1; +// constexpr int kRightMotor = 2; // If you are using multiple modules, make sure to define both the port // number and the module. For example you with a rangefinder: -// constexpr int RANGE_FINDER_PORT = 1; -// constexpr int RANGE_FINDER_MODULE = 1; +// constexpr int kRangeFinderPort = 1; +// constexpr int kRangeFinderModule = 1; diff --git a/wpilibcExamples/src/main/cpp/templates/iterative/Robot.cpp b/wpilibcExamples/src/main/cpp/templates/iterative/Robot.cpp index a4f4b658cb..688de7845a 100644 --- a/wpilibcExamples/src/main/cpp/templates/iterative/Robot.cpp +++ b/wpilibcExamples/src/main/cpp/templates/iterative/Robot.cpp @@ -6,7 +6,6 @@ /*----------------------------------------------------------------------------*/ #include -#include #include #include @@ -17,9 +16,9 @@ class Robot : public frc::IterativeRobot { public: void RobotInit() { - chooser.AddDefault(autoNameDefault, autoNameDefault); - chooser.AddObject(autoNameCustom, autoNameCustom); - frc::SmartDashboard::PutData("Auto Modes", &chooser); + m_chooser.AddDefault(kAutoNameDefault, kAutoNameDefault); + m_chooser.AddObject(kAutoNameCustom, kAutoNameCustom); + frc::SmartDashboard::PutData("Auto Modes", &m_chooser); } /* @@ -37,12 +36,12 @@ public: * well. */ void AutonomousInit() override { - autoSelected = chooser.GetSelected(); - // std::string autoSelected = SmartDashboard::GetString("Auto - // Selector", autoNameDefault); - std::cout << "Auto selected: " << autoSelected << std::endl; + m_autoSelected = m_chooser.GetSelected(); + // m_autoSelected = SmartDashboard::GetString( + // "Auto Selector", kAutoNameDefault); + std::cout << "Auto selected: " << m_autoSelected << std::endl; - if (autoSelected == autoNameCustom) { + if (m_autoSelected == kAutoNameCustom) { // Custom Auto goes here } else { // Default Auto goes here @@ -50,7 +49,7 @@ public: } void AutonomousPeriodic() { - if (autoSelected == autoNameCustom) { + if (m_autoSelected == kAutoNameCustom) { // Custom Auto goes here } else { // Default Auto goes here @@ -61,14 +60,14 @@ public: void TeleopPeriodic() {} - void TestPeriodic() { lw->Run(); } + void TestPeriodic() { m_lw.Run(); } private: - frc::LiveWindow* lw = LiveWindow::GetInstance(); - frc::SendableChooser chooser; - const std::string autoNameDefault = "Default"; - const std::string autoNameCustom = "My Auto"; - std::string autoSelected; + frc::LiveWindow& m_lw = *LiveWindow::GetInstance(); + frc::SendableChooser m_chooser; + const std::string kAutoNameDefault = "Default"; + const std::string kAutoNameCustom = "My Auto"; + std::string m_autoSelected; }; START_ROBOT_CLASS(Robot) diff --git a/wpilibcExamples/src/main/cpp/templates/sample/Robot.cpp b/wpilibcExamples/src/main/cpp/templates/sample/Robot.cpp index 32185fc4f4..ca5c7fe0bb 100644 --- a/wpilibcExamples/src/main/cpp/templates/sample/Robot.cpp +++ b/wpilibcExamples/src/main/cpp/templates/sample/Robot.cpp @@ -6,14 +6,14 @@ /*----------------------------------------------------------------------------*/ #include -#include #include +#include #include -#include #include #include #include +#include #include /** @@ -29,23 +29,17 @@ * instead if you're new. */ class Robot : public frc::SampleRobot { - frc::RobotDrive myRobot{0, 1}; // robot drive system - frc::Joystick stick{0}; // only joystick - frc::SendableChooser chooser; - const std::string autoNameDefault = "Default"; - const std::string autoNameCustom = "My Auto"; - public: Robot() { // Note SmartDashboard is not initialized here, wait until // RobotInit to make SmartDashboard calls - myRobot.SetExpiration(0.1); + m_robotDrive.SetExpiration(0.1); } void RobotInit() { - chooser.AddDefault(autoNameDefault, autoNameDefault); - chooser.AddObject(autoNameCustom, autoNameCustom); - frc::SmartDashboard::PutData("Auto Modes", &chooser); + m_chooser.AddDefault(kAutoNameDefault, kAutoNameDefault); + m_chooser.AddObject(kAutoNameCustom, kAutoNameCustom); + frc::SmartDashboard::PutData("Auto Modes", &m_chooser); } /* @@ -63,26 +57,32 @@ public: * well. */ void Autonomous() { - auto autoSelected = chooser.GetSelected(); - // std::string autoSelected = - // frc::SmartDashboard::GetString("Auto Selector", - // autoNameDefault); + std::string autoSelected = frc::SmartDashboard::GetString( + "Auto Selector", kAutoNameDefault); std::cout << "Auto selected: " << autoSelected << std::endl; - if (autoSelected == autoNameCustom) { + if (autoSelected == kAutoNameCustom) { // Custom Auto goes here std::cout << "Running custom Autonomous" << std::endl; - myRobot.SetSafetyEnabled(false); - myRobot.Drive(-0.5, 1.0); // spin at half speed - frc::Wait(2.0); // for 2 seconds - myRobot.Drive(0.0, 0.0); // stop robot + m_robotDrive.SetSafetyEnabled(false); + + // spin at half speed for two seconds + m_robotDrive.ArcadeDrive(0.0, 1.0); + frc::Wait(2.0); + + // stop robot + m_robotDrive.ArcadeDrive(0.0, 0.0); } else { // Default Auto goes here std::cout << "Running default Autonomous" << std::endl; - myRobot.SetSafetyEnabled(false); - myRobot.Drive(-0.5, 0.0); // drive forwards half speed - frc::Wait(2.0); // for 2 seconds - myRobot.Drive(0.0, 0.0); // stop robot + m_robotDrive.SetSafetyEnabled(false); + + // drive forwards at half speed for two seconds + m_robotDrive.ArcadeDrive(-0.5, 0.0); + frc::Wait(2.0); + + // stop robot + m_robotDrive.ArcadeDrive(0.0, 0.0); } } @@ -90,10 +90,11 @@ public: * Runs the motors with arcade steering. */ void OperatorControl() override { - myRobot.SetSafetyEnabled(true); + m_robotDrive.SetSafetyEnabled(true); while (IsOperatorControl() && IsEnabled()) { // drive with arcade style (use right stick) - myRobot.ArcadeDrive(stick); + m_robotDrive.ArcadeDrive( + m_stick.GetY(), m_stick.GetX()); // wait for a motor update time frc::Wait(0.005); @@ -104,6 +105,18 @@ public: * Runs during test mode */ void Test() override {} + +private: + // Robot drive system + frc::Spark m_leftMotor{0}; + frc::Spark m_rightMotor{1}; + frc::DifferentialDrive m_robotDrive{m_leftMotor, m_rightMotor}; + + frc::Joystick m_stick{0}; + + frc::SendableChooser m_chooser; + const std::string kAutoNameDefault = "Default"; + const std::string kAutoNameCustom = "My Auto"; }; START_ROBOT_CLASS(Robot)