REVLib - C++
SparkLowLevel.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
40#include "rev/REVLibError.h"
41
42// Defined in HIL tester source
43class ConfigBase;
44
45namespace rev::spark {
46
47class SparkBase;
48
49class SparkLowLevel : public frc::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 uint8_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,
89 [[deprecated("We recommend migrating to MAXMotion instead.")]] = 4,
90 kCurrent = 5,
96 [[deprecated("We recommend migrating to MAXMotion instead.")]] = 6,
99 };
100
101 enum class ParameterStatus {
102 kOK = 0,
103 kInvalidID = 1,
104 kMismatchType = 2,
105 kAccessMode = 3,
106 kInvalid = 4,
108 };
109
110 enum class PeriodicFrame {
111 kStatus0 = 0,
112 kStatus1 = 1,
113 kStatus2 = 2,
114 kStatus3 = 3,
115 kStatus4 = 4,
116 kStatus5 = 5,
117 kStatus6 = 6,
118 kStatus7 = 7,
119 };
120
123 double voltage;
124 double current;
132 uint64_t timestamp;
133 };
134
169 uint64_t timestamp;
170 };
171
175 uint64_t timestamp;
176 };
177
182 uint64_t timestamp;
183 };
184
188 uint64_t timestamp;
189 };
190
194 uint64_t timestamp;
195 };
196
201 uint64_t timestamp;
202 };
203
206 uint64_t timestamp;
207 };
208
209 enum class SparkModel {
210 kSparkMax = 0,
211 kSparkFlex = 1,
212 kUnknown = 255,
213 };
214
218 virtual ~SparkLowLevel();
219
227 uint32_t GetFirmwareVersion();
228
229 uint32_t GetFirmwareVersion(bool& isDebugBuild);
230
237 std::string GetFirmwareString();
238
246 std::vector<uint8_t> GetSerialNumber();
247
254 int GetDeviceId() const;
255
263
277 void SetPeriodicFrameTimeout(int timeoutMs);
278
298 void SetCANMaxRetries(int numRetries);
299
309 void SetControlFramePeriodMs(int periodMs);
310
315
316protected:
317 PeriodicStatus0 GetPeriodicStatus0();
318 PeriodicStatus1 GetPeriodicStatus1();
319 PeriodicStatus2 GetPeriodicStatus2();
320 PeriodicStatus3 GetPeriodicStatus3();
321 PeriodicStatus4 GetPeriodicStatus4();
322 PeriodicStatus5 GetPeriodicStatus5();
323 PeriodicStatus6 GetPeriodicStatus6();
324 PeriodicStatus7 GetPeriodicStatus7();
325
328 int pidSlot = 0, double arbFeedforward = 0, int arbFFUnits = 0);
329
330 float GetSafeFloat(float f);
331
334 // The type is void* because we don't want to expose c_Spark_handle to
335 // the consumers of this header file
337
338private:
339 explicit SparkLowLevel(int deviceID, MotorType type, SparkModel model);
340
341 int m_deviceID;
342};
343
344} // namespace rev::spark
Definition: SparkAbsoluteEncoder.h:44
Definition: SparkAbsoluteEncoderSim.h:43
Definition: SparkAnalogSensor.h:51
Definition: SparkAnalogSensorSim.h:43
Definition: SparkBase.h:57
Definition: SparkClosedLoopController.h:51
Definition: SparkExternalEncoderSim.h:42
Definition: SparkFlexExternalEncoder.h:42
Definition: SparkFlex.h:37
Definition: SparkLimitSwitch.h:48
Definition: SparkLimitSwitchSim.h:43
Definition: SparkLowLevel.h:49
static const uint8_t kAPIMinorVersion
Definition: SparkLowLevel.h:73
ParameterStatus
Definition: SparkLowLevel.h:101
MotorType GetMotorType()
Definition: SparkLowLevel.cpp:121
MotorType
Definition: SparkLowLevel.h:77
SparkModel m_expectedSparkModel
Definition: SparkLowLevel.h:333
void SetControlFramePeriodMs(int periodMs)
Definition: SparkLowLevel.cpp:133
float GetSafeFloat(float f)
Definition: SparkLowLevel.cpp:301
MotorType m_motorType
Definition: SparkLowLevel.h:332
PeriodicStatus2 GetPeriodicStatus2()
Definition: SparkLowLevel.cpp:209
virtual ~SparkLowLevel()
Definition: SparkLowLevel.cpp:83
PeriodicStatus4 GetPeriodicStatus4()
Definition: SparkLowLevel.cpp:236
static const uint8_t kAPIMajorVersion
Definition: SparkLowLevel.h:72
PeriodicStatus3 GetPeriodicStatus3()
Definition: SparkLowLevel.cpp:222
PeriodicStatus0 GetPeriodicStatus0()
Definition: SparkLowLevel.cpp:138
void * m_sparkMaxHandle
Definition: SparkLowLevel.h:336
PeriodicStatus5 GetPeriodicStatus5()
Definition: SparkLowLevel.cpp:251
PeriodicStatus1 GetPeriodicStatus1()
Definition: SparkLowLevel.cpp:165
void SetPeriodicFrameTimeout(int timeoutMs)
Definition: SparkLowLevel.cpp:123
std::string GetFirmwareString()
Definition: SparkLowLevel.cpp:102
SparkModel
Definition: SparkLowLevel.h:209
std::vector< uint8_t > GetSerialNumber()
Definition: SparkLowLevel.cpp:117
PeriodicStatus6 GetPeriodicStatus6()
Definition: SparkLowLevel.cpp:264
static const uint32_t kAPIVersion
Definition: SparkLowLevel.h:75
void SetCANMaxRetries(int numRetries)
Definition: SparkLowLevel.cpp:128
void CreateSimFaultManager()
Definition: SparkLowLevel.cpp:306
static const uint8_t kAPIBuildVersion
Definition: SparkLowLevel.h:74
REVLibError SetpointCommand(double value, SparkLowLevel::ControlType ctrl=ControlType::kDutyCycle, int pidSlot=0, double arbFeedforward=0, int arbFFUnits=0)
Definition: SparkLowLevel.cpp:290
uint32_t GetFirmwareVersion()
Definition: SparkLowLevel.cpp:87
PeriodicFrame
Definition: SparkLowLevel.h:110
PeriodicStatus7 GetPeriodicStatus7()
Definition: SparkLowLevel.cpp:278
ControlType
Definition: SparkLowLevel.h:79
int GetDeviceId() const
Definition: SparkLowLevel.cpp:119
Definition: SparkMaxAlternateEncoder.h:46
Definition: SparkMaxAlternateEncoderSim.h:42
Definition: SparkMax.h:37
Definition: SparkRelativeEncoder.h:46
Definition: SparkRelativeEncoderSim.h:43
Definition: SparkSimFaultManager.h:43
Definition: SparkLowLevel.cpp:40
REVLibError
Definition: REVLibError.h:33
Definition: SparkLowLevel.h:121
bool inverted
Definition: SparkLowLevel.h:130
bool hardForwardLimitReached
Definition: SparkLowLevel.h:126
bool softForwardLimitReached
Definition: SparkLowLevel.h:128
uint64_t timestamp
Definition: SparkLowLevel.h:132
double current
Definition: SparkLowLevel.h:124
double appliedOutput
Definition: SparkLowLevel.h:122
double voltage
Definition: SparkLowLevel.h:123
bool primaryHeartbeatLock
Definition: SparkLowLevel.h:131
bool hardReverseLimitReached
Definition: SparkLowLevel.h:127
bool softReverseLimitReached
Definition: SparkLowLevel.h:129
uint8_t motorTemperature
Definition: SparkLowLevel.h:125
Definition: SparkLowLevel.h:135
bool extEepromWarning
Definition: SparkLowLevel.h:147
bool brownoutStickyWarning
Definition: SparkLowLevel.h:160
bool motorTypeStickyFault
Definition: SparkLowLevel.h:153
bool drvStickyFault
Definition: SparkLowLevel.h:157
bool isFollower
Definition: SparkLowLevel.h:168
bool escEepromStickyWarning
Definition: SparkLowLevel.h:162
bool firmwareFault
Definition: SparkLowLevel.h:143
bool sensorStickyFault
Definition: SparkLowLevel.h:154
bool sensorWarning
Definition: SparkLowLevel.h:148
uint64_t timestamp
Definition: SparkLowLevel.h:169
bool stallStickyWarning
Definition: SparkLowLevel.h:165
bool hasResetStickyWarning
Definition: SparkLowLevel.h:166
bool brownoutWarning
Definition: SparkLowLevel.h:144
bool firmwareStickyFault
Definition: SparkLowLevel.h:159
bool overcurrentWarning
Definition: SparkLowLevel.h:145
bool canFault
Definition: SparkLowLevel.h:139
bool stallWarning
Definition: SparkLowLevel.h:149
bool otherFault
Definition: SparkLowLevel.h:136
bool hasResetWarning
Definition: SparkLowLevel.h:150
bool sensorFault
Definition: SparkLowLevel.h:138
bool overcurrentStickyWarning
Definition: SparkLowLevel.h:161
bool canStickyFault
Definition: SparkLowLevel.h:155
bool drvFault
Definition: SparkLowLevel.h:141
bool otherStickyFault
Definition: SparkLowLevel.h:152
bool escEepromFault
Definition: SparkLowLevel.h:142
bool escEepromStickyFault
Definition: SparkLowLevel.h:158
bool escEepromWarning
Definition: SparkLowLevel.h:146
bool otherStickyWarning
Definition: SparkLowLevel.h:167
bool otherWarning
Definition: SparkLowLevel.h:151
bool extEepromStickyWarning
Definition: SparkLowLevel.h:163
bool sensorStickyWarning
Definition: SparkLowLevel.h:164
bool temperatureStickyFault
Definition: SparkLowLevel.h:156
bool temperatureFault
Definition: SparkLowLevel.h:140
bool motorTypeFault
Definition: SparkLowLevel.h:137
Definition: SparkLowLevel.h:172
double primaryEncoderPosition
Definition: SparkLowLevel.h:174
double primaryEncoderVelocity
Definition: SparkLowLevel.h:173
uint64_t timestamp
Definition: SparkLowLevel.h:175
Definition: SparkLowLevel.h:178
uint64_t timestamp
Definition: SparkLowLevel.h:182
double analogVoltage
Definition: SparkLowLevel.h:179
double analogVelocity
Definition: SparkLowLevel.h:180
double analogPosition
Definition: SparkLowLevel.h:181
Definition: SparkLowLevel.h:185
uint64_t timestamp
Definition: SparkLowLevel.h:188
double externalOrAltEncoderPosition
Definition: SparkLowLevel.h:187
double externalOrAltEncoderVelocity
Definition: SparkLowLevel.h:186
Definition: SparkLowLevel.h:191
double dutyCycleEncoderVelocity
Definition: SparkLowLevel.h:192
uint64_t timestamp
Definition: SparkLowLevel.h:194
double dutyCycleEncoderPosition
Definition: SparkLowLevel.h:193
Definition: SparkLowLevel.h:197
uint64_t timestamp
Definition: SparkLowLevel.h:201
double dutyCyclePeriod
Definition: SparkLowLevel.h:199
double unadjustedDutyCycle
Definition: SparkLowLevel.h:198
uint8_t dutyCycleNoSignal
Definition: SparkLowLevel.h:200
Definition: SparkLowLevel.h:204
double iAccumulation
Definition: SparkLowLevel.h:205
uint64_t timestamp
Definition: SparkLowLevel.h:206