SubZero Common
Common library components for an FRC CommandRobot
Loading...
Searching...
No Matches
BaseSingleAxisSubsystem.h
Go to the documentation of this file.
1#pragma once
2
3#include <frc/DigitalInput.h>
4#include <frc/DutyCycleEncoder.h>
5#include <frc/controller/PIDController.h>
6#include <frc/smartdashboard/MechanismLigament2d.h>
7#include <frc/smartdashboard/MechanismRoot2d.h>
8#include <frc2/command/CommandPtr.h>
9#include <frc2/command/FunctionalCommand.h>
10#include <frc2/command/InstantCommand.h>
11#include <frc2/command/TrapezoidProfileSubsystem.h>
12#include <rev/CANSparkFlex.h>
13#include <rev/CANSparkMax.h>
14#include <rev/SparkMaxPIDController.h>
15#include <units/acceleration.h>
16#include <units/angle.h>
17#include <units/angular_acceleration.h>
18#include <units/angular_velocity.h>
19#include <units/length.h>
20#include <units/velocity.h>
21
22#include <memory>
23#include <string>
24
30
31namespace subzero {
32
43template <typename TController, typename TDistance>
45 : public ISingleAxisSubsystem<TDistance>,
46 public frc2::TrapezoidProfileSubsystem<TDistance> {
47public:
48 using PidState = typename frc::TrapezoidProfile<TDistance>::State;
49 using Distance_t = units::unit_t<TDistance>;
50 using Velocity =
51 units::compound_unit<TDistance, units::inverse<units::seconds>>;
52 using Velocity_t = units::unit_t<Velocity>;
54 units::compound_unit<Velocity, units::inverse<units::seconds>>;
55 using Acceleration_t = units::unit_t<Acceleration>;
56
57protected:
58 bool IsMovementAllowed(double speed, bool ignoreEncoder = false);
59
60 bool IsMovementAllowed(bool ignoreEncoder = false);
61
62public:
64 std::string name, TController &controller,
66 frc::MechanismObject2d *mechanismNode = nullptr);
67
73 void Periodic() override;
74
75 virtual void RunMotorVelocity(Velocity_t speed,
76 bool ignoreEncoder = false) = 0;
77
78 inline void UseState(PidState setpoint) override {
79 m_controller.RunToPosition(setpoint.position.value());
80 }
81
82 inline void RunMotorSpeedDefault(bool ignoreEncoder = false) override {
83 RunMotorVelocity(m_config.defaultSpeed, ignoreEncoder);
84 }
85
86 void RunMotorPercentage(double percentSpeed,
87 bool ignoreEncoder = false) override;
88
89 inline Distance_t GetCurrentPosition() override {
90 return Distance_t(m_controller.GetEncoderPosition());
91 }
92
93 void Stop() override;
94
95 void ResetEncoder() override;
96
97 inline bool AtHome() override {
98 return AtLimitSwitchMin() || GetCurrentPosition() <= m_config.minDistance;
99 }
100
101 inline bool AtMax() override {
102 return AtLimitSwitchMax() || GetCurrentPosition() >= m_config.maxDistance;
103 }
104
105 bool AtLimitSwitchMin() override;
106
107 bool AtLimitSwitchMax() override;
108
109 frc2::CommandPtr MoveToPositionAbsolute(Distance_t position) override;
110
111 inline frc2::CommandPtr MoveToPositionRelative(Distance_t position) override {
112 return MoveToPositionAbsolute(m_goalPosition + position);
113 }
114
115 frc2::CommandPtr Home() override;
116
117 inline frc2::CommandPtr ResetRelativeEncoder() {
118 return frc2::InstantCommand([this] { ResetEncoder(); }).ToPtr();
119 }
120
121 inline frc::MechanismLigament2d *GetLigament() { return m_ligament2d; }
122
123 inline bool IsEnabled() override { return m_pidEnabled; }
124
125 void DisablePid() override;
126
127 void EnablePid() override;
128
133 void OnInit();
134
135protected:
136 std::optional<frc::DigitalInput *> m_minLimitSwitch;
137 std::optional<frc::DigitalInput *> m_maxLimitSwitch;
138 TController &m_controller;
140 std::string m_name;
143 bool m_home;
144 bool resetOccurred = false;
146 frc2::CommandPtr m_resetEncCmd = EmptyCommand().ToPtr();
147 frc::MechanismLigament2d *m_ligament2d;
148};
149} // namespace subzero
The ultimate solution for turrets, arms, and much more. This class allows for absolute,...
Distance_t GetCurrentPosition() override
Get the current position in terms of converted distance.
std::optional< frc::DigitalInput * > m_minLimitSwitch
virtual void RunMotorVelocity(Velocity_t speed, bool ignoreEncoder=false)=0
Run at the given velocity; disables any in-progress absolute movements.
void Periodic() override
Runs the absolute positioning task and updates relevant info on SmartDashboard.
void EnablePid() override
Enable absolute movements.
void UseState(PidState setpoint) override
bool AtMax() override
Check if axis is at the maximum extent of motion.
frc::MechanismLigament2d * m_ligament2d
typename frc::TrapezoidProfile< TDistance >::State PidState
frc2::CommandPtr MoveToPositionRelative(Distance_t position) override
Start moving to the position given the current distance and relative position.
bool IsMovementAllowed(double speed, bool ignoreEncoder=false)
void Stop() override
Stop the axis.
bool AtHome() override
Check if axis is at the mimimum extent of motion.
frc2::CommandPtr Home() override
Start the homing sequence.
ISingleAxisSubsystem< TDistance >::SingleAxisConfig m_config
units::compound_unit< Velocity, units::inverse< units::seconds > > Acceleration
frc2::CommandPtr MoveToPositionAbsolute(Distance_t position) override
Start moving to the absolute position.
void RunMotorPercentage(double percentSpeed, bool ignoreEncoder=false) override
Run at the given velocity percentage; disables any in-progress absolute movements.
frc::MechanismLigament2d * GetLigament()
units::unit_t< TDistance > Distance_t
bool AtLimitSwitchMin() override
Only check the limit switch for mimimum extent.
BaseSingleAxisSubsystem(std::string name, TController &controller, ISingleAxisSubsystem< TDistance >::SingleAxisConfig config, frc::MechanismObject2d *mechanismNode=nullptr)
std::optional< frc::DigitalInput * > m_maxLimitSwitch
void RunMotorSpeedDefault(bool ignoreEncoder=false) override
Run at the default motor speed.
void OnInit()
This must be called once upon robot startup.
units::unit_t< Acceleration > Acceleration_t
units::compound_unit< TDistance, units::inverse< units::seconds > > Velocity
void ResetEncoder() override
Reset encoder back to 0 ticks.
bool IsEnabled() override
Check if absolute positioning is active.
bool IsMovementAllowed(bool ignoreEncoder=false)
bool AtLimitSwitchMax() override
Only check the limit switch for maximum extent.
void DisablePid() override
Stop and disable any in-progress absolute movements.
The configuration for single-axis mechanisms.