At the links below you will find code examples in LabVIEW, Java, and C++ for common SPARK MAX control modes. We will be adding examples as we develop them, so please check back regularly. Examples included as of 2/6/19:
Alternate Encoder
Arcade Drive with CAN
Arcade Drive with PWM
Bus Measurements
Get and Set Parameters
Limit Switch
Position Closed Loop Control
Read Encoder Values
Velocity Closed Loop Control
Alternate Encoder
Analog Feedback Device
Bus Measurements
Encoder Feedback Device
Get and Set Parameters
Limit Switch
Motor Follower
Position Closed Loop Control
Read Encoder Values
Smart Motion Example
Soft Limits
Tank Drive With CAN
Velocity Closed Loop Control
Alternate Encoder
Analog Feedback Device
Arcade Drive With CAN
Arcade Drive With PWM
Bus Measurements
Encoder Feedback Device
Get and Set Parameters
Limit Switch
Position Closed Loop Control
Read Encoder Values
Smart Motion Example
Soft Limits
Velocity Closed Loop Control
Even though some examples may only exist in a particular language, they can be a good place to start as the APIs are very similar between languages.
Below you will find information on how to download and install REVLib for LabVIEW, Java, and C++.
Beginning in 2022, The SPARK MAX API and the Color Sensor V3 API have been merged into a unified library called REVLib. You need to make sure that you have uninstalled those old libraries before you install REVLib.
Ensure that the old SPARK MAX API and/or Color Sensor V3 API packages are uninstalled.
Opening a project that used the old APIs should work fine. When it is loading, it may show that there are some missing VIs, but eventually LabVIEW should find the new VIs automatically.
Open your robot project in VSCode.
Click on the WPI icon in the corner to open the WPI Command Pallet.
Select Manage Vendor Libraries.
Select Manage Current Libraries.
Check the REVRobotics
and/or REVColorSensorV3
items (whichever ones are present)
Click the OK
button to uninstall them.
Install REVLib for C++ and Java.
Download the latest REVLib LabVIEW package from the download link above.
Make sure LabVIEW for FRC 2024 is installed and updated.
Open the REVLib LabVIEW Package. The NI Package Manager should automatically open.
Click Next:
Once the installation is complete, you will be able to access the REVLib VIs at LabVIEW Functions Pallet -> WPI Robotics Library -> Third Party -> REV Robotics.
You can use the online method to install REVLib C++/Java if your development machine is connected to the internet:
Open your robot project in VSCode.
Click on the WPI icon in the corner to open the WPI Command Pallet.
Select Manage Vendor Libraries.
Select Install new library (online).
Enter the following installation URL and press ENTER:
Download and unzip the latest REVLib into the C:\Users\Public\wpilib\2024 directory on Windows and ~/wpilib/2024 directory on Unix-like systems.
Follow the WPILib instructions for Adding Offline Libraries.
For a list and description of all classes:
For a list and description of all classes:
It is recommended to keep your SPARK MAX up-to-date with the latest firmware. The REV Hardware Client application will automatically download the latest firmware, but you can also download the firmware manually below:
This firmware will not work with SPARK MAX beta hardware units distributed by REV to the SPARK MAX Beta testers. It is only compatible with units received after 12/20/2018.
Improves filtering of invalid PWM signals
Previously, noise on the signal wires could be occasionally be erroneously interpreted as a PWM signal, causing the motor to spin unexpectedly
Moves the IAccum value to periodic status frame 7
Periodic status frame 7 is new to this release, and by default is sent every 250ms.
Allows changing the CAN ID of a SPARK MAX connected directly via USB without affecting other SPARK devices on the CAN bus with the same CAN ID
Makes changes towards improving the reliability of saving and persisting parameters
Fixes alternate encoder position accuracy
Fixes the main quadrature encoder position jumping in brushed mode
Fixes issue where changing the inversion mode of the duty cycle absolute encoder with a zero offset specified would cause the physical zero position to change
Fixes critical issue where new parameters introduced in 1.6.0 were not being burned to flash correctly
Fixes issue where new parameters were not being read back correctly despite being set correctly
Fixes duty cycle offset to match the inverted setting
Fixes parameters being NaN after updating to 1.6.0
Fixes burn flash command response
Adds new parameters for configuring hall sensor velocity measurement
Adds support for duty cycle absolute encoders
Adds new parameters to enable and configure position PID rollover
Fixes issue with the kDataPortConfig parameter not enabling Alternate Encoder Mode on a power cycle after it has been configured and saved to flash.
Improves Gate Driver Fault recovery.
Improvements to BLDC commutation timing.
Most noticeable at high RPMs only achievable by the NEO 550.
Fixes rare case where the SPARK MAX Status LED shows normal driving but no actual output is occurring until a reboot of the controller.
Adds a unique hash key to the firmware. This key is a hashed value based on the unique 96-bit device ID guaranteed to be unique for every STM32 device. The hash value itself is 32-bits to make transmission/reception and on-board comparison easier. It is unlikely for a key collision especially since most buses have only a small number of devices.
Adds ID Query command to have all devices which match the CAN ID sent to return their unique hash key. This is typically used to identify all devices with CAN ID 0 but can be used to identify devices with conflicting CAN IDs.
Adds ID Assign command, which sends a unique hash key and a CAN ID, which is received by any device whose CAN ID matches the ID field. The device whose unique hash matches has their own CAN ID set to the desired value. This is for initial or automatic provisioning, or to re-address devices with the same CAN ID.
Adds identify command which flashes the LED blue/magenta. This command uses the CAN ID, or the unique hash if CAN ID = 0.
Complete overhaul of USB interface, creating a generic USB-to-CAN driver connection to act as the bridge to the interface.
Adds device manufacturing info to firmware frame.
Adds retry frame to CAN bootloader if there is an issue.
Fixes issue where creating a CANSparkMax object would cause the SensorType parameter to be set to NoSensor.
Adds non-competition heartbeat command when not used with a roboRIO.
Adds Raspbian support using official WPILib tools.
Adds locking mechanism to prevent simultaneous USB and CAN commands.
Proportional blink codes now match inversion settings (i.e. positive input is always green, and negative input is always red).
Limit switch and soft limits now follow inversion settings.
Motor controller inversion now in the firmware instead of the API.
Adds a configurable range for absolute feedback devices (currently only applicable to an absolute mode analog sensor), which also prevents users from setting a setpoint out of range.
Adds ability to use both absolute and relative analog sensors as feedback devices
Adds filtering for Analog sensor with settings for velocity moving average filter
Adds ability to configure how errors are tracked and handled by the user.
Calls can be automatically registered and tracked, with any errors displayed to the DriverStation or users can use the GetLastError() after calls to determine if an error has been thrown. This is done by changing the error timeout through SetCANTimeout(), where a timeout of 0 means that the calls are non-blocking, and errors are checked in a separate thread and sent through to the driver station.
Other minor improvements and bug fixes.
Ability to configure the feedback device for the PIDController.
Addition of CANAnalog which will function as a possible feedback device.
Added API for using encoders with brushed DC motors.
Adds API to enable and set soft limits.
All control modes now reset integrator on limit switch activation for long as the limit switch is held.
Smart Motion now honors acceleration rate after limit switch changes from triggered to not triggered.
Fixes issue where sticky faults can be cleared incorrectly when a new fault is set and the old fault is no longer present.
Adds status 3 periodic frame for analog sensor.
Other minor improvements and bug fixes.
Adds initial CAN bootloading functionality.
Requires continuous power during update and requires USB recovery if the update fails after erasing the flash (i.e. power is lost during update).
This is not yet integrated into a formal tool, but is exposed in the API. Future versions will allow recovery over CAN.
Fix to bus voltage measurement, previous version reported a lower value.
Additional accuracy improvements to all ADC measurements.
Adds additional filtering to bus voltage.
Fix for Follow settings not being reapplied after power cycle.
Adds ability to change units for arbFF between voltage and percent bus voltage (or percent compensated voltage).
Fixes issue that causes a Gate Driver Fault if the NEO is spinning at a certain speed during power up.
e.g. while still moving after a breaker trips for more than ~1.4s after breaker recovery.
Fixes issue where configuration data can be corrupted under a unique set of conditions, including loss of power while configuration settings are being saved to flash memory.
Issue symptoms: Controller cannot be controlled by a normal address and would report a firmware version of 0.0.0 in the Client, even after a successful firmware update. Updating a controller that is in this state to 1.1.33 will recover the controller.
Adds new Smart Velocity mode - uses same constrains as the Smart Motion mode but outputs velocity instead of position. This is not a motion profiling mode, but rather provides acceleration limiting as opposed to simple ramp rates.
Improvements to heartbeat implementation.
CAN ID of 0 now considered 'unconfigured' and will not be enabled.
Changes the default output range for all PID slots to be [-1,1] instead of [0,0]
Changes setpoint commands to 'stick' instead of relying on periodic frames from the controller.
Follow mode can now persists through a power-cycle.
Periodic Status 2 (position data) frame rate default changed from 50ms to 20ms.
Watchdog changed to 220ms.
Proportional blink codes while driving now have a minimum blink rate (minimum is same as 10% applied output blink rate).
Fault flag 'over voltage' replaced with 'IWDT Reset'.
Velocity and Position conversion factors are now used for all internal PID calculations.
Adds new Smart Motion mode utilizing trapezoidal motion profiling
Improvements to velocity decoding for low speeds and associated PID control loops requiring control at low speeds
Improvements to gate driver fault handling
Adds voltage compensation mode
Adds ability to set the stored sensor position
Adds ability to set the maximum PID integral accumulator value to prevent integral windup
Adds ability to set the PID I accumulator value
Adds filter function to PID derivative value
Adds closed-loop current control
Adds closed-loop ramp rate separate from open-loop
Adds ability to follow Phoenix 4.11 motor controllers
Note: This feature depends on the firmware of the Phoenix controllers, and is not controlled by REV. We will do our best to build this functionality, but this is not an officially supported feature.
Adds ability to set a unit conversion for both velocity and position units
Adds ability to reset to factory defaults
Improvements to smart current limits
Improvements to current data sent over CAN
Improvements to internal settings table implementation
Calling the constructor no longer resets the encoder count. This must be done manually by running CANEncoder.setPosition()
Fixes PWM control issue introduced by version 1.0.384.
Fixes GetPosition() randomly returning 0.
Fixes momentary velocity spike when accelerating from 0 speed.
Fixes issue related to rapid control mode switching by the user.
Fixes issue related to extreme low voltage CAN bus operation.
Adds conversion for motor temperature.
Improvement to "Has Reset" flag.
Improvements to brownout fault indicator.
Improvements to brownout behavior.
Language | Current REVLib Version | Documentation |
---|---|---|
Adds for BLDC external encoder support.
This firmware update requires an API update. Please see the section for the latest updates. The table below outlines the compatibility between firmware versions and API versions:
When updating the SPARK MAX Firmware we recommend using the REV Hardware Client and the latest firmware file listed in the . When updating normally, key configuration parameters, such as the CAN ID, will be preserved through an update, preventing the need to reconfigure every time you update. In some rare instances it may be beneficial to completely reinstall the factory firmware and reset all configuration parameters. Factory images can be found below:
2024.2.0
Embedded (Press Ctrl-H)
2024.2.4
2024.2.4
Many teams have been using various CTRE motor controllers such as the Talon SRX and Talon SPX, and have concerns about porting software between platforms. Fortunately the feature set and code required is similar between the two, and porting from one to the other is easy. Below shows the common tasks and the changes required to convert from the code for CTRE Phoenix devices to the SPARK MAX.
This is a brushed motor feature and not needed for using brushless motors with SPARK MAX
For this example velocity is set at 500 RPM
This example runs a position control loop for 10 rotations.
This example assumes a 4" wheel on a 15:1 reduction
This example assumes a 4" wheel on a 15:1 reduction to move 2 feet (24 inches).
This is a brushed motor feature and not needed for using brushless motors with SPARK MAX.
For this example velocity is set at 500 RPM
This example runs a position control loop for 10 rotations.
This example assumes a 4" wheel on a 15:1 reduction
This example assumes a 4" wheel on a 15:1 reduction to move 2 feet (24 inches).
Below is a list of all the configurable parameters within the SPARK MAX. Parameters can be set through the CAN or USB interfaces. The parameters are saved in a different region of memory from the device firmware and persist through a firmware update.
Name
ID
Type
Default
Description
kCanID
0
uint
0
CAN ID This parameter persists through a normal firmware update.
kInputMode
1
Input Mode
0
Input mode, this parameter is read only and the input mode is detected by the firmware automatically. 0 - PWM 1 - CAN 2 - USB
kMotorType
2
Motor Type
BRUSHLESS
Motor type: 0 - Brushed 1 - Brushless This parameter persists through a normal firmware update.
Reserved
3
-
Reserved
kSensorType
4
Sensor Type
HALL_EFFECT
Sensor type: 0 - No Sensor 1 - Hall Sensor 2 - Encoder This parameter persists through a normal firmware update.
kCtrlType
5
Ctrl Type
CTRL_DUTY_CYCLE
Control Type, this is a read only parameter of the currently active control type. The control type is changed by calling the correct API. 0 - Duty Cycle 1 - Velocity 2 - Voltage 3 - Position
kIdleMode
6
Idle Mode
IDLE_COAST
State of the half bridge when the motor controller commands zero output or is disabled. 0 - Coast 1 - Brake This parameter persists through a normal firmware update.
kInputDeadband
7
float32
%0.05
Percent of the input which results in zero output for PWM mode. This parameter persists through a normal firmware update.
Reserved
8
-
-
Reserved
Reserved
9
-
-
Reserved
kPolePairs
10
uint
7
Number of pole pairs for the brushless motor. This is the number of poles/2 and can be determined by either counting the number of magnets or counting the number of windings and dividing by 3. This is an important term for speed regulation to properly calculate the speed.
kCurrentChop
11
float32
115/Amps
If the half bridge detects this current limit, it will disable the motor driver for a fixed amount of time set by kCurrentChopCycles. This is a low sophistication 'current control'. Set to 0 to disable. The max value is 125.
kCurrentChopCycles
12
uint
0
Number of PWM Cycles for the h-bridge to be off in the case that the current limit is set. Min = 1, multiples of PWM period (50μs). During this time the current will be recirculating through the low side MOSFETs, so instead of 'freewheeling' the diodes, the bridge will be in brake mode during this time.
kP_0
13
float32
0
Proportional gain constant for gain slot 0.
kI_0
14
float32
0
Integral gain constant for gain slot 0.
kD_0
15
float32
0
Derivative gain constant for gain slot 0.
kF_0
16
float32
0
Feed Forward gain constant for gain slot 0.
kIZone_0
17
float32
0
Integrator zone constant for gain slot 0. The PIDF loop integrator will only accumulate while the setpoint is within IZone of the target.
kDFilter_0
18
float32
0
PIDF derivative filter constant for gain slot 0.
kOutputMin_0
19
float32
-1
Max output constant for gain slot 0. This is the max output of the controller.
kOutputMax_0
20
float32
1
Min output constant for gain slot 0. This is the min output of the controller.
kP_1
21
float32
0
Proportional gain constant for gain slot 1.
kI_1
22
float32
0
Integral gain constant for gain slot 1.
kD_1
23
float32
0
Derivative gain constant for gain slot 1.
kF_1
24
float32
0
Feed Forward gain constant for gain slot 1.
kIZone_1
25
float32
0
Integrator zone constant for gain slot 1. The PIDF loop integrator will only accumulate while the setpoint is within IZone of the target.
kDFilter_1
26
float32
0
PIDF derivative filter constant for gain slot 1.
kOutputMin_1
27
float32
-1
Max output constant for gain slot 1. This is the max output of the controller.
kOutputMax_1
28
float32
1
Min output constant for gain slot 1. This is the min output of the controller.
kP_2
29
float32
0
Proportional gain constant for gain slot 2.
kI_2
30
float32
0
Integral gain constant for gain slot 2.
kD_2
31
float32
0
Derivative gain constant for gain slot 2.
kF_2
32
float32
0
Feed Forward gain constant for gain slot 2.
kIZone_2
33
float32
0
Integrator zone constant for gain slot 2. The PIDF loop integrator will only accumulate while the setpoint is within IZone of the target.
kDFilter_2
34
float32
0
PIDF derivative filter constant for gain slot 2.
kOutputMin_2
35
float32
-1
Max output constant for gain slot 2. This is the max output of the controller.
kOutputMax_2
36
float32
1
Min output constant for gain slot 2. This is the min output of the controller.
kP_3
37
float32
0
Proportional gain constant for gain slot 3.
kI_3
38
float32
0
Integral gain constant for gain slot 3.
kD_3
39
float32
0
Derivative gain constant for gain slot 3.
kF_3
40
float32
0
Feed Forward gain constant for gain slot 3.
kIZone_3
41
float32
0
Integrator zone constant for gain slot 3. The PIDF loop integrator will only accumulate while the setpoint is within IZone of the target.
kDFilter_3
42
float32
0
PIDF derivative filter constant for gain slot 3.
kOutputMin_3
43
float32
-1
Max output constant for gain slot 3. This is the max output of the controller.
kOutputMax_3
44
float32
1
Min output constant for gain slot 3. This is the min output of the controller.
Reserved
45
-
-
Reserved
Reserved
46
-
-
Reserved
Reserved
47
-
-
Reserved
Reserved
48
-
-
Reserved
Reserved
49
-
-
Reserved
kLimitSwitchFwdPolarity
50
bool
0
Forward Limit Switch polarity. 0 - Normally Open 1 - Normally Closed
kLimitSwitchRevPolarity
51
bool
0
Reverse Limit Switch polarity. 0 - Normally Open 1 - Normally Closed
kHardLimitFwdEn
52
bool
1
Limit switch enable, enabled by default
kHardLimitRevEn
53
bool
1
Limit switch enable, enabled by default
Reserved
54
-
-
Reserved
Reserved
55
-
-
Reserved
kRampRate
56
float32
V/s 0
Voltage ramp rate active for all control modes in % output per second, a value of 0 disables this feature. All APIs take the reciprocal to make the unit 'time from 0 to full'.
kFollowerID
57
uint
0
CAN EXTID of the message with data to follow
kFollowerConfig
58
uint
0
Special configuration register for setting up to follow on a repeating message (follower mode). CFG[0] to CFG[3] where CFG[0] is the motor output start bit (LSB), CFG[1] is the motor output stop bit (MSB). CFG[0] - CFG[1] determines endianness. CFG[2] bits determine sign mode and inverted, CFG[3] sets a preconfigured controller (0x1A = REV, 0x1B = Talon/Victor style as of 2018 season)
kSmartCurrentStallLimit
59
uint
80A
Smart Current Limit at stall, or any RPM less than kSmartCurrentConfig RPM.
kSmartCurrentFreeLimit
60
uint
20A
Smart current limit at free speed
kSmartCurrentConfig
61
uint
10000
Smart current limit RPM value to start linear reduction of current limit. Set this > free speed to disable.
Reserved
62
-
-
Reserved
Reserved
63
-
-
Reserved
Reserved
64
-
-
Reserved
Reserved
65
-
-
Reserved
Reserved
66
-
-
Reserved
Reserved
67
-
-
Reserved
Reserved
68
-
-
Reserved
kEncoderCountsPerRev
69
uint
4096
Number of encoder counts in a single revolution, counting every edge on the A and B lines of a quadrature encoder. (Note: This is different than the CPR spec of the encoder which is 'Cycles per revolution'. This value is 4 * CPR.
kEncoderAverageDepth
70
uint
64
Number of samples to average for velocity data based on quadrature encoder input. This value can be between 1 and 64.
kEncoderSampleDelta
71
uint
200 per 500us
Delta time value for encoder velocity measurement in 500μs increments. The velocity calculation will take delta the current sample, and the sample x * 500μs behind, and divide by this the sample delta time. Can be any number between 1 and 255
Reserved
72
-
-
Reserved
Reserved
73
-
-
Reserved
Reserved
74
-
-
Reserved
kCompensatedNominalVoltage
75
float32
0 V
In voltage compensation mode mode, this is the max scaled voltage.
kSmartMotionMaxVelocity_0
76
float32
0
kSmartMotionMaxAccel_0
77
float32
0
kSmartMotionMinVelOutput_0
78
float32
0
kSmartMotionAllowedClosedLoopError_0
79
float32
0
kSmartMotionAccelStrategy_0
80
float32
0
kSmartMotionMaxVelocity_1
81
float32
0
kSmartMotionMaxAccel_1
82
float32
0
kSmartMotionMinVelOutput_1
83
float32
0
kSmartMotionAllowedClosedLoopError_1
84
float32
0
kSmartMotionAccelStrategy_1
85
float32
0
kSmartMotionMaxVelocity_2
86
float32
0
kSmartMotionMaxAccel_2
87
float32
0
kSmartMotionMinVelOutput_2
88
float32
0
kSmartMotionAllowedClosedLoopError_2
89
float32
0
kSmartMotionAccelStrategy_2
90
float32
0
kSmartMotionMaxVelocity_3
91
float32
0
kSmartMotionMaxAccel_3
92
float32
0
kSmartMotionMinVelOutput_3
93
float32
0
kSmartMotionAllowedClosedLoopError_3
94
float32
0
kSmartMotionAccelStrategy_3
95
float32
0
kIMaxAccum_0
96
float32
0
kSlot3Placeholder1_0
97
float32
0
kSlot3Placeholder2_0
98
float32
0
kSlot3Placeholder3_0
99
float32
0
kIMaxAccum_1
100
float32
0
kSlot3Placeholder1_1
101
float32
0
kSlot3Placeholder2_1
102
float32
0
kSlot3Placeholder3_1
103
float32
0
kIMaxAccum_2
104
float32
0
kSlot3Placeholder1_2
105
float32
0
kSlot3Placeholder2_2
106
float32
0
kSlot3Placeholder3_2
107
float32
0
kIMaxAccum_3
108
float32
0
kSlot3Placeholder1_3
109
float32
0
kSlot3Placeholder2_3
110
float32
0
kSlot3Placeholder3_3
111
float32
0
kPositionConversionFactor
112
float32
1
kVelocityConversionFactor
113
float32
1
kClosedLoopRampRate
114
float32
0 DC/sec
kSoftLimitFwd
115
float32
0
Soft limit forward value
kSoftLimitRev
116
float32
0
Soft limit reverse value
Reserved
117
-
-
Reserved
Reserved
118
-
-
Reserved
kAnalogPositionConversion
119
float32
1 rev/volt
Conversion factor for position from analog sensor. This value is multiplied by the voltage to give an output value.
kAnalogVelocityConversion
120
float32
1 vel/v/s
Conversion factor for velocity from analog sensor. This value is multiplied by the voltage to give an output value.
kAnalogAverageDepth
121
uint
0
Number of samples in moving average of velocity.
kAnalogSensorMode
122
uint
0
0 Absolute: In this mode the sensor position is always read as voltage * conversion factor and reads the absolute position of the sensor. In this mode setPosition() does not have an effect. 1 Relative: In this mode the voltage difference is summed to calculate a relative position.
kAnalogInverted
123
bool
0
When inverted, the voltage is calculated as (ADC Full Scale - ADC Reading). This means that for absolute mode, the sensor value is 3.3V - voltage. In relative mode the direction is reversed.
kAnalogSampleDelta
124
uint
0
Delta time between samples for velocity measurement
Reserved
125
-
-
Reserved
Reserved
126
-
-
Reserved
kDataPortConfig
127
uint
0
0: Default configuration using limit switches 1: Alternate Encoder Mode - limit switches are disabled and alternate encoder is enabled. This parameter persists through a normal firmware update.
kAltEncoderCountsPerRev
128
uint
4096
Number of encoder counts in a single revolution, counting every edge on the A and B lines of a quadrature encoder. (Note: This is different than the CPR spec of the encoder which is 'Cycles per revolution'. This value is 4 * CPR.
kAltEncoderAverageDepth
129
uint
64
Number of samples to average for velocity data based on quadrature encoder input. This value can be between 1 and 64.
kAltEncoderSampleDelta
130
uint
200
Delta time value for encoder velocity measurement in 500μs increments. The velocity calculation will take delta the current sample, and the sample x * 500μs behind, and divide by this the sample delta time. Can be any number between 1 and 255.
kAltEncoderInverted
131
bool
0
Invert the phase of the encoder sensor. This is useful when the motor direction is opposite of the motor direction.
kAltEncoderPositionFactor
132
float32
1
Value multiplied by the native units (rotations) of the encoder for position.
kAltEncoderVelocityFactor
133
float32
1
Value multiplied by the native units (rotations) of the encoder for velocity.