REVLib - C++
SparkLowLevel.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018-2026 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 <wpi/hardware/motor/MotorController.hpp>
37
38#include "rev/ClosedLoopTypes.h"
39#include "rev/REVLibError.h"
40#include "rev/util/Signal.h"
41
42// Defined in HIL tester source
43class ConfigBase;
44
45namespace rev::spark {
46
47class SparkBase;
48
49class SparkLowLevel : public wpi::MotorController {
50 friend class SparkBase;
51 friend class SparkMax;
52 friend class SparkFlex;
53 friend class SparkAnalogSensor;
55 friend class SparkLimitSwitch;
63 friend class SparkLimitSwitchSim;
67
68 // Defined in HIL tester source
69 friend class ::ConfigBase;
70
71public:
72 static const uint16_t kAPIMajorVersion;
73 static const uint8_t kAPIMinorVersion;
74 static const uint8_t kAPIBuildVersion;
75 static const uint32_t kAPIVersion;
76
77 enum class MotorType { kBrushed = 0, kBrushless = 1 };
78
79 enum class ControlType {
80 kDutyCycle = 0,
81 kVelocity = 1,
82 kVoltage = 2,
83 kPosition = 3,
84 kCurrent = 4,
87 };
88
89 enum class ParameterStatus {
90 kOK = 0,
91 kInvalidID = 1,
92 kMismatchType = 2,
93 kAccessMode = 3,
94 kInvalid = 4,
96 };
97
98 enum class PeriodicFrame {
99 kStatus0 = 0,
100 kStatus1 = 1,
101 kStatus2 = 2,
102 kStatus3 = 3,
103 kStatus4 = 4,
104 kStatus5 = 5,
105 kStatus6 = 6,
106 kStatus7 = 7,
107 kStatus8 = 8,
108 kStatus9 = 9,
109 };
110
113 double voltage;
114 double current;
122 uint64_t timestamp;
123 };
124
159 uint64_t timestamp;
160 };
161
165 uint64_t timestamp;
166 };
167
172 uint64_t timestamp;
173 };
174
178 uint64_t timestamp;
179 };
180
184 uint64_t timestamp;
185 };
186
191 uint64_t timestamp;
192 };
193
196 uint64_t timestamp;
197 };
198
200 double setpoint;
203 uint64_t timestamp;
204 };
208 uint64_t timestamp;
209 };
210
211 enum class SparkModel {
212 kUnknown = 0,
213 kSparkFlex = 1,
214 kSparkMax = 2,
215 };
216
220 virtual ~SparkLowLevel();
221
229 uint32_t GetFirmwareVersion();
230
231 uint32_t GetFirmwareVersion(bool& isDebugBuild);
232
239 std::string GetFirmwareString();
240
248 std::vector<uint8_t> GetSerialNumber();
249
256 int GetBusId() const;
257
264 int GetDeviceId() const;
265
273
282
291
300
309
318
327
336
345
354
363
377 void SetPeriodicFrameTimeout(int timeoutMs);
378
393 void SetCANMaxRetries(int numRetries);
394
404 void SetControlFramePeriodMs(int periodMs);
405
410
411protected:
415 double arbFeedforward = 0, int arbFFUnits = 0);
416
417 float GetSafeFloat(float f);
418
421 // The type is void* because we don't want to expose c_Spark_handle to
422 // the consumers of this header file
424
425private:
426 SparkLowLevel(int busID, int deviceID, MotorType type, SparkModel model);
427
428 int m_busID;
429 int m_deviceID;
430};
431
432} // namespace rev::spark
Definition: SparkAbsoluteEncoder.h:43
Definition: SparkAbsoluteEncoderSim.h:43
Definition: SparkAnalogSensor.h:41
Definition: SparkAnalogSensorSim.h:43
Definition: SparkBase.h:51
Definition: SparkClosedLoopController.h:40
Definition: SparkExternalEncoderSim.h:42
Definition: SparkFlexExternalEncoder.h:39
Definition: SparkFlex.h:37
Definition: SparkLimitSwitch.h:38
Definition: SparkLimitSwitchSim.h:43
Definition: SparkLowLevel.h:49
std::string GetFirmwareString()
Definition: SparkLowLevel.cpp:108
ParameterStatus
Definition: SparkLowLevel.h:89
rev::util::Signal< PeriodicStatus8 > GetPeriodicStatus8() const
Definition: SparkLowLevel.cpp:306
void SetControlFramePeriodMs(int periodMs)
Definition: SparkLowLevel.cpp:141
static const uint16_t kAPIMajorVersion
Definition: SparkLowLevel.h:72
rev::util::Signal< PeriodicStatus7 > GetPeriodicStatus7() const
Definition: SparkLowLevel.cpp:292
void SetPeriodicFrameTimeout(int timeoutMs)
Definition: SparkLowLevel.cpp:131
MotorType
Definition: SparkLowLevel.h:77
rev::util::Signal< PeriodicStatus2 > GetPeriodicStatus2() const
Definition: SparkLowLevel.cpp:215
SparkModel m_expectedSparkModel
Definition: SparkLowLevel.h:420
static const uint8_t kAPIMinorVersion
Definition: SparkLowLevel.h:73
rev::util::Signal< PeriodicStatus3 > GetPeriodicStatus3() const
Definition: SparkLowLevel.cpp:230
rev::util::Signal< PeriodicStatus5 > GetPeriodicStatus5() const
Definition: SparkLowLevel.cpp:261
float GetSafeFloat(float f)
Definition: SparkLowLevel.cpp:349
rev::util::Signal< PeriodicStatus6 > GetPeriodicStatus6() const
Definition: SparkLowLevel.cpp:276
MotorType m_motorType
Definition: SparkLowLevel.h:419
int GetBusId() const
Definition: SparkLowLevel.cpp:125
rev::util::Signal< PeriodicStatus9 > GetPeriodicStatus9() const
Definition: SparkLowLevel.cpp:323
REVLibError SetpointCommand(double value, SparkLowLevel::ControlType ctrl=ControlType::kDutyCycle, ClosedLoopSlot pidSlot=ClosedLoopSlot::kSlot0, double arbFeedforward=0, int arbFFUnits=0)
Definition: SparkLowLevel.cpp:338
std::vector< uint8_t > GetSerialNumber()
Definition: SparkLowLevel.cpp:123
void * m_sparkMaxHandle
Definition: SparkLowLevel.h:423
rev::util::Signal< PeriodicStatus1 > GetPeriodicStatus1() const
Definition: SparkLowLevel.cpp:169
static const uint32_t kAPIVersion
Definition: SparkLowLevel.h:75
SparkModel
Definition: SparkLowLevel.h:211
rev::util::Signal< PeriodicStatus0 > GetPeriodicStatus0() const
Definition: SparkLowLevel.cpp:146
void SetCANMaxRetries(int numRetries)
Definition: SparkLowLevel.cpp:136
virtual ~SparkLowLevel()
Definition: SparkLowLevel.cpp:88
uint32_t GetFirmwareVersion()
Definition: SparkLowLevel.cpp:93
MotorType GetMotorType()
Definition: SparkLowLevel.cpp:129
static const uint8_t kAPIBuildVersion
Definition: SparkLowLevel.h:74
rev::util::Signal< PeriodicStatus4 > GetPeriodicStatus4() const
Definition: SparkLowLevel.cpp:246
int GetDeviceId() const
Definition: SparkLowLevel.cpp:127
PeriodicFrame
Definition: SparkLowLevel.h:98
void CreateSimFaultManager()
Definition: SparkLowLevel.cpp:354
ControlType
Definition: SparkLowLevel.h:79
Definition: SparkMaxAlternateEncoder.h:43
Definition: SparkMaxAlternateEncoderSim.h:42
Definition: SparkMax.h:40
Definition: SparkRelativeEncoder.h:43
Definition: SparkRelativeEncoderSim.h:43
Definition: SparkSimFaultManager.h:43
Definition: Signal.h:42
Definition: AnalogInput.h:35
ClosedLoopSlot
Definition: ClosedLoopTypes.h:35
@ kSlot0
Definition: ClosedLoopTypes.h:35
REVLibError
Definition: REVLibError.h:33
Definition: SparkLowLevel.h:111
bool inverted
Definition: SparkLowLevel.h:120
bool hardForwardLimitReached
Definition: SparkLowLevel.h:116
bool softForwardLimitReached
Definition: SparkLowLevel.h:118
uint64_t timestamp
Definition: SparkLowLevel.h:122
double current
Definition: SparkLowLevel.h:114
double appliedOutput
Definition: SparkLowLevel.h:112
double voltage
Definition: SparkLowLevel.h:113
bool primaryHeartbeatLock
Definition: SparkLowLevel.h:121
bool hardReverseLimitReached
Definition: SparkLowLevel.h:117
bool softReverseLimitReached
Definition: SparkLowLevel.h:119
uint8_t motorTemperature
Definition: SparkLowLevel.h:115
Definition: SparkLowLevel.h:125
bool extEepromWarning
Definition: SparkLowLevel.h:137
bool brownoutStickyWarning
Definition: SparkLowLevel.h:150
bool motorTypeStickyFault
Definition: SparkLowLevel.h:143
bool drvStickyFault
Definition: SparkLowLevel.h:147
bool isFollower
Definition: SparkLowLevel.h:158
bool escEepromStickyWarning
Definition: SparkLowLevel.h:152
bool firmwareFault
Definition: SparkLowLevel.h:133
bool sensorStickyFault
Definition: SparkLowLevel.h:144
bool sensorWarning
Definition: SparkLowLevel.h:138
uint64_t timestamp
Definition: SparkLowLevel.h:159
bool stallStickyWarning
Definition: SparkLowLevel.h:155
bool hasResetStickyWarning
Definition: SparkLowLevel.h:156
bool brownoutWarning
Definition: SparkLowLevel.h:134
bool firmwareStickyFault
Definition: SparkLowLevel.h:149
bool overcurrentWarning
Definition: SparkLowLevel.h:135
bool canFault
Definition: SparkLowLevel.h:129
bool stallWarning
Definition: SparkLowLevel.h:139
bool otherFault
Definition: SparkLowLevel.h:126
bool hasResetWarning
Definition: SparkLowLevel.h:140
bool sensorFault
Definition: SparkLowLevel.h:128
bool overcurrentStickyWarning
Definition: SparkLowLevel.h:151
bool canStickyFault
Definition: SparkLowLevel.h:145
bool drvFault
Definition: SparkLowLevel.h:131
bool otherStickyFault
Definition: SparkLowLevel.h:142
bool escEepromFault
Definition: SparkLowLevel.h:132
bool escEepromStickyFault
Definition: SparkLowLevel.h:148
bool escEepromWarning
Definition: SparkLowLevel.h:136
bool otherStickyWarning
Definition: SparkLowLevel.h:157
bool otherWarning
Definition: SparkLowLevel.h:141
bool extEepromStickyWarning
Definition: SparkLowLevel.h:153
bool sensorStickyWarning
Definition: SparkLowLevel.h:154
bool temperatureStickyFault
Definition: SparkLowLevel.h:146
bool temperatureFault
Definition: SparkLowLevel.h:130
bool motorTypeFault
Definition: SparkLowLevel.h:127
Definition: SparkLowLevel.h:162
double primaryEncoderPosition
Definition: SparkLowLevel.h:164
double primaryEncoderVelocity
Definition: SparkLowLevel.h:163
uint64_t timestamp
Definition: SparkLowLevel.h:165
Definition: SparkLowLevel.h:168
uint64_t timestamp
Definition: SparkLowLevel.h:172
double analogVoltage
Definition: SparkLowLevel.h:169
double analogVelocity
Definition: SparkLowLevel.h:170
double analogPosition
Definition: SparkLowLevel.h:171
Definition: SparkLowLevel.h:175
uint64_t timestamp
Definition: SparkLowLevel.h:178
double externalOrAltEncoderPosition
Definition: SparkLowLevel.h:177
double externalOrAltEncoderVelocity
Definition: SparkLowLevel.h:176
Definition: SparkLowLevel.h:181
double dutyCycleEncoderVelocity
Definition: SparkLowLevel.h:182
uint64_t timestamp
Definition: SparkLowLevel.h:184
double dutyCycleEncoderPosition
Definition: SparkLowLevel.h:183
Definition: SparkLowLevel.h:187
bool dutyCycleNoSignal
Definition: SparkLowLevel.h:190
uint64_t timestamp
Definition: SparkLowLevel.h:191
double dutyCyclePeriod
Definition: SparkLowLevel.h:189
double unadjustedDutyCycle
Definition: SparkLowLevel.h:188
Definition: SparkLowLevel.h:194
double iAccumulation
Definition: SparkLowLevel.h:195
uint64_t timestamp
Definition: SparkLowLevel.h:196
Definition: SparkLowLevel.h:199
bool isAtSetpoint
Definition: SparkLowLevel.h:201
double setpoint
Definition: SparkLowLevel.h:200
uint64_t timestamp
Definition: SparkLowLevel.h:203
ClosedLoopSlot selectedPidSlot
Definition: SparkLowLevel.h:202
Definition: SparkLowLevel.h:205
double maxMotionSetpointVelocity
Definition: SparkLowLevel.h:207
double maxMotionSetpointPosition
Definition: SparkLowLevel.h:206
uint64_t timestamp
Definition: SparkLowLevel.h:208