CTRE Phoenix C++ 5.33.1
BasePigeon.h
Go to the documentation of this file.
1/* Copyright (C) Cross The Road Electronics 2024 */
2/*
3 * Software License Agreement
4 *
5 * Copyright (C) Cross The Road Electronics. All rights
6 * reserved.
7 *
8 * Cross The Road Electronics (CTRE) licenses to you the right to
9 * use, publish, and distribute copies of CRF (Cross The Road) firmware files (*.crf) and Software
10 * API Libraries ONLY when in use with Cross The Road Electronics hardware products.
11 *
12 * THE SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT
13 * WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT
14 * LIMITATION, ANY WARRANTY OF MERCHANTABILITY, FITNESS FOR A
15 * PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL
16 * CROSS THE ROAD ELECTRONICS BE LIABLE FOR ANY INCIDENTAL, SPECIAL,
17 * INDIRECT OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF
18 * PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR SERVICES, ANY CLAIMS
19 * BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE
20 * THEREOF), ANY CLAIMS FOR INDEMNITY OR CONTRIBUTION, OR OTHER
21 * SIMILAR COSTS, WHETHER ASSERTED ON THE BASIS OF CONTRACT, TORT
22 * (INCLUDING NEGLIGENCE), BREACH OF WARRANTY, OR OTHERWISE
23 */
24
25#pragma once
26
27#include <string>
37
38/* forward prototype */
39namespace ctre {
40namespace phoenix {
41namespace motorcontrol {
42namespace can {
43class TalonSRX;
44}
45}
46}
47}
48
49namespace ctre {
50namespace phoenix {
51/** sensors namespace */
52namespace sensors {
53
54/**
55 * Configurables available to Pigeon
56 */
59
60 /**
61 * @return String representation of configs
62 */
63 std::string toString() {
64 return toString("");
65 }
66
67 /**
68 * @param prependString
69 * String to prepend to configs
70 * @return String representation of configs
71 */
72 std::string toString(std::string prependString) {
73 std::string retstr = CustomParamConfiguration::toString(prependString);
74
75 return retstr;
76 }
77};// struct BasePigeon
78
79/**
80 * Util class to help with Pigeon configurations
81 */
83private:
84 static BasePigeonConfiguration _default;
85public:
86 /**
87 * Determine if specified value is different from default
88 * @param settings settings to compare against
89 * @return if specified value is different from default
90 * @{
91 */
92 static bool CustomParam0Different (const BasePigeonConfiguration & settings) { return (!(settings.customParam0 == _default.customParam0)) || !settings.enableOptimizations; }
93 static bool CustomParam1Different (const BasePigeonConfiguration & settings) { return (!(settings.customParam1 == _default.customParam1)) || !settings.enableOptimizations; }
94 /** @} */
95};
96
97/**
98 * Pigeon IMU Class.
99 * Class supports communicating over CANbus and over ribbon-cable (CAN Talon SRX).
100 */
102public:
103
104 /**
105 * Create a Pigeon object that communicates with Pigeon on CAN Bus.
106 *
107 * @param deviceNumber
108 * CAN Device Id of Pigeon [0,62]
109 * @param canbus Name of the CANbus; can be a SocketCAN interface (on Linux),
110 * or a CANivore device name or serial number
111 */
112 BasePigeon(int deviceNumber, std::string const &version, std::string const &canbus = "");
113
115
116 /**
117 * Destructs all pigeon objects
118 */
120
121 /**
122 * Sets the Yaw register to the specified value.
123 *
124 * @param angleDeg Degree of Yaw [+/- 368,640 degrees]
125 * @param timeoutMs
126 * Timeout value in ms. If nonzero, function will wait for
127 * config success and report an error if it times out.
128 * If zero, no blocking or checking is performed.
129 * @return Error Code generated by function. 0 indicates no error.
130 */
131 int SetYaw(double angleDeg, int timeoutMs = 0);
132 /**
133 * Atomically add to the Yaw register.
134 *
135 * @param angleDeg Degrees to add to the Yaw register.
136 * @param timeoutMs
137 * Timeout value in ms. If nonzero, function will wait for
138 * config success and report an error if it times out.
139 * If zero, no blocking or checking is performed.
140 * @return Error Code generated by function. 0 indicates no error.
141 */
142 int AddYaw(double angleDeg, int timeoutMs = 0);
143 /**
144 * Sets the Yaw register to match the current compass value.
145 *
146 * @param timeoutMs
147 * Timeout value in ms. If nonzero, function will wait for
148 * config success and report an error if it times out.
149 * If zero, no blocking or checking is performed.
150 * @return Error Code generated by function. 0 indicates no error.
151 */
152 int SetYawToCompass(int timeoutMs = 0);
153
154 /**
155 * Sets the AccumZAngle.
156 *
157 * @param angleDeg Degrees to set AccumZAngle to.
158 * @param timeoutMs
159 * Timeout value in ms. If nonzero, function will wait for
160 * config success and report an error if it times out.
161 * If zero, no blocking or checking is performed.
162 * @return Error Code generated by function. 0 indicates no error.
163 */
164 int SetAccumZAngle(double angleDeg, int timeoutMs = 0);
165 /**
166 * Call GetLastError() generated by this object.
167 * Not all functions return an error code but can
168 * potentially report errors.
169 *
170 * This function can be used to retrieve those error codes.
171 *
172 * @return The last ErrorCode generated.
173 */
175 /**
176 * Get 6d Quaternion data.
177 *
178 * @param wxyz Array to fill with quaternion data w[0], x[1], y[2], z[3]
179 * @return The last ErrorCode generated.
180 */
181 ErrorCode Get6dQuaternion(double wxyz[4]) const;
182 /**
183 * Get Yaw, Pitch, and Roll data.
184 *
185 * @param ypr Array to fill with yaw[0], pitch[1], and roll[2] data.
186 * Yaw is within [-368,640, +368,640] degrees.
187 * Pitch is within [-90,+90] degrees.
188 * Roll is within [-90,+90] degrees.
189 * @return The last ErrorCode generated.
190 */
191 ErrorCode GetYawPitchRoll(double ypr[3]) const;
192 /**
193 * Get the yaw from the Pigeon
194 * @return Yaw
195 */
196 double GetYaw() const;
197 /**
198 * Get the pitch from the Pigeon
199 * @return Pitch
200 */
201 double GetPitch() const;
202 /**
203 * Get the roll from the Pigeon
204 * @return Roll
205 */
206 double GetRoll() const;
207 /**
208 * Get AccumGyro data.
209 * AccumGyro is the integrated gyro value on each axis.
210 *
211 * @param xyz_deg Array to fill with x[0], y[1], and z[2] AccumGyro data
212 * @return The last ErrorCode generated.
213 */
214 int GetAccumGyro(double xyz_deg[3]) const;
215 /**
216 * Get the absolute compass heading.
217 * @return compass heading [0,360) degrees.
218 */
220 /**
221 * Get the continuous compass heading.
222 * @return continuous compass heading [-23040, 23040) degrees. Use
223 * SetCompassHeading to modify the wrap-around portion.
224 */
225 double GetCompassHeading() const;
226 /**
227 * Gets the compass' measured magnetic field strength.
228 * @return field strength in Microteslas (uT).
229 */
231 /**
232 * Gets the temperature of the pigeon.
233 *
234 * @return Temperature in ('C)
235 */
236 double GetTemp() const;
237 /**
238 * Gets the current Pigeon uptime.
239 *
240 * @return How long has Pigeon been running in whole seconds. Value caps at
241 * 255.
242 */
243 uint32_t GetUpTime() const;
244 /**
245 * Get Raw Magnetometer data.
246 *
247 * @param rm_xyz Array to fill with x[0], y[1], and z[2] data
248 * Number is equal to 0.6 microTeslas per unit.
249 * @return The last ErrorCode generated.
250 */
251 int GetRawMagnetometer(int16_t rm_xyz[3]) const;
252
253 /**
254 * Get Biased Magnetometer data.
255 *
256 * @param bm_xyz Array to fill with x[0], y[1], and z[2] data
257 * Number is equal to 0.6 microTeslas per unit.
258 * @return The last ErrorCode generated.
259 */
260 int GetBiasedMagnetometer(int16_t bm_xyz[3]) const;
261 /**
262 * Get Biased Accelerometer data.
263 *
264 * @param ba_xyz Array to fill with x[0], y[1], and z[2] data.
265 * These are in fixed point notation Q2.14. eg. 16384 = 1G
266 * @return The last ErrorCode generated.
267 */
268 int GetBiasedAccelerometer(int16_t ba_xyz[3]) const;
269 /**
270 * Get Raw Gyro data.
271 *
272 * @param xyz_dps Array to fill with x[0], y[1], and z[2] data in degrees per second.
273 * @return The last ErrorCode generated.
274 */
275 int GetRawGyro(double xyz_dps[3]) const;
276 /**
277 * @return number of times Pigeon Reset
278 */
279 uint32_t GetResetCount() const;
280 /**
281 * @return Reset flags for Pigeon
282 */
283 uint32_t GetResetFlags() const;
284 /**
285 * @return firmware version of Pigeon
286 */
287 uint32_t GetFirmVers() const;
288
289 /**
290 * @return true iff a reset has occurred since last call.
291 */
292 bool HasResetOccurred() const;
293
294 /**
295 * Sets the value of a custom parameter. This is for arbitrary use.
296 *
297 * Sometimes it is necessary to save calibration/declination/offset
298 * information in the device. Particularly if the
299 * device is part of a subsystem that can be replaced.
300 *
301 * @param newValue
302 * Value for custom parameter.
303 * @param paramIndex
304 * Index of custom parameter. [0-1]
305 * @param timeoutMs
306 * Timeout value in ms. If nonzero, function will wait for
307 * config success and report an error if it times out.
308 * If zero, no blocking or checking is performed.
309 * @return Error Code generated by function. 0 indicates no error.
310 */
311 ErrorCode ConfigSetCustomParam(int newValue, int paramIndex, int timeoutMs = 0);
312 /**
313 * Gets the value of a custom parameter. This is for arbitrary use.
314 *
315 * Sometimes it is necessary to save calibration/declination/offset
316 * information in the device. Particularly if the
317 * device is part of a subsystem that can be replaced.
318 *
319 * @param paramIndex
320 * Index of custom parameter. [0-1]
321 * @param timeoutMs
322 * Timeout value in ms. If nonzero, function will wait for
323 * config success and report an error if it times out.
324 * If zero, no blocking or checking is performed.
325 * @return Value of the custom param.
326 */
327 int ConfigGetCustomParam(int paramIndex, int timeoutMs = 0);
328 /**
329 * Sets a parameter. Generally this is not used.
330 * This can be utilized in
331 * - Using new features without updating API installation.
332 * - Errata workarounds to circumvent API implementation.
333 * - Allows for rapid testing / unit testing of firmware.
334 *
335 * @param param
336 * Parameter enumeration.
337 * @param value
338 * Value of parameter.
339 * @param subValue
340 * Subvalue for parameter. Maximum value of 255.
341 * @param ordinal
342 * Ordinal of parameter.
343 * @param timeoutMs
344 * Timeout value in ms. If nonzero, function will wait for
345 * config success and report an error if it times out.
346 * If zero, no blocking or checking is performed.
347 * @return Error Code generated by function. 0 indicates no error.
348 */
350 uint8_t subValue, int ordinal, int timeoutMs = 0);
351 /**
352 * Gets a parameter. Generally this is not used.
353 * This can be utilized in
354 * - Using new features without updating API installation.
355 * - Errata workarounds to circumvent API implementation.
356 * - Allows for rapid testing / unit testing of firmware.
357 *
358 * @param param
359 * Parameter enumeration.
360 * @param ordinal
361 * Ordinal of parameter.
362 * @param timeoutMs
363 * Timeout value in ms. If nonzero, function will wait for
364 * config success and report an error if it times out.
365 * If zero, no blocking or checking is performed.
366 * @return Value of parameter.
367 */
368 double ConfigGetParameter(ctre::phoenix::ParamEnum param, int ordinal, int timeoutMs = 0);
369 /**
370 * Gets a parameter by passing an int by reference
371 *
372 * @param param
373 * Parameter enumeration
374 * @param valueToSend
375 * Value to send to parameter
376 * @param valueReceived
377 * Reference to integer to receive
378 * @param subValue
379 * SubValue of parameter
380 * @param ordinal
381 * Ordinal of parameter
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 * @return Error Code generated by function. 0 indicates no error.
387 */
388 ErrorCode ConfigGetParameter(ParamEnum param, int32_t valueToSend,
389 int32_t & valueReceived, uint8_t & subValue, int32_t ordinal,
390 int32_t timeoutMs);
391
392 /**
393 * Sets the period of the given status frame.
394 *
395 * @param statusFrame
396 * Frame whose period is to be changed.
397 * @param periodMs
398 * Period in ms for the given frame.
399 * @param timeoutMs
400 * Timeout value in ms. If nonzero, function will wait for
401 * config success and report an error if it times out.
402 * If zero, no blocking or checking is performed.
403 * @return Error Code generated by function. 0 indicates no error.
404 */
406 int timeoutMs = 0);
407
408 /**
409 * Gets the period of the given status frame.
410 *
411 * @param frame
412 * Frame to get the period of.
413 * @param timeoutMs
414 * Timeout value in ms. If nonzero, function will wait for
415 * config success and report an error if it times out.
416 * If zero, no blocking or checking is performed.
417 * @return Period of the given status frame.
418 */
420 int timeoutMs = 0) ;
421 /**
422 * Sets the period of the given control frame.
423 *
424 * @param frame
425 * Frame whose period is to be changed.
426 * @param periodMs
427 * Period in ms for the given frame.
428 * @return Error Code generated by function. 0 indicates no error.
429 */
431 int periodMs);
432 /**
433 * Gets the firmware version of the device.
434 *
435 * @return param holds the firmware version of the device. Device must be powered
436 * cycled at least once.
437 */
439 /**
440 * Clears the Sticky Faults
441 *
442 * @return Error Code generated by function. 0 indicates no error.
443 */
444 ErrorCode ClearStickyFaults(int timeoutMs = 0);
445
446 /**
447 * @return Pigeon resource handle.
448 */
449 void* GetLowLevelHandle() const {
450 return _handle;
451 }
452
453 //------ All Configs ----------//
454 /**
455 * Configures all persistent settings.
456 *
457 * @param allConfigs Object with all of the persistant settings
458 * @param timeoutMs
459 * Timeout value in ms. If nonzero, function will wait for
460 * config success and report an error if it times out.
461 * If zero, no blocking or checking is performed.
462 *
463 * @return Error Code generated by function. 0 indicates no error.
464 */
465 virtual ctre::phoenix::ErrorCode ConfigAllSettings(const BasePigeonConfiguration &allConfigs, int timeoutMs = 50);
466 /**
467 * Gets all persistant settings.
468 *
469 * @param allConfigs Object with all of the persistant settings
470 * @param timeoutMs
471 * Timeout value in ms. If nonzero, function will wait for
472 * config success and report an error if it times out.
473 * If zero, no blocking or checking is performed.
474 */
475 virtual void GetAllConfigs(BasePigeonConfiguration &allConfigs, int timeoutMs = 50);
476 /**
477 * Configures all persistent settings to defaults.
478 *
479 * @param timeoutMs
480 * Timeout value in ms. If nonzero, function will wait for
481 * config success and report an error if it times out.
482 * If zero, no blocking or checking is performed.
483 *
484 * @return Error Code generated by function. 0 indicates no error.
485 */
486 virtual ErrorCode ConfigFactoryDefault(int timeoutMs = 50);
487
488 /**
489 * @return object that can set simulation inputs.
490 */
492private:
493 /** firmware state reported over CAN */
494 enum MotionDriverState {
495 Init0 = 0,
496 WaitForPowerOff = 1,
497 ConfigAg = 2,
498 SelfTestAg = 3,
499 StartDMP = 4,
500 ConfigCompass_0 = 5,
501 ConfigCompass_1 = 6,
502 ConfigCompass_2 = 7,
503 ConfigCompass_3 = 8,
504 ConfigCompass_4 = 9,
505 ConfigCompass_5 = 10,
506 SelfTestCompass = 11,
507 WaitForGyroStable = 12,
508 AdditionalAccelAdjust = 13,
509 Idle = 14,
510 Calibration = 15,
511 LedInstrum = 16,
512 Error = 31,
513 };
514 /** sub command for the various Set param enums */
515 enum TareType {
516 SetValue = 0x00, AddOffset = 0x01, MatchCompass = 0x02, SetOffset = 0xFF,
517 };
518 /** data storage for reset signals */
519 struct ResetStats {
520 int32_t resetCount;
521 int32_t resetFlags;
522 int32_t firmVers;
523 bool hasReset;
524 };
525 ResetStats _resetStats = { 0, 0, 0, false };
526
527 /** Portion of the arbID for all status and control frames. */
528 void* _handle;
529 uint32_t _deviceNumber;
530 uint32_t _usageHist = 0;
531 uint64_t _cache;
532 uint32_t _len;
533 BasePigeonSimCollection* _simCollection;
534
535 /** overall threshold for when frame data is too old */
536 const uint32_t EXPECTED_RESPONSE_TIMEOUT_MS = (200);
537
538 int PrivateSetParameter(ParamEnum paramEnum, TareType tareType,
539 double angleDeg, int timeoutMs = 0);
540
541 double GetTemp(const uint64_t & statusFrame);
542
543protected:
545
546};// class BasePigeon
547} // namespace signals
548} // namespace phoenix
549} // namespace ctre
Simple address holder.
Definition: CANBusAddressable.h:9
CTRE Talon SRX Motor Controller when used on CAN Bus.
Definition: TalonSRX.h:145
Pigeon IMU Class.
Definition: BasePigeon.h:101
ErrorCode Get6dQuaternion(double wxyz[4]) const
Get 6d Quaternion data.
double GetRoll() const
Get the roll from the Pigeon.
BasePigeon(ctre::phoenix::motorcontrol::can::TalonSRX &talonSrx)
ErrorCode ConfigGetParameter(ParamEnum param, int32_t valueToSend, int32_t &valueReceived, uint8_t &subValue, int32_t ordinal, int32_t timeoutMs)
Gets a parameter by passing an int by reference.
int GetFirmwareVersion()
Gets the firmware version of the device.
int GetStatusFramePeriod(PigeonIMU_StatusFrame frame, int timeoutMs=0)
Gets the period of the given status frame.
int GetRawGyro(double xyz_dps[3]) const
Get Raw Gyro data.
virtual ErrorCode ConfigFactoryDefault(int timeoutMs=50)
Configures all persistent settings to defaults.
double ConfigGetParameter(ctre::phoenix::ParamEnum param, int ordinal, int timeoutMs=0)
Gets a parameter.
ErrorCode ConfigSetCustomParam(int newValue, int paramIndex, int timeoutMs=0)
Sets the value of a custom parameter.
ErrorCode ClearStickyFaults(int timeoutMs=0)
Clears the Sticky Faults.
int GetBiasedAccelerometer(int16_t ba_xyz[3]) const
Get Biased Accelerometer data.
double GetPitch() const
Get the pitch from the Pigeon.
BasePigeon(int deviceNumber, std::string const &version, std::string const &canbus="")
Create a Pigeon object that communicates with Pigeon on CAN Bus.
double GetCompassFieldStrength() const
Gets the compass' measured magnetic field strength.
int GetAccumGyro(double xyz_deg[3]) const
Get AccumGyro data.
int GetBiasedMagnetometer(int16_t bm_xyz[3]) const
Get Biased Magnetometer data.
int SetAccumZAngle(double angleDeg, int timeoutMs=0)
Sets the AccumZAngle.
virtual BasePigeonSimCollection & GetSimCollection()
int SetYaw(double angleDeg, int timeoutMs=0)
Sets the Yaw register to the specified value.
double GetCompassHeading() const
Get the continuous compass heading.
ErrorCode GetYawPitchRoll(double ypr[3]) const
Get Yaw, Pitch, and Roll data.
int ConfigGetCustomParam(int paramIndex, int timeoutMs=0)
Gets the value of a custom parameter.
static void DestroyAllBasePigeons()
Destructs all pigeon objects.
int SetYawToCompass(int timeoutMs=0)
Sets the Yaw register to match the current compass value.
void * GetLowLevelHandle() const
Definition: BasePigeon.h:449
int GetRawMagnetometer(int16_t rm_xyz[3]) const
Get Raw Magnetometer data.
ErrorCode GetLastError() const
Call GetLastError() generated by this object.
virtual void GetAllConfigs(BasePigeonConfiguration &allConfigs, int timeoutMs=50)
Gets all persistant settings.
double GetTemp() const
Gets the temperature of the pigeon.
double GetYaw() const
Get the yaw from the Pigeon.
virtual ctre::phoenix::ErrorCode ConfigAllSettings(const BasePigeonConfiguration &allConfigs, int timeoutMs=50)
Configures all persistent settings.
int AddYaw(double angleDeg, int timeoutMs=0)
Atomically add to the Yaw register.
ErrorCode SetStatusFramePeriod(PigeonIMU_StatusFrame statusFrame, uint8_t periodMs, int timeoutMs=0)
Sets the period of the given status frame.
ErrorCode SetControlFramePeriod(PigeonIMU_ControlFrame frame, int periodMs)
Sets the period of the given control frame.
double GetAbsoluteCompassHeading() const
Get the absolute compass heading.
ErrorCode ConfigSetParameter(ParamEnum param, double value, uint8_t subValue, int ordinal, int timeoutMs=0)
Sets a parameter.
uint32_t GetUpTime() const
Gets the current Pigeon uptime.
Collection of simulation functions available to a Pigeon IMU.
Definition: BasePigeonSimCollection.h:23
PigeonIMU_StatusFrame
Enumerated type for status frame types.
Definition: PigeonIMU_StatusFrame.h:9
PigeonIMU_ControlFrame
Enumerated type for status frame types.
Definition: PigeonIMU_ControlFrame.h:9
ParamEnum
Signal enumeration for generic signal access.
Definition: paramEnum.h:13
ErrorCode
Definition: ErrorCode.h:13
namespace ctre
Definition: paramEnum.h:5
Configurables for any custom param configs.
Definition: CustomParamConfiguration.h:11
int customParam1
Custom Param 1.
Definition: CustomParamConfiguration.h:19
bool enableOptimizations
Enable optimizations for ConfigAll (defaults true)
Definition: CustomParamConfiguration.h:23
int customParam0
Custom Param 0.
Definition: CustomParamConfiguration.h:15
std::string toString()
Definition: CustomParamConfiguration.h:34
Util class to help with Pigeon configurations.
Definition: BasePigeon.h:82
static bool CustomParam1Different(const BasePigeonConfiguration &settings)
Definition: BasePigeon.h:93
static bool CustomParam0Different(const BasePigeonConfiguration &settings)
Determine if specified value is different from default.
Definition: BasePigeon.h:92
Configurables available to Pigeon.
Definition: BasePigeon.h:57
std::string toString()
Definition: BasePigeon.h:63
std::string toString(std::string prependString)
Definition: BasePigeon.h:72
BasePigeonConfiguration()
Definition: BasePigeon.h:58