mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-20 00:51:42 +00:00
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:
committed by
Brad Miller (WPI)
parent
4f8c1dff2f
commit
e017f93f16
@@ -2,7 +2,7 @@
|
||||
#include "Robot.h"
|
||||
|
||||
CloseClaw::CloseClaw() : Command("CloseClaw") {
|
||||
Requires(Robot::claw);
|
||||
Requires(Robot::claw.get());
|
||||
}
|
||||
|
||||
// Called just before this Command runs the first time
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
#include "Robot.h"
|
||||
|
||||
DriveStraight::DriveStraight(double distance) {
|
||||
Requires(Robot::drivetrain);
|
||||
Requires(Robot::drivetrain.get());
|
||||
pid = new PIDController(4, 0, 0, new DriveStraightPIDSource(),
|
||||
new DriveStraightPIDOutput());
|
||||
pid->SetAbsoluteTolerance(0.01);
|
||||
@@ -40,7 +40,7 @@ void DriveStraight::Interrupted() {
|
||||
|
||||
|
||||
DriveStraightPIDSource::~DriveStraightPIDSource() {}
|
||||
double DriveStraightPIDSource::PIDGet() {
|
||||
double DriveStraightPIDSource::PIDGet() const {
|
||||
return Robot::drivetrain->GetDistance();
|
||||
}
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ private:
|
||||
class DriveStraightPIDSource: public PIDSource {
|
||||
public:
|
||||
virtual ~DriveStraightPIDSource();
|
||||
double PIDGet();
|
||||
double PIDGet() const;
|
||||
};
|
||||
|
||||
class DriveStraightPIDOutput: public PIDOutput {
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
#include "Robot.h"
|
||||
|
||||
OpenClaw::OpenClaw() : Command("OpenClaw") {
|
||||
Requires(Robot::claw);
|
||||
Requires(Robot::claw.get());
|
||||
SetTimeout(1);
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
#include "Robot.h"
|
||||
|
||||
SetDistanceToBox::SetDistanceToBox(double distance) {
|
||||
Requires(Robot::drivetrain);
|
||||
Requires(Robot::drivetrain.get());
|
||||
pid = new PIDController(-2, 0, 0, new SetDistanceToBoxPIDSource(),
|
||||
new SetDistanceToBoxPIDOutput());
|
||||
pid->SetAbsoluteTolerance(0.01);
|
||||
@@ -40,7 +40,7 @@ void SetDistanceToBox::Interrupted() {
|
||||
|
||||
|
||||
SetDistanceToBoxPIDSource::~SetDistanceToBoxPIDSource() {}
|
||||
double SetDistanceToBoxPIDSource::PIDGet() {
|
||||
double SetDistanceToBoxPIDSource::PIDGet() const {
|
||||
return Robot::drivetrain->GetDistanceToObstacle();
|
||||
}
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ private:
|
||||
class SetDistanceToBoxPIDSource: public PIDSource {
|
||||
public:
|
||||
virtual ~SetDistanceToBoxPIDSource();
|
||||
double PIDGet();
|
||||
double PIDGet() const;
|
||||
};
|
||||
|
||||
class SetDistanceToBoxPIDOutput: public PIDOutput {
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
SetElevatorSetpoint::SetElevatorSetpoint(double setpoint) : Command("SetElevatorSetpoint") {
|
||||
this->setpoint = setpoint;
|
||||
Requires(Robot::elevator);
|
||||
Requires(Robot::elevator.get());
|
||||
}
|
||||
|
||||
// Called just before this Command runs the first time
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
SetWristSetpoint::SetWristSetpoint(double setpoint) : Command("SetWristSetpoint") {
|
||||
this->setpoint = setpoint;
|
||||
Requires(Robot::wrist);
|
||||
Requires(Robot::wrist.get());
|
||||
}
|
||||
|
||||
// Called just before this Command runs the first time
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
#include "Robot.h"
|
||||
|
||||
TankDriveWithJoystick::TankDriveWithJoystick() : Command("TankDriveWithJoystick") {
|
||||
Requires(Robot::drivetrain);
|
||||
Requires(Robot::drivetrain.get());
|
||||
}
|
||||
|
||||
// Called just before this Command runs the first time
|
||||
|
||||
@@ -1,34 +1,30 @@
|
||||
|
||||
#include "Robot.h"
|
||||
#include "Commands/Autonomous.h"
|
||||
|
||||
DriveTrain* Robot::drivetrain = NULL;
|
||||
Elevator* Robot::elevator = NULL;
|
||||
Wrist* Robot::wrist = NULL;
|
||||
Claw* Robot::claw = NULL;
|
||||
std::shared_ptr<DriveTrain> Robot::drivetrain;
|
||||
std::shared_ptr<Elevator> Robot::elevator;
|
||||
std::shared_ptr<Wrist> Robot::wrist;
|
||||
std::shared_ptr<Claw> Robot::claw;
|
||||
|
||||
OI* Robot::oi = NULL;
|
||||
std::unique_ptr<OI> Robot::oi;
|
||||
|
||||
void Robot::RobotInit() {
|
||||
drivetrain = new DriveTrain();
|
||||
elevator = new Elevator();
|
||||
wrist = new Wrist();
|
||||
claw = new Claw();
|
||||
drivetrain.reset(new DriveTrain());
|
||||
elevator.reset(new Elevator());
|
||||
wrist.reset(new Wrist());
|
||||
claw.reset(new Claw());
|
||||
|
||||
oi = new OI();
|
||||
oi.reset(new OI());
|
||||
|
||||
autonomousCommand = new Autonomous();
|
||||
lw = LiveWindow::GetInstance();
|
||||
|
||||
// Show what command your subsystem is running on the SmartDashboard
|
||||
SmartDashboard::PutData(drivetrain);
|
||||
SmartDashboard::PutData(elevator);
|
||||
SmartDashboard::PutData(wrist);
|
||||
SmartDashboard::PutData(claw);
|
||||
// Show what command your subsystem is running on the SmartDashboard
|
||||
SmartDashboard::PutData(drivetrain.get());
|
||||
SmartDashboard::PutData(elevator.get());
|
||||
SmartDashboard::PutData(wrist.get());
|
||||
SmartDashboard::PutData(claw.get());
|
||||
}
|
||||
|
||||
void Robot::AutonomousInit() {
|
||||
autonomousCommand->Start();
|
||||
autonomousCommand.Start();
|
||||
std::cout << "Starting Auto" << std::endl;
|
||||
}
|
||||
|
||||
@@ -41,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();
|
||||
autonomousCommand.Cancel();
|
||||
std::cout << "Starting Teleop" << std::endl;
|
||||
}
|
||||
|
||||
@@ -50,7 +46,7 @@ void Robot::TeleopPeriodic() {
|
||||
}
|
||||
|
||||
void Robot::TestPeriodic() {
|
||||
lw->Run();
|
||||
lw.Run();
|
||||
}
|
||||
|
||||
START_ROBOT_CLASS(Robot);
|
||||
START_ROBOT_CLASS(Robot)
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
|
||||
#include "WPILib.h"
|
||||
#include "Commands/Command.h"
|
||||
#include "Commands/Autonomous.h"
|
||||
|
||||
#include "Subsystems/DriveTrain.h"
|
||||
#include "Subsystems/Elevator.h"
|
||||
@@ -19,15 +20,15 @@
|
||||
|
||||
class Robot: public IterativeRobot {
|
||||
public:
|
||||
static DriveTrain* drivetrain;
|
||||
static Elevator* elevator;
|
||||
static Wrist* wrist;
|
||||
static Claw* claw;
|
||||
static OI* oi;
|
||||
static std::shared_ptr<DriveTrain> drivetrain;
|
||||
static std::shared_ptr<Elevator> elevator;
|
||||
static std::shared_ptr<Wrist> wrist;
|
||||
static std::shared_ptr<Claw> claw;
|
||||
static std::unique_ptr<OI> oi;
|
||||
|
||||
private:
|
||||
Command *autonomousCommand;
|
||||
LiveWindow *lw;
|
||||
Autonomous autonomousCommand;
|
||||
LiveWindow &lw = LiveWindow::GetInstance();
|
||||
|
||||
void RobotInit();
|
||||
void AutonomousInit();
|
||||
|
||||
@@ -1,16 +1,12 @@
|
||||
#include "DriveTrain.h"
|
||||
#include "Commands/TankDriveWithJoystick.h"
|
||||
|
||||
DriveTrain::DriveTrain() : Subsystem("DriveTrain") {
|
||||
front_left_motor = new Talon(1);
|
||||
back_left_motor = new Talon(2);
|
||||
front_right_motor = new Talon(3);
|
||||
back_right_motor = new Talon(4);
|
||||
drive = new RobotDrive(front_left_motor, back_left_motor,
|
||||
front_right_motor, back_right_motor);
|
||||
|
||||
left_encoder = new Encoder(1, 2);
|
||||
right_encoder = new Encoder(3, 4);
|
||||
DriveTrain::DriveTrain()
|
||||
: Subsystem("DriveTrain"), left_encoder(new Encoder(1, 2)),
|
||||
right_encoder(new Encoder(3, 4)), rangefinder(new AnalogInput(6)),
|
||||
gyro(new Gyro(1)) {
|
||||
drive = new RobotDrive(new Talon(1), new Talon(2),
|
||||
new Talon(3), new Talon(4));
|
||||
|
||||
// Encoders may measure differently in the real world and in
|
||||
// simulation. In this example the robot moves 0.042 barleycorns
|
||||
@@ -26,18 +22,15 @@ DriveTrain::DriveTrain() : Subsystem("DriveTrain") {
|
||||
right_encoder->SetDistancePerPulse((double) (4.0/12.0*M_PI) / 360.0);
|
||||
#endif
|
||||
|
||||
rangefinder = new AnalogInput(6);
|
||||
gyro = new Gyro(1);
|
||||
|
||||
// Let's show everything on the LiveWindow
|
||||
// TODO: LiveWindow::GetInstance()->AddActuator("Drive Train", "Front_Left Motor", (Talon) front_left_motor);
|
||||
// TODO: LiveWindow::GetInstance()->AddActuator("Drive Train", "Back Left Motor", (Talon) back_left_motor);
|
||||
// TODO: LiveWindow::GetInstance()->AddActuator("Drive Train", "Front Right Motor", (Talon) front_right_motor);
|
||||
// TODO: LiveWindow::GetInstance()->AddActuator("Drive Train", "Back Right Motor", (Talon) back_right_motor);
|
||||
LiveWindow::GetInstance()->AddSensor("Drive Train", "Left Encoder", left_encoder);
|
||||
LiveWindow::GetInstance()->AddSensor("Drive Train", "Right Encoder", right_encoder);
|
||||
LiveWindow::GetInstance()->AddSensor("Drive Train", "Rangefinder", rangefinder);
|
||||
LiveWindow::GetInstance()->AddSensor("Drive Train", "Gyro", gyro);
|
||||
// TODO: LiveWindow::GetInstance().AddActuator("Drive Train", "Front_Left Motor", (Talon) front_left_motor);
|
||||
// TODO: LiveWindow::GetInstance().AddActuator("Drive Train", "Back Left Motor", (Talon) back_left_motor);
|
||||
// TODO: LiveWindow::GetInstance().AddActuator("Drive Train", "Front Right Motor", (Talon) front_right_motor);
|
||||
// TODO: LiveWindow::GetInstance().AddActuator("Drive Train", "Back Right Motor", (Talon) back_right_motor);
|
||||
LiveWindow::GetInstance().AddSensor("Drive Train", "Left Encoder", left_encoder);
|
||||
LiveWindow::GetInstance().AddSensor("Drive Train", "Right Encoder", right_encoder);
|
||||
LiveWindow::GetInstance().AddSensor("Drive Train", "Rangefinder", rangefinder);
|
||||
LiveWindow::GetInstance().AddSensor("Drive Train", "Gyro", gyro);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -10,12 +10,10 @@
|
||||
*/
|
||||
class DriveTrain : public Subsystem {
|
||||
private:
|
||||
SpeedController *front_left_motor, *back_left_motor,
|
||||
*front_right_motor, *back_right_motor;
|
||||
RobotDrive* drive;
|
||||
Encoder *left_encoder, *right_encoder;
|
||||
AnalogInput* rangefinder;
|
||||
Gyro* gyro;
|
||||
std::shared_ptr<Encoder> left_encoder, right_encoder;
|
||||
std::shared_ptr<AnalogInput> rangefinder;
|
||||
std::shared_ptr<Gyro> gyro;
|
||||
|
||||
public:
|
||||
DriveTrain();
|
||||
|
||||
@@ -18,16 +18,16 @@ Elevator::Elevator() : PIDSubsystem("Elevator", kP_real, kI_real, 0.0) {
|
||||
#endif
|
||||
|
||||
// Let's show everything on the LiveWindow
|
||||
// TODO: LiveWindow::GetInstance()->AddActuator("Elevator", "Motor", (Victor) motor);
|
||||
// TODO: LiveWindow::GetInstance()->AddSensor("Elevator", "Pot", (AnalogPotentiometer) pot);
|
||||
LiveWindow::GetInstance()->AddActuator("Elevator", "PID", GetPIDController());
|
||||
// TODO: LiveWindow::GetInstance().AddActuator("Elevator", "Motor", (Victor) motor);
|
||||
// TODO: LiveWindow::GetInstance().AddSensor("Elevator", "Pot", (AnalogPotentiometer) pot);
|
||||
LiveWindow::GetInstance().AddActuator("Elevator", "PID", GetPIDController());
|
||||
}
|
||||
|
||||
void Elevator::Log() {
|
||||
// TODO: SmartDashboard::PutData("Wrist Pot", (AnalogPotentiometer) pot);
|
||||
}
|
||||
|
||||
double Elevator::ReturnPIDInput() {
|
||||
double Elevator::ReturnPIDInput() const {
|
||||
return pot->Get();
|
||||
}
|
||||
|
||||
|
||||
@@ -29,7 +29,7 @@ public:
|
||||
* Use the potentiometer as the PID sensor. This method is automatically
|
||||
* called by the subsystem.
|
||||
*/
|
||||
double ReturnPIDInput();
|
||||
double ReturnPIDInput() const;
|
||||
|
||||
|
||||
/**
|
||||
|
||||
@@ -18,16 +18,16 @@ Wrist::Wrist() : PIDSubsystem("Wrist", kP_real, 0.0, 0.0) {
|
||||
#endif
|
||||
|
||||
// Let's show everything on the LiveWindow
|
||||
// TODO: LiveWindow::GetInstance()->AddActuator("Wrist", "Motor", (Victor) motor);
|
||||
// TODO: LiveWindow::GetInstance()->AddSensor("Wrist", "Pot", (AnalogPotentiometer) pot);
|
||||
LiveWindow::GetInstance()->AddActuator("Wrist", "PID", GetPIDController());
|
||||
// TODO: LiveWindow::GetInstance().AddActuator("Wrist", "Motor", (Victor) motor);
|
||||
// TODO: LiveWindow::GetInstance().AddSensor("Wrist", "Pot", (AnalogPotentiometer) pot);
|
||||
LiveWindow::GetInstance().AddActuator("Wrist", "PID", GetPIDController());
|
||||
}
|
||||
|
||||
void Wrist::Log() {
|
||||
// TODO: SmartDashboard::PutData("Wrist Angle", (AnalogPotentiometer) pot);
|
||||
}
|
||||
|
||||
double Wrist::ReturnPIDInput() {
|
||||
double Wrist::ReturnPIDInput() const {
|
||||
return pot->Get();
|
||||
}
|
||||
|
||||
|
||||
@@ -28,7 +28,7 @@ public:
|
||||
* Use the potentiometer as the PID sensor. This method is automatically
|
||||
* called by the subsystem.
|
||||
*/
|
||||
double ReturnPIDInput();
|
||||
double ReturnPIDInput() const;
|
||||
|
||||
/**
|
||||
* Use the motor as the PID output. This method is automatically called by
|
||||
|
||||
Reference in New Issue
Block a user