Line data Source code
1 : // ESCPackets.c was generated by ProtoGen version 3.5.c
2 :
3 : /*
4 : * This file is free software: you can redistribute it and/or modify it
5 : * under the terms of the GNU General Public License as published by the
6 : * Free Software Foundation, either version 3 of the License, or
7 : * (at your option) any later version.
8 : *
9 : * This file is distributed in the hope that it will be useful, but
10 : * WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 : * See the GNU General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU General Public License along
15 : * with this program. If not, see <http://www.gnu.org/licenses/>.
16 : *
17 : * Author: Oliver Walters / Currawong Engineering Pty Ltd
18 : */
19 :
20 : #include "ESCPackets.h"
21 : #include "fielddecode.h"
22 : #include "fieldencode.h"
23 : #include "scaleddecode.h"
24 : #include "scaledencode.h"
25 : #include "ESCDefines.h"
26 :
27 : /*!
28 : * \brief Encode a ESC_HallSensorPattern_t into a byte array
29 : *
30 :
31 : * \param _pg_data points to the byte array to add encoded data to
32 : * \param _pg_bytecount points to the starting location in the byte array, and will be incremented by the number of encoded bytes.
33 : * \param _pg_user is the data to encode in the byte array
34 : */
35 0 : void encodeESC_HallSensorPattern_t(uint8_t* _pg_data, int* _pg_bytecount, const ESC_HallSensorPattern_t* _pg_user)
36 : {
37 0 : int _pg_byteindex = *_pg_bytecount;
38 :
39 : // Range of reserved is 0 to 3.
40 0 : _pg_data[_pg_byteindex] = (uint8_t)_pg_user->reserved << 6;
41 :
42 : // Range of polarity is 0 to 7.
43 0 : _pg_data[_pg_byteindex] |= (uint8_t)_pg_user->polarity << 3;
44 0 : _pg_byteindex += 1; // close bit field
45 :
46 0 : *_pg_bytecount = _pg_byteindex;
47 :
48 0 : }// encodeESC_HallSensorPattern_t
49 :
50 : /*!
51 : * \brief Decode a ESC_HallSensorPattern_t from a byte array
52 : *
53 :
54 : * \param _pg_data points to the byte array to decoded data from
55 : * \param _pg_bytecount points to the starting location in the byte array, and will be incremented by the number of bytes decoded
56 : * \param _pg_user is the data to decode from the byte array
57 : * \return 1 if the data are decoded, else 0.
58 : */
59 0 : int decodeESC_HallSensorPattern_t(const uint8_t* _pg_data, int* _pg_bytecount, ESC_HallSensorPattern_t* _pg_user)
60 : {
61 0 : int _pg_byteindex = *_pg_bytecount;
62 :
63 : // Range of reserved is 0 to 3.
64 0 : _pg_user->reserved = (_pg_data[_pg_byteindex] >> 6);
65 :
66 : // Range of polarity is 0 to 7.
67 0 : _pg_user->polarity = ((_pg_data[_pg_byteindex] >> 3) & 0x7);
68 0 : _pg_byteindex += 1; // close bit field
69 :
70 0 : *_pg_bytecount = _pg_byteindex;
71 :
72 0 : return 1;
73 :
74 : }// decodeESC_HallSensorPattern_t
75 :
76 : /*!
77 : * \brief Create the ESC_RPMControlLoopSettings packet
78 : *
79 : * RPM control loop settings
80 : * \param _pg_pkt points to the packet which will be created by this function
81 : * \param _pg_user points to the user data that will be encoded in _pg_pkt
82 : */
83 0 : void encodeESC_RPMControlLoopSettingsPacketStructure(void* _pg_pkt, const ESC_RPMControlLoopSettings_t* _pg_user)
84 : {
85 0 : uint8_t* _pg_data = getESCVelocityPacketData(_pg_pkt);
86 0 : int _pg_byteindex = 0;
87 :
88 : // Core control loop settings
89 0 : encodeESC_ControlLoopCommon_t(_pg_data, &_pg_byteindex, &_pg_user->pid);
90 :
91 : // RPM setpoint rate limit
92 : // Range of rpmRateLimit is 0 to 65535.
93 0 : uint16ToBeBytes(_pg_user->rpmRateLimit, _pg_data, &_pg_byteindex);
94 :
95 : // Corner frequency for RPM measurement LPF
96 : // Range of rpmFilter is 0.0f to 500.0f.
97 0 : float32ScaledTo2UnsignedBeBytes(_pg_user->rpmFilter, _pg_data, &_pg_byteindex, 0.0f, 131.07f);
98 :
99 : // complete the process of creating the packet
100 0 : finishESCVelocityPacket(_pg_pkt, _pg_byteindex, getESC_RPMControlLoopSettingsPacketID());
101 :
102 0 : }// encodeESC_RPMControlLoopSettingsPacketStructure
103 :
104 : /*!
105 : * \brief Decode the ESC_RPMControlLoopSettings packet
106 : *
107 : * RPM control loop settings
108 : * \param _pg_pkt points to the packet being decoded by this function
109 : * \param _pg_user receives the data decoded from the packet
110 : * \return 0 is returned if the packet ID or size is wrong, else 1
111 : */
112 0 : int decodeESC_RPMControlLoopSettingsPacketStructure(const void* _pg_pkt, ESC_RPMControlLoopSettings_t* _pg_user)
113 : {
114 : int _pg_numbytes;
115 0 : int _pg_byteindex = 0;
116 : const uint8_t* _pg_data;
117 :
118 : // Verify the packet identifier
119 0 : if(getESCVelocityPacketID(_pg_pkt) != getESC_RPMControlLoopSettingsPacketID())
120 0 : return 0;
121 :
122 : // Verify the packet size
123 0 : _pg_numbytes = getESCVelocityPacketSize(_pg_pkt);
124 0 : if(_pg_numbytes < getESC_RPMControlLoopSettingsMinDataLength())
125 0 : return 0;
126 :
127 : // The raw data from the packet
128 0 : _pg_data = getESCVelocityPacketDataConst(_pg_pkt);
129 :
130 : // this packet has default fields, make sure they are set
131 0 : _pg_user->rpmRateLimit = 0;
132 0 : _pg_user->rpmFilter = 50;
133 :
134 : // Core control loop settings
135 0 : if(decodeESC_ControlLoopCommon_t(_pg_data, &_pg_byteindex, &_pg_user->pid) == 0)
136 0 : return 0;
137 :
138 0 : if(_pg_byteindex + 2 > _pg_numbytes)
139 0 : return 1;
140 :
141 : // RPM setpoint rate limit
142 : // Range of rpmRateLimit is 0 to 65535.
143 0 : _pg_user->rpmRateLimit = uint16FromBeBytes(_pg_data, &_pg_byteindex);
144 :
145 0 : if(_pg_byteindex + 2 > _pg_numbytes)
146 0 : return 1;
147 :
148 : // Corner frequency for RPM measurement LPF
149 : // Range of rpmFilter is 0.0f to 500.0f.
150 0 : _pg_user->rpmFilter = float32ScaledFrom2UnsignedBeBytes(_pg_data, &_pg_byteindex, 0.0f, 1.0f/131.07f);
151 :
152 0 : return 1;
153 :
154 : }// decodeESC_RPMControlLoopSettingsPacketStructure
155 :
156 : /*!
157 : * \brief Encode a ESC_RPMControlLoopSettings_t into a byte array
158 : *
159 : * RPM control loop settings
160 : * \param _pg_data points to the byte array to add encoded data to
161 : * \param _pg_bytecount points to the starting location in the byte array, and will be incremented by the number of encoded bytes.
162 : * \param _pg_user is the data to encode in the byte array
163 : */
164 0 : void encodeESC_RPMControlLoopSettings_t(uint8_t* _pg_data, int* _pg_bytecount, const ESC_RPMControlLoopSettings_t* _pg_user)
165 : {
166 0 : int _pg_byteindex = *_pg_bytecount;
167 :
168 : // Core control loop settings
169 0 : encodeESC_ControlLoopCommon_t(_pg_data, &_pg_byteindex, &_pg_user->pid);
170 :
171 : // RPM setpoint rate limit
172 : // Range of rpmRateLimit is 0 to 65535.
173 0 : uint16ToBeBytes(_pg_user->rpmRateLimit, _pg_data, &_pg_byteindex);
174 :
175 : // Corner frequency for RPM measurement LPF
176 : // Range of rpmFilter is 0.0f to 500.0f.
177 0 : float32ScaledTo2UnsignedBeBytes(_pg_user->rpmFilter, _pg_data, &_pg_byteindex, 0.0f, 131.07f);
178 :
179 0 : *_pg_bytecount = _pg_byteindex;
180 :
181 0 : }// encodeESC_RPMControlLoopSettings_t
182 :
183 : /*!
184 : * \brief Decode a ESC_RPMControlLoopSettings_t from a byte array
185 : *
186 : * RPM control loop settings
187 : * \param _pg_data points to the byte array to decoded data from
188 : * \param _pg_bytecount points to the starting location in the byte array, and will be incremented by the number of bytes decoded
189 : * \param _pg_user is the data to decode from the byte array
190 : * \return 1 if the data are decoded, else 0.
191 : */
192 0 : int decodeESC_RPMControlLoopSettings_t(const uint8_t* _pg_data, int* _pg_bytecount, ESC_RPMControlLoopSettings_t* _pg_user)
193 : {
194 0 : int _pg_byteindex = *_pg_bytecount;
195 :
196 : // Core control loop settings
197 0 : if(decodeESC_ControlLoopCommon_t(_pg_data, &_pg_byteindex, &_pg_user->pid) == 0)
198 0 : return 0;
199 :
200 : // RPM setpoint rate limit
201 : // Range of rpmRateLimit is 0 to 65535.
202 0 : _pg_user->rpmRateLimit = uint16FromBeBytes(_pg_data, &_pg_byteindex);
203 :
204 : // Corner frequency for RPM measurement LPF
205 : // Range of rpmFilter is 0.0f to 500.0f.
206 0 : _pg_user->rpmFilter = float32ScaledFrom2UnsignedBeBytes(_pg_data, &_pg_byteindex, 0.0f, 1.0f/131.07f);
207 :
208 0 : *_pg_bytecount = _pg_byteindex;
209 :
210 0 : return 1;
211 :
212 : }// decodeESC_RPMControlLoopSettings_t
213 :
214 : /*!
215 : * \brief Create the ESC_Config packet
216 : *
217 : * General ESC configuration parameters
218 : * \param _pg_pkt points to the packet which will be created by this function
219 : * \param _pg_user points to the user data that will be encoded in _pg_pkt
220 : */
221 0 : void encodeESC_ConfigPacketStructure(void* _pg_pkt, const ESC_Config_t* _pg_user)
222 : {
223 0 : uint8_t* _pg_data = getESCVelocityPacketData(_pg_pkt);
224 0 : int _pg_byteindex = 0;
225 :
226 : // 1 = ESC is inhibited (disabled) on startup
227 0 : _pg_data[_pg_byteindex] = (uint8_t)((_pg_user->swInhibit == true) ? 1 : 0) << 7;
228 :
229 : // 1 = ESC will respond to PICCOLO autopilot commands
230 0 : _pg_data[_pg_byteindex] |= (uint8_t)((_pg_user->piccoloCommands == true) ? 1 : 0) << 6;
231 :
232 : // 1 = ESC will accept broadcast command messages
233 0 : _pg_data[_pg_byteindex] |= (uint8_t)((_pg_user->broadcastCommands == true) ? 1 : 0) << 5;
234 :
235 : // 1 = Digital PWM input is enabled
236 0 : _pg_data[_pg_byteindex] |= (uint8_t)((_pg_user->enablePwmInput == true) ? 1 : 0) << 4;
237 :
238 : // Reserved for future use
239 :
240 : // External motor temperature sensor configuration
241 : // Range of motorTempSensor is 0 to 7.
242 0 : _pg_data[_pg_byteindex] |= (uint8_t)limitMax(_pg_user->motorTempSensor, 7);
243 0 : _pg_byteindex += 1; // close bit field
244 :
245 :
246 : // ESC keepalive timeout - If this is non-zero, the ESC will automatically revert to *STANDBY* mode if it does not receive a valid command for the alloted period
247 : // Range of keepalive is 0 to 255.
248 0 : uint8ToBytes(_pg_user->keepalive, _pg_data, &_pg_byteindex);
249 :
250 : // 1 = Disable LED
251 0 : _pg_data[_pg_byteindex] = (uint8_t)limitMax(_pg_user->disableLED, 1) << 7;
252 :
253 : // CAN bit (baud) rate
254 : // Range of canBitRate is 0 to 15.
255 0 : _pg_data[_pg_byteindex] |= (uint8_t)limitMax(_pg_user->canBitRate, 15) << 3;
256 :
257 : // Protocol to communicate over CAN
258 : // Range of canProtocol is 0 to 3.
259 0 : _pg_data[_pg_byteindex] |= (uint8_t)limitMax(_pg_user->canProtocol, 3) << 1;
260 :
261 : // Enable motor brake strength (applied when motor is off)
262 0 : _pg_data[_pg_byteindex] |= (uint8_t)((_pg_user->motorBraking == true) ? 1 : 0);
263 0 : _pg_byteindex += 1; // close bit field
264 :
265 :
266 : // Delay between hardware inhibit and software inhibit events
267 : // Range of swInhibitDelay is 0 to 255.
268 0 : uint8ToBytes(_pg_user->swInhibitDelay, _pg_data, &_pg_byteindex);
269 :
270 : // Reserved for future use
271 0 : uint8ToBytes((uint8_t)(0), _pg_data, &_pg_byteindex);
272 :
273 : // Reserved for future use
274 0 : uint16ToBeBytes((uint16_t)(0), _pg_data, &_pg_byteindex);
275 :
276 : // Sets the motor beep mode for the ESC
277 : // Range of motorBeepMode is 0 to 7.
278 0 : _pg_data[_pg_byteindex] = (uint8_t)limitMax(_pg_user->motorBeepMode, 7) << 5;
279 :
280 : // Motor beep volume
281 : // Range of motorBeepVolume is 0 to 31.
282 0 : _pg_data[_pg_byteindex] |= (uint8_t)limitMax(_pg_user->motorBeepVolume, 31);
283 0 : _pg_byteindex += 1; // close bit field
284 :
285 : // complete the process of creating the packet
286 0 : finishESCVelocityPacket(_pg_pkt, _pg_byteindex, getESC_ConfigPacketID());
287 :
288 0 : }// encodeESC_ConfigPacketStructure
289 :
290 : /*!
291 : * \brief Decode the ESC_Config packet
292 : *
293 : * General ESC configuration parameters
294 : * \param _pg_pkt points to the packet being decoded by this function
295 : * \param _pg_user receives the data decoded from the packet
296 : * \return 0 is returned if the packet ID or size is wrong, else 1
297 : */
298 0 : int decodeESC_ConfigPacketStructure(const void* _pg_pkt, ESC_Config_t* _pg_user)
299 : {
300 : int _pg_numbytes;
301 0 : int _pg_byteindex = 0;
302 : const uint8_t* _pg_data;
303 :
304 : // Verify the packet identifier
305 0 : if(getESCVelocityPacketID(_pg_pkt) != getESC_ConfigPacketID())
306 0 : return 0;
307 :
308 : // Verify the packet size
309 0 : _pg_numbytes = getESCVelocityPacketSize(_pg_pkt);
310 0 : if(_pg_numbytes < getESC_ConfigMinDataLength())
311 0 : return 0;
312 :
313 : // The raw data from the packet
314 0 : _pg_data = getESCVelocityPacketDataConst(_pg_pkt);
315 :
316 : // 1 = ESC is inhibited (disabled) on startup
317 0 : _pg_user->swInhibit = ((_pg_data[_pg_byteindex] >> 7)) ? true : false;
318 :
319 : // 1 = ESC will respond to PICCOLO autopilot commands
320 0 : _pg_user->piccoloCommands = (((_pg_data[_pg_byteindex] >> 6) & 0x1)) ? true : false;
321 :
322 : // 1 = ESC will accept broadcast command messages
323 0 : _pg_user->broadcastCommands = (((_pg_data[_pg_byteindex] >> 5) & 0x1)) ? true : false;
324 :
325 : // 1 = Digital PWM input is enabled
326 0 : _pg_user->enablePwmInput = (((_pg_data[_pg_byteindex] >> 4) & 0x1)) ? true : false;
327 :
328 : // Reserved for future use
329 :
330 : // External motor temperature sensor configuration
331 : // Range of motorTempSensor is 0 to 7.
332 0 : _pg_user->motorTempSensor = ((_pg_data[_pg_byteindex]) & 0x7);
333 0 : _pg_byteindex += 1; // close bit field
334 :
335 : // ESC keepalive timeout - If this is non-zero, the ESC will automatically revert to *STANDBY* mode if it does not receive a valid command for the alloted period
336 : // Range of keepalive is 0 to 255.
337 0 : _pg_user->keepalive = uint8FromBytes(_pg_data, &_pg_byteindex);
338 :
339 : // 1 = Disable LED
340 0 : _pg_user->disableLED = (_pg_data[_pg_byteindex] >> 7);
341 :
342 : // CAN bit (baud) rate
343 : // Range of canBitRate is 0 to 15.
344 0 : _pg_user->canBitRate = ((_pg_data[_pg_byteindex] >> 3) & 0xF);
345 :
346 : // Protocol to communicate over CAN
347 : // Range of canProtocol is 0 to 3.
348 0 : _pg_user->canProtocol = ((_pg_data[_pg_byteindex] >> 1) & 0x3);
349 :
350 : // Enable motor brake strength (applied when motor is off)
351 0 : _pg_user->motorBraking = (((_pg_data[_pg_byteindex]) & 0x1)) ? true : false;
352 0 : _pg_byteindex += 1; // close bit field
353 :
354 : // Delay between hardware inhibit and software inhibit events
355 : // Range of swInhibitDelay is 0 to 255.
356 0 : _pg_user->swInhibitDelay = uint8FromBytes(_pg_data, &_pg_byteindex);
357 :
358 : // Reserved for future use
359 0 : _pg_byteindex += 1;
360 :
361 : // Reserved for future use
362 0 : _pg_byteindex += 2;
363 :
364 : // Sets the motor beep mode for the ESC
365 : // Range of motorBeepMode is 0 to 7.
366 0 : _pg_user->motorBeepMode = (_pg_data[_pg_byteindex] >> 5);
367 :
368 : // Motor beep volume
369 : // Range of motorBeepVolume is 0 to 31.
370 0 : _pg_user->motorBeepVolume = ((_pg_data[_pg_byteindex]) & 0x1F);
371 0 : _pg_byteindex += 1; // close bit field
372 :
373 0 : return 1;
374 :
375 : }// decodeESC_ConfigPacketStructure
376 :
377 : /*!
378 : * \brief Create the ESC_Config packet
379 : *
380 : * General ESC configuration parameters
381 : * \param _pg_pkt points to the packet which will be created by this function
382 : * \param swInhibit is 1 = ESC is inhibited (disabled) on startup
383 : * \param piccoloCommands is 1 = ESC will respond to PICCOLO autopilot commands
384 : * \param broadcastCommands is 1 = ESC will accept broadcast command messages
385 : * \param enablePwmInput is 1 = Digital PWM input is enabled
386 : * \param motorTempSensor is External motor temperature sensor configuration
387 : * \param keepalive is ESC keepalive timeout - If this is non-zero, the ESC will automatically revert to *STANDBY* mode if it does not receive a valid command for the alloted period
388 : * \param disableLED is 1 = Disable LED
389 : * \param canBitRate is CAN bit (baud) rate
390 : * \param canProtocol is Protocol to communicate over CAN
391 : * \param motorBraking is Enable motor brake strength (applied when motor is off)
392 : * \param swInhibitDelay is Delay between hardware inhibit and software inhibit events
393 : * \param motorBeepMode is Sets the motor beep mode for the ESC
394 : * \param motorBeepVolume is Motor beep volume
395 : */
396 0 : void encodeESC_ConfigPacket(void* _pg_pkt, bool swInhibit, bool piccoloCommands, bool broadcastCommands, bool enablePwmInput, uint8_t motorTempSensor, uint8_t keepalive, uint8_t disableLED, uint8_t canBitRate, uint8_t canProtocol, bool motorBraking, uint8_t swInhibitDelay, uint8_t motorBeepMode, uint8_t motorBeepVolume)
397 : {
398 0 : uint8_t* _pg_data = getESCVelocityPacketData(_pg_pkt);
399 0 : int _pg_byteindex = 0;
400 :
401 : // 1 = ESC is inhibited (disabled) on startup
402 0 : _pg_data[_pg_byteindex] = (uint8_t)((swInhibit == true) ? 1 : 0) << 7;
403 :
404 : // 1 = ESC will respond to PICCOLO autopilot commands
405 0 : _pg_data[_pg_byteindex] |= (uint8_t)((piccoloCommands == true) ? 1 : 0) << 6;
406 :
407 : // 1 = ESC will accept broadcast command messages
408 0 : _pg_data[_pg_byteindex] |= (uint8_t)((broadcastCommands == true) ? 1 : 0) << 5;
409 :
410 : // 1 = Digital PWM input is enabled
411 0 : _pg_data[_pg_byteindex] |= (uint8_t)((enablePwmInput == true) ? 1 : 0) << 4;
412 :
413 : // Reserved for future use
414 :
415 : // External motor temperature sensor configuration
416 : // Range of motorTempSensor is 0 to 7.
417 0 : _pg_data[_pg_byteindex] |= (uint8_t)limitMax(motorTempSensor, 7);
418 0 : _pg_byteindex += 1; // close bit field
419 :
420 : // ESC keepalive timeout - If this is non-zero, the ESC will automatically revert to *STANDBY* mode if it does not receive a valid command for the alloted period
421 : // Range of keepalive is 0 to 255.
422 0 : uint8ToBytes(keepalive, _pg_data, &_pg_byteindex);
423 :
424 : // 1 = Disable LED
425 0 : _pg_data[_pg_byteindex] = (uint8_t)limitMax(disableLED, 1) << 7;
426 :
427 : // CAN bit (baud) rate
428 : // Range of canBitRate is 0 to 15.
429 0 : _pg_data[_pg_byteindex] |= (uint8_t)limitMax(canBitRate, 15) << 3;
430 :
431 : // Protocol to communicate over CAN
432 : // Range of canProtocol is 0 to 3.
433 0 : _pg_data[_pg_byteindex] |= (uint8_t)limitMax(canProtocol, 3) << 1;
434 :
435 : // Enable motor brake strength (applied when motor is off)
436 0 : _pg_data[_pg_byteindex] |= (uint8_t)((motorBraking == true) ? 1 : 0);
437 0 : _pg_byteindex += 1; // close bit field
438 :
439 : // Delay between hardware inhibit and software inhibit events
440 : // Range of swInhibitDelay is 0 to 255.
441 0 : uint8ToBytes(swInhibitDelay, _pg_data, &_pg_byteindex);
442 :
443 : // Reserved for future use
444 0 : uint8ToBytes((uint8_t)(0), _pg_data, &_pg_byteindex);
445 :
446 : // Reserved for future use
447 0 : uint16ToBeBytes((uint16_t)(0), _pg_data, &_pg_byteindex);
448 :
449 : // Sets the motor beep mode for the ESC
450 : // Range of motorBeepMode is 0 to 7.
451 0 : _pg_data[_pg_byteindex] = (uint8_t)limitMax(motorBeepMode, 7) << 5;
452 :
453 : // Motor beep volume
454 : // Range of motorBeepVolume is 0 to 31.
455 0 : _pg_data[_pg_byteindex] |= (uint8_t)limitMax(motorBeepVolume, 31);
456 0 : _pg_byteindex += 1; // close bit field
457 :
458 : // complete the process of creating the packet
459 0 : finishESCVelocityPacket(_pg_pkt, _pg_byteindex, getESC_ConfigPacketID());
460 :
461 0 : }// encodeESC_ConfigPacket
462 :
463 : /*!
464 : * \brief Decode the ESC_Config packet
465 : *
466 : * General ESC configuration parameters
467 : * \param _pg_pkt points to the packet being decoded by this function
468 : * \param swInhibit receives 1 = ESC is inhibited (disabled) on startup
469 : * \param piccoloCommands receives 1 = ESC will respond to PICCOLO autopilot commands
470 : * \param broadcastCommands receives 1 = ESC will accept broadcast command messages
471 : * \param enablePwmInput receives 1 = Digital PWM input is enabled
472 : * \param motorTempSensor receives External motor temperature sensor configuration
473 : * \param keepalive receives ESC keepalive timeout - If this is non-zero, the ESC will automatically revert to *STANDBY* mode if it does not receive a valid command for the alloted period
474 : * \param disableLED receives 1 = Disable LED
475 : * \param canBitRate receives CAN bit (baud) rate
476 : * \param canProtocol receives Protocol to communicate over CAN
477 : * \param motorBraking receives Enable motor brake strength (applied when motor is off)
478 : * \param swInhibitDelay receives Delay between hardware inhibit and software inhibit events
479 : * \param motorBeepMode receives Sets the motor beep mode for the ESC
480 : * \param motorBeepVolume receives Motor beep volume
481 : * \return 0 is returned if the packet ID or size is wrong, else 1
482 : */
483 0 : int decodeESC_ConfigPacket(const void* _pg_pkt, bool* swInhibit, bool* piccoloCommands, bool* broadcastCommands, bool* enablePwmInput, uint8_t* motorTempSensor, uint8_t* keepalive, uint8_t* disableLED, uint8_t* canBitRate, uint8_t* canProtocol, bool* motorBraking, uint8_t* swInhibitDelay, uint8_t* motorBeepMode, uint8_t* motorBeepVolume)
484 : {
485 0 : int _pg_byteindex = 0;
486 0 : const uint8_t* _pg_data = getESCVelocityPacketDataConst(_pg_pkt);
487 0 : int _pg_numbytes = getESCVelocityPacketSize(_pg_pkt);
488 :
489 : // Verify the packet identifier
490 0 : if(getESCVelocityPacketID(_pg_pkt) != getESC_ConfigPacketID())
491 0 : return 0;
492 :
493 0 : if(_pg_numbytes < getESC_ConfigMinDataLength())
494 0 : return 0;
495 :
496 : // 1 = ESC is inhibited (disabled) on startup
497 0 : (*swInhibit) = ((_pg_data[_pg_byteindex] >> 7)) ? true : false;
498 :
499 : // 1 = ESC will respond to PICCOLO autopilot commands
500 0 : (*piccoloCommands) = (((_pg_data[_pg_byteindex] >> 6) & 0x1)) ? true : false;
501 :
502 : // 1 = ESC will accept broadcast command messages
503 0 : (*broadcastCommands) = (((_pg_data[_pg_byteindex] >> 5) & 0x1)) ? true : false;
504 :
505 : // 1 = Digital PWM input is enabled
506 0 : (*enablePwmInput) = (((_pg_data[_pg_byteindex] >> 4) & 0x1)) ? true : false;
507 :
508 : // Reserved for future use
509 :
510 : // External motor temperature sensor configuration
511 : // Range of motorTempSensor is 0 to 7.
512 0 : (*motorTempSensor) = ((_pg_data[_pg_byteindex]) & 0x7);
513 0 : _pg_byteindex += 1; // close bit field
514 :
515 : // ESC keepalive timeout - If this is non-zero, the ESC will automatically revert to *STANDBY* mode if it does not receive a valid command for the alloted period
516 : // Range of keepalive is 0 to 255.
517 0 : (*keepalive) = uint8FromBytes(_pg_data, &_pg_byteindex);
518 :
519 : // 1 = Disable LED
520 0 : (*disableLED) = (_pg_data[_pg_byteindex] >> 7);
521 :
522 : // CAN bit (baud) rate
523 : // Range of canBitRate is 0 to 15.
524 0 : (*canBitRate) = ((_pg_data[_pg_byteindex] >> 3) & 0xF);
525 :
526 : // Protocol to communicate over CAN
527 : // Range of canProtocol is 0 to 3.
528 0 : (*canProtocol) = ((_pg_data[_pg_byteindex] >> 1) & 0x3);
529 :
530 : // Enable motor brake strength (applied when motor is off)
531 0 : (*motorBraking) = (((_pg_data[_pg_byteindex]) & 0x1)) ? true : false;
532 0 : _pg_byteindex += 1; // close bit field
533 :
534 : // Delay between hardware inhibit and software inhibit events
535 : // Range of swInhibitDelay is 0 to 255.
536 0 : (*swInhibitDelay) = uint8FromBytes(_pg_data, &_pg_byteindex);
537 :
538 : // Reserved for future use
539 0 : _pg_byteindex += 1;
540 :
541 : // Reserved for future use
542 0 : _pg_byteindex += 2;
543 :
544 : // Sets the motor beep mode for the ESC
545 : // Range of motorBeepMode is 0 to 7.
546 0 : (*motorBeepMode) = (_pg_data[_pg_byteindex] >> 5);
547 :
548 : // Motor beep volume
549 : // Range of motorBeepVolume is 0 to 31.
550 0 : (*motorBeepVolume) = ((_pg_data[_pg_byteindex]) & 0x1F);
551 0 : _pg_byteindex += 1; // close bit field
552 :
553 0 : return 1;
554 :
555 : }// decodeESC_ConfigPacket
556 :
557 : /*!
558 : * \brief Encode a ESC_Config_t into a byte array
559 : *
560 : * General ESC configuration parameters
561 : * \param _pg_data points to the byte array to add encoded data to
562 : * \param _pg_bytecount points to the starting location in the byte array, and will be incremented by the number of encoded bytes.
563 : * \param _pg_user is the data to encode in the byte array
564 : */
565 0 : void encodeESC_Config_t(uint8_t* _pg_data, int* _pg_bytecount, const ESC_Config_t* _pg_user)
566 : {
567 0 : int _pg_byteindex = *_pg_bytecount;
568 :
569 : // 1 = ESC is inhibited (disabled) on startup
570 0 : _pg_data[_pg_byteindex] = (uint8_t)((_pg_user->swInhibit == true) ? 1 : 0) << 7;
571 :
572 : // 1 = ESC will respond to PICCOLO autopilot commands
573 0 : _pg_data[_pg_byteindex] |= (uint8_t)((_pg_user->piccoloCommands == true) ? 1 : 0) << 6;
574 :
575 : // 1 = ESC will accept broadcast command messages
576 0 : _pg_data[_pg_byteindex] |= (uint8_t)((_pg_user->broadcastCommands == true) ? 1 : 0) << 5;
577 :
578 : // 1 = Digital PWM input is enabled
579 0 : _pg_data[_pg_byteindex] |= (uint8_t)((_pg_user->enablePwmInput == true) ? 1 : 0) << 4;
580 :
581 : // Reserved for future use
582 :
583 : // External motor temperature sensor configuration
584 : // Range of motorTempSensor is 0 to 7.
585 0 : _pg_data[_pg_byteindex] |= (uint8_t)limitMax(_pg_user->motorTempSensor, 7);
586 0 : _pg_byteindex += 1; // close bit field
587 :
588 : // ESC keepalive timeout - If this is non-zero, the ESC will automatically revert to *STANDBY* mode if it does not receive a valid command for the alloted period
589 : // Range of keepalive is 0 to 255.
590 0 : uint8ToBytes(_pg_user->keepalive, _pg_data, &_pg_byteindex);
591 :
592 : // 1 = Disable LED
593 0 : _pg_data[_pg_byteindex] = (uint8_t)limitMax(_pg_user->disableLED, 1) << 7;
594 :
595 : // CAN bit (baud) rate
596 : // Range of canBitRate is 0 to 15.
597 0 : _pg_data[_pg_byteindex] |= (uint8_t)limitMax(_pg_user->canBitRate, 15) << 3;
598 :
599 : // Protocol to communicate over CAN
600 : // Range of canProtocol is 0 to 3.
601 0 : _pg_data[_pg_byteindex] |= (uint8_t)limitMax(_pg_user->canProtocol, 3) << 1;
602 :
603 : // Enable motor brake strength (applied when motor is off)
604 0 : _pg_data[_pg_byteindex] |= (uint8_t)((_pg_user->motorBraking == true) ? 1 : 0);
605 0 : _pg_byteindex += 1; // close bit field
606 :
607 : // Delay between hardware inhibit and software inhibit events
608 : // Range of swInhibitDelay is 0 to 255.
609 0 : uint8ToBytes(_pg_user->swInhibitDelay, _pg_data, &_pg_byteindex);
610 :
611 : // Reserved for future use
612 0 : uint8ToBytes((uint8_t)(0), _pg_data, &_pg_byteindex);
613 :
614 : // Reserved for future use
615 0 : uint16ToBeBytes((uint16_t)(0), _pg_data, &_pg_byteindex);
616 :
617 : // Sets the motor beep mode for the ESC
618 : // Range of motorBeepMode is 0 to 7.
619 0 : _pg_data[_pg_byteindex] = (uint8_t)limitMax(_pg_user->motorBeepMode, 7) << 5;
620 :
621 : // Motor beep volume
622 : // Range of motorBeepVolume is 0 to 31.
623 0 : _pg_data[_pg_byteindex] |= (uint8_t)limitMax(_pg_user->motorBeepVolume, 31);
624 0 : _pg_byteindex += 1; // close bit field
625 :
626 0 : *_pg_bytecount = _pg_byteindex;
627 :
628 0 : }// encodeESC_Config_t
629 :
630 : /*!
631 : * \brief Decode a ESC_Config_t from a byte array
632 : *
633 : * General ESC configuration parameters
634 : * \param _pg_data points to the byte array to decoded data from
635 : * \param _pg_bytecount points to the starting location in the byte array, and will be incremented by the number of bytes decoded
636 : * \param _pg_user is the data to decode from the byte array
637 : * \return 1 if the data are decoded, else 0.
638 : */
639 0 : int decodeESC_Config_t(const uint8_t* _pg_data, int* _pg_bytecount, ESC_Config_t* _pg_user)
640 : {
641 0 : int _pg_byteindex = *_pg_bytecount;
642 :
643 : // 1 = ESC is inhibited (disabled) on startup
644 0 : _pg_user->swInhibit = ((_pg_data[_pg_byteindex] >> 7)) ? true : false;
645 :
646 : // 1 = ESC will respond to PICCOLO autopilot commands
647 0 : _pg_user->piccoloCommands = (((_pg_data[_pg_byteindex] >> 6) & 0x1)) ? true : false;
648 :
649 : // 1 = ESC will accept broadcast command messages
650 0 : _pg_user->broadcastCommands = (((_pg_data[_pg_byteindex] >> 5) & 0x1)) ? true : false;
651 :
652 : // 1 = Digital PWM input is enabled
653 0 : _pg_user->enablePwmInput = (((_pg_data[_pg_byteindex] >> 4) & 0x1)) ? true : false;
654 :
655 : // Reserved for future use
656 :
657 : // External motor temperature sensor configuration
658 : // Range of motorTempSensor is 0 to 7.
659 0 : _pg_user->motorTempSensor = ((_pg_data[_pg_byteindex]) & 0x7);
660 0 : _pg_byteindex += 1; // close bit field
661 :
662 : // ESC keepalive timeout - If this is non-zero, the ESC will automatically revert to *STANDBY* mode if it does not receive a valid command for the alloted period
663 : // Range of keepalive is 0 to 255.
664 0 : _pg_user->keepalive = uint8FromBytes(_pg_data, &_pg_byteindex);
665 :
666 : // 1 = Disable LED
667 0 : _pg_user->disableLED = (_pg_data[_pg_byteindex] >> 7);
668 :
669 : // CAN bit (baud) rate
670 : // Range of canBitRate is 0 to 15.
671 0 : _pg_user->canBitRate = ((_pg_data[_pg_byteindex] >> 3) & 0xF);
672 :
673 : // Protocol to communicate over CAN
674 : // Range of canProtocol is 0 to 3.
675 0 : _pg_user->canProtocol = ((_pg_data[_pg_byteindex] >> 1) & 0x3);
676 :
677 : // Enable motor brake strength (applied when motor is off)
678 0 : _pg_user->motorBraking = (((_pg_data[_pg_byteindex]) & 0x1)) ? true : false;
679 0 : _pg_byteindex += 1; // close bit field
680 :
681 : // Delay between hardware inhibit and software inhibit events
682 : // Range of swInhibitDelay is 0 to 255.
683 0 : _pg_user->swInhibitDelay = uint8FromBytes(_pg_data, &_pg_byteindex);
684 :
685 : // Reserved for future use
686 0 : _pg_byteindex += 1;
687 :
688 : // Reserved for future use
689 0 : _pg_byteindex += 2;
690 :
691 : // Sets the motor beep mode for the ESC
692 : // Range of motorBeepMode is 0 to 7.
693 0 : _pg_user->motorBeepMode = (_pg_data[_pg_byteindex] >> 5);
694 :
695 : // Motor beep volume
696 : // Range of motorBeepVolume is 0 to 31.
697 0 : _pg_user->motorBeepVolume = ((_pg_data[_pg_byteindex]) & 0x1F);
698 0 : _pg_byteindex += 1; // close bit field
699 :
700 0 : *_pg_bytecount = _pg_byteindex;
701 :
702 0 : return 1;
703 :
704 : }// decodeESC_Config_t
705 :
706 : /*!
707 : * \brief Create the ESC_TelemetryConfig packet
708 : *
709 : * Telemetry settings (storage class)
710 : * \param _pg_pkt points to the packet which will be created by this function
711 : * \param _pg_user points to the user data that will be encoded in _pg_pkt
712 : */
713 0 : void encodeESC_TelemetryConfigPacketStructure(void* _pg_pkt, const ESC_TelemetryConfig_t* _pg_user)
714 : {
715 0 : uint8_t* _pg_data = getESCVelocityPacketData(_pg_pkt);
716 0 : int _pg_byteindex = 0;
717 :
718 : // Telemetry period code (maps indirectly to a telemetry period value)
719 : // Range of period is 0 to 255.
720 0 : uint8ToBytes(_pg_user->period, _pg_data, &_pg_byteindex);
721 :
722 : // Telemetry silence period (delay after RESET before telemetry data is sent)
723 : // Range of silence is 0 to 255.
724 0 : uint8ToBytes(_pg_user->silence, _pg_data, &_pg_byteindex);
725 :
726 : // Bitfield describing which telemetry packets are enabled
727 0 : encodeESC_TelemetryPackets_t(_pg_data, &_pg_byteindex, &_pg_user->packets);
728 :
729 : // complete the process of creating the packet
730 0 : finishESCVelocityPacket(_pg_pkt, _pg_byteindex, getESC_TelemetryConfigPacketID());
731 :
732 0 : }// encodeESC_TelemetryConfigPacketStructure
733 :
734 : /*!
735 : * \brief Decode the ESC_TelemetryConfig packet
736 : *
737 : * Telemetry settings (storage class)
738 : * \param _pg_pkt points to the packet being decoded by this function
739 : * \param _pg_user receives the data decoded from the packet
740 : * \return 0 is returned if the packet ID or size is wrong, else 1
741 : */
742 0 : int decodeESC_TelemetryConfigPacketStructure(const void* _pg_pkt, ESC_TelemetryConfig_t* _pg_user)
743 : {
744 : int _pg_numbytes;
745 0 : int _pg_byteindex = 0;
746 : const uint8_t* _pg_data;
747 :
748 : // Verify the packet identifier
749 0 : if(getESCVelocityPacketID(_pg_pkt) != getESC_TelemetryConfigPacketID())
750 0 : return 0;
751 :
752 : // Verify the packet size
753 0 : _pg_numbytes = getESCVelocityPacketSize(_pg_pkt);
754 0 : if(_pg_numbytes < getESC_TelemetryConfigMinDataLength())
755 0 : return 0;
756 :
757 : // The raw data from the packet
758 0 : _pg_data = getESCVelocityPacketDataConst(_pg_pkt);
759 :
760 : // Telemetry period code (maps indirectly to a telemetry period value)
761 : // Range of period is 0 to 255.
762 0 : _pg_user->period = uint8FromBytes(_pg_data, &_pg_byteindex);
763 :
764 : // Telemetry silence period (delay after RESET before telemetry data is sent)
765 : // Range of silence is 0 to 255.
766 0 : _pg_user->silence = uint8FromBytes(_pg_data, &_pg_byteindex);
767 :
768 : // Bitfield describing which telemetry packets are enabled
769 0 : if(decodeESC_TelemetryPackets_t(_pg_data, &_pg_byteindex, &_pg_user->packets) == 0)
770 0 : return 0;
771 :
772 0 : return 1;
773 :
774 : }// decodeESC_TelemetryConfigPacketStructure
775 :
776 : /*!
777 : * \brief Create the ESC_TelemetryConfig packet
778 : *
779 : * Telemetry settings (storage class)
780 : * \param _pg_pkt points to the packet which will be created by this function
781 : * \param period is Telemetry period code (maps indirectly to a telemetry period value)
782 : * \param silence is Telemetry silence period (delay after RESET before telemetry data is sent)
783 : * \param packets is Bitfield describing which telemetry packets are enabled
784 : */
785 0 : void encodeESC_TelemetryConfigPacket(void* _pg_pkt, uint8_t period, uint8_t silence, const ESC_TelemetryPackets_t* packets)
786 : {
787 0 : uint8_t* _pg_data = getESCVelocityPacketData(_pg_pkt);
788 0 : int _pg_byteindex = 0;
789 :
790 : // Telemetry period code (maps indirectly to a telemetry period value)
791 : // Range of period is 0 to 255.
792 0 : uint8ToBytes(period, _pg_data, &_pg_byteindex);
793 :
794 : // Telemetry silence period (delay after RESET before telemetry data is sent)
795 : // Range of silence is 0 to 255.
796 0 : uint8ToBytes(silence, _pg_data, &_pg_byteindex);
797 :
798 : // Bitfield describing which telemetry packets are enabled
799 0 : encodeESC_TelemetryPackets_t(_pg_data, &_pg_byteindex, packets);
800 :
801 : // complete the process of creating the packet
802 0 : finishESCVelocityPacket(_pg_pkt, _pg_byteindex, getESC_TelemetryConfigPacketID());
803 :
804 0 : }// encodeESC_TelemetryConfigPacket
805 :
806 : /*!
807 : * \brief Decode the ESC_TelemetryConfig packet
808 : *
809 : * Telemetry settings (storage class)
810 : * \param _pg_pkt points to the packet being decoded by this function
811 : * \param period receives Telemetry period code (maps indirectly to a telemetry period value)
812 : * \param silence receives Telemetry silence period (delay after RESET before telemetry data is sent)
813 : * \param packets receives Bitfield describing which telemetry packets are enabled
814 : * \return 0 is returned if the packet ID or size is wrong, else 1
815 : */
816 0 : int decodeESC_TelemetryConfigPacket(const void* _pg_pkt, uint8_t* period, uint8_t* silence, ESC_TelemetryPackets_t* packets)
817 : {
818 0 : int _pg_byteindex = 0;
819 0 : const uint8_t* _pg_data = getESCVelocityPacketDataConst(_pg_pkt);
820 0 : int _pg_numbytes = getESCVelocityPacketSize(_pg_pkt);
821 :
822 : // Verify the packet identifier
823 0 : if(getESCVelocityPacketID(_pg_pkt) != getESC_TelemetryConfigPacketID())
824 0 : return 0;
825 :
826 0 : if(_pg_numbytes < getESC_TelemetryConfigMinDataLength())
827 0 : return 0;
828 :
829 : // Telemetry period code (maps indirectly to a telemetry period value)
830 : // Range of period is 0 to 255.
831 0 : (*period) = uint8FromBytes(_pg_data, &_pg_byteindex);
832 :
833 : // Telemetry silence period (delay after RESET before telemetry data is sent)
834 : // Range of silence is 0 to 255.
835 0 : (*silence) = uint8FromBytes(_pg_data, &_pg_byteindex);
836 :
837 : // Bitfield describing which telemetry packets are enabled
838 0 : if(decodeESC_TelemetryPackets_t(_pg_data, &_pg_byteindex, packets) == 0)
839 0 : return 0;
840 :
841 0 : return 1;
842 :
843 : }// decodeESC_TelemetryConfigPacket
844 :
845 : /*!
846 : * \brief Encode a ESC_TelemetryConfig_t into a byte array
847 : *
848 : * Telemetry settings (storage class)
849 : * \param _pg_data points to the byte array to add encoded data to
850 : * \param _pg_bytecount points to the starting location in the byte array, and will be incremented by the number of encoded bytes.
851 : * \param _pg_user is the data to encode in the byte array
852 : */
853 0 : void encodeESC_TelemetryConfig_t(uint8_t* _pg_data, int* _pg_bytecount, const ESC_TelemetryConfig_t* _pg_user)
854 : {
855 0 : int _pg_byteindex = *_pg_bytecount;
856 :
857 : // Telemetry period code (maps indirectly to a telemetry period value)
858 : // Range of period is 0 to 255.
859 0 : uint8ToBytes(_pg_user->period, _pg_data, &_pg_byteindex);
860 :
861 : // Telemetry silence period (delay after RESET before telemetry data is sent)
862 : // Range of silence is 0 to 255.
863 0 : uint8ToBytes(_pg_user->silence, _pg_data, &_pg_byteindex);
864 :
865 : // Bitfield describing which telemetry packets are enabled
866 0 : encodeESC_TelemetryPackets_t(_pg_data, &_pg_byteindex, &_pg_user->packets);
867 :
868 0 : *_pg_bytecount = _pg_byteindex;
869 :
870 0 : }// encodeESC_TelemetryConfig_t
871 :
872 : /*!
873 : * \brief Decode a ESC_TelemetryConfig_t from a byte array
874 : *
875 : * Telemetry settings (storage class)
876 : * \param _pg_data points to the byte array to decoded data from
877 : * \param _pg_bytecount points to the starting location in the byte array, and will be incremented by the number of bytes decoded
878 : * \param _pg_user is the data to decode from the byte array
879 : * \return 1 if the data are decoded, else 0.
880 : */
881 0 : int decodeESC_TelemetryConfig_t(const uint8_t* _pg_data, int* _pg_bytecount, ESC_TelemetryConfig_t* _pg_user)
882 : {
883 0 : int _pg_byteindex = *_pg_bytecount;
884 :
885 : // Telemetry period code (maps indirectly to a telemetry period value)
886 : // Range of period is 0 to 255.
887 0 : _pg_user->period = uint8FromBytes(_pg_data, &_pg_byteindex);
888 :
889 : // Telemetry silence period (delay after RESET before telemetry data is sent)
890 : // Range of silence is 0 to 255.
891 0 : _pg_user->silence = uint8FromBytes(_pg_data, &_pg_byteindex);
892 :
893 : // Bitfield describing which telemetry packets are enabled
894 0 : if(decodeESC_TelemetryPackets_t(_pg_data, &_pg_byteindex, &_pg_user->packets) == 0)
895 0 : return 0;
896 :
897 0 : *_pg_bytecount = _pg_byteindex;
898 :
899 0 : return 1;
900 :
901 : }// decodeESC_TelemetryConfig_t
902 :
903 : /*!
904 : * \brief Create the ESC_CommandMultipleESCs packet
905 : *
906 : * This packet can be used to simultaneously command multiple ESCs which have
907 : * sequential CAN ID values. This packet must be sent as a broadcast packet
908 : * (address = 0xFF) such that all ESCs can receive it. These commands can be
909 : * sent to groups of ESCs with ID values up to 64, using different
910 : * ESC_SETPOINT_x packet ID values.
911 : * \param _pg_pkt points to the packet which will be created by this function
912 : * \param pwmValueA is The PWM (pulse width) command for ESC with CAN ID [PKT_ID + 1]
913 : * \param pwmValueB is The PWM (pulse width) command for ESC with CAN ID [PKT_ID + 2]
914 : * \param pwmValueC is The PWM (pulse width) command for ESC with CAN ID [PKT_ID + 3]
915 : * \param pwmValueD is The PWM (pulse width) command for ESC with CAN ID [PKT_ID + 4]
916 : * \param id is the packet identifier for _pg_pkt
917 : */
918 0 : void encodeESC_CommandMultipleESCsPacket(void* _pg_pkt, uint16_t pwmValueA, uint16_t pwmValueB, uint16_t pwmValueC, uint16_t pwmValueD, uint32_t _pg_id)
919 : {
920 0 : uint8_t* _pg_data = getESCVelocityPacketData(_pg_pkt);
921 0 : int _pg_byteindex = 0;
922 :
923 : // The PWM (pulse width) command for ESC with CAN ID [PKT_ID + 1]
924 : // Range of pwmValueA is 0 to 65535.
925 0 : uint16ToBeBytes(pwmValueA, _pg_data, &_pg_byteindex);
926 :
927 : // The PWM (pulse width) command for ESC with CAN ID [PKT_ID + 2]
928 : // Range of pwmValueB is 0 to 65535.
929 0 : uint16ToBeBytes(pwmValueB, _pg_data, &_pg_byteindex);
930 :
931 : // The PWM (pulse width) command for ESC with CAN ID [PKT_ID + 3]
932 : // Range of pwmValueC is 0 to 65535.
933 0 : uint16ToBeBytes(pwmValueC, _pg_data, &_pg_byteindex);
934 :
935 : // The PWM (pulse width) command for ESC with CAN ID [PKT_ID + 4]
936 : // Range of pwmValueD is 0 to 65535.
937 0 : uint16ToBeBytes(pwmValueD, _pg_data, &_pg_byteindex);
938 :
939 : // complete the process of creating the packet
940 0 : finishESCVelocityPacket(_pg_pkt, _pg_byteindex, _pg_id);
941 :
942 0 : }// encodeESC_CommandMultipleESCsPacket
943 :
944 : /*!
945 : * \brief Decode the ESC_CommandMultipleESCs packet
946 : *
947 : * This packet can be used to simultaneously command multiple ESCs which have
948 : * sequential CAN ID values. This packet must be sent as a broadcast packet
949 : * (address = 0xFF) such that all ESCs can receive it. These commands can be
950 : * sent to groups of ESCs with ID values up to 64, using different
951 : * ESC_SETPOINT_x packet ID values.
952 : * \param _pg_pkt points to the packet being decoded by this function
953 : * \param pwmValueA receives The PWM (pulse width) command for ESC with CAN ID [PKT_ID + 1]
954 : * \param pwmValueB receives The PWM (pulse width) command for ESC with CAN ID [PKT_ID + 2]
955 : * \param pwmValueC receives The PWM (pulse width) command for ESC with CAN ID [PKT_ID + 3]
956 : * \param pwmValueD receives The PWM (pulse width) command for ESC with CAN ID [PKT_ID + 4]
957 : * \return 0 is returned if the packet ID or size is wrong, else 1
958 : */
959 0 : int decodeESC_CommandMultipleESCsPacket(const void* _pg_pkt, uint16_t* pwmValueA, uint16_t* pwmValueB, uint16_t* pwmValueC, uint16_t* pwmValueD)
960 : {
961 0 : int _pg_byteindex = 0;
962 0 : const uint8_t* _pg_data = getESCVelocityPacketDataConst(_pg_pkt);
963 0 : int _pg_numbytes = getESCVelocityPacketSize(_pg_pkt);
964 :
965 : // Verify the packet identifier, multiple options exist
966 0 : uint32_t packetid = getESCVelocityPacketID(_pg_pkt);
967 0 : if( packetid != PKT_ESC_SETPOINT_1 &&
968 0 : packetid != PKT_ESC_SETPOINT_2 &&
969 0 : packetid != PKT_ESC_SETPOINT_3 &&
970 0 : packetid != PKT_ESC_SETPOINT_4 &&
971 0 : packetid != PKT_ESC_SETPOINT_5 &&
972 0 : packetid != PKT_ESC_SETPOINT_6 &&
973 0 : packetid != PKT_ESC_SETPOINT_7 &&
974 0 : packetid != PKT_ESC_SETPOINT_8 &&
975 0 : packetid != PKT_ESC_SETPOINT_9 &&
976 0 : packetid != PKT_ESC_SETPOINT_10 &&
977 0 : packetid != PKT_ESC_SETPOINT_11 &&
978 0 : packetid != PKT_ESC_SETPOINT_12 &&
979 0 : packetid != PKT_ESC_SETPOINT_13 &&
980 0 : packetid != PKT_ESC_SETPOINT_14 &&
981 0 : packetid != PKT_ESC_SETPOINT_15 &&
982 : packetid != PKT_ESC_SETPOINT_16 )
983 0 : return 0;
984 :
985 0 : if(_pg_numbytes < getESC_CommandMultipleESCsMinDataLength())
986 0 : return 0;
987 :
988 : // The PWM (pulse width) command for ESC with CAN ID [PKT_ID + 1]
989 : // Range of pwmValueA is 0 to 65535.
990 0 : (*pwmValueA) = uint16FromBeBytes(_pg_data, &_pg_byteindex);
991 :
992 : // The PWM (pulse width) command for ESC with CAN ID [PKT_ID + 2]
993 : // Range of pwmValueB is 0 to 65535.
994 0 : (*pwmValueB) = uint16FromBeBytes(_pg_data, &_pg_byteindex);
995 :
996 : // The PWM (pulse width) command for ESC with CAN ID [PKT_ID + 3]
997 : // Range of pwmValueC is 0 to 65535.
998 0 : (*pwmValueC) = uint16FromBeBytes(_pg_data, &_pg_byteindex);
999 :
1000 : // The PWM (pulse width) command for ESC with CAN ID [PKT_ID + 4]
1001 : // Range of pwmValueD is 0 to 65535.
1002 0 : (*pwmValueD) = uint16FromBeBytes(_pg_data, &_pg_byteindex);
1003 :
1004 0 : return 1;
1005 :
1006 : }// decodeESC_CommandMultipleESCsPacket
1007 :
1008 : /*!
1009 : * \brief Create the ESC_Disable packet
1010 : *
1011 : * Send this packet to the ESC to disable it. The ESC will not accept PWM/RPM
1012 : * commands until it is re-enabled.
1013 : * \param _pg_pkt points to the packet which will be created by this function
1014 : */
1015 0 : void encodeESC_DisablePacket(void* _pg_pkt)
1016 : {
1017 0 : uint8_t* _pg_data = getESCVelocityPacketData(_pg_pkt);
1018 0 : int _pg_byteindex = 0;
1019 :
1020 : // This value must be set for the command to be accepted
1021 0 : uint8ToBytes((uint8_t)(ESC_DISABLE_A), _pg_data, &_pg_byteindex);
1022 :
1023 : // This value must be set for the command to be accepted
1024 0 : uint8ToBytes((uint8_t)(ESC_DISABLE_B), _pg_data, &_pg_byteindex);
1025 :
1026 : // complete the process of creating the packet
1027 0 : finishESCVelocityPacket(_pg_pkt, _pg_byteindex, getESC_DisablePacketID());
1028 :
1029 0 : }// encodeESC_DisablePacket
1030 :
1031 : /*!
1032 : * \brief Decode the ESC_Disable packet
1033 : *
1034 : * Send this packet to the ESC to disable it. The ESC will not accept PWM/RPM
1035 : * commands until it is re-enabled.
1036 : * \param _pg_pkt points to the packet being decoded by this function
1037 : * \return 0 is returned if the packet ID or size is wrong, else 1
1038 : */
1039 0 : int decodeESC_DisablePacket(const void* _pg_pkt)
1040 : {
1041 0 : int _pg_byteindex = 0;
1042 0 : const uint8_t* _pg_data = getESCVelocityPacketDataConst(_pg_pkt);
1043 0 : int _pg_numbytes = getESCVelocityPacketSize(_pg_pkt);
1044 :
1045 : // Verify the packet identifier
1046 0 : if(getESCVelocityPacketID(_pg_pkt) != getESC_DisablePacketID())
1047 0 : return 0;
1048 :
1049 0 : if(_pg_numbytes < getESC_DisableMinDataLength())
1050 0 : return 0;
1051 :
1052 : // This value must be set for the command to be accepted
1053 0 : if (uint8FromBytes(_pg_data, &_pg_byteindex) != (uint8_t) ESC_DISABLE_A)
1054 0 : return 0;
1055 :
1056 : // This value must be set for the command to be accepted
1057 0 : if (uint8FromBytes(_pg_data, &_pg_byteindex) != (uint8_t) ESC_DISABLE_B)
1058 0 : return 0;
1059 :
1060 0 : return 1;
1061 :
1062 : }// decodeESC_DisablePacket
1063 :
1064 : /*!
1065 : * \brief Create the ESC_Enable packet
1066 : *
1067 : * Send this packet to the ESC to enable it. The ESC will be placed in Standby
1068 : * mode.
1069 : * \param _pg_pkt points to the packet which will be created by this function
1070 : */
1071 0 : void encodeESC_EnablePacket(void* _pg_pkt)
1072 : {
1073 0 : uint8_t* _pg_data = getESCVelocityPacketData(_pg_pkt);
1074 0 : int _pg_byteindex = 0;
1075 :
1076 : // This value must be set for the command to be accepted
1077 0 : uint8ToBytes((uint8_t)(ESC_ENABLE_A), _pg_data, &_pg_byteindex);
1078 :
1079 : // This value must be set for the command to be included
1080 0 : uint8ToBytes((uint8_t)(ESC_ENABLE_B), _pg_data, &_pg_byteindex);
1081 :
1082 : // complete the process of creating the packet
1083 0 : finishESCVelocityPacket(_pg_pkt, _pg_byteindex, getESC_EnablePacketID());
1084 :
1085 0 : }// encodeESC_EnablePacket
1086 :
1087 : /*!
1088 : * \brief Decode the ESC_Enable packet
1089 : *
1090 : * Send this packet to the ESC to enable it. The ESC will be placed in Standby
1091 : * mode.
1092 : * \param _pg_pkt points to the packet being decoded by this function
1093 : * \return 0 is returned if the packet ID or size is wrong, else 1
1094 : */
1095 0 : int decodeESC_EnablePacket(const void* _pg_pkt)
1096 : {
1097 0 : int _pg_byteindex = 0;
1098 0 : const uint8_t* _pg_data = getESCVelocityPacketDataConst(_pg_pkt);
1099 0 : int _pg_numbytes = getESCVelocityPacketSize(_pg_pkt);
1100 :
1101 : // Verify the packet identifier
1102 0 : if(getESCVelocityPacketID(_pg_pkt) != getESC_EnablePacketID())
1103 0 : return 0;
1104 :
1105 0 : if(_pg_numbytes < getESC_EnableMinDataLength())
1106 0 : return 0;
1107 :
1108 : // This value must be set for the command to be accepted
1109 0 : if (uint8FromBytes(_pg_data, &_pg_byteindex) != (uint8_t) ESC_ENABLE_A)
1110 0 : return 0;
1111 :
1112 : // This value must be set for the command to be included
1113 0 : if (uint8FromBytes(_pg_data, &_pg_byteindex) != (uint8_t) ESC_ENABLE_B)
1114 0 : return 0;
1115 :
1116 0 : return 1;
1117 :
1118 : }// decodeESC_EnablePacket
1119 :
1120 : /*!
1121 : * \brief Create the ESC_PWMCommand packet
1122 : *
1123 : * Send a PWM (pulse width) command to an individual ESC. The pulse width value
1124 : * in specified in microseconds for compatibility with standard ESC interface.
1125 : * Use the broadcast ID (0xFF) to send this command to all ESCs on the CAN bus.
1126 : * \param _pg_pkt points to the packet which will be created by this function
1127 : * \param pwmCommand is PWM command
1128 : */
1129 0 : void encodeESC_PWMCommandPacket(void* _pg_pkt, uint16_t pwmCommand)
1130 : {
1131 0 : uint8_t* _pg_data = getESCVelocityPacketData(_pg_pkt);
1132 0 : int _pg_byteindex = 0;
1133 :
1134 : // PWM command
1135 : // Range of pwmCommand is 0 to 65535.
1136 0 : uint16ToBeBytes(pwmCommand, _pg_data, &_pg_byteindex);
1137 :
1138 : // complete the process of creating the packet
1139 0 : finishESCVelocityPacket(_pg_pkt, _pg_byteindex, getESC_PWMCommandPacketID());
1140 :
1141 0 : }// encodeESC_PWMCommandPacket
1142 :
1143 : /*!
1144 : * \brief Decode the ESC_PWMCommand packet
1145 : *
1146 : * Send a PWM (pulse width) command to an individual ESC. The pulse width value
1147 : * in specified in microseconds for compatibility with standard ESC interface.
1148 : * Use the broadcast ID (0xFF) to send this command to all ESCs on the CAN bus.
1149 : * \param _pg_pkt points to the packet being decoded by this function
1150 : * \param pwmCommand receives PWM command
1151 : * \return 0 is returned if the packet ID or size is wrong, else 1
1152 : */
1153 0 : int decodeESC_PWMCommandPacket(const void* _pg_pkt, uint16_t* pwmCommand)
1154 : {
1155 0 : int _pg_byteindex = 0;
1156 0 : const uint8_t* _pg_data = getESCVelocityPacketDataConst(_pg_pkt);
1157 0 : int _pg_numbytes = getESCVelocityPacketSize(_pg_pkt);
1158 :
1159 : // Verify the packet identifier
1160 0 : if(getESCVelocityPacketID(_pg_pkt) != getESC_PWMCommandPacketID())
1161 0 : return 0;
1162 :
1163 0 : if(_pg_numbytes < getESC_PWMCommandMinDataLength())
1164 0 : return 0;
1165 :
1166 : // PWM command
1167 : // Range of pwmCommand is 0 to 65535.
1168 0 : (*pwmCommand) = uint16FromBeBytes(_pg_data, &_pg_byteindex);
1169 :
1170 0 : return 1;
1171 :
1172 : }// decodeESC_PWMCommandPacket
1173 :
1174 : /*!
1175 : * \brief Create the ESC_RPMCommand packet
1176 : *
1177 : * Send an RPM (speed) command to an individual ESC. Use the broadcast ID
1178 : * (0xFF) to send this command to all ESCs on the CAN bus
1179 : * \param _pg_pkt points to the packet which will be created by this function
1180 : * \param rpmCommand is RPM Command
1181 : */
1182 0 : void encodeESC_RPMCommandPacket(void* _pg_pkt, uint16_t rpmCommand)
1183 : {
1184 0 : uint8_t* _pg_data = getESCVelocityPacketData(_pg_pkt);
1185 0 : int _pg_byteindex = 0;
1186 :
1187 : // RPM Command
1188 : // Range of rpmCommand is 0 to 65535.
1189 0 : uint16ToBeBytes(rpmCommand, _pg_data, &_pg_byteindex);
1190 :
1191 : // complete the process of creating the packet
1192 0 : finishESCVelocityPacket(_pg_pkt, _pg_byteindex, getESC_RPMCommandPacketID());
1193 :
1194 0 : }// encodeESC_RPMCommandPacket
1195 :
1196 : /*!
1197 : * \brief Decode the ESC_RPMCommand packet
1198 : *
1199 : * Send an RPM (speed) command to an individual ESC. Use the broadcast ID
1200 : * (0xFF) to send this command to all ESCs on the CAN bus
1201 : * \param _pg_pkt points to the packet being decoded by this function
1202 : * \param rpmCommand receives RPM Command
1203 : * \return 0 is returned if the packet ID or size is wrong, else 1
1204 : */
1205 0 : int decodeESC_RPMCommandPacket(const void* _pg_pkt, uint16_t* rpmCommand)
1206 : {
1207 0 : int _pg_byteindex = 0;
1208 0 : const uint8_t* _pg_data = getESCVelocityPacketDataConst(_pg_pkt);
1209 0 : int _pg_numbytes = getESCVelocityPacketSize(_pg_pkt);
1210 :
1211 : // Verify the packet identifier
1212 0 : if(getESCVelocityPacketID(_pg_pkt) != getESC_RPMCommandPacketID())
1213 0 : return 0;
1214 :
1215 0 : if(_pg_numbytes < getESC_RPMCommandMinDataLength())
1216 0 : return 0;
1217 :
1218 : // RPM Command
1219 : // Range of rpmCommand is 0 to 65535.
1220 0 : (*rpmCommand) = uint16FromBeBytes(_pg_data, &_pg_byteindex);
1221 :
1222 0 : return 1;
1223 :
1224 : }// decodeESC_RPMCommandPacket
1225 :
1226 : /*!
1227 : * \brief Create the ESC_VoltageCommand packet
1228 : *
1229 : * Send a voltage command to an individual ESC. Use the broadcast ID (0xFF) to
1230 : * send this command to all ESCs on the CAN bus
1231 : * \param _pg_pkt points to the packet which will be created by this function
1232 : * \param voltageCommand is
1233 : */
1234 0 : void encodeESC_VoltageCommandPacket(void* _pg_pkt, uint16_t voltageCommand)
1235 : {
1236 0 : uint8_t* _pg_data = getESCVelocityPacketData(_pg_pkt);
1237 0 : int _pg_byteindex = 0;
1238 :
1239 : // Range of voltageCommand is 0 to 65535.
1240 0 : uint16ToBeBytes(voltageCommand, _pg_data, &_pg_byteindex);
1241 :
1242 : // complete the process of creating the packet
1243 0 : finishESCVelocityPacket(_pg_pkt, _pg_byteindex, getESC_VoltageCommandPacketID());
1244 :
1245 0 : }// encodeESC_VoltageCommandPacket
1246 :
1247 : /*!
1248 : * \brief Decode the ESC_VoltageCommand packet
1249 : *
1250 : * Send a voltage command to an individual ESC. Use the broadcast ID (0xFF) to
1251 : * send this command to all ESCs on the CAN bus
1252 : * \param _pg_pkt points to the packet being decoded by this function
1253 : * \param voltageCommand receives
1254 : * \return 0 is returned if the packet ID or size is wrong, else 1
1255 : */
1256 0 : int decodeESC_VoltageCommandPacket(const void* _pg_pkt, uint16_t* voltageCommand)
1257 : {
1258 0 : int _pg_byteindex = 0;
1259 0 : const uint8_t* _pg_data = getESCVelocityPacketDataConst(_pg_pkt);
1260 0 : int _pg_numbytes = getESCVelocityPacketSize(_pg_pkt);
1261 :
1262 : // Verify the packet identifier
1263 0 : if(getESCVelocityPacketID(_pg_pkt) != getESC_VoltageCommandPacketID())
1264 0 : return 0;
1265 :
1266 0 : if(_pg_numbytes < getESC_VoltageCommandMinDataLength())
1267 0 : return 0;
1268 :
1269 : // Range of voltageCommand is 0 to 65535.
1270 0 : (*voltageCommand) = uint16FromBeBytes(_pg_data, &_pg_byteindex);
1271 :
1272 0 : return 1;
1273 :
1274 : }// decodeESC_VoltageCommandPacket
1275 :
1276 : /*!
1277 : * \brief Create the ESC_StatusA packet
1278 : *
1279 : * The ESC_STATUS_A packet contains high-priority ESC status information. This
1280 : * packet is transmitted by the ESC at regular (user-configurable) intervals.
1281 : * It can also be requested (polled) from the ESC by sending a zero-length
1282 : * packet of the same type.
1283 : * \param _pg_pkt points to the packet which will be created by this function
1284 : * \param _pg_user points to the user data that will be encoded in _pg_pkt
1285 : */
1286 0 : void encodeESC_StatusAPacketStructure(void* _pg_pkt, const ESC_StatusA_t* _pg_user)
1287 : {
1288 0 : uint8_t* _pg_data = getESCVelocityPacketData(_pg_pkt);
1289 0 : int _pg_byteindex = 0;
1290 :
1291 : // Set to 1 to indicate a Gen-2 ESC
1292 0 : _pg_data[_pg_byteindex] = (uint8_t)1 << 7;
1293 :
1294 : // Reserved for future use
1295 :
1296 : // ESC operating mode. The lower four bits indicate the operational mode of the ESC, in accordance with the ESCOperatingModes enumeration. The upper three bits are used for debugging and should be ignored for general use.
1297 : // Range of mode is 0 to 15.
1298 0 : _pg_data[_pg_byteindex] |= (uint8_t)limitMax(_pg_user->mode, 15);
1299 0 : _pg_byteindex += 1; // close bit field
1300 :
1301 :
1302 : // ESC status bits
1303 0 : encodeESC_StatusBits_t(_pg_data, &_pg_byteindex, &_pg_user->status);
1304 :
1305 : // ESC operational command - value depends on 'mode' available in this packet. If the ESC is disabled, data reads 0x0000. If the ESC is in open-loop PWM mode, this value is the PWM command in units of 1us, in the range 1000us to 2000us. If the ESC is in closed-loop RPM mode, this value is the RPM command in units of 1RPM
1306 : // Range of command is 0 to 65535.
1307 0 : uint16ToBeBytes(_pg_user->command, _pg_data, &_pg_byteindex);
1308 :
1309 : // Motor speed
1310 : // Range of rpm is 0 to 65535.
1311 0 : uint16ToBeBytes(_pg_user->rpm, _pg_data, &_pg_byteindex);
1312 :
1313 : // complete the process of creating the packet
1314 0 : finishESCVelocityPacket(_pg_pkt, _pg_byteindex, getESC_StatusAPacketID());
1315 :
1316 0 : }// encodeESC_StatusAPacketStructure
1317 :
1318 : /*!
1319 : * \brief Decode the ESC_StatusA packet
1320 : *
1321 : * The ESC_STATUS_A packet contains high-priority ESC status information. This
1322 : * packet is transmitted by the ESC at regular (user-configurable) intervals.
1323 : * It can also be requested (polled) from the ESC by sending a zero-length
1324 : * packet of the same type.
1325 : * \param _pg_pkt points to the packet being decoded by this function
1326 : * \param _pg_user receives the data decoded from the packet
1327 : * \return 0 is returned if the packet ID or size is wrong, else 1
1328 : */
1329 0 : int decodeESC_StatusAPacketStructure(const void* _pg_pkt, ESC_StatusA_t* _pg_user)
1330 : {
1331 : int _pg_numbytes;
1332 0 : int _pg_byteindex = 0;
1333 : const uint8_t* _pg_data;
1334 0 : unsigned int _pg_tempbitfield = 0;
1335 :
1336 : // Verify the packet identifier
1337 0 : if(getESCVelocityPacketID(_pg_pkt) != getESC_StatusAPacketID())
1338 0 : return 0;
1339 :
1340 : // Verify the packet size
1341 0 : _pg_numbytes = getESCVelocityPacketSize(_pg_pkt);
1342 0 : if(_pg_numbytes < getESC_StatusAMinDataLength())
1343 0 : return 0;
1344 :
1345 : // The raw data from the packet
1346 0 : _pg_data = getESCVelocityPacketDataConst(_pg_pkt);
1347 :
1348 : // Set to 1 to indicate a Gen-2 ESC
1349 0 : _pg_tempbitfield = (_pg_data[_pg_byteindex] >> 7);
1350 : // Decoded value must be 1
1351 0 : if(_pg_tempbitfield != 1)
1352 0 : return 0;
1353 :
1354 : // Reserved for future use
1355 :
1356 : // ESC operating mode. The lower four bits indicate the operational mode of the ESC, in accordance with the ESCOperatingModes enumeration. The upper three bits are used for debugging and should be ignored for general use.
1357 : // Range of mode is 0 to 15.
1358 0 : _pg_user->mode = ((_pg_data[_pg_byteindex]) & 0xF);
1359 0 : _pg_byteindex += 1; // close bit field
1360 :
1361 : // ESC status bits
1362 0 : if(decodeESC_StatusBits_t(_pg_data, &_pg_byteindex, &_pg_user->status) == 0)
1363 0 : return 0;
1364 :
1365 : // ESC operational command - value depends on 'mode' available in this packet. If the ESC is disabled, data reads 0x0000. If the ESC is in open-loop PWM mode, this value is the PWM command in units of 1us, in the range 1000us to 2000us. If the ESC is in closed-loop RPM mode, this value is the RPM command in units of 1RPM
1366 : // Range of command is 0 to 65535.
1367 0 : _pg_user->command = uint16FromBeBytes(_pg_data, &_pg_byteindex);
1368 :
1369 : // Motor speed
1370 : // Range of rpm is 0 to 65535.
1371 0 : _pg_user->rpm = uint16FromBeBytes(_pg_data, &_pg_byteindex);
1372 :
1373 0 : return 1;
1374 :
1375 : }// decodeESC_StatusAPacketStructure
1376 :
1377 : /*!
1378 : * \brief Create the ESC_StatusA packet
1379 : *
1380 : * The ESC_STATUS_A packet contains high-priority ESC status information. This
1381 : * packet is transmitted by the ESC at regular (user-configurable) intervals.
1382 : * It can also be requested (polled) from the ESC by sending a zero-length
1383 : * packet of the same type.
1384 : * \param _pg_pkt points to the packet which will be created by this function
1385 : * \param mode is ESC operating mode. The lower four bits indicate the operational mode of the ESC, in accordance with the ESCOperatingModes enumeration. The upper three bits are used for debugging and should be ignored for general use.
1386 : * \param status is ESC status bits
1387 : * \param command is ESC operational command - value depends on 'mode' available in this packet. If the ESC is disabled, data reads 0x0000. If the ESC is in open-loop PWM mode, this value is the PWM command in units of 1us, in the range 1000us to 2000us. If the ESC is in closed-loop RPM mode, this value is the RPM command in units of 1RPM
1388 : * \param rpm is Motor speed
1389 : */
1390 0 : void encodeESC_StatusAPacket(void* _pg_pkt, uint8_t mode, const ESC_StatusBits_t* status, uint16_t command, uint16_t rpm)
1391 : {
1392 0 : uint8_t* _pg_data = getESCVelocityPacketData(_pg_pkt);
1393 0 : int _pg_byteindex = 0;
1394 :
1395 : // Set to 1 to indicate a Gen-2 ESC
1396 0 : _pg_data[_pg_byteindex] = (uint8_t)1 << 7;
1397 :
1398 : // Reserved for future use
1399 :
1400 : // ESC operating mode. The lower four bits indicate the operational mode of the ESC, in accordance with the ESCOperatingModes enumeration. The upper three bits are used for debugging and should be ignored for general use.
1401 : // Range of mode is 0 to 15.
1402 0 : _pg_data[_pg_byteindex] |= (uint8_t)limitMax(mode, 15);
1403 0 : _pg_byteindex += 1; // close bit field
1404 :
1405 : // ESC status bits
1406 0 : encodeESC_StatusBits_t(_pg_data, &_pg_byteindex, status);
1407 :
1408 : // ESC operational command - value depends on 'mode' available in this packet. If the ESC is disabled, data reads 0x0000. If the ESC is in open-loop PWM mode, this value is the PWM command in units of 1us, in the range 1000us to 2000us. If the ESC is in closed-loop RPM mode, this value is the RPM command in units of 1RPM
1409 : // Range of command is 0 to 65535.
1410 0 : uint16ToBeBytes(command, _pg_data, &_pg_byteindex);
1411 :
1412 : // Motor speed
1413 : // Range of rpm is 0 to 65535.
1414 0 : uint16ToBeBytes(rpm, _pg_data, &_pg_byteindex);
1415 :
1416 : // complete the process of creating the packet
1417 0 : finishESCVelocityPacket(_pg_pkt, _pg_byteindex, getESC_StatusAPacketID());
1418 :
1419 0 : }// encodeESC_StatusAPacket
1420 :
1421 : /*!
1422 : * \brief Decode the ESC_StatusA packet
1423 : *
1424 : * The ESC_STATUS_A packet contains high-priority ESC status information. This
1425 : * packet is transmitted by the ESC at regular (user-configurable) intervals.
1426 : * It can also be requested (polled) from the ESC by sending a zero-length
1427 : * packet of the same type.
1428 : * \param _pg_pkt points to the packet being decoded by this function
1429 : * \param mode receives ESC operating mode. The lower four bits indicate the operational mode of the ESC, in accordance with the ESCOperatingModes enumeration. The upper three bits are used for debugging and should be ignored for general use.
1430 : * \param status receives ESC status bits
1431 : * \param command receives ESC operational command - value depends on 'mode' available in this packet. If the ESC is disabled, data reads 0x0000. If the ESC is in open-loop PWM mode, this value is the PWM command in units of 1us, in the range 1000us to 2000us. If the ESC is in closed-loop RPM mode, this value is the RPM command in units of 1RPM
1432 : * \param rpm receives Motor speed
1433 : * \return 0 is returned if the packet ID or size is wrong, else 1
1434 : */
1435 0 : int decodeESC_StatusAPacket(const void* _pg_pkt, uint8_t* mode, ESC_StatusBits_t* status, uint16_t* command, uint16_t* rpm)
1436 : {
1437 0 : unsigned int _pg_tempbitfield = 0;
1438 0 : int _pg_byteindex = 0;
1439 0 : const uint8_t* _pg_data = getESCVelocityPacketDataConst(_pg_pkt);
1440 0 : int _pg_numbytes = getESCVelocityPacketSize(_pg_pkt);
1441 :
1442 : // Verify the packet identifier
1443 0 : if(getESCVelocityPacketID(_pg_pkt) != getESC_StatusAPacketID())
1444 0 : return 0;
1445 :
1446 0 : if(_pg_numbytes < getESC_StatusAMinDataLength())
1447 0 : return 0;
1448 :
1449 : // Set to 1 to indicate a Gen-2 ESC
1450 0 : _pg_tempbitfield = (_pg_data[_pg_byteindex] >> 7);
1451 : // Decoded value must be 1
1452 0 : if(_pg_tempbitfield != 1)
1453 0 : return 0;
1454 :
1455 : // Reserved for future use
1456 :
1457 : // ESC operating mode. The lower four bits indicate the operational mode of the ESC, in accordance with the ESCOperatingModes enumeration. The upper three bits are used for debugging and should be ignored for general use.
1458 : // Range of mode is 0 to 15.
1459 0 : (*mode) = ((_pg_data[_pg_byteindex]) & 0xF);
1460 0 : _pg_byteindex += 1; // close bit field
1461 :
1462 : // ESC status bits
1463 0 : if(decodeESC_StatusBits_t(_pg_data, &_pg_byteindex, status) == 0)
1464 0 : return 0;
1465 :
1466 : // ESC operational command - value depends on 'mode' available in this packet. If the ESC is disabled, data reads 0x0000. If the ESC is in open-loop PWM mode, this value is the PWM command in units of 1us, in the range 1000us to 2000us. If the ESC is in closed-loop RPM mode, this value is the RPM command in units of 1RPM
1467 : // Range of command is 0 to 65535.
1468 0 : (*command) = uint16FromBeBytes(_pg_data, &_pg_byteindex);
1469 :
1470 : // Motor speed
1471 : // Range of rpm is 0 to 65535.
1472 0 : (*rpm) = uint16FromBeBytes(_pg_data, &_pg_byteindex);
1473 :
1474 0 : return 1;
1475 :
1476 : }// decodeESC_StatusAPacket
1477 :
1478 : /*!
1479 : * \brief Create the ESC_StatusB packet
1480 : *
1481 : * The ESC_STATUS_B packet contains ESC operational information. This packet is
1482 : * transmitted by the ESC at regular (user-configurable) intervals. It can also
1483 : * be requested (polled) from the ESC by sending a zero-length packet of the
1484 : * same type.
1485 : * \param _pg_pkt points to the packet which will be created by this function
1486 : * \param _pg_user points to the user data that will be encoded in _pg_pkt
1487 : */
1488 0 : void encodeESC_StatusBPacketStructure(void* _pg_pkt, const ESC_StatusB_t* _pg_user)
1489 : {
1490 0 : uint8_t* _pg_data = getESCVelocityPacketData(_pg_pkt);
1491 0 : int _pg_byteindex = 0;
1492 :
1493 : // ESC Rail Voltage
1494 : // Range of voltage is 0 to 65535.
1495 0 : uint16ToBeBytes(_pg_user->voltage, _pg_data, &_pg_byteindex);
1496 :
1497 : // ESC Current. Current IN to the ESC is positive. Current OUT of the ESC is negative
1498 : // Range of current is -32768 to 32767.
1499 0 : int16ToBeBytes(_pg_user->current, _pg_data, &_pg_byteindex);
1500 :
1501 : // ESC Motor Duty Cycle
1502 : // Range of dutyCycle is 0 to 65535.
1503 0 : uint16ToBeBytes(_pg_user->dutyCycle, _pg_data, &_pg_byteindex);
1504 :
1505 : // ESC Logic Board Temperature
1506 : // Range of escTemperature is -128 to 127.
1507 0 : int8ToBytes(_pg_user->escTemperature, _pg_data, &_pg_byteindex);
1508 :
1509 : // ESC Motor Temperature
1510 : // Range of motorTemperature is 0 to 255.
1511 0 : uint8ToBytes(_pg_user->motorTemperature, _pg_data, &_pg_byteindex);
1512 :
1513 : // complete the process of creating the packet
1514 0 : finishESCVelocityPacket(_pg_pkt, _pg_byteindex, getESC_StatusBPacketID());
1515 :
1516 0 : }// encodeESC_StatusBPacketStructure
1517 :
1518 : /*!
1519 : * \brief Decode the ESC_StatusB packet
1520 : *
1521 : * The ESC_STATUS_B packet contains ESC operational information. This packet is
1522 : * transmitted by the ESC at regular (user-configurable) intervals. It can also
1523 : * be requested (polled) from the ESC by sending a zero-length packet of the
1524 : * same type.
1525 : * \param _pg_pkt points to the packet being decoded by this function
1526 : * \param _pg_user receives the data decoded from the packet
1527 : * \return 0 is returned if the packet ID or size is wrong, else 1
1528 : */
1529 0 : int decodeESC_StatusBPacketStructure(const void* _pg_pkt, ESC_StatusB_t* _pg_user)
1530 : {
1531 : int _pg_numbytes;
1532 0 : int _pg_byteindex = 0;
1533 : const uint8_t* _pg_data;
1534 :
1535 : // Verify the packet identifier
1536 0 : if(getESCVelocityPacketID(_pg_pkt) != getESC_StatusBPacketID())
1537 0 : return 0;
1538 :
1539 : // Verify the packet size
1540 0 : _pg_numbytes = getESCVelocityPacketSize(_pg_pkt);
1541 0 : if(_pg_numbytes < getESC_StatusBMinDataLength())
1542 0 : return 0;
1543 :
1544 : // The raw data from the packet
1545 0 : _pg_data = getESCVelocityPacketDataConst(_pg_pkt);
1546 :
1547 : // ESC Rail Voltage
1548 : // Range of voltage is 0 to 65535.
1549 0 : _pg_user->voltage = uint16FromBeBytes(_pg_data, &_pg_byteindex);
1550 :
1551 : // ESC Current. Current IN to the ESC is positive. Current OUT of the ESC is negative
1552 : // Range of current is -32768 to 32767.
1553 0 : _pg_user->current = int16FromBeBytes(_pg_data, &_pg_byteindex);
1554 :
1555 : // ESC Motor Duty Cycle
1556 : // Range of dutyCycle is 0 to 65535.
1557 0 : _pg_user->dutyCycle = uint16FromBeBytes(_pg_data, &_pg_byteindex);
1558 :
1559 : // ESC Logic Board Temperature
1560 : // Range of escTemperature is -128 to 127.
1561 0 : _pg_user->escTemperature = int8FromBytes(_pg_data, &_pg_byteindex);
1562 :
1563 : // ESC Motor Temperature
1564 : // Range of motorTemperature is 0 to 255.
1565 0 : _pg_user->motorTemperature = uint8FromBytes(_pg_data, &_pg_byteindex);
1566 :
1567 0 : return 1;
1568 :
1569 : }// decodeESC_StatusBPacketStructure
1570 :
1571 : /*!
1572 : * \brief Create the ESC_StatusB packet
1573 : *
1574 : * The ESC_STATUS_B packet contains ESC operational information. This packet is
1575 : * transmitted by the ESC at regular (user-configurable) intervals. It can also
1576 : * be requested (polled) from the ESC by sending a zero-length packet of the
1577 : * same type.
1578 : * \param _pg_pkt points to the packet which will be created by this function
1579 : * \param voltage is ESC Rail Voltage
1580 : * \param current is ESC Current. Current IN to the ESC is positive. Current OUT of the ESC is negative
1581 : * \param dutyCycle is ESC Motor Duty Cycle
1582 : * \param escTemperature is ESC Logic Board Temperature
1583 : * \param motorTemperature is ESC Motor Temperature
1584 : */
1585 0 : void encodeESC_StatusBPacket(void* _pg_pkt, uint16_t voltage, int16_t current, uint16_t dutyCycle, int8_t escTemperature, uint8_t motorTemperature)
1586 : {
1587 0 : uint8_t* _pg_data = getESCVelocityPacketData(_pg_pkt);
1588 0 : int _pg_byteindex = 0;
1589 :
1590 : // ESC Rail Voltage
1591 : // Range of voltage is 0 to 65535.
1592 0 : uint16ToBeBytes(voltage, _pg_data, &_pg_byteindex);
1593 :
1594 : // ESC Current. Current IN to the ESC is positive. Current OUT of the ESC is negative
1595 : // Range of current is -32768 to 32767.
1596 0 : int16ToBeBytes(current, _pg_data, &_pg_byteindex);
1597 :
1598 : // ESC Motor Duty Cycle
1599 : // Range of dutyCycle is 0 to 65535.
1600 0 : uint16ToBeBytes(dutyCycle, _pg_data, &_pg_byteindex);
1601 :
1602 : // ESC Logic Board Temperature
1603 : // Range of escTemperature is -128 to 127.
1604 0 : int8ToBytes(escTemperature, _pg_data, &_pg_byteindex);
1605 :
1606 : // ESC Motor Temperature
1607 : // Range of motorTemperature is 0 to 255.
1608 0 : uint8ToBytes(motorTemperature, _pg_data, &_pg_byteindex);
1609 :
1610 : // complete the process of creating the packet
1611 0 : finishESCVelocityPacket(_pg_pkt, _pg_byteindex, getESC_StatusBPacketID());
1612 :
1613 0 : }// encodeESC_StatusBPacket
1614 :
1615 : /*!
1616 : * \brief Decode the ESC_StatusB packet
1617 : *
1618 : * The ESC_STATUS_B packet contains ESC operational information. This packet is
1619 : * transmitted by the ESC at regular (user-configurable) intervals. It can also
1620 : * be requested (polled) from the ESC by sending a zero-length packet of the
1621 : * same type.
1622 : * \param _pg_pkt points to the packet being decoded by this function
1623 : * \param voltage receives ESC Rail Voltage
1624 : * \param current receives ESC Current. Current IN to the ESC is positive. Current OUT of the ESC is negative
1625 : * \param dutyCycle receives ESC Motor Duty Cycle
1626 : * \param escTemperature receives ESC Logic Board Temperature
1627 : * \param motorTemperature receives ESC Motor Temperature
1628 : * \return 0 is returned if the packet ID or size is wrong, else 1
1629 : */
1630 0 : int decodeESC_StatusBPacket(const void* _pg_pkt, uint16_t* voltage, int16_t* current, uint16_t* dutyCycle, int8_t* escTemperature, uint8_t* motorTemperature)
1631 : {
1632 0 : int _pg_byteindex = 0;
1633 0 : const uint8_t* _pg_data = getESCVelocityPacketDataConst(_pg_pkt);
1634 0 : int _pg_numbytes = getESCVelocityPacketSize(_pg_pkt);
1635 :
1636 : // Verify the packet identifier
1637 0 : if(getESCVelocityPacketID(_pg_pkt) != getESC_StatusBPacketID())
1638 0 : return 0;
1639 :
1640 0 : if(_pg_numbytes < getESC_StatusBMinDataLength())
1641 0 : return 0;
1642 :
1643 : // ESC Rail Voltage
1644 : // Range of voltage is 0 to 65535.
1645 0 : (*voltage) = uint16FromBeBytes(_pg_data, &_pg_byteindex);
1646 :
1647 : // ESC Current. Current IN to the ESC is positive. Current OUT of the ESC is negative
1648 : // Range of current is -32768 to 32767.
1649 0 : (*current) = int16FromBeBytes(_pg_data, &_pg_byteindex);
1650 :
1651 : // ESC Motor Duty Cycle
1652 : // Range of dutyCycle is 0 to 65535.
1653 0 : (*dutyCycle) = uint16FromBeBytes(_pg_data, &_pg_byteindex);
1654 :
1655 : // ESC Logic Board Temperature
1656 : // Range of escTemperature is -128 to 127.
1657 0 : (*escTemperature) = int8FromBytes(_pg_data, &_pg_byteindex);
1658 :
1659 : // ESC Motor Temperature
1660 : // Range of motorTemperature is 0 to 255.
1661 0 : (*motorTemperature) = uint8FromBytes(_pg_data, &_pg_byteindex);
1662 :
1663 0 : return 1;
1664 :
1665 : }// decodeESC_StatusBPacket
1666 :
1667 : /*!
1668 : * \brief Create the ESC_StatusC packet
1669 : *
1670 : * The ESC_STATUS_C packet contains ESC operational information. This packet is
1671 : * transmitted by the ESC at regular intervals (user-configurable). It can also
1672 : * be requested (polled) from the ESC by sending a zero-length packet of the
1673 : * same type.
1674 : * \param _pg_pkt points to the packet which will be created by this function
1675 : * \param _pg_user points to the user data that will be encoded in _pg_pkt
1676 : */
1677 0 : void encodeESC_StatusCPacketStructure(void* _pg_pkt, const ESC_StatusC_t* _pg_user)
1678 : {
1679 0 : uint8_t* _pg_data = getESCVelocityPacketData(_pg_pkt);
1680 0 : int _pg_byteindex = 0;
1681 :
1682 : // Reserved for future use
1683 0 : int16ToBeBytes((int16_t)(0), _pg_data, &_pg_byteindex);
1684 :
1685 : // ESC Phase Board Temperature
1686 : // Range of fetTemperature is -50.0f to 205.0f.
1687 0 : float32ScaledTo1UnsignedBytes(_pg_user->fetTemperature, _pg_data, &_pg_byteindex, -50.0f, 1.0f);
1688 :
1689 : // Current motor PWM frequency (10 Hz per bit)
1690 : // Range of pwmFrequency is 0 to 65535.
1691 0 : uint16ToBeBytes(_pg_user->pwmFrequency, _pg_data, &_pg_byteindex);
1692 :
1693 : // Current timing advance (0.1 degree per bit)
1694 : // Range of timingAdvance is 0 to 65535.
1695 0 : uint16ToBeBytes(_pg_user->timingAdvance, _pg_data, &_pg_byteindex);
1696 :
1697 : // complete the process of creating the packet
1698 0 : finishESCVelocityPacket(_pg_pkt, _pg_byteindex, getESC_StatusCPacketID());
1699 :
1700 0 : }// encodeESC_StatusCPacketStructure
1701 :
1702 : /*!
1703 : * \brief Decode the ESC_StatusC packet
1704 : *
1705 : * The ESC_STATUS_C packet contains ESC operational information. This packet is
1706 : * transmitted by the ESC at regular intervals (user-configurable). It can also
1707 : * be requested (polled) from the ESC by sending a zero-length packet of the
1708 : * same type.
1709 : * \param _pg_pkt points to the packet being decoded by this function
1710 : * \param _pg_user receives the data decoded from the packet
1711 : * \return 0 is returned if the packet ID or size is wrong, else 1
1712 : */
1713 0 : int decodeESC_StatusCPacketStructure(const void* _pg_pkt, ESC_StatusC_t* _pg_user)
1714 : {
1715 : int _pg_numbytes;
1716 0 : int _pg_byteindex = 0;
1717 : const uint8_t* _pg_data;
1718 :
1719 : // Verify the packet identifier
1720 0 : if(getESCVelocityPacketID(_pg_pkt) != getESC_StatusCPacketID())
1721 0 : return 0;
1722 :
1723 : // Verify the packet size
1724 0 : _pg_numbytes = getESCVelocityPacketSize(_pg_pkt);
1725 0 : if(_pg_numbytes < getESC_StatusCMinDataLength())
1726 0 : return 0;
1727 :
1728 : // The raw data from the packet
1729 0 : _pg_data = getESCVelocityPacketDataConst(_pg_pkt);
1730 :
1731 : // Reserved for future use
1732 0 : _pg_byteindex += 2;
1733 :
1734 : // ESC Phase Board Temperature
1735 : // Range of fetTemperature is -50.0f to 205.0f.
1736 0 : _pg_user->fetTemperature = float32ScaledFrom1UnsignedBytes(_pg_data, &_pg_byteindex, -50.0f, 1.0f/1.0f);
1737 :
1738 : // Current motor PWM frequency (10 Hz per bit)
1739 : // Range of pwmFrequency is 0 to 65535.
1740 0 : _pg_user->pwmFrequency = uint16FromBeBytes(_pg_data, &_pg_byteindex);
1741 :
1742 : // Current timing advance (0.1 degree per bit)
1743 : // Range of timingAdvance is 0 to 65535.
1744 0 : _pg_user->timingAdvance = uint16FromBeBytes(_pg_data, &_pg_byteindex);
1745 :
1746 0 : return 1;
1747 :
1748 : }// decodeESC_StatusCPacketStructure
1749 :
1750 : /*!
1751 : * \brief Create the ESC_StatusC packet
1752 : *
1753 : * The ESC_STATUS_C packet contains ESC operational information. This packet is
1754 : * transmitted by the ESC at regular intervals (user-configurable). It can also
1755 : * be requested (polled) from the ESC by sending a zero-length packet of the
1756 : * same type.
1757 : * \param _pg_pkt points to the packet which will be created by this function
1758 : * \param fetTemperature is ESC Phase Board Temperature
1759 : * \param pwmFrequency is Current motor PWM frequency (10 Hz per bit)
1760 : * \param timingAdvance is Current timing advance (0.1 degree per bit)
1761 : */
1762 0 : void encodeESC_StatusCPacket(void* _pg_pkt, float fetTemperature, uint16_t pwmFrequency, uint16_t timingAdvance)
1763 : {
1764 0 : uint8_t* _pg_data = getESCVelocityPacketData(_pg_pkt);
1765 0 : int _pg_byteindex = 0;
1766 :
1767 : // Reserved for future use
1768 0 : int16ToBeBytes((int16_t)(0), _pg_data, &_pg_byteindex);
1769 :
1770 : // ESC Phase Board Temperature
1771 : // Range of fetTemperature is -50.0f to 205.0f.
1772 0 : float32ScaledTo1UnsignedBytes(fetTemperature, _pg_data, &_pg_byteindex, -50.0f, 1.0f);
1773 :
1774 : // Current motor PWM frequency (10 Hz per bit)
1775 : // Range of pwmFrequency is 0 to 65535.
1776 0 : uint16ToBeBytes(pwmFrequency, _pg_data, &_pg_byteindex);
1777 :
1778 : // Current timing advance (0.1 degree per bit)
1779 : // Range of timingAdvance is 0 to 65535.
1780 0 : uint16ToBeBytes(timingAdvance, _pg_data, &_pg_byteindex);
1781 :
1782 : // complete the process of creating the packet
1783 0 : finishESCVelocityPacket(_pg_pkt, _pg_byteindex, getESC_StatusCPacketID());
1784 :
1785 0 : }// encodeESC_StatusCPacket
1786 :
1787 : /*!
1788 : * \brief Decode the ESC_StatusC packet
1789 : *
1790 : * The ESC_STATUS_C packet contains ESC operational information. This packet is
1791 : * transmitted by the ESC at regular intervals (user-configurable). It can also
1792 : * be requested (polled) from the ESC by sending a zero-length packet of the
1793 : * same type.
1794 : * \param _pg_pkt points to the packet being decoded by this function
1795 : * \param fetTemperature receives ESC Phase Board Temperature
1796 : * \param pwmFrequency receives Current motor PWM frequency (10 Hz per bit)
1797 : * \param timingAdvance receives Current timing advance (0.1 degree per bit)
1798 : * \return 0 is returned if the packet ID or size is wrong, else 1
1799 : */
1800 0 : int decodeESC_StatusCPacket(const void* _pg_pkt, float* fetTemperature, uint16_t* pwmFrequency, uint16_t* timingAdvance)
1801 : {
1802 0 : int _pg_byteindex = 0;
1803 0 : const uint8_t* _pg_data = getESCVelocityPacketDataConst(_pg_pkt);
1804 0 : int _pg_numbytes = getESCVelocityPacketSize(_pg_pkt);
1805 :
1806 : // Verify the packet identifier
1807 0 : if(getESCVelocityPacketID(_pg_pkt) != getESC_StatusCPacketID())
1808 0 : return 0;
1809 :
1810 0 : if(_pg_numbytes < getESC_StatusCMinDataLength())
1811 0 : return 0;
1812 :
1813 : // Reserved for future use
1814 0 : _pg_byteindex += 2;
1815 :
1816 : // ESC Phase Board Temperature
1817 : // Range of fetTemperature is -50.0f to 205.0f.
1818 0 : (*fetTemperature) = float32ScaledFrom1UnsignedBytes(_pg_data, &_pg_byteindex, -50.0f, 1.0f/1.0f);
1819 :
1820 : // Current motor PWM frequency (10 Hz per bit)
1821 : // Range of pwmFrequency is 0 to 65535.
1822 0 : (*pwmFrequency) = uint16FromBeBytes(_pg_data, &_pg_byteindex);
1823 :
1824 : // Current timing advance (0.1 degree per bit)
1825 : // Range of timingAdvance is 0 to 65535.
1826 0 : (*timingAdvance) = uint16FromBeBytes(_pg_data, &_pg_byteindex);
1827 :
1828 0 : return 1;
1829 :
1830 : }// decodeESC_StatusCPacket
1831 :
1832 : /*!
1833 : * \brief Create the ESC_StatusD packet
1834 : *
1835 : * The ESC_STATUS_D packet contains ESC operational information. This packet is
1836 : * transmitted by the ESC at regular intervals (user-configurable). It can also
1837 : * be requested (polled) from the ESC by sending a zero-length packet of the
1838 : * same type.
1839 : * \param _pg_pkt points to the packet which will be created by this function
1840 : * \param _pg_user points to the user data that will be encoded in _pg_pkt
1841 : */
1842 0 : void encodeESC_StatusDPacketStructure(void* _pg_pkt, const ESC_StatusD_t* _pg_user)
1843 : {
1844 0 : uint8_t* _pg_data = getESCVelocityPacketData(_pg_pkt);
1845 0 : int _pg_byteindex = 0;
1846 0 : unsigned _pg_i = 0;
1847 :
1848 : // Reserved for future use
1849 0 : _pg_data[_pg_byteindex] = 0;
1850 :
1851 : // Measured demag angle of motor
1852 : // Range of demagAngle is 0 to 127.
1853 0 : _pg_data[_pg_byteindex] |= (uint8_t)limitMax(_pg_user->demagAngle, 127);
1854 0 : _pg_byteindex += 1; // close bit field
1855 :
1856 :
1857 : // Reserved for future packet expansion
1858 0 : for(_pg_i = 0; _pg_i < 7; _pg_i++)
1859 0 : uint8ToBytes((uint8_t)(0), _pg_data, &_pg_byteindex);
1860 :
1861 : // complete the process of creating the packet
1862 0 : finishESCVelocityPacket(_pg_pkt, _pg_byteindex, getESC_StatusDPacketID());
1863 :
1864 0 : }// encodeESC_StatusDPacketStructure
1865 :
1866 : /*!
1867 : * \brief Decode the ESC_StatusD packet
1868 : *
1869 : * The ESC_STATUS_D packet contains ESC operational information. This packet is
1870 : * transmitted by the ESC at regular intervals (user-configurable). It can also
1871 : * be requested (polled) from the ESC by sending a zero-length packet of the
1872 : * same type.
1873 : * \param _pg_pkt points to the packet being decoded by this function
1874 : * \param _pg_user receives the data decoded from the packet
1875 : * \return 0 is returned if the packet ID or size is wrong, else 1
1876 : */
1877 0 : int decodeESC_StatusDPacketStructure(const void* _pg_pkt, ESC_StatusD_t* _pg_user)
1878 : {
1879 : int _pg_numbytes;
1880 0 : int _pg_byteindex = 0;
1881 : const uint8_t* _pg_data;
1882 :
1883 : // Verify the packet identifier
1884 0 : if(getESCVelocityPacketID(_pg_pkt) != getESC_StatusDPacketID())
1885 0 : return 0;
1886 :
1887 : // Verify the packet size
1888 0 : _pg_numbytes = getESCVelocityPacketSize(_pg_pkt);
1889 0 : if(_pg_numbytes < getESC_StatusDMinDataLength())
1890 0 : return 0;
1891 :
1892 : // The raw data from the packet
1893 0 : _pg_data = getESCVelocityPacketDataConst(_pg_pkt);
1894 :
1895 : // Reserved for future use
1896 :
1897 : // Measured demag angle of motor
1898 : // Range of demagAngle is 0 to 127.
1899 0 : _pg_user->demagAngle = ((_pg_data[_pg_byteindex]) & 0x7F);
1900 0 : _pg_byteindex += 1; // close bit field
1901 :
1902 : // Reserved for future packet expansion
1903 0 : _pg_byteindex += 1*7;
1904 :
1905 0 : return 1;
1906 :
1907 : }// decodeESC_StatusDPacketStructure
1908 :
1909 : /*!
1910 : * \brief Create the ESC_StatusD packet
1911 : *
1912 : * The ESC_STATUS_D packet contains ESC operational information. This packet is
1913 : * transmitted by the ESC at regular intervals (user-configurable). It can also
1914 : * be requested (polled) from the ESC by sending a zero-length packet of the
1915 : * same type.
1916 : * \param _pg_pkt points to the packet which will be created by this function
1917 : * \param demagAngle is Measured demag angle of motor
1918 : */
1919 0 : void encodeESC_StatusDPacket(void* _pg_pkt, uint8_t demagAngle)
1920 : {
1921 0 : uint8_t* _pg_data = getESCVelocityPacketData(_pg_pkt);
1922 0 : int _pg_byteindex = 0;
1923 0 : unsigned _pg_i = 0;
1924 :
1925 : // Reserved for future use
1926 0 : _pg_data[_pg_byteindex] = 0;
1927 :
1928 : // Measured demag angle of motor
1929 : // Range of demagAngle is 0 to 127.
1930 0 : _pg_data[_pg_byteindex] |= (uint8_t)limitMax(demagAngle, 127);
1931 0 : _pg_byteindex += 1; // close bit field
1932 :
1933 : // Reserved for future packet expansion
1934 0 : for(_pg_i = 0; _pg_i < 7; _pg_i++)
1935 0 : uint8ToBytes((uint8_t)(0), _pg_data, &_pg_byteindex);
1936 :
1937 : // complete the process of creating the packet
1938 0 : finishESCVelocityPacket(_pg_pkt, _pg_byteindex, getESC_StatusDPacketID());
1939 :
1940 0 : }// encodeESC_StatusDPacket
1941 :
1942 : /*!
1943 : * \brief Decode the ESC_StatusD packet
1944 : *
1945 : * The ESC_STATUS_D packet contains ESC operational information. This packet is
1946 : * transmitted by the ESC at regular intervals (user-configurable). It can also
1947 : * be requested (polled) from the ESC by sending a zero-length packet of the
1948 : * same type.
1949 : * \param _pg_pkt points to the packet being decoded by this function
1950 : * \param demagAngle receives Measured demag angle of motor
1951 : * \return 0 is returned if the packet ID or size is wrong, else 1
1952 : */
1953 0 : int decodeESC_StatusDPacket(const void* _pg_pkt, uint8_t* demagAngle)
1954 : {
1955 0 : int _pg_byteindex = 0;
1956 0 : const uint8_t* _pg_data = getESCVelocityPacketDataConst(_pg_pkt);
1957 0 : int _pg_numbytes = getESCVelocityPacketSize(_pg_pkt);
1958 :
1959 : // Verify the packet identifier
1960 0 : if(getESCVelocityPacketID(_pg_pkt) != getESC_StatusDPacketID())
1961 0 : return 0;
1962 :
1963 0 : if(_pg_numbytes < getESC_StatusDMinDataLength())
1964 0 : return 0;
1965 :
1966 : // Reserved for future use
1967 :
1968 : // Measured demag angle of motor
1969 : // Range of demagAngle is 0 to 127.
1970 0 : (*demagAngle) = ((_pg_data[_pg_byteindex]) & 0x7F);
1971 0 : _pg_byteindex += 1; // close bit field
1972 :
1973 : // Reserved for future packet expansion
1974 0 : _pg_byteindex += 1*7;
1975 :
1976 0 : return 1;
1977 :
1978 : }// decodeESC_StatusDPacket
1979 :
1980 : /*!
1981 : * \brief Create the ESC_WarningErrorStatus packet
1982 : *
1983 : * Warning and error status information. If any warning or error flags are set,
1984 : * this packet is transmitted by the ESC at 10Hz. If there are no warning or
1985 : * error flags set, the packet is transmitted by the ESC at 1Hz.
1986 : * \param _pg_pkt points to the packet which will be created by this function
1987 : * \param warnings is ESC warning bitfield
1988 : * \param errors is ESC error bitfield
1989 : * \param extended is Set to indicate that this packet contains extended warning and error information
1990 : * \param warningsExtended is Extension of ESC warning bitfield
1991 : * \param errorsExtended is Extension of ESC error bifield
1992 : */
1993 0 : void encodeESC_WarningErrorStatusPacket(void* _pg_pkt, const ESC_WarningBits_t* warnings, const ESC_ErrorBits_t* errors, uint8_t extended, const ESC_WarningBits_t* warningsExtended, const ESC_ErrorBits_t* errorsExtended)
1994 : {
1995 0 : uint8_t* _pg_data = getESCVelocityPacketData(_pg_pkt);
1996 0 : int _pg_byteindex = 0;
1997 :
1998 : // ESC warning bitfield
1999 0 : encodeESC_WarningBits_t(_pg_data, &_pg_byteindex, warnings);
2000 :
2001 : // ESC error bitfield
2002 0 : encodeESC_ErrorBits_t(_pg_data, &_pg_byteindex, errors);
2003 :
2004 : // Set to indicate that this packet contains extended warning and error information
2005 : // Range of extended is 0 to 255.
2006 0 : uint8ToBytes(extended, _pg_data, &_pg_byteindex);
2007 :
2008 : // Extension of ESC warning bitfield
2009 0 : if(extended)
2010 : {
2011 0 : encodeESC_ExtendedWarningBits_t(_pg_data, &_pg_byteindex, warningsExtended);
2012 : }
2013 :
2014 : // Extension of ESC error bifield
2015 0 : if(extended)
2016 : {
2017 0 : encodeESC_ExtendedErrorBits_t(_pg_data, &_pg_byteindex, errorsExtended);
2018 : }
2019 :
2020 : // complete the process of creating the packet
2021 0 : finishESCVelocityPacket(_pg_pkt, _pg_byteindex, getESC_WarningErrorStatusPacketID());
2022 :
2023 0 : }// encodeESC_WarningErrorStatusPacket
2024 :
2025 : /*!
2026 : * \brief Decode the ESC_WarningErrorStatus packet
2027 : *
2028 : * Warning and error status information. If any warning or error flags are set,
2029 : * this packet is transmitted by the ESC at 10Hz. If there are no warning or
2030 : * error flags set, the packet is transmitted by the ESC at 1Hz.
2031 : * \param _pg_pkt points to the packet being decoded by this function
2032 : * \param warnings receives ESC warning bitfield
2033 : * \param errors receives ESC error bitfield
2034 : * \param extended receives Set to indicate that this packet contains extended warning and error information
2035 : * \param warningsExtended receives Extension of ESC warning bitfield
2036 : * \param errorsExtended receives Extension of ESC error bifield
2037 : * \return 0 is returned if the packet ID or size is wrong, else 1
2038 : */
2039 0 : int decodeESC_WarningErrorStatusPacket(const void* _pg_pkt, ESC_WarningBits_t* warnings, ESC_ErrorBits_t* errors, uint8_t* extended, ESC_WarningBits_t* warningsExtended, ESC_ErrorBits_t* errorsExtended)
2040 : {
2041 0 : int _pg_byteindex = 0;
2042 0 : const uint8_t* _pg_data = getESCVelocityPacketDataConst(_pg_pkt);
2043 0 : int _pg_numbytes = getESCVelocityPacketSize(_pg_pkt);
2044 :
2045 : // Verify the packet identifier
2046 0 : if(getESCVelocityPacketID(_pg_pkt) != getESC_WarningErrorStatusPacketID())
2047 0 : return 0;
2048 :
2049 0 : if(_pg_numbytes < getESC_WarningErrorStatusMinDataLength())
2050 0 : return 0;
2051 :
2052 : // this packet has default fields, make sure they are set
2053 0 : (*extended) = 0;
2054 :
2055 : // ESC warning bitfield
2056 0 : if(decodeESC_WarningBits_t(_pg_data, &_pg_byteindex, warnings) == 0)
2057 0 : return 0;
2058 :
2059 : // ESC error bitfield
2060 0 : if(decodeESC_ErrorBits_t(_pg_data, &_pg_byteindex, errors) == 0)
2061 0 : return 0;
2062 :
2063 0 : if(_pg_byteindex + 1 > _pg_numbytes)
2064 0 : return 1;
2065 :
2066 : // Set to indicate that this packet contains extended warning and error information
2067 : // Range of extended is 0 to 255.
2068 0 : (*extended) = uint8FromBytes(_pg_data, &_pg_byteindex);
2069 :
2070 : // Extension of ESC warning bitfield
2071 0 : if((*extended))
2072 : {
2073 0 : if(decodeESC_ExtendedWarningBits_t(_pg_data, &_pg_byteindex, warningsExtended) == 0)
2074 0 : return 0;
2075 : }
2076 :
2077 : // Extension of ESC error bifield
2078 0 : if((*extended))
2079 : {
2080 0 : if(decodeESC_ExtendedErrorBits_t(_pg_data, &_pg_byteindex, errorsExtended) == 0)
2081 0 : return 0;
2082 : }
2083 :
2084 0 : return 1;
2085 :
2086 : }// decodeESC_WarningErrorStatusPacket
2087 :
2088 : /*!
2089 : * \brief Create the ESC_ControlLoopOutputs packet
2090 : *
2091 : * This packet contains the current output values for the RPM controller.
2092 : * \param _pg_pkt points to the packet which will be created by this function
2093 : * \param _pg_user points to the user data that will be encoded in _pg_pkt
2094 : */
2095 0 : void encodeESC_ControlLoopOutputsPacketStructure(void* _pg_pkt, const ESC_ControlLoopOutputs_t* _pg_user)
2096 : {
2097 0 : uint8_t* _pg_data = getESCVelocityPacketData(_pg_pkt);
2098 0 : int _pg_byteindex = 0;
2099 :
2100 : // Range of pTerm is -500.0f to 500.0f.
2101 0 : float32ScaledTo2SignedBeBytes(_pg_user->pTerm, _pg_data, &_pg_byteindex, 65.534f);
2102 :
2103 : // Range of iTerm is -500.0f to 500.0f.
2104 0 : float32ScaledTo2SignedBeBytes(_pg_user->iTerm, _pg_data, &_pg_byteindex, 65.534f);
2105 :
2106 : // Range of fTerm is -500.0f to 500.0f.
2107 0 : float32ScaledTo2SignedBeBytes(_pg_user->fTerm, _pg_data, &_pg_byteindex, 65.534f);
2108 :
2109 : // Output duty cycle
2110 : // Range of output is -32768 to 32767.
2111 0 : int16ToBeBytes(_pg_user->output, _pg_data, &_pg_byteindex);
2112 :
2113 : // complete the process of creating the packet
2114 0 : finishESCVelocityPacket(_pg_pkt, _pg_byteindex, getESC_ControlLoopOutputsPacketID());
2115 :
2116 0 : }// encodeESC_ControlLoopOutputsPacketStructure
2117 :
2118 : /*!
2119 : * \brief Decode the ESC_ControlLoopOutputs packet
2120 : *
2121 : * This packet contains the current output values for the RPM controller.
2122 : * \param _pg_pkt points to the packet being decoded by this function
2123 : * \param _pg_user receives the data decoded from the packet
2124 : * \return 0 is returned if the packet ID or size is wrong, else 1
2125 : */
2126 0 : int decodeESC_ControlLoopOutputsPacketStructure(const void* _pg_pkt, ESC_ControlLoopOutputs_t* _pg_user)
2127 : {
2128 : int _pg_numbytes;
2129 0 : int _pg_byteindex = 0;
2130 : const uint8_t* _pg_data;
2131 :
2132 : // Verify the packet identifier
2133 0 : if(getESCVelocityPacketID(_pg_pkt) != getESC_ControlLoopOutputsPacketID())
2134 0 : return 0;
2135 :
2136 : // Verify the packet size
2137 0 : _pg_numbytes = getESCVelocityPacketSize(_pg_pkt);
2138 0 : if(_pg_numbytes < getESC_ControlLoopOutputsMinDataLength())
2139 0 : return 0;
2140 :
2141 : // The raw data from the packet
2142 0 : _pg_data = getESCVelocityPacketDataConst(_pg_pkt);
2143 :
2144 : // Range of pTerm is -500.0f to 500.0f.
2145 0 : _pg_user->pTerm = float32ScaledFrom2SignedBeBytes(_pg_data, &_pg_byteindex, 1.0f/65.534f);
2146 :
2147 : // Range of iTerm is -500.0f to 500.0f.
2148 0 : _pg_user->iTerm = float32ScaledFrom2SignedBeBytes(_pg_data, &_pg_byteindex, 1.0f/65.534f);
2149 :
2150 : // Range of fTerm is -500.0f to 500.0f.
2151 0 : _pg_user->fTerm = float32ScaledFrom2SignedBeBytes(_pg_data, &_pg_byteindex, 1.0f/65.534f);
2152 :
2153 : // Output duty cycle
2154 : // Range of output is -32768 to 32767.
2155 0 : _pg_user->output = int16FromBeBytes(_pg_data, &_pg_byteindex);
2156 :
2157 0 : return 1;
2158 :
2159 : }// decodeESC_ControlLoopOutputsPacketStructure
2160 :
2161 : /*!
2162 : * \brief Create the ESC_ControlLoopOutputs packet
2163 : *
2164 : * This packet contains the current output values for the RPM controller.
2165 : * \param _pg_pkt points to the packet which will be created by this function
2166 : * \param pTerm is
2167 : * \param iTerm is
2168 : * \param fTerm is
2169 : * \param output is Output duty cycle
2170 : */
2171 0 : void encodeESC_ControlLoopOutputsPacket(void* _pg_pkt, float pTerm, float iTerm, float fTerm, int16_t output)
2172 : {
2173 0 : uint8_t* _pg_data = getESCVelocityPacketData(_pg_pkt);
2174 0 : int _pg_byteindex = 0;
2175 :
2176 : // Range of pTerm is -500.0f to 500.0f.
2177 0 : float32ScaledTo2SignedBeBytes(pTerm, _pg_data, &_pg_byteindex, 65.534f);
2178 :
2179 : // Range of iTerm is -500.0f to 500.0f.
2180 0 : float32ScaledTo2SignedBeBytes(iTerm, _pg_data, &_pg_byteindex, 65.534f);
2181 :
2182 : // Range of fTerm is -500.0f to 500.0f.
2183 0 : float32ScaledTo2SignedBeBytes(fTerm, _pg_data, &_pg_byteindex, 65.534f);
2184 :
2185 : // Output duty cycle
2186 : // Range of output is -32768 to 32767.
2187 0 : int16ToBeBytes(output, _pg_data, &_pg_byteindex);
2188 :
2189 : // complete the process of creating the packet
2190 0 : finishESCVelocityPacket(_pg_pkt, _pg_byteindex, getESC_ControlLoopOutputsPacketID());
2191 :
2192 0 : }// encodeESC_ControlLoopOutputsPacket
2193 :
2194 : /*!
2195 : * \brief Decode the ESC_ControlLoopOutputs packet
2196 : *
2197 : * This packet contains the current output values for the RPM controller.
2198 : * \param _pg_pkt points to the packet being decoded by this function
2199 : * \param pTerm receives
2200 : * \param iTerm receives
2201 : * \param fTerm receives
2202 : * \param output receives Output duty cycle
2203 : * \return 0 is returned if the packet ID or size is wrong, else 1
2204 : */
2205 0 : int decodeESC_ControlLoopOutputsPacket(const void* _pg_pkt, float* pTerm, float* iTerm, float* fTerm, int16_t* output)
2206 : {
2207 0 : int _pg_byteindex = 0;
2208 0 : const uint8_t* _pg_data = getESCVelocityPacketDataConst(_pg_pkt);
2209 0 : int _pg_numbytes = getESCVelocityPacketSize(_pg_pkt);
2210 :
2211 : // Verify the packet identifier
2212 0 : if(getESCVelocityPacketID(_pg_pkt) != getESC_ControlLoopOutputsPacketID())
2213 0 : return 0;
2214 :
2215 0 : if(_pg_numbytes < getESC_ControlLoopOutputsMinDataLength())
2216 0 : return 0;
2217 :
2218 : // Range of pTerm is -500.0f to 500.0f.
2219 0 : (*pTerm) = float32ScaledFrom2SignedBeBytes(_pg_data, &_pg_byteindex, 1.0f/65.534f);
2220 :
2221 : // Range of iTerm is -500.0f to 500.0f.
2222 0 : (*iTerm) = float32ScaledFrom2SignedBeBytes(_pg_data, &_pg_byteindex, 1.0f/65.534f);
2223 :
2224 : // Range of fTerm is -500.0f to 500.0f.
2225 0 : (*fTerm) = float32ScaledFrom2SignedBeBytes(_pg_data, &_pg_byteindex, 1.0f/65.534f);
2226 :
2227 : // Output duty cycle
2228 : // Range of output is -32768 to 32767.
2229 0 : (*output) = int16FromBeBytes(_pg_data, &_pg_byteindex);
2230 :
2231 0 : return 1;
2232 :
2233 : }// decodeESC_ControlLoopOutputsPacket
2234 :
2235 : /*!
2236 : * \brief Create the ESC_HallSensorInfo packet
2237 : *
2238 : * This packet contains information on the hall sensor
2239 : * \param _pg_pkt points to the packet which will be created by this function
2240 : * \param detectedPattern is
2241 : */
2242 0 : void encodeESC_HallSensorInfoPacket(void* _pg_pkt, const ESC_HallSensorPattern_t* detectedPattern)
2243 : {
2244 0 : uint8_t* _pg_data = getESCVelocityPacketData(_pg_pkt);
2245 0 : int _pg_byteindex = 0;
2246 :
2247 0 : encodeESC_HallSensorPattern_t(_pg_data, &_pg_byteindex, detectedPattern);
2248 :
2249 : // complete the process of creating the packet
2250 0 : finishESCVelocityPacket(_pg_pkt, _pg_byteindex, getESC_HallSensorInfoPacketID());
2251 :
2252 0 : }// encodeESC_HallSensorInfoPacket
2253 :
2254 : /*!
2255 : * \brief Decode the ESC_HallSensorInfo packet
2256 : *
2257 : * This packet contains information on the hall sensor
2258 : * \param _pg_pkt points to the packet being decoded by this function
2259 : * \param detectedPattern receives
2260 : * \return 0 is returned if the packet ID or size is wrong, else 1
2261 : */
2262 0 : int decodeESC_HallSensorInfoPacket(const void* _pg_pkt, ESC_HallSensorPattern_t* detectedPattern)
2263 : {
2264 0 : int _pg_byteindex = 0;
2265 0 : const uint8_t* _pg_data = getESCVelocityPacketDataConst(_pg_pkt);
2266 0 : int _pg_numbytes = getESCVelocityPacketSize(_pg_pkt);
2267 :
2268 : // Verify the packet identifier
2269 0 : if(getESCVelocityPacketID(_pg_pkt) != getESC_HallSensorInfoPacketID())
2270 0 : return 0;
2271 :
2272 0 : if(_pg_numbytes < getESC_HallSensorInfoMinDataLength())
2273 0 : return 0;
2274 :
2275 0 : if(decodeESC_HallSensorPattern_t(_pg_data, &_pg_byteindex, detectedPattern) == 0)
2276 0 : return 0;
2277 :
2278 0 : return 1;
2279 :
2280 : }// decodeESC_HallSensorInfoPacket
2281 :
2282 : /*!
2283 : * \brief Create the ESC_MotorStatusFlags packet
2284 : *
2285 : * Motor status flags. If any warning or error flags are set, this packet is
2286 : * transmitted by the ESC at 10Hz. If there are no warning or error flags set,
2287 : * the packet is transmitted by the ESC at 1Hz.
2288 : * \param _pg_pkt points to the packet which will be created by this function
2289 : * \param _pg_user points to the user data that will be encoded in _pg_pkt
2290 : */
2291 0 : void encodeESC_MotorStatusFlagsPacketStructure(void* _pg_pkt, const ESC_MotorStatusFlags_t* _pg_user)
2292 : {
2293 0 : uint8_t* _pg_data = getESCVelocityPacketData(_pg_pkt);
2294 0 : int _pg_byteindex = 0;
2295 :
2296 : // Cause of most recent standby event
2297 : // Range of standbyCause is 0 to 65535.
2298 0 : uint16ToBeBytes(_pg_user->standbyCause, _pg_data, &_pg_byteindex);
2299 :
2300 : // Cause of most recent disable event
2301 : // Range of disableCause is 0 to 65535.
2302 0 : uint16ToBeBytes(_pg_user->disableCause, _pg_data, &_pg_byteindex);
2303 :
2304 : // Cause of most recent motor-stop event
2305 : // Range of offCause is 0 to 65535.
2306 0 : uint16ToBeBytes(_pg_user->offCause, _pg_data, &_pg_byteindex);
2307 :
2308 : // Cause of most recent failed-start
2309 : // Range of failedStartCause is 0 to 65535.
2310 0 : uint16ToBeBytes(_pg_user->failedStartCause, _pg_data, &_pg_byteindex);
2311 :
2312 : // complete the process of creating the packet
2313 0 : finishESCVelocityPacket(_pg_pkt, _pg_byteindex, getESC_MotorStatusFlagsPacketID());
2314 :
2315 0 : }// encodeESC_MotorStatusFlagsPacketStructure
2316 :
2317 : /*!
2318 : * \brief Decode the ESC_MotorStatusFlags packet
2319 : *
2320 : * Motor status flags. If any warning or error flags are set, this packet is
2321 : * transmitted by the ESC at 10Hz. If there are no warning or error flags set,
2322 : * the packet is transmitted by the ESC at 1Hz.
2323 : * \param _pg_pkt points to the packet being decoded by this function
2324 : * \param _pg_user receives the data decoded from the packet
2325 : * \return 0 is returned if the packet ID or size is wrong, else 1
2326 : */
2327 0 : int decodeESC_MotorStatusFlagsPacketStructure(const void* _pg_pkt, ESC_MotorStatusFlags_t* _pg_user)
2328 : {
2329 : int _pg_numbytes;
2330 0 : int _pg_byteindex = 0;
2331 : const uint8_t* _pg_data;
2332 :
2333 : // Verify the packet identifier
2334 0 : if(getESCVelocityPacketID(_pg_pkt) != getESC_MotorStatusFlagsPacketID())
2335 0 : return 0;
2336 :
2337 : // Verify the packet size
2338 0 : _pg_numbytes = getESCVelocityPacketSize(_pg_pkt);
2339 0 : if(_pg_numbytes < getESC_MotorStatusFlagsMinDataLength())
2340 0 : return 0;
2341 :
2342 : // The raw data from the packet
2343 0 : _pg_data = getESCVelocityPacketDataConst(_pg_pkt);
2344 :
2345 : // Cause of most recent standby event
2346 : // Range of standbyCause is 0 to 65535.
2347 0 : _pg_user->standbyCause = uint16FromBeBytes(_pg_data, &_pg_byteindex);
2348 :
2349 : // Cause of most recent disable event
2350 : // Range of disableCause is 0 to 65535.
2351 0 : _pg_user->disableCause = uint16FromBeBytes(_pg_data, &_pg_byteindex);
2352 :
2353 : // Cause of most recent motor-stop event
2354 : // Range of offCause is 0 to 65535.
2355 0 : _pg_user->offCause = uint16FromBeBytes(_pg_data, &_pg_byteindex);
2356 :
2357 : // Cause of most recent failed-start
2358 : // Range of failedStartCause is 0 to 65535.
2359 0 : _pg_user->failedStartCause = uint16FromBeBytes(_pg_data, &_pg_byteindex);
2360 :
2361 0 : return 1;
2362 :
2363 : }// decodeESC_MotorStatusFlagsPacketStructure
2364 :
2365 : /*!
2366 : * \brief Create the ESC_MotorStatusFlags packet
2367 : *
2368 : * Motor status flags. If any warning or error flags are set, this packet is
2369 : * transmitted by the ESC at 10Hz. If there are no warning or error flags set,
2370 : * the packet is transmitted by the ESC at 1Hz.
2371 : * \param _pg_pkt points to the packet which will be created by this function
2372 : * \param standbyCause is Cause of most recent standby event
2373 : * \param disableCause is Cause of most recent disable event
2374 : * \param offCause is Cause of most recent motor-stop event
2375 : * \param failedStartCause is Cause of most recent failed-start
2376 : */
2377 0 : void encodeESC_MotorStatusFlagsPacket(void* _pg_pkt, uint16_t standbyCause, uint16_t disableCause, uint16_t offCause, uint16_t failedStartCause)
2378 : {
2379 0 : uint8_t* _pg_data = getESCVelocityPacketData(_pg_pkt);
2380 0 : int _pg_byteindex = 0;
2381 :
2382 : // Cause of most recent standby event
2383 : // Range of standbyCause is 0 to 65535.
2384 0 : uint16ToBeBytes(standbyCause, _pg_data, &_pg_byteindex);
2385 :
2386 : // Cause of most recent disable event
2387 : // Range of disableCause is 0 to 65535.
2388 0 : uint16ToBeBytes(disableCause, _pg_data, &_pg_byteindex);
2389 :
2390 : // Cause of most recent motor-stop event
2391 : // Range of offCause is 0 to 65535.
2392 0 : uint16ToBeBytes(offCause, _pg_data, &_pg_byteindex);
2393 :
2394 : // Cause of most recent failed-start
2395 : // Range of failedStartCause is 0 to 65535.
2396 0 : uint16ToBeBytes(failedStartCause, _pg_data, &_pg_byteindex);
2397 :
2398 : // complete the process of creating the packet
2399 0 : finishESCVelocityPacket(_pg_pkt, _pg_byteindex, getESC_MotorStatusFlagsPacketID());
2400 :
2401 0 : }// encodeESC_MotorStatusFlagsPacket
2402 :
2403 : /*!
2404 : * \brief Decode the ESC_MotorStatusFlags packet
2405 : *
2406 : * Motor status flags. If any warning or error flags are set, this packet is
2407 : * transmitted by the ESC at 10Hz. If there are no warning or error flags set,
2408 : * the packet is transmitted by the ESC at 1Hz.
2409 : * \param _pg_pkt points to the packet being decoded by this function
2410 : * \param standbyCause receives Cause of most recent standby event
2411 : * \param disableCause receives Cause of most recent disable event
2412 : * \param offCause receives Cause of most recent motor-stop event
2413 : * \param failedStartCause receives Cause of most recent failed-start
2414 : * \return 0 is returned if the packet ID or size is wrong, else 1
2415 : */
2416 0 : int decodeESC_MotorStatusFlagsPacket(const void* _pg_pkt, uint16_t* standbyCause, uint16_t* disableCause, uint16_t* offCause, uint16_t* failedStartCause)
2417 : {
2418 0 : int _pg_byteindex = 0;
2419 0 : const uint8_t* _pg_data = getESCVelocityPacketDataConst(_pg_pkt);
2420 0 : int _pg_numbytes = getESCVelocityPacketSize(_pg_pkt);
2421 :
2422 : // Verify the packet identifier
2423 0 : if(getESCVelocityPacketID(_pg_pkt) != getESC_MotorStatusFlagsPacketID())
2424 0 : return 0;
2425 :
2426 0 : if(_pg_numbytes < getESC_MotorStatusFlagsMinDataLength())
2427 0 : return 0;
2428 :
2429 : // Cause of most recent standby event
2430 : // Range of standbyCause is 0 to 65535.
2431 0 : (*standbyCause) = uint16FromBeBytes(_pg_data, &_pg_byteindex);
2432 :
2433 : // Cause of most recent disable event
2434 : // Range of disableCause is 0 to 65535.
2435 0 : (*disableCause) = uint16FromBeBytes(_pg_data, &_pg_byteindex);
2436 :
2437 : // Cause of most recent motor-stop event
2438 : // Range of offCause is 0 to 65535.
2439 0 : (*offCause) = uint16FromBeBytes(_pg_data, &_pg_byteindex);
2440 :
2441 : // Cause of most recent failed-start
2442 : // Range of failedStartCause is 0 to 65535.
2443 0 : (*failedStartCause) = uint16FromBeBytes(_pg_data, &_pg_byteindex);
2444 :
2445 0 : return 1;
2446 :
2447 : }// decodeESC_MotorStatusFlagsPacket
2448 :
2449 : /*!
2450 : * \brief Create the ESC_VoltageControlLoopSettings packet
2451 : *
2452 : * Voltage control loop settings
2453 : * \param _pg_pkt points to the packet which will be created by this function
2454 : * \param _pg_user points to the user data that will be encoded in _pg_pkt
2455 : */
2456 0 : void encodeESC_VoltageControlLoopSettingsPacketStructure(void* _pg_pkt, const ESC_VoltageControlLoopSettings_t* _pg_user)
2457 : {
2458 0 : uint8_t* _pg_data = getESCVelocityPacketData(_pg_pkt);
2459 0 : int _pg_byteindex = 0;
2460 :
2461 : // Core control loop settings
2462 0 : encodeESC_ControlLoopCommon_t(_pg_data, &_pg_byteindex, &_pg_user->pid);
2463 :
2464 : // complete the process of creating the packet
2465 0 : finishESCVelocityPacket(_pg_pkt, _pg_byteindex, getESC_VoltageControlLoopSettingsPacketID());
2466 :
2467 0 : }// encodeESC_VoltageControlLoopSettingsPacketStructure
2468 :
2469 : /*!
2470 : * \brief Decode the ESC_VoltageControlLoopSettings packet
2471 : *
2472 : * Voltage control loop settings
2473 : * \param _pg_pkt points to the packet being decoded by this function
2474 : * \param _pg_user receives the data decoded from the packet
2475 : * \return 0 is returned if the packet ID or size is wrong, else 1
2476 : */
2477 0 : int decodeESC_VoltageControlLoopSettingsPacketStructure(const void* _pg_pkt, ESC_VoltageControlLoopSettings_t* _pg_user)
2478 : {
2479 : int _pg_numbytes;
2480 0 : int _pg_byteindex = 0;
2481 : const uint8_t* _pg_data;
2482 :
2483 : // Verify the packet identifier
2484 0 : if(getESCVelocityPacketID(_pg_pkt) != getESC_VoltageControlLoopSettingsPacketID())
2485 0 : return 0;
2486 :
2487 : // Verify the packet size
2488 0 : _pg_numbytes = getESCVelocityPacketSize(_pg_pkt);
2489 0 : if(_pg_numbytes < getESC_VoltageControlLoopSettingsMinDataLength())
2490 0 : return 0;
2491 :
2492 : // The raw data from the packet
2493 0 : _pg_data = getESCVelocityPacketDataConst(_pg_pkt);
2494 :
2495 : // Core control loop settings
2496 0 : if(decodeESC_ControlLoopCommon_t(_pg_data, &_pg_byteindex, &_pg_user->pid) == 0)
2497 0 : return 0;
2498 :
2499 0 : return 1;
2500 :
2501 : }// decodeESC_VoltageControlLoopSettingsPacketStructure
2502 :
2503 : /*!
2504 : * \brief Create the ESC_VoltageControlLoopSettings packet
2505 : *
2506 : * Voltage control loop settings
2507 : * \param _pg_pkt points to the packet which will be created by this function
2508 : * \param pid is Core control loop settings
2509 : */
2510 0 : void encodeESC_VoltageControlLoopSettingsPacket(void* _pg_pkt, const ESC_ControlLoopCommon_t* pid)
2511 : {
2512 0 : uint8_t* _pg_data = getESCVelocityPacketData(_pg_pkt);
2513 0 : int _pg_byteindex = 0;
2514 :
2515 : // Core control loop settings
2516 0 : encodeESC_ControlLoopCommon_t(_pg_data, &_pg_byteindex, pid);
2517 :
2518 : // complete the process of creating the packet
2519 0 : finishESCVelocityPacket(_pg_pkt, _pg_byteindex, getESC_VoltageControlLoopSettingsPacketID());
2520 :
2521 0 : }// encodeESC_VoltageControlLoopSettingsPacket
2522 :
2523 : /*!
2524 : * \brief Decode the ESC_VoltageControlLoopSettings packet
2525 : *
2526 : * Voltage control loop settings
2527 : * \param _pg_pkt points to the packet being decoded by this function
2528 : * \param pid receives Core control loop settings
2529 : * \return 0 is returned if the packet ID or size is wrong, else 1
2530 : */
2531 0 : int decodeESC_VoltageControlLoopSettingsPacket(const void* _pg_pkt, ESC_ControlLoopCommon_t* pid)
2532 : {
2533 0 : int _pg_byteindex = 0;
2534 0 : const uint8_t* _pg_data = getESCVelocityPacketDataConst(_pg_pkt);
2535 0 : int _pg_numbytes = getESCVelocityPacketSize(_pg_pkt);
2536 :
2537 : // Verify the packet identifier
2538 0 : if(getESCVelocityPacketID(_pg_pkt) != getESC_VoltageControlLoopSettingsPacketID())
2539 0 : return 0;
2540 :
2541 0 : if(_pg_numbytes < getESC_VoltageControlLoopSettingsMinDataLength())
2542 0 : return 0;
2543 :
2544 : // Core control loop settings
2545 0 : if(decodeESC_ControlLoopCommon_t(_pg_data, &_pg_byteindex, pid) == 0)
2546 0 : return 0;
2547 :
2548 0 : return 1;
2549 :
2550 : }// decodeESC_VoltageControlLoopSettingsPacket
2551 :
2552 : /*!
2553 : * \brief Create the ESC_ProtectionValues packet
2554 : *
2555 : * ESC self-protection settings
2556 : * \param _pg_pkt points to the packet which will be created by this function
2557 : * \param _pg_user points to the user data that will be encoded in _pg_pkt
2558 : */
2559 0 : void encodeESC_ProtectionValuesPacketStructure(void* _pg_pkt, const ESC_ProtectionValues_t* _pg_user)
2560 : {
2561 0 : uint8_t* _pg_data = getESCVelocityPacketData(_pg_pkt);
2562 0 : int _pg_byteindex = 0;
2563 :
2564 : // Maximum allowable DC bus current
2565 : // Range of overcurrentBus is 0.0f to 1275.0f.
2566 0 : float32ScaledTo1UnsignedBytes(_pg_user->overcurrentBus, _pg_data, &_pg_byteindex, 0.0f, 0.2f);
2567 :
2568 : // Maximum allowable motor phase current
2569 : // Range of overcurrentPhase is 0.0f to 1275.0f.
2570 0 : float32ScaledTo1UnsignedBytes(_pg_user->overcurrentPhase, _pg_data, &_pg_byteindex, 0.0f, 0.2f);
2571 :
2572 : // Maximum allowable regen current
2573 : // Range of overcurrentRegen is 0.0f to 1275.0f.
2574 0 : float32ScaledTo1UnsignedBytes(_pg_user->overcurrentRegen, _pg_data, &_pg_byteindex, 0.0f, 0.2f);
2575 :
2576 : // Maximum allowable internal temperature
2577 : // Range of overtemperatureInternal is 0 to 255.
2578 0 : uint8ToBytes(_pg_user->overtemperatureInternal, _pg_data, &_pg_byteindex);
2579 :
2580 : // Maximum allowable motor temperature
2581 : // Range of overtemperatureMotor is 0 to 255.
2582 0 : uint8ToBytes(_pg_user->overtemperatureMotor, _pg_data, &_pg_byteindex);
2583 :
2584 : // Maximum motor speed
2585 : // Range of overspeed is 0 to 65535.
2586 0 : uint16ToBeBytes(_pg_user->overspeed, _pg_data, &_pg_byteindex);
2587 :
2588 : // ESC undervoltage warning threshold
2589 : // Range of undervoltage is 0 to 255.
2590 0 : uint8ToBytes(_pg_user->undervoltage, _pg_data, &_pg_byteindex);
2591 :
2592 : // ESC overvoltage warning threshold
2593 : // Range of overvoltage is 0 to 255.
2594 0 : uint8ToBytes(_pg_user->overvoltage, _pg_data, &_pg_byteindex);
2595 :
2596 : // Maximum allowable demag angle
2597 : // Range of demagAngle is 0 to 255.
2598 0 : uint8ToBytes(_pg_user->demagAngle, _pg_data, &_pg_byteindex);
2599 :
2600 : // Maximum allowable timing advance
2601 : // Range of timingAdvance is 0 to 255.
2602 0 : uint8ToBytes(_pg_user->timingAdvance, _pg_data, &_pg_byteindex);
2603 :
2604 : // complete the process of creating the packet
2605 0 : finishESCVelocityPacket(_pg_pkt, _pg_byteindex, getESC_ProtectionValuesPacketID());
2606 :
2607 0 : }// encodeESC_ProtectionValuesPacketStructure
2608 :
2609 : /*!
2610 : * \brief Decode the ESC_ProtectionValues packet
2611 : *
2612 : * ESC self-protection settings
2613 : * \param _pg_pkt points to the packet being decoded by this function
2614 : * \param _pg_user receives the data decoded from the packet
2615 : * \return 0 is returned if the packet ID or size is wrong, else 1
2616 : */
2617 0 : int decodeESC_ProtectionValuesPacketStructure(const void* _pg_pkt, ESC_ProtectionValues_t* _pg_user)
2618 : {
2619 : int _pg_numbytes;
2620 0 : int _pg_byteindex = 0;
2621 : const uint8_t* _pg_data;
2622 :
2623 : // Verify the packet identifier
2624 0 : if(getESCVelocityPacketID(_pg_pkt) != getESC_ProtectionValuesPacketID())
2625 0 : return 0;
2626 :
2627 : // Verify the packet size
2628 0 : _pg_numbytes = getESCVelocityPacketSize(_pg_pkt);
2629 0 : if(_pg_numbytes < getESC_ProtectionValuesMinDataLength())
2630 0 : return 0;
2631 :
2632 : // The raw data from the packet
2633 0 : _pg_data = getESCVelocityPacketDataConst(_pg_pkt);
2634 :
2635 : // this packet has default fields, make sure they are set
2636 0 : _pg_user->timingAdvance = 20;
2637 :
2638 : // Maximum allowable DC bus current
2639 : // Range of overcurrentBus is 0.0f to 1275.0f.
2640 0 : _pg_user->overcurrentBus = float32ScaledFrom1UnsignedBytes(_pg_data, &_pg_byteindex, 0.0f, 1.0f/0.2f);
2641 :
2642 : // Maximum allowable motor phase current
2643 : // Range of overcurrentPhase is 0.0f to 1275.0f.
2644 0 : _pg_user->overcurrentPhase = float32ScaledFrom1UnsignedBytes(_pg_data, &_pg_byteindex, 0.0f, 1.0f/0.2f);
2645 :
2646 : // Maximum allowable regen current
2647 : // Range of overcurrentRegen is 0.0f to 1275.0f.
2648 0 : _pg_user->overcurrentRegen = float32ScaledFrom1UnsignedBytes(_pg_data, &_pg_byteindex, 0.0f, 1.0f/0.2f);
2649 :
2650 : // Maximum allowable internal temperature
2651 : // Range of overtemperatureInternal is 0 to 255.
2652 0 : _pg_user->overtemperatureInternal = uint8FromBytes(_pg_data, &_pg_byteindex);
2653 :
2654 : // Maximum allowable motor temperature
2655 : // Range of overtemperatureMotor is 0 to 255.
2656 0 : _pg_user->overtemperatureMotor = uint8FromBytes(_pg_data, &_pg_byteindex);
2657 :
2658 : // Maximum motor speed
2659 : // Range of overspeed is 0 to 65535.
2660 0 : _pg_user->overspeed = uint16FromBeBytes(_pg_data, &_pg_byteindex);
2661 :
2662 : // ESC undervoltage warning threshold
2663 : // Range of undervoltage is 0 to 255.
2664 0 : _pg_user->undervoltage = uint8FromBytes(_pg_data, &_pg_byteindex);
2665 :
2666 : // ESC overvoltage warning threshold
2667 : // Range of overvoltage is 0 to 255.
2668 0 : _pg_user->overvoltage = uint8FromBytes(_pg_data, &_pg_byteindex);
2669 :
2670 : // Maximum allowable demag angle
2671 : // Range of demagAngle is 0 to 255.
2672 0 : _pg_user->demagAngle = uint8FromBytes(_pg_data, &_pg_byteindex);
2673 :
2674 0 : if(_pg_byteindex + 1 > _pg_numbytes)
2675 0 : return 1;
2676 :
2677 : // Maximum allowable timing advance
2678 : // Range of timingAdvance is 0 to 255.
2679 0 : _pg_user->timingAdvance = uint8FromBytes(_pg_data, &_pg_byteindex);
2680 :
2681 0 : return 1;
2682 :
2683 : }// decodeESC_ProtectionValuesPacketStructure
2684 :
2685 : /*!
2686 : * \brief Create the ESC_ProtectionValues packet
2687 : *
2688 : * ESC self-protection settings
2689 : * \param _pg_pkt points to the packet which will be created by this function
2690 : * \param overcurrentBus is Maximum allowable DC bus current
2691 : * \param overcurrentPhase is Maximum allowable motor phase current
2692 : * \param overcurrentRegen is Maximum allowable regen current
2693 : * \param overtemperatureInternal is Maximum allowable internal temperature
2694 : * \param overtemperatureMotor is Maximum allowable motor temperature
2695 : * \param overspeed is Maximum motor speed
2696 : * \param undervoltage is ESC undervoltage warning threshold
2697 : * \param overvoltage is ESC overvoltage warning threshold
2698 : * \param demagAngle is Maximum allowable demag angle
2699 : * \param timingAdvance is Maximum allowable timing advance
2700 : */
2701 0 : void encodeESC_ProtectionValuesPacket(void* _pg_pkt, float overcurrentBus, float overcurrentPhase, float overcurrentRegen, uint8_t overtemperatureInternal, uint8_t overtemperatureMotor, uint16_t overspeed, uint8_t undervoltage, uint8_t overvoltage, uint8_t demagAngle, uint8_t timingAdvance)
2702 : {
2703 0 : uint8_t* _pg_data = getESCVelocityPacketData(_pg_pkt);
2704 0 : int _pg_byteindex = 0;
2705 :
2706 : // Maximum allowable DC bus current
2707 : // Range of overcurrentBus is 0.0f to 1275.0f.
2708 0 : float32ScaledTo1UnsignedBytes(overcurrentBus, _pg_data, &_pg_byteindex, 0.0f, 0.2f);
2709 :
2710 : // Maximum allowable motor phase current
2711 : // Range of overcurrentPhase is 0.0f to 1275.0f.
2712 0 : float32ScaledTo1UnsignedBytes(overcurrentPhase, _pg_data, &_pg_byteindex, 0.0f, 0.2f);
2713 :
2714 : // Maximum allowable regen current
2715 : // Range of overcurrentRegen is 0.0f to 1275.0f.
2716 0 : float32ScaledTo1UnsignedBytes(overcurrentRegen, _pg_data, &_pg_byteindex, 0.0f, 0.2f);
2717 :
2718 : // Maximum allowable internal temperature
2719 : // Range of overtemperatureInternal is 0 to 255.
2720 0 : uint8ToBytes(overtemperatureInternal, _pg_data, &_pg_byteindex);
2721 :
2722 : // Maximum allowable motor temperature
2723 : // Range of overtemperatureMotor is 0 to 255.
2724 0 : uint8ToBytes(overtemperatureMotor, _pg_data, &_pg_byteindex);
2725 :
2726 : // Maximum motor speed
2727 : // Range of overspeed is 0 to 65535.
2728 0 : uint16ToBeBytes(overspeed, _pg_data, &_pg_byteindex);
2729 :
2730 : // ESC undervoltage warning threshold
2731 : // Range of undervoltage is 0 to 255.
2732 0 : uint8ToBytes(undervoltage, _pg_data, &_pg_byteindex);
2733 :
2734 : // ESC overvoltage warning threshold
2735 : // Range of overvoltage is 0 to 255.
2736 0 : uint8ToBytes(overvoltage, _pg_data, &_pg_byteindex);
2737 :
2738 : // Maximum allowable demag angle
2739 : // Range of demagAngle is 0 to 255.
2740 0 : uint8ToBytes(demagAngle, _pg_data, &_pg_byteindex);
2741 :
2742 : // Maximum allowable timing advance
2743 : // Range of timingAdvance is 0 to 255.
2744 0 : uint8ToBytes(timingAdvance, _pg_data, &_pg_byteindex);
2745 :
2746 : // complete the process of creating the packet
2747 0 : finishESCVelocityPacket(_pg_pkt, _pg_byteindex, getESC_ProtectionValuesPacketID());
2748 :
2749 0 : }// encodeESC_ProtectionValuesPacket
2750 :
2751 : /*!
2752 : * \brief Decode the ESC_ProtectionValues packet
2753 : *
2754 : * ESC self-protection settings
2755 : * \param _pg_pkt points to the packet being decoded by this function
2756 : * \param overcurrentBus receives Maximum allowable DC bus current
2757 : * \param overcurrentPhase receives Maximum allowable motor phase current
2758 : * \param overcurrentRegen receives Maximum allowable regen current
2759 : * \param overtemperatureInternal receives Maximum allowable internal temperature
2760 : * \param overtemperatureMotor receives Maximum allowable motor temperature
2761 : * \param overspeed receives Maximum motor speed
2762 : * \param undervoltage receives ESC undervoltage warning threshold
2763 : * \param overvoltage receives ESC overvoltage warning threshold
2764 : * \param demagAngle receives Maximum allowable demag angle
2765 : * \param timingAdvance receives Maximum allowable timing advance
2766 : * \return 0 is returned if the packet ID or size is wrong, else 1
2767 : */
2768 0 : int decodeESC_ProtectionValuesPacket(const void* _pg_pkt, float* overcurrentBus, float* overcurrentPhase, float* overcurrentRegen, uint8_t* overtemperatureInternal, uint8_t* overtemperatureMotor, uint16_t* overspeed, uint8_t* undervoltage, uint8_t* overvoltage, uint8_t* demagAngle, uint8_t* timingAdvance)
2769 : {
2770 0 : int _pg_byteindex = 0;
2771 0 : const uint8_t* _pg_data = getESCVelocityPacketDataConst(_pg_pkt);
2772 0 : int _pg_numbytes = getESCVelocityPacketSize(_pg_pkt);
2773 :
2774 : // Verify the packet identifier
2775 0 : if(getESCVelocityPacketID(_pg_pkt) != getESC_ProtectionValuesPacketID())
2776 0 : return 0;
2777 :
2778 0 : if(_pg_numbytes < getESC_ProtectionValuesMinDataLength())
2779 0 : return 0;
2780 :
2781 : // this packet has default fields, make sure they are set
2782 0 : (*timingAdvance) = 20;
2783 :
2784 : // Maximum allowable DC bus current
2785 : // Range of overcurrentBus is 0.0f to 1275.0f.
2786 0 : (*overcurrentBus) = float32ScaledFrom1UnsignedBytes(_pg_data, &_pg_byteindex, 0.0f, 1.0f/0.2f);
2787 :
2788 : // Maximum allowable motor phase current
2789 : // Range of overcurrentPhase is 0.0f to 1275.0f.
2790 0 : (*overcurrentPhase) = float32ScaledFrom1UnsignedBytes(_pg_data, &_pg_byteindex, 0.0f, 1.0f/0.2f);
2791 :
2792 : // Maximum allowable regen current
2793 : // Range of overcurrentRegen is 0.0f to 1275.0f.
2794 0 : (*overcurrentRegen) = float32ScaledFrom1UnsignedBytes(_pg_data, &_pg_byteindex, 0.0f, 1.0f/0.2f);
2795 :
2796 : // Maximum allowable internal temperature
2797 : // Range of overtemperatureInternal is 0 to 255.
2798 0 : (*overtemperatureInternal) = uint8FromBytes(_pg_data, &_pg_byteindex);
2799 :
2800 : // Maximum allowable motor temperature
2801 : // Range of overtemperatureMotor is 0 to 255.
2802 0 : (*overtemperatureMotor) = uint8FromBytes(_pg_data, &_pg_byteindex);
2803 :
2804 : // Maximum motor speed
2805 : // Range of overspeed is 0 to 65535.
2806 0 : (*overspeed) = uint16FromBeBytes(_pg_data, &_pg_byteindex);
2807 :
2808 : // ESC undervoltage warning threshold
2809 : // Range of undervoltage is 0 to 255.
2810 0 : (*undervoltage) = uint8FromBytes(_pg_data, &_pg_byteindex);
2811 :
2812 : // ESC overvoltage warning threshold
2813 : // Range of overvoltage is 0 to 255.
2814 0 : (*overvoltage) = uint8FromBytes(_pg_data, &_pg_byteindex);
2815 :
2816 : // Maximum allowable demag angle
2817 : // Range of demagAngle is 0 to 255.
2818 0 : (*demagAngle) = uint8FromBytes(_pg_data, &_pg_byteindex);
2819 :
2820 0 : if(_pg_byteindex + 1 > _pg_numbytes)
2821 0 : return 1;
2822 :
2823 : // Maximum allowable timing advance
2824 : // Range of timingAdvance is 0 to 255.
2825 0 : (*timingAdvance) = uint8FromBytes(_pg_data, &_pg_byteindex);
2826 :
2827 0 : return 1;
2828 :
2829 : }// decodeESC_ProtectionValuesPacket
2830 :
2831 : /*!
2832 : * \brief Create the ESC_ProtectionActions packet
2833 : *
2834 : * ESC protection actions
2835 : * \param _pg_pkt points to the packet which will be created by this function
2836 : * \param _pg_user points to the user data that will be encoded in _pg_pkt
2837 : */
2838 0 : void encodeESC_ProtectionActionsPacketStructure(void* _pg_pkt, const ESC_ProtectionActions_t* _pg_user)
2839 : {
2840 0 : uint8_t* _pg_data = getESCVelocityPacketData(_pg_pkt);
2841 0 : int _pg_byteindex = 0;
2842 0 : unsigned int _pg_tempbitfield = 0;
2843 :
2844 : // Action when bus current exceeds limit
2845 : // Range of overcurrentBus is 0 to 7.
2846 0 : _pg_data[_pg_byteindex] = (uint8_t)limitMax(_pg_user->overcurrentBus, 7) << 5;
2847 :
2848 : // Action when phase current exceeds limit
2849 : // Range of overcurrentPhase is 0 to 7.
2850 0 : _pg_data[_pg_byteindex] |= (uint8_t)limitMax(_pg_user->overcurrentPhase, 7) << 2;
2851 :
2852 : // Action when regen current exceeds limit
2853 : // Range of overcurrentRegen is 0 to 7.
2854 0 : _pg_tempbitfield = (unsigned int)limitMax(_pg_user->overcurrentRegen, 7);
2855 0 : _pg_data[_pg_byteindex + 1] = (uint8_t)(_pg_tempbitfield << 7);
2856 :
2857 0 : _pg_tempbitfield >>= 1;
2858 0 : _pg_data[_pg_byteindex] |= (uint8_t)_pg_tempbitfield;
2859 :
2860 : // Action when ESC temperature exceeds limit
2861 : // Range of overtemperatureInternal is 0 to 7.
2862 0 : _pg_data[_pg_byteindex + 1] |= (uint8_t)limitMax(_pg_user->overtemperatureInternal, 7) << 4;
2863 :
2864 : // Action when motor temperature exceeds limit
2865 : // Range of overtemperatureMotor is 0 to 7.
2866 0 : _pg_data[_pg_byteindex + 1] |= (uint8_t)limitMax(_pg_user->overtemperatureMotor, 7) << 1;
2867 :
2868 : // Action when motor speed exceeds limit
2869 : // Range of overspeed is 0 to 7.
2870 0 : _pg_tempbitfield = (unsigned int)limitMax(_pg_user->overspeed, 7);
2871 0 : _pg_data[_pg_byteindex + 2] = (uint8_t)(_pg_tempbitfield << 6);
2872 :
2873 0 : _pg_tempbitfield >>= 2;
2874 0 : _pg_data[_pg_byteindex + 1] |= (uint8_t)_pg_tempbitfield;
2875 :
2876 : // Action when DC voltage falls below configured lower limit
2877 : // Range of undervoltage is 0 to 7.
2878 0 : _pg_data[_pg_byteindex + 2] |= (uint8_t)limitMax(_pg_user->undervoltage, 7) << 3;
2879 :
2880 : // Action when DC voltage exceeds configured upper limit
2881 : // Range of overvoltage is 0 to 7.
2882 0 : _pg_data[_pg_byteindex + 2] |= (uint8_t)limitMax(_pg_user->overvoltage, 7);
2883 :
2884 : // Action when motor demag angle exceeds limit
2885 : // Range of demagAngle is 0 to 7.
2886 0 : _pg_data[_pg_byteindex + 3] = (uint8_t)limitMax(_pg_user->demagAngle, 7) << 5;
2887 :
2888 : // Action on motor commutation estimator failure
2889 : // Range of commutationLoss is 0 to 7.
2890 0 : _pg_data[_pg_byteindex + 3] |= (uint8_t)limitMax(_pg_user->commutationLoss, 7) << 2;
2891 0 : _pg_byteindex += 4; // close bit field
2892 :
2893 : // complete the process of creating the packet
2894 0 : finishESCVelocityPacket(_pg_pkt, _pg_byteindex, getESC_ProtectionActionsPacketID());
2895 :
2896 0 : }// encodeESC_ProtectionActionsPacketStructure
2897 :
2898 : /*!
2899 : * \brief Decode the ESC_ProtectionActions packet
2900 : *
2901 : * ESC protection actions
2902 : * \param _pg_pkt points to the packet being decoded by this function
2903 : * \param _pg_user receives the data decoded from the packet
2904 : * \return 0 is returned if the packet ID or size is wrong, else 1
2905 : */
2906 0 : int decodeESC_ProtectionActionsPacketStructure(const void* _pg_pkt, ESC_ProtectionActions_t* _pg_user)
2907 : {
2908 : int _pg_numbytes;
2909 0 : int _pg_byteindex = 0;
2910 : const uint8_t* _pg_data;
2911 0 : unsigned int _pg_tempbitfield = 0;
2912 :
2913 : // Verify the packet identifier
2914 0 : if(getESCVelocityPacketID(_pg_pkt) != getESC_ProtectionActionsPacketID())
2915 0 : return 0;
2916 :
2917 : // Verify the packet size
2918 0 : _pg_numbytes = getESCVelocityPacketSize(_pg_pkt);
2919 0 : if(_pg_numbytes < getESC_ProtectionActionsMinDataLength())
2920 0 : return 0;
2921 :
2922 : // The raw data from the packet
2923 0 : _pg_data = getESCVelocityPacketDataConst(_pg_pkt);
2924 :
2925 : // this packet has default fields, make sure they are set
2926 0 : _pg_user->commutationLoss = ESC_PROTECTION_WARNING;
2927 :
2928 : // Action when bus current exceeds limit
2929 : // Range of overcurrentBus is 0 to 7.
2930 0 : _pg_user->overcurrentBus = (_pg_data[_pg_byteindex] >> 5);
2931 :
2932 : // Action when phase current exceeds limit
2933 : // Range of overcurrentPhase is 0 to 7.
2934 0 : _pg_user->overcurrentPhase = ((_pg_data[_pg_byteindex] >> 2) & 0x7);
2935 :
2936 : // Action when regen current exceeds limit
2937 : // Range of overcurrentRegen is 0 to 7.
2938 0 : _pg_tempbitfield = (_pg_data[_pg_byteindex] & 0x3);
2939 :
2940 0 : _pg_tempbitfield <<= 1;
2941 0 : _pg_tempbitfield |= (_pg_data[_pg_byteindex + 1] >> 7);
2942 :
2943 0 : _pg_user->overcurrentRegen = _pg_tempbitfield;
2944 :
2945 : // Action when ESC temperature exceeds limit
2946 : // Range of overtemperatureInternal is 0 to 7.
2947 0 : _pg_user->overtemperatureInternal = ((_pg_data[_pg_byteindex + 1] >> 4) & 0x7);
2948 :
2949 : // Action when motor temperature exceeds limit
2950 : // Range of overtemperatureMotor is 0 to 7.
2951 0 : _pg_user->overtemperatureMotor = ((_pg_data[_pg_byteindex + 1] >> 1) & 0x7);
2952 :
2953 : // Action when motor speed exceeds limit
2954 : // Range of overspeed is 0 to 7.
2955 0 : _pg_tempbitfield = (_pg_data[_pg_byteindex + 1] & 0x1);
2956 :
2957 0 : _pg_tempbitfield <<= 2;
2958 0 : _pg_tempbitfield |= (_pg_data[_pg_byteindex + 2] >> 6);
2959 :
2960 0 : _pg_user->overspeed = _pg_tempbitfield;
2961 :
2962 : // Action when DC voltage falls below configured lower limit
2963 : // Range of undervoltage is 0 to 7.
2964 0 : _pg_user->undervoltage = ((_pg_data[_pg_byteindex + 2] >> 3) & 0x7);
2965 :
2966 : // Action when DC voltage exceeds configured upper limit
2967 : // Range of overvoltage is 0 to 7.
2968 0 : _pg_user->overvoltage = ((_pg_data[_pg_byteindex + 2]) & 0x7);
2969 :
2970 : // Action when motor demag angle exceeds limit
2971 : // Range of demagAngle is 0 to 7.
2972 0 : _pg_user->demagAngle = (_pg_data[_pg_byteindex + 3] >> 5);
2973 :
2974 0 : if(_pg_byteindex + 4 > _pg_numbytes)
2975 0 : return 1;
2976 :
2977 : // Action on motor commutation estimator failure
2978 : // Range of commutationLoss is 0 to 7.
2979 0 : _pg_user->commutationLoss = ((_pg_data[_pg_byteindex + 3] >> 2) & 0x7);
2980 0 : _pg_byteindex += 4; // close bit field
2981 :
2982 0 : return 1;
2983 :
2984 : }// decodeESC_ProtectionActionsPacketStructure
2985 :
2986 : /*!
2987 : * \brief Create the ESC_ProtectionActions packet
2988 : *
2989 : * ESC protection actions
2990 : * \param _pg_pkt points to the packet which will be created by this function
2991 : * \param overcurrentBus is Action when bus current exceeds limit
2992 : * \param overcurrentPhase is Action when phase current exceeds limit
2993 : * \param overcurrentRegen is Action when regen current exceeds limit
2994 : * \param overtemperatureInternal is Action when ESC temperature exceeds limit
2995 : * \param overtemperatureMotor is Action when motor temperature exceeds limit
2996 : * \param overspeed is Action when motor speed exceeds limit
2997 : * \param undervoltage is Action when DC voltage falls below configured lower limit
2998 : * \param overvoltage is Action when DC voltage exceeds configured upper limit
2999 : * \param demagAngle is Action when motor demag angle exceeds limit
3000 : * \param commutationLoss is Action on motor commutation estimator failure
3001 : */
3002 0 : void encodeESC_ProtectionActionsPacket(void* _pg_pkt, uint8_t overcurrentBus, uint8_t overcurrentPhase, uint8_t overcurrentRegen, uint8_t overtemperatureInternal, uint8_t overtemperatureMotor, uint8_t overspeed, uint8_t undervoltage, uint8_t overvoltage, uint8_t demagAngle, uint8_t commutationLoss)
3003 : {
3004 0 : uint8_t* _pg_data = getESCVelocityPacketData(_pg_pkt);
3005 0 : int _pg_byteindex = 0;
3006 0 : unsigned int _pg_tempbitfield = 0;
3007 :
3008 : // Action when bus current exceeds limit
3009 : // Range of overcurrentBus is 0 to 7.
3010 0 : _pg_data[_pg_byteindex] = (uint8_t)limitMax(overcurrentBus, 7) << 5;
3011 :
3012 : // Action when phase current exceeds limit
3013 : // Range of overcurrentPhase is 0 to 7.
3014 0 : _pg_data[_pg_byteindex] |= (uint8_t)limitMax(overcurrentPhase, 7) << 2;
3015 :
3016 : // Action when regen current exceeds limit
3017 : // Range of overcurrentRegen is 0 to 7.
3018 0 : _pg_tempbitfield = (unsigned int)limitMax(overcurrentRegen, 7);
3019 0 : _pg_data[_pg_byteindex + 1] = (uint8_t)(_pg_tempbitfield << 7);
3020 :
3021 0 : _pg_tempbitfield >>= 1;
3022 0 : _pg_data[_pg_byteindex] |= (uint8_t)_pg_tempbitfield;
3023 :
3024 : // Action when ESC temperature exceeds limit
3025 : // Range of overtemperatureInternal is 0 to 7.
3026 0 : _pg_data[_pg_byteindex + 1] |= (uint8_t)limitMax(overtemperatureInternal, 7) << 4;
3027 :
3028 : // Action when motor temperature exceeds limit
3029 : // Range of overtemperatureMotor is 0 to 7.
3030 0 : _pg_data[_pg_byteindex + 1] |= (uint8_t)limitMax(overtemperatureMotor, 7) << 1;
3031 :
3032 : // Action when motor speed exceeds limit
3033 : // Range of overspeed is 0 to 7.
3034 0 : _pg_tempbitfield = (unsigned int)limitMax(overspeed, 7);
3035 0 : _pg_data[_pg_byteindex + 2] = (uint8_t)(_pg_tempbitfield << 6);
3036 :
3037 0 : _pg_tempbitfield >>= 2;
3038 0 : _pg_data[_pg_byteindex + 1] |= (uint8_t)_pg_tempbitfield;
3039 :
3040 : // Action when DC voltage falls below configured lower limit
3041 : // Range of undervoltage is 0 to 7.
3042 0 : _pg_data[_pg_byteindex + 2] |= (uint8_t)limitMax(undervoltage, 7) << 3;
3043 :
3044 : // Action when DC voltage exceeds configured upper limit
3045 : // Range of overvoltage is 0 to 7.
3046 0 : _pg_data[_pg_byteindex + 2] |= (uint8_t)limitMax(overvoltage, 7);
3047 :
3048 : // Action when motor demag angle exceeds limit
3049 : // Range of demagAngle is 0 to 7.
3050 0 : _pg_data[_pg_byteindex + 3] = (uint8_t)limitMax(demagAngle, 7) << 5;
3051 :
3052 : // Action on motor commutation estimator failure
3053 : // Range of commutationLoss is 0 to 7.
3054 0 : _pg_data[_pg_byteindex + 3] |= (uint8_t)limitMax(commutationLoss, 7) << 2;
3055 0 : _pg_byteindex += 4; // close bit field
3056 :
3057 : // complete the process of creating the packet
3058 0 : finishESCVelocityPacket(_pg_pkt, _pg_byteindex, getESC_ProtectionActionsPacketID());
3059 :
3060 0 : }// encodeESC_ProtectionActionsPacket
3061 :
3062 : /*!
3063 : * \brief Decode the ESC_ProtectionActions packet
3064 : *
3065 : * ESC protection actions
3066 : * \param _pg_pkt points to the packet being decoded by this function
3067 : * \param overcurrentBus receives Action when bus current exceeds limit
3068 : * \param overcurrentPhase receives Action when phase current exceeds limit
3069 : * \param overcurrentRegen receives Action when regen current exceeds limit
3070 : * \param overtemperatureInternal receives Action when ESC temperature exceeds limit
3071 : * \param overtemperatureMotor receives Action when motor temperature exceeds limit
3072 : * \param overspeed receives Action when motor speed exceeds limit
3073 : * \param undervoltage receives Action when DC voltage falls below configured lower limit
3074 : * \param overvoltage receives Action when DC voltage exceeds configured upper limit
3075 : * \param demagAngle receives Action when motor demag angle exceeds limit
3076 : * \param commutationLoss receives Action on motor commutation estimator failure
3077 : * \return 0 is returned if the packet ID or size is wrong, else 1
3078 : */
3079 0 : int decodeESC_ProtectionActionsPacket(const void* _pg_pkt, uint8_t* overcurrentBus, uint8_t* overcurrentPhase, uint8_t* overcurrentRegen, uint8_t* overtemperatureInternal, uint8_t* overtemperatureMotor, uint8_t* overspeed, uint8_t* undervoltage, uint8_t* overvoltage, uint8_t* demagAngle, uint8_t* commutationLoss)
3080 : {
3081 0 : unsigned int _pg_tempbitfield = 0;
3082 0 : int _pg_byteindex = 0;
3083 0 : const uint8_t* _pg_data = getESCVelocityPacketDataConst(_pg_pkt);
3084 0 : int _pg_numbytes = getESCVelocityPacketSize(_pg_pkt);
3085 :
3086 : // Verify the packet identifier
3087 0 : if(getESCVelocityPacketID(_pg_pkt) != getESC_ProtectionActionsPacketID())
3088 0 : return 0;
3089 :
3090 0 : if(_pg_numbytes < getESC_ProtectionActionsMinDataLength())
3091 0 : return 0;
3092 :
3093 : // this packet has default fields, make sure they are set
3094 0 : (*commutationLoss) = ESC_PROTECTION_WARNING;
3095 :
3096 : // Action when bus current exceeds limit
3097 : // Range of overcurrentBus is 0 to 7.
3098 0 : (*overcurrentBus) = (_pg_data[_pg_byteindex] >> 5);
3099 :
3100 : // Action when phase current exceeds limit
3101 : // Range of overcurrentPhase is 0 to 7.
3102 0 : (*overcurrentPhase) = ((_pg_data[_pg_byteindex] >> 2) & 0x7);
3103 :
3104 : // Action when regen current exceeds limit
3105 : // Range of overcurrentRegen is 0 to 7.
3106 0 : _pg_tempbitfield = (_pg_data[_pg_byteindex] & 0x3);
3107 :
3108 0 : _pg_tempbitfield <<= 1;
3109 0 : _pg_tempbitfield |= (_pg_data[_pg_byteindex + 1] >> 7);
3110 :
3111 0 : (*overcurrentRegen) = _pg_tempbitfield;
3112 :
3113 : // Action when ESC temperature exceeds limit
3114 : // Range of overtemperatureInternal is 0 to 7.
3115 0 : (*overtemperatureInternal) = ((_pg_data[_pg_byteindex + 1] >> 4) & 0x7);
3116 :
3117 : // Action when motor temperature exceeds limit
3118 : // Range of overtemperatureMotor is 0 to 7.
3119 0 : (*overtemperatureMotor) = ((_pg_data[_pg_byteindex + 1] >> 1) & 0x7);
3120 :
3121 : // Action when motor speed exceeds limit
3122 : // Range of overspeed is 0 to 7.
3123 0 : _pg_tempbitfield = (_pg_data[_pg_byteindex + 1] & 0x1);
3124 :
3125 0 : _pg_tempbitfield <<= 2;
3126 0 : _pg_tempbitfield |= (_pg_data[_pg_byteindex + 2] >> 6);
3127 :
3128 0 : (*overspeed) = _pg_tempbitfield;
3129 :
3130 : // Action when DC voltage falls below configured lower limit
3131 : // Range of undervoltage is 0 to 7.
3132 0 : (*undervoltage) = ((_pg_data[_pg_byteindex + 2] >> 3) & 0x7);
3133 :
3134 : // Action when DC voltage exceeds configured upper limit
3135 : // Range of overvoltage is 0 to 7.
3136 0 : (*overvoltage) = ((_pg_data[_pg_byteindex + 2]) & 0x7);
3137 :
3138 : // Action when motor demag angle exceeds limit
3139 : // Range of demagAngle is 0 to 7.
3140 0 : (*demagAngle) = (_pg_data[_pg_byteindex + 3] >> 5);
3141 :
3142 0 : if(_pg_byteindex + 4 > _pg_numbytes)
3143 0 : return 1;
3144 :
3145 : // Action on motor commutation estimator failure
3146 : // Range of commutationLoss is 0 to 7.
3147 0 : (*commutationLoss) = ((_pg_data[_pg_byteindex + 3] >> 2) & 0x7);
3148 0 : _pg_byteindex += 4; // close bit field
3149 :
3150 0 : return 1;
3151 :
3152 : }// decodeESC_ProtectionActionsPacket
3153 :
3154 : /*!
3155 : * \brief Create the ESC_MotorParameters packet
3156 : *
3157 : * Motor and system parameters
3158 : * \param _pg_pkt points to the packet which will be created by this function
3159 : * \param _pg_user points to the user data that will be encoded in _pg_pkt
3160 : */
3161 0 : void encodeESC_MotorParametersPacketStructure(void* _pg_pkt, const ESC_MotorParameters_t* _pg_user)
3162 : {
3163 0 : uint8_t* _pg_data = getESCVelocityPacketData(_pg_pkt);
3164 0 : int _pg_byteindex = 0;
3165 :
3166 : // Reserved for future packet expansion
3167 0 : uint8ToBytes((uint8_t)(0), _pg_data, &_pg_byteindex);
3168 :
3169 : // Motor RPM constant
3170 : // Range of motorKv is 0 to 65535.
3171 0 : uint16ToBeBytes(_pg_user->motorKv, _pg_data, &_pg_byteindex);
3172 :
3173 : // Motor pole pairs count
3174 : // Range of polePairs is 0 to 255.
3175 0 : uint8ToBytes(_pg_user->polePairs, _pg_data, &_pg_byteindex);
3176 :
3177 : // Maximum motor speed
3178 : // Range of maxSpeed is 0 to 65535.
3179 0 : uint16ToBeBytes(_pg_user->maxSpeed, _pg_data, &_pg_byteindex);
3180 :
3181 : // complete the process of creating the packet
3182 0 : finishESCVelocityPacket(_pg_pkt, _pg_byteindex, getESC_MotorParametersPacketID());
3183 :
3184 0 : }// encodeESC_MotorParametersPacketStructure
3185 :
3186 : /*!
3187 : * \brief Decode the ESC_MotorParameters packet
3188 : *
3189 : * Motor and system parameters
3190 : * \param _pg_pkt points to the packet being decoded by this function
3191 : * \param _pg_user receives the data decoded from the packet
3192 : * \return 0 is returned if the packet ID or size is wrong, else 1
3193 : */
3194 0 : int decodeESC_MotorParametersPacketStructure(const void* _pg_pkt, ESC_MotorParameters_t* _pg_user)
3195 : {
3196 : int _pg_numbytes;
3197 0 : int _pg_byteindex = 0;
3198 : const uint8_t* _pg_data;
3199 :
3200 : // Verify the packet identifier
3201 0 : if(getESCVelocityPacketID(_pg_pkt) != getESC_MotorParametersPacketID())
3202 0 : return 0;
3203 :
3204 : // Verify the packet size
3205 0 : _pg_numbytes = getESCVelocityPacketSize(_pg_pkt);
3206 0 : if(_pg_numbytes < getESC_MotorParametersMinDataLength())
3207 0 : return 0;
3208 :
3209 : // The raw data from the packet
3210 0 : _pg_data = getESCVelocityPacketDataConst(_pg_pkt);
3211 :
3212 : // this packet has default fields, make sure they are set
3213 0 : _pg_user->maxSpeed = 20000;
3214 :
3215 : // Reserved for future packet expansion
3216 : // Range of reserved is 0 to 255.
3217 0 : _pg_user->reserved = uint8FromBytes(_pg_data, &_pg_byteindex);
3218 :
3219 : // Motor RPM constant
3220 : // Range of motorKv is 0 to 65535.
3221 0 : _pg_user->motorKv = uint16FromBeBytes(_pg_data, &_pg_byteindex);
3222 :
3223 : // Motor pole pairs count
3224 : // Range of polePairs is 0 to 255.
3225 0 : _pg_user->polePairs = uint8FromBytes(_pg_data, &_pg_byteindex);
3226 :
3227 0 : if(_pg_byteindex + 2 > _pg_numbytes)
3228 0 : return 1;
3229 :
3230 : // Maximum motor speed
3231 : // Range of maxSpeed is 0 to 65535.
3232 0 : _pg_user->maxSpeed = uint16FromBeBytes(_pg_data, &_pg_byteindex);
3233 :
3234 0 : return 1;
3235 :
3236 : }// decodeESC_MotorParametersPacketStructure
3237 :
3238 : /*!
3239 : * \brief Create the ESC_MotorHallSensorConfig packet
3240 : *
3241 : * Hall sensor configuration options
3242 : * \param _pg_pkt points to the packet which will be created by this function
3243 : * \param _pg_user points to the user data that will be encoded in _pg_pkt
3244 : */
3245 0 : void encodeESC_MotorHallSensorConfigPacketStructure(void* _pg_pkt, const ESC_MotorHallSensorConfig_t* _pg_user)
3246 : {
3247 0 : uint8_t* _pg_data = getESCVelocityPacketData(_pg_pkt);
3248 0 : int _pg_byteindex = 0;
3249 :
3250 : // Reserved for future use
3251 0 : _pg_data[_pg_byteindex] = 0;
3252 :
3253 : // Bypass extra checks for starting in hall sensor mode
3254 0 : _pg_data[_pg_byteindex] |= (uint8_t)((_pg_user->bypassStartingChecks == true) ? 1 : 0) << 5;
3255 :
3256 : // Hall sensor operational mode
3257 : // Range of mode is 0 to 31.
3258 0 : _pg_data[_pg_byteindex] |= (uint8_t)limitMax(_pg_user->mode, 31);
3259 0 : _pg_byteindex += 1; // close bit field
3260 :
3261 :
3262 : // Hall sensor U/V/W polarity
3263 : // Range of polarity is 0 to 255.
3264 0 : uint8ToBytes(_pg_user->polarity, _pg_data, &_pg_byteindex);
3265 :
3266 : // Time (in milliseconds) for no hall sensor transitions to trigger an error (0 = forever)
3267 : // Range of timeout is 0 to 65535.
3268 0 : uint16ToBeBytes(_pg_user->timeout, _pg_data, &_pg_byteindex);
3269 :
3270 : // Maximum electrical RPM when running under hall sensor control
3271 : // Range of maxSpeed is 0 to 65535.
3272 0 : uint16ToBeBytes(_pg_user->maxSpeed, _pg_data, &_pg_byteindex);
3273 :
3274 : // RPM threshold for transferring to sensorless motor control
3275 : // Range of transitionSpeed is 0 to 65535.
3276 0 : uint16ToBeBytes(_pg_user->transitionSpeed, _pg_data, &_pg_byteindex);
3277 :
3278 : // RPM hysteresis for switching between sensored and sensorless control
3279 : // Range of transitionHysteresis is 0 to 255.
3280 0 : uint8ToBytes(_pg_user->transitionHysteresis, _pg_data, &_pg_byteindex);
3281 :
3282 : // complete the process of creating the packet
3283 0 : finishESCVelocityPacket(_pg_pkt, _pg_byteindex, getESC_MotorHallSensorConfigPacketID());
3284 :
3285 0 : }// encodeESC_MotorHallSensorConfigPacketStructure
3286 :
3287 : /*!
3288 : * \brief Decode the ESC_MotorHallSensorConfig packet
3289 : *
3290 : * Hall sensor configuration options
3291 : * \param _pg_pkt points to the packet being decoded by this function
3292 : * \param _pg_user receives the data decoded from the packet
3293 : * \return 0 is returned if the packet ID or size is wrong, else 1
3294 : */
3295 0 : int decodeESC_MotorHallSensorConfigPacketStructure(const void* _pg_pkt, ESC_MotorHallSensorConfig_t* _pg_user)
3296 : {
3297 : int _pg_numbytes;
3298 0 : int _pg_byteindex = 0;
3299 : const uint8_t* _pg_data;
3300 :
3301 : // Verify the packet identifier
3302 0 : if(getESCVelocityPacketID(_pg_pkt) != getESC_MotorHallSensorConfigPacketID())
3303 0 : return 0;
3304 :
3305 : // Verify the packet size
3306 0 : _pg_numbytes = getESCVelocityPacketSize(_pg_pkt);
3307 0 : if(_pg_numbytes < getESC_MotorHallSensorConfigMinDataLength())
3308 0 : return 0;
3309 :
3310 : // The raw data from the packet
3311 0 : _pg_data = getESCVelocityPacketDataConst(_pg_pkt);
3312 :
3313 : // Reserved for future use
3314 :
3315 : // Bypass extra checks for starting in hall sensor mode
3316 0 : _pg_user->bypassStartingChecks = (((_pg_data[_pg_byteindex] >> 5) & 0x1)) ? true : false;
3317 :
3318 : // Hall sensor operational mode
3319 : // Range of mode is 0 to 31.
3320 0 : _pg_user->mode = ((_pg_data[_pg_byteindex]) & 0x1F);
3321 0 : _pg_byteindex += 1; // close bit field
3322 :
3323 : // Hall sensor U/V/W polarity
3324 : // Range of polarity is 0 to 255.
3325 0 : _pg_user->polarity = uint8FromBytes(_pg_data, &_pg_byteindex);
3326 :
3327 : // Time (in milliseconds) for no hall sensor transitions to trigger an error (0 = forever)
3328 : // Range of timeout is 0 to 65535.
3329 0 : _pg_user->timeout = uint16FromBeBytes(_pg_data, &_pg_byteindex);
3330 :
3331 : // Maximum electrical RPM when running under hall sensor control
3332 : // Range of maxSpeed is 0 to 65535.
3333 0 : _pg_user->maxSpeed = uint16FromBeBytes(_pg_data, &_pg_byteindex);
3334 :
3335 : // RPM threshold for transferring to sensorless motor control
3336 : // Range of transitionSpeed is 0 to 65535.
3337 0 : _pg_user->transitionSpeed = uint16FromBeBytes(_pg_data, &_pg_byteindex);
3338 :
3339 : // RPM hysteresis for switching between sensored and sensorless control
3340 : // Range of transitionHysteresis is 0 to 255.
3341 0 : _pg_user->transitionHysteresis = uint8FromBytes(_pg_data, &_pg_byteindex);
3342 :
3343 0 : return 1;
3344 :
3345 : }// decodeESC_MotorHallSensorConfigPacketStructure
3346 :
3347 : /*!
3348 : * \brief Create the ESC_Address packet
3349 : *
3350 : * This packet contains the serial number for the ESC. Additionally there are
3351 : * two extra values (each 16-bit) which can be programmed by the user for any
3352 : * purpose.
3353 : * \param _pg_pkt points to the packet which will be created by this function
3354 : * \param _pg_user points to the user data that will be encoded in _pg_pkt
3355 : */
3356 0 : void encodeESC_AddressPacketStructure(void* _pg_pkt, const ESC_Address_t* _pg_user)
3357 : {
3358 0 : uint8_t* _pg_data = getESCVelocityPacketData(_pg_pkt);
3359 0 : int _pg_byteindex = 0;
3360 :
3361 : // ESC hardware revision (OTP - not configurable by user)
3362 : // Range of HardwareRevision is 0 to 255.
3363 0 : uint8ToBytes(_pg_user->HardwareRevision, _pg_data, &_pg_byteindex);
3364 :
3365 : // ESC model (OTP - not configurable by user)
3366 : // Range of Model is 0 to 255.
3367 0 : uint8ToBytes(_pg_user->Model, _pg_data, &_pg_byteindex);
3368 :
3369 : // ESC Serial Number (OTP - not configurable by user)
3370 : // Range of SerialNumber is 0 to 65535.
3371 0 : uint16ToBeBytes(_pg_user->SerialNumber, _pg_data, &_pg_byteindex);
3372 :
3373 : // User ID Value A - user can set this value to any value
3374 : // Range of UserIDA is 0 to 65535.
3375 0 : uint16ToBeBytes(_pg_user->UserIDA, _pg_data, &_pg_byteindex);
3376 :
3377 : // User ID Value B - user can set this value to any value
3378 : // Range of UserIDB is 0 to 65535.
3379 0 : uint16ToBeBytes(_pg_user->UserIDB, _pg_data, &_pg_byteindex);
3380 :
3381 : // complete the process of creating the packet
3382 0 : finishESCVelocityPacket(_pg_pkt, _pg_byteindex, getESC_AddressPacketID());
3383 :
3384 0 : }// encodeESC_AddressPacketStructure
3385 :
3386 : /*!
3387 : * \brief Decode the ESC_Address packet
3388 : *
3389 : * This packet contains the serial number for the ESC. Additionally there are
3390 : * two extra values (each 16-bit) which can be programmed by the user for any
3391 : * purpose.
3392 : * \param _pg_pkt points to the packet being decoded by this function
3393 : * \param _pg_user receives the data decoded from the packet
3394 : * \return 0 is returned if the packet ID or size is wrong, else 1
3395 : */
3396 0 : int decodeESC_AddressPacketStructure(const void* _pg_pkt, ESC_Address_t* _pg_user)
3397 : {
3398 : int _pg_numbytes;
3399 0 : int _pg_byteindex = 0;
3400 : const uint8_t* _pg_data;
3401 :
3402 : // Verify the packet identifier
3403 0 : if(getESCVelocityPacketID(_pg_pkt) != getESC_AddressPacketID())
3404 0 : return 0;
3405 :
3406 : // Verify the packet size
3407 0 : _pg_numbytes = getESCVelocityPacketSize(_pg_pkt);
3408 0 : if(_pg_numbytes < getESC_AddressMinDataLength())
3409 0 : return 0;
3410 :
3411 : // The raw data from the packet
3412 0 : _pg_data = getESCVelocityPacketDataConst(_pg_pkt);
3413 :
3414 : // ESC hardware revision (OTP - not configurable by user)
3415 : // Range of HardwareRevision is 0 to 255.
3416 0 : _pg_user->HardwareRevision = uint8FromBytes(_pg_data, &_pg_byteindex);
3417 :
3418 : // ESC model (OTP - not configurable by user)
3419 : // Range of Model is 0 to 255.
3420 0 : _pg_user->Model = uint8FromBytes(_pg_data, &_pg_byteindex);
3421 :
3422 : // ESC Serial Number (OTP - not configurable by user)
3423 : // Range of SerialNumber is 0 to 65535.
3424 0 : _pg_user->SerialNumber = uint16FromBeBytes(_pg_data, &_pg_byteindex);
3425 :
3426 : // User ID Value A - user can set this value to any value
3427 : // Range of UserIDA is 0 to 65535.
3428 0 : _pg_user->UserIDA = uint16FromBeBytes(_pg_data, &_pg_byteindex);
3429 :
3430 : // User ID Value B - user can set this value to any value
3431 : // Range of UserIDB is 0 to 65535.
3432 0 : _pg_user->UserIDB = uint16FromBeBytes(_pg_data, &_pg_byteindex);
3433 :
3434 0 : return 1;
3435 :
3436 : }// decodeESC_AddressPacketStructure
3437 :
3438 : /*!
3439 : * \brief Create the ESC_Address packet
3440 : *
3441 : * This packet contains the serial number for the ESC. Additionally there are
3442 : * two extra values (each 16-bit) which can be programmed by the user for any
3443 : * purpose.
3444 : * \param _pg_pkt points to the packet which will be created by this function
3445 : * \param HardwareRevision is ESC hardware revision (OTP - not configurable by user)
3446 : * \param Model is ESC model (OTP - not configurable by user)
3447 : * \param SerialNumber is ESC Serial Number (OTP - not configurable by user)
3448 : * \param UserIDA is User ID Value A - user can set this value to any value
3449 : * \param UserIDB is User ID Value B - user can set this value to any value
3450 : */
3451 0 : void encodeESC_AddressPacket(void* _pg_pkt, uint8_t HardwareRevision, uint8_t Model, uint16_t SerialNumber, uint16_t UserIDA, uint16_t UserIDB)
3452 : {
3453 0 : uint8_t* _pg_data = getESCVelocityPacketData(_pg_pkt);
3454 0 : int _pg_byteindex = 0;
3455 :
3456 : // ESC hardware revision (OTP - not configurable by user)
3457 : // Range of HardwareRevision is 0 to 255.
3458 0 : uint8ToBytes(HardwareRevision, _pg_data, &_pg_byteindex);
3459 :
3460 : // ESC model (OTP - not configurable by user)
3461 : // Range of Model is 0 to 255.
3462 0 : uint8ToBytes(Model, _pg_data, &_pg_byteindex);
3463 :
3464 : // ESC Serial Number (OTP - not configurable by user)
3465 : // Range of SerialNumber is 0 to 65535.
3466 0 : uint16ToBeBytes(SerialNumber, _pg_data, &_pg_byteindex);
3467 :
3468 : // User ID Value A - user can set this value to any value
3469 : // Range of UserIDA is 0 to 65535.
3470 0 : uint16ToBeBytes(UserIDA, _pg_data, &_pg_byteindex);
3471 :
3472 : // User ID Value B - user can set this value to any value
3473 : // Range of UserIDB is 0 to 65535.
3474 0 : uint16ToBeBytes(UserIDB, _pg_data, &_pg_byteindex);
3475 :
3476 : // complete the process of creating the packet
3477 0 : finishESCVelocityPacket(_pg_pkt, _pg_byteindex, getESC_AddressPacketID());
3478 :
3479 0 : }// encodeESC_AddressPacket
3480 :
3481 : /*!
3482 : * \brief Decode the ESC_Address packet
3483 : *
3484 : * This packet contains the serial number for the ESC. Additionally there are
3485 : * two extra values (each 16-bit) which can be programmed by the user for any
3486 : * purpose.
3487 : * \param _pg_pkt points to the packet being decoded by this function
3488 : * \param HardwareRevision receives ESC hardware revision (OTP - not configurable by user)
3489 : * \param Model receives ESC model (OTP - not configurable by user)
3490 : * \param SerialNumber receives ESC Serial Number (OTP - not configurable by user)
3491 : * \param UserIDA receives User ID Value A - user can set this value to any value
3492 : * \param UserIDB receives User ID Value B - user can set this value to any value
3493 : * \return 0 is returned if the packet ID or size is wrong, else 1
3494 : */
3495 0 : int decodeESC_AddressPacket(const void* _pg_pkt, uint8_t* HardwareRevision, uint8_t* Model, uint16_t* SerialNumber, uint16_t* UserIDA, uint16_t* UserIDB)
3496 : {
3497 0 : int _pg_byteindex = 0;
3498 0 : const uint8_t* _pg_data = getESCVelocityPacketDataConst(_pg_pkt);
3499 0 : int _pg_numbytes = getESCVelocityPacketSize(_pg_pkt);
3500 :
3501 : // Verify the packet identifier
3502 0 : if(getESCVelocityPacketID(_pg_pkt) != getESC_AddressPacketID())
3503 0 : return 0;
3504 :
3505 0 : if(_pg_numbytes < getESC_AddressMinDataLength())
3506 0 : return 0;
3507 :
3508 : // ESC hardware revision (OTP - not configurable by user)
3509 : // Range of HardwareRevision is 0 to 255.
3510 0 : (*HardwareRevision) = uint8FromBytes(_pg_data, &_pg_byteindex);
3511 :
3512 : // ESC model (OTP - not configurable by user)
3513 : // Range of Model is 0 to 255.
3514 0 : (*Model) = uint8FromBytes(_pg_data, &_pg_byteindex);
3515 :
3516 : // ESC Serial Number (OTP - not configurable by user)
3517 : // Range of SerialNumber is 0 to 65535.
3518 0 : (*SerialNumber) = uint16FromBeBytes(_pg_data, &_pg_byteindex);
3519 :
3520 : // User ID Value A - user can set this value to any value
3521 : // Range of UserIDA is 0 to 65535.
3522 0 : (*UserIDA) = uint16FromBeBytes(_pg_data, &_pg_byteindex);
3523 :
3524 : // User ID Value B - user can set this value to any value
3525 : // Range of UserIDB is 0 to 65535.
3526 0 : (*UserIDB) = uint16FromBeBytes(_pg_data, &_pg_byteindex);
3527 :
3528 0 : return 1;
3529 :
3530 : }// decodeESC_AddressPacket
3531 :
3532 : /*!
3533 : * \brief Create the ESC_Title packet
3534 : *
3535 : * This packet contains a zero-terminated string (max-length 8) used to
3536 : * identify the particular ESC.
3537 : * \param _pg_pkt points to the packet which will be created by this function
3538 : * \param _pg_user points to the user data that will be encoded in _pg_pkt
3539 : */
3540 0 : void encodeESC_TitlePacketStructure(void* _pg_pkt, const ESC_Title_t* _pg_user)
3541 : {
3542 0 : uint8_t* _pg_data = getESCVelocityPacketData(_pg_pkt);
3543 0 : int _pg_byteindex = 0;
3544 0 : unsigned _pg_i = 0;
3545 :
3546 : // Description of this ESC
3547 : // Range of ESCTitle is 0 to 255.
3548 0 : for(_pg_i = 0; _pg_i < 8; _pg_i++)
3549 0 : uint8ToBytes(_pg_user->ESCTitle[_pg_i], _pg_data, &_pg_byteindex);
3550 :
3551 : // complete the process of creating the packet
3552 0 : finishESCVelocityPacket(_pg_pkt, _pg_byteindex, getESC_TitlePacketID());
3553 :
3554 0 : }// encodeESC_TitlePacketStructure
3555 :
3556 : /*!
3557 : * \brief Decode the ESC_Title packet
3558 : *
3559 : * This packet contains a zero-terminated string (max-length 8) used to
3560 : * identify the particular ESC.
3561 : * \param _pg_pkt points to the packet being decoded by this function
3562 : * \param _pg_user receives the data decoded from the packet
3563 : * \return 0 is returned if the packet ID or size is wrong, else 1
3564 : */
3565 0 : int decodeESC_TitlePacketStructure(const void* _pg_pkt, ESC_Title_t* _pg_user)
3566 : {
3567 : int _pg_numbytes;
3568 0 : int _pg_byteindex = 0;
3569 : const uint8_t* _pg_data;
3570 0 : unsigned _pg_i = 0;
3571 :
3572 : // Verify the packet identifier
3573 0 : if(getESCVelocityPacketID(_pg_pkt) != getESC_TitlePacketID())
3574 0 : return 0;
3575 :
3576 : // Verify the packet size
3577 0 : _pg_numbytes = getESCVelocityPacketSize(_pg_pkt);
3578 0 : if(_pg_numbytes < getESC_TitleMinDataLength())
3579 0 : return 0;
3580 :
3581 : // The raw data from the packet
3582 0 : _pg_data = getESCVelocityPacketDataConst(_pg_pkt);
3583 :
3584 : // Description of this ESC
3585 : // Range of ESCTitle is 0 to 255.
3586 0 : for(_pg_i = 0; _pg_i < 8; _pg_i++)
3587 0 : _pg_user->ESCTitle[_pg_i] = uint8FromBytes(_pg_data, &_pg_byteindex);
3588 :
3589 0 : return 1;
3590 :
3591 : }// decodeESC_TitlePacketStructure
3592 :
3593 : /*!
3594 : * \brief Create the ESC_Title packet
3595 : *
3596 : * This packet contains a zero-terminated string (max-length 8) used to
3597 : * identify the particular ESC.
3598 : * \param _pg_pkt points to the packet which will be created by this function
3599 : * \param ESCTitle is Description of this ESC
3600 : */
3601 0 : void encodeESC_TitlePacket(void* _pg_pkt, const uint8_t ESCTitle[8])
3602 : {
3603 0 : uint8_t* _pg_data = getESCVelocityPacketData(_pg_pkt);
3604 0 : int _pg_byteindex = 0;
3605 0 : unsigned _pg_i = 0;
3606 :
3607 : // Description of this ESC
3608 : // Range of ESCTitle is 0 to 255.
3609 0 : for(_pg_i = 0; _pg_i < 8; _pg_i++)
3610 0 : uint8ToBytes(ESCTitle[_pg_i], _pg_data, &_pg_byteindex);
3611 :
3612 : // complete the process of creating the packet
3613 0 : finishESCVelocityPacket(_pg_pkt, _pg_byteindex, getESC_TitlePacketID());
3614 :
3615 0 : }// encodeESC_TitlePacket
3616 :
3617 : /*!
3618 : * \brief Decode the ESC_Title packet
3619 : *
3620 : * This packet contains a zero-terminated string (max-length 8) used to
3621 : * identify the particular ESC.
3622 : * \param _pg_pkt points to the packet being decoded by this function
3623 : * \param ESCTitle receives Description of this ESC
3624 : * \return 0 is returned if the packet ID or size is wrong, else 1
3625 : */
3626 0 : int decodeESC_TitlePacket(const void* _pg_pkt, uint8_t ESCTitle[8])
3627 : {
3628 0 : unsigned _pg_i = 0;
3629 0 : int _pg_byteindex = 0;
3630 0 : const uint8_t* _pg_data = getESCVelocityPacketDataConst(_pg_pkt);
3631 0 : int _pg_numbytes = getESCVelocityPacketSize(_pg_pkt);
3632 :
3633 : // Verify the packet identifier
3634 0 : if(getESCVelocityPacketID(_pg_pkt) != getESC_TitlePacketID())
3635 0 : return 0;
3636 :
3637 0 : if(_pg_numbytes < getESC_TitleMinDataLength())
3638 0 : return 0;
3639 :
3640 : // Description of this ESC
3641 : // Range of ESCTitle is 0 to 255.
3642 0 : for(_pg_i = 0; _pg_i < 8; _pg_i++)
3643 0 : ESCTitle[_pg_i] = uint8FromBytes(_pg_data, &_pg_byteindex);
3644 :
3645 0 : return 1;
3646 :
3647 : }// decodeESC_TitlePacket
3648 :
3649 : /*!
3650 : * \brief Create the ESC_Firmware packet
3651 : *
3652 : * This packet contains the firmware version of the ESC
3653 : * \param _pg_pkt points to the packet which will be created by this function
3654 : * \param _pg_user points to the user data that will be encoded in _pg_pkt
3655 : */
3656 0 : void encodeESC_FirmwarePacketStructure(void* _pg_pkt, const ESC_Firmware_t* _pg_user)
3657 : {
3658 0 : uint8_t* _pg_data = getESCVelocityPacketData(_pg_pkt);
3659 0 : int _pg_byteindex = 0;
3660 :
3661 : // Major firmware version number
3662 : // Range of versionMajor is 0 to 255.
3663 0 : uint8ToBytes(_pg_user->versionMajor, _pg_data, &_pg_byteindex);
3664 :
3665 : // Minor firmware version numner
3666 : // Range of versionMinor is 0 to 255.
3667 0 : uint8ToBytes(_pg_user->versionMinor, _pg_data, &_pg_byteindex);
3668 :
3669 : // Firmware release date, day-of-month
3670 : // Range of versionDay is 0 to 255.
3671 0 : uint8ToBytes(_pg_user->versionDay, _pg_data, &_pg_byteindex);
3672 :
3673 : // Firmware release data, month-of-year
3674 : // Range of versionMonth is 0 to 255.
3675 0 : uint8ToBytes(_pg_user->versionMonth, _pg_data, &_pg_byteindex);
3676 :
3677 : // Firmware release date, year
3678 : // Range of versionYear is 0 to 65535.
3679 0 : uint16ToBeBytes(_pg_user->versionYear, _pg_data, &_pg_byteindex);
3680 :
3681 : // Firmware checksum
3682 : // Range of firmwareChecksum is 0 to 65535.
3683 0 : uint16ToBeBytes(_pg_user->firmwareChecksum, _pg_data, &_pg_byteindex);
3684 :
3685 : // complete the process of creating the packet
3686 0 : finishESCVelocityPacket(_pg_pkt, _pg_byteindex, getESC_FirmwarePacketID());
3687 :
3688 0 : }// encodeESC_FirmwarePacketStructure
3689 :
3690 : /*!
3691 : * \brief Decode the ESC_Firmware packet
3692 : *
3693 : * This packet contains the firmware version of the ESC
3694 : * \param _pg_pkt points to the packet being decoded by this function
3695 : * \param _pg_user receives the data decoded from the packet
3696 : * \return 0 is returned if the packet ID or size is wrong, else 1
3697 : */
3698 0 : int decodeESC_FirmwarePacketStructure(const void* _pg_pkt, ESC_Firmware_t* _pg_user)
3699 : {
3700 : int _pg_numbytes;
3701 0 : int _pg_byteindex = 0;
3702 : const uint8_t* _pg_data;
3703 :
3704 : // Verify the packet identifier
3705 0 : if(getESCVelocityPacketID(_pg_pkt) != getESC_FirmwarePacketID())
3706 0 : return 0;
3707 :
3708 : // Verify the packet size
3709 0 : _pg_numbytes = getESCVelocityPacketSize(_pg_pkt);
3710 0 : if(_pg_numbytes < getESC_FirmwareMinDataLength())
3711 0 : return 0;
3712 :
3713 : // The raw data from the packet
3714 0 : _pg_data = getESCVelocityPacketDataConst(_pg_pkt);
3715 :
3716 : // Major firmware version number
3717 : // Range of versionMajor is 0 to 255.
3718 0 : _pg_user->versionMajor = uint8FromBytes(_pg_data, &_pg_byteindex);
3719 :
3720 : // Minor firmware version numner
3721 : // Range of versionMinor is 0 to 255.
3722 0 : _pg_user->versionMinor = uint8FromBytes(_pg_data, &_pg_byteindex);
3723 :
3724 : // Firmware release date, day-of-month
3725 : // Range of versionDay is 0 to 255.
3726 0 : _pg_user->versionDay = uint8FromBytes(_pg_data, &_pg_byteindex);
3727 :
3728 : // Firmware release data, month-of-year
3729 : // Range of versionMonth is 0 to 255.
3730 0 : _pg_user->versionMonth = uint8FromBytes(_pg_data, &_pg_byteindex);
3731 :
3732 : // Firmware release date, year
3733 : // Range of versionYear is 0 to 65535.
3734 0 : _pg_user->versionYear = uint16FromBeBytes(_pg_data, &_pg_byteindex);
3735 :
3736 : // Firmware checksum
3737 : // Range of firmwareChecksum is 0 to 65535.
3738 0 : _pg_user->firmwareChecksum = uint16FromBeBytes(_pg_data, &_pg_byteindex);
3739 :
3740 0 : return 1;
3741 :
3742 : }// decodeESC_FirmwarePacketStructure
3743 :
3744 : /*!
3745 : * \brief Create the ESC_Firmware packet
3746 : *
3747 : * This packet contains the firmware version of the ESC
3748 : * \param _pg_pkt points to the packet which will be created by this function
3749 : * \param versionMajor is Major firmware version number
3750 : * \param versionMinor is Minor firmware version numner
3751 : * \param versionDay is Firmware release date, day-of-month
3752 : * \param versionMonth is Firmware release data, month-of-year
3753 : * \param versionYear is Firmware release date, year
3754 : * \param firmwareChecksum is Firmware checksum
3755 : */
3756 0 : void encodeESC_FirmwarePacket(void* _pg_pkt, uint8_t versionMajor, uint8_t versionMinor, uint8_t versionDay, uint8_t versionMonth, uint16_t versionYear, uint16_t firmwareChecksum)
3757 : {
3758 0 : uint8_t* _pg_data = getESCVelocityPacketData(_pg_pkt);
3759 0 : int _pg_byteindex = 0;
3760 :
3761 : // Major firmware version number
3762 : // Range of versionMajor is 0 to 255.
3763 0 : uint8ToBytes(versionMajor, _pg_data, &_pg_byteindex);
3764 :
3765 : // Minor firmware version numner
3766 : // Range of versionMinor is 0 to 255.
3767 0 : uint8ToBytes(versionMinor, _pg_data, &_pg_byteindex);
3768 :
3769 : // Firmware release date, day-of-month
3770 : // Range of versionDay is 0 to 255.
3771 0 : uint8ToBytes(versionDay, _pg_data, &_pg_byteindex);
3772 :
3773 : // Firmware release data, month-of-year
3774 : // Range of versionMonth is 0 to 255.
3775 0 : uint8ToBytes(versionMonth, _pg_data, &_pg_byteindex);
3776 :
3777 : // Firmware release date, year
3778 : // Range of versionYear is 0 to 65535.
3779 0 : uint16ToBeBytes(versionYear, _pg_data, &_pg_byteindex);
3780 :
3781 : // Firmware checksum
3782 : // Range of firmwareChecksum is 0 to 65535.
3783 0 : uint16ToBeBytes(firmwareChecksum, _pg_data, &_pg_byteindex);
3784 :
3785 : // complete the process of creating the packet
3786 0 : finishESCVelocityPacket(_pg_pkt, _pg_byteindex, getESC_FirmwarePacketID());
3787 :
3788 0 : }// encodeESC_FirmwarePacket
3789 :
3790 : /*!
3791 : * \brief Decode the ESC_Firmware packet
3792 : *
3793 : * This packet contains the firmware version of the ESC
3794 : * \param _pg_pkt points to the packet being decoded by this function
3795 : * \param versionMajor receives Major firmware version number
3796 : * \param versionMinor receives Minor firmware version numner
3797 : * \param versionDay receives Firmware release date, day-of-month
3798 : * \param versionMonth receives Firmware release data, month-of-year
3799 : * \param versionYear receives Firmware release date, year
3800 : * \param firmwareChecksum receives Firmware checksum
3801 : * \return 0 is returned if the packet ID or size is wrong, else 1
3802 : */
3803 0 : int decodeESC_FirmwarePacket(const void* _pg_pkt, uint8_t* versionMajor, uint8_t* versionMinor, uint8_t* versionDay, uint8_t* versionMonth, uint16_t* versionYear, uint16_t* firmwareChecksum)
3804 : {
3805 0 : int _pg_byteindex = 0;
3806 0 : const uint8_t* _pg_data = getESCVelocityPacketDataConst(_pg_pkt);
3807 0 : int _pg_numbytes = getESCVelocityPacketSize(_pg_pkt);
3808 :
3809 : // Verify the packet identifier
3810 0 : if(getESCVelocityPacketID(_pg_pkt) != getESC_FirmwarePacketID())
3811 0 : return 0;
3812 :
3813 0 : if(_pg_numbytes < getESC_FirmwareMinDataLength())
3814 0 : return 0;
3815 :
3816 : // Major firmware version number
3817 : // Range of versionMajor is 0 to 255.
3818 0 : (*versionMajor) = uint8FromBytes(_pg_data, &_pg_byteindex);
3819 :
3820 : // Minor firmware version numner
3821 : // Range of versionMinor is 0 to 255.
3822 0 : (*versionMinor) = uint8FromBytes(_pg_data, &_pg_byteindex);
3823 :
3824 : // Firmware release date, day-of-month
3825 : // Range of versionDay is 0 to 255.
3826 0 : (*versionDay) = uint8FromBytes(_pg_data, &_pg_byteindex);
3827 :
3828 : // Firmware release data, month-of-year
3829 : // Range of versionMonth is 0 to 255.
3830 0 : (*versionMonth) = uint8FromBytes(_pg_data, &_pg_byteindex);
3831 :
3832 : // Firmware release date, year
3833 : // Range of versionYear is 0 to 65535.
3834 0 : (*versionYear) = uint16FromBeBytes(_pg_data, &_pg_byteindex);
3835 :
3836 : // Firmware checksum
3837 : // Range of firmwareChecksum is 0 to 65535.
3838 0 : (*firmwareChecksum) = uint16FromBeBytes(_pg_data, &_pg_byteindex);
3839 :
3840 0 : return 1;
3841 :
3842 : }// decodeESC_FirmwarePacket
3843 :
3844 : /*!
3845 : * \brief Create the ESC_SystemInfo packet
3846 : *
3847 : * This packet contains system runtime information
3848 : * \param _pg_pkt points to the packet which will be created by this function
3849 : * \param _pg_user points to the user data that will be encoded in _pg_pkt
3850 : */
3851 0 : void encodeESC_SystemInfoPacketStructure(void* _pg_pkt, const ESC_SystemInfo_t* _pg_user)
3852 : {
3853 0 : uint8_t* _pg_data = getESCVelocityPacketData(_pg_pkt);
3854 0 : int _pg_byteindex = 0;
3855 :
3856 : // Number of milliseconds since the ESC last experienced a reset/power-on event
3857 : // Range of millisecondsSinceReset is 0 to 4294967295.
3858 0 : uint32ToBeBytes(_pg_user->millisecondsSinceReset, _pg_data, &_pg_byteindex);
3859 :
3860 : // Number of power cycle events that the ESC has experienced
3861 : // Range of powerCycles is 0 to 65535.
3862 0 : uint16ToBeBytes(_pg_user->powerCycles, _pg_data, &_pg_byteindex);
3863 :
3864 : // Processor RESET code for debug purposes
3865 : // Range of resetCode is 0 to 255.
3866 0 : uint8ToBytes(_pg_user->resetCode, _pg_data, &_pg_byteindex);
3867 :
3868 : // Processor usage
3869 : // Range of cpuOccupancy is 0 to 255.
3870 0 : uint8ToBytes(_pg_user->cpuOccupancy, _pg_data, &_pg_byteindex);
3871 :
3872 : // complete the process of creating the packet
3873 0 : finishESCVelocityPacket(_pg_pkt, _pg_byteindex, getESC_SystemInfoPacketID());
3874 :
3875 0 : }// encodeESC_SystemInfoPacketStructure
3876 :
3877 : /*!
3878 : * \brief Decode the ESC_SystemInfo packet
3879 : *
3880 : * This packet contains system runtime information
3881 : * \param _pg_pkt points to the packet being decoded by this function
3882 : * \param _pg_user receives the data decoded from the packet
3883 : * \return 0 is returned if the packet ID or size is wrong, else 1
3884 : */
3885 0 : int decodeESC_SystemInfoPacketStructure(const void* _pg_pkt, ESC_SystemInfo_t* _pg_user)
3886 : {
3887 : int _pg_numbytes;
3888 0 : int _pg_byteindex = 0;
3889 : const uint8_t* _pg_data;
3890 :
3891 : // Verify the packet identifier
3892 0 : if(getESCVelocityPacketID(_pg_pkt) != getESC_SystemInfoPacketID())
3893 0 : return 0;
3894 :
3895 : // Verify the packet size
3896 0 : _pg_numbytes = getESCVelocityPacketSize(_pg_pkt);
3897 0 : if(_pg_numbytes < getESC_SystemInfoMinDataLength())
3898 0 : return 0;
3899 :
3900 : // The raw data from the packet
3901 0 : _pg_data = getESCVelocityPacketDataConst(_pg_pkt);
3902 :
3903 : // Number of milliseconds since the ESC last experienced a reset/power-on event
3904 : // Range of millisecondsSinceReset is 0 to 4294967295.
3905 0 : _pg_user->millisecondsSinceReset = uint32FromBeBytes(_pg_data, &_pg_byteindex);
3906 :
3907 : // Number of power cycle events that the ESC has experienced
3908 : // Range of powerCycles is 0 to 65535.
3909 0 : _pg_user->powerCycles = uint16FromBeBytes(_pg_data, &_pg_byteindex);
3910 :
3911 : // Processor RESET code for debug purposes
3912 : // Range of resetCode is 0 to 255.
3913 0 : _pg_user->resetCode = uint8FromBytes(_pg_data, &_pg_byteindex);
3914 :
3915 : // Processor usage
3916 : // Range of cpuOccupancy is 0 to 255.
3917 0 : _pg_user->cpuOccupancy = uint8FromBytes(_pg_data, &_pg_byteindex);
3918 :
3919 0 : return 1;
3920 :
3921 : }// decodeESC_SystemInfoPacketStructure
3922 :
3923 : /*!
3924 : * \brief Create the ESC_SystemInfo packet
3925 : *
3926 : * This packet contains system runtime information
3927 : * \param _pg_pkt points to the packet which will be created by this function
3928 : * \param millisecondsSinceReset is Number of milliseconds since the ESC last experienced a reset/power-on event
3929 : * \param powerCycles is Number of power cycle events that the ESC has experienced
3930 : * \param resetCode is Processor RESET code for debug purposes
3931 : * \param cpuOccupancy is Processor usage
3932 : */
3933 0 : void encodeESC_SystemInfoPacket(void* _pg_pkt, uint32_t millisecondsSinceReset, uint16_t powerCycles, uint8_t resetCode, uint8_t cpuOccupancy)
3934 : {
3935 0 : uint8_t* _pg_data = getESCVelocityPacketData(_pg_pkt);
3936 0 : int _pg_byteindex = 0;
3937 :
3938 : // Number of milliseconds since the ESC last experienced a reset/power-on event
3939 : // Range of millisecondsSinceReset is 0 to 4294967295.
3940 0 : uint32ToBeBytes(millisecondsSinceReset, _pg_data, &_pg_byteindex);
3941 :
3942 : // Number of power cycle events that the ESC has experienced
3943 : // Range of powerCycles is 0 to 65535.
3944 0 : uint16ToBeBytes(powerCycles, _pg_data, &_pg_byteindex);
3945 :
3946 : // Processor RESET code for debug purposes
3947 : // Range of resetCode is 0 to 255.
3948 0 : uint8ToBytes(resetCode, _pg_data, &_pg_byteindex);
3949 :
3950 : // Processor usage
3951 : // Range of cpuOccupancy is 0 to 255.
3952 0 : uint8ToBytes(cpuOccupancy, _pg_data, &_pg_byteindex);
3953 :
3954 : // complete the process of creating the packet
3955 0 : finishESCVelocityPacket(_pg_pkt, _pg_byteindex, getESC_SystemInfoPacketID());
3956 :
3957 0 : }// encodeESC_SystemInfoPacket
3958 :
3959 : /*!
3960 : * \brief Decode the ESC_SystemInfo packet
3961 : *
3962 : * This packet contains system runtime information
3963 : * \param _pg_pkt points to the packet being decoded by this function
3964 : * \param millisecondsSinceReset receives Number of milliseconds since the ESC last experienced a reset/power-on event
3965 : * \param powerCycles receives Number of power cycle events that the ESC has experienced
3966 : * \param resetCode receives Processor RESET code for debug purposes
3967 : * \param cpuOccupancy receives Processor usage
3968 : * \return 0 is returned if the packet ID or size is wrong, else 1
3969 : */
3970 0 : int decodeESC_SystemInfoPacket(const void* _pg_pkt, uint32_t* millisecondsSinceReset, uint16_t* powerCycles, uint8_t* resetCode, uint8_t* cpuOccupancy)
3971 : {
3972 0 : int _pg_byteindex = 0;
3973 0 : const uint8_t* _pg_data = getESCVelocityPacketDataConst(_pg_pkt);
3974 0 : int _pg_numbytes = getESCVelocityPacketSize(_pg_pkt);
3975 :
3976 : // Verify the packet identifier
3977 0 : if(getESCVelocityPacketID(_pg_pkt) != getESC_SystemInfoPacketID())
3978 0 : return 0;
3979 :
3980 0 : if(_pg_numbytes < getESC_SystemInfoMinDataLength())
3981 0 : return 0;
3982 :
3983 : // Number of milliseconds since the ESC last experienced a reset/power-on event
3984 : // Range of millisecondsSinceReset is 0 to 4294967295.
3985 0 : (*millisecondsSinceReset) = uint32FromBeBytes(_pg_data, &_pg_byteindex);
3986 :
3987 : // Number of power cycle events that the ESC has experienced
3988 : // Range of powerCycles is 0 to 65535.
3989 0 : (*powerCycles) = uint16FromBeBytes(_pg_data, &_pg_byteindex);
3990 :
3991 : // Processor RESET code for debug purposes
3992 : // Range of resetCode is 0 to 255.
3993 0 : (*resetCode) = uint8FromBytes(_pg_data, &_pg_byteindex);
3994 :
3995 : // Processor usage
3996 : // Range of cpuOccupancy is 0 to 255.
3997 0 : (*cpuOccupancy) = uint8FromBytes(_pg_data, &_pg_byteindex);
3998 :
3999 0 : return 1;
4000 :
4001 : }// decodeESC_SystemInfoPacket
4002 :
4003 : /*!
4004 : * \brief Create the ESC_TelemetrySettings packet
4005 : *
4006 : * This packet contains the telemetry packet configuration
4007 : * \param _pg_pkt points to the packet which will be created by this function
4008 : * \param _pg_user points to the user data that will be encoded in _pg_pkt
4009 : */
4010 0 : void encodeESC_TelemetrySettingsPacketStructure(void* _pg_pkt, const ESC_TelemetrySettings_t* _pg_user)
4011 : {
4012 0 : uint8_t* _pg_data = getESCVelocityPacketData(_pg_pkt);
4013 0 : int _pg_byteindex = 0;
4014 :
4015 : // Telemetry settings
4016 0 : encodeESC_TelemetryConfig_t(_pg_data, &_pg_byteindex, &_pg_user->settings);
4017 :
4018 : // The API version of the ESC
4019 0 : stringToBytes(getESCVelocityVersion(), _pg_data, &_pg_byteindex, 5, 0);
4020 :
4021 : // complete the process of creating the packet
4022 0 : finishESCVelocityPacket(_pg_pkt, _pg_byteindex, getESC_TelemetrySettingsPacketID());
4023 :
4024 0 : }// encodeESC_TelemetrySettingsPacketStructure
4025 :
4026 : /*!
4027 : * \brief Decode the ESC_TelemetrySettings packet
4028 : *
4029 : * This packet contains the telemetry packet configuration
4030 : * \param _pg_pkt points to the packet being decoded by this function
4031 : * \param _pg_user receives the data decoded from the packet
4032 : * \return 0 is returned if the packet ID or size is wrong, else 1
4033 : */
4034 0 : int decodeESC_TelemetrySettingsPacketStructure(const void* _pg_pkt, ESC_TelemetrySettings_t* _pg_user)
4035 : {
4036 : int _pg_numbytes;
4037 0 : int _pg_byteindex = 0;
4038 : const uint8_t* _pg_data;
4039 :
4040 : // Verify the packet identifier
4041 0 : if(getESCVelocityPacketID(_pg_pkt) != getESC_TelemetrySettingsPacketID())
4042 0 : return 0;
4043 :
4044 : // Verify the packet size
4045 0 : _pg_numbytes = getESCVelocityPacketSize(_pg_pkt);
4046 0 : if(_pg_numbytes < getESC_TelemetrySettingsMinDataLength())
4047 0 : return 0;
4048 :
4049 : // The raw data from the packet
4050 0 : _pg_data = getESCVelocityPacketDataConst(_pg_pkt);
4051 :
4052 : // Telemetry settings
4053 0 : if(decodeESC_TelemetryConfig_t(_pg_data, &_pg_byteindex, &_pg_user->settings) == 0)
4054 0 : return 0;
4055 :
4056 : // The API version of the ESC
4057 0 : stringFromBytes(_pg_user->apiVersion, _pg_data, &_pg_byteindex, 5, 0);
4058 :
4059 0 : return 1;
4060 :
4061 : }// decodeESC_TelemetrySettingsPacketStructure
4062 :
4063 : /*!
4064 : * \brief Create the ESC_TelemetrySettings packet
4065 : *
4066 : * This packet contains the telemetry packet configuration
4067 : * \param _pg_pkt points to the packet which will be created by this function
4068 : * \param settings is Telemetry settings
4069 : */
4070 0 : void encodeESC_TelemetrySettingsPacket(void* _pg_pkt, const ESC_TelemetryConfig_t* settings)
4071 : {
4072 0 : uint8_t* _pg_data = getESCVelocityPacketData(_pg_pkt);
4073 0 : int _pg_byteindex = 0;
4074 :
4075 : // Telemetry settings
4076 0 : encodeESC_TelemetryConfig_t(_pg_data, &_pg_byteindex, settings);
4077 :
4078 : // The API version of the ESC
4079 0 : stringToBytes(getESCVelocityVersion(), _pg_data, &_pg_byteindex, 5, 0);
4080 :
4081 : // complete the process of creating the packet
4082 0 : finishESCVelocityPacket(_pg_pkt, _pg_byteindex, getESC_TelemetrySettingsPacketID());
4083 :
4084 0 : }// encodeESC_TelemetrySettingsPacket
4085 :
4086 : /*!
4087 : * \brief Decode the ESC_TelemetrySettings packet
4088 : *
4089 : * This packet contains the telemetry packet configuration
4090 : * \param _pg_pkt points to the packet being decoded by this function
4091 : * \param settings receives Telemetry settings
4092 : * \param apiVersion receives The API version of the ESC
4093 : * \return 0 is returned if the packet ID or size is wrong, else 1
4094 : */
4095 0 : int decodeESC_TelemetrySettingsPacket(const void* _pg_pkt, ESC_TelemetryConfig_t* settings, char apiVersion[5])
4096 : {
4097 0 : int _pg_byteindex = 0;
4098 0 : const uint8_t* _pg_data = getESCVelocityPacketDataConst(_pg_pkt);
4099 0 : int _pg_numbytes = getESCVelocityPacketSize(_pg_pkt);
4100 :
4101 : // Verify the packet identifier
4102 0 : if(getESCVelocityPacketID(_pg_pkt) != getESC_TelemetrySettingsPacketID())
4103 0 : return 0;
4104 :
4105 0 : if(_pg_numbytes < getESC_TelemetrySettingsMinDataLength())
4106 0 : return 0;
4107 :
4108 : // Telemetry settings
4109 0 : if(decodeESC_TelemetryConfig_t(_pg_data, &_pg_byteindex, settings) == 0)
4110 0 : return 0;
4111 :
4112 : // The API version of the ESC
4113 0 : stringFromBytes(apiVersion, _pg_data, &_pg_byteindex, 5, 0);
4114 :
4115 0 : return 1;
4116 :
4117 : }// decodeESC_TelemetrySettingsPacket
4118 :
4119 : /*!
4120 : * \brief Create the ESC_EEPROMSettings packet
4121 : *
4122 : * This packet contains information on the non-volatile ESC settings
4123 : * \param _pg_pkt points to the packet which will be created by this function
4124 : * \param _pg_user points to the user data that will be encoded in _pg_pkt
4125 : */
4126 0 : void encodeESC_EEPROMSettingsPacketStructure(void* _pg_pkt, const ESC_EEPROMSettings_t* _pg_user)
4127 : {
4128 0 : uint8_t* _pg_data = getESCVelocityPacketData(_pg_pkt);
4129 0 : int _pg_byteindex = 0;
4130 :
4131 : // Set if the ESC settings are locked
4132 0 : _pg_data[_pg_byteindex] = (uint8_t)((_pg_user->settingsLocked == true) ? 1 : 0) << 7;
4133 :
4134 : // Version of EEPROM data
4135 : // Range of version is 0 to 127.
4136 0 : _pg_data[_pg_byteindex] |= (uint8_t)limitMax(_pg_user->version, 127);
4137 0 : _pg_byteindex += 1; // close bit field
4138 :
4139 :
4140 : // Size of settings data
4141 : // Range of size is 0 to 65535.
4142 0 : uint16ToBeBytes(_pg_user->size, _pg_data, &_pg_byteindex);
4143 :
4144 : // Settings checksum
4145 : // Range of checksum is 0 to 65535.
4146 0 : uint16ToBeBytes(_pg_user->checksum, _pg_data, &_pg_byteindex);
4147 :
4148 : // Validated settings checksum
4149 : // Range of validatedChecksum is 0 to 65535.
4150 0 : uint16ToBeBytes(_pg_user->validatedChecksum, _pg_data, &_pg_byteindex);
4151 :
4152 : // complete the process of creating the packet
4153 0 : finishESCVelocityPacket(_pg_pkt, _pg_byteindex, getESC_EEPROMSettingsPacketID());
4154 :
4155 0 : }// encodeESC_EEPROMSettingsPacketStructure
4156 :
4157 : /*!
4158 : * \brief Decode the ESC_EEPROMSettings packet
4159 : *
4160 : * This packet contains information on the non-volatile ESC settings
4161 : * \param _pg_pkt points to the packet being decoded by this function
4162 : * \param _pg_user receives the data decoded from the packet
4163 : * \return 0 is returned if the packet ID or size is wrong, else 1
4164 : */
4165 0 : int decodeESC_EEPROMSettingsPacketStructure(const void* _pg_pkt, ESC_EEPROMSettings_t* _pg_user)
4166 : {
4167 : int _pg_numbytes;
4168 0 : int _pg_byteindex = 0;
4169 : const uint8_t* _pg_data;
4170 :
4171 : // Verify the packet identifier
4172 0 : if(getESCVelocityPacketID(_pg_pkt) != getESC_EEPROMSettingsPacketID())
4173 0 : return 0;
4174 :
4175 : // Verify the packet size
4176 0 : _pg_numbytes = getESCVelocityPacketSize(_pg_pkt);
4177 0 : if(_pg_numbytes < getESC_EEPROMSettingsMinDataLength())
4178 0 : return 0;
4179 :
4180 : // The raw data from the packet
4181 0 : _pg_data = getESCVelocityPacketDataConst(_pg_pkt);
4182 :
4183 : // this packet has default fields, make sure they are set
4184 0 : _pg_user->validatedChecksum = 0;
4185 :
4186 : // Set if the ESC settings are locked
4187 0 : _pg_user->settingsLocked = ((_pg_data[_pg_byteindex] >> 7)) ? true : false;
4188 :
4189 : // Version of EEPROM data
4190 : // Range of version is 0 to 127.
4191 0 : _pg_user->version = ((_pg_data[_pg_byteindex]) & 0x7F);
4192 0 : _pg_byteindex += 1; // close bit field
4193 :
4194 : // Size of settings data
4195 : // Range of size is 0 to 65535.
4196 0 : _pg_user->size = uint16FromBeBytes(_pg_data, &_pg_byteindex);
4197 :
4198 : // Settings checksum
4199 : // Range of checksum is 0 to 65535.
4200 0 : _pg_user->checksum = uint16FromBeBytes(_pg_data, &_pg_byteindex);
4201 :
4202 0 : if(_pg_byteindex + 2 > _pg_numbytes)
4203 0 : return 1;
4204 :
4205 : // Validated settings checksum
4206 : // Range of validatedChecksum is 0 to 65535.
4207 0 : _pg_user->validatedChecksum = uint16FromBeBytes(_pg_data, &_pg_byteindex);
4208 :
4209 0 : return 1;
4210 :
4211 : }// decodeESC_EEPROMSettingsPacketStructure
4212 :
4213 : /*!
4214 : * \brief Create the ESC_EEPROMSettings packet
4215 : *
4216 : * This packet contains information on the non-volatile ESC settings
4217 : * \param _pg_pkt points to the packet which will be created by this function
4218 : * \param settingsLocked is Set if the ESC settings are locked
4219 : * \param version is Version of EEPROM data
4220 : * \param size is Size of settings data
4221 : * \param checksum is Settings checksum
4222 : * \param validatedChecksum is Validated settings checksum
4223 : */
4224 0 : void encodeESC_EEPROMSettingsPacket(void* _pg_pkt, bool settingsLocked, uint8_t version, uint16_t size, uint16_t checksum, uint16_t validatedChecksum)
4225 : {
4226 0 : uint8_t* _pg_data = getESCVelocityPacketData(_pg_pkt);
4227 0 : int _pg_byteindex = 0;
4228 :
4229 : // Set if the ESC settings are locked
4230 0 : _pg_data[_pg_byteindex] = (uint8_t)((settingsLocked == true) ? 1 : 0) << 7;
4231 :
4232 : // Version of EEPROM data
4233 : // Range of version is 0 to 127.
4234 0 : _pg_data[_pg_byteindex] |= (uint8_t)limitMax(version, 127);
4235 0 : _pg_byteindex += 1; // close bit field
4236 :
4237 : // Size of settings data
4238 : // Range of size is 0 to 65535.
4239 0 : uint16ToBeBytes(size, _pg_data, &_pg_byteindex);
4240 :
4241 : // Settings checksum
4242 : // Range of checksum is 0 to 65535.
4243 0 : uint16ToBeBytes(checksum, _pg_data, &_pg_byteindex);
4244 :
4245 : // Validated settings checksum
4246 : // Range of validatedChecksum is 0 to 65535.
4247 0 : uint16ToBeBytes(validatedChecksum, _pg_data, &_pg_byteindex);
4248 :
4249 : // complete the process of creating the packet
4250 0 : finishESCVelocityPacket(_pg_pkt, _pg_byteindex, getESC_EEPROMSettingsPacketID());
4251 :
4252 0 : }// encodeESC_EEPROMSettingsPacket
4253 :
4254 : /*!
4255 : * \brief Decode the ESC_EEPROMSettings packet
4256 : *
4257 : * This packet contains information on the non-volatile ESC settings
4258 : * \param _pg_pkt points to the packet being decoded by this function
4259 : * \param settingsLocked receives Set if the ESC settings are locked
4260 : * \param version receives Version of EEPROM data
4261 : * \param size receives Size of settings data
4262 : * \param checksum receives Settings checksum
4263 : * \param validatedChecksum receives Validated settings checksum
4264 : * \return 0 is returned if the packet ID or size is wrong, else 1
4265 : */
4266 0 : int decodeESC_EEPROMSettingsPacket(const void* _pg_pkt, bool* settingsLocked, uint8_t* version, uint16_t* size, uint16_t* checksum, uint16_t* validatedChecksum)
4267 : {
4268 0 : int _pg_byteindex = 0;
4269 0 : const uint8_t* _pg_data = getESCVelocityPacketDataConst(_pg_pkt);
4270 0 : int _pg_numbytes = getESCVelocityPacketSize(_pg_pkt);
4271 :
4272 : // Verify the packet identifier
4273 0 : if(getESCVelocityPacketID(_pg_pkt) != getESC_EEPROMSettingsPacketID())
4274 0 : return 0;
4275 :
4276 0 : if(_pg_numbytes < getESC_EEPROMSettingsMinDataLength())
4277 0 : return 0;
4278 :
4279 : // this packet has default fields, make sure they are set
4280 0 : (*validatedChecksum) = 0;
4281 :
4282 : // Set if the ESC settings are locked
4283 0 : (*settingsLocked) = ((_pg_data[_pg_byteindex] >> 7)) ? true : false;
4284 :
4285 : // Version of EEPROM data
4286 : // Range of version is 0 to 127.
4287 0 : (*version) = ((_pg_data[_pg_byteindex]) & 0x7F);
4288 0 : _pg_byteindex += 1; // close bit field
4289 :
4290 : // Size of settings data
4291 : // Range of size is 0 to 65535.
4292 0 : (*size) = uint16FromBeBytes(_pg_data, &_pg_byteindex);
4293 :
4294 : // Settings checksum
4295 : // Range of checksum is 0 to 65535.
4296 0 : (*checksum) = uint16FromBeBytes(_pg_data, &_pg_byteindex);
4297 :
4298 0 : if(_pg_byteindex + 2 > _pg_numbytes)
4299 0 : return 1;
4300 :
4301 : // Validated settings checksum
4302 : // Range of validatedChecksum is 0 to 65535.
4303 0 : (*validatedChecksum) = uint16FromBeBytes(_pg_data, &_pg_byteindex);
4304 :
4305 0 : return 1;
4306 :
4307 : }// decodeESC_EEPROMSettingsPacket
4308 :
4309 : /*!
4310 : * \brief Create the ESC_TempSensorConfig packet
4311 : *
4312 : * Motor temperature sensor settings
4313 : * \param _pg_pkt points to the packet which will be created by this function
4314 : * \param _pg_user points to the user data that will be encoded in _pg_pkt
4315 : */
4316 0 : void encodeESC_TempSensorConfigPacketStructure(void* _pg_pkt, const ESC_TempSensorConfig_t* _pg_user)
4317 : {
4318 0 : uint8_t* _pg_data = getESCVelocityPacketData(_pg_pkt);
4319 0 : int _pg_byteindex = 0;
4320 0 : unsigned _pg_i = 0;
4321 :
4322 : // Reference temperature values
4323 : // Range of NTC_T is -32768 to 32767.
4324 0 : for(_pg_i = 0; _pg_i < 3; _pg_i++)
4325 0 : int16ToBeBytes(_pg_user->NTC_T[_pg_i], _pg_data, &_pg_byteindex);
4326 :
4327 : // Reference resistance values
4328 : // Range of NTC_R is -3.402823466e+38f to 3.402823466e+38f.
4329 0 : for(_pg_i = 0; _pg_i < 3; _pg_i++)
4330 0 : float32ToBeBytes((float)_pg_user->NTC_R[_pg_i], _pg_data, &_pg_byteindex);
4331 :
4332 : // Beta constant
4333 : // Range of Beta is 0 to 65535.
4334 0 : uint16ToBeBytes(_pg_user->Beta, _pg_data, &_pg_byteindex);
4335 :
4336 : // complete the process of creating the packet
4337 0 : finishESCVelocityPacket(_pg_pkt, _pg_byteindex, getESC_TempSensorConfigPacketID());
4338 :
4339 0 : }// encodeESC_TempSensorConfigPacketStructure
4340 :
4341 : /*!
4342 : * \brief Decode the ESC_TempSensorConfig packet
4343 : *
4344 : * Motor temperature sensor settings
4345 : * \param _pg_pkt points to the packet being decoded by this function
4346 : * \param _pg_user receives the data decoded from the packet
4347 : * \return 0 is returned if the packet ID or size is wrong, else 1
4348 : */
4349 0 : int decodeESC_TempSensorConfigPacketStructure(const void* _pg_pkt, ESC_TempSensorConfig_t* _pg_user)
4350 : {
4351 : int _pg_numbytes;
4352 0 : int _pg_byteindex = 0;
4353 : const uint8_t* _pg_data;
4354 0 : unsigned _pg_i = 0;
4355 :
4356 : // Verify the packet identifier
4357 0 : if(getESCVelocityPacketID(_pg_pkt) != getESC_TempSensorConfigPacketID())
4358 0 : return 0;
4359 :
4360 : // Verify the packet size
4361 0 : _pg_numbytes = getESCVelocityPacketSize(_pg_pkt);
4362 0 : if(_pg_numbytes < getESC_TempSensorConfigMinDataLength())
4363 0 : return 0;
4364 :
4365 : // The raw data from the packet
4366 0 : _pg_data = getESCVelocityPacketDataConst(_pg_pkt);
4367 :
4368 : // Reference temperature values
4369 : // Range of NTC_T is -32768 to 32767.
4370 0 : for(_pg_i = 0; _pg_i < 3; _pg_i++)
4371 0 : _pg_user->NTC_T[_pg_i] = int16FromBeBytes(_pg_data, &_pg_byteindex);
4372 :
4373 : // Reference resistance values
4374 : // Range of NTC_R is -3.402823466e+38f to 3.402823466e+38f.
4375 0 : for(_pg_i = 0; _pg_i < 3; _pg_i++)
4376 0 : _pg_user->NTC_R[_pg_i] = float32FromBeBytes(_pg_data, &_pg_byteindex);
4377 :
4378 : // Beta constant
4379 : // Range of Beta is 0 to 65535.
4380 0 : _pg_user->Beta = uint16FromBeBytes(_pg_data, &_pg_byteindex);
4381 :
4382 0 : return 1;
4383 :
4384 : }// decodeESC_TempSensorConfigPacketStructure
4385 :
4386 : /*!
4387 : * \brief Create the ESC_TempSensorConfig packet
4388 : *
4389 : * Motor temperature sensor settings
4390 : * \param _pg_pkt points to the packet which will be created by this function
4391 : * \param NTC_T is Reference temperature values
4392 : * \param NTC_R is Reference resistance values
4393 : * \param Beta is Beta constant
4394 : */
4395 0 : void encodeESC_TempSensorConfigPacket(void* _pg_pkt, const int16_t NTC_T[3], const float NTC_R[3], uint16_t Beta)
4396 : {
4397 0 : uint8_t* _pg_data = getESCVelocityPacketData(_pg_pkt);
4398 0 : int _pg_byteindex = 0;
4399 0 : unsigned _pg_i = 0;
4400 :
4401 : // Reference temperature values
4402 : // Range of NTC_T is -32768 to 32767.
4403 0 : for(_pg_i = 0; _pg_i < 3; _pg_i++)
4404 0 : int16ToBeBytes(NTC_T[_pg_i], _pg_data, &_pg_byteindex);
4405 :
4406 : // Reference resistance values
4407 : // Range of NTC_R is -3.402823466e+38f to 3.402823466e+38f.
4408 0 : for(_pg_i = 0; _pg_i < 3; _pg_i++)
4409 0 : float32ToBeBytes((float)NTC_R[_pg_i], _pg_data, &_pg_byteindex);
4410 :
4411 : // Beta constant
4412 : // Range of Beta is 0 to 65535.
4413 0 : uint16ToBeBytes(Beta, _pg_data, &_pg_byteindex);
4414 :
4415 : // complete the process of creating the packet
4416 0 : finishESCVelocityPacket(_pg_pkt, _pg_byteindex, getESC_TempSensorConfigPacketID());
4417 :
4418 0 : }// encodeESC_TempSensorConfigPacket
4419 :
4420 : /*!
4421 : * \brief Decode the ESC_TempSensorConfig packet
4422 : *
4423 : * Motor temperature sensor settings
4424 : * \param _pg_pkt points to the packet being decoded by this function
4425 : * \param NTC_T receives Reference temperature values
4426 : * \param NTC_R receives Reference resistance values
4427 : * \param Beta receives Beta constant
4428 : * \return 0 is returned if the packet ID or size is wrong, else 1
4429 : */
4430 0 : int decodeESC_TempSensorConfigPacket(const void* _pg_pkt, int16_t NTC_T[3], float NTC_R[3], uint16_t* Beta)
4431 : {
4432 0 : unsigned _pg_i = 0;
4433 0 : int _pg_byteindex = 0;
4434 0 : const uint8_t* _pg_data = getESCVelocityPacketDataConst(_pg_pkt);
4435 0 : int _pg_numbytes = getESCVelocityPacketSize(_pg_pkt);
4436 :
4437 : // Verify the packet identifier
4438 0 : if(getESCVelocityPacketID(_pg_pkt) != getESC_TempSensorConfigPacketID())
4439 0 : return 0;
4440 :
4441 0 : if(_pg_numbytes < getESC_TempSensorConfigMinDataLength())
4442 0 : return 0;
4443 :
4444 : // Reference temperature values
4445 : // Range of NTC_T is -32768 to 32767.
4446 0 : for(_pg_i = 0; _pg_i < 3; _pg_i++)
4447 0 : NTC_T[_pg_i] = int16FromBeBytes(_pg_data, &_pg_byteindex);
4448 :
4449 : // Reference resistance values
4450 : // Range of NTC_R is -3.402823466e+38f to 3.402823466e+38f.
4451 0 : for(_pg_i = 0; _pg_i < 3; _pg_i++)
4452 0 : NTC_R[_pg_i] = float32FromBeBytes(_pg_data, &_pg_byteindex);
4453 :
4454 : // Beta constant
4455 : // Range of Beta is 0 to 65535.
4456 0 : (*Beta) = uint16FromBeBytes(_pg_data, &_pg_byteindex);
4457 :
4458 0 : return 1;
4459 :
4460 : }// decodeESC_TempSensorConfigPacket
4461 :
4462 : /*!
4463 : * \brief Create the ESC_TelltaleValues packet
4464 : *
4465 : * ESC telltale values
4466 : * \param _pg_pkt points to the packet which will be created by this function
4467 : * \param _pg_user points to the user data that will be encoded in _pg_pkt
4468 : */
4469 0 : void encodeESC_TelltaleValuesPacketStructure(void* _pg_pkt, const ESC_TelltaleValues_t* _pg_user)
4470 : {
4471 0 : uint8_t* _pg_data = getESCVelocityPacketData(_pg_pkt);
4472 0 : int _pg_byteindex = 0;
4473 :
4474 : // Maximum recorded internal temperature
4475 : // Range of maxTemperature is 0 to 255.
4476 0 : uint8ToBytes(_pg_user->maxTemperature, _pg_data, &_pg_byteindex);
4477 :
4478 : // Maximum recorded MOSFET temperature
4479 : // Range of maxFetTemperature is 0 to 255.
4480 0 : uint8ToBytes(_pg_user->maxFetTemperature, _pg_data, &_pg_byteindex);
4481 :
4482 : // Maximum recorded motor temperature
4483 : // Range of maxMotorTemperature is 0 to 255.
4484 0 : uint8ToBytes(_pg_user->maxMotorTemperature, _pg_data, &_pg_byteindex);
4485 :
4486 : // Maximum recorded battery voltage
4487 : // Range of maxRippleVoltage is 0 to 255.
4488 0 : uint8ToBytes(_pg_user->maxRippleVoltage, _pg_data, &_pg_byteindex);
4489 :
4490 : // Maximum recorded battery current
4491 : // Range of maxBatteryCurrent is -3276.7f to 3276.7f.
4492 0 : float32ScaledTo2SignedBeBytes(_pg_user->maxBatteryCurrent, _pg_data, &_pg_byteindex, 10.0f);
4493 :
4494 : // Maximum recorded regen current
4495 : // Range of maxRegenCurrent is -3276.7f to 3276.7f.
4496 0 : float32ScaledTo2SignedBeBytes(_pg_user->maxRegenCurrent, _pg_data, &_pg_byteindex, 10.0f);
4497 :
4498 : // Number of successful motor start events
4499 : // Range of totalStarts is 0 to 65535.
4500 0 : uint16ToBeBytes(_pg_user->totalStarts, _pg_data, &_pg_byteindex);
4501 :
4502 : // Number of failed motor start events
4503 : // Range of failedStarts is 0 to 65535.
4504 0 : uint16ToBeBytes(_pg_user->failedStarts, _pg_data, &_pg_byteindex);
4505 :
4506 : // ESC run time
4507 : // Range of escRunTime is 0 to 4294967295.
4508 0 : uint32ToBeBytes(_pg_user->escRunTime, _pg_data, &_pg_byteindex);
4509 :
4510 : // Motor run time
4511 : // Range of motorRunTime is 0 to 4294967295.
4512 0 : uint32ToBeBytes(_pg_user->motorRunTime, _pg_data, &_pg_byteindex);
4513 :
4514 : // Number of recorded motor desync events
4515 : // Range of desyncEvents is 0 to 4294967295.
4516 0 : uint32ToBeBytes(_pg_user->desyncEvents, _pg_data, &_pg_byteindex);
4517 :
4518 : // complete the process of creating the packet
4519 0 : finishESCVelocityPacket(_pg_pkt, _pg_byteindex, getESC_TelltaleValuesPacketID());
4520 :
4521 0 : }// encodeESC_TelltaleValuesPacketStructure
4522 :
4523 : /*!
4524 : * \brief Decode the ESC_TelltaleValues packet
4525 : *
4526 : * ESC telltale values
4527 : * \param _pg_pkt points to the packet being decoded by this function
4528 : * \param _pg_user receives the data decoded from the packet
4529 : * \return 0 is returned if the packet ID or size is wrong, else 1
4530 : */
4531 0 : int decodeESC_TelltaleValuesPacketStructure(const void* _pg_pkt, ESC_TelltaleValues_t* _pg_user)
4532 : {
4533 : int _pg_numbytes;
4534 0 : int _pg_byteindex = 0;
4535 : const uint8_t* _pg_data;
4536 :
4537 : // Verify the packet identifier
4538 0 : if(getESCVelocityPacketID(_pg_pkt) != getESC_TelltaleValuesPacketID())
4539 0 : return 0;
4540 :
4541 : // Verify the packet size
4542 0 : _pg_numbytes = getESCVelocityPacketSize(_pg_pkt);
4543 0 : if(_pg_numbytes < getESC_TelltaleValuesMinDataLength())
4544 0 : return 0;
4545 :
4546 : // The raw data from the packet
4547 0 : _pg_data = getESCVelocityPacketDataConst(_pg_pkt);
4548 :
4549 : // this packet has default fields, make sure they are set
4550 0 : _pg_user->maxRippleVoltage = 0;
4551 0 : _pg_user->maxBatteryCurrent = 0;
4552 0 : _pg_user->maxRegenCurrent = 0;
4553 0 : _pg_user->totalStarts = 0;
4554 0 : _pg_user->failedStarts = 0;
4555 0 : _pg_user->escRunTime = 0;
4556 0 : _pg_user->motorRunTime = 0;
4557 0 : _pg_user->desyncEvents = 0;
4558 :
4559 : // Maximum recorded internal temperature
4560 : // Range of maxTemperature is 0 to 255.
4561 0 : _pg_user->maxTemperature = uint8FromBytes(_pg_data, &_pg_byteindex);
4562 :
4563 : // Maximum recorded MOSFET temperature
4564 : // Range of maxFetTemperature is 0 to 255.
4565 0 : _pg_user->maxFetTemperature = uint8FromBytes(_pg_data, &_pg_byteindex);
4566 :
4567 : // Maximum recorded motor temperature
4568 : // Range of maxMotorTemperature is 0 to 255.
4569 0 : _pg_user->maxMotorTemperature = uint8FromBytes(_pg_data, &_pg_byteindex);
4570 :
4571 0 : if(_pg_byteindex + 1 > _pg_numbytes)
4572 0 : return 1;
4573 :
4574 : // Maximum recorded battery voltage
4575 : // Range of maxRippleVoltage is 0 to 255.
4576 0 : _pg_user->maxRippleVoltage = uint8FromBytes(_pg_data, &_pg_byteindex);
4577 :
4578 0 : if(_pg_byteindex + 2 > _pg_numbytes)
4579 0 : return 1;
4580 :
4581 : // Maximum recorded battery current
4582 : // Range of maxBatteryCurrent is -3276.7f to 3276.7f.
4583 0 : _pg_user->maxBatteryCurrent = float32ScaledFrom2SignedBeBytes(_pg_data, &_pg_byteindex, 1.0f/10.0f);
4584 :
4585 0 : if(_pg_byteindex + 2 > _pg_numbytes)
4586 0 : return 1;
4587 :
4588 : // Maximum recorded regen current
4589 : // Range of maxRegenCurrent is -3276.7f to 3276.7f.
4590 0 : _pg_user->maxRegenCurrent = float32ScaledFrom2SignedBeBytes(_pg_data, &_pg_byteindex, 1.0f/10.0f);
4591 :
4592 0 : if(_pg_byteindex + 2 > _pg_numbytes)
4593 0 : return 1;
4594 :
4595 : // Number of successful motor start events
4596 : // Range of totalStarts is 0 to 65535.
4597 0 : _pg_user->totalStarts = uint16FromBeBytes(_pg_data, &_pg_byteindex);
4598 :
4599 0 : if(_pg_byteindex + 2 > _pg_numbytes)
4600 0 : return 1;
4601 :
4602 : // Number of failed motor start events
4603 : // Range of failedStarts is 0 to 65535.
4604 0 : _pg_user->failedStarts = uint16FromBeBytes(_pg_data, &_pg_byteindex);
4605 :
4606 0 : if(_pg_byteindex + 4 > _pg_numbytes)
4607 0 : return 1;
4608 :
4609 : // ESC run time
4610 : // Range of escRunTime is 0 to 4294967295.
4611 0 : _pg_user->escRunTime = uint32FromBeBytes(_pg_data, &_pg_byteindex);
4612 :
4613 0 : if(_pg_byteindex + 4 > _pg_numbytes)
4614 0 : return 1;
4615 :
4616 : // Motor run time
4617 : // Range of motorRunTime is 0 to 4294967295.
4618 0 : _pg_user->motorRunTime = uint32FromBeBytes(_pg_data, &_pg_byteindex);
4619 :
4620 0 : if(_pg_byteindex + 4 > _pg_numbytes)
4621 0 : return 1;
4622 :
4623 : // Number of recorded motor desync events
4624 : // Range of desyncEvents is 0 to 4294967295.
4625 0 : _pg_user->desyncEvents = uint32FromBeBytes(_pg_data, &_pg_byteindex);
4626 :
4627 0 : return 1;
4628 :
4629 : }// decodeESC_TelltaleValuesPacketStructure
4630 :
4631 : /*!
4632 : * \brief Create the ESC_TelltaleValues packet
4633 : *
4634 : * ESC telltale values
4635 : * \param _pg_pkt points to the packet which will be created by this function
4636 : * \param maxTemperature is Maximum recorded internal temperature
4637 : * \param maxFetTemperature is Maximum recorded MOSFET temperature
4638 : * \param maxMotorTemperature is Maximum recorded motor temperature
4639 : * \param maxRippleVoltage is Maximum recorded battery voltage
4640 : * \param maxBatteryCurrent is Maximum recorded battery current
4641 : * \param maxRegenCurrent is Maximum recorded regen current
4642 : * \param totalStarts is Number of successful motor start events
4643 : * \param failedStarts is Number of failed motor start events
4644 : * \param escRunTime is ESC run time
4645 : * \param motorRunTime is Motor run time
4646 : * \param desyncEvents is Number of recorded motor desync events
4647 : */
4648 0 : void encodeESC_TelltaleValuesPacket(void* _pg_pkt, uint8_t maxTemperature, uint8_t maxFetTemperature, uint8_t maxMotorTemperature, uint8_t maxRippleVoltage, float maxBatteryCurrent, float maxRegenCurrent, uint16_t totalStarts, uint16_t failedStarts, uint32_t escRunTime, uint32_t motorRunTime, uint32_t desyncEvents)
4649 : {
4650 0 : uint8_t* _pg_data = getESCVelocityPacketData(_pg_pkt);
4651 0 : int _pg_byteindex = 0;
4652 :
4653 : // Maximum recorded internal temperature
4654 : // Range of maxTemperature is 0 to 255.
4655 0 : uint8ToBytes(maxTemperature, _pg_data, &_pg_byteindex);
4656 :
4657 : // Maximum recorded MOSFET temperature
4658 : // Range of maxFetTemperature is 0 to 255.
4659 0 : uint8ToBytes(maxFetTemperature, _pg_data, &_pg_byteindex);
4660 :
4661 : // Maximum recorded motor temperature
4662 : // Range of maxMotorTemperature is 0 to 255.
4663 0 : uint8ToBytes(maxMotorTemperature, _pg_data, &_pg_byteindex);
4664 :
4665 : // Maximum recorded battery voltage
4666 : // Range of maxRippleVoltage is 0 to 255.
4667 0 : uint8ToBytes(maxRippleVoltage, _pg_data, &_pg_byteindex);
4668 :
4669 : // Maximum recorded battery current
4670 : // Range of maxBatteryCurrent is -3276.7f to 3276.7f.
4671 0 : float32ScaledTo2SignedBeBytes(maxBatteryCurrent, _pg_data, &_pg_byteindex, 10.0f);
4672 :
4673 : // Maximum recorded regen current
4674 : // Range of maxRegenCurrent is -3276.7f to 3276.7f.
4675 0 : float32ScaledTo2SignedBeBytes(maxRegenCurrent, _pg_data, &_pg_byteindex, 10.0f);
4676 :
4677 : // Number of successful motor start events
4678 : // Range of totalStarts is 0 to 65535.
4679 0 : uint16ToBeBytes(totalStarts, _pg_data, &_pg_byteindex);
4680 :
4681 : // Number of failed motor start events
4682 : // Range of failedStarts is 0 to 65535.
4683 0 : uint16ToBeBytes(failedStarts, _pg_data, &_pg_byteindex);
4684 :
4685 : // ESC run time
4686 : // Range of escRunTime is 0 to 4294967295.
4687 0 : uint32ToBeBytes(escRunTime, _pg_data, &_pg_byteindex);
4688 :
4689 : // Motor run time
4690 : // Range of motorRunTime is 0 to 4294967295.
4691 0 : uint32ToBeBytes(motorRunTime, _pg_data, &_pg_byteindex);
4692 :
4693 : // Number of recorded motor desync events
4694 : // Range of desyncEvents is 0 to 4294967295.
4695 0 : uint32ToBeBytes(desyncEvents, _pg_data, &_pg_byteindex);
4696 :
4697 : // complete the process of creating the packet
4698 0 : finishESCVelocityPacket(_pg_pkt, _pg_byteindex, getESC_TelltaleValuesPacketID());
4699 :
4700 0 : }// encodeESC_TelltaleValuesPacket
4701 :
4702 : /*!
4703 : * \brief Decode the ESC_TelltaleValues packet
4704 : *
4705 : * ESC telltale values
4706 : * \param _pg_pkt points to the packet being decoded by this function
4707 : * \param maxTemperature receives Maximum recorded internal temperature
4708 : * \param maxFetTemperature receives Maximum recorded MOSFET temperature
4709 : * \param maxMotorTemperature receives Maximum recorded motor temperature
4710 : * \param maxRippleVoltage receives Maximum recorded battery voltage
4711 : * \param maxBatteryCurrent receives Maximum recorded battery current
4712 : * \param maxRegenCurrent receives Maximum recorded regen current
4713 : * \param totalStarts receives Number of successful motor start events
4714 : * \param failedStarts receives Number of failed motor start events
4715 : * \param escRunTime receives ESC run time
4716 : * \param motorRunTime receives Motor run time
4717 : * \param desyncEvents receives Number of recorded motor desync events
4718 : * \return 0 is returned if the packet ID or size is wrong, else 1
4719 : */
4720 0 : int decodeESC_TelltaleValuesPacket(const void* _pg_pkt, uint8_t* maxTemperature, uint8_t* maxFetTemperature, uint8_t* maxMotorTemperature, uint8_t* maxRippleVoltage, float* maxBatteryCurrent, float* maxRegenCurrent, uint16_t* totalStarts, uint16_t* failedStarts, uint32_t* escRunTime, uint32_t* motorRunTime, uint32_t* desyncEvents)
4721 : {
4722 0 : int _pg_byteindex = 0;
4723 0 : const uint8_t* _pg_data = getESCVelocityPacketDataConst(_pg_pkt);
4724 0 : int _pg_numbytes = getESCVelocityPacketSize(_pg_pkt);
4725 :
4726 : // Verify the packet identifier
4727 0 : if(getESCVelocityPacketID(_pg_pkt) != getESC_TelltaleValuesPacketID())
4728 0 : return 0;
4729 :
4730 0 : if(_pg_numbytes < getESC_TelltaleValuesMinDataLength())
4731 0 : return 0;
4732 :
4733 : // this packet has default fields, make sure they are set
4734 0 : (*maxRippleVoltage) = 0;
4735 0 : (*maxBatteryCurrent) = 0;
4736 0 : (*maxRegenCurrent) = 0;
4737 0 : (*totalStarts) = 0;
4738 0 : (*failedStarts) = 0;
4739 0 : (*escRunTime) = 0;
4740 0 : (*motorRunTime) = 0;
4741 0 : (*desyncEvents) = 0;
4742 :
4743 : // Maximum recorded internal temperature
4744 : // Range of maxTemperature is 0 to 255.
4745 0 : (*maxTemperature) = uint8FromBytes(_pg_data, &_pg_byteindex);
4746 :
4747 : // Maximum recorded MOSFET temperature
4748 : // Range of maxFetTemperature is 0 to 255.
4749 0 : (*maxFetTemperature) = uint8FromBytes(_pg_data, &_pg_byteindex);
4750 :
4751 : // Maximum recorded motor temperature
4752 : // Range of maxMotorTemperature is 0 to 255.
4753 0 : (*maxMotorTemperature) = uint8FromBytes(_pg_data, &_pg_byteindex);
4754 :
4755 0 : if(_pg_byteindex + 1 > _pg_numbytes)
4756 0 : return 1;
4757 :
4758 : // Maximum recorded battery voltage
4759 : // Range of maxRippleVoltage is 0 to 255.
4760 0 : (*maxRippleVoltage) = uint8FromBytes(_pg_data, &_pg_byteindex);
4761 :
4762 0 : if(_pg_byteindex + 2 > _pg_numbytes)
4763 0 : return 1;
4764 :
4765 : // Maximum recorded battery current
4766 : // Range of maxBatteryCurrent is -3276.7f to 3276.7f.
4767 0 : (*maxBatteryCurrent) = float32ScaledFrom2SignedBeBytes(_pg_data, &_pg_byteindex, 1.0f/10.0f);
4768 :
4769 0 : if(_pg_byteindex + 2 > _pg_numbytes)
4770 0 : return 1;
4771 :
4772 : // Maximum recorded regen current
4773 : // Range of maxRegenCurrent is -3276.7f to 3276.7f.
4774 0 : (*maxRegenCurrent) = float32ScaledFrom2SignedBeBytes(_pg_data, &_pg_byteindex, 1.0f/10.0f);
4775 :
4776 0 : if(_pg_byteindex + 2 > _pg_numbytes)
4777 0 : return 1;
4778 :
4779 : // Number of successful motor start events
4780 : // Range of totalStarts is 0 to 65535.
4781 0 : (*totalStarts) = uint16FromBeBytes(_pg_data, &_pg_byteindex);
4782 :
4783 0 : if(_pg_byteindex + 2 > _pg_numbytes)
4784 0 : return 1;
4785 :
4786 : // Number of failed motor start events
4787 : // Range of failedStarts is 0 to 65535.
4788 0 : (*failedStarts) = uint16FromBeBytes(_pg_data, &_pg_byteindex);
4789 :
4790 0 : if(_pg_byteindex + 4 > _pg_numbytes)
4791 0 : return 1;
4792 :
4793 : // ESC run time
4794 : // Range of escRunTime is 0 to 4294967295.
4795 0 : (*escRunTime) = uint32FromBeBytes(_pg_data, &_pg_byteindex);
4796 :
4797 0 : if(_pg_byteindex + 4 > _pg_numbytes)
4798 0 : return 1;
4799 :
4800 : // Motor run time
4801 : // Range of motorRunTime is 0 to 4294967295.
4802 0 : (*motorRunTime) = uint32FromBeBytes(_pg_data, &_pg_byteindex);
4803 :
4804 0 : if(_pg_byteindex + 4 > _pg_numbytes)
4805 0 : return 1;
4806 :
4807 : // Number of recorded motor desync events
4808 : // Range of desyncEvents is 0 to 4294967295.
4809 0 : (*desyncEvents) = uint32FromBeBytes(_pg_data, &_pg_byteindex);
4810 :
4811 0 : return 1;
4812 :
4813 : }// decodeESC_TelltaleValuesPacket
4814 :
4815 : /*!
4816 : * \brief Create the ESC_GitHash packet
4817 : *
4818 : * Git commit hash for the ESC firmware
4819 : * \param _pg_pkt points to the packet which will be created by this function
4820 : * \param _pg_user points to the user data that will be encoded in _pg_pkt
4821 : */
4822 0 : void encodeESC_GitHashPacketStructure(void* _pg_pkt, const ESC_GitHash_t* _pg_user)
4823 : {
4824 0 : uint8_t* _pg_data = getESCVelocityPacketData(_pg_pkt);
4825 0 : int _pg_byteindex = 0;
4826 :
4827 : // git commit hash
4828 0 : stringToBytes(_pg_user->hash, _pg_data, &_pg_byteindex, 8, 0);
4829 :
4830 : // complete the process of creating the packet
4831 0 : finishESCVelocityPacket(_pg_pkt, _pg_byteindex, getESC_GitHashPacketID());
4832 :
4833 0 : }// encodeESC_GitHashPacketStructure
4834 :
4835 : /*!
4836 : * \brief Decode the ESC_GitHash packet
4837 : *
4838 : * Git commit hash for the ESC firmware
4839 : * \param _pg_pkt points to the packet being decoded by this function
4840 : * \param _pg_user receives the data decoded from the packet
4841 : * \return 0 is returned if the packet ID or size is wrong, else 1
4842 : */
4843 0 : int decodeESC_GitHashPacketStructure(const void* _pg_pkt, ESC_GitHash_t* _pg_user)
4844 : {
4845 : int _pg_numbytes;
4846 0 : int _pg_byteindex = 0;
4847 : const uint8_t* _pg_data;
4848 :
4849 : // Verify the packet identifier
4850 0 : if(getESCVelocityPacketID(_pg_pkt) != getESC_GitHashPacketID())
4851 0 : return 0;
4852 :
4853 : // Verify the packet size
4854 0 : _pg_numbytes = getESCVelocityPacketSize(_pg_pkt);
4855 0 : if(_pg_numbytes < getESC_GitHashMinDataLength())
4856 0 : return 0;
4857 :
4858 : // The raw data from the packet
4859 0 : _pg_data = getESCVelocityPacketDataConst(_pg_pkt);
4860 :
4861 : // git commit hash
4862 0 : stringFromBytes(_pg_user->hash, _pg_data, &_pg_byteindex, 8, 0);
4863 :
4864 0 : return 1;
4865 :
4866 : }// decodeESC_GitHashPacketStructure
4867 :
4868 : /*!
4869 : * \brief Create the ESC_GitHash packet
4870 : *
4871 : * Git commit hash for the ESC firmware
4872 : * \param _pg_pkt points to the packet which will be created by this function
4873 : * \param hash is git commit hash
4874 : */
4875 0 : void encodeESC_GitHashPacket(void* _pg_pkt, const char hash[8])
4876 : {
4877 0 : uint8_t* _pg_data = getESCVelocityPacketData(_pg_pkt);
4878 0 : int _pg_byteindex = 0;
4879 :
4880 : // git commit hash
4881 0 : stringToBytes(hash, _pg_data, &_pg_byteindex, 8, 0);
4882 :
4883 : // complete the process of creating the packet
4884 0 : finishESCVelocityPacket(_pg_pkt, _pg_byteindex, getESC_GitHashPacketID());
4885 :
4886 0 : }// encodeESC_GitHashPacket
4887 :
4888 : /*!
4889 : * \brief Decode the ESC_GitHash packet
4890 : *
4891 : * Git commit hash for the ESC firmware
4892 : * \param _pg_pkt points to the packet being decoded by this function
4893 : * \param hash receives git commit hash
4894 : * \return 0 is returned if the packet ID or size is wrong, else 1
4895 : */
4896 0 : int decodeESC_GitHashPacket(const void* _pg_pkt, char hash[8])
4897 : {
4898 0 : int _pg_byteindex = 0;
4899 0 : const uint8_t* _pg_data = getESCVelocityPacketDataConst(_pg_pkt);
4900 0 : int _pg_numbytes = getESCVelocityPacketSize(_pg_pkt);
4901 :
4902 : // Verify the packet identifier
4903 0 : if(getESCVelocityPacketID(_pg_pkt) != getESC_GitHashPacketID())
4904 0 : return 0;
4905 :
4906 0 : if(_pg_numbytes < getESC_GitHashMinDataLength())
4907 0 : return 0;
4908 :
4909 : // git commit hash
4910 0 : stringFromBytes(hash, _pg_data, &_pg_byteindex, 8, 0);
4911 :
4912 0 : return 1;
4913 :
4914 : }// decodeESC_GitHashPacket
4915 :
4916 : /*!
4917 : * \brief Create the ESC_PWMInputCalibration packet
4918 : *
4919 : * PWM input calibration
4920 : * \param _pg_pkt points to the packet which will be created by this function
4921 : * \param _pg_user points to the user data that will be encoded in _pg_pkt
4922 : */
4923 0 : void encodeESC_PWMInputCalibrationPacketStructure(void* _pg_pkt, const ESC_PWMInputCalibration_t* _pg_user)
4924 : {
4925 0 : uint8_t* _pg_data = getESCVelocityPacketData(_pg_pkt);
4926 0 : int _pg_byteindex = 0;
4927 :
4928 : // Reserved bits for future use
4929 0 : _pg_data[_pg_byteindex] = 0;
4930 :
4931 : // Enable median glitch filter for PWM input
4932 0 : _pg_data[_pg_byteindex] |= (uint8_t)((_pg_user->glitchFilter == true) ? 1 : 0) << 4;
4933 :
4934 : // Protocol version (reserved for future use)
4935 0 : _pg_byteindex += 1; // close bit field
4936 :
4937 :
4938 : // PWM offset compensation value
4939 : // Range of pwmOffset is -128 to 127.
4940 0 : int8ToBytes(_pg_user->pwmOffset, _pg_data, &_pg_byteindex);
4941 :
4942 : // PWM value corresponding with 0% throttle
4943 : // Range of inputMin is 0 to 65535.
4944 0 : uint16ToBeBytes(_pg_user->inputMin, _pg_data, &_pg_byteindex);
4945 :
4946 : // PWM value corresponding with 1000% throttle
4947 : // Range of inputMax is 0 to 65535.
4948 0 : uint16ToBeBytes(_pg_user->inputMax, _pg_data, &_pg_byteindex);
4949 :
4950 : // PWM arming threshold
4951 : // Range of armThreshold is 0 to 65535.
4952 0 : uint16ToBeBytes(_pg_user->armThreshold, _pg_data, &_pg_byteindex);
4953 :
4954 : // complete the process of creating the packet
4955 0 : finishESCVelocityPacket(_pg_pkt, _pg_byteindex, getESC_PWMInputCalibrationPacketID());
4956 :
4957 0 : }// encodeESC_PWMInputCalibrationPacketStructure
4958 :
4959 : /*!
4960 : * \brief Decode the ESC_PWMInputCalibration packet
4961 : *
4962 : * PWM input calibration
4963 : * \param _pg_pkt points to the packet being decoded by this function
4964 : * \param _pg_user receives the data decoded from the packet
4965 : * \return 0 is returned if the packet ID or size is wrong, else 1
4966 : */
4967 0 : int decodeESC_PWMInputCalibrationPacketStructure(const void* _pg_pkt, ESC_PWMInputCalibration_t* _pg_user)
4968 : {
4969 : int _pg_numbytes;
4970 0 : int _pg_byteindex = 0;
4971 : const uint8_t* _pg_data;
4972 :
4973 : // Verify the packet identifier
4974 0 : if(getESCVelocityPacketID(_pg_pkt) != getESC_PWMInputCalibrationPacketID())
4975 0 : return 0;
4976 :
4977 : // Verify the packet size
4978 0 : _pg_numbytes = getESCVelocityPacketSize(_pg_pkt);
4979 0 : if(_pg_numbytes < getESC_PWMInputCalibrationMinDataLength())
4980 0 : return 0;
4981 :
4982 : // The raw data from the packet
4983 0 : _pg_data = getESCVelocityPacketDataConst(_pg_pkt);
4984 :
4985 : // Reserved bits for future use
4986 :
4987 : // Enable median glitch filter for PWM input
4988 0 : _pg_user->glitchFilter = (((_pg_data[_pg_byteindex] >> 4) & 0x1)) ? true : false;
4989 :
4990 : // Protocol version (reserved for future use)
4991 : // Range of protocol is 0 to 15.
4992 0 : _pg_user->protocol = ((_pg_data[_pg_byteindex]) & 0xF);
4993 0 : _pg_byteindex += 1; // close bit field
4994 :
4995 : // PWM offset compensation value
4996 : // Range of pwmOffset is -128 to 127.
4997 0 : _pg_user->pwmOffset = int8FromBytes(_pg_data, &_pg_byteindex);
4998 :
4999 : // PWM value corresponding with 0% throttle
5000 : // Range of inputMin is 0 to 65535.
5001 0 : _pg_user->inputMin = uint16FromBeBytes(_pg_data, &_pg_byteindex);
5002 :
5003 : // PWM value corresponding with 1000% throttle
5004 : // Range of inputMax is 0 to 65535.
5005 0 : _pg_user->inputMax = uint16FromBeBytes(_pg_data, &_pg_byteindex);
5006 :
5007 : // PWM arming threshold
5008 : // Range of armThreshold is 0 to 65535.
5009 0 : _pg_user->armThreshold = uint16FromBeBytes(_pg_data, &_pg_byteindex);
5010 :
5011 0 : return 1;
5012 :
5013 : }// decodeESC_PWMInputCalibrationPacketStructure
5014 :
5015 : /*!
5016 : * \brief Create the ESC_PWMInputCalibration packet
5017 : *
5018 : * PWM input calibration
5019 : * \param _pg_pkt points to the packet which will be created by this function
5020 : * \param glitchFilter is Enable median glitch filter for PWM input
5021 : * \param pwmOffset is PWM offset compensation value
5022 : * \param inputMin is PWM value corresponding with 0% throttle
5023 : * \param inputMax is PWM value corresponding with 1000% throttle
5024 : * \param armThreshold is PWM arming threshold
5025 : */
5026 0 : void encodeESC_PWMInputCalibrationPacket(void* _pg_pkt, bool glitchFilter, int8_t pwmOffset, uint16_t inputMin, uint16_t inputMax, uint16_t armThreshold)
5027 : {
5028 0 : uint8_t* _pg_data = getESCVelocityPacketData(_pg_pkt);
5029 0 : int _pg_byteindex = 0;
5030 :
5031 : // Reserved bits for future use
5032 0 : _pg_data[_pg_byteindex] = 0;
5033 :
5034 : // Enable median glitch filter for PWM input
5035 0 : _pg_data[_pg_byteindex] |= (uint8_t)((glitchFilter == true) ? 1 : 0) << 4;
5036 :
5037 : // Protocol version (reserved for future use)
5038 0 : _pg_byteindex += 1; // close bit field
5039 :
5040 : // PWM offset compensation value
5041 : // Range of pwmOffset is -128 to 127.
5042 0 : int8ToBytes(pwmOffset, _pg_data, &_pg_byteindex);
5043 :
5044 : // PWM value corresponding with 0% throttle
5045 : // Range of inputMin is 0 to 65535.
5046 0 : uint16ToBeBytes(inputMin, _pg_data, &_pg_byteindex);
5047 :
5048 : // PWM value corresponding with 1000% throttle
5049 : // Range of inputMax is 0 to 65535.
5050 0 : uint16ToBeBytes(inputMax, _pg_data, &_pg_byteindex);
5051 :
5052 : // PWM arming threshold
5053 : // Range of armThreshold is 0 to 65535.
5054 0 : uint16ToBeBytes(armThreshold, _pg_data, &_pg_byteindex);
5055 :
5056 : // complete the process of creating the packet
5057 0 : finishESCVelocityPacket(_pg_pkt, _pg_byteindex, getESC_PWMInputCalibrationPacketID());
5058 :
5059 0 : }// encodeESC_PWMInputCalibrationPacket
5060 :
5061 : /*!
5062 : * \brief Decode the ESC_PWMInputCalibration packet
5063 : *
5064 : * PWM input calibration
5065 : * \param _pg_pkt points to the packet being decoded by this function
5066 : * \param glitchFilter receives Enable median glitch filter for PWM input
5067 : * \param protocol receives Protocol version (reserved for future use)
5068 : * \param pwmOffset receives PWM offset compensation value
5069 : * \param inputMin receives PWM value corresponding with 0% throttle
5070 : * \param inputMax receives PWM value corresponding with 1000% throttle
5071 : * \param armThreshold receives PWM arming threshold
5072 : * \return 0 is returned if the packet ID or size is wrong, else 1
5073 : */
5074 0 : int decodeESC_PWMInputCalibrationPacket(const void* _pg_pkt, bool* glitchFilter, uint8_t* protocol, int8_t* pwmOffset, uint16_t* inputMin, uint16_t* inputMax, uint16_t* armThreshold)
5075 : {
5076 0 : int _pg_byteindex = 0;
5077 0 : const uint8_t* _pg_data = getESCVelocityPacketDataConst(_pg_pkt);
5078 0 : int _pg_numbytes = getESCVelocityPacketSize(_pg_pkt);
5079 :
5080 : // Verify the packet identifier
5081 0 : if(getESCVelocityPacketID(_pg_pkt) != getESC_PWMInputCalibrationPacketID())
5082 0 : return 0;
5083 :
5084 0 : if(_pg_numbytes < getESC_PWMInputCalibrationMinDataLength())
5085 0 : return 0;
5086 :
5087 : // Reserved bits for future use
5088 :
5089 : // Enable median glitch filter for PWM input
5090 0 : (*glitchFilter) = (((_pg_data[_pg_byteindex] >> 4) & 0x1)) ? true : false;
5091 :
5092 : // Protocol version (reserved for future use)
5093 : // Range of protocol is 0 to 15.
5094 0 : (*protocol) = ((_pg_data[_pg_byteindex]) & 0xF);
5095 0 : _pg_byteindex += 1; // close bit field
5096 :
5097 : // PWM offset compensation value
5098 : // Range of pwmOffset is -128 to 127.
5099 0 : (*pwmOffset) = int8FromBytes(_pg_data, &_pg_byteindex);
5100 :
5101 : // PWM value corresponding with 0% throttle
5102 : // Range of inputMin is 0 to 65535.
5103 0 : (*inputMin) = uint16FromBeBytes(_pg_data, &_pg_byteindex);
5104 :
5105 : // PWM value corresponding with 1000% throttle
5106 : // Range of inputMax is 0 to 65535.
5107 0 : (*inputMax) = uint16FromBeBytes(_pg_data, &_pg_byteindex);
5108 :
5109 : // PWM arming threshold
5110 : // Range of armThreshold is 0 to 65535.
5111 0 : (*armThreshold) = uint16FromBeBytes(_pg_data, &_pg_byteindex);
5112 :
5113 0 : return 1;
5114 :
5115 : }// decodeESC_PWMInputCalibrationPacket
5116 : // end of ESCPackets.c
|