SPARK MAX - C++ Documentation
CANSparkMax.h
1 /*
2  * Copyright (c) 2018-2019 REV Robotics
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * 3. Neither the name of REV Robotics nor the names of its
13  * contributors may be used to endorse or promote products derived from
14  * this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #pragma once
30 
31 #include <stdint.h>
32 
33 #include <map>
34 #include <string>
35 
36 #include "rev/CANDigitalInput.h"
37 #include "rev/CANEncoder.h"
38 #include "rev/CANError.h"
39 #include "rev/CANPIDController.h"
40 #include "rev/CANSparkMaxLowLevel.h"
41 #include "rev/CANAnalog.h"
42 #include "rev/CANStreamable.h"
43 
44 #include "rev/CANSparkMaxDriver.h"
45 
46 namespace rev {
47 
48 class CANSparkMax : public CANSparkMaxLowLevel, public CANStreamable<c_SparkMax_TelemetryID, c_SparkMax_TelemetryMessage> {
49 public:
50 
51  enum class IdleMode { kCoast = 0, kBrake = 1 };
52 
53  enum class InputMode { kPWM = 0, kCAN = 1 };
54 
55  enum class SoftLimitDirection { kForward, kReverse };
56 
57  enum class FaultID {
58  kBrownout = 0,
59  kOvercurrent = 1,
60  kIWDTReset = 2,
61  kMotorFault = 3,
62  kSensorFault = 4,
63  kStall = 5,
64  kEEPROMCRC = 6,
65  kCANTX = 7,
66  kCANRX = 8,
67  kHasReset = 9,
68  kDRVFault = 10,
69  kOtherFault = 11,
70  kSoftLimitFwd = 12,
71  kSoftLimitRev = 13,
72  kHardLimitFwd = 14,
73  kHardLimitRev = 15
74  };
75 
77  int arbId;
78  int configId;
79  };
80 
81  static constexpr ExternalFollower kFollowerDisabled{0, 0};
82  static constexpr ExternalFollower kFollowerSparkMax{0x2051800, 26};
83  static constexpr ExternalFollower kFollowerPhoenix{0x2040080, 27};
84 
94  explicit CANSparkMax(int deviceID, MotorType type);
95 
99  ~CANSparkMax() override = default;
100 
101  /**** Speed Controller Interface ****/
107  void Set(double speed) override;
108 
114  double Get() const override;
115 
123  void SetInverted(bool isInverted) override;
124 
132  bool GetInverted() const override;
133 
137  void Disable() override;
138 
142  void StopMotor() override;
143 
144  void PIDWrite(double output) override;
145 
146  /******* Extended Functions *******/
154  CANEncoder GetEncoder(CANEncoder::EncoderType sensorType = CANEncoder::EncoderType::kHallSensor, int counts_per_rev = 0);
155 
166  CANEncoder GetAlternateEncoder(CANEncoder::AlternateEncoderType sensorType, int counts_per_rev);
167 
173  CANAnalog GetAnalog(CANAnalog::AnalogMode mode = CANAnalog::AnalogMode::kAbsolute);
174 
178  CANPIDController GetPIDController();
179 
187  CANDigitalInput GetForwardLimitSwitch(
188  CANDigitalInput::LimitSwitchPolarity polarity);
189 
197  CANDigitalInput GetReverseLimitSwitch(
198  CANDigitalInput::LimitSwitchPolarity polarity);
199 
217  CANError SetSmartCurrentLimit(unsigned int limit);
218 
243  CANError SetSmartCurrentLimit(unsigned int stallLimit,
244  unsigned int freeLimit,
245  unsigned int limitRPM = 20000);
246 
274  CANError SetSecondaryCurrentLimit(double limit, int limitCycles = 0);
275 
281  CANError SetIdleMode(IdleMode mode);
282 
293  IdleMode GetIdleMode();
294 
303  CANError EnableVoltageCompensation(double nominalVoltage);
304 
310  CANError DisableVoltageCompensation();
311 
318 
327  CANError SetOpenLoopRampRate(double rate);
328 
337  CANError SetClosedLoopRampRate(double rate);
338 
347  double GetOpenLoopRampRate();
348 
357  double GetClosedLoopRampRate();
358 
371  CANError Follow(const CANSparkMax& leader, bool invert = false);
372 
387  CANError Follow(ExternalFollower leader, int deviceID, bool invert = false);
388 
395  bool IsFollower();
396 
400  uint16_t GetFaults();
401 
405  uint16_t GetStickyFaults();
406 
410  bool GetFault(FaultID faultID);
411 
415  bool GetStickyFault(FaultID faultID);
416 
420  double GetBusVoltage();
421 
425  double GetAppliedOutput();
426 
430  double GetOutputCurrent();
431 
435  double GetMotorTemperature();
436 
442  CANError ClearFaults();
443 
447  CANError BurnFlash();
448 
456  CANError SetCANTimeout(int milliseconds);
457 
465  CANError EnableSoftLimit(SoftLimitDirection direction, bool enable);
466 
470  bool IsSoftLimitEnabled(SoftLimitDirection direction);
471 
483  CANError SetSoftLimit(SoftLimitDirection direction, double limit);
484 
492  double GetSoftLimit(SoftLimitDirection direction);
493 
502  CANError GetLastError();
503 
504  void StartStream() override;
505 
506  // void StartStream(rev::CANSparkMax* device) override;
507 
508  void ReadStream(std::vector<c_SparkMax_TelemetryID>& streams, std::vector<c_SparkMax_TelemetryMessage>* messages) override;
509 
510  void CloseStream() override;
511 
512  void ListStreamable(std::vector<c_SparkMax_TelemetryMessage>& streams) override;
513 
514 
515 private:
516  // Only used for Get() or Set() api
517  double m_setpoint;
518 
519  friend class CANDigitalInput;
520  friend class CANEncoder;
521 
522  bool m_altEncInitialized = false;
523  bool m_limitSwitchInitialized = false;
524 
525  // Only used for managing the telemetry streams
526  uint32_t m_telemetryHandle = 0;
527 
531  int GetFeedbackDeviceID();
532 
533 };
534 
535 } // namespace rev
CANError SetSmartCurrentLimit(unsigned int limit)
Definition: CANSparkMax.cpp:94
Definition: CANSparkMax.h:48
double GetClosedLoopRampRate()
Definition: CANSparkMax.cpp:153
void Disable() override
Definition: CANSparkMax.cpp:64
Definition: CANStreamable.h:38
double GetAppliedOutput()
Definition: CANSparkMax.cpp:208
CANError SetClosedLoopRampRate(double rate)
Definition: CANSparkMax.cpp:142
double Get() const override
Definition: CANSparkMax.cpp:52
CANError SetSoftLimit(SoftLimitDirection direction, double limit)
Definition: CANSparkMax.cpp:253
uint16_t GetFaults()
Definition: CANSparkMax.cpp:178
CANError SetCANTimeout(int milliseconds)
Definition: CANSparkMax.cpp:236
bool GetInverted() const override
Definition: CANSparkMax.cpp:58
void StopMotor() override
Definition: CANSparkMax.cpp:66
CANSparkMax(int deviceID, MotorType type)
Definition: CANSparkMax.cpp:42
CANError DisableVoltageCompensation()
Definition: CANSparkMax.cpp:126
double GetOpenLoopRampRate()
Definition: CANSparkMax.cpp:147
AnalogMode
Definition: CANAnalog.h:47
bool GetStickyFault(FaultID faultID)
Definition: CANSparkMax.cpp:196
void ReadStream(std::vector< c_SparkMax_TelemetryID > &streams, std::vector< c_SparkMax_TelemetryMessage > *messages) override
Definition: CANSparkMax.cpp:307
double GetBusVoltage()
Definition: CANSparkMax.cpp:202
void StartStream() override
Definition: CANSparkMax.cpp:303
CANDigitalInput GetForwardLimitSwitch(CANDigitalInput::LimitSwitchPolarity polarity)
Definition: CANSparkMax.cpp:82
CANError GetLastError()
Definition: CANSparkMax.cpp:299
double GetOutputCurrent()
Definition: CANSparkMax.cpp:214
bool GetFault(FaultID faultID)
Definition: CANSparkMax.cpp:190
double GetVoltageCompensationNominalVoltage()
Definition: CANSparkMax.cpp:131
CANPIDController GetPIDController()
Definition: CANSparkMax.cpp:80
void SetInverted(bool isInverted) override
Definition: CANSparkMax.cpp:54
CANError Follow(const CANSparkMax &leader, bool invert=false)
Definition: CANSparkMax.cpp:159
CANError EnableSoftLimit(SoftLimitDirection direction, bool enable)
Definition: CANSparkMax.cpp:241
void Set(double speed) override
Definition: CANSparkMax.cpp:46
bool IsFollower()
Definition: CANSparkMax.cpp:172
CANDigitalInput GetReverseLimitSwitch(CANDigitalInput::LimitSwitchPolarity polarity)
Definition: CANSparkMax.cpp:88
Definition: CANSparkMaxLowLevel.h:45
double GetMotorTemperature()
Definition: CANSparkMax.cpp:220
CANError EnableVoltageCompensation(double nominalVoltage)
Definition: CANSparkMax.cpp:121
Definition: CANSparkMax.h:76
CANAnalog GetAnalog(CANAnalog::AnalogMode mode=CANAnalog::AnalogMode::kAbsolute)
Definition: CANSparkMax.cpp:78
CANError ClearFaults()
Definition: CANSparkMax.cpp:226
IdleMode GetIdleMode()
Definition: CANSparkMax.cpp:115
CANEncoder GetEncoder(CANEncoder::EncoderType sensorType=CANEncoder::EncoderType::kHallSensor, int counts_per_rev=0)
Definition: CANSparkMax.cpp:70
bool IsSoftLimitEnabled(SoftLimitDirection direction)
Definition: CANSparkMax.cpp:279
CANEncoder GetAlternateEncoder(CANEncoder::AlternateEncoderType sensorType, int counts_per_rev)
Definition: CANSparkMax.cpp:74
uint16_t GetStickyFaults()
Definition: CANSparkMax.cpp:184
void CloseStream() override
Definition: CANSparkMax.cpp:317
CANError SetSecondaryCurrentLimit(double limit, int limitCycles=0)
Definition: CANSparkMax.cpp:105
CANError SetOpenLoopRampRate(double rate)
Definition: CANSparkMax.cpp:137
~CANSparkMax() override=default
double GetSoftLimit(SoftLimitDirection direction)
Definition: CANSparkMax.cpp:265
CANError BurnFlash()
Definition: CANSparkMax.cpp:231
CANError SetIdleMode(IdleMode mode)
Definition: CANSparkMax.cpp:110