REVLib - C++
SparkBase.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#ifdef _MSC_VER
32// Disable deprecation warnings for this file when using VS compiler
33#pragma warning(disable : 4996)
34#endif
35
36#ifdef __GNUC__
37#pragma GCC diagnostic push
38#pragma GCC diagnostic ignored "-Wdeprecated"
39#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
40#endif
41
42#include "rev/REVLibError.h"
47#include "rev/SparkLowLevel.h"
49
50// Defined in HIL tester source
51class ConfigBase;
52
53namespace rev::spark {
54
55class SparkBaseConfig;
56
57class SparkBase : public SparkLowLevel {
58 friend class SparkMax;
59 friend class SparkFlex;
60 friend class SparkLimitSwitch;
64 friend class SparkSim;
65
66 // Defined in HIL tester source
67 friend class ::ConfigBase;
68
69public:
70 enum class IdleMode { kCoast = 0, kBrake = 1 };
71
73
75
77
78 struct Faults {
79 bool other;
81 bool sensor;
82 bool can;
87
88 Faults() {}
89
90 explicit Faults(uint16_t faults) {
91 other = (faults & 0x1) != 0;
92 motorType = (faults & 0x2) != 0;
93 sensor = (faults & 0x4) != 0;
94 can = (faults & 0x8) != 0;
95 temperature = (faults & 0x10) != 0;
96 gateDriver = (faults & 0x20) != 0;
97 escEeprom = (faults & 0x40) != 0;
98 firmware = (faults & 0x80) != 0;
99 }
100 };
101
102 struct Warnings {
107 bool sensor;
108 bool stall;
110 bool other;
111
113
114 explicit Warnings(uint16_t warnings) {
115 brownout = (warnings & 0x1) != 0;
116 overcurrent = (warnings & 0x2) != 0;
117 escEeprom = (warnings & 0x4) != 0;
118 extEeprom = (warnings & 0x8) != 0;
119 sensor = (warnings & 0x10) != 0;
120 stall = (warnings & 0x20) != 0;
121 hasReset = (warnings & 0x40) != 0;
122 other = (warnings & 0x80) != 0;
123 }
124 };
125
129 ~SparkBase() override = default;
130
131 /**** Speed Controller Interface ****/
137 void Set(double speed) override;
138
149 void SetVoltage(units::volt_t output) override;
150
156 double Get() const override;
157
166 void SetInverted(bool isInverted) override;
167
175 bool GetInverted() const override;
176
180 void Disable() override;
181
185 void StopMotor() override;
186
210 virtual REVLibError Configure(SparkBaseConfig& config, ResetMode resetMode,
211 PersistMode persistMode);
212
219
226
233
239
247
255
267
277
284 bool IsFollower();
285
293 bool HasActiveFault();
294
301 bool HasStickyFault();
302
309 bool HasActiveWarning();
310
317 bool HasStickyWarning();
318
326
337
345
356
360 double GetBusVoltage();
361
369 double GetAppliedOutput();
370
374 double GetOutputCurrent();
375
379 double GetMotorTemperature();
380
387
395 REVLibError SetCANTimeout(int milliseconds);
396
406
407protected:
408 // Only used for Get() or Set() API
409 double m_setpoint{0.0};
410
420 explicit SparkBase(int deviceID, MotorType type, SparkModel model);
421
422 // Used by the HIL tester
424
429 uint8_t GetMotorInterface();
430
438
439protected:
441 std::atomic<bool> m_relativeEncoderCreated{false};
442
444 std::atomic<bool> m_analogSensorCreated{false};
445
447 std::atomic<bool> m_absoluteEncoderCreated{false};
448
450 std::atomic<bool> m_closedLoopControllerCreated{false};
451
453 std::atomic<bool> m_forwardLimitSwitchCreated{false};
454
456 std::atomic<bool> m_reverseLimitSwitchCreated{false};
457};
458
459} // namespace rev::spark
460
461#ifdef __GNUC__
462#pragma GCC diagnostic pop
463#endif
Definition: SparkAbsoluteEncoder.h:44
Definition: SparkAnalogSensor.h:51
Definition: SparkBaseConfig.h:46
Definition: SparkBase.h:57
bool HasStickyWarning()
Definition: SparkBase.cpp:188
SparkRelativeEncoder & GetEncoder()
Definition: SparkBase.cpp:104
uint8_t GetMotorInterface()
Definition: SparkBase.cpp:134
Warnings GetStickyWarnings()
Definition: SparkBase.cpp:218
std::atomic< bool > m_absoluteEncoderCreated
Definition: SparkBase.h:447
IdleMode
Definition: SparkBase.h:70
bool IsFollower()
Definition: SparkBase.cpp:161
REVLibError SetCANTimeout(int milliseconds)
Definition: SparkBase.cpp:259
SoftLimitDirection
Definition: SparkBase.h:72
virtual SparkAbsoluteEncoder & GetAbsoluteEncoder()
Definition: SparkBase.cpp:114
Faults GetStickyFaults()
Definition: SparkBase.cpp:202
std::atomic< bool > m_relativeEncoderCreated
Definition: SparkBase.h:441
std::atomic< bool > m_forwardLimitSwitchCreated
Definition: SparkBase.h:453
~SparkBase() override=default
double GetAppliedOutput()
Definition: SparkBase.cpp:232
SparkClosedLoopController & GetClosedLoopController()
Definition: SparkBase.cpp:119
std::atomic< bool > m_reverseLimitSwitchCreated
Definition: SparkBase.h:456
bool HasActiveFault()
Definition: SparkBase.cpp:168
SparkLimitSwitch m_ReverseLimitSwitch
Definition: SparkBase.h:455
Warnings GetWarnings()
Definition: SparkBase.cpp:210
Faults GetFaults()
Definition: SparkBase.cpp:195
std::atomic< bool > m_analogSensorCreated
Definition: SparkBase.h:444
bool HasStickyFault()
Definition: SparkBase.cpp:174
std::atomic< bool > m_closedLoopControllerCreated
Definition: SparkBase.h:450
SparkClosedLoopController m_ClosedLoopController
Definition: SparkBase.h:449
void Disable() override
Definition: SparkBase.cpp:85
double Get() const override
Definition: SparkBase.cpp:71
REVLibError ClearFaults()
Definition: SparkBase.cpp:253
SparkRelativeEncoder GetEncoderEvenIfAlreadyCreated()
Definition: SparkBase.cpp:271
double m_setpoint
Definition: SparkBase.h:409
ResetMode
Definition: SparkBase.h:74
double GetOutputCurrent()
Definition: SparkBase.cpp:239
rev::REVLibError PauseFollowerMode()
Definition: SparkBase.cpp:155
void SetInverted(bool isInverted) override
Definition: SparkBase.cpp:73
virtual REVLibError Configure(SparkBaseConfig &config, ResetMode resetMode, PersistMode persistMode)
Definition: SparkBase.cpp:89
SparkAnalogSensor m_AnalogSensor
Definition: SparkBase.h:443
REVLibError GetLastError()
Definition: SparkBase.cpp:265
bool GetInverted() const override
Definition: SparkBase.cpp:78
void Set(double speed) override
Definition: SparkBase.cpp:56
virtual SparkLimitSwitch & GetReverseLimitSwitch()
Definition: SparkBase.cpp:129
SparkBase::SparkModel GetSparkModel()
Definition: SparkBase.cpp:142
SparkAnalogSensor & GetAnalog()
Definition: SparkBase.cpp:109
rev::REVLibError ResumeFollowerMode()
Definition: SparkBase.cpp:149
SparkAbsoluteEncoder m_AbsoluteEncoder
Definition: SparkBase.h:446
bool HasActiveWarning()
Definition: SparkBase.cpp:181
void SetVoltage(units::volt_t output) override
Definition: SparkBase.cpp:62
SparkRelativeEncoder m_RelativeEncoder
Definition: SparkBase.h:440
double GetBusVoltage()
Definition: SparkBase.cpp:226
PersistMode
Definition: SparkBase.h:76
double GetMotorTemperature()
Definition: SparkBase.cpp:246
SparkLimitSwitch m_ForwardLimitSwitch
Definition: SparkBase.h:452
virtual SparkLimitSwitch & GetForwardLimitSwitch()
Definition: SparkBase.cpp:124
void StopMotor() override
Definition: SparkBase.cpp:87
Definition: SparkClosedLoopController.h:51
Definition: SparkFlex.h:37
Definition: SparkLimitSwitch.h:48
Definition: SparkLowLevel.h:49
MotorType
Definition: SparkLowLevel.h:77
friend class SparkBase
Definition: SparkLowLevel.h:50
SparkModel
Definition: SparkLowLevel.h:209
Definition: SparkMaxAlternateEncoder.h:46
Definition: SparkMax.h:37
Definition: SparkRelativeEncoder.h:46
Definition: SparkSim.h:48
Definition: SparkLowLevel.cpp:40
REVLibError
Definition: REVLibError.h:33
Definition: SparkBase.h:78
Faults()
Definition: SparkBase.h:88
bool motorType
Definition: SparkBase.h:80
bool other
Definition: SparkBase.h:79
Faults(uint16_t faults)
Definition: SparkBase.h:90
bool temperature
Definition: SparkBase.h:83
bool can
Definition: SparkBase.h:82
bool gateDriver
Definition: SparkBase.h:84
bool escEeprom
Definition: SparkBase.h:85
bool sensor
Definition: SparkBase.h:81
bool firmware
Definition: SparkBase.h:86
Definition: SparkBase.h:102
bool extEeprom
Definition: SparkBase.h:106
bool escEeprom
Definition: SparkBase.h:105
bool overcurrent
Definition: SparkBase.h:104
bool hasReset
Definition: SparkBase.h:109
bool sensor
Definition: SparkBase.h:107
Warnings()
Definition: SparkBase.h:112
bool other
Definition: SparkBase.h:110
Warnings(uint16_t warnings)
Definition: SparkBase.h:114
bool stall
Definition: SparkBase.h:108
bool brownout
Definition: SparkBase.h:103