CTRE Phoenix C++ 5.33.0
CANdle.h
Go to the documentation of this file.
1/* Copyright (C) Cross The Road Electronics 2024 */
2#pragma once
3
17
18namespace ctre{ namespace phoenix{ namespace led{
19
20/**
21 * CTRE CANdle
22 *
23 * Device for controlling LEDs from the CAN bus.
24 *
25 * <pre>
26 * {@code
27 * // Example usage of a CANdle
28 * CANdle candle{0}; // creates a new CANdle with ID 0
29 *
30 * CANdleConfiguration config;
31 * config.stripType = LEDStripType::RGB; // set the strip type to RGB
32 * config.brightnessScalar = 0.5; // dim the LEDs to half brightness
33 * candle.ConfigAllSettings(config);
34 *
35 * candle.SetLEDs(255, 255, 255); // set the CANdle LEDs to white
36 *
37 * // create a rainbow animation:
38 * // - max brightness
39 * // - half speed
40 * // - 64 LEDs
41 * RainbowAnimation rainbowAnim{1, 0.5, 64};
42 * candle.Animate(rainbowAnim);
43 *
44 * ErrorCode error = candle.GetLastError(); // gets the last error generated by the CANdle
45 * CANdleFaults faults;
46 * ErrorCode faultsError = candle.GetFaults(faults); // fills faults with the current CANdle faults; returns the last error generated
47 * }
48 * </pre>
49 */
50class CANdle {
51private:
52 void *_handle;
53
54
55 ctre::phoenix::ErrorCode AnimateStandard(BaseStandardAnimation& animation, int animSlot);
56 ctre::phoenix::ErrorCode AnimateTwoSize(BaseTwoSizeAnimation& animation, int animSlot);
57public:
58
59 /**
60 * Constructor for a CANdle Device
61 * @param deviceId The Device ID of the CANdle
62 * @param canbus Name of the CANbus; can be a SocketCAN interface (on Linux),
63 * or a CANivore device name or serial number
64 */
65 CANdle(int deviceId, std::string const &canbus = "");
67
68 /**
69 * Gets the Voltage of VBat as measured by CANdle
70 * @return Voltage of VBat
71 */
72 double GetBusVoltage();
73 /**
74 * Gets the Voltage of the 5V line as measured by CANdle
75 * @return Voltage of the 5V line
76 */
78 /**
79 * Gets the low-side current as measured by CANdle
80 * @return Current in Amps
81 */
82 double GetCurrent();
83 /**
84 * Gets the temperature of the CANdle in Celcius
85 * @return Temperature in Celcius
86 */
88 /**
89 * Gets the applied vbat modulation in percent.
90 * If the CANdle is configured to always enable VBat, this returns 1
91 * If the CANdle is confgigured to always disable VBat, this returns 0
92 * Otherwise it returns the last set Modulation as a value [0, 1]
93 * @return VBat Output Modulation
94 */
96 /**
97 * Gets the maximum number of simultaneous animations this version of CANdle firmware supports.
98 * If you specify an animation slot >= to this return, Phoenix will error out.
99 * You can also get the maximum count from a self-test snapshot.
100 * @return Maximum number of simultaneous animations this version of firmware supports.
101 */
103
104 /**
105 * Animates the CANdle with the passed-in animation
106 * If the animation changes after calling this function,
107 * it must be passed into animate again for the changes to take effect
108 * @param animation The animation that CANdle will run. If this is null, it will clear the animation at the specified slot
109 * @param animSlot The animation slot to use for the animation, range is [0, getMaxSimultaneousAnimationCount()) exclusive
110 * @return ErrorCode generated by function. OK indicates no error.
111 */
112 ctre::phoenix::ErrorCode Animate(Animation& animation, int animSlot = 0);
113
114 /**
115 * Clears the animation occurring in the selected selected animSlot.
116 * @param animSlot Animation slot to clear
117 * @return ErrorCode generated by function. OK indicates no error.
118 */
120
121 /**
122 * Sets a block of LEDs to the specified color
123 * @param r The amount of Red to set, range is [0, 255]
124 * @param g The amount of Green to set, range is [0, 255]
125 * @param b The amount of Blue to set, range is [0, 255]
126 * @param w The amount of White to set, range is [0, 255]. This only applies for LED strips with white in them.
127 * @param startIdx Where to start setting the LEDs
128 * @param count The number of LEDs to apply this to
129 * @return ErrorCode generated by function. OK indicates no error.
130 */
131 ctre::phoenix::ErrorCode SetLEDs(int r, int g, int b, int w = 0, int startIdx = 0, int count = 512);
132
133 /**
134 * Modulates the VBat output to the specified duty cycle percentage
135 * This function will only do something if the CANdle's VBatOutput is configured to Modulated
136 * @param dutyCyclePrcnt The duty cycle of the output modulation [0, 1]
137 * @return ErrorCode generated by function. OK indicates no error.
138 */
139 ErrorCode ModulateVBatOutput(double dutyCyclePrcnt);
140
141 /**
142 * Configures what the CANdle should do if it loses communications to the Controller
143 * @param disableWhenLOS Set to true to disable the LEDs on Loss of Signal.
144 * @param timeoutMs
145 * Timeout value in ms. If nonzero, function will wait for
146 * config success and report an error if it times out.
147 * If zero, no blocking or checking is performed.
148 * @return ErrorCode generated by function. OK indicates no error.
149 */
150 ErrorCode ConfigLOSBehavior(bool disableWhenLOS, int timeoutMs = 0);
151 /**
152 * Configures the type of LED the CANdle controls
153 * @param type The type of the LEDs the CANdle controls
154 * @param timeoutMs
155 * Timeout value in ms. If nonzero, function will wait for
156 * config success and report an error if it times out.
157 * If zero, no blocking or checking is performed.
158 * @return ErrorCode generated by function. OK indicates no error.
159 */
160 ErrorCode ConfigLEDType(LEDStripType type, int timeoutMs = 0);
161 /**
162 * Configures the brightness scalar to be applied to every LED output.
163 * This value is bounded to [0, 1].
164 *
165 * Setting this to 1 will allow the LEDs to function at max brightness.
166 * Setting this to 0.5 will scale all values to half their applied value.
167 * Setting this to 0 will turn off the LEDs.
168 *
169 * Forcing the LEDs off this way may be useful in certain testing circumstances
170 * but is generally not necessary. Self-test (Tuner) may be used to verify what
171 * the effective scalar is in case user forgot to restore the scalar to a
172 * non-zero value.
173 *
174 * @param brightness Value from [0, 1] that will scale the LED output.
175 * @param timeoutMs
176 Timeout value in ms. If nonzero, function will wait for
177 config success and report an error if it times out.
178 If zero, no blocking or checking is performed.
179 * @return Error Code generated by function. 0 indicates no error.
180 */
181 ErrorCode ConfigBrightnessScalar(double brightness, int timeoutMs = 0);
182
183 /**
184 * Configures how the status led will behave when the CANdle is actively controlling LEDs
185 * If the CANdle is LOS or not actively commanded a value, it will always turn on its status LED.
186 * @param disableWhenRunning Disables the status LED when the CANdle is running
187 * @param timeoutMs
188 Timeout value in ms. If nonzero, function will wait for
189 config success and report an error if it times out.
190 If zero, no blocking or checking is performed.
191 * @return Error Code generated by function. 0 indicates no error.
192 */
193 ErrorCode ConfigStatusLedState(bool disableWhenRunning, int timeoutMs = 0);
194 /**
195 * Configures how the VBat Output will behave
196 * @param mode VBat Output Behavior
197 * @param timeoutMs
198 Timeout value in ms. If nonzero, function will wait for
199 config success and report an error if it times out.
200 If zero, no blocking or checking is performed.
201 * @return Error Code generated by function. 0 indicates no error.
202 */
203 ErrorCode ConfigVBatOutput(VBatOutputMode mode, int timeoutMs = 0);
204 /**
205 * Configures the enable state for the 5V rail. This also affects the on-board LEDs.
206 * @param v5Enabled True to enable the 5V rail.
207 * @param timeoutMs
208 * Timeout value in ms. If nonzero, function will wait for
209 * config success and report an error if it times out.
210 * If zero, no blocking or checking is performed.
211 * @return ErrorCode generated by function. OK indicates no error.
212 */
213 ErrorCode configV5Enabled(bool v5Enabled, int timeoutMs = 0);
214
215 /**
216 * Gets a parameter. Generally this is not used.
217 * This can be utilized in
218 * - Using new features without updating API installation.
219 * - Errata workarounds to circumvent API implementation.
220 * - Allows for rapid testing / unit testing of firmware.
221 *
222 * @param param
223 * Parameter enumeration.
224 * @param ordinal
225 * Ordinal of parameter.
226 * @param timeoutMs
227 * Timeout value in ms. If nonzero, function will wait for
228 * config success and report an error if it times out.
229 * If zero, no blocking or checking is performed.
230 * @return Value of parameter.
231 */
232 double ConfigGetParameter(ParamEnum param, int ordinal, int timeoutMs = 0);
233 /**
234 * Sets a parameter. Generally this is not used.
235 * This can be utilized in
236 * - Using new features without updating API installation.
237 * - Errata workarounds to circumvent API implementation.
238 * - Allows for rapid testing / unit testing of firmware.
239 *
240 * @param param
241 * Parameter enumeration.
242 * @param value
243 * Value of parameter.
244 * @param subValue
245 * Subvalue for parameter. Maximum value of 255.
246 * @param ordinal
247 * Ordinal of parameter.
248 * @param timeoutMs
249 * Timeout value in ms. If nonzero, function will wait for
250 * config success and report an error if it times out.
251 * If zero, no blocking or checking is performed.
252 * @return Error Code generated by function. 0 indicates no error.
253 */
254 ErrorCode ConfigSetParameter(ParamEnum param, double value, int subValue = 0, int ordinal = 0, int timeoutMs = 0);
255 /**
256 * Gets the value of a custom parameter. This is for arbitrary use.
257 *
258 * Sometimes it is necessary to save calibration/duty cycle/output
259 * information in the device. Particularly if the
260 * device is part of a subsystem that can be replaced.
261 *
262 * @param paramIndex
263 * Index of custom parameter. [0-1]
264 * @param timeoutMs
265 * Timeout value in ms. If nonzero, function will wait for
266 * config success and report an error if it times out.
267 * If zero, no blocking or checking is performed.
268 * @return Value of the custom param.
269 */
270 int ConfigGetCustomParam(int paramIndex, int timeoutMs = 0);
271 /**
272 * Sets the value of a custom parameter. This is for arbitrary use.
273 *
274 * Sometimes it is necessary to save calibration/duty cycle/output
275 * information in the device. Particularly if the
276 * device is part of a subsystem that can be replaced.
277 *
278 * @param newValue
279 * Value for custom parameter.
280 * @param paramIndex
281 * Index of custom parameter. [0-1]
282 * @param timeoutMs
283 * Timeout value in ms. If nonzero, function will wait for
284 * config success and report an error if it times out.
285 * If zero, no blocking or checking is performed.
286 * @return Error Code generated by function. 0 indicates no error.
287 */
288 ErrorCode ConfigSetCustomParam(int paramIndex, int value, int timeoutMs = 0);
289 /**
290 * Configures all persistent settings to defaults.
291 *
292 * @param timeoutMs
293 * Timeout value in ms. If nonzero, function will wait for
294 * config success and report an error if it times out.
295 * If zero, no blocking or checking is performed.
296 *
297 * @return Error Code generated by function. 0 indicates no error.
298 */
299 ErrorCode ConfigFactoryDefault(int timeoutMs = 50);
300 /**
301 * Gets the CANdle fault status
302 *
303 * @param toFill Container for fault statuses.
304 * @return Error Code generated by function. OK indicates no error.
305 */
307 /**
308 * Gets the CANdle sticky fault status
309 *
310 * @param toFill Container for sticky fault statuses.
311 * @return Error Code generated by function. OK indicates no error.
312 */
314 /**
315 * Clears the sticky faults.
316 *
317 * @param timeoutMs
318 * Timeout value in ms. If nonzero, function will wait for
319 * config success and report an error if it times out.
320 * If zero, no blocking or checking is performed.
321 * @return Error Code generated by function. 0 indicates no error.
322 */
323 ErrorCode ClearStickyFaults(int timeoutMs = 0);
324 /**
325 * Returns true if the device has reset since last call.
326 *
327 * @return Has a Device Reset Occurred?
328 */
330 /**
331 * Sets the period of the given status frame.
332 *
333 * @param frame
334 * Frame whose period is to be changed.
335 * @param periodMs
336 * Period in ms for the given frame.
337 * @param timeoutMs
338 * Timeout value in ms. If nonzero, function will wait for
339 * config success and report an error if it times out.
340 * If zero, no blocking or checking is performed.
341 * @return Error Code generated by function. 0 indicates no error.
342 */
343 ErrorCode SetStatusFramePeriod(CANdleStatusFrame frame, int periodMs, int timeoutMs = 0);
344 /**
345 * Gets the period of the given status frame.
346 *
347 * @param frame
348 * Frame to get the period of.
349 * @param timeoutMs
350 * Timeout value in ms. If nonzero, function will wait for
351 * config success and report an error if it times out.
352 * If zero, no blocking or checking is performed.
353 * @return Period of the given status frame.
354 */
355 int GetStatusFramePeriod(CANdleStatusFrame frame, int timeoutMs = 0);
356 /**
357 * Sets the period of the given control frame.
358 *
359 * @param frame
360 * Frame whose period is to be changed.
361 * @param periodMs
362 * Period in ms for the given frame.
363 * @return Error Code generated by function. 0 indicates no error.
364 */
366
367 /**
368 * Configures all persistent settings.
369 *
370 * @param allConfigs Object with all of the persistant settings
371 * @param timeoutMs
372 * Timeout value in ms. If nonzero, function will wait for
373 * config success and report an error if it times out.
374 * If zero, no blocking or checking is performed.
375 * @return Error Code generated by function. 0 indicates no error.
376 */
377 ErrorCode ConfigAllSettings(CANdleConfiguration allConfigs, int timeoutMs = 50);
378 /**
379 * Gets all persistant settings.
380 *
381 * @param allConfigs Object with all of the persistant settings
382 * @param timeoutMs
383 * Timeout value in ms. If nonzero, function will wait for
384 * config success and report an error if it times out.
385 * If zero, no blocking or checking is performed.
386 */
387 void GetAllConfigs(CANdleConfiguration allConfigs, int timeoutMs = 50);
388
389 /**
390 * Call GetLastError() generated by this object.
391 * Not all functions return an error code but can
392 * potentially report errors.
393 *
394 * This function can be used to retrieve those error codes.
395 *
396 * @return The last ErrorCode generated.
397 */
399};
400
401} // namespace led
402} // namespace phoenix
403} // namespace ctre
404
The base class for all animations that CANdle supports.
Definition: Animation.h:14
The base class for one generic type of animation.
Definition: BaseStandardAnimation.h:12
The base class for one generic type of animation.
Definition: BaseTwoSizeAnimation.h:12
CTRE CANdle.
Definition: CANdle.h:50
ErrorCode GetStickyFaults(CANdleStickyFaults &toFill)
Gets the CANdle sticky fault status.
ErrorCode ClearStickyFaults(int timeoutMs=0)
Clears the sticky faults.
ErrorCode ConfigLOSBehavior(bool disableWhenLOS, int timeoutMs=0)
Configures what the CANdle should do if it loses communications to the Controller.
ErrorCode ConfigSetCustomParam(int paramIndex, int value, int timeoutMs=0)
Sets the value of a custom parameter.
int ConfigGetCustomParam(int paramIndex, int timeoutMs=0)
Gets the value of a custom parameter.
ctre::phoenix::ErrorCode Animate(Animation &animation, int animSlot=0)
Animates the CANdle with the passed-in animation If the animation changes after calling this function...
double GetCurrent()
Gets the low-side current as measured by CANdle.
ErrorCode ConfigVBatOutput(VBatOutputMode mode, int timeoutMs=0)
Configures how the VBat Output will behave.
ErrorCode SetStatusFramePeriod(CANdleStatusFrame frame, int periodMs, int timeoutMs=0)
Sets the period of the given status frame.
double GetBusVoltage()
Gets the Voltage of VBat as measured by CANdle.
ErrorCode ConfigAllSettings(CANdleConfiguration allConfigs, int timeoutMs=50)
Configures all persistent settings.
ErrorCode ConfigSetParameter(ParamEnum param, double value, int subValue=0, int ordinal=0, int timeoutMs=0)
Sets a parameter.
ErrorCode ConfigLEDType(LEDStripType type, int timeoutMs=0)
Configures the type of LED the CANdle controls.
ErrorCode ConfigFactoryDefault(int timeoutMs=50)
Configures all persistent settings to defaults.
ErrorCode GetFaults(CANdleFaults &toFill)
Gets the CANdle fault status.
ErrorCode ModulateVBatOutput(double dutyCyclePrcnt)
Modulates the VBat output to the specified duty cycle percentage This function will only do something...
ErrorCode configV5Enabled(bool v5Enabled, int timeoutMs=0)
Configures the enable state for the 5V rail.
int GetStatusFramePeriod(CANdleStatusFrame frame, int timeoutMs=0)
Gets the period of the given status frame.
void GetAllConfigs(CANdleConfiguration allConfigs, int timeoutMs=50)
Gets all persistant settings.
double Get5VRailVoltage()
Gets the Voltage of the 5V line as measured by CANdle.
ctre::phoenix::ErrorCode ClearAnimation(int animSlot)
Clears the animation occurring in the selected selected animSlot.
int GetMaxSimultaneousAnimationCount()
Gets the maximum number of simultaneous animations this version of CANdle firmware supports.
ErrorCode ConfigBrightnessScalar(double brightness, int timeoutMs=0)
Configures the brightness scalar to be applied to every LED output.
ErrorCode GetLastError()
Call GetLastError() generated by this object.
ctre::phoenix::ErrorCode SetLEDs(int r, int g, int b, int w=0, int startIdx=0, int count=512)
Sets a block of LEDs to the specified color.
bool HasResetOccurred()
Returns true if the device has reset since last call.
ErrorCode SetControlFramePeriod(CANdleControlFrame frame, int periodMs)
Sets the period of the given control frame.
double GetVBatModulation()
Gets the applied vbat modulation in percent.
CANdle(int deviceId, std::string const &canbus="")
Constructor for a CANdle Device.
ErrorCode ConfigStatusLedState(bool disableWhenRunning, int timeoutMs=0)
Configures how the status led will behave when the CANdle is actively controlling LEDs If the CANdle ...
double GetTemperature()
Gets the temperature of the CANdle in Celcius.
double ConfigGetParameter(ParamEnum param, int ordinal, int timeoutMs=0)
Gets a parameter.
CANdleControlFrame
Enumerated type for status frame types.
Definition: CANdleControlFrame.h:9
LEDStripType
The various LED types that the CANdle can support.
Definition: CANdleLedStripType.h:8
VBatOutputMode
The various methods of managing the VBat output behavior.
Definition: VBatOutputMode.h:9
CANdleStatusFrame
Enumerated type for status frame types.
Definition: CANdleStatusFrame.h:9
ParamEnum
Signal enumeration for generic signal access.
Definition: paramEnum.h:13
ErrorCode
Definition: ErrorCode.h:13
namespace ctre
Definition: PigeonIMU_Faults.h:6
Configurables available to CANdle.
Definition: CANdleConfiguration.h:14
Faults available to CANdle (Currently has none)
Definition: CANdleFaults.h:13
Faults available to CANdle (Currently has none)
Definition: CANdleFaults.h:116