ReduxLib C++ 2026.1.2
Loading...
Searching...
No Matches
Canandcolor.h
1// Copyright (c) Bagholders of Redux Robotics and other contributors.
2// This is open source and can be modified and shared under the Mozilla Public License v2.0.
3
4#pragma once
5#include <units/time.h>
6#include "redux/canand/CanandDevice.h"
7#include "redux/frames/Frame.h"
8#include "redux/canand/CanandSettingsManager.h"
9#include "CanandcolorDetails.h"
10#include "CanandcolorStatus.h"
11#include "CanandcolorFaults.h"
12#include "CanandcolorSettings.h"
13#include "ColorData.h"
14#include "DigoutSlotState.h"
15
16/**
17 * Namespace for all classes relating to the Canandcolor.
18 */
20
21/**
22 * Class for the CAN interface of the
23 * <a href="https://docs.reduxrobotics.com/canandcolor/index.html">Canandcolor.</a>
24 *
25 * <p>
26 * The C++ vendordep uses the
27 * <a href="https://docs.wpilib.org/en/stable/docs/software/basic-programming/cpp-units.html">WPILib units library</a>
28 * for all dimensioned values, including settings.
29 * </p>
30 *
31 * <p>
32 * Sensor reads (proximity/color/status) do not block: data is received asynchronously from the CAN receive thread and
33 * getters return the most recently received values.
34 * </p>
35 *
36 * <p>
37 * Settings operations may block briefly (default ~20 ms per setting) while waiting for a confirmation packet, unless
38 * the timeout is set to 0.
39 * </p>
40 *
41 * Example code:
42 * ```cpp
43 * Canandcolor color{0}; // device with CAN id 0
44 *
45 * // Non-blocking reads (last received values):
46 * double proximity = color.GetProximity(); // normalized [0..1]
47 * double r = color.GetRed(); // normalized [0..1]
48 * double hue = color.GetHSVHue(); // normalized [0..1)
49 *
50 * // Timestamped reads:
51 * auto proximityFrame = color.GetProximityFrame().GetFrameData();
52 * proximityFrame.GetValue();
53 * proximityFrame.GetTimestamp();
54 *
55 * // Settings (blocking):
56 * CanandcolorSettings settings;
57 * settings.SetStatusFramePeriod(1000_ms);
58 * settings.SetProximityFramePeriod(20_ms);
59 * settings.SetColorFramePeriod(20_ms);
60 * settings.SetColorIntegrationPeriod(ColorPeriod::k16ms);
61 * settings.SetProximityIntegrationPeriod(ProximityPeriod::k25ms);
62 * color.SetSettings(settings, 20_ms);
63 *
64 * // LED control:
65 * color.SetLampLEDBrightness(1.0); // clamp to [0..1]
66 *
67 * // Faults:
68 * color.ClearStickyFaults();
69 * auto sticky = color.GetStickyFaults();
70 * sticky.powerCycle;
71 *
72 * // Digital output config:
73 * settings.SetDigoutPinConfig(0, ActiveHighDigoutPinConfig{});
74 * settings.SetDigoutFrameTrigger(0, DigoutFrameTrigger::kRisingAndFalling);
75 * ```
76 */
78 public:
79 /**
80 * Constructor with the device's CAN id.
81 *
82 * This object will be constant with respect to whatever CAN id assigned to it, so if a device changes id it may
83 * change which device this object reads from.
84 *
85 * @param canID device CAN id [0..63]
86 * @param bus the message bus to use. Defaults to "halcan".
87 */
88 Canandcolor(int canID, std::string bus = "halcan");
89
90 /**
91 * Destructor.
92 *
93 * Unregisters the internal CAN listener.
94 */
96
97 /**
98 * Gets the most recently received proximity value, normalized to [0..1].
99 *
100 * @return proximity [0..1]
101 */
102 double GetProximity();
103
104 /**
105 * Gets the most recently received red channel, normalized to [0..1].
106 *
107 * @return red [0..1]
108 */
109 double GetRed();
110
111 /**
112 * Gets the most recently received green channel, normalized to [0..1].
113 *
114 * @return green [0..1]
115 */
116 double GetGreen();
117
118 /**
119 * Gets the most recently received blue channel, normalized to [0..1].
120 *
121 * @return blue [0..1]
122 */
123 double GetBlue();
124
125 /**
126 * Gets the HSV hue derived from the most recently received RGB channels.
127 *
128 * The hue is normalized to [0..1), where 0 and 1 represent the same hue.
129 *
130 * @return hue [0..1)
131 */
132 double GetHSVHue();
133
134 /**
135 * Gets the HSV saturation derived from the most recently received RGB channels.
136 *
137 * @return saturation [0..1]
138 */
140
141 /**
142 * Gets the HSV value derived from the most recently received RGB channels.
143 *
144 * @return value [0..1]
145 */
146 double GetHSVValue();
147
148 /**
149 * Gets the most recently received RGB triplet.
150 *
151 * @return ColorData snapshot of the most recently received color
152 */
154
155 /**
156 * Gets the most recently received digital output state.
157 *
158 * @return DigoutSlotState snapshot of the most recently received digital output frame
159 */
161
162 /**
163 * Gets sticky faults from the most recently received status frame.
164 *
165 * Sticky faults remain set until cleared via ClearStickyFaults().
166 *
167 * @return sticky faults (faultsValid indicates whether the device has reported status yet)
168 */
170
171 /**
172 * Gets active faults from the most recently received status frame.
173 *
174 * @return active faults (faultsValid indicates whether the device has reported status yet)
175 */
177
178 /**
179 * Gets the most recently received temperature.
180 *
181 * @return temperature in degrees Celsius
182 */
183 units::celsius_t GetTemperature();
184
185 /**
186 * Gets the most recently received device status.
187 *
188 * @return CanandcolorStatus snapshot
189 */
191
192 /**
193 * Clears sticky faults on the device.
194 *
195 * This call does not block.
196 */
198
199 /**
200 * Clears sticky digital output event flags on the device.
201 *
202 * This call does not block.
203 */
205
206 /**
207 * Controls "party mode" (device identification LED animation).
208 *
209 * This call does not block.
210 *
211 * @param level party level, clamped to [0..10]
212 */
213 void SetPartyMode(uint8_t level);
214
215 /**
216 * Fetches the device settings in a blocking manner.
217 *
218 * @param timeout total timeout to wait for all settings
219 * @param missingTimeout per-setting timeout for retrying missing settings
220 * @param attempts number of retries for missing settings
221 * @return received settings snapshot
222 */
223 CanandcolorSettings GetSettings(units::second_t timeout = 350_ms,
224 units::second_t missingTimeout = 20_ms,
225 uint32_t attempts = 3);
226
227 /**
228 * Starts an asynchronous fetch of settings.
229 *
230 * Use GetSettingsAsync() to retrieve whatever is currently known.
231 */
233
234 /**
235 * Gets the currently known settings cache.
236 *
237 * @return settings currently received so far
238 */
240
241 /**
242 * Sets device settings.
243 *
244 * @param settings settings to apply
245 * @param timeout maximum time to wait for each setting to be confirmed; set to 0 to not block
246 * @param attempts number of attempts per setting
247 * @return settings object containing any settings that failed to apply
248 */
250 units::second_t timeout = 20_ms,
251 uint32_t attempts = 3);
252
253 /**
254 * Resets the device to factory defaults.
255 *
256 * @param timeout total timeout to wait for the reset command to be confirmed
257 * @return received settings snapshot after reset (best-effort)
258 */
259 CanandcolorSettings ResetFactoryDefaults(units::second_t timeout = 350_ms);
260
261 /**
262 * Sets the lamp LED brightness.
263 *
264 * @param brightness desired brightness, clamped to [0..1]
265 */
266 void SetLampLEDBrightness(double brightness);
267
268 /**
269 * Gets the proximity frame, which holds the most recently received proximity value and its receive timestamp.
270 *
271 * @return reference to the internal Frame<double>
272 */
276
277 /**
278 * Gets the color frame, which holds the most recently received ColorData and its receive timestamp.
279 *
280 * @return reference to the internal ColorFrame
281 */
285
286 /**
287 * Gets the digital output frame, which holds the most recently received DigoutSlotState and its receive timestamp.
288 *
289 * @return reference to the internal Frame<DigoutSlotState>
290 */
294
295 /**
296 * Gets the status frame, which holds the most recently received CanandcolorStatus and its receive timestamp.
297 *
298 * @return reference to the internal Frame<CanandcolorStatus>
299 */
303
304 /**
305 * Gets the internal settings manager.
306 *
307 * This is an advanced API for custom settings workflows.
308 *
309 * @return internal CanandSettingsManager handle
310 */
314
315 /**
316 * Internal CAN message handler.
317 * @param msg received CAN message
318 */
320
321 /**
322 * Gets the CAN device address for this instance.
323 * @return CanandAddress reference
324 */
326
327 /**
328 * Gets the device class name string.
329 * @return "Canandcolor"
330 */
331 std::string GetDeviceClassName() override;
332
333 /**
334 * Gets the minimum firmware version expected by this library.
335 * @return minimum firmware version
336 */
338
339 protected:
340 /** proximity frame */
342 /** color data frame */
344 /** digout status frame */
346 /** status frame*/
348 /** stg manager */
350
351 private:
353 bool dataRecvOnce_;
354 units::second_t lastMessageTime_;
355};
356
357} // namespace redux::sensors::canandcolor
Definition CanandAddress.h:62
Definition CanandDevice.h:35
Definition CanandMessage.h:26
Definition CanandSettingsManager.h:82
Definition Frame.h:52
Definition CanandcolorFaults.h:14
Definition CanandcolorSettings.h:39
Definition Canandcolor.h:77
redux::frames::Frame< ColorData > & GetColorFrame()
Definition Canandcolor.h:282
redux::canand::CanandSettingsManager< CanandcolorSettings > stg_
Definition Canandcolor.h:349
void HandleMessage(redux::canand::CanandMessage &msg) override
redux::frames::Frame< double > & GetProximityFrame()
Definition Canandcolor.h:273
CanandcolorSettings ResetFactoryDefaults(units::second_t timeout=350_ms)
redux::canand::CanandFirmwareVersion GetMinimumFirmwareVersion() override
void SetLampLEDBrightness(double brightness)
Canandcolor(int canID, std::string bus="halcan")
redux::frames::Frame< ColorData > color_
Definition Canandcolor.h:343
redux::frames::Frame< double > proximity_
Definition Canandcolor.h:341
redux::frames::Frame< CanandcolorStatus > & GetStatusFrame()
Definition Canandcolor.h:300
CanandcolorSettings GetSettings(units::second_t timeout=350_ms, units::second_t missingTimeout=20_ms, uint32_t attempts=3)
redux::canand::CanandSettingsManager< CanandcolorSettings > & GetInternalSettingsManager()
Definition Canandcolor.h:311
redux::frames::Frame< DigoutSlotState > & GetDigoutFrame()
Definition Canandcolor.h:291
redux::canand::CanandAddress & GetAddress() override
std::string GetDeviceClassName() override
redux::frames::Frame< CanandcolorStatus > status_
Definition Canandcolor.h:347
redux::frames::Frame< DigoutSlotState > digout_
Definition Canandcolor.h:345
CanandcolorSettings SetSettings(CanandcolorSettings &settings, units::second_t timeout=20_ms, uint32_t attempts=3)
Definition Canandcolor.h:19
Definition CanandFirmwareVersion.h:17
Definition CanandcolorStatus.h:15
Definition DigoutSlotState.h:15