SubZero Common
Common library components for an FRC CommandRobot
Loading...
Searching...
No Matches
ISingleAxisSubsystem.h
Go to the documentation of this file.
1#pragma once
2
3#include <frc/DigitalInput.h>
4#include <frc/trajectory/TrapezoidProfile.h>
5#include <frc/util/Color8Bit.h>
6#include <frc2/command/CommandPtr.h>
7#include <frc2/command/SubsystemBase.h>
8#include <units/angle.h>
9#include <units/angular_velocity.h>
10#include <units/length.h>
11#include <units/velocity.h>
12
13#include <functional>
14#include <memory>
15#include <string>
16
17namespace subzero {
18
24 units::meter_t minimumLength;
25 units::degree_t minimumAngle;
26 double lineWidth;
27 frc::Color8Bit color;
28};
29
36template <typename Distance> class ISingleAxisSubsystem {
37public:
38 using Distance_t = units::unit_t<Distance>;
39 using Velocity =
40 units::compound_unit<Distance, units::inverse<units::seconds>>;
41 using Velocity_t = units::unit_t<Velocity>;
43 units::compound_unit<Velocity, units::inverse<units::seconds>>;
44 using Acceleration_t = units::unit_t<Acceleration>;
45
73 std::optional<Distance_t> absoluteEncoderDistancePerRevolution;
95 std::optional<frc::DigitalInput *> minLimitSwitch;
101 std::optional<frc::DigitalInput *> maxLimitSwitch;
117 std::optional<std::function<std::string(Distance_t)>> conversionFunction;
122 std::function<bool()> ignoreLimit;
127 frc::TrapezoidProfile<Distance>::Constraints profileConstraints;
128
130 Distance_t _minDistance, Distance_t _maxDistance,
131 Distance_t _encoderDistancePerRevolution,
132 std::optional<Distance_t> _absoluteEncoderDistancePerRevolution,
133 Velocity_t _defaultSpeed, double _velocityScalar, Distance_t _tolerance,
134 std::optional<frc::DigitalInput *> _minLimitSwitch,
135 std::optional<frc::DigitalInput *> _maxLimitSwitch, bool _reversed,
136 SingleAxisMechanism _mechanismConfig,
137 std::optional<std::function<std::string(Distance_t)>>
138 _conversionFunction,
139 std::function<bool()> _ignoreLimit,
140 frc::TrapezoidProfile<Distance>::Constraints _profileConstraints)
141 : minDistance{_minDistance}, maxDistance{_maxDistance},
142 encoderDistancePerRevolution{_encoderDistancePerRevolution},
144 _absoluteEncoderDistancePerRevolution},
145 defaultSpeed{_defaultSpeed}, velocityScalar{_velocityScalar},
146 tolerance{_tolerance}, minLimitSwitch{_minLimitSwitch},
147 maxLimitSwitch{_maxLimitSwitch}, reversed{_reversed},
148 mechanismConfig{_mechanismConfig},
149 conversionFunction{_conversionFunction}, ignoreLimit{_ignoreLimit},
150 profileConstraints{_profileConstraints} {}
151 };
152
160 virtual void RunMotorVelocity(Velocity_t speed,
161 bool ignoreEncoder = false) = 0;
169 virtual void RunMotorPercentage(double percentSpeed,
170 bool ignoreEncoder = false) = 0;
176 virtual void RunMotorSpeedDefault(bool ignoreEncoder = false) = 0;
177
182 virtual void ResetEncoder() = 0;
183
190
197 virtual bool AtHome() = 0;
198
205 virtual bool AtMax() = 0;
206
213 virtual bool AtLimitSwitchMin() = 0;
214
221 virtual bool AtLimitSwitchMax() = 0;
222
229 virtual frc2::CommandPtr MoveToPositionAbsolute(Distance_t position) = 0;
230
238 virtual frc2::CommandPtr MoveToPositionRelative(Distance_t position) = 0;
239
245 virtual frc2::CommandPtr Home() = 0;
246
253 virtual bool IsEnabled() = 0;
254
259 virtual void DisablePid() = 0;
260
265 virtual void EnablePid() = 0;
266
271 virtual void Stop() = 0;
272};
273} // namespace subzero
virtual void EnablePid()=0
Enable absolute movements.
virtual bool AtLimitSwitchMax()=0
Only check the limit switch for maximum extent.
units::unit_t< Distance > Distance_t
virtual void DisablePid()=0
Stop and disable any in-progress absolute movements.
virtual bool AtMax()=0
Check if axis is at the maximum extent of motion.
virtual frc2::CommandPtr MoveToPositionRelative(Distance_t position)=0
Start moving to the position given the current distance and relative position.
virtual frc2::CommandPtr MoveToPositionAbsolute(Distance_t position)=0
Start moving to the absolute position.
units::compound_unit< Distance, units::inverse< units::seconds > > Velocity
units::compound_unit< Velocity, units::inverse< units::seconds > > Acceleration
virtual Distance_t GetCurrentPosition()=0
Get the current position in terms of converted distance.
virtual bool AtHome()=0
Check if axis is at the mimimum extent of motion.
virtual void Stop()=0
Stop the axis.
virtual void ResetEncoder()=0
Reset encoder back to 0 ticks.
virtual bool IsEnabled()=0
Check if absolute positioning is active.
units::unit_t< Acceleration > Acceleration_t
units::unit_t< Velocity > Velocity_t
virtual frc2::CommandPtr Home()=0
Start the homing sequence.
virtual void RunMotorVelocity(Velocity_t speed, bool ignoreEncoder=false)=0
Run at the given velocity; disables any in-progress absolute movements.
virtual void RunMotorSpeedDefault(bool ignoreEncoder=false)=0
Run at the default motor speed.
virtual void RunMotorPercentage(double percentSpeed, bool ignoreEncoder=false)=0
Run at the given velocity percentage; disables any in-progress absolute movements.
virtual bool AtLimitSwitchMin()=0
Only check the limit switch for mimimum extent.
The configuration for single-axis mechanisms.
frc::TrapezoidProfile< Distance >::Constraints profileConstraints
Motion profile constraints.
Distance_t tolerance
Absolute positioning goal is reached within a range of +/- this value.
Distance_t minDistance
Minimum extent of motion.
Velocity_t defaultSpeed
Default movement speed; used when homing.
bool reversed
Motion is reversed if true; only used for simulations currently.
double velocityScalar
Multiply the motor output by this factor.
std::function< bool()> ignoreLimit
If true, soft limits will be disabled.
Distance_t maxDistance
Maximum extent of motion.
std::optional< std::function< std::string(Distance_t)> > conversionFunction
Optional. Will be called on each loop; useful for outputting values to SmartDashboard in a different ...
SingleAxisMechanism mechanismConfig
Simulation configuration.
std::optional< frc::DigitalInput * > minLimitSwitch
Optional. Will check for the limit switch being active when seeing if at limit.
std::optional< Distance_t > absoluteEncoderDistancePerRevolution
Similar to encoderDistancePerRevolution but using the absolute encoder.
std::optional< frc::DigitalInput * > maxLimitSwitch
Optional. Will check for the limit switch being active when seeing if at limit.
SingleAxisConfig(Distance_t _minDistance, Distance_t _maxDistance, Distance_t _encoderDistancePerRevolution, std::optional< Distance_t > _absoluteEncoderDistancePerRevolution, Velocity_t _defaultSpeed, double _velocityScalar, Distance_t _tolerance, std::optional< frc::DigitalInput * > _minLimitSwitch, std::optional< frc::DigitalInput * > _maxLimitSwitch, bool _reversed, SingleAxisMechanism _mechanismConfig, std::optional< std::function< std::string(Distance_t)> > _conversionFunction, std::function< bool()> _ignoreLimit, frc::TrapezoidProfile< Distance >::Constraints _profileConstraints)
Describes the axis for simulation.