CTRE_Phoenix  5.19.4
CANifier.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/CANifierControlFrame.h"
9 #include "ctre/phoenix/CANifierStatusFrame.h"
10 #include "ctre/phoenix/CANifierStickyFaults.h"
11 #include "ctre/phoenix/CANifierFaults.h"
12 #include "ctre/phoenix/CANifierVelocityMeasPeriod.h"
13 
14 namespace ctre {namespace phoenix {
15 
40 
42  velocityMeasurementPeriod(Period_100Ms),
43  velocityMeasurementWindow(64),
44  clearPositionOnLimitF(false),
45  clearPositionOnLimitR(false),
46  clearPositionOnQuadIdx(false)
47  {
48  }
49 
53  std::string toString() {
54  return toString("");
55  }
56 
62  std::string toString(std::string prependString) {
63 
64  std::string retstr = prependString + ".velocityMeasurementPeriod = " + CANifierVelocityMeasPeriodRoutines::toString(velocityMeasurementPeriod) + ";\n";
65  retstr += prependString + ".velocityMeasurementWindow = " + std::to_string(velocityMeasurementWindow) + ";\n";
66  retstr += prependString + ".clearPositionOnLimitF = " + std::to_string(clearPositionOnLimitF) + ";\n";
67  retstr += prependString + ".clearPositionOnLimitR = " + std::to_string(clearPositionOnLimitR) + ";\n";
68  retstr += prependString + ".clearPositionOnQuadIdx = " + std::to_string(clearPositionOnQuadIdx) + ";\n";
69 
70  retstr += CustomParamConfiguration::toString(prependString);
71 
72  return retstr;
73  }
74 
75 };// struct CANifierConfiguration
76 
81 private:
82  static CANifierConfiguration _default;
83 public:
90  static bool VelocityMeasurementPeriodDifferent (const CANifierConfiguration & settings) { return (!(settings.velocityMeasurementPeriod == _default.velocityMeasurementPeriod)) || !settings.enableOptimizations; }
91  static bool VelocityMeasurementWindowDifferent (const CANifierConfiguration & settings) { return (!(settings.velocityMeasurementWindow == _default.velocityMeasurementWindow)) || !settings.enableOptimizations; }
92  static bool ClearPositionOnLimitFDifferent (const CANifierConfiguration & settings) { return (!(settings.clearPositionOnLimitF == _default.clearPositionOnLimitF)) || !settings.enableOptimizations; }
93  static bool ClearPositionOnLimitRDifferent (const CANifierConfiguration & settings) { return (!(settings.clearPositionOnLimitR == _default.clearPositionOnLimitR)) || !settings.enableOptimizations; }
94  static bool ClearPositionOnQuadIdxDifferent (const CANifierConfiguration & settings) { return (!(settings.clearPositionOnQuadIdx == _default.clearPositionOnQuadIdx)) || !settings.enableOptimizations; }
95  static bool CustomParam0Different (const CANifierConfiguration & settings) { return (!(settings.customParam0 == _default.customParam0)) || !settings.enableOptimizations; }
96  static bool CustomParam1Different (const CANifierConfiguration & settings) { return (!(settings.customParam1 == _default.customParam1)) || !settings.enableOptimizations; }
98 };
99 
100 
107 public:
111  enum LEDChannel {
115  LEDChannelA = 0,
119  LEDChannelB = 1,
123  LEDChannelC = 2,
124  };
125 
129  enum PWMChannel {
133  PWMChannel0 = 0,
137  PWMChannel1 = 1,
141  PWMChannel2 = 2,
145  PWMChannel3 = 3,
146  };
147 
151  const int PWMChannelCount = 4;
152 
156  enum GeneralPin { //----- Must match CANifier_CCI enums -----//
160  QUAD_IDX = 0,
164  QUAD_B = 1,
168  QUAD_A = 2,
172  LIMR = 3,
176  LIMF = 4,
180  SDA = 5,
184  SCL = 6,
188  SPI_CS = 7,
192  SPI_MISO_PWM2P = 8,
196  SPI_MOSI_PWM1P = 9,
200  SPI_CLK_PWM0P = 10,
201  };
202 
206  struct PinValues {
210  bool QUAD_IDX;
214  bool QUAD_B;
218  bool QUAD_A;
222  bool LIMR;
226  bool LIMF;
230  bool SDA;
234  bool SCL;
251  };
252 
257  CANifier(int deviceNumber);
258 
259  ~CANifier();
260 
264  static void DestroyAllCANifiers();
265 
271  ErrorCode SetLEDOutput(double percentOutput, LEDChannel ledChannel);
278  ErrorCode SetGeneralOutput(GeneralPin outputPin, bool outputValue, bool outputEnable);
284  ErrorCode SetGeneralOutputs(int outputBits, int isOutputBits);
289  ErrorCode GetGeneralInputs(PinValues &allPins);
295  bool GetGeneralInput(GeneralPin inputPin);
300  int GetQuadraturePosition();
305  int GetQuadratureVelocity();
315  ErrorCode SetQuadraturePosition(int newPosition, int timeoutMs = 0);
328  ErrorCode ConfigVelocityMeasurementPeriod(
329  CANifierVelocityMeasPeriod period, int timeoutMs = 0);
342  ErrorCode ConfigVelocityMeasurementWindow(int windowSize, int timeoutMs = 0);
353  ErrorCode ConfigClearPositionOnLimitF (bool clearPositionOnLimitF, int timeoutMs = 0);
364  ErrorCode ConfigClearPositionOnLimitR (bool clearPositionOnLimitR, int timeoutMs = 0);
375  ErrorCode ConfigClearPositionOnQuadIdx(bool clearPositionOnQuadIdx, int timeoutMs = 0);
381  double GetBusVoltage();
391  ErrorCode GetLastError();
398  ErrorCode SetPWMOutput(int pwmChannel, double dutyCycle);
405  ErrorCode EnablePWMOutput(int pwmChannel, bool bEnable);
411  ErrorCode GetPWMInput(PWMChannel pwmChannel, double pulseWidthAndPeriod[]);
412 
413  //------ Custom Persistent Params ----------//
428  ErrorCode ConfigSetCustomParam(int newValue, int paramIndex,
429  int timeoutMs = 0);
443  int ConfigGetCustomParam(int paramIndex,
444  int timeoutMs = 0);
445  //------ Generic Param API, typically not used ----------//
462  ErrorCode ConfigSetParameter(ParamEnum param, double value,
463  uint8_t subValue, int ordinal, int timeoutMs = 0);
478  double ConfigGetParameter(ParamEnum param, int ordinal, int timeoutMs = 0);
479 
499  ErrorCode ConfigGetParameter(ParamEnum param, int32_t valueToSend,
500  int32_t & valueReceived, uint8_t & subValue, int32_t ordinal,
501  int32_t timeoutMs);
502 
503 
514  ErrorCode SetStatusFramePeriod(CANifierStatusFrame statusFrame,
515  uint8_t periodMs, int timeoutMs = 0);
525  int GetStatusFramePeriod(CANifierStatusFrame frame, int timeoutMs = 0);
533  ErrorCode SetControlFramePeriod(CANifierControlFrame frame, int periodMs);
539  int GetFirmwareVersion();
545  bool HasResetOccurred();
553  ErrorCode GetFaults(CANifierFaults & toFill);
561  ErrorCode GetStickyFaults(CANifierStickyFaults & toFill);
571  ErrorCode ClearStickyFaults(int timeoutMs = 0);
572 
573  //------ All Configs ----------//
584  ctre::phoenix::ErrorCode ConfigAllSettings(const CANifierConfiguration &allConfigs, int timeoutMs = 50);
593  void GetAllConfigs(CANifierConfiguration &allConfigs, int timeoutMs = 50);
603  ErrorCode ConfigFactoryDefault(int timeoutMs = 50);
604 
605 
606 private:
607  void* m_handle;
608  bool _tempPins[11];
609 };// class CANifier
610 
611 } // namespace phoenix
612 } // namespace ctre
Definition: CANifier.h:106
bool enableOptimizations
Definition: CustomParamConfiguration.h:22
bool clearPositionOnLimitF
Definition: CANifier.h:31
std::string toString()
Definition: CustomParamConfiguration.h:33
static std::string toString(CANifierVelocityMeasPeriod value)
Definition: CANifierVelocityMeasPeriod.h:56
Definition: ErrorCode.h:4
ErrorCode
Definition: ErrorCode.h:7
CANifierVelocityMeasPeriod velocityMeasurementPeriod
Definition: CANifier.h:23
int customParam0
Definition: CustomParamConfiguration.h:14
Definition: CANifierFaults.h:9
bool QUAD_A
Definition: CANifier.h:218
GeneralPin
Definition: CANifier.h:156
int customParam1
Definition: CustomParamConfiguration.h:18
Definition: CANifierStickyFaults.h:9
bool SPI_CS_PWM3
Definition: CANifier.h:238
bool SDA
Definition: CANifier.h:230
bool clearPositionOnLimitR
Definition: CANifier.h:35
Definition: CANifier.h:19
bool QUAD_IDX
Definition: CANifier.h:210
Definition: CustomParamConfiguration.h:10
static bool VelocityMeasurementPeriodDifferent(const CANifierConfiguration &settings)
Definition: CANifier.h:90
Definition: CANBusAddressable.h:8
Definition: CANifier.h:206
std::string toString(std::string prependString)
Definition: CANifier.h:62
Definition: CANifier.h:80
std::string toString()
Definition: CANifier.h:53
Definition: CANifierVelocityMeasPeriod.h:43
int velocityMeasurementWindow
Definition: CANifier.h:27
CANifierControlFrame
Definition: CANifierControlFrame.h:7
CANifierStatusFrame
Definition: CANifierStatusFrame.h:7
bool clearPositionOnQuadIdx
Definition: CANifier.h:39
bool SPI_CLK_PWM0
Definition: CANifier.h:250
CANifierVelocityMeasPeriod
Definition: CANifierVelocityMeasPeriod.h:11
bool LIMF
Definition: CANifier.h:226
bool SPI_MOSI_PWM1
Definition: CANifier.h:246
LEDChannel
Definition: CANifier.h:111
bool SPI_MISO_PWM2
Definition: CANifier.h:242
ParamEnum
Definition: paramEnum.h:10
PWMChannel
Definition: CANifier.h:129
bool SCL
Definition: CANifier.h:234
bool LIMR
Definition: CANifier.h:222
bool QUAD_B
Definition: CANifier.h:214