Fixed examples to build/run with new WPILib versions.

Also added some references/smart pointers to a couple places
that seemed convenient to the user.

I haven't updated the constructors for RobotDrive() related
examples, pending the results of gerrit change https://usfirst.collab.net/gerrit/#/c/960/

A few things that we are noticing:
--It might be nice if ReturnPIDInput() didn't have to be const;
  when people try to override it, they have to remember to put
  the const in and if they don't, then the compiler error isn't the
  most obvious (especially since this is a change). This would also
  apply to PIDGet() in the PIDSource interface.
--SendableChooser still takes raw pointers. This could lead to an
  issue I had to debug briefly where you accidentally call
  GetSelected() on autoChooser and put the resulting raw pointer
  into a unique_ptr, which destroys the pointer when it goes out of
  scope. Specifically, I was testing the PacGoat example and
  I ended up with a situation where if auto mode was run once, it
  was fine, but if it was run twice, the selected command would
  have been destroyed by the unique_ptr. I believe that this
  just requires updating SendableChosser to take shared_ptr.
--When the samples are compiled with -pedantic, it points out that
  START_ROBOT_CLASS macro expansion results in a redundant semicolon.

Change-Id: Ib4c025a61263d0d2780d4253faa31713e15333a5
This commit is contained in:
James Kuszmaul
2015-07-24 19:19:40 -04:00
committed by Brad Miller (WPI)
parent 4f8c1dff2f
commit e017f93f16
76 changed files with 411 additions and 424 deletions

View File

@@ -3,7 +3,7 @@
#include "Robot.h"
CloseClaw::CloseClaw() {
Requires(Robot::collector);
Requires(Robot::collector.get());
}
// Called just before this Command runs the first time

View File

@@ -3,7 +3,7 @@
#include "Robot.h"
void DriveForward::init(double dist, double maxSpeed) {
Requires(Robot::drivetrain);
Requires(Robot::drivetrain.get());
distance = dist;
driveForwardSpeed = maxSpeed;
error = 0;

View File

@@ -3,7 +3,7 @@
#include "Robot.h"
DriveWithJoystick::DriveWithJoystick() {
Requires(Robot::drivetrain);
Requires(Robot::drivetrain.get());
}
// Called just before this Command runs the first time

View File

@@ -3,7 +3,7 @@
#include "Robot.h"
ExtendShooter::ExtendShooter() {
Requires(Robot::shooter);
Requires(Robot::shooter.get());
SetTimeout(1);
}

View File

@@ -3,7 +3,7 @@
#include "Robot.h"
OpenClaw::OpenClaw() {
Requires(Robot::collector);
Requires(Robot::collector.get());
}
// Called just before this Command runs the first time

View File

@@ -3,7 +3,7 @@
#include "Robot.h"
SetCollectionSpeed::SetCollectionSpeed(double speed) {
Requires(Robot::collector);
Requires(Robot::collector.get());
this->speed = speed;
}

View File

@@ -4,7 +4,7 @@
SetPivotSetpoint::SetPivotSetpoint(double setpoint) {
this->setpoint = setpoint;
Requires(Robot::pivot);
Requires(Robot::pivot.get());
}
// Called just before this Command runs the first time

View File

@@ -3,7 +3,7 @@
#include "Robot.h"
WaitForBall::WaitForBall() {
Requires(Robot::collector);
Requires(Robot::collector.get());
}
// Called just before this Command runs the first time

View File

@@ -4,7 +4,7 @@
WaitForPressure::WaitForPressure()
{
Requires(Robot::pneumatics);
Requires(Robot::pneumatics.get());
}
// Called just before this Command runs the first time

View File

@@ -9,21 +9,21 @@
#include "Commands/DriveForward.h"
#include "Commands/SetCollectionSpeed.h"
OI::OI() {
joystick = new Joystick(0);
OI::OI()
: joystick(0),
L1(&joystick, 11),
L2(&joystick, 9),
R1(&joystick, 12),
R2(&joystick, 10),
sticks(&joystick, 2, 3) {
R1 = new JoystickButton(joystick, 12);
R1->WhenPressed(new LowGoal());
R2 = new JoystickButton(joystick, 10);
R2->WhenPressed(new Collect());
R1.WhenPressed(new LowGoal());
R2.WhenPressed(new Collect());
L1 = new JoystickButton(joystick, 11);
L1->WhenPressed(new SetPivotSetpoint(Pivot::SHOOT));
L2 = new JoystickButton(joystick, 9);
L2->WhenPressed(new SetPivotSetpoint(Pivot::SHOOT_NEAR));
L1.WhenPressed(new SetPivotSetpoint(Pivot::SHOOT));
L2.WhenPressed(new SetPivotSetpoint(Pivot::SHOOT_NEAR));
sticks = new DoubleButton(joystick, 2, 3);
sticks->WhenActive(new Shoot());
sticks.WhenActive(new Shoot());
// SmartDashboard Buttons
@@ -36,5 +36,5 @@ OI::OI() {
Joystick* OI::GetJoystick() {
return joystick;
return &joystick;
}

View File

@@ -6,9 +6,9 @@
class OI {
private:
Joystick* joystick;
JoystickButton *L1, *L2, *R1, *R2;
DoubleButton* sticks;
Joystick joystick;
JoystickButton L1, L2, R1, R2;
DoubleButton sticks;
public:
OI();
Joystick* GetJoystick();

View File

@@ -1,46 +1,40 @@
#include "Robot.h"
#include "Commands/DriveAndShootAutonomous.h"
#include "Commands/DriveForward.h"
DriveTrain* Robot::drivetrain = NULL;
Pivot* Robot::pivot = NULL;
Collector* Robot::collector = NULL;
Shooter* Robot::shooter = NULL;
Pneumatics* Robot::pneumatics = NULL;
std::shared_ptr<DriveTrain> Robot::drivetrain;
std::shared_ptr<Pivot> Robot::pivot;
std::shared_ptr<Collector> Robot::collector;
std::shared_ptr<Shooter> Robot::shooter;
std::shared_ptr<Pneumatics> Robot::pneumatics;
OI* Robot::oi = NULL;
std::unique_ptr<OI> Robot::oi;
void Robot::RobotInit() {
drivetrain = new DriveTrain();
pivot = new Pivot();
collector = new Collector();
shooter = new Shooter();
pneumatics = new Pneumatics();
drivetrain.reset(new DriveTrain());
pivot.reset(new Pivot());
collector.reset(new Collector());
shooter.reset(new Shooter());
pneumatics.reset(new Pneumatics());
// Show what command your subsystem is running on the SmartDashboard
SmartDashboard::PutData(drivetrain);
SmartDashboard::PutData(pivot);
SmartDashboard::PutData(collector);
SmartDashboard::PutData(shooter);
SmartDashboard::PutData(pneumatics);
oi.reset(new OI());
oi = new OI();
autonomousCommand = new DriveAndShootAutonomous();
lw = LiveWindow::GetInstance();
// Show what command your subsystem is running on the SmartDashboard
SmartDashboard::PutData(drivetrain.get());
SmartDashboard::PutData(pivot.get());
SmartDashboard::PutData(collector.get());
SmartDashboard::PutData(shooter.get());
SmartDashboard::PutData(pneumatics.get());
// instantiate the command used for the autonomous period
autoChooser = new SendableChooser();
autoChooser->AddDefault("Drive and Shoot", new DriveAndShootAutonomous());
autoChooser->AddObject("Drive Forward", new DriveForward());
SmartDashboard::PutData("Auto Mode", autoChooser);
autoChooser.AddDefault("Drive and Shoot", driveAndShootAuto.get());
autoChooser.AddObject("Drive Forward", driveForwardAuto.get());
SmartDashboard::PutData("Auto Mode", &autoChooser);
pneumatics->Start(); // Pressurize the pneumatics.
}
void Robot::AutonomousInit() {
autonomousCommand = (Command*) autoChooser->GetSelected();
autonomousCommand = (Command *)autoChooser.GetSelected();
autonomousCommand->Start();
}
@@ -54,7 +48,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.
if (autonomousCommand != NULL) {
if (autonomousCommand != nullptr) {
autonomousCommand->Cancel();
}
std::cout << "Starting Teleop" << std::endl;
@@ -66,7 +60,7 @@ void Robot::TeleopPeriodic() {
}
void Robot::TestPeriodic() {
lw->Run();
LiveWindow::GetInstance().Run();
}
void Robot::DisabledInit() {
@@ -87,4 +81,4 @@ void Robot::Log() {
SmartDashboard::PutNumber("Right Distance", drivetrain->GetRightEncoder()->GetDistance());
}
START_ROBOT_CLASS(Robot);
START_ROBOT_CLASS(Robot)

View File

@@ -11,6 +11,8 @@
#include "WPILib.h"
#include "Commands/Command.h"
#include "Commands/DriveAndShootAutonomous.h"
#include "Commands/DriveForward.h"
#include "Subsystems/DriveTrain.h"
#include "Subsystems/Pivot.h"
#include "Subsystems/Collector.h"
@@ -20,17 +22,18 @@
class Robot: public IterativeRobot {
public:
static DriveTrain* drivetrain;
static Pivot* pivot;
static Collector* collector;
static Shooter* shooter;
static Pneumatics* pneumatics;
static OI* oi;
static std::shared_ptr<DriveTrain> drivetrain;
static std::shared_ptr<Pivot> pivot;
static std::shared_ptr<Collector> collector;
static std::shared_ptr<Shooter> shooter;
static std::shared_ptr<Pneumatics> pneumatics;
static std::unique_ptr<OI> oi;
private:
Command *autonomousCommand;
LiveWindow *lw;
SendableChooser* autoChooser;
Command* autonomousCommand = nullptr;
std::unique_ptr<Command> driveAndShootAuto{new DriveAndShootAutonomous()},
driveForwardAuto{new DriveForward()};
SendableChooser autoChooser;
void RobotInit();
void AutonomousInit();
@@ -44,5 +47,4 @@ private:
void Log();
};
#endif /* ROBOT_H_ */

View File

@@ -1,19 +1,19 @@
#include "Collector.h"
Collector::Collector() :
Subsystem("Collector")
Subsystem("Collector"),
// Configure devices
rollerMotor(new Victor(6)),
ballDetector(new DigitalInput(10)),
piston(new Solenoid(1)),
openDetector(new DigitalInput(6))
{
// Configure devices
rollerMotor = new Victor(6);
ballDetector = new DigitalInput(10);
openDetector = new DigitalInput(6);
piston = new Solenoid(1);
// Put everything to the LiveWindow for testing.
// XXX: LiveWindow::GetInstance()->AddActuator("Collector", "Roller Motor", (Victor) rollerMotor);
LiveWindow::GetInstance()->AddSensor("Collector", "Ball Detector", ballDetector);
LiveWindow::GetInstance()->AddSensor("Collector", "Claw Open Detector", openDetector);
LiveWindow::GetInstance()->AddActuator("Collector", "Piston", piston);
LiveWindow::GetInstance().AddSensor("Collector", "Ball Detector", ballDetector);
LiveWindow::GetInstance().AddSensor("Collector", "Claw Open Detector", openDetector);
LiveWindow::GetInstance().AddActuator("Collector", "Piston", piston);
}
bool Collector::HasBall() {

View File

@@ -19,10 +19,10 @@ public:
private:
// Subsystem devices
SpeedController* rollerMotor;
DigitalInput* ballDetector;
Solenoid* piston;
DigitalInput* openDetector;
std::shared_ptr<SpeedController> rollerMotor;
std::shared_ptr<DigitalInput> ballDetector;
std::shared_ptr<Solenoid> piston;
std::shared_ptr<DigitalInput> openDetector;
public:
Collector();

View File

@@ -3,13 +3,17 @@
#define _USE_MATH_DEFINES
#include <math.h>
DriveTrain::DriveTrain() :
Subsystem("DriveTrain") {
// Configure drive motors
frontLeftCIM = new Victor(1);
frontRightCIM = new Victor(2);
backLeftCIM = new Victor(3);
backRightCIM = new Victor(4);
DriveTrain::DriveTrain()
: Subsystem("DriveTrain"),
// Configure drive motors
frontLeftCIM(new Victor(1)),
frontRightCIM(new Victor(2)),
backLeftCIM(new Victor(3)),
backRightCIM(new Victor(4)),
drive(frontRightCIM, backLeftCIM, frontRightCIM, backRightCIM),
rightEncoder(new Encoder(1, 2, true, Encoder::k4X)),
leftEncoder(new Encoder(3, 4, false, Encoder::k4X)),
gyro(new Gyro(0)) {
// XXX: LiveWindow::GetInstance()->AddActuator("DriveTrain", "Front Left CIM", (Victor) frontLeftCIM);
// XXX: LiveWindow::GetInstance()->AddActuator("DriveTrain", "Front Right CIM", (Victor) frontRightCIM);
// XXX: LiveWindow::GetInstance()->AddActuator("DriveTrain", "Back Left CIM", (Victor) backLeftCIM);
@@ -17,19 +21,16 @@ DriveTrain::DriveTrain() :
// Configure the RobotDrive to reflect the fact that all our motors are
// wired backwards and our drivers sensitivity preferences.
drive = new RobotDrive(frontLeftCIM, backLeftCIM, frontRightCIM, backRightCIM);
drive->SetSafetyEnabled(true);
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);
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);
// Configure encoders
rightEncoder = new Encoder(1, 2, true, Encoder::k4X);
leftEncoder = new Encoder(3, 4, false, Encoder::k4X);
rightEncoder->SetPIDSourceParameter(PIDSource::kDistance);
leftEncoder->SetPIDSourceParameter(PIDSource::kDistance);
@@ -43,15 +44,14 @@ DriveTrain::DriveTrain() :
leftEncoder->SetDistancePerPulse((4.0/*in*/*M_PI)/(360.0*12.0/*in/ft*/));
#endif
LiveWindow::GetInstance()->AddSensor("DriveTrain", "Right Encoder", rightEncoder);
LiveWindow::GetInstance()->AddSensor("DriveTrain", "Left Encoder", leftEncoder);
LiveWindow::GetInstance().AddSensor("DriveTrain", "Right Encoder", rightEncoder);
LiveWindow::GetInstance().AddSensor("DriveTrain", "Left Encoder", leftEncoder);
// Configure gyro
gyro = new Gyro(2);
#ifdef REAL
gyro->SetSensitivity(0.007); // TODO: Handle more gracefully?
#endif
LiveWindow::GetInstance()->AddSensor("DriveTrain", "Gyro", gyro);
LiveWindow::GetInstance().AddSensor("DriveTrain", "Gyro", gyro);
}
void DriveTrain::InitDefaultCommand() {
@@ -59,22 +59,22 @@ void DriveTrain::InitDefaultCommand() {
}
void DriveTrain::TankDrive(Joystick* joy) {
drive->TankDrive(joy->GetY(), joy->GetRawAxis(4));
drive.TankDrive(joy->GetY(), joy->GetRawAxis(4));
}
void DriveTrain::TankDrive(double leftAxis, double rightAxis) {
drive->TankDrive(leftAxis, rightAxis);
drive.TankDrive(leftAxis, rightAxis);
}
void DriveTrain::Stop() {
drive->TankDrive(0.0, 0.0);
drive.TankDrive(0.0, 0.0);
}
Encoder* DriveTrain::GetLeftEncoder() {
std::shared_ptr<Encoder> DriveTrain::GetLeftEncoder() {
return leftEncoder;
}
Encoder* DriveTrain::GetRightEncoder() {
std::shared_ptr<Encoder> DriveTrain::GetRightEncoder() {
return rightEncoder;
}

View File

@@ -12,11 +12,11 @@ class DriveTrain: public Subsystem
{
private:
// Subsystem devices
SpeedController *frontLeftCIM, *frontRightCIM;
SpeedController *backLeftCIM, *backRightCIM;
RobotDrive* drive;
Encoder *rightEncoder, *leftEncoder;
Gyro* gyro;
std::shared_ptr<SpeedController> frontLeftCIM, frontRightCIM;
std::shared_ptr<SpeedController> backLeftCIM, backRightCIM;
RobotDrive drive;
std::shared_ptr<Encoder> rightEncoder, leftEncoder;
std::shared_ptr<Gyro> gyro;
public:
DriveTrain();
@@ -46,12 +46,12 @@ public:
/**
* @return The encoder getting the distance and speed of left side of the drivetrain.
*/
Encoder* GetLeftEncoder();
std::shared_ptr<Encoder> GetLeftEncoder();
/**
* @return The encoder getting the distance and speed of right side of the drivetrain.
*/
Encoder* GetRightEncoder();
std::shared_ptr<Encoder> GetRightEncoder();
/**
* @return The current angle of the drivetrain.

View File

@@ -1,7 +1,15 @@
#include "Pivot.h"
Pivot::Pivot() :
PIDSubsystem("Pivot", 7.0, 0.0, 8.0)
PIDSubsystem("Pivot", 7.0, 0.0, 8.0),
// Sensors for measuring the position of the pivot.
upperLimitSwitch(new DigitalInput(13)),
lowerLimitSwitch(new DigitalInput(12)),
// 0 degrees is vertical facing up.
// Angle increases the more forward the pivot goes.
pot(new AnalogPotentiometer(1)),
// Motor to move the pivot.
motor(new Victor(5))
{
SetAbsoluteTolerance(0.005);
GetPIDController()->SetContinuous(false);
@@ -11,28 +19,17 @@ Pivot::Pivot() :
SetAbsoluteTolerance(5);
#endif
// Motor to move the pivot.
motor = new Victor(5);
// Sensors for measuring the position of the pivot.
upperLimitSwitch = new DigitalInput(13);
lowerLimitSwitch = new DigitalInput(12);
// 0 degrees is vertical facing up.
// Angle increases the more forward the pivot goes.
pot = new AnalogPotentiometer(1);
// Put everything to the LiveWindow for testing.
LiveWindow::GetInstance()->AddSensor("Pivot", "Upper Limit Switch", upperLimitSwitch);
LiveWindow::GetInstance()->AddSensor("Pivot", "Lower Limit Switch", lowerLimitSwitch);
// XXX: LiveWindow::GetInstance()->AddSensor("Pivot", "Pot", (AnalogPotentiometer) pot);
// XXX: LiveWindow::GetInstance()->AddActuator("Pivot", "Motor", (Victor) motor);
LiveWindow::GetInstance()->AddActuator("Pivot", "PIDSubsystem Controller", GetPIDController());
LiveWindow::GetInstance().AddSensor("Pivot", "Upper Limit Switch", upperLimitSwitch);
LiveWindow::GetInstance().AddSensor("Pivot", "Lower Limit Switch", lowerLimitSwitch);
// XXX: LiveWindow::GetInstance().AddSensor("Pivot", "Pot", (AnalogPotentiometer) pot);
// XXX: LiveWindow::GetInstance().AddActuator("Pivot", "Motor", (Victor) motor);
LiveWindow::GetInstance().AddActuator("Pivot", "PIDSubsystem Controller", GetPIDController());
}
void InitDefaultCommand() {}
double Pivot::ReturnPIDInput() {
double Pivot::ReturnPIDInput() const {
return pot->Get();
}

View File

@@ -19,10 +19,10 @@ public:
private:
// Subsystem devices
DigitalInput* upperLimitSwitch;
DigitalInput* lowerLimitSwitch;
Potentiometer* pot;
SpeedController* motor;
std::shared_ptr<DigitalInput> upperLimitSwitch;
std::shared_ptr<DigitalInput> lowerLimitSwitch;
std::shared_ptr<Potentiometer> pot;
std::shared_ptr<SpeedController> motor;
public:
Pivot();
@@ -35,7 +35,7 @@ public:
/**
* @return The angle read in by the potentiometer
*/
double ReturnPIDInput();
double ReturnPIDInput() const;
/**
* Set the motor speed based off of the PID output

View File

@@ -1,14 +1,14 @@
#include "Pneumatics.h"
Pneumatics::Pneumatics() :
Subsystem("Pneumatics")
Subsystem("Pneumatics"),
pressureSensor(new AnalogInput(3))
{
pressureSensor = new AnalogInput(3);
#ifdef REAL
compressor = new Compressor(uint8_t(1)); // TODO: (1, 14, 1, 8);
#endif
LiveWindow::GetInstance()->AddSensor("Pneumatics", "Pressure Sensor", pressureSensor);
LiveWindow::GetInstance().AddSensor("Pneumatics", "Pressure Sensor", pressureSensor);
}
/**

View File

@@ -12,7 +12,7 @@
class Pneumatics: public Subsystem
{
private:
AnalogInput* pressureSensor;
std::shared_ptr<AnalogInput> pressureSensor;
#ifdef REAL
Compressor* compressor;
#endif

View File

@@ -1,21 +1,21 @@
#include "Shooter.h"
Shooter::Shooter() :
Subsystem("Shooter")
Subsystem("Shooter"),
// Configure Devices
piston1(new DoubleSolenoid(3, 4)),
piston2(new DoubleSolenoid(5, 6)),
latchPiston(new Solenoid(1, 2)),
piston1ReedSwitchFront(new DigitalInput(9)),
piston1ReedSwitchBack(new DigitalInput(11)),
hotGoalSensor(new DigitalInput(7))
{
// Configure Devices
hotGoalSensor = new DigitalInput(3);
piston1 = new DoubleSolenoid(3, 4);
piston2 = new DoubleSolenoid(5, 6);
latchPiston = new Solenoid(1, 2);
piston1ReedSwitchFront = new DigitalInput(9);
piston1ReedSwitchBack = new DigitalInput(11);
// Put everything to the LiveWindow for testing.
LiveWindow::GetInstance()->AddSensor("Shooter", "Hot Goal Sensor", hotGoalSensor);
LiveWindow::GetInstance()->AddSensor("Shooter", "Piston1 Reed Switch Front ", piston1ReedSwitchFront);
LiveWindow::GetInstance()->AddSensor("Shooter", "Piston1 Reed Switch Back ", piston1ReedSwitchBack);
LiveWindow::GetInstance()->AddActuator("Shooter", "Latch Piston", latchPiston);
LiveWindow::GetInstance().AddSensor("Shooter", "Hot Goal Sensor", hotGoalSensor);
LiveWindow::GetInstance().AddSensor("Shooter", "Piston1 Reed Switch Front ", piston1ReedSwitchFront);
LiveWindow::GetInstance().AddSensor("Shooter", "Piston1 Reed Switch Back ", piston1ReedSwitchBack);
LiveWindow::GetInstance().AddActuator("Shooter", "Latch Piston", latchPiston);
}
void Shooter::InitDefaultCommand()

View File

@@ -18,12 +18,12 @@ class Shooter: public Subsystem
{
private:
// Devices
DoubleSolenoid* piston1;
DoubleSolenoid* piston2;
Solenoid* latchPiston;
DigitalInput* piston1ReedSwitchFront;
DigitalInput* piston1ReedSwitchBack;
DigitalInput* hotGoalSensor; // NOTE: Currently ignored in simulation
std::shared_ptr<DoubleSolenoid> piston1;
std::shared_ptr<DoubleSolenoid> piston2;
std::shared_ptr<Solenoid> latchPiston;
std::shared_ptr<DigitalInput> piston1ReedSwitchFront;
std::shared_ptr<DigitalInput> piston1ReedSwitchBack;
std::shared_ptr<DigitalInput> hotGoalSensor; // NOTE: Currently ignored in simulation
public:
Shooter();