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>
22#include "valkyrie/Loggable.h"
23#include "valkyrie/controllers/PIDF.h"
30enum NeutralMode { Brake, Coast };
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);
69 LogChild(
"Tuning Mode", &tuningModeChooser);
70 LogChild(
"Operational Mode", &opModeChooser);
78 virtual void applyConfig() = 0;
80 enum class OperationalMode { NonOperational, Tuning, Operational };
81 enum class TuningMode { Voltage, Current, DutyCycle, Speed, Position };
83 OperationalMode getOpMode() {
return opModeChooser.GetSelected(); }
85 TuningMode getTuneMode() {
return tuningModeChooser.GetSelected(); }
93 units::turns_per_second_t
getMaxMechSpeed() {
return motorSpec.freeSpeed / (rotorToSensor * sensorToMech); }
101 units::volt_t getVoltageCompensation() {
return voltageCompensation; }
142 virtual void setEncoderPosition(units::turn_t position) = 0;
157 void setPosition(units::turn_t position,
int slot = 0) {
158 if (getOpMode() == OperationalMode::Operational)
160 else if (getOpMode() == OperationalMode::Tuning &&
ReadLog<bool>(
"Tune"))
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"))
201 virtual void powerSetter(units::volt_t
voltage) {
assert(
false); }
202 virtual void powerSetter(units::ampere_t
current) {
assert(
false); }
205 template <
typename T>
207 if (getOpMode() == OperationalMode::Operational)
208 powerSetter(
static_cast<T>(
power));
209 else if (getOpMode() == OperationalMode::Tuning &&
ReadLog<bool>(
"Tune"))
293 virtual units::turn_t getAbsEncoderPosition() = 0;
295 virtual units::volt_t calculateAppliedVoltage() = 0;
296 virtual void setSimState(frc::sim::DCMotorSim&) = 0;
298 frc::DCMotor motorSpec;
299 double rotorToSensor;
303 units::volt_t voltageCompensation;
306 void handleTuningMode() {
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:
315 case TuningMode::Speed:
317 case TuningMode::Position:
322 frc::SendableChooser<TuningMode> tuningModeChooser;
323 frc::SendableChooser<OperationalMode> opModeChooser;
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