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 <wpi/deprecated.h>
43
44#include "rev/REVLibError.h"
49#include "rev/SparkLowLevel.h"
51
52// Defined in HIL tester source
53class ConfigBase;
54
55namespace rev::spark {
56
57class SparkBaseConfig;
58
59class SparkBase : public SparkLowLevel {
60 friend class SparkMax;
61 friend class SparkFlex;
62 friend class SparkLimitSwitch;
66 friend class SparkSim;
67
68 // Defined in HIL tester source
69 friend class ::ConfigBase;
70
71public:
72 enum class IdleMode { kCoast = 0, kBrake = 1 };
73
75
77
79
80 struct Faults {
81 bool other;
83 bool sensor;
84 bool can;
90
91 Faults() {}
92
93 explicit Faults(uint16_t faults) {
94 rawBits = faults;
95 other = (faults & 0x1) != 0;
96 motorType = (faults & 0x2) != 0;
97 sensor = (faults & 0x4) != 0;
98 can = (faults & 0x8) != 0;
99 temperature = (faults & 0x10) != 0;
100 gateDriver = (faults & 0x20) != 0;
101 escEeprom = (faults & 0x40) != 0;
102 firmware = (faults & 0x80) != 0;
103 }
104 };
105
106 struct Warnings {
111 bool sensor;
112 bool stall;
114 bool other;
116
118
119 explicit Warnings(uint16_t warnings) {
120 rawBits = warnings;
121 brownout = (warnings & 0x1) != 0;
122 overcurrent = (warnings & 0x2) != 0;
123 escEeprom = (warnings & 0x4) != 0;
124 extEeprom = (warnings & 0x8) != 0;
125 sensor = (warnings & 0x10) != 0;
126 stall = (warnings & 0x20) != 0;
127 hasReset = (warnings & 0x40) != 0;
128 other = (warnings & 0x80) != 0;
129 }
130 };
131
135 ~SparkBase() override = default;
136
137 /**** Speed Controller Interface ****/
143 void Set(double speed) override;
144
155 void SetVoltage(units::volt_t output) override;
156
162 double Get() const override;
163
173 [[deprecated("Use SparkBaseConfig.Inverted() with Configure() instead")]]
174 void SetInverted(bool isInverted) override;
175
185 [[deprecated(
186 "Use SparkBaseConfigAccessor.GetInverted() via SparkMax.configAccessor "
187 "or SparkFlex.configAccessor instead")]]
188 bool GetInverted() const override;
189
193 void Disable() override;
194
198 void StopMotor() override;
199
200 /* ***** Extended Functions ****** */
201
223 virtual REVLibError Configure(SparkBaseConfig& config, ResetMode resetMode,
224 PersistMode persistMode);
225
253 ResetMode resetMode,
254 PersistMode persistMode);
255
262
269
276
282
290
298
310
328
338
353
360 bool IsFollower();
361
369 bool HasActiveFault();
370
377 bool HasStickyFault();
378
385 bool HasActiveWarning();
386
393 bool HasStickyWarning();
394
402
413
421
432
436 double GetBusVoltage();
437
445 double GetAppliedOutput();
446
450 double GetOutputCurrent();
451
455 double GetMotorTemperature();
456
463
469 REVLibError SetCANTimeout(int milliseconds);
470
480
481protected:
482 // Only used for Get() or Set() API
483 double m_setpoint{0.0};
484
494 explicit SparkBase(int deviceID, MotorType type, SparkModel model);
495
496 // Used by the HIL tester
498
503 uint8_t GetMotorInterface();
504
512
513protected:
515 std::atomic<bool> m_relativeEncoderCreated{false};
516
518 std::atomic<bool> m_analogSensorCreated{false};
519
521 std::atomic<bool> m_absoluteEncoderCreated{false};
522
524 std::atomic<bool> m_closedLoopControllerCreated{false};
525
527 std::atomic<bool> m_forwardLimitSwitchCreated{false};
528
530 std::atomic<bool> m_reverseLimitSwitchCreated{false};
531};
532
533} // namespace rev::spark
534
535#ifdef __GNUC__
536#pragma GCC diagnostic pop
537#endif
Definition: SparkAbsoluteEncoder.h:44
Definition: SparkAnalogSensor.h:51
Definition: SparkBaseConfig.h:46
Definition: SparkBase.h:59
bool HasStickyWarning()
Definition: SparkBase.cpp:227
SparkRelativeEncoder & GetEncoder()
Definition: SparkBase.cpp:131
uint8_t GetMotorInterface()
Definition: SparkBase.cpp:161
Warnings GetStickyWarnings()
Definition: SparkBase.cpp:257
std::atomic< bool > m_absoluteEncoderCreated
Definition: SparkBase.h:521
IdleMode
Definition: SparkBase.h:72
bool IsFollower()
Definition: SparkBase.cpp:200
rev::REVLibError PauseFollowerModeAsync()
Definition: SparkBase.cpp:194
REVLibError SetCANTimeout(int milliseconds)
Definition: SparkBase.cpp:298
SoftLimitDirection
Definition: SparkBase.h:74
virtual SparkAbsoluteEncoder & GetAbsoluteEncoder()
Definition: SparkBase.cpp:141
Faults GetStickyFaults()
Definition: SparkBase.cpp:241
std::atomic< bool > m_relativeEncoderCreated
Definition: SparkBase.h:515
std::atomic< bool > m_forwardLimitSwitchCreated
Definition: SparkBase.h:527
~SparkBase() override=default
double GetAppliedOutput()
Definition: SparkBase.cpp:271
SparkClosedLoopController & GetClosedLoopController()
Definition: SparkBase.cpp:146
std::atomic< bool > m_reverseLimitSwitchCreated
Definition: SparkBase.h:530
bool HasActiveFault()
Definition: SparkBase.cpp:207
SparkLimitSwitch m_ReverseLimitSwitch
Definition: SparkBase.h:529
Warnings GetWarnings()
Definition: SparkBase.cpp:249
Faults GetFaults()
Definition: SparkBase.cpp:234
std::atomic< bool > m_analogSensorCreated
Definition: SparkBase.h:518
bool HasStickyFault()
Definition: SparkBase.cpp:213
std::atomic< bool > m_closedLoopControllerCreated
Definition: SparkBase.h:524
SparkClosedLoopController m_ClosedLoopController
Definition: SparkBase.h:523
void Disable() override
Definition: SparkBase.cpp:92
double Get() const override
Definition: SparkBase.cpp:72
REVLibError ClearFaults()
Definition: SparkBase.cpp:292
SparkRelativeEncoder GetEncoderEvenIfAlreadyCreated()
Definition: SparkBase.cpp:310
double m_setpoint
Definition: SparkBase.h:483
ResetMode
Definition: SparkBase.h:76
double GetOutputCurrent()
Definition: SparkBase.cpp:278
rev::REVLibError PauseFollowerMode()
Definition: SparkBase.cpp:188
void SetInverted(bool isInverted) override
Definition: SparkBase.cpp:74
virtual REVLibError Configure(SparkBaseConfig &config, ResetMode resetMode, PersistMode persistMode)
Definition: SparkBase.cpp:96
SparkAnalogSensor m_AnalogSensor
Definition: SparkBase.h:517
REVLibError GetLastError()
Definition: SparkBase.cpp:304
bool GetInverted() const override
Definition: SparkBase.cpp:82
void Set(double speed) override
Definition: SparkBase.cpp:57
virtual SparkLimitSwitch & GetReverseLimitSwitch()
Definition: SparkBase.cpp:156
SparkBase::SparkModel GetSparkModel()
Definition: SparkBase.cpp:169
virtual REVLibError ConfigureAsync(SparkBaseConfig &config, ResetMode resetMode, PersistMode persistMode)
Definition: SparkBase.cpp:118
SparkAnalogSensor & GetAnalog()
Definition: SparkBase.cpp:136
rev::REVLibError ResumeFollowerMode()
Definition: SparkBase.cpp:176
SparkAbsoluteEncoder m_AbsoluteEncoder
Definition: SparkBase.h:520
bool HasActiveWarning()
Definition: SparkBase.cpp:220
void SetVoltage(units::volt_t output) override
Definition: SparkBase.cpp:63
SparkRelativeEncoder m_RelativeEncoder
Definition: SparkBase.h:514
double GetBusVoltage()
Definition: SparkBase.cpp:265
rev::REVLibError ResumeFollowerModeAsync()
Definition: SparkBase.cpp:182
PersistMode
Definition: SparkBase.h:78
double GetMotorTemperature()
Definition: SparkBase.cpp:285
SparkLimitSwitch m_ForwardLimitSwitch
Definition: SparkBase.h:526
virtual SparkLimitSwitch & GetForwardLimitSwitch()
Definition: SparkBase.cpp:151
void StopMotor() override
Definition: SparkBase.cpp:94
Definition: SparkClosedLoopController.h:52
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:80
int rawBits
Definition: SparkBase.h:89
Faults()
Definition: SparkBase.h:91
bool motorType
Definition: SparkBase.h:82
bool other
Definition: SparkBase.h:81
Faults(uint16_t faults)
Definition: SparkBase.h:93
bool temperature
Definition: SparkBase.h:85
bool can
Definition: SparkBase.h:84
bool gateDriver
Definition: SparkBase.h:86
bool escEeprom
Definition: SparkBase.h:87
bool sensor
Definition: SparkBase.h:83
bool firmware
Definition: SparkBase.h:88
Definition: SparkBase.h:106
bool extEeprom
Definition: SparkBase.h:110
bool escEeprom
Definition: SparkBase.h:109
bool overcurrent
Definition: SparkBase.h:108
bool hasReset
Definition: SparkBase.h:113
bool sensor
Definition: SparkBase.h:111
Warnings()
Definition: SparkBase.h:117
int rawBits
Definition: SparkBase.h:115
bool other
Definition: SparkBase.h:114
Warnings(uint16_t warnings)
Definition: SparkBase.h:119
bool stall
Definition: SparkBase.h:112
bool brownout
Definition: SparkBase.h:107