REVLib - C++
Loading...
Searching...
No Matches
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/bus/CANPort.hpp>
37#include <wpi/hardware/motor/MotorController.hpp>
38
39#include "rev/ClosedLoopTypes.h"
40#include "rev/REVLibError.h"
41#include "rev/util/Signal.h"
42
43// Defined in HIL tester source
44class ConfigBase;
45
46namespace rev::spark {
47
48class SparkBase;
49
50class SparkLowLevel : public wpi::MotorController {
51 friend class SparkBase;
52 friend class SparkMax;
53 friend class SparkFlex;
54 friend class SparkAnalogSensor;
56 friend class SparkLimitSwitch;
64 friend class SparkLimitSwitchSim;
68
69 // Defined in HIL tester source
70 friend class ::ConfigBase;
71
72public:
73 static const uint16_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 kCurrent = 4,
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 kStatus8 = 8,
109 kStatus9 = 9,
110 };
111
125
162
168
175
181
187
194
197 uint64_t timestamp;
198 };
199
211
212 enum class SparkModel {
213 kUnknown = 0,
214 kSparkFlex = 1,
215 kSparkMax = 2,
216 };
217
221 virtual ~SparkLowLevel();
222
230 uint32_t GetFirmwareVersion();
231
232 uint32_t GetFirmwareVersion(bool& isDebugBuild);
233
240 std::string GetFirmwareString();
241
249 std::vector<uint8_t> GetSerialNumber();
250
257 wpi::CANPort GetCanPort() const;
258
265 int GetDeviceId() const;
266
274
283
292
301
310
319
328
337
346
355
364
378 void SetPeriodicFrameTimeout(int timeoutMs);
379
394 void SetCANMaxRetries(int numRetries);
395
405 void SetControlFramePeriodMs(int periodMs);
406
411
412protected:
416 double arbFeedforward = 0, int arbFFUnits = 0);
417
418 float GetSafeFloat(float f);
419
422 // The type is void* because we don't want to expose c_Spark_handle to
423 // the consumers of this header file
425
426private:
427 SparkLowLevel(wpi::CANPort canPort, int deviceID, MotorType type,
428 SparkModel model);
429
430 int m_busID;
431 int m_deviceID;
432};
433
434} // namespace rev::spark
Definition SparkAbsoluteEncoder.h:43
Definition SparkAbsoluteEncoderSim.h:43
Definition SparkAnalogSensor.h:41
Definition SparkAnalogSensorSim.h:43
Definition SparkBase.h:53
Definition SparkClosedLoopController.h:40
Definition SparkExternalEncoderSim.h:42
Definition SparkFlexExternalEncoder.h:39
Definition SparkFlex.h:39
Definition SparkLimitSwitch.h:38
Definition SparkLimitSwitchSim.h:43
Definition SparkLowLevel.h:50
std::string GetFirmwareString()
Definition SparkLowLevel.cpp:106
ParameterStatus
Definition SparkLowLevel.h:90
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:73
rev::util::Signal< PeriodicStatus7 > GetPeriodicStatus7() const
Definition SparkLowLevel.cpp:292
void SetPeriodicFrameTimeout(int timeoutMs)
Definition SparkLowLevel.cpp:131
MotorType
Definition SparkLowLevel.h:78
rev::util::Signal< PeriodicStatus2 > GetPeriodicStatus2() const
Definition SparkLowLevel.cpp:215
SparkModel m_expectedSparkModel
Definition SparkLowLevel.h:421
wpi::CANPort GetCanPort() const
Definition SparkLowLevel.cpp:123
static const uint8_t kAPIMinorVersion
Definition SparkLowLevel.h:74
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:420
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:121
void * m_sparkMaxHandle
Definition SparkLowLevel.h:424
rev::util::Signal< PeriodicStatus1 > GetPeriodicStatus1() const
Definition SparkLowLevel.cpp:169
static const uint32_t kAPIVersion
Definition SparkLowLevel.h:76
SparkModel
Definition SparkLowLevel.h:212
rev::util::Signal< PeriodicStatus0 > GetPeriodicStatus0() const
Definition SparkLowLevel.cpp:146
void SetCANMaxRetries(int numRetries)
Definition SparkLowLevel.cpp:136
virtual ~SparkLowLevel()
Definition SparkLowLevel.cpp:86
uint32_t GetFirmwareVersion()
Definition SparkLowLevel.cpp:91
MotorType GetMotorType()
Definition SparkLowLevel.cpp:129
static const uint8_t kAPIBuildVersion
Definition SparkLowLevel.h:75
rev::util::Signal< PeriodicStatus4 > GetPeriodicStatus4() const
Definition SparkLowLevel.cpp:246
int GetDeviceId() const
Definition SparkLowLevel.cpp:127
PeriodicFrame
Definition SparkLowLevel.h:99
void CreateSimFaultManager()
Definition SparkLowLevel.cpp:354
ControlType
Definition SparkLowLevel.h:80
Definition SparkMaxAlternateEncoder.h:43
Definition SparkMaxAlternateEncoderSim.h:42
Definition SparkMax.h:42
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:112
bool inverted
Definition SparkLowLevel.h:121
bool hardForwardLimitReached
Definition SparkLowLevel.h:117
bool softForwardLimitReached
Definition SparkLowLevel.h:119
uint64_t timestamp
Definition SparkLowLevel.h:123
double current
Definition SparkLowLevel.h:115
double appliedOutput
Definition SparkLowLevel.h:113
double voltage
Definition SparkLowLevel.h:114
bool primaryHeartbeatLock
Definition SparkLowLevel.h:122
bool hardReverseLimitReached
Definition SparkLowLevel.h:118
bool softReverseLimitReached
Definition SparkLowLevel.h:120
uint8_t motorTemperature
Definition SparkLowLevel.h:116
Definition SparkLowLevel.h:126
bool extEepromWarning
Definition SparkLowLevel.h:138
bool brownoutStickyWarning
Definition SparkLowLevel.h:151
bool motorTypeStickyFault
Definition SparkLowLevel.h:144
bool drvStickyFault
Definition SparkLowLevel.h:148
bool isFollower
Definition SparkLowLevel.h:159
bool escEepromStickyWarning
Definition SparkLowLevel.h:153
bool firmwareFault
Definition SparkLowLevel.h:134
bool sensorStickyFault
Definition SparkLowLevel.h:145
bool sensorWarning
Definition SparkLowLevel.h:139
uint64_t timestamp
Definition SparkLowLevel.h:160
bool stallStickyWarning
Definition SparkLowLevel.h:156
bool hasResetStickyWarning
Definition SparkLowLevel.h:157
bool brownoutWarning
Definition SparkLowLevel.h:135
bool firmwareStickyFault
Definition SparkLowLevel.h:150
bool overcurrentWarning
Definition SparkLowLevel.h:136
bool canFault
Definition SparkLowLevel.h:130
bool stallWarning
Definition SparkLowLevel.h:140
bool otherFault
Definition SparkLowLevel.h:127
bool hasResetWarning
Definition SparkLowLevel.h:141
bool sensorFault
Definition SparkLowLevel.h:129
bool overcurrentStickyWarning
Definition SparkLowLevel.h:152
bool canStickyFault
Definition SparkLowLevel.h:146
bool drvFault
Definition SparkLowLevel.h:132
bool otherStickyFault
Definition SparkLowLevel.h:143
bool escEepromFault
Definition SparkLowLevel.h:133
bool escEepromStickyFault
Definition SparkLowLevel.h:149
bool escEepromWarning
Definition SparkLowLevel.h:137
bool otherStickyWarning
Definition SparkLowLevel.h:158
bool otherWarning
Definition SparkLowLevel.h:142
bool extEepromStickyWarning
Definition SparkLowLevel.h:154
bool sensorStickyWarning
Definition SparkLowLevel.h:155
bool temperatureStickyFault
Definition SparkLowLevel.h:147
bool temperatureFault
Definition SparkLowLevel.h:131
bool motorTypeFault
Definition SparkLowLevel.h:128
Definition SparkLowLevel.h:163
double primaryEncoderPosition
Definition SparkLowLevel.h:165
double primaryEncoderVelocity
Definition SparkLowLevel.h:164
uint64_t timestamp
Definition SparkLowLevel.h:166
Definition SparkLowLevel.h:169
uint64_t timestamp
Definition SparkLowLevel.h:173
double analogVoltage
Definition SparkLowLevel.h:170
double analogVelocity
Definition SparkLowLevel.h:171
double analogPosition
Definition SparkLowLevel.h:172
Definition SparkLowLevel.h:176
uint64_t timestamp
Definition SparkLowLevel.h:179
double externalOrAltEncoderPosition
Definition SparkLowLevel.h:178
double externalOrAltEncoderVelocity
Definition SparkLowLevel.h:177
Definition SparkLowLevel.h:182
double dutyCycleEncoderVelocity
Definition SparkLowLevel.h:183
uint64_t timestamp
Definition SparkLowLevel.h:185
double dutyCycleEncoderPosition
Definition SparkLowLevel.h:184
Definition SparkLowLevel.h:188
bool dutyCycleNoSignal
Definition SparkLowLevel.h:191
uint64_t timestamp
Definition SparkLowLevel.h:192
double dutyCyclePeriod
Definition SparkLowLevel.h:190
double unadjustedDutyCycle
Definition SparkLowLevel.h:189
Definition SparkLowLevel.h:195
double iAccumulation
Definition SparkLowLevel.h:196
uint64_t timestamp
Definition SparkLowLevel.h:197
Definition SparkLowLevel.h:200
bool isAtSetpoint
Definition SparkLowLevel.h:202
double setpoint
Definition SparkLowLevel.h:201
uint64_t timestamp
Definition SparkLowLevel.h:204
ClosedLoopSlot selectedPidSlot
Definition SparkLowLevel.h:203
Definition SparkLowLevel.h:206
double maxMotionSetpointVelocity
Definition SparkLowLevel.h:208
double maxMotionSetpointPosition
Definition SparkLowLevel.h:207
uint64_t timestamp
Definition SparkLowLevel.h:209