forked from vedderb/bldc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
commands.c
910 lines (773 loc) · 33.6 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
/*
Copyright 2012-2015 Benjamin Vedder [email protected]
This program 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.
This program 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/>.
*/
/*
* commands.c
*
* Created on: 19 sep 2014
* Author: benjamin
*/
#include "commands.h"
#include "ch.h"
#include "hal.h"
#include "mc_interface.h"
#include "stm32f4xx_conf.h"
#include "servo.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.h"
#include <math.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
// Threads
static THD_FUNCTION(detect_thread, arg);
static THD_WORKING_AREA(detect_thread_wa, 2048);
static thread_t *detect_tp;
// Private variables
static uint8_t send_buffer[PACKET_MAX_PL_LEN];
static float detect_cycle_int_limit;
static float detect_coupling_k;
static float detect_current;
static float detect_min_rpm;
static float detect_low_duty;
static int8_t detect_hall_table[8];
static int detect_hall_res;
static void(*send_func)(unsigned char *data, unsigned int len) = 0;
static void(*appdata_func)(unsigned char *data, unsigned int len) = 0;
static disp_pos_mode display_position_mode;
void commands_init(void) {
chThdCreateStatic(detect_thread_wa, sizeof(detect_thread_wa), NORMALPRIO, detect_thread, NULL);
}
/**
* Provide a function to use the next time there are packets to be sent.
*
* @param func
* A pointer to the packet sending function.
*/
void commands_set_send_func(void(*func)(unsigned char *data, unsigned int len)) {
send_func = func;
}
/**
* 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);
}
}
/**
* 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) {
if (!len) {
return;
}
COMM_PACKET_ID packet_id;
int32_t ind = 0;
uint16_t sample_len;
uint8_t decimation;
bool at_start;
static mc_configuration mcconf, mcconf_old; // Static to save some stack space
app_configuration appconf;
uint16_t flash_res;
uint32_t new_app_offset;
chuck_data chuck_d_tmp;
char *sub;
packet_id = data[0];
sub = "wheelLB";
data++;
len--;
switch (packet_id) {
case COMM_FW_VERSION:
ind = 0;
send_buffer[ind++] = COMM_FW_VERSION;
send_buffer[ind++] = FW_VERSION_MAJOR;
send_buffer[ind++] = FW_VERSION_MINOR;
commands_send_packet(send_buffer, ind);
break;
case COMM_JUMP_TO_BOOTLOADER:
flash_helper_jump_to_bootloader();
break;
case COMM_ERASE_NEW_APP:
ind = 0;
flash_res = flash_helper_erase_new_app(buffer_get_uint32(data, &ind));
ind = 0;
send_buffer[ind++] = COMM_ERASE_NEW_APP;
send_buffer[ind++] = flash_res == FLASH_COMPLETE ? 1 : 0;
commands_send_packet(send_buffer, ind);
break;
case COMM_WRITE_NEW_APP_DATA:
ind = 0;
new_app_offset = buffer_get_uint32(data, &ind);
flash_res = flash_helper_write_new_app_data(new_app_offset, data + ind, len - ind);
ind = 0;
send_buffer[ind++] = COMM_WRITE_NEW_APP_DATA;
send_buffer[ind++] = flash_res == FLASH_COMPLETE ? 1 : 0;
commands_send_packet(send_buffer, ind);
break;
case COMM_GET_VALUES:
ind = 0;
send_buffer[ind++] = COMM_GET_VALUES;
buffer_append_float16(send_buffer, NTC_TEMP(ADC_IND_TEMP_MOS1), 1e1, &ind);
buffer_append_float16(send_buffer, NTC_TEMP(ADC_IND_TEMP_MOS2), 1e1, &ind);
buffer_append_float16(send_buffer, NTC_TEMP(ADC_IND_TEMP_MOS3), 1e1, &ind);
buffer_append_float16(send_buffer, NTC_TEMP(ADC_IND_TEMP_MOS4), 1e1, &ind);
buffer_append_float16(send_buffer, NTC_TEMP(ADC_IND_TEMP_MOS5), 1e1, &ind);
buffer_append_float16(send_buffer, NTC_TEMP(ADC_IND_TEMP_MOS6), 1e1, &ind);
buffer_append_float16(send_buffer, NTC_TEMP(ADC_IND_TEMP_PCB), 1e1, &ind);
buffer_append_float32(send_buffer, mc_interface_read_reset_avg_motor_current(), 1e2, &ind);
buffer_append_float32(send_buffer, mc_interface_read_reset_avg_input_current(), 1e2, &ind);
buffer_append_float16(send_buffer, mc_interface_get_duty_cycle_now(), 1e3, &ind);
buffer_append_float32(send_buffer, mc_interface_get_rpm(), 1e0, &ind);
buffer_append_float16(send_buffer, GET_INPUT_VOLTAGE(), 1e1, &ind);
buffer_append_float32(send_buffer, mc_interface_get_amp_hours(false), 1e4, &ind);
buffer_append_float32(send_buffer, mc_interface_get_amp_hours_charged(false), 1e4, &ind);
buffer_append_float32(send_buffer, mc_interface_get_watt_hours(false), 1e4, &ind);
buffer_append_float32(send_buffer, mc_interface_get_watt_hours_charged(false), 1e4, &ind);
buffer_append_int32(send_buffer, mc_interface_get_tachometer_value(false), &ind);
buffer_append_int32(send_buffer, mc_interface_get_tachometer_abs_value(false), &ind);
send_buffer[ind++] = mc_interface_get_fault();
// TODO: send FOC values id, iq, abs
commands_send_packet(send_buffer, ind);
break;
case COMM_SET_DUTY:
ind = 0;
mc_interface_set_duty((float)buffer_get_int32(data, &ind) / 100000.0);
timeout_reset();
break;
case COMM_SET_CURRENT:
ind = 0;
mc_interface_set_current((float)buffer_get_int32(data, &ind) / 1000.0);
timeout_reset();
break;
case COMM_SET_CURRENT_BRAKE:
ind = 0;
mc_interface_set_brake_current((float)buffer_get_int32(data, &ind) / 1000.0);
timeout_reset();
break;
case COMM_SET_RPM:
ind = 0;
mc_interface_set_pid_speed((float)buffer_get_int32(data, &ind));
timeout_reset();
break;
case COMM_SET_POS:
ind = 0;
mc_interface_set_pid_pos((float)buffer_get_int32(data, &ind) / 1000000.0);
timeout_reset();
break;
case COMM_SET_DETECT:
mcconf = *mc_interface_get_configuration();
ind = 0;
display_position_mode = data[ind++];
if (mcconf.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:
#if SERVO_OUT_ENABLE
ind = 0;
#if SERVO_OUT_SIMPLE
servo_simple_set_output(buffer_get_float16(data, 1000.0, &ind));
#else
servos[0].pos = (int16_t)(buffer_get_float16(data, 1000.0, &ind) * 255.0);
#endif
#endif
break;
case COMM_SET_MCCONF:
mcconf = *mc_interface_get_configuration();
ind = 0;
mcconf.pwm_mode = data[ind++];
mcconf.comm_mode = data[ind++];
mcconf.motor_type = data[ind++];
mcconf.sensor_mode = data[ind++];
mcconf.l_current_max = buffer_get_float32(data, 1000.0, &ind);
mcconf.l_current_min = buffer_get_float32(data, 1000.0, &ind);
mcconf.l_in_current_max = buffer_get_float32(data, 1000.0, &ind);
mcconf.l_in_current_min = buffer_get_float32(data, 1000.0, &ind);
mcconf.l_abs_current_max = buffer_get_float32(data, 1000.0, &ind);
mcconf.l_min_erpm = buffer_get_float32(data, 1000.0, &ind);
mcconf.l_max_erpm = buffer_get_float32(data, 1000.0, &ind);
mcconf.l_max_erpm_fbrake = buffer_get_float32(data, 1000.0, &ind);
mcconf.l_max_erpm_fbrake_cc = buffer_get_float32(data, 1000.0, &ind);
mcconf.l_min_vin = buffer_get_float32(data, 1000.0, &ind);
mcconf.l_max_vin = buffer_get_float32(data, 1000.0, &ind);
mcconf.l_battery_cut_start = buffer_get_float32(data, 1000.0, &ind);
mcconf.l_battery_cut_end = buffer_get_float32(data, 1000.0, &ind);
mcconf.l_slow_abs_current = data[ind++];
mcconf.l_rpm_lim_neg_torque = data[ind++];
mcconf.l_temp_fet_start = buffer_get_float32(data, 1000.0, &ind);
mcconf.l_temp_fet_end = buffer_get_float32(data, 1000.0, &ind);
mcconf.l_temp_motor_start = buffer_get_float32(data, 1000.0, &ind);
mcconf.l_temp_motor_end = buffer_get_float32(data, 1000.0, &ind);
mcconf.l_min_duty = buffer_get_float32(data, 1000000.0, &ind);
mcconf.l_max_duty = buffer_get_float32(data, 1000000.0, &ind);
mcconf.lo_current_max = mcconf.l_current_max;
mcconf.lo_current_min = mcconf.l_current_min;
mcconf.lo_in_current_max = mcconf.l_in_current_max;
mcconf.lo_in_current_min = mcconf.l_in_current_min;
mcconf.sl_min_erpm = (float)buffer_get_int32(data, &ind) / 1000.0;
mcconf.sl_min_erpm_cycle_int_limit = (float)buffer_get_int32(data, &ind) / 1000.0;
mcconf.sl_max_fullbreak_current_dir_change = (float)buffer_get_int32(data, &ind) / 1000.0;
mcconf.sl_cycle_int_limit = (float)buffer_get_int32(data, &ind) / 1000.0;
mcconf.sl_phase_advance_at_br = (float)buffer_get_int32(data, &ind) / 1000.0;
mcconf.sl_cycle_int_rpm_br = (float)buffer_get_int32(data, &ind) / 1000.0;
mcconf.sl_bemf_coupling_k = (float)buffer_get_int32(data, &ind) / 1000.0;
memcpy(mcconf.hall_table, data + ind, 8);
ind += 8;
mcconf.hall_sl_erpm = (float)buffer_get_int32(data, &ind) / 1000.0;
mcconf.foc_current_kp = buffer_get_float32(data, 1e5, &ind);
mcconf.foc_current_ki = buffer_get_float32(data, 1e5, &ind);
mcconf.foc_f_sw = buffer_get_float32(data, 1e3, &ind);
mcconf.foc_dt_us = buffer_get_float32(data, 1e6, &ind);
mcconf.foc_encoder_inverted = data[ind++];
mcconf.foc_encoder_offset = buffer_get_float32(data, 1e3, &ind);
mcconf.foc_encoder_ratio = buffer_get_float32(data, 1e3, &ind);
mcconf.foc_sensor_mode = data[ind++];
mcconf.foc_pll_kp = buffer_get_float32(data, 1e3, &ind);
mcconf.foc_pll_ki = buffer_get_float32(data, 1e3, &ind);
mcconf.foc_motor_l = buffer_get_float32(data, 1e8, &ind);
mcconf.foc_motor_r = buffer_get_float32(data, 1e5, &ind);
mcconf.foc_motor_flux_linkage = buffer_get_float32(data, 1e5, &ind);
mcconf.foc_observer_gain = buffer_get_float32(data, 1e0, &ind);
mcconf.foc_duty_dowmramp_kp = buffer_get_float32(data, 1e3, &ind);
mcconf.foc_duty_dowmramp_ki = buffer_get_float32(data, 1e3, &ind);
mcconf.foc_openloop_rpm = buffer_get_float32(data, 1e3, &ind);
mcconf.foc_sl_openloop_hyst = buffer_get_float32(data, 1e3, &ind);
mcconf.foc_sl_openloop_time = buffer_get_float32(data, 1e3, &ind);
mcconf.foc_sl_d_current_duty = buffer_get_float32(data, 1e3, &ind);
mcconf.foc_sl_d_current_factor = buffer_get_float32(data, 1e3, &ind);
memcpy(mcconf.foc_hall_table, data + ind, 8);
ind += 8;
mcconf.foc_sl_erpm = (float)buffer_get_int32(data, &ind) / 1000.0;
mcconf.s_pid_kp = (float)buffer_get_int32(data, &ind) / 1000000.0;
mcconf.s_pid_ki = (float)buffer_get_int32(data, &ind) / 1000000.0;
mcconf.s_pid_kd = (float)buffer_get_int32(data, &ind) / 1000000.0;
mcconf.s_pid_min_erpm = (float)buffer_get_int32(data, &ind) / 1000.0;
mcconf.p_pid_kp = (float)buffer_get_int32(data, &ind) / 1000000.0;
mcconf.p_pid_ki = (float)buffer_get_int32(data, &ind) / 1000000.0;
mcconf.p_pid_kd = (float)buffer_get_int32(data, &ind) / 1000000.0;
mcconf.p_pid_ang_div = (float)buffer_get_int32(data, &ind) / 100000.0;
mcconf.cc_startup_boost_duty = (float)buffer_get_int32(data, &ind) / 1000000.0;
mcconf.cc_min_current = (float)buffer_get_int32(data, &ind) / 1000.0;
mcconf.cc_gain = (float)buffer_get_int32(data, &ind) / 1000000.0;
mcconf.cc_ramp_step_max = buffer_get_float32(data, 1e6, &ind);
mcconf.m_fault_stop_time_ms = buffer_get_int32(data, &ind);
mcconf.m_duty_ramp_step = (float)buffer_get_float32(data, 1000000.0, &ind);
mcconf.m_duty_ramp_step_rpm_lim = (float)buffer_get_float32(data, 1000000.0, &ind);
mcconf.m_current_backoff_gain = (float)buffer_get_float32(data, 1000000.0, &ind);
mcconf.m_encoder_counts = buffer_get_uint32(data, &ind);
mcconf.m_sensor_port_mode = data[ind++];
conf_general_store_mc_configuration(&mcconf);
mc_interface_set_configuration(&mcconf);
ind = 0;
send_buffer[ind++] = packet_id;
commands_send_packet(send_buffer, ind);
break;
case COMM_GET_MCCONF:
case COMM_GET_MCCONF_DEFAULT:
if (packet_id == COMM_GET_MCCONF) {
mcconf = *mc_interface_get_configuration();
} else {
conf_general_get_default_mc_configuration(&mcconf);
}
ind = 0;
send_buffer[ind++] = packet_id;
send_buffer[ind++] = mcconf.pwm_mode;
send_buffer[ind++] = mcconf.comm_mode;
send_buffer[ind++] = mcconf.motor_type;
send_buffer[ind++] = mcconf.sensor_mode;
buffer_append_int32(send_buffer, (int32_t)(mcconf.l_current_max * 1000.0), &ind);
buffer_append_int32(send_buffer, (int32_t)(mcconf.l_current_min * 1000.0), &ind);
buffer_append_int32(send_buffer, (int32_t)(mcconf.l_in_current_max * 1000.0), &ind);
buffer_append_int32(send_buffer, (int32_t)(mcconf.l_in_current_min * 1000.0), &ind);
buffer_append_int32(send_buffer, (int32_t)(mcconf.l_abs_current_max * 1000.0), &ind);
buffer_append_int32(send_buffer, (int32_t)(mcconf.l_min_erpm * 1000.0), &ind);
buffer_append_int32(send_buffer, (int32_t)(mcconf.l_max_erpm * 1000.0), &ind);
buffer_append_int32(send_buffer, (int32_t)(mcconf.l_max_erpm_fbrake * 1000.0), &ind);
buffer_append_int32(send_buffer, (int32_t)(mcconf.l_max_erpm_fbrake_cc * 1000.0), &ind);
buffer_append_int32(send_buffer, (int32_t)(mcconf.l_min_vin * 1000.0), &ind);
buffer_append_int32(send_buffer, (int32_t)(mcconf.l_max_vin * 1000.0), &ind);
buffer_append_float32(send_buffer, mcconf.l_battery_cut_start, 1000.0, &ind);
buffer_append_float32(send_buffer, mcconf.l_battery_cut_end, 1000.0, &ind);
send_buffer[ind++] = mcconf.l_slow_abs_current;
send_buffer[ind++] = mcconf.l_rpm_lim_neg_torque;
buffer_append_int32(send_buffer, (int32_t)(mcconf.l_temp_fet_start * 1000.0), &ind);
buffer_append_int32(send_buffer, (int32_t)(mcconf.l_temp_fet_end * 1000.0), &ind);
buffer_append_int32(send_buffer, (int32_t)(mcconf.l_temp_motor_start * 1000.0), &ind);
buffer_append_int32(send_buffer, (int32_t)(mcconf.l_temp_motor_end * 1000.0), &ind);
buffer_append_int32(send_buffer, (int32_t)(mcconf.l_min_duty * 1000000.0), &ind);
buffer_append_int32(send_buffer, (int32_t)(mcconf.l_max_duty * 1000000.0), &ind);
buffer_append_int32(send_buffer, (int32_t)(mcconf.sl_min_erpm * 1000.0), &ind);
buffer_append_int32(send_buffer, (int32_t)(mcconf.sl_min_erpm_cycle_int_limit * 1000.0), &ind);
buffer_append_int32(send_buffer, (int32_t)(mcconf.sl_max_fullbreak_current_dir_change * 1000.0), &ind);
buffer_append_int32(send_buffer, (int32_t)(mcconf.sl_cycle_int_limit * 1000.0), &ind);
buffer_append_int32(send_buffer, (int32_t)(mcconf.sl_phase_advance_at_br * 1000.0), &ind);
buffer_append_int32(send_buffer, (int32_t)(mcconf.sl_cycle_int_rpm_br * 1000.0), &ind);
buffer_append_int32(send_buffer, (int32_t)(mcconf.sl_bemf_coupling_k * 1000.0), &ind);
memcpy(send_buffer + ind, mcconf.hall_table, 8);
ind += 8;
buffer_append_int32(send_buffer, (int32_t)(mcconf.hall_sl_erpm * 1000.0), &ind);
buffer_append_float32(send_buffer, mcconf.foc_current_kp, 1e5, &ind);
buffer_append_float32(send_buffer, mcconf.foc_current_ki, 1e5, &ind);
buffer_append_float32(send_buffer, mcconf.foc_f_sw, 1e3, &ind);
buffer_append_float32(send_buffer, mcconf.foc_dt_us, 1e6, &ind);
send_buffer[ind++] = mcconf.foc_encoder_inverted;
buffer_append_float32(send_buffer, mcconf.foc_encoder_offset, 1e3, &ind);
buffer_append_float32(send_buffer, mcconf.foc_encoder_ratio, 1e3, &ind);
send_buffer[ind++] = mcconf.foc_sensor_mode;
buffer_append_float32(send_buffer, mcconf.foc_pll_kp, 1e3, &ind);
buffer_append_float32(send_buffer, mcconf.foc_pll_ki, 1e3, &ind);
buffer_append_float32(send_buffer, mcconf.foc_motor_l, 1e8, &ind);
buffer_append_float32(send_buffer, mcconf.foc_motor_r, 1e5, &ind);
buffer_append_float32(send_buffer, mcconf.foc_motor_flux_linkage, 1e5, &ind);
buffer_append_float32(send_buffer, mcconf.foc_observer_gain, 1e0, &ind);
buffer_append_float32(send_buffer, mcconf.foc_duty_dowmramp_kp, 1e3, &ind);
buffer_append_float32(send_buffer, mcconf.foc_duty_dowmramp_ki, 1e3, &ind);
buffer_append_float32(send_buffer, mcconf.foc_openloop_rpm, 1e3, &ind);
buffer_append_float32(send_buffer, mcconf.foc_sl_openloop_hyst, 1e3, &ind);
buffer_append_float32(send_buffer, mcconf.foc_sl_openloop_time, 1e3, &ind);
buffer_append_float32(send_buffer, mcconf.foc_sl_d_current_duty, 1e3, &ind);
buffer_append_float32(send_buffer, mcconf.foc_sl_d_current_factor, 1e3, &ind);
memcpy(send_buffer + ind, mcconf.foc_hall_table, 8);
ind += 8;
buffer_append_int32(send_buffer, (int32_t)(mcconf.foc_sl_erpm * 1000.0), &ind);
buffer_append_int32(send_buffer, (int32_t)(mcconf.s_pid_kp * 1000000.0), &ind);
buffer_append_int32(send_buffer, (int32_t)(mcconf.s_pid_ki * 1000000.0), &ind);
buffer_append_int32(send_buffer, (int32_t)(mcconf.s_pid_kd * 1000000.0), &ind);
buffer_append_int32(send_buffer, (int32_t)(mcconf.s_pid_min_erpm * 1000.0), &ind);
buffer_append_int32(send_buffer, (int32_t)(mcconf.p_pid_kp * 1000000.0), &ind);
buffer_append_int32(send_buffer, (int32_t)(mcconf.p_pid_ki * 1000000.0), &ind);
buffer_append_int32(send_buffer, (int32_t)(mcconf.p_pid_kd * 1000000.0), &ind);
buffer_append_float32(send_buffer, mcconf.p_pid_ang_div, 1e5, &ind);
buffer_append_int32(send_buffer, (int32_t)(mcconf.cc_startup_boost_duty * 1000000.0), &ind);
buffer_append_int32(send_buffer, (int32_t)(mcconf.cc_min_current * 1000.0), &ind);
buffer_append_int32(send_buffer, (int32_t)(mcconf.cc_gain * 1000000.0), &ind);
buffer_append_int32(send_buffer, (int32_t)(mcconf.cc_ramp_step_max * 1000000.0), &ind);
buffer_append_int32(send_buffer, mcconf.m_fault_stop_time_ms, &ind);
buffer_append_float32(send_buffer, mcconf.m_duty_ramp_step, 1000000.0, &ind);
buffer_append_float32(send_buffer, mcconf.m_duty_ramp_step_rpm_lim, 1000000.0, &ind);
buffer_append_float32(send_buffer, mcconf.m_current_backoff_gain, 1000000.0, &ind);
buffer_append_uint32(send_buffer, mcconf.m_encoder_counts, &ind);
send_buffer[ind++] = mcconf.m_sensor_port_mode;
commands_send_packet(send_buffer, ind);
break;
case COMM_SET_APPCONF:
appconf = *app_get_configuration();
ind = 0;
appconf.controller_id = data[ind++];
appconf.timeout_msec = buffer_get_uint32(data, &ind);
appconf.timeout_brake_current = (float)buffer_get_int32(data, &ind) / 1000.0;
appconf.send_can_status = data[ind++];
appconf.send_can_status_rate_hz = buffer_get_uint16(data, &ind);
appconf.app_to_use = data[ind++];
appconf.app_ppm_conf.ctrl_type = data[ind++];
appconf.app_ppm_conf.pid_max_erpm = (float)buffer_get_int32(data, &ind) / 1000.0;
appconf.app_ppm_conf.hyst = (float)buffer_get_int32(data, &ind) / 1000.0;
appconf.app_ppm_conf.pulse_start = (float)buffer_get_int32(data, &ind) / 1000.0;
appconf.app_ppm_conf.pulse_end = (float)buffer_get_int32(data, &ind) / 1000.0;
appconf.app_ppm_conf.median_filter = data[ind++];
appconf.app_ppm_conf.safe_start = data[ind++];
appconf.app_ppm_conf.rpm_lim_start = (float)buffer_get_int32(data, &ind) / 1000.0;
appconf.app_ppm_conf.rpm_lim_end = (float)buffer_get_int32(data, &ind) / 1000.0;
appconf.app_ppm_conf.multi_esc = data[ind++];
appconf.app_ppm_conf.tc = data[ind++];
appconf.app_ppm_conf.tc_max_diff = (float)buffer_get_int32(data, &ind) / 1000.0;
appconf.app_adc_conf.ctrl_type = data[ind++];
appconf.app_adc_conf.hyst = (float)buffer_get_int32(data, &ind) / 1000.0;
appconf.app_adc_conf.voltage_start = (float)buffer_get_int32(data, &ind) / 1000.0;
appconf.app_adc_conf.voltage_end = (float)buffer_get_int32(data, &ind) / 1000.0;
appconf.app_adc_conf.use_filter = data[ind++];
appconf.app_adc_conf.safe_start = data[ind++];
appconf.app_adc_conf.cc_button_inverted = data[ind++];
appconf.app_adc_conf.rev_button_inverted = data[ind++];
appconf.app_adc_conf.voltage_inverted = data[ind++];
appconf.app_adc_conf.rpm_lim_start = (float)buffer_get_int32(data, &ind) / 1000.0;
appconf.app_adc_conf.rpm_lim_end = (float)buffer_get_int32(data, &ind) / 1000.0;
appconf.app_adc_conf.multi_esc = data[ind++];
appconf.app_adc_conf.tc = data[ind++];
appconf.app_adc_conf.tc_max_diff = (float)buffer_get_int32(data, &ind) / 1000.0;
appconf.app_adc_conf.update_rate_hz = buffer_get_uint16(data, &ind);
appconf.app_uart_baudrate = buffer_get_uint32(data, &ind);
appconf.app_chuk_conf.ctrl_type = data[ind++];
appconf.app_chuk_conf.hyst = buffer_get_float32(data, 1000.0, &ind);
appconf.app_chuk_conf.rpm_lim_start = buffer_get_float32(data, 1000.0, &ind);
appconf.app_chuk_conf.rpm_lim_end = buffer_get_float32(data, 1000.0, &ind);
appconf.app_chuk_conf.ramp_time_pos = buffer_get_float32(data, 1000.0, &ind);
appconf.app_chuk_conf.ramp_time_neg = buffer_get_float32(data, 1000.0, &ind);
appconf.app_chuk_conf.stick_erpm_per_s_in_cc = buffer_get_float32(data, 1000.0, &ind);
appconf.app_chuk_conf.multi_esc = data[ind++];
appconf.app_chuk_conf.tc = data[ind++];
appconf.app_chuk_conf.tc_max_diff = buffer_get_float32(data, 1000.0, &ind);
appconf.app_nrf_conf.speed = data[ind++];
appconf.app_nrf_conf.power = data[ind++];
appconf.app_nrf_conf.crc_type = data[ind++];
appconf.app_nrf_conf.retry_delay = data[ind++];
appconf.app_nrf_conf.retries = data[ind++];
appconf.app_nrf_conf.channel = data[ind++];
memcpy(appconf.app_nrf_conf.address, data + ind, 3);
ind += 3;
appconf.app_nrf_conf.send_crc_ack = data[ind++];
conf_general_store_app_configuration(&appconf);
app_set_configuration(&appconf);
timeout_configure(appconf.timeout_msec, appconf.timeout_brake_current);
ind = 0;
send_buffer[ind++] = packet_id;
commands_send_packet(send_buffer, ind);
break;
case COMM_GET_APPCONF:
case COMM_GET_APPCONF_DEFAULT:
if (packet_id == COMM_GET_APPCONF) {
appconf = *app_get_configuration();
} else {
conf_general_get_default_app_configuration(&appconf);
}
ind = 0;
send_buffer[ind++] = packet_id;
send_buffer[ind++] = appconf.controller_id;
buffer_append_uint32(send_buffer, appconf.timeout_msec, &ind);
buffer_append_int32(send_buffer, (int32_t)(appconf.timeout_brake_current * 1000.0), &ind);
send_buffer[ind++] = appconf.send_can_status;
buffer_append_uint16(send_buffer, appconf.send_can_status_rate_hz, &ind);
send_buffer[ind++] = appconf.app_to_use;
send_buffer[ind++] = appconf.app_ppm_conf.ctrl_type;
buffer_append_int32(send_buffer, (int32_t)(appconf.app_ppm_conf.pid_max_erpm * 1000.0), &ind);
buffer_append_int32(send_buffer, (int32_t)(appconf.app_ppm_conf.hyst * 1000.0), &ind);
buffer_append_int32(send_buffer, (int32_t)(appconf.app_ppm_conf.pulse_start * 1000.0), &ind);
buffer_append_int32(send_buffer, (int32_t)(appconf.app_ppm_conf.pulse_end * 1000.0), &ind);
send_buffer[ind++] = appconf.app_ppm_conf.median_filter;
send_buffer[ind++] = appconf.app_ppm_conf.safe_start;
buffer_append_int32(send_buffer, (int32_t)(appconf.app_ppm_conf.rpm_lim_start * 1000.0), &ind);
buffer_append_int32(send_buffer, (int32_t)(appconf.app_ppm_conf.rpm_lim_end * 1000.0), &ind);
send_buffer[ind++] = appconf.app_ppm_conf.multi_esc;
send_buffer[ind++] = appconf.app_ppm_conf.tc;
buffer_append_int32(send_buffer, (int32_t)(appconf.app_ppm_conf.tc_max_diff * 1000.0), &ind);
send_buffer[ind++] = appconf.app_adc_conf.ctrl_type;
buffer_append_int32(send_buffer, (int32_t)(appconf.app_adc_conf.hyst * 1000.0), &ind);
buffer_append_int32(send_buffer, (int32_t)(appconf.app_adc_conf.voltage_start * 1000.0), &ind);
buffer_append_int32(send_buffer, (int32_t)(appconf.app_adc_conf.voltage_end * 1000.0), &ind);
send_buffer[ind++] = appconf.app_adc_conf.use_filter;
send_buffer[ind++] = appconf.app_adc_conf.safe_start;
send_buffer[ind++] = appconf.app_adc_conf.cc_button_inverted;
send_buffer[ind++] = appconf.app_adc_conf.rev_button_inverted;
send_buffer[ind++] = appconf.app_adc_conf.voltage_inverted;
buffer_append_int32(send_buffer, (int32_t)(appconf.app_adc_conf.rpm_lim_start * 1000.0), &ind);
buffer_append_int32(send_buffer, (int32_t)(appconf.app_adc_conf.rpm_lim_end * 1000.0), &ind);
send_buffer[ind++] = appconf.app_adc_conf.multi_esc;
send_buffer[ind++] = appconf.app_adc_conf.tc;
buffer_append_int32(send_buffer, (int32_t)(appconf.app_adc_conf.tc_max_diff * 1000.0), &ind);
buffer_append_uint16(send_buffer, appconf.app_adc_conf.update_rate_hz, &ind);
buffer_append_uint32(send_buffer, appconf.app_uart_baudrate, &ind);
send_buffer[ind++] = appconf.app_chuk_conf.ctrl_type;
buffer_append_float32(send_buffer, appconf.app_chuk_conf.hyst, 1000.0, &ind);
buffer_append_float32(send_buffer, appconf.app_chuk_conf.rpm_lim_start, 1000.0, &ind);
buffer_append_float32(send_buffer, appconf.app_chuk_conf.rpm_lim_end, 1000.0, &ind);
buffer_append_float32(send_buffer, appconf.app_chuk_conf.ramp_time_pos, 1000.0, &ind);
buffer_append_float32(send_buffer, appconf.app_chuk_conf.ramp_time_neg, 1000.0, &ind);
buffer_append_float32(send_buffer, appconf.app_chuk_conf.stick_erpm_per_s_in_cc, 1000.0, &ind);
send_buffer[ind++] = appconf.app_chuk_conf.multi_esc;
send_buffer[ind++] = appconf.app_chuk_conf.tc;
buffer_append_int32(send_buffer, (int32_t)(appconf.app_chuk_conf.tc_max_diff * 1000.0), &ind);
send_buffer[ind++] = appconf.app_nrf_conf.speed;
send_buffer[ind++] = appconf.app_nrf_conf.power;
send_buffer[ind++] = appconf.app_nrf_conf.crc_type;
send_buffer[ind++] = appconf.app_nrf_conf.retry_delay;
send_buffer[ind++] = appconf.app_nrf_conf.retries;
send_buffer[ind++] = appconf.app_nrf_conf.channel;
memcpy(send_buffer + ind, appconf.app_nrf_conf.address, 3);
ind += 3;
send_buffer[ind++] = appconf.app_nrf_conf.send_crc_ack;
commands_send_packet(send_buffer, ind);
break;
case COMM_SAMPLE_PRINT:
ind = 0;
at_start = data[ind++];
sample_len = buffer_get_uint16(data, &ind);
decimation = data[ind++];
mc_interface_sample_print_data(at_start, sample_len, decimation);
break;
case COMM_TERMINAL_CMD:
data[len] = '\0';
terminal_process_string((char*)data);
break;
case COMM_ROTOR_POSITION:
commands_send_rotor_pos(encoder_read_deg());
break;
case COMM_DETECT_MOTOR_PARAM:
ind = 0;
detect_current = buffer_get_float32(data, 1e3, &ind);
detect_min_rpm = buffer_get_float32(data, 1e3, &ind);
detect_low_duty = buffer_get_float32(data, 1e3, &ind);
chEvtSignal(detect_tp, (eventmask_t) 1);
break;
case COMM_DETECT_MOTOR_R_L: {
mcconf = *mc_interface_get_configuration();
mcconf_old = mcconf;
mcconf.motor_type = MOTOR_TYPE_FOC;
mc_interface_set_configuration(&mcconf);
float r = 0.0;
float l = 0.0;
bool res = mcpwm_foc_measure_res_ind(&r, &l);
mc_interface_set_configuration(&mcconf_old);
if (!res) {
r = 0.0;
l = 0.0;
}
ind = 0;
send_buffer[ind++] = COMM_DETECT_MOTOR_R_L;
buffer_append_float32(send_buffer, r, 1e6, &ind);
buffer_append_float32(send_buffer, l, 1e3, &ind);
commands_send_packet(send_buffer, ind);
}
break;
case COMM_DETECT_MOTOR_FLUX_LINKAGE: {
ind = 0;
float current = buffer_get_float32(data, 1e3, &ind);
float min_rpm = buffer_get_float32(data, 1e3, &ind);
float duty = buffer_get_float32(data, 1e3, &ind);
float resistance = buffer_get_float32(data, 1e6, &ind);
float linkage;
bool res = conf_general_measure_flux_linkage(current, duty, min_rpm, resistance, &linkage);
if (!res) {
linkage = 0.0;
}
ind = 0;
send_buffer[ind++] = COMM_DETECT_MOTOR_FLUX_LINKAGE;
buffer_append_float32(send_buffer, linkage, 1e7, &ind);
commands_send_packet(send_buffer, ind);
}
break;
case COMM_DETECT_ENCODER: {
if (encoder_is_configured()) {
mcconf = *mc_interface_get_configuration();
mcconf_old = mcconf;
ind = 0;
float current = buffer_get_float32(data, 1e3, &ind);
mcconf.motor_type = MOTOR_TYPE_FOC;
mcconf.foc_f_sw = 10000.0;
mcconf.foc_current_kp = 0.01;
mcconf.foc_current_ki = 10.0;
mc_interface_set_configuration(&mcconf);
float offset = 0.0;
float ratio = 0.0;
bool inverted = false;
mcpwm_foc_encoder_detect(current, false, &offset, &ratio, &inverted);
mc_interface_set_configuration(&mcconf_old);
ind = 0;
send_buffer[ind++] = COMM_DETECT_ENCODER;
buffer_append_float32(send_buffer, offset, 1e6, &ind);
buffer_append_float32(send_buffer, ratio, 1e6, &ind);
send_buffer[ind++] = inverted;
commands_send_packet(send_buffer, ind);
} else {
ind = 0;
send_buffer[ind++] = COMM_DETECT_ENCODER;
buffer_append_float32(send_buffer, 1001.0, 1e6, &ind);
buffer_append_float32(send_buffer, 0.0, 1e6, &ind);
send_buffer[ind++] = false;
commands_send_packet(send_buffer, ind);
}
}
break;
case COMM_DETECT_HALL_FOC: {
mcconf = *mc_interface_get_configuration();
if (mcconf.m_sensor_port_mode == SENSOR_PORT_MODE_HALL) {
mcconf_old = mcconf;
ind = 0;
float current = buffer_get_float32(data, 1e3, &ind);
mcconf.motor_type = MOTOR_TYPE_FOC;
mcconf.foc_f_sw = 10000.0;
mcconf.foc_current_kp = 0.01;
mcconf.foc_current_ki = 10.0;
mc_interface_set_configuration(&mcconf);
uint8_t hall_tab[8];
bool res = mcpwm_foc_hall_detect(current, hall_tab);
mc_interface_set_configuration(&mcconf_old);
ind = 0;
send_buffer[ind++] = COMM_DETECT_HALL_FOC;
memcpy(send_buffer + ind, hall_tab, 8);
ind += 8;
send_buffer[ind++] = res ? 0 : 1;
commands_send_packet(send_buffer, ind);
} else {
ind = 0;
send_buffer[ind++] = COMM_DETECT_HALL_FOC;
memset(send_buffer, 255, 8);
ind += 8;
send_buffer[ind++] = 0;
}
}
break;
case COMM_REBOOT:
// Lock the system and enter an infinite loop. The watchdog will reboot.
__disable_irq();
for(;;){};
break;
case COMM_ALIVE:
timeout_reset();
break;
case COMM_GET_DECODED_PPM:
ind = 0;
send_buffer[ind++] = COMM_GET_DECODED_PPM;
buffer_append_int32(send_buffer, (int32_t)(servodec_get_servo(0) * 1000000.0), &ind);
buffer_append_int32(send_buffer, (int32_t)(servodec_get_last_pulse_len(0) * 1000000.0), &ind);
commands_send_packet(send_buffer, ind);
break;
case COMM_GET_DECODED_ADC:
ind = 0;
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);
commands_send_packet(send_buffer, ind);
break;
case COMM_GET_DECODED_CHUK:
ind = 0;
send_buffer[ind++] = COMM_GET_DECODED_CHUK;
buffer_append_int32(send_buffer, (int32_t)(app_nunchuk_get_decoded_chuk() * 1000000.0), &ind);
commands_send_packet(send_buffer, ind);
break;
case COMM_FORWARD_CAN:
comm_can_send_buffer(data[0], data + 1, len - 1, false);
break;
case COMM_SET_CHUCK_DATA:
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);
app_nunchuk_update_output(&chuck_d_tmp);
break;
case COMM_CUSTOM_APP_DATA:
if (appdata_func) {
appdata_func(data, len);
}
break;
case COMM_GET_SUBSCRIBED:
ind = 0;
send_buffer[ind++] = COMM_GET_SUBSCRIBED;
strcpy(send_buffer+ind, sub);
commands_send_packet(send_buffer, strlen(sub)+1);
break;
default:
break;
}
}
void commands_printf(char* format, ...) {
va_list arg;
va_start (arg, format);
int len;
static char print_buffer[255];
print_buffer[0] = COMM_PRINT;
len = vsnprintf(print_buffer+1, 254, format, arg);
va_end (arg);
if(len > 0) {
commands_send_packet((unsigned char*)print_buffer, (len<254)? len+1: 255);
}
}
void commands_send_samples(uint8_t *data, int len) {
uint8_t buffer[len + 1];
int index = 0;
buffer[index++] = COMM_SAMPLE_PRINT;
for (int i = 0;i < len;i++) {
buffer[index++] = data[i];
}
commands_send_packet(buffer, index);
}
void commands_send_rotor_pos(float rotor_pos) {
uint8_t buffer[5];
int32_t index = 0;
buffer[index++] = COMM_ROTOR_POSITION;
buffer_append_int32(buffer, (int32_t)(rotor_pos * 100000.0), &index);
commands_send_packet(buffer, index);
}
void commands_send_experiment_samples(float *samples, int len) {
if ((len * 4 + 1) > 256) {
return;
}
uint8_t buffer[len * 4 + 1];
int32_t index = 0;
buffer[index++] = COMM_EXPERIMENT_SAMPLE;
for (int i = 0;i < len;i++) {
buffer_append_int32(buffer, (int32_t)(samples[i] * 10000.0), &index);
}
commands_send_packet(buffer, index);
}
disp_pos_mode commands_get_disp_pos_mode(void) {
return display_position_mode;
}
void commands_set_app_data_handler(void(*func)(unsigned char *data, unsigned int len)) {
appdata_func = func;
}
void commands_send_app_data(unsigned char *data, unsigned int len) {
int32_t index = 0;
send_buffer[index++] = COMM_CUSTOM_APP_DATA;
memcpy(send_buffer + index, data, len);
index += len;
commands_send_packet(send_buffer, index);
}
static THD_FUNCTION(detect_thread, arg) {
(void)arg;
chRegSetThreadName("Detect");
detect_tp = chThdGetSelfX();
for(;;) {
chEvtWaitAny((eventmask_t) 1);
if (!conf_general_detect_motor_param(detect_current, detect_min_rpm,
detect_low_duty, &detect_cycle_int_limit, &detect_coupling_k,
detect_hall_table, &detect_hall_res)) {
detect_cycle_int_limit = 0.0;
detect_coupling_k = 0.0;
}
int32_t ind = 0;
send_buffer[ind++] = COMM_DETECT_MOTOR_PARAM;
buffer_append_int32(send_buffer, (int32_t)(detect_cycle_int_limit * 1000.0), &ind);
buffer_append_int32(send_buffer, (int32_t)(detect_coupling_k * 1000.0), &ind);
memcpy(send_buffer + ind, detect_hall_table, 8);
ind += 8;
send_buffer[ind++] = detect_hall_res;
commands_send_packet(send_buffer, ind);
}
}