Valor 6800 1.0
Loading...
Searching...
No Matches
BaseController.h
1
2#pragma once
3
4#include <memory>
5#include <string>
6
7#include <ctre/phoenix6/CANBus.hpp>
8#include <ctre/phoenix6/CANcoder.hpp>
9#include <ctre/phoenix6/TalonFX.hpp>
10#include <frc/smartdashboard/SendableChooser.h>
11#include <frc/smartdashboard/SmartDashboard.h>
12#include <frc/system/plant/DCMotor.h>
13#include <networktables/NetworkTable.h>
14#include <networktables/NetworkTableEntry.h>
15#include <networktables/NetworkTableInstance.h>
16#include <units/angle.h>
17#include <units/angular_velocity.h>
18#include <units/current.h>
19#include <units/voltage.h>
20#include <frc/simulation/DCMotorSim.h>
21
22#include "valkyrie/Loggable.h"
23#include "valkyrie/controllers/PIDF.h"
24
25namespace valor {
26
30enum NeutralMode { Brake, Coast };
31
54 public:
58 explicit BaseController(frc::DCMotor _motorSpec)
59 : motorSpec{_motorSpec}, rotorToSensor(1), sensorToMech(1), voltageCompensation(units::volt_t{12.0}) {
60 tuningModeChooser.AddOption("Current", TuningMode::Current);
61 tuningModeChooser.AddOption("Duty Cycle", TuningMode::DutyCycle);
62 tuningModeChooser.AddOption("Speed", TuningMode::Speed);
63 tuningModeChooser.AddOption("Position", TuningMode::Position);
64 tuningModeChooser.SetDefaultOption("Voltage", TuningMode::Voltage);
65 opModeChooser.AddOption("Tuning", OperationalMode::Tuning);
66 opModeChooser.AddOption("Non Operational", OperationalMode::NonOperational);
67 opModeChooser.SetDefaultOption("Operational", OperationalMode::Operational);
68
69 LogChild("Tuning Mode", &tuningModeChooser);
70 LogChild("Operational Mode", &opModeChooser);
71 }
72
73 void OnLoggingStart() override {
74 WriteLog("Tuning Setpoint", 0.0);
75 WriteLog("Tune", false);
76 }
77
78 virtual void applyConfig() = 0;
79
80 enum class OperationalMode { NonOperational, Tuning, Operational };
81 enum class TuningMode { Voltage, Current, DutyCycle, Speed, Position };
82
83 OperationalMode getOpMode() { return opModeChooser.GetSelected(); }
84
85 TuningMode getTuneMode() { return tuningModeChooser.GetSelected(); }
86
93 units::turns_per_second_t getMaxMechSpeed() { return motorSpec.freeSpeed / (rotorToSensor * sensorToMech); }
94
99 void setVoltageCompensation(units::volt_t _voltageCompensation) { voltageCompensation = _voltageCompensation; }
100
101 units::volt_t getVoltageCompensation() { return voltageCompensation; }
102
113 virtual void reset() = 0;
114
122 virtual units::turn_t getPosition() = 0;
123
131 virtual units::ampere_t getCurrent() = 0;
132
140 virtual units::turns_per_second_t getSpeed() = 0;
141
142 virtual void setEncoderPosition(units::turn_t position) = 0;
143
155 virtual void positionSetter(units::turn_t position, int slot = 0) = 0;
156
157 void setPosition(units::turn_t position, int slot = 0) {
158 if (getOpMode() == OperationalMode::Operational)
159 positionSetter(position, slot);
160 else if (getOpMode() == OperationalMode::Tuning && ReadLog<bool>("Tune"))
161 handleTuningMode();
162 else
163 powerSetter(0_V);
164 }
165
174 virtual void setPositionWithFeedForward(units::turn_t position, units::turns_per_second_t feedforwardVelocity, int slot = 0) {
175 // Default: ignore feedforward, just call normal position setter
176 setPosition(position, slot);
177 }
178
190 virtual void speedSetter(units::turns_per_second_t speed, int slot = 0) = 0;
191
192 void setSpeed(units::turns_per_second_t speed, int slot = 0) {
193 if (getOpMode() == OperationalMode::Operational)
195 else if (getOpMode() == OperationalMode::Tuning && ReadLog<bool>("Tune"))
196 handleTuningMode();
197 else
198 powerSetter(0_V);
199 }
200
201 virtual void powerSetter(units::volt_t voltage) { assert(false); }
202 virtual void powerSetter(units::ampere_t current) { assert(false); }
203 virtual void powerSetter(double dutyCycle) { assert(false); }
204
205 template <typename T>
206 void setPower(T power) {
207 if (getOpMode() == OperationalMode::Operational)
208 powerSetter(static_cast<T>(power));
209 else if (getOpMode() == OperationalMode::Tuning && ReadLog<bool>("Tune"))
210 handleTuningMode();
211 else
212 powerSetter(0_V);
213 }
214
226 virtual void setupFollower(int canID, bool followerInverted = false) = 0;
227
238 virtual void setPIDF(valor::PIDF pidf, int slot = 0, bool saveImmediately = false) = 0;
239
251 void setLimits(units::turn_t reverse, units::turn_t forward, bool saveImmediately = false) {
254 }
255
263 virtual void setForwardLimit(units::turn_t forward, bool saveImmediately = false) = 0;
264
272 virtual void setReverseLimit(units::turn_t reverse, bool saveImmediately = false) = 0;
273
289 virtual void setGearRatios(double rotorToSensor, double sensorToMech, bool saveImmediately = false) = 0;
290
291 virtual void setOpenLoopRamp(units::second_t time, bool saveImmediately = false) = 0;
292
293 virtual units::turn_t getAbsEncoderPosition() = 0;
294
295 virtual units::volt_t calculateAppliedVoltage() = 0;
296 virtual void setSimState(frc::sim::DCMotorSim&) = 0;
297
298 frc::DCMotor motorSpec;
299 double rotorToSensor;
300 double sensorToMech;
301
302 protected:
303 units::volt_t voltageCompensation;
304
305 private:
306 void handleTuningMode() {
307 double setpoint = ReadLog<double>("Tuning Setpoint");
308 switch (tuningModeChooser.GetSelected()) {
309 case TuningMode::Voltage:
310 return powerSetter(units::volt_t{setpoint});
311 case TuningMode::Current:
312 return powerSetter(units::ampere_t{setpoint});
313 case TuningMode::DutyCycle:
314 return powerSetter(setpoint);
315 case TuningMode::Speed:
316 return speedSetter(units::turns_per_second_t{setpoint});
317 case TuningMode::Position:
318 return positionSetter(units::turn_t{setpoint});
319 }
320 }
321
322 frc::SendableChooser<TuningMode> tuningModeChooser;
323 frc::SendableChooser<OperationalMode> opModeChooser;
324};
325} // namespace valor
Abstract class that all Valor controllers's should implement.
Definition BaseController.h:53
void setLimits(units::turn_t reverse, units::turn_t forward, bool saveImmediately=false)
Set both soft limits for the motor.
Definition BaseController.h:251
virtual void reset()=0
Resets the motor and any state.
virtual void setForwardLimit(units::turn_t forward, bool saveImmediately=false)=0
Set the forward soft limit for the motor.
BaseController(frc::DCMotor _motorSpec)
Construct a new Valor Controller object.
Definition BaseController.h:58
void setVoltageCompensation(units::volt_t _voltageCompensation)
Setup the voltage compensation for the motor.
Definition BaseController.h:99
virtual void setPIDF(valor::PIDF pidf, int slot=0, bool saveImmediately=false)=0
Change the PIDF values for the motor.
virtual void setupFollower(int canID, bool followerInverted=false)=0
If a motor is paired with another motor, setup that other motor as a follower.
virtual void positionSetter(units::turn_t position, int slot=0)=0
Send the motor to a specific position.
virtual void setReverseLimit(units::turn_t reverse, bool saveImmediately=false)=0
Set the reverse soft limit for the motor.
virtual void setPositionWithFeedForward(units::turn_t position, units::turns_per_second_t feedforwardVelocity, int slot=0)
Send the motor to a specific position with a velocity feedforward.
Definition BaseController.h:174
units::turns_per_second_t getMaxMechSpeed()
Get the mechanisms's maximum speed.
Definition BaseController.h:93
virtual void setGearRatios(double rotorToSensor, double sensorToMech, bool saveImmediately=false)=0
Set the gear ratios for the motor.
virtual units::turns_per_second_t getSpeed()=0
Get the motors speed.
virtual void speedSetter(units::turns_per_second_t speed, int slot=0)=0
Send the motor to a specific speed.
virtual units::ampere_t getCurrent()=0
Get the motors output current.
virtual units::turn_t getPosition()=0
Get the motors position.
void OnLoggingStart() override
Hook invoked when logging is started for this object.
Definition BaseController.h:73
Base helper for publishing and subscribing values to NetworkTables.
Definition Loggable.h:204
T ReadLog(std::string_view field, const T &defaultValue={})
Read a value from NetworkTables for the given field.
Definition Loggable.h:343
T WriteLog(std::string_view field, const T &data)
Publish a value to NetworkTables under the given field.
Definition Loggable.h:313
void LogChild(std::string_view name, Loggable *child)
Register a child Loggable under a named subtree.
Container to hold PID and feed forward values for the motor controller.
Definition PIDF.h:22