Line data Source code
1 : // ESCDefines.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 "ESCDefines.h"
21 : #include "fielddecode.h"
22 : #include "fieldencode.h"
23 : #include "scaleddecode.h"
24 : #include "scaledencode.h"
25 :
26 : /*!
27 : * \brief Encode a ESC_StatusBits_t into a byte array
28 : *
29 : * The *status* of the ESC is represented using these status bits. ESC system
30 : * functionality can be quickly determined using these bits
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_StatusBits_t(uint8_t* _pg_data, int* _pg_bytecount, const ESC_StatusBits_t* _pg_user)
36 : {
37 0 : int _pg_byteindex = *_pg_bytecount;
38 :
39 : // Set if hardware inhibit is active (ESC is disabled)
40 0 : _pg_data[_pg_byteindex] = (uint8_t)((_pg_user->hwInhibit == true) ? 1 : 0) << 7;
41 :
42 : // Set if software inhibit is active (ESC is disabled)
43 0 : _pg_data[_pg_byteindex] |= (uint8_t)((_pg_user->swInhibit == true) ? 1 : 0) << 6;
44 :
45 : // Set if Active Freewheeling is currently active
46 0 : _pg_data[_pg_byteindex] |= (uint8_t)((_pg_user->afwEnabled == true) ? 1 : 0) << 5;
47 :
48 : // 0 = Motor direction is FORWARDS, 1= Motor direction is REVERSE
49 0 : _pg_data[_pg_byteindex] |= (uint8_t)limitMax(_pg_user->direction, 1) << 4;
50 :
51 : // Set if the ESC command timeout period has elapsed (and the ESC is in STANDBY mode)
52 0 : _pg_data[_pg_byteindex] |= (uint8_t)((_pg_user->timeout == true) ? 1 : 0) << 3;
53 :
54 : // Set if ESC is in starting mode (Cleared if ESC is stopped or running)
55 0 : _pg_data[_pg_byteindex] |= (uint8_t)((_pg_user->starting == true) ? 1 : 0) << 2;
56 :
57 : // 0 = most recent command from CAN, 1 = most recent command from PWM
58 0 : _pg_data[_pg_byteindex] |= (uint8_t)limitMax(_pg_user->commandSource, 1) << 1;
59 :
60 : // Set if ESC is running
61 0 : _pg_data[_pg_byteindex] |= (uint8_t)((_pg_user->running == true) ? 1 : 0);
62 :
63 : // Warning active - refer to the PKT_ESC_WARNINGS_ERRORS packet
64 0 : _pg_data[_pg_byteindex + 1] = (uint8_t)((_pg_user->anyWarnings == true) ? 1 : 0) << 7;
65 :
66 : // Error active - refer to the PKT_ESC_WARNINGS_ERRORS packet
67 0 : _pg_data[_pg_byteindex + 1] |= (uint8_t)((_pg_user->anyErrors == true) ? 1 : 0) << 6;
68 :
69 : // Set if ESC is armed to respond to CAN commands
70 0 : _pg_data[_pg_byteindex + 1] |= (uint8_t)((_pg_user->canArmed == true) ? 1 : 0) << 5;
71 :
72 : // Set if ESC is armed to respond to PWM Input commands
73 0 : _pg_data[_pg_byteindex + 1] |= (uint8_t)((_pg_user->pwmArmed == true) ? 1 : 0) << 4;
74 :
75 : // Reserved bits for future use
76 :
77 : // Set if the motor is spinning (even if it is not being driven)
78 0 : _pg_data[_pg_byteindex + 1] |= (uint8_t)((_pg_user->spinning == true) ? 1 : 0);
79 :
80 : // Set if motor is spinning opposite to configured rotation direction
81 0 : _pg_data[_pg_byteindex + 2] = (uint8_t)((_pg_user->spinningReversed == true) ? 1 : 0) << 7;
82 :
83 : // Set if motor duty cycle is being actively reduced due to ESC protection settings
84 0 : _pg_data[_pg_byteindex + 2] |= (uint8_t)((_pg_user->foldback == true) ? 1 : 0) << 6;
85 :
86 : // Set if the ESC is attempting to sync with the motor
87 0 : _pg_data[_pg_byteindex + 2] |= (uint8_t)((_pg_user->syncing == true) ? 1 : 0) << 5;
88 :
89 : // Set if the ESC is applying motor braking
90 0 : _pg_data[_pg_byteindex + 2] |= (uint8_t)((_pg_user->braking == true) ? 1 : 0) << 4;
91 :
92 : // Set if the motor duty cycle is being actively increased due to ESC protection settings
93 0 : _pg_data[_pg_byteindex + 2] |= (uint8_t)((_pg_user->foldforward == true) ? 1 : 0) << 3;
94 :
95 : // Reserved bits for future use
96 :
97 : // Set if the ESC settings are locked and cannot be changed
98 0 : _pg_data[_pg_byteindex + 2] |= (uint8_t)((_pg_user->settingsLocked == true) ? 1 : 0) << 1;
99 :
100 : // Set if the ESC is in debug mode (factory use only)
101 0 : _pg_data[_pg_byteindex + 2] |= (uint8_t)((_pg_user->debug == true) ? 1 : 0);
102 0 : _pg_byteindex += 3; // close bit field
103 :
104 0 : *_pg_bytecount = _pg_byteindex;
105 :
106 0 : }// encodeESC_StatusBits_t
107 :
108 : /*!
109 : * \brief Decode a ESC_StatusBits_t from a byte array
110 : *
111 : * The *status* of the ESC is represented using these status bits. ESC system
112 : * functionality can be quickly determined using these bits
113 : * \param _pg_data points to the byte array to decoded data from
114 : * \param _pg_bytecount points to the starting location in the byte array, and will be incremented by the number of bytes decoded
115 : * \param _pg_user is the data to decode from the byte array
116 : * \return 1 if the data are decoded, else 0.
117 : */
118 0 : int decodeESC_StatusBits_t(const uint8_t* _pg_data, int* _pg_bytecount, ESC_StatusBits_t* _pg_user)
119 : {
120 0 : int _pg_byteindex = *_pg_bytecount;
121 :
122 : // Set if hardware inhibit is active (ESC is disabled)
123 0 : _pg_user->hwInhibit = ((_pg_data[_pg_byteindex] >> 7)) ? true : false;
124 :
125 : // Set if software inhibit is active (ESC is disabled)
126 0 : _pg_user->swInhibit = (((_pg_data[_pg_byteindex] >> 6) & 0x1)) ? true : false;
127 :
128 : // Set if Active Freewheeling is currently active
129 0 : _pg_user->afwEnabled = (((_pg_data[_pg_byteindex] >> 5) & 0x1)) ? true : false;
130 :
131 : // 0 = Motor direction is FORWARDS, 1= Motor direction is REVERSE
132 0 : _pg_user->direction = ((_pg_data[_pg_byteindex] >> 4) & 0x1);
133 :
134 : // Set if the ESC command timeout period has elapsed (and the ESC is in STANDBY mode)
135 0 : _pg_user->timeout = (((_pg_data[_pg_byteindex] >> 3) & 0x1)) ? true : false;
136 :
137 : // Set if ESC is in starting mode (Cleared if ESC is stopped or running)
138 0 : _pg_user->starting = (((_pg_data[_pg_byteindex] >> 2) & 0x1)) ? true : false;
139 :
140 : // 0 = most recent command from CAN, 1 = most recent command from PWM
141 0 : _pg_user->commandSource = ((_pg_data[_pg_byteindex] >> 1) & 0x1);
142 :
143 : // Set if ESC is running
144 0 : _pg_user->running = (((_pg_data[_pg_byteindex]) & 0x1)) ? true : false;
145 :
146 : // Warning active - refer to the PKT_ESC_WARNINGS_ERRORS packet
147 0 : _pg_user->anyWarnings = ((_pg_data[_pg_byteindex + 1] >> 7)) ? true : false;
148 :
149 : // Error active - refer to the PKT_ESC_WARNINGS_ERRORS packet
150 0 : _pg_user->anyErrors = (((_pg_data[_pg_byteindex + 1] >> 6) & 0x1)) ? true : false;
151 :
152 : // Set if ESC is armed to respond to CAN commands
153 0 : _pg_user->canArmed = (((_pg_data[_pg_byteindex + 1] >> 5) & 0x1)) ? true : false;
154 :
155 : // Set if ESC is armed to respond to PWM Input commands
156 0 : _pg_user->pwmArmed = (((_pg_data[_pg_byteindex + 1] >> 4) & 0x1)) ? true : false;
157 :
158 : // Reserved bits for future use
159 :
160 : // Set if the motor is spinning (even if it is not being driven)
161 0 : _pg_user->spinning = (((_pg_data[_pg_byteindex + 1]) & 0x1)) ? true : false;
162 :
163 : // Set if motor is spinning opposite to configured rotation direction
164 0 : _pg_user->spinningReversed = ((_pg_data[_pg_byteindex + 2] >> 7)) ? true : false;
165 :
166 : // Set if motor duty cycle is being actively reduced due to ESC protection settings
167 0 : _pg_user->foldback = (((_pg_data[_pg_byteindex + 2] >> 6) & 0x1)) ? true : false;
168 :
169 : // Set if the ESC is attempting to sync with the motor
170 0 : _pg_user->syncing = (((_pg_data[_pg_byteindex + 2] >> 5) & 0x1)) ? true : false;
171 :
172 : // Set if the ESC is applying motor braking
173 0 : _pg_user->braking = (((_pg_data[_pg_byteindex + 2] >> 4) & 0x1)) ? true : false;
174 :
175 : // Set if the motor duty cycle is being actively increased due to ESC protection settings
176 0 : _pg_user->foldforward = (((_pg_data[_pg_byteindex + 2] >> 3) & 0x1)) ? true : false;
177 :
178 : // Reserved bits for future use
179 :
180 : // Set if the ESC settings are locked and cannot be changed
181 0 : _pg_user->settingsLocked = (((_pg_data[_pg_byteindex + 2] >> 1) & 0x1)) ? true : false;
182 :
183 : // Set if the ESC is in debug mode (factory use only)
184 0 : _pg_user->debug = (((_pg_data[_pg_byteindex + 2]) & 0x1)) ? true : false;
185 0 : _pg_byteindex += 3; // close bit field
186 :
187 0 : *_pg_bytecount = _pg_byteindex;
188 :
189 0 : return 1;
190 :
191 : }// decodeESC_StatusBits_t
192 :
193 : /*!
194 : * \brief Encode a ESC_WarningBits_t into a byte array
195 : *
196 : * The *warning* bits enumerate various system warnings/errors of which the
197 : * user (or user software) should be made aware. These *warning* bits are
198 : * transmitted in the telemetry packets such that user software is aware of any
199 : * these *warning* conditions and can poll the ESC for particular packets if
200 : * any further information is needed. The ESC will continue to function in the
201 : * case of a *warning* state
202 : * \param _pg_data points to the byte array to add encoded data to
203 : * \param _pg_bytecount points to the starting location in the byte array, and will be incremented by the number of encoded bytes.
204 : * \param _pg_user is the data to encode in the byte array
205 : */
206 0 : void encodeESC_WarningBits_t(uint8_t* _pg_data, int* _pg_bytecount, const ESC_WarningBits_t* _pg_user)
207 : {
208 0 : int _pg_byteindex = *_pg_bytecount;
209 :
210 : // Reserved for future use
211 0 : _pg_data[_pg_byteindex] = 0;
212 :
213 : // Set if the ESC motor speed exceeds the configured warning threshold
214 0 : _pg_data[_pg_byteindex] |= (uint8_t)((_pg_user->overspeed == true) ? 1 : 0) << 6;
215 :
216 : // Set if the ESC DC current (positive, into the motor) exceeds the configured warning threshold
217 0 : _pg_data[_pg_byteindex] |= (uint8_t)((_pg_user->overcurrent == true) ? 1 : 0) << 5;
218 :
219 : // Set if the internal ESC temperature is above the warning threshold
220 0 : _pg_data[_pg_byteindex] |= (uint8_t)((_pg_user->escTemperature == true) ? 1 : 0) << 4;
221 :
222 : // Set if the motor temperature is above the warning threshold
223 0 : _pg_data[_pg_byteindex] |= (uint8_t)((_pg_user->motorTemperature == true) ? 1 : 0) << 3;
224 :
225 : // Set if the input voltage is below the minimum threshold
226 0 : _pg_data[_pg_byteindex] |= (uint8_t)((_pg_user->undervoltage == true) ? 1 : 0) << 2;
227 :
228 : // Set if the input voltage is above the maximum threshold
229 0 : _pg_data[_pg_byteindex] |= (uint8_t)((_pg_user->overvoltage == true) ? 1 : 0) << 1;
230 :
231 : // Set if hardware PWM input is enabled but invalid
232 0 : _pg_data[_pg_byteindex] |= (uint8_t)((_pg_user->invalidPWMsignal == true) ? 1 : 0);
233 :
234 : // Set if the motor demag angle exceeds the maximum threshold
235 0 : _pg_data[_pg_byteindex + 1] = (uint8_t)((_pg_user->demagAngle == true) ? 1 : 0) << 7;
236 :
237 : // Set if the auto-advance exceeds 25 degrees
238 0 : _pg_data[_pg_byteindex + 1] |= (uint8_t)((_pg_user->advanceLimit == true) ? 1 : 0) << 6;
239 :
240 : // Set if the measured demag pulse is exceptionally long
241 0 : _pg_data[_pg_byteindex + 1] |= (uint8_t)((_pg_user->longDemag == true) ? 1 : 0) << 5;
242 :
243 : // Set if a zero-crossing measurement was missed
244 0 : _pg_data[_pg_byteindex + 1] |= (uint8_t)((_pg_user->missedZeroCrossing == true) ? 1 : 0) << 4;
245 :
246 : // Motor is spinning in the wrong direction
247 0 : _pg_data[_pg_byteindex + 1] |= (uint8_t)((_pg_user->spinningReversed == true) ? 1 : 0) << 3;
248 :
249 : // Motor has reached maximum allowable commutation speed
250 0 : _pg_data[_pg_byteindex + 1] |= (uint8_t)((_pg_user->commSpeedLimit == true) ? 1 : 0) << 2;
251 :
252 : // Settings checksum does not match programmed value
253 0 : _pg_data[_pg_byteindex + 1] |= (uint8_t)((_pg_user->settingsChecksum == true) ? 1 : 0) << 1;
254 :
255 : // Set if the ESC's CPU usage is high
256 0 : _pg_data[_pg_byteindex + 1] |= (uint8_t)((_pg_user->highCPUUsage == true) ? 1 : 0);
257 0 : _pg_byteindex += 2; // close bit field
258 :
259 0 : *_pg_bytecount = _pg_byteindex;
260 :
261 0 : }// encodeESC_WarningBits_t
262 :
263 : /*!
264 : * \brief Decode a ESC_WarningBits_t from a byte array
265 : *
266 : * The *warning* bits enumerate various system warnings/errors of which the
267 : * user (or user software) should be made aware. These *warning* bits are
268 : * transmitted in the telemetry packets such that user software is aware of any
269 : * these *warning* conditions and can poll the ESC for particular packets if
270 : * any further information is needed. The ESC will continue to function in the
271 : * case of a *warning* state
272 : * \param _pg_data points to the byte array to decoded data from
273 : * \param _pg_bytecount points to the starting location in the byte array, and will be incremented by the number of bytes decoded
274 : * \param _pg_user is the data to decode from the byte array
275 : * \return 1 if the data are decoded, else 0.
276 : */
277 0 : int decodeESC_WarningBits_t(const uint8_t* _pg_data, int* _pg_bytecount, ESC_WarningBits_t* _pg_user)
278 : {
279 0 : int _pg_byteindex = *_pg_bytecount;
280 :
281 : // Reserved for future use
282 :
283 : // Set if the ESC motor speed exceeds the configured warning threshold
284 0 : _pg_user->overspeed = (((_pg_data[_pg_byteindex] >> 6) & 0x1)) ? true : false;
285 :
286 : // Set if the ESC DC current (positive, into the motor) exceeds the configured warning threshold
287 0 : _pg_user->overcurrent = (((_pg_data[_pg_byteindex] >> 5) & 0x1)) ? true : false;
288 :
289 : // Set if the internal ESC temperature is above the warning threshold
290 0 : _pg_user->escTemperature = (((_pg_data[_pg_byteindex] >> 4) & 0x1)) ? true : false;
291 :
292 : // Set if the motor temperature is above the warning threshold
293 0 : _pg_user->motorTemperature = (((_pg_data[_pg_byteindex] >> 3) & 0x1)) ? true : false;
294 :
295 : // Set if the input voltage is below the minimum threshold
296 0 : _pg_user->undervoltage = (((_pg_data[_pg_byteindex] >> 2) & 0x1)) ? true : false;
297 :
298 : // Set if the input voltage is above the maximum threshold
299 0 : _pg_user->overvoltage = (((_pg_data[_pg_byteindex] >> 1) & 0x1)) ? true : false;
300 :
301 : // Set if hardware PWM input is enabled but invalid
302 0 : _pg_user->invalidPWMsignal = (((_pg_data[_pg_byteindex]) & 0x1)) ? true : false;
303 :
304 : // Set if the motor demag angle exceeds the maximum threshold
305 0 : _pg_user->demagAngle = ((_pg_data[_pg_byteindex + 1] >> 7)) ? true : false;
306 :
307 : // Set if the auto-advance exceeds 25 degrees
308 0 : _pg_user->advanceLimit = (((_pg_data[_pg_byteindex + 1] >> 6) & 0x1)) ? true : false;
309 :
310 : // Set if the measured demag pulse is exceptionally long
311 0 : _pg_user->longDemag = (((_pg_data[_pg_byteindex + 1] >> 5) & 0x1)) ? true : false;
312 :
313 : // Set if a zero-crossing measurement was missed
314 0 : _pg_user->missedZeroCrossing = (((_pg_data[_pg_byteindex + 1] >> 4) & 0x1)) ? true : false;
315 :
316 : // Motor is spinning in the wrong direction
317 0 : _pg_user->spinningReversed = (((_pg_data[_pg_byteindex + 1] >> 3) & 0x1)) ? true : false;
318 :
319 : // Motor has reached maximum allowable commutation speed
320 0 : _pg_user->commSpeedLimit = (((_pg_data[_pg_byteindex + 1] >> 2) & 0x1)) ? true : false;
321 :
322 : // Settings checksum does not match programmed value
323 0 : _pg_user->settingsChecksum = (((_pg_data[_pg_byteindex + 1] >> 1) & 0x1)) ? true : false;
324 :
325 : // Set if the ESC's CPU usage is high
326 0 : _pg_user->highCPUUsage = (((_pg_data[_pg_byteindex + 1]) & 0x1)) ? true : false;
327 0 : _pg_byteindex += 2; // close bit field
328 :
329 0 : *_pg_bytecount = _pg_byteindex;
330 :
331 0 : return 1;
332 :
333 : }// decodeESC_WarningBits_t
334 :
335 : /*!
336 : * \brief Encode a ESC_ExtendedWarningBits_t into a byte array
337 : *
338 :
339 : * \param _pg_data points to the byte array to add encoded data to
340 : * \param _pg_bytecount points to the starting location in the byte array, and will be incremented by the number of encoded bytes.
341 : * \param _pg_user is the data to encode in the byte array
342 : */
343 0 : void encodeESC_ExtendedWarningBits_t(uint8_t* _pg_data, int* _pg_bytecount, const ESC_WarningBits_t* _pg_user)
344 : {
345 0 : int _pg_byteindex = *_pg_bytecount;
346 0 : unsigned int _pg_tempbitfield = 0;
347 :
348 : // Set if the ESC DC current (negative, out of the motor) exceeds the configured warning threshold
349 0 : _pg_data[_pg_byteindex] = (uint8_t)((_pg_user->regenCurrent == true) ? 1 : 0) << 7;
350 :
351 : // Set if the hall sensors are running out of specification
352 0 : _pg_data[_pg_byteindex] |= (uint8_t)((_pg_user->hallSensors == true) ? 1 : 0) << 6;
353 :
354 : // Reserved for future use
355 : // Range of reservedWarningBits is 0 to 16383.
356 0 : _pg_tempbitfield = (unsigned int)limitMax(_pg_user->reservedWarningBits, 16383);
357 0 : _pg_data[_pg_byteindex + 1] = (uint8_t)_pg_tempbitfield;
358 :
359 0 : _pg_tempbitfield >>= 8;
360 0 : _pg_data[_pg_byteindex] |= (uint8_t)_pg_tempbitfield;
361 0 : _pg_byteindex += 2; // close bit field
362 :
363 0 : *_pg_bytecount = _pg_byteindex;
364 :
365 0 : }// encodeESC_ExtendedWarningBits_t
366 :
367 : /*!
368 : * \brief Decode a ESC_ExtendedWarningBits_t from a byte array
369 : *
370 :
371 : * \param _pg_data points to the byte array to decoded data from
372 : * \param _pg_bytecount points to the starting location in the byte array, and will be incremented by the number of bytes decoded
373 : * \param _pg_user is the data to decode from the byte array
374 : * \return 1 if the data are decoded, else 0.
375 : */
376 0 : int decodeESC_ExtendedWarningBits_t(const uint8_t* _pg_data, int* _pg_bytecount, ESC_WarningBits_t* _pg_user)
377 : {
378 0 : int _pg_byteindex = *_pg_bytecount;
379 0 : unsigned int _pg_tempbitfield = 0;
380 :
381 : // Set if the ESC DC current (negative, out of the motor) exceeds the configured warning threshold
382 0 : _pg_user->regenCurrent = ((_pg_data[_pg_byteindex] >> 7)) ? true : false;
383 :
384 : // Set if the hall sensors are running out of specification
385 0 : _pg_user->hallSensors = (((_pg_data[_pg_byteindex] >> 6) & 0x1)) ? true : false;
386 :
387 : // Reserved for future use
388 : // Range of reservedWarningBits is 0 to 16383.
389 0 : _pg_tempbitfield = (_pg_data[_pg_byteindex] & 0x3F);
390 :
391 0 : _pg_tempbitfield <<= 8;
392 0 : _pg_tempbitfield |= _pg_data[_pg_byteindex + 1];
393 :
394 0 : _pg_user->reservedWarningBits = _pg_tempbitfield;
395 0 : _pg_byteindex += 2; // close bit field
396 :
397 0 : *_pg_bytecount = _pg_byteindex;
398 :
399 0 : return 1;
400 :
401 : }// decodeESC_ExtendedWarningBits_t
402 :
403 : /*!
404 : * \brief Encode a ESC_ErrorBits_t into a byte array
405 : *
406 : * The *error* bits enumerate critical system errors that will cause the ESC to
407 : * stop functioning until the error cases are alleviated
408 : * \param _pg_data points to the byte array to add encoded data to
409 : * \param _pg_bytecount points to the starting location in the byte array, and will be incremented by the number of encoded bytes.
410 : * \param _pg_user is the data to encode in the byte array
411 : */
412 0 : void encodeESC_ErrorBits_t(uint8_t* _pg_data, int* _pg_bytecount, const ESC_ErrorBits_t* _pg_user)
413 : {
414 0 : int _pg_byteindex = *_pg_bytecount;
415 :
416 : // Set if the ESC failed to start the motor
417 0 : _pg_data[_pg_byteindex] = (uint8_t)((_pg_user->failedStart == true) ? 1 : 0) << 7;
418 :
419 : // Lost commutation
420 0 : _pg_data[_pg_byteindex] |= (uint8_t)((_pg_user->commutation == true) ? 1 : 0) << 6;
421 :
422 : // Set if hall sensor error detected
423 0 : _pg_data[_pg_byteindex] |= (uint8_t)((_pg_user->hallSensor == true) ? 1 : 0) << 5;
424 :
425 : // Current exceeded hard limit
426 0 : _pg_data[_pg_byteindex] |= (uint8_t)((_pg_user->overcurrent == true) ? 1 : 0) << 4;
427 :
428 : // Temperature exceeded hard-limit
429 0 : _pg_data[_pg_byteindex] |= (uint8_t)((_pg_user->overtemperature == true) ? 1 : 0) << 3;
430 :
431 : // Motor commutation speed exceeded hard-limit
432 0 : _pg_data[_pg_byteindex] |= (uint8_t)((_pg_user->overspeed == true) ? 1 : 0) << 2;
433 :
434 : // Motor stopped due to high demag angle
435 0 : _pg_data[_pg_byteindex] |= (uint8_t)((_pg_user->demag == true) ? 1 : 0) << 1;
436 :
437 : // Phase voltage error detected
438 0 : _pg_data[_pg_byteindex] |= (uint8_t)((_pg_user->phaseVoltage == true) ? 1 : 0);
439 :
440 : // Regen current exceeded hard limit
441 0 : _pg_data[_pg_byteindex + 1] = (uint8_t)((_pg_user->regenCurrent == true) ? 1 : 0) << 7;
442 :
443 : // Reserved for future use
444 0 : _pg_byteindex += 2; // close bit field
445 :
446 0 : *_pg_bytecount = _pg_byteindex;
447 :
448 0 : }// encodeESC_ErrorBits_t
449 :
450 : /*!
451 : * \brief Decode a ESC_ErrorBits_t from a byte array
452 : *
453 : * The *error* bits enumerate critical system errors that will cause the ESC to
454 : * stop functioning until the error cases are alleviated
455 : * \param _pg_data points to the byte array to decoded data from
456 : * \param _pg_bytecount points to the starting location in the byte array, and will be incremented by the number of bytes decoded
457 : * \param _pg_user is the data to decode from the byte array
458 : * \return 1 if the data are decoded, else 0.
459 : */
460 0 : int decodeESC_ErrorBits_t(const uint8_t* _pg_data, int* _pg_bytecount, ESC_ErrorBits_t* _pg_user)
461 : {
462 0 : int _pg_byteindex = *_pg_bytecount;
463 :
464 : // Set if the ESC failed to start the motor
465 0 : _pg_user->failedStart = ((_pg_data[_pg_byteindex] >> 7)) ? true : false;
466 :
467 : // Lost commutation
468 0 : _pg_user->commutation = (((_pg_data[_pg_byteindex] >> 6) & 0x1)) ? true : false;
469 :
470 : // Set if hall sensor error detected
471 0 : _pg_user->hallSensor = (((_pg_data[_pg_byteindex] >> 5) & 0x1)) ? true : false;
472 :
473 : // Current exceeded hard limit
474 0 : _pg_user->overcurrent = (((_pg_data[_pg_byteindex] >> 4) & 0x1)) ? true : false;
475 :
476 : // Temperature exceeded hard-limit
477 0 : _pg_user->overtemperature = (((_pg_data[_pg_byteindex] >> 3) & 0x1)) ? true : false;
478 :
479 : // Motor commutation speed exceeded hard-limit
480 0 : _pg_user->overspeed = (((_pg_data[_pg_byteindex] >> 2) & 0x1)) ? true : false;
481 :
482 : // Motor stopped due to high demag angle
483 0 : _pg_user->demag = (((_pg_data[_pg_byteindex] >> 1) & 0x1)) ? true : false;
484 :
485 : // Phase voltage error detected
486 0 : _pg_user->phaseVoltage = (((_pg_data[_pg_byteindex]) & 0x1)) ? true : false;
487 :
488 : // Regen current exceeded hard limit
489 0 : _pg_user->regenCurrent = ((_pg_data[_pg_byteindex + 1] >> 7)) ? true : false;
490 :
491 : // Reserved for future use
492 0 : _pg_byteindex += 2; // close bit field
493 :
494 0 : *_pg_bytecount = _pg_byteindex;
495 :
496 0 : return 1;
497 :
498 : }// decodeESC_ErrorBits_t
499 :
500 : /*!
501 : * \brief Encode a ESC_ExtendedErrorBits_t into a byte array
502 : *
503 :
504 : * \param _pg_data points to the byte array to add encoded data to
505 : * \param _pg_bytecount points to the starting location in the byte array, and will be incremented by the number of encoded bytes.
506 : * \param _pg_user is the data to encode in the byte array
507 : */
508 0 : void encodeESC_ExtendedErrorBits_t(uint8_t* _pg_data, int* _pg_bytecount, const ESC_ErrorBits_t* _pg_user)
509 : {
510 0 : int _pg_byteindex = *_pg_bytecount;
511 :
512 : // Reserved for future use
513 : // Range of reservedErrorBits is 0 to 255.
514 0 : uint8ToBytes(_pg_user->reservedErrorBits, _pg_data, &_pg_byteindex);
515 :
516 0 : *_pg_bytecount = _pg_byteindex;
517 :
518 0 : }// encodeESC_ExtendedErrorBits_t
519 :
520 : /*!
521 : * \brief Decode a ESC_ExtendedErrorBits_t from a byte array
522 : *
523 :
524 : * \param _pg_data points to the byte array to decoded data from
525 : * \param _pg_bytecount points to the starting location in the byte array, and will be incremented by the number of bytes decoded
526 : * \param _pg_user is the data to decode from the byte array
527 : * \return 1 if the data are decoded, else 0.
528 : */
529 0 : int decodeESC_ExtendedErrorBits_t(const uint8_t* _pg_data, int* _pg_bytecount, ESC_ErrorBits_t* _pg_user)
530 : {
531 0 : int _pg_byteindex = *_pg_bytecount;
532 :
533 : // Reserved for future use
534 : // Range of reservedErrorBits is 0 to 255.
535 0 : _pg_user->reservedErrorBits = uint8FromBytes(_pg_data, &_pg_byteindex);
536 :
537 0 : *_pg_bytecount = _pg_byteindex;
538 :
539 0 : return 1;
540 :
541 : }// decodeESC_ExtendedErrorBits_t
542 :
543 : /*!
544 : * \brief Encode a ESC_TelemetryPackets_t into a byte array
545 : *
546 : * These bits are used to determine which packets are automatically transmitted
547 : * as telemetry data by the ESC. Only the packets described here can be
548 : * configured as telemetry packets
549 : * \param _pg_data points to the byte array to add encoded data to
550 : * \param _pg_bytecount points to the starting location in the byte array, and will be incremented by the number of encoded bytes.
551 : * \param _pg_user is the data to encode in the byte array
552 : */
553 0 : void encodeESC_TelemetryPackets_t(uint8_t* _pg_data, int* _pg_bytecount, const ESC_TelemetryPackets_t* _pg_user)
554 : {
555 0 : int _pg_byteindex = *_pg_bytecount;
556 :
557 : // If this bit is set, the STATUS_A packet will be transmitted at the configured rate
558 0 : _pg_data[_pg_byteindex] = (uint8_t)((_pg_user->statusA == true) ? 1 : 0) << 7;
559 :
560 : // If this bit is set, the STATUS_B packet will be transmitted at the configured rate
561 0 : _pg_data[_pg_byteindex] |= (uint8_t)((_pg_user->statusB == true) ? 1 : 0) << 6;
562 :
563 : // If this bit is set, the STATUS_C packet will be transmitted at the configured rate
564 0 : _pg_data[_pg_byteindex] |= (uint8_t)((_pg_user->statusC == true) ? 1 : 0) << 5;
565 :
566 : // Hidden field skipped
567 :
568 : // If this bit is set, the STATUS_D packet will be transmitted at the configured rate
569 0 : _pg_data[_pg_byteindex] |= (uint8_t)((_pg_user->statusD == true) ? 1 : 0) << 3;
570 :
571 : // Reserved for future use
572 0 : _pg_data[_pg_byteindex] |= (uint8_t)((_pg_user->reservedTelemA == true) ? 1 : 0) << 2;
573 :
574 : // If this bit is set, any STATUS_x packets selected for telemetry will be mirrored on the Piccolo Downlink packet group (0x14)
575 0 : _pg_data[_pg_byteindex] |= (uint8_t)((_pg_user->piccoloDownlink == true) ? 1 : 0) << 1;
576 :
577 : // Reserved for future use
578 0 : _pg_data[_pg_byteindex] |= (uint8_t)((_pg_user->reservedTelemC == true) ? 1 : 0);
579 0 : _pg_byteindex += 1; // close bit field
580 :
581 0 : *_pg_bytecount = _pg_byteindex;
582 :
583 0 : }// encodeESC_TelemetryPackets_t
584 :
585 : /*!
586 : * \brief Decode a ESC_TelemetryPackets_t from a byte array
587 : *
588 : * These bits are used to determine which packets are automatically transmitted
589 : * as telemetry data by the ESC. Only the packets described here can be
590 : * configured as telemetry packets
591 : * \param _pg_data points to the byte array to decoded data from
592 : * \param _pg_bytecount points to the starting location in the byte array, and will be incremented by the number of bytes decoded
593 : * \param _pg_user is the data to decode from the byte array
594 : * \return 1 if the data are decoded, else 0.
595 : */
596 0 : int decodeESC_TelemetryPackets_t(const uint8_t* _pg_data, int* _pg_bytecount, ESC_TelemetryPackets_t* _pg_user)
597 : {
598 0 : int _pg_byteindex = *_pg_bytecount;
599 :
600 : // If this bit is set, the STATUS_A packet will be transmitted at the configured rate
601 0 : _pg_user->statusA = ((_pg_data[_pg_byteindex] >> 7)) ? true : false;
602 :
603 : // If this bit is set, the STATUS_B packet will be transmitted at the configured rate
604 0 : _pg_user->statusB = (((_pg_data[_pg_byteindex] >> 6) & 0x1)) ? true : false;
605 :
606 : // If this bit is set, the STATUS_C packet will be transmitted at the configured rate
607 0 : _pg_user->statusC = (((_pg_data[_pg_byteindex] >> 5) & 0x1)) ? true : false;
608 :
609 : // Hidden field skipped
610 :
611 : // If this bit is set, the STATUS_D packet will be transmitted at the configured rate
612 0 : _pg_user->statusD = (((_pg_data[_pg_byteindex] >> 3) & 0x1)) ? true : false;
613 :
614 : // Reserved for future use
615 0 : _pg_user->reservedTelemA = (((_pg_data[_pg_byteindex] >> 2) & 0x1)) ? true : false;
616 :
617 : // If this bit is set, any STATUS_x packets selected for telemetry will be mirrored on the Piccolo Downlink packet group (0x14)
618 0 : _pg_user->piccoloDownlink = (((_pg_data[_pg_byteindex] >> 1) & 0x1)) ? true : false;
619 :
620 : // Reserved for future use
621 0 : _pg_user->reservedTelemC = (((_pg_data[_pg_byteindex]) & 0x1)) ? true : false;
622 0 : _pg_byteindex += 1; // close bit field
623 :
624 0 : *_pg_bytecount = _pg_byteindex;
625 :
626 0 : return 1;
627 :
628 : }// decodeESC_TelemetryPackets_t
629 :
630 : /*!
631 : * \brief Encode a ESC_DebugPackets_t into a byte array
632 : *
633 : * These bits are used to select which debug packets are transmitted at
634 : * high-frequency by the ESC
635 : * \param _pg_data points to the byte array to add encoded data to
636 : * \param _pg_bytecount points to the starting location in the byte array, and will be incremented by the number of encoded bytes.
637 : * \param _pg_user is the data to encode in the byte array
638 : */
639 0 : void encodeESC_DebugPackets_t(uint8_t* _pg_data, int* _pg_bytecount, const ESC_DebugPackets_t* _pg_user)
640 : {
641 0 : int _pg_byteindex = *_pg_bytecount;
642 :
643 : // Control loop terms
644 0 : _pg_data[_pg_byteindex] = (uint8_t)((_pg_user->ctrlLoopOutputs == true) ? 1 : 0) << 7;
645 :
646 : // Hall sensor debug information
647 0 : _pg_data[_pg_byteindex] |= (uint8_t)((_pg_user->hallSensors == true) ? 1 : 0) << 6;
648 :
649 : // Commutation A debug information
650 0 : _pg_data[_pg_byteindex] |= (uint8_t)((_pg_user->commutationA == true) ? 1 : 0) << 5;
651 :
652 : // Demag debug information
653 0 : _pg_data[_pg_byteindex] |= (uint8_t)((_pg_user->demag == true) ? 1 : 0) << 4;
654 :
655 : // PWM input debug information
656 0 : _pg_data[_pg_byteindex] |= (uint8_t)((_pg_user->pwmInput == true) ? 1 : 0) << 3;
657 :
658 : // Commutation B debug information
659 0 : _pg_data[_pg_byteindex] |= (uint8_t)((_pg_user->commutationB == true) ? 1 : 0) << 2;
660 :
661 : // Commutation C debug information
662 0 : _pg_data[_pg_byteindex] |= (uint8_t)((_pg_user->commutationC == true) ? 1 : 0) << 1;
663 :
664 : // Direct phase sampling debug information
665 0 : _pg_data[_pg_byteindex] |= (uint8_t)((_pg_user->phaseSamples == true) ? 1 : 0);
666 :
667 : // Starting debug information
668 0 : _pg_data[_pg_byteindex + 1] = (uint8_t)((_pg_user->starting == true) ? 1 : 0) << 7;
669 :
670 : // Reserved for future use
671 0 : _pg_byteindex += 2; // close bit field
672 :
673 0 : *_pg_bytecount = _pg_byteindex;
674 :
675 0 : }// encodeESC_DebugPackets_t
676 :
677 : /*!
678 : * \brief Decode a ESC_DebugPackets_t from a byte array
679 : *
680 : * These bits are used to select which debug packets are transmitted at
681 : * high-frequency by the ESC
682 : * \param _pg_data points to the byte array to decoded data from
683 : * \param _pg_bytecount points to the starting location in the byte array, and will be incremented by the number of bytes decoded
684 : * \param _pg_user is the data to decode from the byte array
685 : * \return 1 if the data are decoded, else 0.
686 : */
687 0 : int decodeESC_DebugPackets_t(const uint8_t* _pg_data, int* _pg_bytecount, ESC_DebugPackets_t* _pg_user)
688 : {
689 0 : int _pg_byteindex = *_pg_bytecount;
690 :
691 : // Control loop terms
692 0 : _pg_user->ctrlLoopOutputs = ((_pg_data[_pg_byteindex] >> 7)) ? true : false;
693 :
694 : // Hall sensor debug information
695 0 : _pg_user->hallSensors = (((_pg_data[_pg_byteindex] >> 6) & 0x1)) ? true : false;
696 :
697 : // Commutation A debug information
698 0 : _pg_user->commutationA = (((_pg_data[_pg_byteindex] >> 5) & 0x1)) ? true : false;
699 :
700 : // Demag debug information
701 0 : _pg_user->demag = (((_pg_data[_pg_byteindex] >> 4) & 0x1)) ? true : false;
702 :
703 : // PWM input debug information
704 0 : _pg_user->pwmInput = (((_pg_data[_pg_byteindex] >> 3) & 0x1)) ? true : false;
705 :
706 : // Commutation B debug information
707 0 : _pg_user->commutationB = (((_pg_data[_pg_byteindex] >> 2) & 0x1)) ? true : false;
708 :
709 : // Commutation C debug information
710 0 : _pg_user->commutationC = (((_pg_data[_pg_byteindex] >> 1) & 0x1)) ? true : false;
711 :
712 : // Direct phase sampling debug information
713 0 : _pg_user->phaseSamples = (((_pg_data[_pg_byteindex]) & 0x1)) ? true : false;
714 :
715 : // Starting debug information
716 0 : _pg_user->starting = ((_pg_data[_pg_byteindex + 1] >> 7)) ? true : false;
717 :
718 : // Reserved for future use
719 0 : _pg_byteindex += 2; // close bit field
720 :
721 0 : *_pg_bytecount = _pg_byteindex;
722 :
723 0 : return 1;
724 :
725 : }// decodeESC_DebugPackets_t
726 :
727 : /*!
728 : * \brief Encode a ESC_ControlLoopCommon_t into a byte array
729 : *
730 :
731 : * \param _pg_data points to the byte array to add encoded data to
732 : * \param _pg_bytecount points to the starting location in the byte array, and will be incremented by the number of encoded bytes.
733 : * \param _pg_user is the data to encode in the byte array
734 : */
735 0 : void encodeESC_ControlLoopCommon_t(uint8_t* _pg_data, int* _pg_bytecount, const ESC_ControlLoopCommon_t* _pg_user)
736 : {
737 0 : int _pg_byteindex = *_pg_bytecount;
738 :
739 : // Control loop proportional gain
740 : // Range of Kp is 0.0f to 50.0f.
741 0 : float32ScaledTo2UnsignedBeBytes(_pg_user->Kp, _pg_data, &_pg_byteindex, 0.0f, 1310.7f);
742 :
743 : // Control loop integral gain
744 : // Range of Ki is 0.0f to 50.0f.
745 0 : float32ScaledTo2UnsignedBeBytes(_pg_user->Ki, _pg_data, &_pg_byteindex, 0.0f, 1310.7f);
746 :
747 : // Control loop derivative
748 : // Range of Kd is 0.0f to 50.0f.
749 0 : float32ScaledTo2UnsignedBeBytes(_pg_user->Kd, _pg_data, &_pg_byteindex, 0.0f, 1310.7f);
750 :
751 : // Control loop feed-forward gain
752 : // Range of Kf is 0.0f to 50.0f.
753 0 : float32ScaledTo2UnsignedBeBytes(_pg_user->Kf, _pg_data, &_pg_byteindex, 0.0f, 1310.7f);
754 :
755 : // Minimum PWM limit for control loop output
756 : // Range of minPwm is 0 to 255.
757 0 : uint8ToBytes(_pg_user->minPwm, _pg_data, &_pg_byteindex);
758 :
759 : // Maximum PWM limit for control loop output
760 : // Range of maxPwm is 0 to 255.
761 0 : uint8ToBytes(_pg_user->maxPwm, _pg_data, &_pg_byteindex);
762 :
763 : // Reserved for future use
764 : // Range of reserved is 0 to 255.
765 0 : uint8ToBytes(_pg_user->reserved, _pg_data, &_pg_byteindex);
766 :
767 0 : *_pg_bytecount = _pg_byteindex;
768 :
769 0 : }// encodeESC_ControlLoopCommon_t
770 :
771 : /*!
772 : * \brief Decode a ESC_ControlLoopCommon_t from a byte array
773 : *
774 :
775 : * \param _pg_data points to the byte array to decoded data from
776 : * \param _pg_bytecount points to the starting location in the byte array, and will be incremented by the number of bytes decoded
777 : * \param _pg_user is the data to decode from the byte array
778 : * \return 1 if the data are decoded, else 0.
779 : */
780 0 : int decodeESC_ControlLoopCommon_t(const uint8_t* _pg_data, int* _pg_bytecount, ESC_ControlLoopCommon_t* _pg_user)
781 : {
782 0 : int _pg_byteindex = *_pg_bytecount;
783 :
784 : // Control loop proportional gain
785 : // Range of Kp is 0.0f to 50.0f.
786 0 : _pg_user->Kp = float32ScaledFrom2UnsignedBeBytes(_pg_data, &_pg_byteindex, 0.0f, 1.0f/1310.7f);
787 :
788 : // Control loop integral gain
789 : // Range of Ki is 0.0f to 50.0f.
790 0 : _pg_user->Ki = float32ScaledFrom2UnsignedBeBytes(_pg_data, &_pg_byteindex, 0.0f, 1.0f/1310.7f);
791 :
792 : // Control loop derivative
793 : // Range of Kd is 0.0f to 50.0f.
794 0 : _pg_user->Kd = float32ScaledFrom2UnsignedBeBytes(_pg_data, &_pg_byteindex, 0.0f, 1.0f/1310.7f);
795 :
796 : // Control loop feed-forward gain
797 : // Range of Kf is 0.0f to 50.0f.
798 0 : _pg_user->Kf = float32ScaledFrom2UnsignedBeBytes(_pg_data, &_pg_byteindex, 0.0f, 1.0f/1310.7f);
799 :
800 : // Minimum PWM limit for control loop output
801 : // Range of minPwm is 0 to 255.
802 0 : _pg_user->minPwm = uint8FromBytes(_pg_data, &_pg_byteindex);
803 :
804 : // Maximum PWM limit for control loop output
805 : // Range of maxPwm is 0 to 255.
806 0 : _pg_user->maxPwm = uint8FromBytes(_pg_data, &_pg_byteindex);
807 :
808 : // Reserved for future use
809 : // Range of reserved is 0 to 255.
810 0 : _pg_user->reserved = uint8FromBytes(_pg_data, &_pg_byteindex);
811 :
812 0 : *_pg_bytecount = _pg_byteindex;
813 :
814 0 : return 1;
815 :
816 : }// decodeESC_ControlLoopCommon_t
817 :
818 : // end of ESCDefines.c
|