REVLib - C++
CANSparkLowLevel.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018-2024 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 <string>
34#include <vector>
35
36#include <frc/motorcontrol/MotorController.h>
37#include <hal/Types.h>
38#include <wpi/deprecated.h>
39
41#include "rev/ControlType.h"
42#include "rev/REVLibError.h"
43
44// Defined in HIL tester source
45class ConfigBase;
46
47namespace rev {
48
49class CANSparkBase;
50
51class CANSparkLowLevel : public frc::MotorController {
52 friend class CANAnalog;
53 friend class CANDigitalInput;
54 friend class CANEncoder;
55 friend class CANPIDController;
56 friend class CANSensor;
57 friend class CANSparkBase;
58 friend class CANSparkMax;
59 friend class CANSparkFlex;
60 friend class MotorFeedbackSensor;
63 friend class SparkMaxLimitSwitch;
68
69 // Defined in HIL tester source
70 friend class ::ConfigBase;
71
72public:
73 static const uint8_t kAPIMajorVersion;
74 static const uint8_t kAPIMinorVersion;
75 static const uint8_t kAPIBuildVersion;
76 static const uint32_t kAPIVersion;
77
78 enum class MotorType { kBrushed = 0, kBrushless = 1 };
79
80 enum class ControlType {
81 kDutyCycle = 0,
82 kVelocity = 1,
83 kVoltage = 2,
84 kPosition = 3,
85 kSmartMotion = 4,
86 kCurrent = 5,
88 };
89
90 enum class ParameterStatus {
91 kOK = 0,
92 kInvalidID = 1,
93 kMismatchType = 2,
94 kAccessMode = 3,
95 kInvalid = 4,
97 };
98
99 enum class PeriodicFrame {
100 kStatus0 = 0,
101 kStatus1 = 1,
102 kStatus2 = 2,
103 kStatus3 = 3,
104 kStatus4 = 4,
105 kStatus5 = 5,
106 kStatus6 = 6,
107 kStatus7 = 7,
108 };
109
112 uint16_t faults;
113 uint16_t stickyFaults;
116 uint8_t lock;
117 uint8_t roboRIO;
118 uint8_t isInverted;
119 uint64_t timestamp;
120 };
121
127 uint64_t timestamp;
128 };
129
132 double iAccum;
133 uint64_t timestamp;
134 };
135
136 enum class TelemetryID {
137 kBusVoltage = 0,
139 kVelocity,
140 kPosition,
141 kIAccum,
144 kFaults,
152 };
153
156 float value = 0;
157 uint64_t timestamp = 0;
158 const char* name;
159 const char* units;
160 float lowerBnd;
161 float upperBnd;
162 };
163
164 enum class SparkModel {
165 kSparkMax = 0,
166 kSparkFlex = 1,
167 kUnknown = 255,
168 };
169
173 virtual ~CANSparkLowLevel();
174
182 uint32_t GetFirmwareVersion();
183
184 uint32_t GetFirmwareVersion(bool& isDebugBuild);
185
192 std::string GetFirmwareString();
193
201 std::vector<uint8_t> GetSerialNumber();
202
209 int GetDeviceId() const;
210
222 WPI_DEPRECATED("Use GetMotorType() instead")
224
232
263
295 WPI_DEPRECATED(
297 "instead")
299 int periodMs);
300
314 void SetPeriodicFrameTimeout(int timeoutMs);
315
335 void SetCANMaxRetries(int numRetries);
336
346 void SetControlFramePeriodMs(int periodMs);
347
356 REVLibError RestoreFactoryDefaults(bool persist = false);
357
373 static void EnableExternalUSBControl(bool enable);
374
375#ifndef __FRC_ROBORIO__
389 static void SetEnable(bool enable);
390#endif
391
392protected:
394 kNoSensor = 0,
395 kHallSensor = 1,
396 kQuadrature = 2,
397 kSensorless = 3,
398 kAnalog = 4,
399 kAltQuadrature = 5,
401 };
402
404 uint32_t rsvd1 : 18;
405 uint32_t invert : 1;
406 uint32_t rsvd2 : 5;
407 uint32_t predefined : 8;
408 };
409
411 uint32_t leaderArbId;
413 uint32_t value;
416 };
417
418 PeriodicStatus0 GetPeriodicStatus0();
419 PeriodicStatus1 GetPeriodicStatus1();
420 PeriodicStatus2 GetPeriodicStatus2();
421
422 REVLibError SetFollow(FollowConfig config);
423
425 double value,
427 int pidSlot = 0, double arbFeedforward = 0, int arbFFUnits = 0);
428
429 float GetSafeFloat(float f);
430
433 // The type is void* because we don't want to expose c_SparkMax_handle to
434 // the consumers of this header file
436
437private:
438 explicit CANSparkLowLevel(int deviceID, MotorType type, SparkModel model);
439
440 int m_deviceID;
441};
442
443} // namespace rev
Definition: CANAnalog.h:44
Definition: CANDigitalInput.h:40
Definition: CANEncoder.h:45
Definition: CANPIDController.h:56
Definition: CANSensor.h:42
Definition: CANSparkBase.h:72
Definition: CANSparkFlex.h:38
Definition: CANSparkLowLevel.h:51
uint32_t GetFirmwareVersion()
Definition: CANSparkLowLevel.cpp:87
static const uint8_t kAPIBuildVersion
Definition: CANSparkLowLevel.h:75
std::vector< uint8_t > GetSerialNumber()
Definition: CANSparkLowLevel.cpp:117
MotorType
Definition: CANSparkLowLevel.h:78
TelemetryID
Definition: CANSparkLowLevel.h:136
void * m_sparkMaxHandle
Definition: CANSparkLowLevel.h:435
FeedbackSensorType
Definition: CANSparkLowLevel.h:393
static const uint8_t kAPIMajorVersion
Definition: CANSparkLowLevel.h:73
SparkModel
Definition: CANSparkLowLevel.h:164
PeriodicStatus1 GetPeriodicStatus1()
Definition: CANSparkLowLevel.cpp:187
static void EnableExternalUSBControl(bool enable)
Definition: CANSparkLowLevel.cpp:243
MotorType m_motorType
Definition: CANSparkLowLevel.h:431
int GetDeviceId() const
Definition: CANSparkLowLevel.cpp:119
ParameterStatus
Definition: CANSparkLowLevel.h:90
std::string GetFirmwareString()
Definition: CANSparkLowLevel.cpp:102
PeriodicStatus0 GetPeriodicStatus0()
Definition: CANSparkLowLevel.cpp:162
REVLibError SetpointCommand(double value, CANSparkLowLevel::ControlType ctrl=ControlType::kDutyCycle, int pidSlot=0, double arbFeedforward=0, int arbFFUnits=0)
Definition: CANSparkLowLevel.cpp:221
PeriodicStatus2 GetPeriodicStatus2()
Definition: CANSparkLowLevel.cpp:202
virtual ~CANSparkLowLevel()
Definition: CANSparkLowLevel.cpp:83
static void SetEnable(bool enable)
Definition: CANSparkLowLevel.cpp:248
float GetSafeFloat(float f)
Definition: CANSparkLowLevel.cpp:232
REVLibError RestoreFactoryDefaults(bool persist=false)
Definition: CANSparkLowLevel.cpp:237
SparkModel m_expectedSparkModel
Definition: CANSparkLowLevel.h:432
void SetPeriodicFrameTimeout(int timeoutMs)
Definition: CANSparkLowLevel.cpp:147
static const uint32_t kAPIVersion
Definition: CANSparkLowLevel.h:76
PeriodicFrame
Definition: CANSparkLowLevel.h:99
MotorType GetInitialMotorType()
Definition: CANSparkLowLevel.cpp:121
REVLibError SetPeriodicFramePeriod(PeriodicFrame frame, int periodMs)
Definition: CANSparkLowLevel.cpp:129
REVLibError SetFollow(FollowConfig config)
Definition: CANSparkLowLevel.cpp:214
void SetCANMaxRetries(int numRetries)
Definition: CANSparkLowLevel.cpp:152
MotorType GetMotorType()
Definition: CANSparkLowLevel.cpp:125
void SetControlFramePeriodMs(int periodMs)
Definition: CANSparkLowLevel.cpp:157
ControlType
Definition: CANSparkLowLevel.h:80
static const uint8_t kAPIMinorVersion
Definition: CANSparkLowLevel.h:74
Definition: CANSparkMax.h:38
Definition: CANSparkMaxLowLevel.h:40
Definition: MotorFeedbackSensor.h:51
Definition: SparkFlexExternalEncoder.h:42
Definition: SparkMaxAbsoluteEncoder.h:49
Definition: SparkMaxAlternateEncoder.h:46
Definition: SparkMaxAnalogSensor.h:59
Definition: SparkMaxLimitSwitch.h:55
Definition: SparkMaxPIDController.h:59
Definition: SparkMaxRelativeEncoder.h:51
Definition: CANSparkLowLevel.cpp:39
REVLibError
Definition: REVLibError.h:33
Definition: CANSparkLowLevel.h:403
uint32_t rsvd2
Definition: CANSparkLowLevel.h:406
uint32_t invert
Definition: CANSparkLowLevel.h:405
uint32_t predefined
Definition: CANSparkLowLevel.h:407
uint32_t rsvd1
Definition: CANSparkLowLevel.h:404
Definition: CANSparkLowLevel.h:410
uint32_t leaderArbId
Definition: CANSparkLowLevel.h:411
union rev::CANSparkLowLevel::FollowConfig::FollowConfigUnion config
Definition: CANSparkLowLevel.h:110
uint8_t roboRIO
Definition: CANSparkLowLevel.h:117
bool isFollower
Definition: CANSparkLowLevel.h:115
uint8_t isInverted
Definition: CANSparkLowLevel.h:118
MotorType motorType
Definition: CANSparkLowLevel.h:114
double appliedOutput
Definition: CANSparkLowLevel.h:111
uint64_t timestamp
Definition: CANSparkLowLevel.h:119
uint8_t lock
Definition: CANSparkLowLevel.h:116
uint16_t stickyFaults
Definition: CANSparkLowLevel.h:113
uint16_t faults
Definition: CANSparkLowLevel.h:112
Definition: CANSparkLowLevel.h:122
double outputCurrent
Definition: CANSparkLowLevel.h:126
uint64_t timestamp
Definition: CANSparkLowLevel.h:127
double sensorVelocity
Definition: CANSparkLowLevel.h:123
double busVoltage
Definition: CANSparkLowLevel.h:125
uint8_t motorTemperature
Definition: CANSparkLowLevel.h:124
Definition: CANSparkLowLevel.h:130
double iAccum
Definition: CANSparkLowLevel.h:132
uint64_t timestamp
Definition: CANSparkLowLevel.h:133
double sensorPosition
Definition: CANSparkLowLevel.h:131
Definition: CANSparkLowLevel.h:154
float value
Definition: CANSparkLowLevel.h:156
uint64_t timestamp
Definition: CANSparkLowLevel.h:157
float upperBnd
Definition: CANSparkLowLevel.h:161
const char * name
Definition: CANSparkLowLevel.h:158
const char * units
Definition: CANSparkLowLevel.h:159
TelemetryID id
Definition: CANSparkLowLevel.h:155
float lowerBnd
Definition: CANSparkLowLevel.h:160
FollowConfigBits bits
Definition: CANSparkLowLevel.h:414
uint32_t value
Definition: CANSparkLowLevel.h:413