CTRE_Phoenix  5.19.4
CANCoder.h
1 #pragma once
2 
3 #include <cstdint>
4 #include "ctre/phoenix/CANBusAddressable.h"
5 #include "ctre/phoenix/CustomParamConfiguration.h"
6 #include "ctre/phoenix/ErrorCode.h"
7 #include "ctre/phoenix/paramEnum.h"
8 #include "ctre/phoenix/sensors/AbsoluteSensorRange.h"
9 #include "ctre/phoenix/sensors/CANCoderStatusFrame.h"
10 #include "ctre/phoenix/sensors/CANCoderStickyFaults.h"
11 #include "ctre/phoenix/sensors/CANCoderFaults.h"
12 #include "ctre/phoenix/sensors/SensorVelocityMeasPeriod.h"
13 #include "ctre/phoenix/sensors/SensorInitializationStrategy.h"
14 #include "ctre/phoenix/sensors/MagnetFieldStrength.h"
15 #include "ctre/phoenix/sensors/SensorTimeBase.h"
16 
17 namespace ctre {
18  namespace phoenix {
19  namespace sensors {
20 
28  SensorVelocityMeasPeriod velocityMeasurementPeriod = SensorVelocityMeasPeriod::Period_100Ms;
38  ctre::phoenix::sensors::AbsoluteSensorRange absoluteSensorRange = AbsoluteSensorRange::Unsigned_0_to_360;
45  double magnetOffsetDegrees = 0;
52  bool sensorDirection = false;
62  ctre::phoenix::sensors::SensorInitializationStrategy initializationStrategy = ctre::phoenix::sensors::SensorInitializationStrategy::BootToZero;
66  double sensorCoefficient = 360.0 / 4096.0;
73  std::string unitString = "deg";
78  ctre::phoenix::sensors::SensorTimeBase sensorTimeBase = ctre::phoenix::sensors::SensorTimeBase::PerSecond;
79 
84 
88  std::string toString() {
89  return toString("");
90  }
91 
97  std::string toString(std::string prependString) {
98 
99  std::string retstr;
100  retstr += prependString + ".velocityMeasurementPeriod = " + SensorVelocityMeasPeriodRoutines::toString(velocityMeasurementPeriod) + ";\n";
101  retstr += prependString + ".velocityMeasurementWindow = " + std::to_string(velocityMeasurementWindow) + ";\n";
102  retstr += prependString + ".absoluteSensorRange = " + std::to_string(absoluteSensorRange) + ";\n";
103  retstr += prependString + ".magnetOffsetDegrees = " + std::to_string(magnetOffsetDegrees) + ";\n";
104  retstr += prependString + ".sensorDirection = " + std::to_string(sensorDirection) + ";\n";
105  retstr += prependString + ".initializationStrategy = " + std::to_string(initializationStrategy) + ";\n";
106  retstr += prependString + ".sensorCoefficient = " + std::to_string(sensorCoefficient) + ";\n";
107  retstr += prependString + ".unitString = \"" + unitString.c_str() + "\";\n";
108  retstr += prependString + ".sensorTimeBase = " + std::to_string(sensorTimeBase) + ";\n";
109  retstr += CustomParamConfiguration::toString(prependString);
110 
111  return retstr;
112  }
113 
114  };// struct CANCoderConfiguration
115 
120  private:
121  static CANCoderConfiguration _default;
122  public:
129  static bool VelocityMeasurementPeriodDifferent(const CANCoderConfiguration& settings) { return (!(settings.velocityMeasurementPeriod == _default.velocityMeasurementPeriod)) || !settings.enableOptimizations; }
130  static bool VelocityMeasurementWindowDifferent(const CANCoderConfiguration& settings) { return (!(settings.velocityMeasurementWindow == _default.velocityMeasurementWindow)) || !settings.enableOptimizations; }
131  static bool AbsoluteSensorRangeDifferent(const CANCoderConfiguration& settings) { return (!(settings.absoluteSensorRange == _default.absoluteSensorRange)) || !settings.enableOptimizations; }
132  static bool MagnetOffsetDegreesDifferent(const CANCoderConfiguration& settings) { return (!(settings.magnetOffsetDegrees == _default.magnetOffsetDegrees)) || !settings.enableOptimizations; }
133  static bool SensorDirectionDifferent(const CANCoderConfiguration& settings) { return (!(settings.sensorDirection == _default.sensorDirection)) || !settings.enableOptimizations; }
134  static bool InitializationStrategyDifferent(const CANCoderConfiguration& settings) { return (!(settings.initializationStrategy == _default.initializationStrategy)) || !settings.enableOptimizations; }
135  static bool SensorCoefficientDifferent(const CANCoderConfiguration& settings) { return (!(settings.sensorCoefficient == _default.sensorCoefficient)) || !settings.enableOptimizations; }
136  static bool UnitStringDifferent(const CANCoderConfiguration& settings) { return (!(settings.unitString == _default.unitString)) || !settings.enableOptimizations; }
137  static bool SensorTimeBaseDifferent(const CANCoderConfiguration& settings) { return (!(settings.sensorTimeBase == _default.sensorTimeBase)) || !settings.enableOptimizations; }
139  };
140 
141 
147  class CANCoder : public CANBusAddressable {
148  public:
153  CANCoder(int deviceNumber);
154 
155  ~CANCoder();
156 
160  static void DestroyAllCANCoders();
161 
162 
168  double GetPosition();
175  double GetVelocity();
182  ErrorCode SetPosition(double newPosition, int timeoutMs = 0);
188  ErrorCode SetPositionToAbsolute(int timeoutMs = 0);
189 
197  double GetAbsolutePosition();
210  ErrorCode ConfigVelocityMeasurementPeriod(SensorVelocityMeasPeriod period, int timeoutMs = 0);
223  ErrorCode ConfigVelocityMeasurementWindow(int windowSize, int timeoutMs = 0);
236  ErrorCode ConfigAbsoluteSensorRange(ctre::phoenix::sensors::AbsoluteSensorRange absoluteSensorRange, int timeoutMs = 0);
250  ErrorCode ConfigMagnetOffset(double offsetDegrees, int timeoutMs = 0);
266  ErrorCode ConfigSensorInitializationStrategy(ctre::phoenix::sensors::SensorInitializationStrategy initializationStrategy, int timeoutMs = 0);
286  ErrorCode ConfigFeedbackCoefficient(double sensorCoefficient, const std::string& unitString, ctre::phoenix::sensors::SensorTimeBase sensortimeBase, int timeoutMs = 0);
287 
293  double GetBusVoltage();
299  ctre::phoenix::sensors::MagnetFieldStrength GetMagnetFieldStrength();
300 
313  ErrorCode ConfigSensorDirection(bool bSensorDirection, int timeoutMs = 0);
314 
324  ErrorCode GetLastError();
325 
329  std::string GetLastUnitString();
330 
334  double GetLastTimestamp();
335 
350  ErrorCode ConfigSetCustomParam(int newValue, int paramIndex,
351  int timeoutMs = 0);
365  int ConfigGetCustomParam(int paramIndex,
366  int timeoutMs = 0);
367  //------ Generic Param API, typically not used ----------//
384  ErrorCode ConfigSetParameter(ParamEnum param, double value,
385  uint8_t subValue, int ordinal, int timeoutMs = 0);
400  double ConfigGetParameter(ParamEnum param, int ordinal, int timeoutMs = 0);
401 
421  ErrorCode ConfigGetParameter(ParamEnum param, int32_t valueToSend,
422  int32_t& valueReceived, uint8_t& subValue, int32_t ordinal,
423  int32_t timeoutMs);
424 
425 
436  ErrorCode SetStatusFramePeriod(CANCoderStatusFrame statusFrame,
437  uint8_t periodMs, int timeoutMs = 0);
447  int GetStatusFramePeriod(CANCoderStatusFrame frame, int timeoutMs = 0);
453  int GetFirmwareVersion();
459  bool HasResetOccurred();
467  ErrorCode GetFaults(CANCoderFaults& toFill);
475  ErrorCode GetStickyFaults(CANCoderStickyFaults& toFill);
485  ErrorCode ClearStickyFaults(int timeoutMs = 0);
486 
487  //------ All Configs ----------//
498  ctre::phoenix::ErrorCode ConfigAllSettings(const CANCoderConfiguration& allConfigs, int timeoutMs = 50);
508  ErrorCode GetAllConfigs(CANCoderConfiguration& allConfigs, int timeoutMs = 50);
518  ErrorCode ConfigFactoryDefault(int timeoutMs = 50);
519 
520  private:
521  void* m_handle;
522 
523  };// class CANCoder
524 
525  } // namespace sensor
526  } // namespace phoenix
527 } // namespace ctre
bool enableOptimizations
Definition: CustomParamConfiguration.h:22
double sensorCoefficient
Definition: CANCoder.h:66
ctre::phoenix::sensors::AbsoluteSensorRange absoluteSensorRange
Definition: CANCoder.h:38
std::string toString()
Definition: CustomParamConfiguration.h:33
SensorTimeBase
Definition: SensorTimeBase.h:13
Definition: CANCoder.h:147
std::string toString()
Definition: CANCoder.h:88
Definition: ErrorCode.h:4
ErrorCode
Definition: ErrorCode.h:7
CANCoderStatusFrame
Definition: CANCoderStatusFrame.h:8
std::string unitString
Definition: CANCoder.h:73
CANCoderConfiguration()
Definition: CANCoder.h:83
ctre::phoenix::sensors::SensorInitializationStrategy initializationStrategy
Definition: CANCoder.h:62
double magnetOffsetDegrees
Definition: CANCoder.h:45
SensorVelocityMeasPeriod
Definition: SensorVelocityMeasPeriod.h:12
Definition: CANCoderFaults.h:10
Definition: CustomParamConfiguration.h:52
Definition: CANCoderStickyFaults.h:10
std::string toString(std::string prependString)
Definition: CANCoder.h:97
AbsoluteSensorRange
Definition: AbsoluteSensorRange.h:12
Definition: CustomParamConfiguration.h:10
SensorVelocityMeasPeriod velocityMeasurementPeriod
Definition: CANCoder.h:28
Definition: CANBusAddressable.h:8
ctre::phoenix::sensors::SensorTimeBase sensorTimeBase
Definition: CANCoder.h:78
bool sensorDirection
Definition: CANCoder.h:52
static bool VelocityMeasurementPeriodDifferent(const CANCoderConfiguration &settings)
Definition: CANCoder.h:129
SensorInitializationStrategy
Definition: SensorInitializationStrategy.h:12
static std::string toString(SensorVelocityMeasPeriod value)
Definition: SensorVelocityMeasPeriod.h:57
ParamEnum
Definition: paramEnum.h:10
MagnetFieldStrength
Definition: MagnetFieldStrength.h:8
int velocityMeasurementWindow
Definition: CANCoder.h:32