sparkcan Documentation  1.0
SparkBase.hpp
Go to the documentation of this file.
1 
7 #ifndef SPARKBASE_HPP
8 #define SPARKBASE_HPP
9 
10 #include <array>
11 #include <chrono>
12 #include <cmath>
13 #include <cstdint>
14 #include <cstring>
15 #include <limits>
16 #include <map>
17 #include <stdexcept>
18 #include <system_error>
19 #include <thread>
20 #include <variant>
21 #include <optional>
22 
23 #include <linux/can.h>
24 #include <net/if.h>
25 #include <sys/ioctl.h>
26 #include <sys/socket.h>
27 #include <unistd.h>
28 
29 #define RED "\033[31m"
30 #define RESET "\033[0m"
31 
32 constexpr uint8_t PARAM_TYPE_UINT = 0x01;
33 constexpr uint8_t PARAM_TYPE_FLOAT = 0x02;
34 constexpr uint8_t PARAM_TYPE_BOOL = 0x03;
35 
40 enum class SystemControl : uint32_t
41 {
42  BurnFlash = 0x205FC80,
43  FactoryDefaults = 0x2051D00,
44  FactoryReset = 0x2051D40,
45  Identify = 0x2051D80,
46  ResetFaults = 0x2053000,
47  ClearStickyFaults = 0x2054400
48 };
49 
53 enum class MotorControl : uint32_t
54 {
55  Setpoint = 0x2050040,
56  DutyCycle = 0x2050080,
57  Velocity = 0x2050480,
58  SmartVelocity = 0x20504C0,
59  Position = 0x2050C80,
60  Voltage = 0x2051080,
61  Current = 0x20510C0,
62  SmartMotion = 0x2051480
63 };
64 
68 enum class Status : uint32_t
69 {
70  Period0 = 0x2051800,
71  Period1 = 0x2051840,
72  Period2 = 0x2051880,
73  Period3 = 0x20518C0,
74  Period4 = 0x2051900
75 };
76 
80 enum class Parameter : uint32_t
81 {
82  kInputMode = 1,
83  kMotorType = 2,
84  kCommAdvance = 3,
85  kSensorType = 4,
86  kCtrlType = 5,
87  kIdleMode = 6,
88  kInputDeadband = 7,
91  kPolePairs = 10,
92  kCurrentChop = 11,
93  kCurrentChopCycles = 12,
94  kP_0 = 13,
95  kI_0 = 14,
96  kD_0 = 15,
97  kF_0 = 16,
98  kIZone_0 = 17,
99  kDFilter_0 = 18,
100  kOutputMin_0 = 19,
101  kOutputMax_0 = 20,
102  kP_1 = 21,
103  kI_1 = 22,
104  kD_1 = 23,
105  kF_1 = 24,
106  kIZone_1 = 25,
107  kDFilter_1 = 26,
108  kOutputMin_1 = 27,
109  kOutputMax_1 = 28,
110  kP_2 = 29,
111  kI_2 = 30,
112  kD_2 = 31,
113  kF_2 = 32,
114  kIZone_2 = 33,
115  kDFilter_2 = 34,
116  kOutputMin_2 = 35,
117  kOutputMax_2 = 36,
118  kP_3 = 37,
119  kI_3 = 38,
120  kD_3 = 39,
121  kF_3 = 40,
122  kIZone_3 = 41,
123  kDFilter_3 = 42,
124  kOutputMin_3 = 43,
125  kOutputMax_3 = 44,
126  kInverted = 45,
127  kOutputRatio = 46,
128  kSerialNumberLow = 47,
129  kSerialNumberMid = 48,
130  kSerialNumberHigh = 49,
133  kHardLimitFwdEn = 52,
134  kHardLimitRevEn = 53,
135  kSoftLimitFwdEn = 54,
136  kSoftLimitRevEn = 55,
137  kRampRate = 56,
138  kFollowerID = 57,
139  kFollowerConfig = 58,
142  kSmartCurrentConfig = 61,
143  kMotorKv = 63,
144  kMotorR = 64,
145  kMotorL = 65,
148  kEncoderSampleDelta = 71,
149  kEncoderInverted = 72,
172  kIMaxAccum_0 = 96,
176  kIMaxAccum_1 = 100,
177  kSlot3Placeholder1_1 = 101,
178  kSlot3Placeholder2_1 = 102,
179  kSlot3Placeholder3_1 = 103,
180  kIMaxAccum_2 = 104,
181  kSlot3Placeholder1_2 = 105,
182  kSlot3Placeholder2_2 = 106,
183  kSlot3Placeholder3_2 = 107,
184  kIMaxAccum_3 = 108,
185  kSlot3Placeholder1_3 = 109,
186  kSlot3Placeholder2_3 = 110,
187  kSlot3Placeholder3_3 = 111,
190  kClosedLoopRampRate = 114,
191  kSoftLimitFwd = 115,
192  kSoftLimitRev = 116,
195  kAnalogAverageDepth = 121,
196  kAnalogSensorMode = 122,
197  kAnalogInverted = 123,
198  kAnalogSampleDelta = 124,
199  kDataPortConfig = 127,
203  kAltEncoderInverted = 131,
206  kHallSensorSampleRate = 136,
210  kDutyCycleInverted = 141,
213  kPositionPIDMinInput = 150,
214  kPositionPIDMaxInput = 151,
215  kDutyCyclePrescalar = 153,
217 };
218 
222 enum class MotorType : uint8_t
223 {
224  kBrushed = 0,
225  kBrushless = 1
226 };
227 
231 enum class SensorType : uint8_t
232 {
233  kNoSensor = 0,
234  kHallSensor = 1,
235  kEncoder = 2
236 };
237 
241 enum class CtrlType : uint8_t
242 {
243  kDutyCycle = 0,
244  kVelocity = 1,
245  kVoltage = 2,
246  kPosition = 3
247 };
248 
252 enum class IdleMode : uint8_t
253 {
254  kCoast = 0,
255  kBrake = 1
256 };
257 
266 {
267 private:
268  static int soc;
269  std::string interfaceName;
270  uint8_t deviceId;
271  struct sockaddr_can addr;
272  struct ifreq ifr;
273  mutable std::map<Status,
274  std::pair<uint64_t, std::chrono::steady_clock::time_point>>
275  cachedStatus;
276 
284  void SendCanFrame(
285  uint32_t arbitrationId, uint8_t dlc,
286  const std::array<uint8_t, 8> &data = std::array<uint8_t, 8>{}) const;
287 
300  void SendControlMessage(
301  std::variant<MotorControl, SystemControl> command, std::string commandName, float value, std::optional<float> minValue = std::nullopt,
302  std::optional<float> maxValue = std::nullopt) const;
303 
310  uint64_t ReadPeriodicStatus(Status period) const;
311 
326  void SetParameter(
327  Parameter parameterId,
328  uint8_t parameterType,
329  std::string parameterName,
330  std::variant<float, uint32_t, uint16_t, uint8_t, bool> value,
331  std::optional<float> minValue = std::nullopt,
332  std::optional<float> maxValue = std::nullopt,
333  std::optional<std::string> customErrorMessage = std::nullopt);
334 
342  std::variant<float, uint32_t, bool> ReadParameter(Parameter parameterId);
343 
344 public:
365  SparkBase(const std::string &interfaceName, uint8_t deviceId);
366 
372  virtual ~SparkBase();
373 
374  // SystemControl Methods //
375 
379  static void Heartbeat();
380 
384  void ResetFaults();
385 
389  void ClearStickyFaults();
390 
394  void BurnFlash();
395 
399  void FactoryDefaults();
400 
404  void FactoryReset();
405 
409  void Identify();
410 
411  // MotorControl Methods //
412 
417  void SetSetpoint(float setpoint);
418 
423  void SetDutyCycle(float dutyCycle);
424 
429  void SetVelocity(float velocity);
430 
435  void SetSmartVelocity(float smartVelocity);
436 
441  void SetPosition(float position);
442 
447  void SetVoltage(float voltage);
448 
453  void SetCurrent(float current);
454 
459  void SetSmartMotion(float smartMotion);
460 
461  // Status Methods //
462 
467  float GetDutyCycle() const;
468 
473  uint16_t GetFaults() const;
474 
479  uint16_t GetStickyFaults() const;
480 
485  bool GetInverted() const;
486 
491  bool GetIdleMode() const;
492 
497  bool IsFollower() const;
498 
503  float GetVelocity() const;
504 
509  float GetTemperature() const;
510 
515  float GetVoltage() const;
516 
521  float GetCurrent() const;
522 
527  float GetPosition() const;
528 
533  float GetAnalogVoltage() const;
534 
539  float GetAnalogVelocity() const;
540 
545  float GetAnalogPosition() const;
546 
551  float GetAlternateEncoderVelocity() const;
552 
557  float GetAlternateEncoderPosition() const;
558 
559  // Parameter Setters //
560 
565  void SetInputMode(uint8_t mode);
566 
571  void SetMotorType(MotorType type);
572 
577  void SetSensorType(SensorType sensor);
578 
583  void SetIdleMode(IdleMode mode);
584 
590  void SetInputDeadband(float deadband);
591 
596  void SetInverted(bool inverted);
597 
602  void SetRampRate(float rate);
603 
604  // Advanced //
605 
610  void SetMotorKv(uint16_t kv);
611 
616  void SetMotorR(uint16_t r);
617 
622  void SetMotorL(uint16_t l);
623 
624  // Closed Loop //
625 
630  void SetCtrlType(CtrlType type);
631 
636  void SetFeedbackSensorPID0(uint16_t sensor);
637 
643  void SetClosedLoopVoltageMode(uint8_t mode);
644 
649  void SetCompensatedNominalVoltage(float voltage);
650 
655  void SetPositionPIDWrapEnable(bool enable);
656 
661  void SetPositionPIDMinInput(float minInput);
662 
668  void SetPositionPIDMaxInput(float maxInput);
669 
670  // Brushless //
671 
676  void SetPolePairs(uint16_t pairs);
677 
678  // Current Limit //
679 
684  void SetCurrentChop(float chop);
685 
690  void SetCurrentChopCycles(uint16_t cycles);
691 
696  void SetSmartCurrentStallLimit(uint16_t limit);
697 
702  void SetSmartCurrentFreeLimit(uint16_t limit);
703 
708  void SetSmartCurrentConfig(uint16_t config);
709 
710  // PIDF //
711 
717  void SetP(uint8_t slot, float p);
718 
725  void SetI(uint8_t slot, float i);
726 
733  void SetD(uint8_t slot, float d);
734 
741  void SetF(uint8_t slot, float f);
742 
749  void SetIZone(uint8_t slot, float iZone);
750 
757  void SetDFilter(uint8_t slot, float dFilter);
758 
765  void SetOutputMin(uint8_t slot, float min);
766 
773  void SetOutputMax(uint8_t slot, float max);
774 
775  // Limits //
776 
781  void SetHardLimitFwdEn(bool enable);
782 
787  void SetHardLimitRevEn(bool enable);
788 
793  void SetLimitSwitchFwdPolarity(bool polarity);
794 
799  void SetLimitSwitchRevPolarity(bool polarity);
800 
805  void SetSoftLimitFwdEn(bool enable);
806 
811  void SetSoftLimitRevEn(bool enable);
812 
817  void SetSoftLimitFwd(float limit);
818 
823  void SetSoftLimitRev(float limit);
824 
825  // Follower //
826 
831  void SetFollowerID(uint32_t id);
832 
837  void SetFollowerConfig(uint32_t config);
838 
839  // Encoder Port //
840 
845  void SetEncoderCountsPerRev(uint16_t counts);
846 
851  void SetEncoderAverageDepth(uint8_t depth);
852 
857  void SetEncoderSampleDelta(uint8_t delta);
858 
863  void SetEncoderInverted(bool inverted);
864 
869  void SetPositionConversionFactor(float factor);
870 
875  void SetVelocityConversionFactor(float factor);
876 
881  void SetClosedLoopRampRate(float rampRate);
882 
887  void SetHallSensorSampleRate(float rate);
888 
893  void SetHallSensorAverageDepth(uint16_t depth);
894 
895  // Smart Motion //
896 
903  void SetSmartMotionMaxVelocity(uint8_t slot, float maxVel);
904 
911  void SetSmartMotionMaxAccel(uint8_t slot, float maxAccel);
912 
919  void SetSmartMotionMinVelOutput(uint8_t slot, float minVel);
920 
927  void SetSmartMotionAllowedClosedLoopError(uint8_t slot, float error);
928 
935  void SetSmartMotionAccelStrategy(uint8_t slot, float strategy);
936 
943  void SetIMaxAccum(uint8_t slot, float maxAccum);
944 
951  void SetSlot3Placeholder1(uint8_t slot, float value);
952 
959  void SetSlot3Placeholder2(uint8_t slot, float value);
960 
967  void SetSlot3Placeholder3(uint8_t slot, float value);
968 
969  // Analog Sensor //
970 
975  void SetAnalogPositionConversion(float factor);
976 
981  void SetAnalogVelocityConversion(float factor);
982 
987  void SetAnalogAverageDepth(uint16_t depth);
988 
993  void SetAnalogSensorMode(uint8_t mode);
994 
999  void SetAnalogInverted(bool inverted);
1000 
1005  void SetAnalogSampleDelta(uint16_t delta);
1006 
1007  // Alternate Encoder //
1008 
1013  void SetDataPortConfig(uint8_t config);
1014 
1019  void SetAltEncoderCountsPerRev(uint16_t counts);
1020 
1025  void SetAltEncoderAverageDepth(uint8_t depth);
1026 
1031  void SetAltEncoderSampleDelta(uint8_t delta);
1032 
1037  void SetAltEncoderInverted(bool inverted);
1038 
1043  void SetAltEncoderPositionFactor(float factor);
1044 
1049  void SetAltEncoderVelocityFactor(float factor);
1050 
1051  // Duty Cycle Absolute Encoder //
1052 
1057  void SetDutyCyclePositionFactor(float factor);
1058 
1063  void SetDutyCycleVelocityFactor(float factor);
1064 
1069  void SetDutyCycleInverted(bool inverted);
1070 
1075  void SetDutyCycleAverageDepth(uint8_t depth);
1076 
1081  void SetDutyCyclePrescalar(uint8_t prescalar);
1082 
1087  void SetDutyCycleZeroOffset(float offset);
1088 
1089  // Parameter Getters //
1090 
1091  // Basic //
1096  uint8_t GetMotorType();
1097 
1102  uint8_t GetSensorType();
1103 
1108  uint8_t GetIdleMode();
1109 
1114  float GetInputDeadband();
1115 
1120  bool GetInverted();
1121 
1126  float GetRampRate();
1127 
1128  // Advanced //
1129 
1134  uint16_t GetMotorKv();
1135 
1140  uint16_t GetMotorR();
1141 
1146  uint16_t GetMotorL();
1147 
1148  // Closed Loop //
1149 
1154  uint8_t GetCtrlType();
1155 
1160  uint16_t GetFeedbackSensorPID0();
1161 
1166  uint8_t GetClosedLoopVoltageMode();
1167 
1173 
1178  bool GetPositionPIDWrapEnable();
1179 
1184  float GetPositionPIDMinInput();
1185 
1190  float GetPositionPIDMaxInput();
1191 
1192  // Brushless //
1193 
1198  uint16_t GetPolePairs();
1199 
1200  // Current Limit //
1201 
1206  float GetCurrentChop();
1207 
1212  uint16_t GetCurrentChopCycles();
1213 
1218  uint16_t GetSmartCurrentStallLimit();
1219 
1224  uint16_t GetSmartCurrentFreeLimit();
1225 
1230  uint16_t GetSmartCurrentConfig();
1231 
1232  // PIDF //
1233 
1239  float GetP(uint8_t slot);
1240 
1246  float GetI(uint8_t slot);
1247 
1253  float GetD(uint8_t slot);
1254 
1260  float GetF(uint8_t slot);
1261 
1267  float GetIZone(uint8_t slot);
1268 
1274  float GetDFilter(uint8_t slot);
1275 
1281  float GetOutputMin(uint8_t slot);
1282 
1288  float GetOutputMax(uint8_t slot);
1289 
1290  // Limits //
1291 
1296  bool GetHardLimitFwdEn();
1297 
1302  bool GetHardLimitRevEn();
1303 
1309 
1315 
1320  bool GetSoftLimitFwdEn();
1321 
1326  bool GetSoftLimitRevEn();
1327 
1332  float GetSoftLimitFwd();
1333 
1338  float GetSoftLimitRev();
1339 
1340  // Follower //
1341 
1346  uint32_t GetFollowerID();
1347 
1352  uint32_t GetFollowerConfig();
1353 
1354  // Encoder Port //
1355 
1360  uint16_t GetEncoderCountsPerRev();
1361 
1366  uint8_t GetEncoderAverageDepth();
1367 
1372  uint8_t GetEncoderSampleDelta();
1373 
1378  bool GetEncoderInverted();
1379 
1385 
1391 
1396  float GetClosedLoopRampRate();
1397 
1402  float GetHallSensorSampleRate();
1403 
1408  uint16_t GetHallSensorAverageDepth();
1409 
1410  // Smart Motion //
1411 
1417  float GetSmartMotionMaxVelocity(uint8_t slot);
1418 
1424  float GetSmartMotionMaxAccel(uint8_t slot);
1425 
1431  float GetSmartMotionMinVelOutput(uint8_t slot);
1432 
1438  float GetSmartMotionAllowedClosedLoopError(uint8_t slot);
1439 
1445  float GetSmartMotionAccelStrategy(uint8_t slot);
1446 
1452  float GetIMaxAccum(uint8_t slot);
1453 
1459  float GetSlot3Placeholder1(uint8_t slot);
1460 
1466  float GetSlot3Placeholder2(uint8_t slot);
1467 
1473  float GetSlot3Placeholder3(uint8_t slot);
1474 
1475  // Analog Sensor //
1476 
1482 
1488 
1493  uint16_t GetAnalogAverageDepth();
1494 
1499  uint8_t GetAnalogSensorMode();
1500 
1505  bool GetAnalogInverted();
1506 
1511  uint16_t GetAnalogSampleDelta();
1512 
1513  // Alternate Encoder //
1514 
1519  uint8_t GetDataPortConfig();
1520 
1525  uint16_t GetAltEncoderCountsPerRev();
1526 
1531  uint8_t GetAltEncoderAverageDepth();
1532 
1537  uint8_t GetAltEncoderSampleDelta();
1538 
1543  bool GetAltEncoderInverted();
1544 
1550 
1556 
1557  // Duty Cycle Absolute Encoder //
1558 
1564 
1570 
1575  bool GetDutyCycleInverted();
1576 
1581  uint8_t GetDutyCycleAverageDepth();
1582 
1587  uint8_t GetDutyCyclePrescalar();
1588 
1593  float GetDutyCycleZeroOffset();
1594 };
1595 
1596 #endif // SPARKBASE_HPP
constexpr uint8_t PARAM_TYPE_UINT
Parameter type for unsigned integers.
Definition: SparkBase.hpp:32
SystemControl
System control commands for the SPARK controller.
Definition: SparkBase.hpp:41
MotorType
Motor type parameter.
Definition: SparkBase.hpp:223
SensorType
Sensor type parameter.
Definition: SparkBase.hpp:232
CtrlType
Control type parameter.
Definition: SparkBase.hpp:242
constexpr uint8_t PARAM_TYPE_BOOL
Parameter type for boolean values.
Definition: SparkBase.hpp:34
IdleMode
Idle mode parameter.
Definition: SparkBase.hpp:253
Parameter
Parameters for the SPARK controller.
Definition: SparkBase.hpp:81
@ kEncoderAverageDepth
@ kSlot3Placeholder2_2
@ kSmartCurrentConfig
@ kCompensatedNominalVoltage
@ kLimitSwitchRevPolarity
@ kSmartMotionAccelStrategy_0
@ kSmartMotionAllowedClosedLoopError_3
@ kPositionConversionFactor
@ kSlot3Placeholder3_2
@ kSlot3Placeholder1_3
@ kFeedbackSensorPID0
@ kDutyCyclePositionFactor
@ kHallSensorSampleRate
@ kClosedLoopVoltageMode
@ kVelocityConversionFactor
@ kAltEncoderPositionFactor
@ kSerialNumberMid
@ kSmartMotionMinVelOutput_2
@ kAltEncoderCountsPerRev
@ kAnalogSampleDelta
@ kSlot3Placeholder3_0
@ kDutyCycleInverted
@ kSlot3Placeholder1_1
@ kAnalogVelocityConversion
@ kFeedbackSensorPID1
@ kSlot3Placeholder2_0
@ kSmartMotionMaxVelocity_3
@ kAnalogSensorMode
@ kSlot3Placeholder1_0
@ kPositionPIDMinInput
@ kSmartMotionAccelStrategy_2
@ kSmartMotionMinVelOutput_3
@ kAltEncoderSampleDelta
@ kSlot3Placeholder3_3
@ kSmartMotionAllowedClosedLoopError_2
@ kSmartMotionMinVelOutput_1
@ kSmartMotionMaxVelocity_2
@ kSlot3Placeholder3_1
@ kSmartMotionAllowedClosedLoopError_0
@ kAltEncoderVelocityFactor
@ kSmartMotionMaxAccel_3
@ kSmartMotionMaxAccel_0
@ kSmartMotionMaxVelocity_0
@ kSlot3Placeholder2_3
@ kPositionPIDMaxInput
@ kSmartCurrentFreeLimit
@ kSmartMotionAccelStrategy_3
@ kSerialNumberLow
@ kHallSensorAverageDepth
@ kSmartMotionMaxAccel_2
@ kSmartMotionAccelStrategy_1
@ kSlot3Placeholder2_1
@ kSmartCurrentStallLimit
@ kSmartMotionMaxVelocity_1
@ kAnalogAverageDepth
@ kSerialNumberHigh
@ kAltEncoderAverageDepth
@ kSmartMotionMinVelOutput_0
@ kAltEncoderInverted
@ kEncoderInverted
@ kSlot3Placeholder1_2
@ kCurrentChopCycles
@ kClosedLoopRampRate
@ kDutyCycleVelocityFactor
@ kEncoderSampleDelta
@ kAnalogPositionConversion
@ kSmartMotionAllowedClosedLoopError_1
@ kDutyCyclePrescalar
@ kPositionPIDWrapEnable
@ kEncoderCountsPerRev
@ kSmartMotionMaxAccel_1
@ kDutyCycleZeroOffset
@ kLimitSwitchFwdPolarity
@ kDutyCycleAverageDepth
constexpr uint8_t PARAM_TYPE_FLOAT
Parameter type for floating-point numbers.
Definition: SparkBase.hpp:33
Status
Status periods for the SPARK controller.
Definition: SparkBase.hpp:69
MotorControl
Motor control commands for the SPARK controller.
Definition: SparkBase.hpp:54
A base class for controlling REV Robotics SPARK motor controllers via CAN bus.
Definition: SparkBase.hpp:266
void SetAltEncoderPositionFactor(float factor)
Sets the position factor for the alternate encoder.
Definition: SparkBase.cpp:1110
bool GetIdleMode() const
Gets the current idle mode.
Definition: SparkBase.cpp:460
float GetAnalogPosition() const
Gets the current analog position.
Definition: SparkBase.cpp:526
void SetSensorType(SensorType sensor)
Sets the sensor type.
Definition: SparkBase.cpp:555
void SetOutputMax(uint8_t slot, float max)
Sets the output maximum for the specified PID slot.
Definition: SparkBase.cpp:786
float GetRampRate()
Get the ramp rate.
Definition: SparkBase.cpp:1205
uint16_t GetAnalogSampleDelta()
Get the analog sample delta.
Definition: SparkBase.cpp:1895
float GetPosition() const
Gets the current position of the motor.
Definition: SparkBase.cpp:502
void SetAnalogInverted(bool inverted)
Sets whether the analog sensor is inverted.
Definition: SparkBase.cpp:1072
void SetSmartCurrentFreeLimit(uint16_t limit)
Sets the smart current free limit.
Definition: SparkBase.cpp:662
uint8_t GetDataPortConfig()
Get the data port configuration.
Definition: SparkBase.cpp:1905
bool GetLimitSwitchFwdPolarity()
Get the forward limit switch polarity.
Definition: SparkBase.cpp:1531
float GetSmartMotionMinVelOutput(uint8_t slot)
Get the minimum velocity output for Smart Motion in a given slot.
Definition: SparkBase.cpp:1733
float GetOutputMin(uint8_t slot)
Get the output minimum value for a given slot.
Definition: SparkBase.cpp:1479
void SetDutyCyclePositionFactor(float factor)
Sets the position factor for the duty cycle encoder.
Definition: SparkBase.cpp:1122
void SetMotorKv(uint16_t kv)
Sets the motor Kv (velocity constant)
Definition: SparkBase.cpp:584
virtual ~SparkBase()
Destructor for SparkBase.
Definition: SparkBase.cpp:56
float GetDFilter(uint8_t slot)
Get the DFilter value for a given slot.
Definition: SparkBase.cpp:1463
void SetMotorR(uint16_t r)
Sets the motor resistance.
Definition: SparkBase.cpp:589
void SetCtrlType(CtrlType type)
Sets the control type.
Definition: SparkBase.cpp:601
bool GetLimitSwitchRevPolarity()
Get the reverse limit switch polarity.
Definition: SparkBase.cpp:1541
void SetHardLimitFwdEn(bool enable)
Enables or disables the forward hard limit switch.
Definition: SparkBase.cpp:804
void SetAltEncoderSampleDelta(uint8_t delta)
Sets the sample delta for the alternate encoder.
Definition: SparkBase.cpp:1100
void SetDutyCyclePrescalar(uint8_t prescalar)
Sets the prescalar for the duty cycle encoder.
Definition: SparkBase.cpp:1143
float GetAlternateEncoderVelocity() const
Gets the velocity from the alternate encoder.
Definition: SparkBase.cpp:533
bool GetAnalogInverted()
Get the analog inversion state.
Definition: SparkBase.cpp:1885
void SetLimitSwitchFwdPolarity(bool polarity)
Sets the polarity of the forward limit switch.
Definition: SparkBase.cpp:814
uint16_t GetSmartCurrentConfig()
Get the smart current configuration.
Definition: SparkBase.cpp:1373
uint8_t GetAltEncoderAverageDepth()
Get the alternate encoder average depth.
Definition: SparkBase.cpp:1925
uint8_t GetAltEncoderSampleDelta()
Get the alternate encoder sample delta.
Definition: SparkBase.cpp:1935
uint16_t GetSmartCurrentStallLimit()
Get the smart current stall limit.
Definition: SparkBase.cpp:1353
void SetPositionPIDWrapEnable(bool enable)
Enables or disables position PID wrap.
Definition: SparkBase.cpp:623
void SetCurrentChopCycles(uint16_t cycles)
Sets the number of cycles for current chopping.
Definition: SparkBase.cpp:652
void SetSoftLimitFwdEn(bool enable)
Enables or disables the forward soft limit.
Definition: SparkBase.cpp:824
uint8_t GetCtrlType()
Get the control type.
Definition: SparkBase.cpp:1249
void SetSmartMotionMaxVelocity(uint8_t slot, float maxVel)
Sets the maximum velocity for Smart Motion in the specified slot.
Definition: SparkBase.cpp:905
static void Heartbeat()
Sends a heartbeat signal to keep all SPARK controllers active.
Definition: SparkBase.cpp:307
float GetAnalogVelocityConversion()
Get the analog velocity conversion factor.
Definition: SparkBase.cpp:1855
uint16_t GetHallSensorAverageDepth()
Get the Hall sensor average depth.
Definition: SparkBase.cpp:1691
void SetAnalogSampleDelta(uint16_t delta)
Sets the sample delta for analog readings.
Definition: SparkBase.cpp:1077
uint16_t GetAltEncoderCountsPerRev()
Get the alternate encoder counts per revolution.
Definition: SparkBase.cpp:1915
bool GetDutyCycleInverted()
Get the duty cycle inversion state.
Definition: SparkBase.cpp:1995
uint32_t GetFollowerConfig()
Get the follower configuration.
Definition: SparkBase.cpp:1601
void SetClosedLoopVoltageMode(uint8_t mode)
Sets the closed loop voltage mode.
Definition: SparkBase.cpp:612
void SetInputDeadband(float deadband)
Sets the input deadband.
Definition: SparkBase.cpp:567
float GetTemperature() const
Gets the current temperature of the SPARK controller.
Definition: SparkBase.cpp:479
float GetF(uint8_t slot)
Get the feedforward (F) constant for a given slot.
Definition: SparkBase.cpp:1431
uint16_t GetSmartCurrentFreeLimit()
Get the smart current free limit.
Definition: SparkBase.cpp:1363
float GetAltEncoderPositionFactor()
Get the alternate encoder position factor.
Definition: SparkBase.cpp:1955
void SetEncoderInverted(bool inverted)
Sets whether the encoder is inverted.
Definition: SparkBase.cpp:873
float GetCurrentChop()
Get the current chop value.
Definition: SparkBase.cpp:1333
uint8_t GetEncoderAverageDepth()
Get the encoder average depth.
Definition: SparkBase.cpp:1631
void SetPositionPIDMaxInput(float maxInput)
Sets the maximum input for position PID.
Definition: SparkBase.cpp:633
uint8_t GetDutyCyclePrescalar()
Get the duty cycle prescalar.
Definition: SparkBase.cpp:2015
uint16_t GetAnalogAverageDepth()
Get the analog average depth.
Definition: SparkBase.cpp:1865
void SetSmartMotionMinVelOutput(uint8_t slot, float minVel)
Sets the minimum velocity output for Smart Motion in the specified slot.
Definition: SparkBase.cpp:937
void SetEncoderCountsPerRev(uint16_t counts)
Sets the encoder counts per revolution.
Definition: SparkBase.cpp:858
void SetHallSensorAverageDepth(uint16_t depth)
Sets the hall sensor average depth.
Definition: SparkBase.cpp:898
void SetDutyCycleAverageDepth(uint8_t depth)
Sets the average depth for the duty cycle encoder.
Definition: SparkBase.cpp:1137
void SetSoftLimitRev(float limit)
Sets the reverse soft limit.
Definition: SparkBase.cpp:839
void SetAltEncoderCountsPerRev(uint16_t counts)
Sets the counts per revolution for the alternate encoder.
Definition: SparkBase.cpp:1090
float GetPositionConversionFactor()
Get the position conversion factor.
Definition: SparkBase.cpp:1661
void SetVelocity(float velocity)
Sets the motor's velocity.
Definition: SparkBase.cpp:401
void SetCurrentChop(float chop)
Sets the current chop limit.
Definition: SparkBase.cpp:647
void SetD(uint8_t slot, float d)
Sets the derivative gain for the specified PID slot.
Definition: SparkBase.cpp:706
void SetRampRate(float rate)
Sets the ramp rate.
Definition: SparkBase.cpp:577
void FactoryReset()
Performs a factory reset on the SPARK controller.
Definition: SparkBase.cpp:361
float GetDutyCycleZeroOffset()
Get the duty cycle zero offset.
Definition: SparkBase.cpp:2025
void SetClosedLoopRampRate(float rampRate)
Sets the closed loop ramp rate.
Definition: SparkBase.cpp:888
void SetSmartCurrentConfig(uint16_t config)
Sets the smart current configuration.
Definition: SparkBase.cpp:667
float GetIMaxAccum(uint8_t slot)
Get the maximum accumulated I term for Smart Motion in a given slot.
Definition: SparkBase.cpp:1781
void SetSmartMotionAccelStrategy(uint8_t slot, float strategy)
Sets the acceleration strategy for Smart Motion in the specified slot.
Definition: SparkBase.cpp:969
float GetAnalogVelocity() const
Gets the current analog velocity.
Definition: SparkBase.cpp:518
uint8_t GetAnalogSensorMode()
Get the analog sensor mode.
Definition: SparkBase.cpp:1875
float GetCurrent() const
Gets the current drawn by the SPARK controller.
Definition: SparkBase.cpp:493
void ClearStickyFaults()
Clears sticky faults on the SPARK controller.
Definition: SparkBase.cpp:384
float GetPositionPIDMaxInput()
Get the position PID maximum input.
Definition: SparkBase.cpp:1309
bool GetAltEncoderInverted()
Get the alternate encoder inversion state.
Definition: SparkBase.cpp:1945
void SetSmartCurrentStallLimit(uint16_t limit)
Sets the smart current stall limit.
Definition: SparkBase.cpp:657
float GetSoftLimitFwd()
Get the forward soft limit value.
Definition: SparkBase.cpp:1571
void SetAnalogPositionConversion(float factor)
Sets the conversion factor for analog position readings.
Definition: SparkBase.cpp:1051
void SetF(uint8_t slot, float f)
Sets the feedforward gain for the specified PID slot.
Definition: SparkBase.cpp:722
void SetAnalogVelocityConversion(float factor)
Sets the conversion factor for analog velocity readings.
Definition: SparkBase.cpp:1056
void SetInverted(bool inverted)
Sets whether the motor is inverted.
Definition: SparkBase.cpp:572
uint8_t GetMotorType()
Get the motor type.
Definition: SparkBase.cpp:1155
void SetHardLimitRevEn(bool enable)
Enables or disables the reverse hard limit switch.
Definition: SparkBase.cpp:809
void SetSmartMotionAllowedClosedLoopError(uint8_t slot, float error)
Sets the allowed closed loop error for Smart Motion in the specified slot.
Definition: SparkBase.cpp:953
float GetSmartMotionMaxAccel(uint8_t slot)
Get the maximum acceleration for Smart Motion in a given slot.
Definition: SparkBase.cpp:1717
bool GetEncoderInverted()
Get the encoder inversion state.
Definition: SparkBase.cpp:1651
float GetSlot3Placeholder3(uint8_t slot)
Get the value for Slot 3 Placeholder 3 in a given slot.
Definition: SparkBase.cpp:1829
void SetOutputMin(uint8_t slot, float min)
Sets the output minimum for the specified PID slot.
Definition: SparkBase.cpp:770
float GetPositionPIDMinInput()
Get the position PID minimum input.
Definition: SparkBase.cpp:1299
uint16_t GetFeedbackSensorPID0()
Get the feedback sensor PID0 value.
Definition: SparkBase.cpp:1259
void SetFeedbackSensorPID0(uint16_t sensor)
Sets the feedback sensor for PID0.
Definition: SparkBase.cpp:607
uint8_t GetDutyCycleAverageDepth()
Get the duty cycle average depth.
Definition: SparkBase.cpp:2005
float GetSlot3Placeholder2(uint8_t slot)
Get the value for Slot 3 Placeholder 2 in a given slot.
Definition: SparkBase.cpp:1813
bool GetSoftLimitRevEn()
Get the reverse soft limit enable state.
Definition: SparkBase.cpp:1561
void SetPosition(float position)
Sets the motor's position.
Definition: SparkBase.cpp:411
bool GetPositionPIDWrapEnable()
Get the position PID wrap enable state.
Definition: SparkBase.cpp:1289
uint16_t GetMotorR()
Get the motor resistance.
Definition: SparkBase.cpp:1227
float GetVelocityConversionFactor()
Get the velocity conversion factor.
Definition: SparkBase.cpp:1671
uint16_t GetStickyFaults() const
Retrieves the sticky faults.
Definition: SparkBase.cpp:448
void SetDutyCycleInverted(bool inverted)
Sets whether the duty cycle encoder is inverted.
Definition: SparkBase.cpp:1132
float GetCompensatedNominalVoltage()
Get the compensated nominal voltage.
Definition: SparkBase.cpp:1279
void ResetFaults()
Resets all faults on the SPARK controller.
Definition: SparkBase.cpp:379
uint16_t GetMotorKv()
Get the motor Kv rating.
Definition: SparkBase.cpp:1217
uint16_t GetEncoderCountsPerRev()
Get the encoder counts per revolution.
Definition: SparkBase.cpp:1621
void SetInputMode(uint8_t mode)
Sets the input mode.
bool GetInverted() const
Checks if the motor is inverted.
Definition: SparkBase.cpp:454
void SetCurrent(float current)
Sets the motor's current.
Definition: SparkBase.cpp:421
void SetFollowerID(uint32_t id)
Sets the follower ID for this SparkBase.
Definition: SparkBase.cpp:846
void SetAnalogSensorMode(uint8_t mode)
Sets the analog sensor mode.
Definition: SparkBase.cpp:1066
uint16_t GetFaults() const
Retrieves the current faults.
Definition: SparkBase.cpp:442
void SetPositionPIDMinInput(float minInput)
Sets the minimum input for position PID.
Definition: SparkBase.cpp:628
void SetEncoderSampleDelta(uint8_t delta)
Sets the encoder sample delta.
Definition: SparkBase.cpp:868
float GetAnalogVoltage() const
Gets the current analog voltage.
Definition: SparkBase.cpp:510
float GetAltEncoderVelocityFactor()
Get the alternate encoder velocity factor.
Definition: SparkBase.cpp:1965
void SetI(uint8_t slot, float i)
Sets the integral gain for the specified PID slot.
Definition: SparkBase.cpp:690
float GetClosedLoopRampRate()
Get the closed loop ramp rate.
Definition: SparkBase.cpp:1611
bool GetHardLimitRevEn()
Get the reverse hard limit enable state.
Definition: SparkBase.cpp:1521
void SetSlot3Placeholder2(uint8_t slot, float value)
Sets a placeholder value for slot 3 (purpose undefined)
Definition: SparkBase.cpp:1017
void SetDutyCycleZeroOffset(float offset)
Sets the zero offset for the duty cycle encoder.
Definition: SparkBase.cpp:1148
void SetSmartVelocity(float smartVelocity)
Sets the motor's smart velocity.
Definition: SparkBase.cpp:406
void SetPolePairs(uint16_t pairs)
Sets the number of pole pairs for brushless motors.
Definition: SparkBase.cpp:640
bool GetHardLimitFwdEn()
Get the forward hard limit enable state.
Definition: SparkBase.cpp:1511
float GetInputDeadband()
Get the input deadband.
Definition: SparkBase.cpp:1185
void SetMotorL(uint16_t l)
Sets the motor inductance.
Definition: SparkBase.cpp:594
void SetAltEncoderVelocityFactor(float factor)
Sets the velocity factor for the alternate encoder.
Definition: SparkBase.cpp:1115
float GetDutyCycle() const
Retrieves the current applied output.
Definition: SparkBase.cpp:434
void SetSoftLimitFwd(float limit)
Sets the forward soft limit.
Definition: SparkBase.cpp:834
uint16_t GetMotorL()
Get the motor inductance.
Definition: SparkBase.cpp:1237
float GetAnalogPositionConversion()
Get the analog position conversion factor.
Definition: SparkBase.cpp:1845
void SetAltEncoderAverageDepth(uint8_t depth)
Sets the average depth for the alternate encoder.
Definition: SparkBase.cpp:1095
void SetMotorType(MotorType type)
Sets the motor type.
Definition: SparkBase.cpp:549
float GetSmartMotionAccelStrategy(uint8_t slot)
Get the acceleration strategy for Smart Motion in a given slot.
Definition: SparkBase.cpp:1765
uint16_t GetPolePairs()
Get the number of pole pairs.
Definition: SparkBase.cpp:1321
void SetAnalogAverageDepth(uint16_t depth)
Sets the average depth for analog readings.
Definition: SparkBase.cpp:1061
float GetP(uint8_t slot)
Get the proportional (P) constant for a given slot.
Definition: SparkBase.cpp:1383
void SetIZone(uint8_t slot, float iZone)
Sets the integral zone for the specified PID slot.
Definition: SparkBase.cpp:738
void SetSoftLimitRevEn(bool enable)
Enables or disables the reverse soft limit.
Definition: SparkBase.cpp:829
float GetAlternateEncoderPosition() const
Gets the position from the alternate encoder.
Definition: SparkBase.cpp:539
float GetDutyCycleVelocityFactor()
Get the duty cycle velocity factor.
Definition: SparkBase.cpp:1985
void SetVelocityConversionFactor(float factor)
Sets the velocity conversion factor.
Definition: SparkBase.cpp:883
void SetSlot3Placeholder3(uint8_t slot, float value)
Sets a placeholder value for slot 3 (purpose undefined)
Definition: SparkBase.cpp:1033
void SetDFilter(uint8_t slot, float dFilter)
Sets the derivative filter for the specified PID slot.
Definition: SparkBase.cpp:754
void BurnFlash()
Burns the current configuration to the SPARK controller's flash memory.
Definition: SparkBase.cpp:342
float GetSoftLimitRev()
Get the reverse soft limit value.
Definition: SparkBase.cpp:1581
float GetOutputMax(uint8_t slot)
Get the output maximum value for a given slot.
Definition: SparkBase.cpp:1495
void SetIdleMode(IdleMode mode)
Sets the idle mode.
Definition: SparkBase.cpp:561
void SetP(uint8_t slot, float p)
Sets the proportional gain for the specified PID slot.
Definition: SparkBase.cpp:674
float GetSlot3Placeholder1(uint8_t slot)
Get the value for Slot 3 Placeholder 1 in a given slot.
Definition: SparkBase.cpp:1797
float GetD(uint8_t slot)
Get the derivative (D) constant for a given slot.
Definition: SparkBase.cpp:1415
float GetHallSensorSampleRate()
Get the Hall sensor sample rate.
Definition: SparkBase.cpp:1681
uint8_t GetSensorType()
Get the sensor type.
Definition: SparkBase.cpp:1165
void SetFollowerConfig(uint32_t config)
Sets the follower configuration.
Definition: SparkBase.cpp:851
void SetSetpoint(float setpoint)
Sets the value for the currently set control type.
Definition: SparkBase.cpp:391
void SetDutyCycleVelocityFactor(float factor)
Sets the velocity factor for the duty cycle encoder.
Definition: SparkBase.cpp:1127
uint32_t GetFollowerID()
Get the follower ID.
Definition: SparkBase.cpp:1591
void Identify()
Triggers the SPARK controller to identify itself.
Definition: SparkBase.cpp:370
uint8_t GetEncoderSampleDelta()
Get the encoder sample delta.
Definition: SparkBase.cpp:1641
void SetSmartMotion(float smartMotion)
Sets the motor's smart motion.
Definition: SparkBase.cpp:426
void SetEncoderAverageDepth(uint8_t depth)
Sets the encoder average depth.
Definition: SparkBase.cpp:863
float GetDutyCyclePositionFactor()
Get the duty cycle position factor.
Definition: SparkBase.cpp:1975
float GetVoltage() const
Gets the current voltage of the SPARK controller.
Definition: SparkBase.cpp:485
void SetVoltage(float voltage)
Sets the motor's voltage.
Definition: SparkBase.cpp:416
void SetHallSensorSampleRate(float rate)
Sets the hall sensor sample rate.
Definition: SparkBase.cpp:893
float GetIZone(uint8_t slot)
Get the IZone value for a given slot.
Definition: SparkBase.cpp:1447
SparkBase(const std::string &interfaceName, uint8_t deviceId)
Initializes SparkBase with the specified CAN interface and ID.
Definition: SparkBase.cpp:11
void SetIMaxAccum(uint8_t slot, float maxAccum)
Sets the maximum accumulator value for the I term in the specified slot.
Definition: SparkBase.cpp:985
void SetCompensatedNominalVoltage(float voltage)
Sets the compensated nominal voltage.
Definition: SparkBase.cpp:618
void SetAltEncoderInverted(bool inverted)
Sets whether the alternate encoder is inverted.
Definition: SparkBase.cpp:1105
bool GetSoftLimitFwdEn()
Get the forward soft limit enable state.
Definition: SparkBase.cpp:1551
float GetVelocity() const
Gets the current velocity.
Definition: SparkBase.cpp:473
float GetSmartMotionAllowedClosedLoopError(uint8_t slot)
Get the allowed closed-loop error for Smart Motion in a given slot.
Definition: SparkBase.cpp:1749
void SetSlot3Placeholder1(uint8_t slot, float value)
Sets a placeholder value for slot 3 (purpose undefined)
Definition: SparkBase.cpp:1001
void SetSmartMotionMaxAccel(uint8_t slot, float maxAccel)
Sets the maximum acceleration for Smart Motion in the specified slot.
Definition: SparkBase.cpp:921
float GetSmartMotionMaxVelocity(uint8_t slot)
Get the maximum velocity for Smart Motion in a given slot.
Definition: SparkBase.cpp:1701
void SetPositionConversionFactor(float factor)
Sets the position conversion factor.
Definition: SparkBase.cpp:878
uint16_t GetCurrentChopCycles()
Get the current chop cycles.
Definition: SparkBase.cpp:1343
void SetDutyCycle(float dutyCycle)
Sets the motor's applied output.
Definition: SparkBase.cpp:396
float GetI(uint8_t slot)
Get the integral (I) constant for a given slot.
Definition: SparkBase.cpp:1399
uint8_t GetClosedLoopVoltageMode()
Get the closed loop voltage mode.
Definition: SparkBase.cpp:1269
void SetLimitSwitchRevPolarity(bool polarity)
Sets the polarity of the reverse limit switch.
Definition: SparkBase.cpp:819
bool IsFollower() const
Checks if the SPARK controller is in follower mode.
Definition: SparkBase.cpp:466
void FactoryDefaults()
Resets the SPARK controller to factory default settings.
Definition: SparkBase.cpp:351
void SetDataPortConfig(uint8_t config)
Configures the data port.
Definition: SparkBase.cpp:1084