mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-20 00:51:42 +00:00
Add new C++ Command framework (#1785)
This is the C++ version of #1682. The old command framework is still available, but will be deprecated. Due to name conflicts, the new framework is in the frc2 namespace. Eventually (after the old command framework is removed in a future year) it will be moved into the main frc namespace.
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2017-2019 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 "Robot.h"
|
||||
|
||||
#include <frc2/command/CommandScheduler.h>
|
||||
|
||||
#include <frc/smartdashboard/SmartDashboard.h>
|
||||
|
||||
void Robot::RobotInit() {}
|
||||
|
||||
/**
|
||||
* This function is called every robot packet, no matter the mode. Use
|
||||
* this for items like diagnostics that you want to run during disabled,
|
||||
* autonomous, teleoperated and test.
|
||||
*
|
||||
* <p> This runs after the mode specific periodic functions, but before
|
||||
* LiveWindow and SmartDashboard integrated updating.
|
||||
*/
|
||||
void Robot::RobotPeriodic() { frc2::CommandScheduler::GetInstance().Run(); }
|
||||
|
||||
/**
|
||||
* This function is called once each time the robot enters Disabled mode. You
|
||||
* can use it to reset any subsystem information you want to clear when the
|
||||
* robot is disabled.
|
||||
*/
|
||||
void Robot::DisabledInit() {}
|
||||
|
||||
void Robot::DisabledPeriodic() {}
|
||||
|
||||
/**
|
||||
* This autonomous runs the autonomous command selected by your {@link
|
||||
* RobotContainer} class.
|
||||
*/
|
||||
void Robot::AutonomousInit() {
|
||||
m_autonomousCommand = m_container.GetAutonomousCommand();
|
||||
|
||||
if (m_autonomousCommand != nullptr) {
|
||||
m_autonomousCommand->Schedule();
|
||||
}
|
||||
}
|
||||
|
||||
void Robot::AutonomousPeriodic() {}
|
||||
|
||||
void Robot::TeleopInit() {
|
||||
// This makes sure that the autonomous stops running when
|
||||
// teleop starts running. If you want the autonomous to
|
||||
// continue until interrupted by another command, remove
|
||||
// this line or comment it out.
|
||||
if (m_autonomousCommand != nullptr) {
|
||||
m_autonomousCommand->Cancel();
|
||||
m_autonomousCommand = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This function is called periodically during operator control.
|
||||
*/
|
||||
void Robot::TeleopPeriodic() {}
|
||||
|
||||
/**
|
||||
* This function is called periodically during test mode.
|
||||
*/
|
||||
void Robot::TestPeriodic() {}
|
||||
|
||||
#ifndef RUNNING_FRC_TESTS
|
||||
int main() { return frc::StartRobot<Robot>(); }
|
||||
#endif
|
||||
@@ -0,0 +1,61 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2019 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 "RobotContainer.h"
|
||||
|
||||
#include <frc2/command/button/JoystickButton.h>
|
||||
|
||||
#include <frc/shuffleboard/Shuffleboard.h>
|
||||
|
||||
#include "commands/DefaultDrive.h"
|
||||
#include "commands/GrabHatch.h"
|
||||
#include "commands/HalveDriveSpeed.h"
|
||||
#include "commands/ReleaseHatch.h"
|
||||
|
||||
RobotContainer::RobotContainer() {
|
||||
// Initialize all of your commands and subsystems here
|
||||
|
||||
// Add commands to the autonomous command chooser
|
||||
m_chooser.AddOption("Simple Auto", &m_simpleAuto);
|
||||
m_chooser.AddOption("Complex Auto", &m_complexAuto);
|
||||
|
||||
// Put the chooser on the dashboard
|
||||
frc::Shuffleboard::GetTab("Autonomous").Add(m_chooser);
|
||||
|
||||
// Configure the button bindings
|
||||
ConfigureButtonBindings();
|
||||
|
||||
// Set up default drive command
|
||||
m_drive.SetDefaultCommand(DefaultDrive(
|
||||
&m_drive,
|
||||
[this] { return m_driverController.GetY(frc::GenericHID::kLeftHand); },
|
||||
[this] { return m_driverController.GetX(frc::GenericHID::kRightHand); }));
|
||||
}
|
||||
|
||||
void RobotContainer::ConfigureButtonBindings() {
|
||||
// Configure your button bindings here
|
||||
|
||||
// NOTE: Using `new` here will leak these commands if they are ever no longer
|
||||
// needed. This is usually a non-issue as button-bindings tend to be permanent
|
||||
// - however, if you wish to avoid this, the commands should be
|
||||
// stack-allocated and declared as members of RobotContainer.
|
||||
|
||||
// Grab the hatch when the 'A' button is pressed.
|
||||
frc2::JoystickButton(&m_driverController, 1)
|
||||
.WhenPressed(new GrabHatch(&m_hatch));
|
||||
// Release the hatch when the 'B' button is pressed.
|
||||
frc2::JoystickButton(&m_driverController, 2)
|
||||
.WhenPressed(new ReleaseHatch(&m_hatch));
|
||||
// While holding the shoulder button, drive at half speed
|
||||
frc2::JoystickButton(&m_driverController, 6)
|
||||
.WhenHeld(new HalveDriveSpeed(&m_drive));
|
||||
}
|
||||
|
||||
frc2::Command* RobotContainer::GetAutonomousCommand() {
|
||||
// Runs the chosen command in autonomous
|
||||
return m_chooser.GetSelected();
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2019 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 "commands/ComplexAuto.h"
|
||||
|
||||
using namespace AutoConstants;
|
||||
|
||||
ComplexAuto::ComplexAuto(DriveSubsystem* drive, HatchSubsystem* hatch) {
|
||||
AddCommands(
|
||||
// Drive forward the specified distance
|
||||
DriveDistance(kAutoDriveDistanceInches, kAutoDriveSpeed, drive),
|
||||
// Release the hatch
|
||||
ReleaseHatch(hatch),
|
||||
// Drive backward the specified distance
|
||||
DriveDistance(kAutoBackupDistanceInches, -kAutoDriveSpeed, drive));
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2019 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 "commands/DefaultDrive.h"
|
||||
|
||||
DefaultDrive::DefaultDrive(DriveSubsystem* subsystem,
|
||||
std::function<double()> forward,
|
||||
std::function<double()> rotation)
|
||||
: m_drive{subsystem}, m_forward{forward}, m_rotation{rotation} {
|
||||
AddRequirements({subsystem});
|
||||
}
|
||||
|
||||
void DefaultDrive::Execute() {
|
||||
m_drive->ArcadeDrive(m_forward(), m_rotation());
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2019 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 "commands/DriveDistance.h"
|
||||
|
||||
#include <cmath>
|
||||
|
||||
DriveDistance::DriveDistance(double inches, double speed,
|
||||
DriveSubsystem* subsystem)
|
||||
: m_drive(subsystem), m_distance(inches), m_speed(speed) {
|
||||
AddRequirements({subsystem});
|
||||
}
|
||||
|
||||
void DriveDistance::Initialize() {
|
||||
m_drive->ResetEncoders();
|
||||
m_drive->ArcadeDrive(m_speed, 0);
|
||||
}
|
||||
|
||||
void DriveDistance::End(bool interrupted) { m_drive->ArcadeDrive(0, 0); }
|
||||
|
||||
bool DriveDistance::IsFinished() {
|
||||
return std::abs(m_drive->GetAverageEncoderDistance()) >= m_distance;
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2019 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 "commands/GrabHatch.h"
|
||||
|
||||
GrabHatch::GrabHatch(HatchSubsystem* subsystem) : m_hatch(subsystem) {
|
||||
AddRequirements({subsystem});
|
||||
}
|
||||
|
||||
void GrabHatch::Initialize() { m_hatch->GrabHatch(); }
|
||||
|
||||
bool GrabHatch::IsFinished() { return true; }
|
||||
@@ -0,0 +1,15 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2019 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 "commands/HalveDriveSpeed.h"
|
||||
|
||||
HalveDriveSpeed::HalveDriveSpeed(DriveSubsystem* subsystem)
|
||||
: m_drive(subsystem) {}
|
||||
|
||||
void HalveDriveSpeed::Initialize() { m_drive->SetMaxOutput(.5); }
|
||||
|
||||
void HalveDriveSpeed::End(bool interrupted) { m_drive->SetMaxOutput(1); }
|
||||
@@ -0,0 +1,16 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2019 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 "commands/ReleaseHatch.h"
|
||||
|
||||
ReleaseHatch::ReleaseHatch(HatchSubsystem* subsystem) : m_hatch(subsystem) {
|
||||
AddRequirements({subsystem});
|
||||
}
|
||||
|
||||
void ReleaseHatch::Initialize() { m_hatch->ReleaseHatch(); }
|
||||
|
||||
bool ReleaseHatch::IsFinished() { return true; }
|
||||
@@ -0,0 +1,47 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2019 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 "subsystems/DriveSubsystem.h"
|
||||
|
||||
using namespace DriveConstants;
|
||||
|
||||
DriveSubsystem::DriveSubsystem()
|
||||
: m_left1{kLeftMotor1Port},
|
||||
m_left2{kLeftMotor2Port},
|
||||
m_right1{kRightMotor1Port},
|
||||
m_right2{kRightMotor2Port},
|
||||
m_leftEncoder{kLeftEncoderPorts[0], kLeftEncoderPorts[1]},
|
||||
m_rightEncoder{kRightEncoderPorts[0], kRightEncoderPorts[1]} {
|
||||
// Set the distance per pulse for the encoders
|
||||
m_leftEncoder.SetDistancePerPulse(kEncoderDistancePerPulse);
|
||||
m_rightEncoder.SetDistancePerPulse(kEncoderDistancePerPulse);
|
||||
}
|
||||
|
||||
void DriveSubsystem::Periodic() {
|
||||
// Implementation of subsystem periodic method goes here.
|
||||
}
|
||||
|
||||
void DriveSubsystem::ArcadeDrive(double fwd, double rot) {
|
||||
m_drive.ArcadeDrive(fwd, rot);
|
||||
}
|
||||
|
||||
void DriveSubsystem::ResetEncoders() {
|
||||
m_leftEncoder.Reset();
|
||||
m_rightEncoder.Reset();
|
||||
}
|
||||
|
||||
double DriveSubsystem::GetAverageEncoderDistance() {
|
||||
return (m_leftEncoder.GetDistance() + m_rightEncoder.GetDistance()) / 2.;
|
||||
}
|
||||
|
||||
frc::Encoder& DriveSubsystem::GetLeftEncoder() { return m_leftEncoder; }
|
||||
|
||||
frc::Encoder& DriveSubsystem::GetRightEncoder() { return m_rightEncoder; }
|
||||
|
||||
void DriveSubsystem::SetMaxOutput(double maxOutput) {
|
||||
m_drive.SetMaxOutput(maxOutput);
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2019 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 "subsystems/HatchSubsystem.h"
|
||||
|
||||
using namespace HatchConstants;
|
||||
|
||||
HatchSubsystem::HatchSubsystem()
|
||||
: m_hatchSolenoid{kHatchSolenoidPorts[0], kHatchSolenoidPorts[1]} {}
|
||||
|
||||
void HatchSubsystem::GrabHatch() {
|
||||
m_hatchSolenoid.Set(frc::DoubleSolenoid::kForward);
|
||||
}
|
||||
|
||||
void HatchSubsystem::ReleaseHatch() {
|
||||
m_hatchSolenoid.Set(frc::DoubleSolenoid::kReverse);
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2019 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
|
||||
|
||||
/**
|
||||
* The Constants header provides a convenient place for teams to hold robot-wide
|
||||
* numerical or bool constants. This should not be used for any other purpose.
|
||||
*
|
||||
* It is generally a good idea to place constants into subsystem- or
|
||||
* command-specific namespaces within this header, which can then be used where
|
||||
* they are needed.
|
||||
*/
|
||||
|
||||
namespace DriveConstants {
|
||||
const int kLeftMotor1Port = 0;
|
||||
const int kLeftMotor2Port = 1;
|
||||
const int kRightMotor1Port = 2;
|
||||
const int kRightMotor2Port = 3;
|
||||
|
||||
const int kLeftEncoderPorts[]{0, 1};
|
||||
const int kRightEncoderPorts[]{2, 3};
|
||||
const bool kLeftEncoderReversed = false;
|
||||
const bool kRightEncoderReversed = true;
|
||||
|
||||
const int kEncoderCPR = 1024;
|
||||
const double kWheelDiameterInches = 6;
|
||||
const double kEncoderDistancePerPulse =
|
||||
// Assumes the encoders are directly mounted on the wheel shafts
|
||||
(kWheelDiameterInches * 3.142) / static_cast<double>(kEncoderCPR);
|
||||
} // namespace DriveConstants
|
||||
|
||||
namespace HatchConstants {
|
||||
const int kHatchSolenoidModule = 0;
|
||||
const int kHatchSolenoidPorts[]{0, 1};
|
||||
} // namespace HatchConstants
|
||||
|
||||
namespace AutoConstants {
|
||||
const double kAutoDriveDistanceInches = 60;
|
||||
const double kAutoBackupDistanceInches = 20;
|
||||
const double kAutoDriveSpeed = .5;
|
||||
} // namespace AutoConstants
|
||||
|
||||
namespace OIConstants {
|
||||
const int kDriverControllerPort = 1;
|
||||
} // namespace OIConstants
|
||||
@@ -0,0 +1,34 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2017-2019 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 <frc2/command/Command.h>
|
||||
|
||||
#include <frc/TimedRobot.h>
|
||||
|
||||
#include "RobotContainer.h"
|
||||
|
||||
class Robot : public frc::TimedRobot {
|
||||
public:
|
||||
void RobotInit() override;
|
||||
void RobotPeriodic() override;
|
||||
void DisabledInit() override;
|
||||
void DisabledPeriodic() override;
|
||||
void AutonomousInit() override;
|
||||
void AutonomousPeriodic() override;
|
||||
void TeleopInit() override;
|
||||
void TeleopPeriodic() override;
|
||||
void TestPeriodic() override;
|
||||
|
||||
private:
|
||||
// Have it null by default so that if testing teleop it
|
||||
// doesn't have undefined behavior and potentially crash.
|
||||
frc2::Command* m_autonomousCommand = nullptr;
|
||||
|
||||
RobotContainer m_container;
|
||||
};
|
||||
@@ -0,0 +1,54 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2019 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 <frc2/command/Command.h>
|
||||
|
||||
#include <frc/XboxController.h>
|
||||
#include <frc/smartdashboard/SendableChooser.h>
|
||||
|
||||
#include "Constants.h"
|
||||
#include "commands/ComplexAuto.h"
|
||||
#include "commands/DefaultDrive.h"
|
||||
#include "commands/DriveDistance.h"
|
||||
#include "subsystems/DriveSubsystem.h"
|
||||
#include "subsystems/HatchSubsystem.h"
|
||||
|
||||
/**
|
||||
* This class is where the bulk of the robot should be declared. Since
|
||||
* Command-based is a "declarative" paradigm, very little robot logic should
|
||||
* actually be handled in the {@link Robot} periodic methods (other than the
|
||||
* scheduler calls). Instead, the structure of the robot (including subsystems,
|
||||
* commands, and button mappings) should be declared here.
|
||||
*/
|
||||
class RobotContainer {
|
||||
public:
|
||||
RobotContainer();
|
||||
|
||||
frc2::Command* GetAutonomousCommand();
|
||||
|
||||
private:
|
||||
// The robot's subsystems and commands are defined here...
|
||||
|
||||
// The robot's subsystems
|
||||
DriveSubsystem m_drive;
|
||||
HatchSubsystem m_hatch;
|
||||
|
||||
// The autonomous routines
|
||||
DriveDistance m_simpleAuto{AutoConstants::kAutoDriveDistanceInches,
|
||||
AutoConstants::kAutoDriveSpeed, &m_drive};
|
||||
ComplexAuto m_complexAuto{&m_drive, &m_hatch};
|
||||
|
||||
// The chooser for the autonomous routines
|
||||
frc::SendableChooser<frc2::Command*> m_chooser;
|
||||
|
||||
// The driver's controller
|
||||
frc::XboxController m_driverController{OIConstants::kDriverControllerPort};
|
||||
|
||||
void ConfigureButtonBindings();
|
||||
};
|
||||
@@ -0,0 +1,31 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2019 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 <frc2/command/CommandHelper.h>
|
||||
#include <frc2/command/SequentialCommandGroup.h>
|
||||
|
||||
#include "Constants.h"
|
||||
#include "commands/DriveDistance.h"
|
||||
#include "commands/ReleaseHatch.h"
|
||||
|
||||
/**
|
||||
* A complex auto command that drives forward, releases a hatch, and then drives
|
||||
* backward.
|
||||
*/
|
||||
class ComplexAuto
|
||||
: public frc2::CommandHelper<frc2::SequentialCommandGroup, ComplexAuto> {
|
||||
public:
|
||||
/**
|
||||
* Creates a new ComplexAuto.
|
||||
*
|
||||
* @param drive The drive subsystem this command will run on
|
||||
* @param hatch The hatch subsystem this command will run on
|
||||
*/
|
||||
ComplexAuto(DriveSubsystem* drive, HatchSubsystem* hatch);
|
||||
};
|
||||
@@ -0,0 +1,41 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2019 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 <frc2/command/CommandBase.h>
|
||||
#include <frc2/command/CommandHelper.h>
|
||||
|
||||
#include "subsystems/DriveSubsystem.h"
|
||||
|
||||
/**
|
||||
* A command to drive the robot with joystick input passed in through lambdas.
|
||||
* Written explicitly for pedagogical purposes - actual code should inline a
|
||||
* command this simple with RunCommand.
|
||||
*
|
||||
* @see RunCommand
|
||||
*/
|
||||
class DefaultDrive
|
||||
: public frc2::CommandHelper<frc2::CommandBase, DefaultDrive> {
|
||||
public:
|
||||
/**
|
||||
* Creates a new DefaultDrive.
|
||||
*
|
||||
* @param subsystem The drive subsystem this command wil run on.
|
||||
* @param forward The control input for driving forwards/backwards
|
||||
* @param rotation The control input for turning
|
||||
*/
|
||||
DefaultDrive(DriveSubsystem* subsystem, std::function<double()> forward,
|
||||
std::function<double()> rotation);
|
||||
|
||||
void Execute() override;
|
||||
|
||||
private:
|
||||
DriveSubsystem* m_drive;
|
||||
std::function<double()> m_forward;
|
||||
std::function<double()> m_rotation;
|
||||
};
|
||||
@@ -0,0 +1,37 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2019 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 <frc2/command/CommandBase.h>
|
||||
#include <frc2/command/CommandHelper.h>
|
||||
|
||||
#include "subsystems/DriveSubsystem.h"
|
||||
|
||||
class DriveDistance
|
||||
: public frc2::CommandHelper<frc2::CommandBase, DriveDistance> {
|
||||
public:
|
||||
/**
|
||||
* Creates a new DriveDistance.
|
||||
*
|
||||
* @param inches The number of inches the robot will drive
|
||||
* @param speed The speed at which the robot will drive
|
||||
* @param drive The drive subsystem on which this command will run
|
||||
*/
|
||||
DriveDistance(double inches, double speed, DriveSubsystem* subsystem);
|
||||
|
||||
void Initialize() override;
|
||||
|
||||
void End(bool interrupted) override;
|
||||
|
||||
bool IsFinished() override;
|
||||
|
||||
private:
|
||||
DriveSubsystem* m_drive;
|
||||
double m_distance;
|
||||
double m_speed;
|
||||
};
|
||||
@@ -0,0 +1,32 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2019 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 <frc2/command/CommandBase.h>
|
||||
#include <frc2/command/CommandHelper.h>
|
||||
|
||||
#include "subsystems/HatchSubsystem.h"
|
||||
|
||||
/**
|
||||
* A simple command that grabs a hatch with the HatchSubsystem. Written
|
||||
* explicitly for pedagogical purposes. Actual code should inline a command
|
||||
* this simple with InstantCommand.
|
||||
*
|
||||
* @see InstantCommand
|
||||
*/
|
||||
class GrabHatch : public frc2::CommandHelper<frc2::CommandBase, GrabHatch> {
|
||||
public:
|
||||
explicit GrabHatch(HatchSubsystem* subsystem);
|
||||
|
||||
void Initialize() override;
|
||||
|
||||
bool IsFinished() override;
|
||||
|
||||
private:
|
||||
HatchSubsystem* m_hatch;
|
||||
};
|
||||
@@ -0,0 +1,26 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2019 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 <frc2/command/CommandBase.h>
|
||||
#include <frc2/command/CommandHelper.h>
|
||||
|
||||
#include "subsystems/DriveSubsystem.h"
|
||||
|
||||
class HalveDriveSpeed
|
||||
: public frc2::CommandHelper<frc2::CommandBase, HalveDriveSpeed> {
|
||||
public:
|
||||
explicit HalveDriveSpeed(DriveSubsystem* subsystem);
|
||||
|
||||
void Initialize() override;
|
||||
|
||||
void End(bool interrupted) override;
|
||||
|
||||
private:
|
||||
DriveSubsystem* m_drive;
|
||||
};
|
||||
@@ -0,0 +1,33 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2019 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 <frc2/command/CommandBase.h>
|
||||
#include <frc2/command/CommandHelper.h>
|
||||
|
||||
#include "subsystems/HatchSubsystem.h"
|
||||
|
||||
/**
|
||||
* A simple command that releases a hatch with the HatchSubsystem. Written
|
||||
* explicitly for pedagogical purposes. Actual code should inline a command
|
||||
* this simple with InstantCommand.
|
||||
*
|
||||
* @see InstantCommand
|
||||
*/
|
||||
class ReleaseHatch
|
||||
: public frc2::CommandHelper<frc2::CommandBase, ReleaseHatch> {
|
||||
public:
|
||||
explicit ReleaseHatch(HatchSubsystem* subsystem);
|
||||
|
||||
void Initialize() override;
|
||||
|
||||
bool IsFinished() override;
|
||||
|
||||
private:
|
||||
HatchSubsystem* m_hatch;
|
||||
};
|
||||
@@ -0,0 +1,96 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2019 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 <frc2/command/SubsystemBase.h>
|
||||
|
||||
#include <frc/Encoder.h>
|
||||
#include <frc/PWMVictorSPX.h>
|
||||
#include <frc/SpeedControllerGroup.h>
|
||||
#include <frc/drive/DifferentialDrive.h>
|
||||
|
||||
#include "Constants.h"
|
||||
|
||||
class DriveSubsystem : public frc2::SubsystemBase {
|
||||
public:
|
||||
DriveSubsystem();
|
||||
|
||||
/**
|
||||
* Will be called periodically whenever the CommandScheduler runs.
|
||||
*/
|
||||
void Periodic() override;
|
||||
|
||||
// Subsystem methods go here.
|
||||
|
||||
/**
|
||||
* Drives the robot using arcade controls.
|
||||
*
|
||||
* @param fwd the commanded forward movement
|
||||
* @param rot the commanded rotation
|
||||
*/
|
||||
void ArcadeDrive(double fwd, double rot);
|
||||
|
||||
/**
|
||||
* Resets the drive encoders to currently read a position of 0.
|
||||
*/
|
||||
void ResetEncoders();
|
||||
|
||||
/**
|
||||
* Gets the average distance of the TWO encoders.
|
||||
*
|
||||
* @return the average of the TWO encoder readings
|
||||
*/
|
||||
double GetAverageEncoderDistance();
|
||||
|
||||
/**
|
||||
* Gets the left drive encoder.
|
||||
*
|
||||
* @return the left drive encoder
|
||||
*/
|
||||
frc::Encoder& GetLeftEncoder();
|
||||
|
||||
/**
|
||||
* Gets the right drive encoder.
|
||||
*
|
||||
* @return the right drive encoder
|
||||
*/
|
||||
frc::Encoder& GetRightEncoder();
|
||||
|
||||
/**
|
||||
* Sets the max output of the drive. Useful for scaling the drive to drive
|
||||
* more slowly.
|
||||
*
|
||||
* @param maxOutput the maximum output to which the drive will be constrained
|
||||
*/
|
||||
void SetMaxOutput(double maxOutput);
|
||||
|
||||
private:
|
||||
// Components (e.g. motor controllers and sensors) should generally be
|
||||
// declared private and exposed only through public methods.
|
||||
|
||||
// The motor controllers
|
||||
frc::PWMVictorSPX m_left1;
|
||||
frc::PWMVictorSPX m_left2;
|
||||
frc::PWMVictorSPX m_right1;
|
||||
frc::PWMVictorSPX m_right2;
|
||||
|
||||
// The motors on the left side of the drive
|
||||
frc::SpeedControllerGroup m_leftMotors{m_left1, m_left2};
|
||||
|
||||
// The motors on the right side of the drive
|
||||
frc::SpeedControllerGroup m_rightMotors{m_right1, m_right2};
|
||||
|
||||
// The robot's drive
|
||||
frc::DifferentialDrive m_drive{m_leftMotors, m_rightMotors};
|
||||
|
||||
// The left-side drive encoder
|
||||
frc::Encoder m_leftEncoder;
|
||||
|
||||
// The right-side drive encoder
|
||||
frc::Encoder m_rightEncoder;
|
||||
};
|
||||
@@ -0,0 +1,36 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2019 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 <frc2/command/SubsystemBase.h>
|
||||
|
||||
#include <frc/DoubleSolenoid.h>
|
||||
|
||||
#include "Constants.h"
|
||||
|
||||
class HatchSubsystem : public frc2::SubsystemBase {
|
||||
public:
|
||||
HatchSubsystem();
|
||||
|
||||
// Subsystem methods go here.
|
||||
|
||||
/**
|
||||
* Grabs the hatch.
|
||||
*/
|
||||
void GrabHatch();
|
||||
|
||||
/**
|
||||
* Releases the hatch.
|
||||
*/
|
||||
void ReleaseHatch();
|
||||
|
||||
private:
|
||||
// Components (e.g. motor controllers and sensors) should generally be
|
||||
// declared private and exposed only through public methods.
|
||||
frc::DoubleSolenoid m_hatchSolenoid;
|
||||
};
|
||||
Reference in New Issue
Block a user