forked from vedderb/bldc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.c
2327 lines (1977 loc) · 67.2 KB
/
commands.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2016 - 2021 Benjamin Vedder [email protected]
This file is part of the VESC firmware.
The VESC firmware is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
The VESC firmware is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "commands.h"
#include "ch.h"
#include "hal.h"
#include "mc_interface.h"
#include "stm32f4xx_conf.h"
#include "servo_simple.h"
#include "buffer.h"
#include "terminal.h"
#include "hw.h"
#include "mcpwm.h"
#include "mcpwm_foc.h"
#include "mc_interface.h"
#include "app.h"
#include "timeout.h"
#include "servo_dec.h"
#include "comm_can.h"
#include "flash_helper.h"
#include "utils.h"
#include "packet.h"
#include "encoder/encoder.h"
#include "nrf_driver.h"
#include "gpdrive.h"
#include "confgenerator.h"
#include "imu.h"
#include "shutdown.h"
#if HAS_BLACKMAGIC
#include "bm_if.h"
#endif
#include "minilzo.h"
#include "mempools.h"
#include "bms.h"
#include "qmlui.h"
#include "crc.h"
#ifdef USE_LISPBM
#include "lispif.h"
#endif
#include "main.h"
#include <math.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
// Settings
#define PRINT_BUFFER_SIZE 255
// Threads
static THD_FUNCTION(blocking_thread, arg);
static THD_WORKING_AREA(blocking_thread_wa, 3000);
static thread_t *blocking_tp;
// Global variables (for conserving RAM)
uint8_t send_buffer_global[PACKET_MAX_PL_LEN];
mutex_t send_buffer_mutex;
// Private variables
static char print_buffer[PRINT_BUFFER_SIZE];
static uint8_t blocking_thread_cmd_buffer[PACKET_MAX_PL_LEN];
static volatile unsigned int blocking_thread_cmd_len = 0;
static volatile bool is_blocking = false;
static volatile int blocking_thread_motor = 1;
static void(* volatile send_func)(unsigned char *data, unsigned int len) = 0;
static void(* volatile send_func_blocking)(unsigned char *data, unsigned int len) = 0;
static void(* volatile send_func_nrf)(unsigned char *data, unsigned int len) = 0;
static void(* volatile send_func_can_fwd)(unsigned char *data, unsigned int len) = 0;
static void(* volatile appdata_func)(unsigned char *data, unsigned int len) = 0;
static void(* volatile hwdata_func)(unsigned char *data, unsigned int len) = 0;
static disp_pos_mode display_position_mode;
static mutex_t print_mutex;
static mutex_t terminal_mutex;
static volatile int fw_version_sent_cnt = 0;
static bool is_initialized = false;
static int nrf_flags = 0;
void commands_init(void) {
chMtxObjectInit(&print_mutex);
chMtxObjectInit(&send_buffer_mutex);
chMtxObjectInit(&terminal_mutex);
chThdCreateStatic(blocking_thread_wa, sizeof(blocking_thread_wa), NORMALPRIO, blocking_thread, NULL);
is_initialized = true;
}
bool commands_is_initialized(void) {
return is_initialized;
}
/**
* Send a packet using the set send function.
*
* @param data
* The packet data.
*
* @param len
* The data length.
*/
void commands_send_packet(unsigned char *data, unsigned int len) {
if (send_func) {
send_func(data, len);
}
}
/**
* Send a packet using the last can fwd function.
*
* @param data
* The packet data.
*
* @param len
* The data length.
*/
void commands_send_packet_can_last(unsigned char *data, unsigned int len) {
if (send_func_can_fwd) {
send_func_can_fwd(data, len);
}
}
/**
* Send a packet using the set NRF51 send function. The NRF51 send function
* is set when the COMM_EXT_NRF_PRESENT and COMM_EXT_NRF_ESB_RX_DATA commands
* are received, at which point the previous send function is restored. The
* intention behind that is to make the NRF51-related communication only with
* the interface that has an NRF51, and prevent the NRF51 communication from
* interfering with other communication.
*
* @param data
* The packet data.
*
* @param len
* The data length.
*/
void commands_send_packet_nrf(unsigned char *data, unsigned int len) {
if (send_func_nrf) {
send_func_nrf(data, len);
}
}
/**
* Send data using the function last used by the blocking thread.
*
* @param data
* The packet data.
*
* @param len
* The data length.
*/
void commands_send_packet_last_blocking(unsigned char *data, unsigned int len) {
if (send_func_blocking) {
send_func_blocking(data, len);
}
}
/**
* Process a received buffer with commands and data.
*
* @param data
* The buffer to process.
*
* @param len
* The length of the buffer.
*/
void commands_process_packet(unsigned char *data, unsigned int len,
void(*reply_func)(unsigned char *data, unsigned int len)) {
if (!len) {
return;
}
COMM_PACKET_ID packet_id;
packet_id = data[0];
data++;
len--;
// The NRF51 ESB implementation is treated like it has its own
// independent communication interface.
if (packet_id == COMM_EXT_NRF_PRESENT ||
packet_id == COMM_EXT_NRF_ESB_RX_DATA) {
send_func_nrf = reply_func;
} else {
send_func = reply_func;
}
// Avoid calling invalid function pointer if it is null.
// commands_send_packet will make the check.
if (!reply_func) {
reply_func = commands_send_packet;
}
if (!send_func_can_fwd) {
send_func_can_fwd = reply_func;
}
switch (packet_id) {
case COMM_FW_VERSION: {
int32_t ind = 0;
uint8_t send_buffer[65];
send_buffer[ind++] = COMM_FW_VERSION;
send_buffer[ind++] = FW_VERSION_MAJOR;
send_buffer[ind++] = FW_VERSION_MINOR;
strcpy((char*)(send_buffer + ind), HW_NAME);
ind += strlen(HW_NAME) + 1;
memcpy(send_buffer + ind, STM32_UUID_8, 12);
ind += 12;
// Add 1 to the UUID for the second motor, so that configuration backup and
// restore works.
if (mc_interface_get_motor_thread() == 2) {
send_buffer[ind - 1]++;
}
send_buffer[ind++] = app_get_configuration()->pairing_done;
send_buffer[ind++] = FW_TEST_VERSION_NUMBER;
send_buffer[ind++] = HW_TYPE_VESC;
send_buffer[ind++] = 0; // No custom config
#ifdef HW_HAS_PHASE_FILTERS
send_buffer[ind++] = 1;
#else
send_buffer[ind++] = 0;
#endif
#ifdef QMLUI_SOURCE_HW
#ifdef QMLUI_HW_FULLSCREEN
send_buffer[ind++] = 2;
#else
send_buffer[ind++] = 1;
#endif
#else
send_buffer[ind++] = 0;
#endif
#ifdef QMLUI_SOURCE_APP
#ifdef QMLUI_APP_FULLSCREEN
send_buffer[ind++] = 2;
#else
send_buffer[ind++] = 1;
#endif
#else
if (flash_helper_code_data(CODE_IND_QML)) {
send_buffer[ind++] = flash_helper_code_flags(CODE_IND_QML);
} else {
send_buffer[ind++] = 0;
}
#endif
send_buffer[ind++] = nrf_flags;
fw_version_sent_cnt++;
reply_func(send_buffer, ind);
} break;
case COMM_JUMP_TO_BOOTLOADER_ALL_CAN:
data[-1] = COMM_JUMP_TO_BOOTLOADER;
comm_can_send_buffer(255, data - 1, len + 1, 2);
chThdSleepMilliseconds(100);
/* Falls through. */
/* no break */
case COMM_JUMP_TO_BOOTLOADER:
flash_helper_jump_to_bootloader();
break;
case COMM_ERASE_NEW_APP_ALL_CAN:
if (nrf_driver_ext_nrf_running()) {
nrf_driver_pause(6000);
}
data[-1] = COMM_ERASE_NEW_APP;
comm_can_send_buffer(255, data - 1, len + 1, 2);
chThdSleepMilliseconds(1500);
/* Falls through. */
/* no break */
case COMM_ERASE_NEW_APP: {
int32_t ind = 0;
if (nrf_driver_ext_nrf_running()) {
nrf_driver_pause(6000);
}
uint16_t flash_res = flash_helper_erase_new_app(buffer_get_uint32(data, &ind));
ind = 0;
uint8_t send_buffer[50];
send_buffer[ind++] = COMM_ERASE_NEW_APP;
send_buffer[ind++] = flash_res == FLASH_COMPLETE ? 1 : 0;
reply_func(send_buffer, ind);
} break;
case COMM_WRITE_NEW_APP_DATA_ALL_CAN_LZO:
case COMM_WRITE_NEW_APP_DATA_ALL_CAN:
if (packet_id == COMM_WRITE_NEW_APP_DATA_ALL_CAN_LZO) {
chMtxLock(&send_buffer_mutex);
memcpy(send_buffer_global, data + 6, len - 6);
int32_t ind = 4;
lzo_uint decompressed_len = buffer_get_uint16(data, &ind);
lzo1x_decompress_safe(send_buffer_global, len - 6, data + 4, &decompressed_len, NULL);
chMtxUnlock(&send_buffer_mutex);
len = decompressed_len + 4;
}
if (nrf_driver_ext_nrf_running()) {
nrf_driver_pause(2000);
}
data[-1] = COMM_WRITE_NEW_APP_DATA;
comm_can_send_buffer(255, data - 1, len + 1, 2);
/* Falls through. */
/* no break */
case COMM_WRITE_NEW_APP_DATA_LZO:
case COMM_WRITE_NEW_APP_DATA: {
if (packet_id == COMM_WRITE_NEW_APP_DATA_LZO) {
chMtxLock(&send_buffer_mutex);
memcpy(send_buffer_global, data + 6, len - 6);
int32_t ind = 4;
lzo_uint decompressed_len = buffer_get_uint16(data, &ind);
lzo1x_decompress_safe(send_buffer_global, len - 6, data + 4, &decompressed_len, NULL);
chMtxUnlock(&send_buffer_mutex);
len = decompressed_len + 4;
}
int32_t ind = 0;
uint32_t new_app_offset = buffer_get_uint32(data, &ind);
if (nrf_driver_ext_nrf_running()) {
nrf_driver_pause(2000);
}
uint16_t flash_res = flash_helper_write_new_app_data(new_app_offset, data + ind, len - ind);
SHUTDOWN_RESET();
ind = 0;
uint8_t send_buffer[50];
send_buffer[ind++] = COMM_WRITE_NEW_APP_DATA;
send_buffer[ind++] = flash_res == FLASH_COMPLETE ? 1 : 0;
buffer_append_uint32(send_buffer, new_app_offset, &ind);
reply_func(send_buffer, ind);
} break;
case COMM_GET_VALUES:
case COMM_GET_VALUES_SELECTIVE: {
int32_t ind = 0;
chMtxLock(&send_buffer_mutex);
uint8_t *send_buffer = send_buffer_global;
send_buffer[ind++] = packet_id;
uint32_t mask = 0xFFFFFFFF;
if (packet_id == COMM_GET_VALUES_SELECTIVE) {
int32_t ind2 = 0;
mask = buffer_get_uint32(data, &ind2);
buffer_append_uint32(send_buffer, mask, &ind);
}
if (mask & ((uint32_t)1 << 0)) {
buffer_append_float16(send_buffer, mc_interface_temp_fet_filtered(), 1e1, &ind);
}
if (mask & ((uint32_t)1 << 1)) {
buffer_append_float16(send_buffer, mc_interface_temp_motor_filtered(), 1e1, &ind);
}
if (mask & ((uint32_t)1 << 2)) {
buffer_append_float32(send_buffer, mc_interface_read_reset_avg_motor_current(), 1e2, &ind);
}
if (mask & ((uint32_t)1 << 3)) {
buffer_append_float32(send_buffer, mc_interface_read_reset_avg_input_current(), 1e2, &ind);
}
if (mask & ((uint32_t)1 << 4)) {
buffer_append_float32(send_buffer, mc_interface_read_reset_avg_id(), 1e2, &ind);
}
if (mask & ((uint32_t)1 << 5)) {
buffer_append_float32(send_buffer, mc_interface_read_reset_avg_iq(), 1e2, &ind);
}
if (mask & ((uint32_t)1 << 6)) {
buffer_append_float16(send_buffer, mc_interface_get_duty_cycle_now(), 1e3, &ind);
}
if (mask & ((uint32_t)1 << 7)) {
buffer_append_float32(send_buffer, mc_interface_get_rpm(), 1e0, &ind);
}
if (mask & ((uint32_t)1 << 8)) {
buffer_append_float16(send_buffer, mc_interface_get_input_voltage_filtered(), 1e1, &ind);
}
if (mask & ((uint32_t)1 << 9)) {
buffer_append_float32(send_buffer, mc_interface_get_amp_hours(false), 1e4, &ind);
}
if (mask & ((uint32_t)1 << 10)) {
buffer_append_float32(send_buffer, mc_interface_get_amp_hours_charged(false), 1e4, &ind);
}
if (mask & ((uint32_t)1 << 11)) {
buffer_append_float32(send_buffer, mc_interface_get_watt_hours(false), 1e4, &ind);
}
if (mask & ((uint32_t)1 << 12)) {
buffer_append_float32(send_buffer, mc_interface_get_watt_hours_charged(false), 1e4, &ind);
}
if (mask & ((uint32_t)1 << 13)) {
buffer_append_int32(send_buffer, mc_interface_get_tachometer_value(false), &ind);
}
if (mask & ((uint32_t)1 << 14)) {
buffer_append_int32(send_buffer, mc_interface_get_tachometer_abs_value(false), &ind);
}
if (mask & ((uint32_t)1 << 15)) {
send_buffer[ind++] = mc_interface_get_fault();
}
if (mask & ((uint32_t)1 << 16)) {
buffer_append_float32(send_buffer, mc_interface_get_pid_pos_now(), 1e6, &ind);
}
if (mask & ((uint32_t)1 << 17)) {
uint8_t current_controller_id = app_get_configuration()->controller_id;
#ifdef HW_HAS_DUAL_MOTORS
if (mc_interface_get_motor_thread() == 2) {
current_controller_id = utils_second_motor_id();
}
#endif
send_buffer[ind++] = current_controller_id;
}
if (mask & ((uint32_t)1 << 18)) {
buffer_append_float16(send_buffer, NTC_TEMP_MOS1(), 1e1, &ind);
buffer_append_float16(send_buffer, NTC_TEMP_MOS2(), 1e1, &ind);
buffer_append_float16(send_buffer, NTC_TEMP_MOS3(), 1e1, &ind);
}
if (mask & ((uint32_t)1 << 19)) {
buffer_append_float32(send_buffer, mc_interface_read_reset_avg_vd(), 1e3, &ind);
}
if (mask & ((uint32_t)1 << 20)) {
buffer_append_float32(send_buffer, mc_interface_read_reset_avg_vq(), 1e3, &ind);
}
if (mask & ((uint32_t)1 << 21)) {
uint8_t status = 0;
status |= timeout_has_timeout();
status |= timeout_kill_sw_active() << 1;
send_buffer[ind++] = status;
}
reply_func(send_buffer, ind);
chMtxUnlock(&send_buffer_mutex);
} break;
case COMM_SET_DUTY: {
int32_t ind = 0;
mc_interface_set_duty((float)buffer_get_int32(data, &ind) / 100000.0);
timeout_reset();
} break;
case COMM_SET_CURRENT: {
int32_t ind = 0;
mc_interface_set_current((float)buffer_get_int32(data, &ind) / 1000.0);
timeout_reset();
} break;
case COMM_SET_CURRENT_BRAKE: {
int32_t ind = 0;
mc_interface_set_brake_current((float)buffer_get_int32(data, &ind) / 1000.0);
timeout_reset();
} break;
case COMM_SET_RPM: {
int32_t ind = 0;
mc_interface_set_pid_speed((float)buffer_get_int32(data, &ind));
timeout_reset();
} break;
case COMM_SET_POS: {
int32_t ind = 0;
mc_interface_set_pid_pos((float)buffer_get_int32(data, &ind) / 1000000.0);
timeout_reset();
} break;
case COMM_SET_HANDBRAKE: {
int32_t ind = 0;
mc_interface_set_handbrake(buffer_get_float32(data, 1e3, &ind));
timeout_reset();
} break;
case COMM_SET_DETECT: {
int32_t ind = 0;
display_position_mode = data[ind++];
if (mc_interface_get_configuration()->motor_type == MOTOR_TYPE_BLDC) {
if (display_position_mode == DISP_POS_MODE_NONE) {
mc_interface_release_motor();
} else if (display_position_mode == DISP_POS_MODE_INDUCTANCE) {
mcpwm_set_detect();
}
}
timeout_reset();
} break;
case COMM_SET_SERVO_POS: {
int32_t ind = 0;
servo_simple_set_output(buffer_get_float16(data, 1000.0, &ind));
} break;
case COMM_SET_MCCONF: {
#ifndef HW_MCCONF_READ_ONLY
mc_configuration *mcconf = mempools_alloc_mcconf();
*mcconf = *mc_interface_get_configuration();
if (confgenerator_deserialize_mcconf(data, mcconf)) {
utils_truncate_number(&mcconf->l_current_max_scale , 0.0, 1.0);
utils_truncate_number(&mcconf->l_current_min_scale , 0.0, 1.0);
#ifdef HW_HAS_DUAL_MOTORS
mcconf->motor_type = MOTOR_TYPE_FOC;
#endif
mcconf->lo_current_max = mcconf->l_current_max * mcconf->l_current_max_scale;
mcconf->lo_current_min = mcconf->l_current_min * mcconf->l_current_min_scale;
mcconf->lo_in_current_max = mcconf->l_in_current_max;
mcconf->lo_in_current_min = mcconf->l_in_current_min;
mcconf->lo_current_motor_max_now = mcconf->lo_current_max;
mcconf->lo_current_motor_min_now = mcconf->lo_current_min;
commands_apply_mcconf_hw_limits(mcconf);
conf_general_store_mc_configuration(mcconf, mc_interface_get_motor_thread() == 2);
mc_interface_set_configuration(mcconf);
chThdSleepMilliseconds(200);
int32_t ind = 0;
uint8_t send_buffer[50];
send_buffer[ind++] = packet_id;
reply_func(send_buffer, ind);
} else {
commands_printf("Warning: Could not set mcconf due to wrong signature");
}
mempools_free_mcconf(mcconf);
#endif
} break;
case COMM_GET_MCCONF:
case COMM_GET_MCCONF_DEFAULT: {
mc_configuration *mcconf = mempools_alloc_mcconf();
if (packet_id == COMM_GET_MCCONF) {
*mcconf = *mc_interface_get_configuration();
} else {
confgenerator_set_defaults_mcconf(mcconf);
volatile const mc_configuration *mcconf_now = mc_interface_get_configuration();
// Keep the old offsets
mcconf->foc_offsets_current[0] = mcconf_now->foc_offsets_current[0];
mcconf->foc_offsets_current[1] = mcconf_now->foc_offsets_current[1];
mcconf->foc_offsets_current[2] = mcconf_now->foc_offsets_current[2];
mcconf->foc_offsets_voltage[0] = mcconf_now->foc_offsets_voltage[0];
mcconf->foc_offsets_voltage[1] = mcconf_now->foc_offsets_voltage[1];
mcconf->foc_offsets_voltage[2] = mcconf_now->foc_offsets_voltage[2];
mcconf->foc_offsets_voltage_undriven[0] = mcconf_now->foc_offsets_voltage_undriven[0];
mcconf->foc_offsets_voltage_undriven[1] = mcconf_now->foc_offsets_voltage_undriven[1];
mcconf->foc_offsets_voltage_undriven[2] = mcconf_now->foc_offsets_voltage_undriven[2];
}
commands_send_mcconf(packet_id, mcconf);
mempools_free_mcconf(mcconf);
} break;
case COMM_SET_APPCONF: {
#ifndef HW_APPCONF_READ_ONLY
app_configuration *appconf = mempools_alloc_appconf();
*appconf = *app_get_configuration();
if (confgenerator_deserialize_appconf(data, appconf)) {
#ifdef HW_HAS_DUAL_MOTORS
// Ignore ID when setting second motor config
if (mc_interface_get_motor_thread() == 2) {
appconf->controller_id = app_get_configuration()->controller_id;
}
#endif
conf_general_store_app_configuration(appconf);
app_set_configuration(appconf);
timeout_configure(appconf->timeout_msec, appconf->timeout_brake_current, appconf->kill_sw_mode);
chThdSleepMilliseconds(200);
int32_t ind = 0;
uint8_t send_buffer[50];
send_buffer[ind++] = packet_id;
reply_func(send_buffer, ind);
} else {
commands_printf("Warning: Could not set appconf due to wrong signature");
}
mempools_free_appconf(appconf);
#endif
} break;
case COMM_GET_APPCONF:
case COMM_GET_APPCONF_DEFAULT: {
app_configuration *appconf = mempools_alloc_appconf();
if (packet_id == COMM_GET_APPCONF) {
*appconf = *app_get_configuration();
} else {
confgenerator_set_defaults_appconf(appconf);
}
#ifdef HW_HAS_DUAL_MOTORS
if (mc_interface_get_motor_thread() == 2) {
appconf->controller_id = utils_second_motor_id();
}
#endif
commands_send_appconf(packet_id, appconf);
mempools_free_appconf(appconf);
} break;
case COMM_SAMPLE_PRINT: {
uint16_t sample_len;
uint8_t decimation;
debug_sampling_mode mode;
int32_t ind = 0;
mode = data[ind++];
sample_len = buffer_get_uint16(data, &ind);
decimation = data[ind++];
bool raw = false;
if (len > (uint32_t)ind) {
raw = data[ind++];
}
mc_interface_sample_print_data(mode, sample_len, decimation, raw);
} break;
case COMM_REBOOT:
conf_general_store_backup_data();
// Lock the system and enter an infinite loop. The watchdog will reboot.
__disable_irq();
for(;;){};
break;
case COMM_ALIVE:
SHUTDOWN_RESET();
timeout_reset();
break;
case COMM_GET_DECODED_PPM: {
int32_t ind = 0;
uint8_t send_buffer[50];
send_buffer[ind++] = COMM_GET_DECODED_PPM;
buffer_append_int32(send_buffer, (int32_t)(app_ppm_get_decoded_level() * 1000000.0), &ind);
buffer_append_int32(send_buffer, (int32_t)(servodec_get_last_pulse_len(0) * 1000000.0), &ind);
reply_func(send_buffer, ind);
} break;
case COMM_GET_DECODED_ADC: {
int32_t ind = 0;
uint8_t send_buffer[50];
send_buffer[ind++] = COMM_GET_DECODED_ADC;
buffer_append_int32(send_buffer, (int32_t)(app_adc_get_decoded_level() * 1000000.0), &ind);
buffer_append_int32(send_buffer, (int32_t)(app_adc_get_voltage() * 1000000.0), &ind);
buffer_append_int32(send_buffer, (int32_t)(app_adc_get_decoded_level2() * 1000000.0), &ind);
buffer_append_int32(send_buffer, (int32_t)(app_adc_get_voltage2() * 1000000.0), &ind);
reply_func(send_buffer, ind);
} break;
case COMM_GET_DECODED_CHUK: {
int32_t ind = 0;
uint8_t send_buffer[50];
send_buffer[ind++] = COMM_GET_DECODED_CHUK;
buffer_append_int32(send_buffer, (int32_t)(app_nunchuk_get_decoded_chuk() * 1000000.0), &ind);
reply_func(send_buffer, ind);
} break;
case COMM_GET_DECODED_BALANCE: {
int32_t ind = 0;
uint8_t send_buffer[50];
send_buffer[ind++] = COMM_GET_DECODED_BALANCE;
buffer_append_int32(send_buffer, (int32_t)(app_balance_get_pid_output() * 1000000.0), &ind);
buffer_append_int32(send_buffer, (int32_t)(app_balance_get_pitch_angle() * 1000000.0), &ind);
buffer_append_int32(send_buffer, (int32_t)(app_balance_get_roll_angle() * 1000000.0), &ind);
buffer_append_uint32(send_buffer, app_balance_get_diff_time(), &ind);
buffer_append_int32(send_buffer, (int32_t)(app_balance_get_motor_current() * 1000000.0), &ind);
buffer_append_int32(send_buffer, (int32_t)(app_balance_get_debug1() * 1000000.0), &ind);
buffer_append_uint16(send_buffer, app_balance_get_state(), &ind);
buffer_append_uint16(send_buffer, app_balance_get_switch_state(), &ind);
buffer_append_int32(send_buffer, (int32_t)(app_balance_get_adc1() * 1000000.0), &ind);
buffer_append_int32(send_buffer, (int32_t)(app_balance_get_adc2() * 1000000.0), &ind);
buffer_append_int32(send_buffer, (int32_t)(app_balance_get_debug2() * 1000000.0), &ind);
reply_func(send_buffer, ind);
} break;
case COMM_FORWARD_CAN: {
send_func_can_fwd = reply_func;
#ifdef HW_HAS_DUAL_MOTORS
if (data[0] == utils_second_motor_id()) {
mc_interface_select_motor_thread(2);
commands_process_packet(data + 1, len - 1, reply_func);
mc_interface_select_motor_thread(1);
} else {
comm_can_send_buffer(data[0], data + 1, len - 1, 0);
}
#else
comm_can_send_buffer(data[0], data + 1, len - 1, 0);
#endif
} break;
case COMM_SET_CHUCK_DATA: {
chuck_data chuck_d_tmp;
int32_t ind = 0;
chuck_d_tmp.js_x = data[ind++];
chuck_d_tmp.js_y = data[ind++];
chuck_d_tmp.bt_c = data[ind++];
chuck_d_tmp.bt_z = data[ind++];
chuck_d_tmp.acc_x = buffer_get_int16(data, &ind);
chuck_d_tmp.acc_y = buffer_get_int16(data, &ind);
chuck_d_tmp.acc_z = buffer_get_int16(data, &ind);
if (len >= (unsigned int)ind + 2) {
chuck_d_tmp.rev_has_state = data[ind++];
chuck_d_tmp.is_rev = data[ind++];
} else {
chuck_d_tmp.rev_has_state = false;
chuck_d_tmp.is_rev = false;
}
app_nunchuk_update_output(&chuck_d_tmp);
} break;
case COMM_CUSTOM_APP_DATA:
if (appdata_func) {
appdata_func(data, len);
}
#ifdef USE_LISPBM
lispif_process_custom_app_data(data, len);
#endif
break;
case COMM_CUSTOM_HW_DATA:
if (hwdata_func) {
hwdata_func(data, len);
}
break;
case COMM_NRF_START_PAIRING: {
int32_t ind = 0;
nrf_driver_start_pairing(buffer_get_int32(data, &ind));
ind = 0;
uint8_t send_buffer[50];
send_buffer[ind++] = packet_id;
send_buffer[ind++] = NRF_PAIR_STARTED;
reply_func(send_buffer, ind);
} break;
case COMM_GPD_SET_FSW: {
timeout_reset();
int32_t ind = 0;
gpdrive_set_switching_frequency((float)buffer_get_int32(data, &ind));
} break;
case COMM_GPD_BUFFER_SIZE_LEFT: {
int32_t ind = 0;
uint8_t send_buffer[50];
send_buffer[ind++] = COMM_GPD_BUFFER_SIZE_LEFT;
buffer_append_int32(send_buffer, gpdrive_buffer_size_left(), &ind);
reply_func(send_buffer, ind);
} break;
case COMM_GPD_FILL_BUFFER: {
timeout_reset();
int32_t ind = 0;
while (ind < (int)len) {
gpdrive_add_buffer_sample(buffer_get_float32_auto(data, &ind));
}
} break;
case COMM_GPD_OUTPUT_SAMPLE: {
timeout_reset();
int32_t ind = 0;
gpdrive_output_sample(buffer_get_float32_auto(data, &ind));
} break;
case COMM_GPD_SET_MODE: {
timeout_reset();
int32_t ind = 0;
gpdrive_set_mode(data[ind++]);
} break;
case COMM_GPD_FILL_BUFFER_INT8: {
timeout_reset();
int32_t ind = 0;
while (ind < (int)len) {
gpdrive_add_buffer_sample_int((int8_t)data[ind++]);
}
} break;
case COMM_GPD_FILL_BUFFER_INT16: {
timeout_reset();
int32_t ind = 0;
while (ind < (int)len) {
gpdrive_add_buffer_sample_int(buffer_get_int16(data, &ind));
}
} break;
case COMM_GPD_SET_BUFFER_INT_SCALE: {
int32_t ind = 0;
gpdrive_set_buffer_int_scale(buffer_get_float32_auto(data, &ind));
} break;
case COMM_GET_VALUES_SETUP:
case COMM_GET_VALUES_SETUP_SELECTIVE: {
setup_values val = mc_interface_get_setup_values();
float wh_batt_left = 0.0;
float battery_level = mc_interface_get_battery_level(&wh_batt_left);
int32_t ind = 0;
chMtxLock(&send_buffer_mutex);
uint8_t *send_buffer = send_buffer_global;
send_buffer[ind++] = packet_id;
uint32_t mask = 0xFFFFFFFF;
if (packet_id == COMM_GET_VALUES_SETUP_SELECTIVE) {
int32_t ind2 = 0;
mask = buffer_get_uint32(data, &ind2);
buffer_append_uint32(send_buffer, mask, &ind);
}
if (mask & ((uint32_t)1 << 0)) {
buffer_append_float16(send_buffer, mc_interface_temp_fet_filtered(), 1e1, &ind);
}
if (mask & ((uint32_t)1 << 1)) {
buffer_append_float16(send_buffer, mc_interface_temp_motor_filtered(), 1e1, &ind);
}
if (mask & ((uint32_t)1 << 2)) {
buffer_append_float32(send_buffer, val.current_tot, 1e2, &ind);
}
if (mask & ((uint32_t)1 << 3)) {
buffer_append_float32(send_buffer, val.current_in_tot, 1e2, &ind);
}
if (mask & ((uint32_t)1 << 4)) {
buffer_append_float16(send_buffer, mc_interface_get_duty_cycle_now(), 1e3, &ind);
}
if (mask & ((uint32_t)1 << 5)) {
buffer_append_float32(send_buffer, mc_interface_get_rpm(), 1e0, &ind);
}
if (mask & ((uint32_t)1 << 6)) {
buffer_append_float32(send_buffer, mc_interface_get_speed(), 1e3, &ind);
}
if (mask & ((uint32_t)1 << 7)) {
buffer_append_float16(send_buffer, mc_interface_get_input_voltage_filtered(), 1e1, &ind);
}
if (mask & ((uint32_t)1 << 8)) {
buffer_append_float16(send_buffer, battery_level, 1e3, &ind);
}
if (mask & ((uint32_t)1 << 9)) {
buffer_append_float32(send_buffer, val.ah_tot, 1e4, &ind);
}
if (mask & ((uint32_t)1 << 10)) {
buffer_append_float32(send_buffer, val.ah_charge_tot, 1e4, &ind);
}
if (mask & ((uint32_t)1 << 11)) {
buffer_append_float32(send_buffer, val.wh_tot, 1e4, &ind);
}
if (mask & ((uint32_t)1 << 12)) {
buffer_append_float32(send_buffer, val.wh_charge_tot, 1e4, &ind);
}
if (mask & ((uint32_t)1 << 13)) {
buffer_append_float32(send_buffer, mc_interface_get_distance(), 1e3, &ind);
}
if (mask & ((uint32_t)1 << 14)) {
buffer_append_float32(send_buffer, mc_interface_get_distance_abs(), 1e3, &ind);
}
if (mask & ((uint32_t)1 << 15)) {
buffer_append_float32(send_buffer, mc_interface_get_pid_pos_now(), 1e6, &ind);
}
if (mask & ((uint32_t)1 << 16)) {
send_buffer[ind++] = mc_interface_get_fault();
}
if (mask & ((uint32_t)1 << 17)) {
uint8_t current_controller_id = app_get_configuration()->controller_id;
#ifdef HW_HAS_DUAL_MOTORS
if (mc_interface_get_motor_thread() == 2) {
current_controller_id = utils_second_motor_id();
}
#endif
send_buffer[ind++] = current_controller_id;
}
if (mask & ((uint32_t)1 << 18)) {
send_buffer[ind++] = val.num_vescs;
}
if (mask & ((uint32_t)1 << 19)) {
buffer_append_float32(send_buffer, wh_batt_left, 1e3, &ind);
}
if (mask & ((uint32_t)1 << 20)) {
buffer_append_uint32(send_buffer, mc_interface_get_odometer(), &ind);
}
if (mask & ((uint32_t)1 << 21)) {
buffer_append_uint32(send_buffer, chVTGetSystemTimeX() / (CH_CFG_ST_FREQUENCY / 1000), &ind);
}
reply_func(send_buffer, ind);
chMtxUnlock(&send_buffer_mutex);
} break;
case COMM_SET_ODOMETER: {
int32_t ind = 0;
mc_interface_set_odometer(buffer_get_uint32(data, &ind));
timeout_reset();
} break;
case COMM_SET_MCCONF_TEMP:
case COMM_SET_MCCONF_TEMP_SETUP: {
mc_configuration *mcconf = mempools_alloc_mcconf();
*mcconf = *mc_interface_get_configuration();
int32_t ind = 0;
bool store = data[ind++];
bool forward_can = data[ind++];
bool ack = data[ind++];
bool divide_by_controllers = data[ind++];
float controller_num = 1.0;
if (divide_by_controllers) {
for (int i = 0;i < CAN_STATUS_MSGS_TO_STORE;i++) {
can_status_msg *msg = comm_can_get_status_msg_index(i);
if (msg->id >= 0 && UTILS_AGE_S(msg->rx_time) < 0.1) {
controller_num += 1.0;
}
}
}
mcconf->l_current_min_scale = buffer_get_float32_auto(data, &ind);
mcconf->l_current_max_scale = buffer_get_float32_auto(data, &ind);
if (packet_id == COMM_SET_MCCONF_TEMP_SETUP) {
const float fact = ((mcconf->si_motor_poles / 2.0) * 60.0 *
mcconf->si_gear_ratio) / (mcconf->si_wheel_diameter * M_PI);
mcconf->l_min_erpm = buffer_get_float32_auto(data, &ind) * fact;
mcconf->l_max_erpm = buffer_get_float32_auto(data, &ind) * fact;
// Write computed RPM back and change forwarded packet id to
// COMM_SET_MCCONF_TEMP. This way only the master has to be
// aware of the setup information.
ind -= 8;
buffer_append_float32_auto(data, mcconf->l_min_erpm, &ind);
buffer_append_float32_auto(data, mcconf->l_max_erpm, &ind);
} else {
mcconf->l_min_erpm = buffer_get_float32_auto(data, &ind);
mcconf->l_max_erpm = buffer_get_float32_auto(data, &ind);
}
mcconf->l_min_duty = buffer_get_float32_auto(data, &ind);
mcconf->l_max_duty = buffer_get_float32_auto(data, &ind);
mcconf->l_watt_min = buffer_get_float32_auto(data, &ind) / controller_num;
mcconf->l_watt_max = buffer_get_float32_auto(data, &ind) / controller_num;
// Write divided data back to the buffer, as the other controllers have no way to tell
// how many controllers are on the bus and thus need pre-divided data.
// We set divide by controllers to false before forwarding.
ind -= 8;
buffer_append_float32_auto(data, mcconf->l_watt_min, &ind);
buffer_append_float32_auto(data, mcconf->l_watt_max, &ind);
// Battery limits can be set optionally in a backwards-compatible way.
if ((int32_t)len >= (ind + 8)) {
mcconf->l_in_current_min = buffer_get_float32_auto(data, &ind);
mcconf->l_in_current_max = buffer_get_float32_auto(data, &ind);
}
mcconf->lo_current_min = mcconf->l_current_min * mcconf->l_current_min_scale;
mcconf->lo_current_max = mcconf->l_current_max * mcconf->l_current_max_scale;
mcconf->lo_current_motor_min_now = mcconf->lo_current_min;
mcconf->lo_current_motor_max_now = mcconf->lo_current_max;
mcconf->lo_in_current_min = mcconf->l_in_current_min;
mcconf->lo_in_current_max = mcconf->l_in_current_max;
commands_apply_mcconf_hw_limits(mcconf);
if (store) {
conf_general_store_mc_configuration(mcconf, mc_interface_get_motor_thread() == 2);
}
mc_interface_set_configuration(mcconf);
if (forward_can) {