forked from vedderb/vesc_tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.cpp
executable file
·2334 lines (1989 loc) · 60.6 KB
/
commands.cpp
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 - 2019 Benjamin Vedder [email protected]
This file is part of VESC Tool.
VESC Tool 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.
VESC Tool 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 <QDebug>
#include <QEventLoop>
Commands::Commands(QObject *parent) : QObject(parent)
{
mSendCan = false;
mCanId = -1;
mIsLimitedMode = false;
mLimitedSupportsFwdAllCan = false;
mLimitedSupportsEraseBootloader = false;
mMaxPowerLossBug = false;
mCheckNextMcConfig = false;
mTimer = new QTimer(this);
mTimer->setInterval(10);
mTimer->start();
mMcConfig = nullptr;
mAppConfig = nullptr;
mTimeoutCount = 100;
mTimeoutFwVer = 0;
mTimeoutMcconf = 0;
mTimeoutAppconf = 0;
mTimeoutValues = 0;
mTimeoutValuesSetup = 0;
mTimeoutImuData = 0;
mTimeoutDecPpm = 0;
mTimeoutDecAdc = 0;
mTimeoutDecChuk = 0;
mTimeoutDecBalance = 0;
mTimeoutPingCan = 0;
mTimeoutCustomConf = 0;
mTimeoutBmsVal = 0;
connect(mTimer, SIGNAL(timeout()), this, SLOT(timerSlot()));
}
void Commands::setLimitedMode(bool is_limited)
{
mIsLimitedMode = is_limited;
}
bool Commands::isLimitedMode()
{
return mIsLimitedMode;
}
bool Commands::setSendCan(bool sendCan, int id)
{
if (id >= 0) {
mCanId = id;
}
if (mCanId >= 0) {
mSendCan = sendCan;
} else {
mSendCan = false;
}
return mSendCan == sendCan;
}
bool Commands::getSendCan()
{
return mSendCan;
}
void Commands::setCanSendId(unsigned int id)
{
mCanId = int(id);
}
int Commands::getCanSendId()
{
return mCanId;
}
void Commands::processPacket(QByteArray data)
{
VByteArray vb(data);
COMM_PACKET_ID id = COMM_PACKET_ID(vb.vbPopFrontUint8());
switch (id) {
case COMM_FW_VERSION: {
mTimeoutFwVer = 0;
FW_RX_PARAMS params;
if (vb.size() >= 2) {
params.major = vb.vbPopFrontInt8();
params.minor = vb.vbPopFrontInt8();
params.hw = vb.vbPopFrontString();
}
if (vb.size() >= 12) {
params.uuid.append(vb.left(12));
vb.remove(0, 12);
}
if (vb.size() >= 1) {
params.isPaired = vb.vbPopFrontInt8();
}
if (vb.size() >= 1) {
params.isTestFw = vb.vbPopFrontInt8();
}
if (vb.size() >= 1) {
params.hwType = HW_TYPE(vb.vbPopFrontInt8());
}
if (vb.size() >= 1) {
params.customConfigNum = vb.vbPopFrontInt8();
}
if (vb.size() >= 1) {
params.hasPhaseFilters = vb.vbPopFrontInt8();
}
if (vb.size() >= 2) {
auto qmlHw = vb.vbPopFrontInt8();
auto qmlApp = vb.vbPopFrontInt8();
params.hasQmlHw = qmlHw > 0;
params.qmlHwFullscreen = qmlHw == 2;
params.hasQmlApp = qmlApp > 0;
params.qmlAppFullscreen = qmlApp == 2;
}
if (vb.size() >= 1) {
auto nrfFlags = vb.vbPopFrontUint8();
params.nrfNameSupported = nrfFlags & 1;
params.nrfPinSupported = nrfFlags & 2;
}
emit fwVersionReceived(params);
} break;
case COMM_ERASE_NEW_APP:
emit eraseNewAppResReceived(vb.at(0));
break;
case COMM_WRITE_NEW_APP_DATA: {
bool ok = vb.vbPopFrontInt8();
bool hasOffset = false;
quint32 offset = 0;
if (vb.size() >= 4) {
hasOffset = true;
offset = vb.vbPopFrontUint32();
}
emit writeNewAppDataResReceived(ok, hasOffset, offset);
} break;
case COMM_ERASE_BOOTLOADER:
emit eraseBootloaderResReceived(vb.at(0));
break;
case COMM_GET_VALUES:
case COMM_GET_VALUES_SELECTIVE: {
mTimeoutValues = 0;
MC_VALUES values;
uint32_t mask = 0xFFFFFFFF;
if (id == COMM_GET_VALUES_SELECTIVE) {
mask = vb.vbPopFrontUint32();
}
if (mask & (uint32_t(1) << 0)) {
values.temp_mos = vb.vbPopFrontDouble16(1e1);
}
if (mask & (uint32_t(1) << 1)) {
values.temp_motor = vb.vbPopFrontDouble16(1e1);
}
if (mask & (uint32_t(1) << 2)) {
values.current_motor = vb.vbPopFrontDouble32(1e2);
}
if (mask & (uint32_t(1) << 3)) {
values.current_in = vb.vbPopFrontDouble32(1e2);
}
if (mask & (uint32_t(1) << 4)) {
values.id = vb.vbPopFrontDouble32(1e2);
}
if (mask & (uint32_t(1) << 5)) {
values.iq = vb.vbPopFrontDouble32(1e2);
}
if (mask & (uint32_t(1) << 6)) {
values.duty_now = vb.vbPopFrontDouble16(1e3);
}
if (mask & (uint32_t(1) << 7)) {
values.rpm = vb.vbPopFrontDouble32(1e0);
}
if (mask & (uint32_t(1) << 8)) {
values.v_in = vb.vbPopFrontDouble16(1e1);
}
if (mask & (uint32_t(1) << 9)) {
values.amp_hours = vb.vbPopFrontDouble32(1e4);
}
if (mask & (uint32_t(1) << 10)) {
values.amp_hours_charged = vb.vbPopFrontDouble32(1e4);
}
if (mask & (uint32_t(1) << 11)) {
values.watt_hours = vb.vbPopFrontDouble32(1e4);
}
if (mask & (uint32_t(1) << 12)) {
values.watt_hours_charged = vb.vbPopFrontDouble32(1e4);
}
if (mask & (uint32_t(1) << 13)) {
values.tachometer = vb.vbPopFrontInt32();
}
if (mask & (uint32_t(1) << 14)) {
values.tachometer_abs = vb.vbPopFrontInt32();
}
if (mask & (uint32_t(1) << 15)) {
values.fault_code = mc_fault_code(vb.vbPopFrontInt8());
values.fault_str = faultToStr(values.fault_code);
}
if (vb.size() >= 4) {
if (mask & (uint32_t(1) << 16)) {
values.position = vb.vbPopFrontDouble32(1e6);
}
} else {
values.position = -1.0;
}
if (vb.size() >= 1) {
if (mask & (uint32_t(1) << 17)) {
values.vesc_id = vb.vbPopFrontUint8();
}
} else {
values.vesc_id = 255;
}
if (vb.size() >= 6) {
if (mask & (uint32_t(1) << 18)) {
values.temp_mos_1 = vb.vbPopFrontDouble16(1e1);
values.temp_mos_2 = vb.vbPopFrontDouble16(1e1);
values.temp_mos_3 = vb.vbPopFrontDouble16(1e1);
}
}
if (vb.size() >= 8) {
if (mask & (uint32_t(1) << 19)) {
values.vd = vb.vbPopFrontDouble32(1e3);
}
if (mask & (uint32_t(1) << 20)) {
values.vq = vb.vbPopFrontDouble32(1e3);
}
}
if (vb.size() >= 1) {
if (mask & (uint32_t(1) << 21)) {
quint8 status = vb.vbPopFrontUint8();
values.has_timeout = status & 1;
values.kill_sw_active = (status >> 1) & 1;
}
}
emit valuesReceived(values, mask);
} break;
case COMM_PRINT:
emit printReceived(QString::fromLatin1(vb));
break;
case COMM_SAMPLE_PRINT:
emit samplesReceived(vb);
break;
case COMM_ROTOR_POSITION:
emit rotorPosReceived(vb.vbPopFrontDouble32(1e5));
break;
case COMM_EXPERIMENT_SAMPLE: {
QVector<double> samples;
while (!vb.isEmpty()) {
samples.append(vb.vbPopFrontDouble32(1e4));
}
emit experimentSamplesReceived(samples);
} break;
case COMM_GET_MCCONF:
case COMM_GET_MCCONF_DEFAULT:
mTimeoutMcconf = 0;
if (mMcConfig) {
if (mMcConfig->deSerialize(vb)) {
mMcConfig->updateDone();
if (mCheckNextMcConfig) {
mCheckNextMcConfig = false;
emit mcConfigCheckResult(mMcConfig->checkDifference(&mMcConfigLast));
}
} else {
emit deserializeConfigFailed(true, false);
}
}
break;
case COMM_GET_APPCONF:
case COMM_GET_APPCONF_DEFAULT:
mTimeoutAppconf = 0;
if (mAppConfig) {
if (mAppConfig->deSerialize(vb)) {
mAppConfig->updateDone();
} else {
emit deserializeConfigFailed(false, true);
}
}
break;
case COMM_DETECT_MOTOR_PARAM: {
bldc_detect param;
param.cycle_int_limit = vb.vbPopFrontDouble32(1e3);
param.bemf_coupling_k = vb.vbPopFrontDouble32(1e3);
for (int i = 0;i < 8;i++) {
param.hall_table.append(int(vb.vbPopFrontUint8()));
}
param.hall_res = int(vb.vbPopFrontUint8());
emit bldcDetectReceived(param);
} break;
case COMM_DETECT_MOTOR_R_L: {
double r = vb.vbPopFrontDouble32(1e6);
double l = vb.vbPopFrontDouble32(1e3);
double ld_lq_diff = 0.0;
if (vb.size() >= 4) {
ld_lq_diff = vb.vbPopFrontDouble32(1e3);
}
emit motorRLReceived(r, l, ld_lq_diff);
} break;
case COMM_DETECT_MOTOR_FLUX_LINKAGE: {
emit motorLinkageReceived(vb.vbPopFrontDouble32(1e7));
} break;
case COMM_DETECT_ENCODER: {
double offset = vb.vbPopFrontDouble32(1e6);
double ratio = vb.vbPopFrontDouble32(1e6);
bool inverted = vb.vbPopFrontInt8();
emit encoderParamReceived(offset, ratio, inverted);
} break;
case COMM_DETECT_HALL_FOC: {
QVector<int> table;
for (int i = 0;i < 8;i++) {
table.append(vb.vbPopFrontUint8());
}
int res = vb.vbPopFrontUint8();
emit focHallTableReceived(table, res);
} break;
case COMM_GET_DECODED_PPM: {
mTimeoutDecPpm = 0;
double dec_ppm = vb.vbPopFrontDouble32(1e6);
double ppm_last_len = vb.vbPopFrontDouble32(1e6);
emit decodedPpmReceived(dec_ppm, ppm_last_len);
} break;
case COMM_GET_DECODED_ADC: {
mTimeoutDecAdc = 0;
double dec_adc = vb.vbPopFrontDouble32(1e6);
double dec_adc_voltage = vb.vbPopFrontDouble32(1e6);
double dec_adc2 = vb.vbPopFrontDouble32(1e6);
double dec_adc_voltage2 = vb.vbPopFrontDouble32(1e6);
emit decodedAdcReceived(dec_adc, dec_adc_voltage, dec_adc2, dec_adc_voltage2);
} break;
case COMM_GET_DECODED_CHUK:
mTimeoutDecChuk = 0;
emit decodedChukReceived(vb.vbPopFrontDouble32(1000000.0));
break;
case COMM_GET_DECODED_BALANCE: {
mTimeoutDecBalance = 0;
BALANCE_VALUES values;
values.pid_output = vb.vbPopFrontDouble32(1e6);
values.pitch_angle = vb.vbPopFrontDouble32(1e6);
values.roll_angle = vb.vbPopFrontDouble32(1e6);
values.diff_time = vb.vbPopFrontUint32();
values.motor_current = vb.vbPopFrontDouble32(1e6);
values.debug1 = vb.vbPopFrontDouble32(1e6);
values.state = vb.vbPopFrontUint16();
values.switch_value = vb.vbPopFrontUint16();
values.adc1 = vb.vbPopFrontDouble32(1e6);
values.adc2 = vb.vbPopFrontDouble32(1e6);
values.debug2 = vb.vbPopFrontDouble32(1e6);
emit decodedBalanceReceived(values);
} break;
case COMM_SET_MCCONF:
emit ackReceived("MCCONF Write OK");
break;
case COMM_SET_APPCONF:
emit ackReceived("APPCONF Write OK");
break;
case COMM_CUSTOM_APP_DATA:
emit customAppDataReceived(vb);
break;
case COMM_CUSTOM_HW_DATA:
emit customHwDataReceived(vb);
break;
case COMM_NRF_START_PAIRING:
emit nrfPairingRes(NRF_PAIR_RES(vb.vbPopFrontInt8()));
break;
case COMM_GPD_BUFFER_NOTIFY:
emit gpdBufferNotifyReceived();
break;
case COMM_GPD_BUFFER_SIZE_LEFT:
emit gpdBufferSizeLeftReceived(vb.vbPopFrontInt16());
break;
case COMM_GET_VALUES_SETUP:
case COMM_GET_VALUES_SETUP_SELECTIVE: {
mTimeoutValuesSetup = 0;
SETUP_VALUES values;
uint32_t mask = 0xFFFFFFFF;
if (id == COMM_GET_VALUES_SETUP_SELECTIVE) {
mask = vb.vbPopFrontUint32();
}
if (mask & (uint32_t(1) << 0)) {
values.temp_mos = vb.vbPopFrontDouble16(1e1);
}
if (mask & (uint32_t(1) << 1)) {
values.temp_motor = vb.vbPopFrontDouble16(1e1);
}
if (mask & (uint32_t(1) << 2)) {
values.current_motor = vb.vbPopFrontDouble32(1e2);
}
if (mask & (uint32_t(1) << 3)) {
values.current_in = vb.vbPopFrontDouble32(1e2);
}
if (mask & (uint32_t(1) << 4)) {
values.duty_now = vb.vbPopFrontDouble16(1e3);
}
if (mask & (uint32_t(1) << 5)) {
values.rpm = vb.vbPopFrontDouble32(1e0);
}
if (mask & (uint32_t(1) << 6)) {
values.speed = vb.vbPopFrontDouble32(1e3);
}
if (mask & (uint32_t(1) << 7)) {
values.v_in = vb.vbPopFrontDouble16(1e1);
}
if (mask & (uint32_t(1) << 8)) {
values.battery_level = vb.vbPopFrontDouble16(1e3);
}
if (mask & (uint32_t(1) << 9)) {
values.amp_hours = vb.vbPopFrontDouble32(1e4);
}
if (mask & (uint32_t(1) << 10)) {
values.amp_hours_charged = vb.vbPopFrontDouble32(1e4);
}
if (mask & (uint32_t(1) << 11)) {
values.watt_hours = vb.vbPopFrontDouble32(1e4);
}
if (mask & (uint32_t(1) << 12)) {
values.watt_hours_charged = vb.vbPopFrontDouble32(1e4);
}
if (mask & (uint32_t(1) << 13)) {
values.tachometer = vb.vbPopFrontDouble32(1e3);
}
if (mask & (uint32_t(1) << 14)) {
values.tachometer_abs = vb.vbPopFrontDouble32(1e3);
}
if (mask & (uint32_t(1) << 15)) {
values.position = vb.vbPopFrontDouble32(1e6);
}
if (mask & (uint32_t(1) << 16)) {
values.fault_code = mc_fault_code(vb.vbPopFrontInt8());
values.fault_str = faultToStr(values.fault_code);
}
if (mask & (uint32_t(1) << 17)) {
values.vesc_id = vb.vbPopFrontUint8();
}
if (mask & (uint32_t(1) << 18)) {
values.num_vescs = vb.vbPopFrontUint8();
}
if (mask & (uint32_t(1) << 19)) {
values.battery_wh = vb.vbPopFrontDouble32(1e3);
}
if (mask & (uint32_t(1) << 20)) {
values.odometer = vb.vbPopFrontUint32();
}
if (mask & (uint32_t(1) << 21)) {
values.uptime_ms = vb.vbPopFrontUint32();
}
emit valuesSetupReceived(values, mask);
} break;
case COMM_SET_MCCONF_TEMP:
emit ackReceived("COMM_SET_MCCONF_TEMP Write OK");
break;
case COMM_SET_MCCONF_TEMP_SETUP:
emit ackReceived("COMM_SET_MCCONF_TEMP_SETUP Write OK");
break;
case COMM_DETECT_MOTOR_FLUX_LINKAGE_OPENLOOP:
emit motorLinkageReceived(vb.vbPopFrontDouble32(1e7));
break;
case COMM_DETECT_APPLY_ALL_FOC:
emit detectAllFocReceived(vb.vbPopFrontInt16());
break;
case COMM_PING_CAN: {
mTimeoutPingCan = 0;
QVector<int> devs;
while(vb.size() > 0) {
devs.append(vb.vbPopFrontUint8());
}
emit pingCanRx(devs, false);
} break;
case COMM_GET_IMU_DATA: {
mTimeoutImuData = 0;
IMU_VALUES values;
uint32_t mask = vb.vbPopFrontUint16();
if (mask & (uint32_t(1) << 0)) {
values.roll = vb.vbPopFrontDouble32Auto();
}
if (mask & (uint32_t(1) << 1)) {
values.pitch = vb.vbPopFrontDouble32Auto();
}
if (mask & (uint32_t(1) << 2)) {
values.yaw = vb.vbPopFrontDouble32Auto();
}
if (mask & (uint32_t(1) << 3)) {
values.accX = vb.vbPopFrontDouble32Auto();
}
if (mask & (uint32_t(1) << 4)) {
values.accY = vb.vbPopFrontDouble32Auto();
}
if (mask & (uint32_t(1) << 5)) {
values.accZ = vb.vbPopFrontDouble32Auto();
}
if (mask & (uint32_t(1) << 6)) {
values.gyroX = vb.vbPopFrontDouble32Auto();
}
if (mask & (uint32_t(1) << 7)) {
values.gyroY = vb.vbPopFrontDouble32Auto();
}
if (mask & (uint32_t(1) << 8)) {
values.gyroZ = vb.vbPopFrontDouble32Auto();
}
if (mask & (uint32_t(1) << 9)) {
values.magX = vb.vbPopFrontDouble32Auto();
}
if (mask & (uint32_t(1) << 10)) {
values.magY = vb.vbPopFrontDouble32Auto();
}
if (mask & (uint32_t(1) << 11)) {
values.magZ = vb.vbPopFrontDouble32Auto();
}
if (mask & (uint32_t(1) << 12)) {
values.q0 = vb.vbPopFrontDouble32Auto();
}
if (mask & (uint32_t(1) << 13)) {
values.q1 = vb.vbPopFrontDouble32Auto();
}
if (mask & (uint32_t(1) << 14)) {
values.q2 = vb.vbPopFrontDouble32Auto();
}
if (mask & (uint32_t(1) << 15)) {
values.q3 = vb.vbPopFrontDouble32Auto();
}
if (vb.size() >= 1) {
if (mask & (uint32_t(1) << 16)) {
values.vesc_id = vb.vbPopFrontUint8();
}
}
emit valuesImuReceived(values, mask);
} break;
case COMM_GET_IMU_CALIBRATION: {
QVector<double> cal;
for (int i = 0;i < 9;i++) {
cal.append(vb.vbPopFrontDouble32(1e6));
}
emit imuCalibrationReceived(cal);
} break;
case COMM_BM_CONNECT:
emit bmConnRes(vb.vbPopFrontInt16());
break;
case COMM_BM_ERASE_FLASH_ALL:
emit bmEraseFlashAllRes(vb.vbPopFrontInt16());
break;
case COMM_BM_WRITE_FLASH:
case COMM_BM_WRITE_FLASH_LZO:
emit bmWriteFlashRes(vb.vbPopFrontInt16());
break;
case COMM_BM_REBOOT:
emit bmRebootRes(vb.vbPopFrontInt16());
break;
case COMM_BM_DISCONNECT:
emit ackReceived("COMM_BM_DISCONNECT OK");
break;
case COMM_BM_MAP_PINS_DEFAULT:
emit bmMapPinsDefaultRes(vb.vbPopFrontInt16());
break;
case COMM_BM_MAP_PINS_NRF5X:
emit bmMapPinsNrf5xRes(vb.vbPopFrontInt16());
break;
case COMM_PLOT_INIT: {
QString xL = vb.vbPopFrontString();
QString yL = vb.vbPopFrontString();
emit plotInitReceived(xL, yL);
} break;
case COMM_PLOT_DATA: {
double x = vb.vbPopFrontDouble32Auto();
double y = vb.vbPopFrontDouble32Auto();
emit plotDataReceived(x, y);
} break;
case COMM_PLOT_ADD_GRAPH: {
emit plotAddGraphReceived(vb.vbPopFrontString());
} break;
case COMM_PLOT_SET_GRAPH: {
emit plotSetGraphReceived(vb.vbPopFrontInt8());
} break;
case COMM_BM_MEM_READ: {
int res = vb.vbPopFrontInt16();
emit bmReadMemRes(res, vb);
} break;
case COMM_CAN_FWD_FRAME: {
quint32 id = vb.vbPopFrontUint32();
bool isExtended = vb.vbPopFrontInt8();
emit canFrameRx(vb, id, isExtended);
} break;
case COMM_SET_BATTERY_CUT:
emit ackReceived("COMM_SET_BATTERY_CUT Write OK");
break;
case COMM_BMS_GET_VALUES: {
mTimeoutBmsVal = 0;
BMS_VALUES val;
val.v_tot = vb.vbPopFrontDouble32(1e6);
val.v_charge = vb.vbPopFrontDouble32(1e6);
val.i_in = vb.vbPopFrontDouble32(1e6);
val.i_in_ic = vb.vbPopFrontDouble32(1e6);
val.ah_cnt = vb.vbPopFrontDouble32(1e3);
val.wh_cnt = vb.vbPopFrontDouble32(1e3);
int cells = vb.vbPopFrontUint8();
for (int i = 0;i < cells;i++) {
val.v_cells.append(vb.vbPopFrontDouble16(1e3));
}
for (int i = 0;i < cells;i++) {
val.is_balancing.append(vb.vbPopFrontUint8());
}
int sensors = vb.vbPopFrontUint8();
for (int i = 0;i < sensors;i++) {
val.temps.append(vb.vbPopFrontDouble16(1e2));
}
val.temp_ic = vb.vbPopFrontDouble16(1e2);
val.temp_hum_sensor = vb.vbPopFrontDouble16(1e2);
val.humidity = vb.vbPopFrontDouble16(1e2);
val.temp_cells_highest = vb.vbPopFrontDouble16(1e2);
if (vb.size() >= 2) {
val.soc = vb.vbPopFrontDouble16(1e3);
}
if (vb.size() >= 2) {
val.soh = vb.vbPopFrontDouble16(1e3);
}
if (vb.size() >= 1) {
val.can_id = vb.vbPopFrontUint8();
}
if (vb.size() >= 16) {
val.ah_cnt_chg_total = vb.vbPopFrontDouble32Auto();
val.wh_cnt_chg_total = vb.vbPopFrontDouble32Auto();
val.ah_cnt_dis_total = vb.vbPopFrontDouble32Auto();
val.wh_cnt_dis_total = vb.vbPopFrontDouble32Auto();
}
val.updateTimeStamp();
emit bmsValuesRx(val);
} break;
case COMM_SET_CUSTOM_CONFIG:
emit ackReceived("COMM_SET_CUSTOM_CONFIG Write OK");
break;
case COMM_GET_CUSTOM_CONFIG:
case COMM_GET_CUSTOM_CONFIG_DEFAULT: {
mTimeoutCustomConf = 0;
int confInd = vb.vbPopFrontInt8();
emit customConfigRx(confInd, vb);
} break;
case COMM_GET_CUSTOM_CONFIG_XML: {
int confInd = vb.vbPopFrontInt8();
int confSize = vb.vbPopFrontInt32();
int offset = vb.vbPopFrontInt32();
emit customConfigChunkRx(confInd, confSize, offset, vb);
} break;
case COMM_PSW_GET_STATUS: {
PSW_STATUS stat;
stat.id = vb.vbPopFrontInt16();
stat.psws_num = vb.vbPopFrontInt16();
stat.age_seconds = vb.vbPopFrontDouble32Auto();
stat.v_in = vb.vbPopFrontDouble32Auto();
stat.v_out = vb.vbPopFrontDouble32Auto();
stat.temp = vb.vbPopFrontDouble32Auto();
stat.is_out_on = vb.vbPopFrontInt8();
stat.is_pch_on = vb.vbPopFrontInt8();
stat.is_dsc_on = vb.vbPopFrontInt8();
emit pswStatusRx(stat);
} break;
case COMM_BMS_FWD_CAN_RX: {
int id = vb.vbPopFrontUint8();
CAN_PACKET_ID cmd = CAN_PACKET_ID(vb.vbPopFrontUint8());
BMS_VALUES &val = mBmsValues[id];
switch (cmd) {
case CAN_PACKET_BMS_SOC_SOH_TEMP_STAT: {
vb.vbPopFrontDouble16(1e3); // V_CELL_MIN
vb.vbPopFrontDouble16(1e3); // V_CELL_MAX
val.can_id = id;
val.soc = double(vb.vbPopFrontUint8()) / 255.0;
val.soh = double(vb.vbPopFrontUint8()) / 255.0;
val.temp_cells_highest = double(vb.vbPopFrontUint8());
val.updateTimeStamp();
} break;
case CAN_PACKET_BMS_V_TOT: {
val.can_id = id;
val.v_tot = vb.vbPopFrontDouble32Auto();
val.v_charge = vb.vbPopFrontDouble32Auto();
val.updateTimeStamp();
} break;
case CAN_PACKET_BMS_I: {
val.can_id = id;
val.i_in = vb.vbPopFrontDouble32Auto();
val.i_in_ic = vb.vbPopFrontDouble32Auto();
val.updateTimeStamp();
} break;
case CAN_PACKET_BMS_AH_WH: {
val.can_id = id;
val.ah_cnt = vb.vbPopFrontDouble32Auto();
val.wh_cnt = vb.vbPopFrontDouble32Auto();
val.updateTimeStamp();
} break;
case CAN_PACKET_BMS_V_CELL: {
val.can_id = id;
int ofs = vb.vbPopFrontUint8();
val.v_cells.resize(vb.vbPopFrontUint8());
while (vb.size() > 1) {
val.v_cells[ofs++] = vb.vbPopFrontDouble16(1e3);
}
val.updateTimeStamp();
} break;
case CAN_PACKET_BMS_BAL: {
val.can_id = id;
uint64_t bal_state_0 = vb.vbPopFrontUint32();
int cell_num = (bal_state_0 >> 24) & 0xFF;
bal_state_0 &= 0x00FFFFFF;
uint64_t bal_state_1 = vb.vbPopFrontUint32();
uint64_t bal_state = bal_state_0 << 32 | bal_state_1;
int32_t ind = 0;
val.is_balancing.resize(cell_num);
while (ind < cell_num) {
val.is_balancing[ind] = (bal_state >> ind) & 1;
ind++;
}
val.updateTimeStamp();
} break;
case CAN_PACKET_BMS_TEMPS: {
val.can_id = id;
unsigned int ofs = vb.vbPopFrontUint8();
val.temps.resize(vb.vbPopFrontUint8());
while (vb.size() > 1) {
val.temps[ofs++] = vb.vbPopFrontDouble16(1e2);
}
val.updateTimeStamp();
} break;
case CAN_PACKET_BMS_HUM: {
val.can_id = id;
val.temp_hum_sensor = vb.vbPopFrontDouble16(1e2);
val.humidity = vb.vbPopFrontDouble16(1e2);
val.temp_ic = vb.vbPopFrontDouble16(1e2);
val.updateTimeStamp();
} break;
case CAN_PACKET_BMS_AH_WH_CHG_TOTAL: {
val.can_id = id;
val.ah_cnt_chg_total = vb.vbPopFrontDouble32Auto();
val.wh_cnt_chg_total = vb.vbPopFrontDouble32Auto();
val.updateTimeStamp();
} break;
case CAN_PACKET_BMS_AH_WH_DIS_TOTAL: {
val.can_id = id;
val.ah_cnt_dis_total = vb.vbPopFrontDouble32Auto();
val.wh_cnt_dis_total = vb.vbPopFrontDouble32Auto();
val.updateTimeStamp();
} break;
default:
break;
}
} break;
case COMM_GET_QML_UI_HW: {
int qmlSize = vb.vbPopFrontInt32();
int offset = vb.vbPopFrontInt32();
emit qmluiHwRx(qmlSize, offset, vb);
} break;
case COMM_GET_QML_UI_APP: {
int qmlSize = vb.vbPopFrontInt32();
int offset = vb.vbPopFrontInt32();
emit qmluiAppRx(qmlSize, offset, vb);
} break;
case COMM_QMLUI_ERASE:
emit eraseQmluiResReceived(vb.at(0));
break;
case COMM_QMLUI_WRITE: {
bool ok = vb.vbPopFrontInt8();
quint32 offset = vb.vbPopFrontUint32();
emit writeQmluiResReceived(ok, offset);
} break;
case COMM_IO_BOARD_GET_ALL: {
IO_BOARD_VALUES val;
val.id = vb.vbPopFrontInt16();
while (vb.size() > 0) {
int type = vb.vbPopFrontInt8();
switch (type) {
case 1: {
val.adc_1_4_age = vb.vbPopFrontDouble32Auto();
val.adc_1_4.append(vb.vbPopFrontDouble16(1e2));
val.adc_1_4.append(vb.vbPopFrontDouble16(1e2));
val.adc_1_4.append(vb.vbPopFrontDouble16(1e2));
val.adc_1_4.append(vb.vbPopFrontDouble16(1e2));
} break;
case 2: {
val.adc_5_8_age = vb.vbPopFrontDouble32Auto();
val.adc_5_8.append(vb.vbPopFrontDouble16(1e2));
val.adc_5_8.append(vb.vbPopFrontDouble16(1e2));
val.adc_5_8.append(vb.vbPopFrontDouble16(1e2));
val.adc_5_8.append(vb.vbPopFrontDouble16(1e2));
} break;
case 3: {
val.digital_age = vb.vbPopFrontDouble32Auto();
uint64_t hi = vb.vbPopFrontUint32();
uint64_t lo = vb.vbPopFrontUint32();
uint64_t combined = (hi << 32) | lo;
for (int i = 0;i < 64;i++) {
val.digital.append((combined >> i) & 1);
}
} break;
default:
break;
}
}
emit ioBoardValRx(val);
} break;
case COMM_GET_STATS: {
mTimeoutStats = 0;
STAT_VALUES values;
uint32_t mask = vb.vbPopFrontUint32();
if (mask & ((uint32_t)1 << 0)) { values.speed_avg = vb.vbPopFrontDouble32Auto(); }
if (mask & ((uint32_t)1 << 1)) { values.speed_max = vb.vbPopFrontDouble32Auto(); }
if (mask & ((uint32_t)1 << 2)) { values.power_avg = vb.vbPopFrontDouble32Auto(); }
if (mask & ((uint32_t)1 << 3)) { values.power_max = vb.vbPopFrontDouble32Auto(); }
if (mask & ((uint32_t)1 << 4)) { values.current_avg = vb.vbPopFrontDouble32Auto(); }
if (mask & ((uint32_t)1 << 5)) { values.current_max = vb.vbPopFrontDouble32Auto(); }
if (mask & ((uint32_t)1 << 6)) { values.temp_mos_avg = vb.vbPopFrontDouble32Auto(); }
if (mask & ((uint32_t)1 << 7)) { values.temp_mos_max = vb.vbPopFrontDouble32Auto(); }
if (mask & ((uint32_t)1 << 8)) { values.temp_motor_avg = vb.vbPopFrontDouble32Auto(); }
if (mask & ((uint32_t)1 << 9)) { values.temp_motor_max = vb.vbPopFrontDouble32Auto(); }
if (mask & ((uint32_t)1 << 10)) { values.count_time = vb.vbPopFrontDouble32Auto(); }
emit statsRx(values, mask);
} break;
case COMM_LISP_READ_CODE: {
int qmlSize = vb.vbPopFrontInt32();
int offset = vb.vbPopFrontInt32();
emit lispReadCodeRx(qmlSize, offset, vb);
} break;
case COMM_LISP_ERASE_CODE:
emit lispEraseCodeRx(vb.at(0));
break;
case COMM_LISP_WRITE_CODE: {
bool ok = vb.vbPopFrontInt8();
quint32 offset = vb.vbPopFrontUint32();
emit lispWriteCodeRx(ok, offset);
} break;
case COMM_LISP_PRINT:
emit lispPrintReceived(QString::fromLatin1(vb));
break;
case COMM_LISP_GET_STATS: {
LISP_STATS stats;
stats.cpu_use = vb.vbPopFrontDouble16(1e2);
stats.heap_use = vb.vbPopFrontDouble16(1e2);
stats.mem_use = vb.vbPopFrontDouble16(1e2);
stats.stack_use = vb.vbPopFrontDouble16(1e2);
stats.done_ctx_r = vb.vbPopFrontString();
while (vb.size() > 0) {
auto name = vb.vbPopFrontString();
auto num = vb.vbPopFrontDouble32Auto();
stats.number_bindings.append(qMakePair(name, num));
}
emit lispStatsRx(stats);
} break;
case COMM_LISP_SET_RUNNING:
emit lispRunningResRx(vb.at(0));
break;
default: