-
-
Notifications
You must be signed in to change notification settings - Fork 148
/
flightlog_parser.js
1718 lines (1489 loc) · 71.2 KB
/
flightlog_parser.js
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
"use strict";
var FlightLogIndex,
FIRMWARE_TYPE_UNKNOWN = 0,
FIRMWARE_TYPE_BASEFLIGHT = 1,
FIRMWARE_TYPE_CLEANFLIGHT = 2,
FIRMWARE_TYPE_BETAFLIGHT = 3,
FIRMWARE_TYPE_INAV = 4;
var FlightLogParser = function(logData) {
//Private constants:
var
FLIGHT_LOG_MAX_FIELDS = 128,
FLIGHT_LOG_MAX_FRAME_LENGTH = 256,
//Assume that even in the most woeful logging situation, we won't miss 10 seconds of frames
MAXIMUM_TIME_JUMP_BETWEEN_FRAMES = (10 * 1000000),
//Likewise for iteration count
MAXIMUM_ITERATION_JUMP_BETWEEN_FRAMES = (500 * 10),
// Flight log field predictors:
//No prediction:
FLIGHT_LOG_FIELD_PREDICTOR_0 = 0,
//Predict that the field is the same as last frame:
FLIGHT_LOG_FIELD_PREDICTOR_PREVIOUS = 1,
//Predict that the slope between this field and the previous item is the same as that between the past two history items:
FLIGHT_LOG_FIELD_PREDICTOR_STRAIGHT_LINE = 2,
//Predict that this field is the same as the average of the last two history items:
FLIGHT_LOG_FIELD_PREDICTOR_AVERAGE_2 = 3,
//Predict that this field is minthrottle
FLIGHT_LOG_FIELD_PREDICTOR_MINTHROTTLE = 4,
//Predict that this field is the same as motor 0
FLIGHT_LOG_FIELD_PREDICTOR_MOTOR_0 = 5,
//This field always increments
FLIGHT_LOG_FIELD_PREDICTOR_INC = 6,
//Predict this GPS co-ordinate is the GPS home co-ordinate (or no prediction if that coordinate is not set)
FLIGHT_LOG_FIELD_PREDICTOR_HOME_COORD = 7,
//Predict 1500
FLIGHT_LOG_FIELD_PREDICTOR_1500 = 8,
//Predict vbatref, the reference ADC level stored in the header
FLIGHT_LOG_FIELD_PREDICTOR_VBATREF = 9,
//Predict the last time value written in the main stream
FLIGHT_LOG_FIELD_PREDICTOR_LAST_MAIN_FRAME_TIME = 10,
//Predict that this field is minthrottle
FLIGHT_LOG_FIELD_PREDICTOR_MINMOTOR = 11,
//Home coord predictors appear in pairs (two copies of FLIGHT_LOG_FIELD_PREDICTOR_HOME_COORD). Rewrite the second
//one we see to this to make parsing easier
FLIGHT_LOG_FIELD_PREDICTOR_HOME_COORD_1 = 256,
FLIGHT_LOG_FIELD_ENCODING_SIGNED_VB = 0, // Signed variable-byte
FLIGHT_LOG_FIELD_ENCODING_UNSIGNED_VB = 1, // Unsigned variable-byte
FLIGHT_LOG_FIELD_ENCODING_NEG_14BIT = 3, // Unsigned variable-byte but we negate the value before storing, value is 14 bits
FLIGHT_LOG_FIELD_ENCODING_TAG8_8SVB = 6,
FLIGHT_LOG_FIELD_ENCODING_TAG2_3S32 = 7,
FLIGHT_LOG_FIELD_ENCODING_TAG8_4S16 = 8,
FLIGHT_LOG_FIELD_ENCODING_NULL = 9, // Nothing is written to the file, take value to be zero
FLIGHT_LOG_FIELD_ENCODING_TAG2_3SVARIABLE = 10,
FLIGHT_LOG_EVENT_LOG_END = 255,
EOF = ArrayDataStream.prototype.EOF,
NEWLINE = '\n'.charCodeAt(0),
INFLIGHT_ADJUSTMENT_FUNCTIONS = [
{
name: 'None'
},
{
name: 'RC Rate',
scale: 0.01
},
{
name : 'RC Expo',
scale: 0.01
},
{
name: 'Throttle Expo',
scale: 0.01
},
{
name: 'Pitch & Roll Rate',
scale: 0.01
},
{
name: 'Yaw rate',
scale: 0.01
},
{
name: 'Pitch & Roll P',
scale: 0.1,
scalef: 1
},
{
name: 'Pitch & Roll I',
scale: 0.001,
scalef: 0.1
},
{
name: 'Pitch & Roll D',
scalef: 1000
},
{
name: 'Yaw P',
scale: 0.1,
scalef: 1
},
{
name: 'Yaw I',
scale: 0.001,
scalef: 0.1
},
{
name: 'Yaw D',
scalef: 1000
},
{
name: "Rate Profile"
},
{
name: 'Pitch Rate',
scale: 0.01
},
{
name: 'Roll Rate',
scale: 0.01
},
{
name: 'Pitch P',
scale: 0.1,
scalef: 1
},
{
name: 'Pitch I',
scale: 0.001,
scalef: 0.1
},
{
name: 'Pitch D',
scalef: 1000
},
{
name: 'Roll P',
scale: 0.1,
scalef: 1
},
{
name : 'Roll I',
scale: 0.001,
scalef: 0.1
},
{
name: 'Roll D',
scalef: 1000
}
];
//Private variables:
var
that = this,
dataVersion,
defaultSysConfig = {
frameIntervalI: 32,
frameIntervalPNum: 1,
frameIntervalPDenom: 1,
firmwareType: FIRMWARE_TYPE_UNKNOWN,
rcRate: 90,
vbatscale: 110,
vbatref: 4095,
vbatmincellvoltage: 33,
vbatmaxcellvoltage:43,
vbatwarningcellvoltage: 35,
gyroScale: 0.0001, // Not even close to the default, but it's hardware specific so we can't do much better
acc_1G: 4096, // Ditto ^
minthrottle: 1150,
maxthrottle: 1850,
currentMeterOffset: 0,
currentMeterScale: 400,
deviceUID: null
},
// These are now part of the blackbox log header, but they are in addition to the
// standard logger.
defaultSysConfigExtension = {
abs_control_gain:null, // Aboslute control gain
anti_gravity_gain:null, // Anti gravity gain
anti_gravity_mode:null, // Anti gravity mode
anti_gravity_threshold:null, // Anti gravity threshold for step mode
thrMid:null, // Throttle Mid Position
thrExpo:null, // Throttle Expo
tpa_breakpoint:null, // TPA Breakpoint
airmode_activate_throttle:null, // airmode activation level
serialrx_provider:null, // name of the serial rx provider
superExpoFactor:null, // Super Expo Factor
rates:[null, null, null], // Rates [ROLL, PITCH, YAW]
rate_limits:[1998, 1998, 1998], // Limits [ROLL, PITCH, YAW] with defaults for backward compatibility
rc_rates:[null, null, null], // RC Rates [ROLL, PITCH, YAW]
rc_expo:[null, null, null], // RC Expo [ROLL, PITCH, YAW]
looptime:null, // Looptime
gyro_sync_denom:null, // Gyro Sync Denom
pid_process_denom:null, // PID Process Denom
pidController:null, // Active PID Controller
rollPID:[null, null, null], // Roll [P, I, D]
pitchPID:[null, null, null], // Pitch[P, I, D]
yawPID:[null, null, null], // Yaw [P, I, D]
feedforward_transition:null, // Feedforward transition
altPID:[null, null, null], // Altitude Hold [P, I, D]
posPID:[null, null, null], // Position Hold [P, I, D]
posrPID:[null, null, null], // Position Rate [P, I, D]
navrPID:[null, null, null], // Nav Rate [P, I, D]
levelPID:[null, null, null], // Level Mode [P, I, D]
magPID:null, // Magnetometer P
velPID:[null, null, null], // Velocity [P, I, D]
yaw_p_limit:null, // Yaw P Limit
yaw_lpf_hz:null, // Yaw LowPass Filter Hz
dterm_average_count:null, // DTerm Average Count
rollPitchItermResetRate:null, // ITerm Reset rate for Roll and Pitch
yawItermResetRate:null, // ITerm Reset Rate for Yaw
dshot_bidir:null, // DShot bidir protocol enabled
dterm_lpf_hz:null, // DTerm Lowpass Filter Hz
dterm_lpf_dyn_hz:[null, null], // DTerm Lowpass Dynamic Filter Min and Max Hz
dterm_lpf2_hz:null, // DTerm Lowpass Filter Hz 2
dterm_differentiator:null, // DTerm Differentiator
H_sensitivity:null, // Horizon Sensitivity
iterm_reset_offset:null, // I-Term reset offset
deadband:null, // Roll, Pitch Deadband
yaw_deadband:null, // Yaw Deadband
gyro_lpf:null, // Gyro lpf setting.
gyro_32khz_hardware_lpf:null, // Gyro 32khz hardware lpf setting. (post BF3.4)
gyro_lowpass_hz:null, // Gyro Soft Lowpass Filter Hz
gyro_lowpass_dyn_hz:[null, null], // Gyro Soft Lowpass Dynamic Filter Min and Max Hz
gyro_lowpass2_hz:null, // Gyro Soft Lowpass Filter Hz 2
gyro_notch_hz:null, // Gyro Notch Frequency
gyro_notch_cutoff:null, // Gyro Notch Cutoff
gyro_rpm_notch_harmonics:null, // Number of Harmonics in the gyro rpm filter
gyro_rpm_notch_q:null, // Value of Q in the gyro rpm filter
gyro_rpm_notch_min:null, // Min Hz for the gyro rpm filter
dterm_rpm_notch_harmonics:null, // Number of Harmonics in the dterm rpm filter
dterm_rpm_notch_q:null, // Value of Q in the dterm rpm filter
dterm_rpm_notch_min:null, // Min Hz for the dterm rpm filter
dterm_notch_hz:null, // Dterm Notch Frequency
dterm_notch_cutoff:null, // Dterm Notch Cutoff
acc_lpf_hz:null, // Accelerometer Lowpass filter Hz
acc_hardware:null, // Accelerometer Hardware type
baro_hardware:null, // Barometer Hardware type
mag_hardware:null, // Magnetometer Hardware type
gyro_cal_on_first_arm:null, // Gyro Calibrate on first arm
vbat_pid_compensation:null, // VBAT PID compensation
rate_limits:[null, null, null], // RC Rate limits
rc_smoothing:null, // RC Control Smoothing
rc_smoothing_type:null, // Type of the RC Smoothing
rc_interpolation:null, // RC Control Interpolation type
rc_interpolation_channels:null, // RC Control Interpotlation channels
rc_interpolation_interval:null, // RC Control Interpolation Interval
rc_smoothing_active_cutoffs:[null,null],// RC Smoothing active cutoffs
rc_smoothing_auto_factor:null, // RC Smoothing auto factor
rc_smoothing_cutoffs:[null, null], // RC Smoothing input and derivative cutoff
rc_smoothing_filter_type:[null,null], // RC Smoothing input and derivative type
rc_smoothing_rx_average:null, // RC Smoothing rx average readed in ms
rc_smoothing_debug_axis:null, // Axis recorded in the debug mode of rc_smoothing
dterm_filter_type:null, // D term filtering type (PT1, BIQUAD)
dterm_filter2_type:null, // D term 2 filtering type (PT1, BIQUAD)
pidAtMinThrottle:null, // Stabilisation at zero throttle
itermThrottleGain:null, // Betaflight PID
ptermSetpointWeight:null, // Betaflight PID
dtermSetpointWeight:null, // Betaflight PID
yawRateAccelLimit:null, // Betaflight PID
rateAccelLimit:null, // Betaflight PID
gyro_soft_type:null, // Gyro soft filter type (PT1, BIQUAD)
gyro_soft2_type:null, // Gyro soft filter 2 type (PT1, BIQUAD)
debug_mode:null, // Selected Debug Mode
features:null, // Activated features (e.g. MOTORSTOP etc)
Craft_name:null, // Craft Name
motorOutput:[null,null], // Minimum and maximum outputs to motor's
digitalIdleOffset:null, // min throttle for d-shot (as a percentage)
pidSumLimit:null, // PID sum limit
pidSumLimitYaw:null, // PID sum limit yaw
use_integrated_yaw : null, // Use integrated yaw
d_min : [null, null, null], // D_Min [P, I, D]
d_min_gain : null, // D_Min gain
d_min_advance : null, // D_Min advance
iterm_relax: null, // ITerm Relax mode
iterm_relax_type: null, // ITerm Relax type
iterm_relax_cutoff: null, // ITerm Relax cutoff
dyn_notch_range: null, // Dyn Notch Range (LOW, MED, HIGH or AUTO)
dyn_notch_width_percent: null, // Dyn Notch width percent distance between the two notches
dyn_notch_q: null, // Dyn Notch width of each dynamic filter
dyn_notch_min_hz: null, // Dyn Notch min limit in Hz for the filter
dyn_notch_max_hz: null, // Dyn Notch max limit in Hz for the filter
rates_type: null,
fields_disabled_mask: null,
vbat_sag_compensation: null,
unknownHeaders : [] // Unknown Extra Headers
},
// Translation of the field values name to the sysConfig var where it must be stored
translationValues = {
acc_limit_yaw : "yawRateAccelLimit",
accel_limit : "rateAccelLimit",
acc_limit : "rateAccelLimit",
anti_gravity_thresh : "anti_gravity_threshold",
currentSensor : "currentMeter",
d_notch_cut : "dterm_notch_cutoff",
d_setpoint_weight : "dtermSetpointWeight",
dterm_lowpass_hz : "dterm_lpf_hz",
dterm_lowpass_dyn_hz : "dterm_lpf_dyn_hz",
dterm_lowpass2_hz : "dterm_lpf2_hz",
dterm_setpoint_weight : "dtermSetpointWeight",
digital_idle_value : "digitalIdleOffset",
dshot_idle_value : "digitalIdleOffset",
gyro_hardware_lpf : "gyro_lpf",
gyro_lowpass : "gyro_lowpass_hz",
gyro_lowpass_type : "gyro_soft_type",
gyro_lowpass2_type : "gyro_soft2_type",
"gyro.scale" : "gyro_scale",
iterm_windup : "itermWindupPointPercent",
motor_pwm_protocol : "fast_pwm_protocol",
pidsum_limit : "pidSumLimit",
pidsum_limit_yaw : "pidSumLimitYaw",
rc_expo_yaw : "rcYawExpo",
rc_interp : "rc_interpolation",
rc_interp_int : "rc_interpolation_interval",
rc_rate : "rc_rates",
rc_rate_yaw : "rcYawRate",
rc_yaw_expo : "rcYawExpo",
rcExpo : "rc_expo",
rcRate : "rc_rates",
setpoint_relax_ratio : "setpointRelaxRatio",
setpoint_relaxation_ratio : "setpointRelaxRatio",
thr_expo : "thrExpo",
thr_mid : "thrMid",
tpa_rate : "dynThrPID",
use_unsynced_pwm : "unsynced_fast_pwm",
vbat_scale : "vbatscale",
vbat_pid_gain : "vbat_pid_compensation",
yaw_accel_limit : "yawRateAccelLimit",
yaw_lowpass_hz : "yaw_lpf_hz"
},
frameTypes,
// Blackbox state:
mainHistoryRing,
/* Points into blackboxHistoryRing to give us a circular buffer.
*
* 0 - space to decode new frames into, 1 - previous frame, 2 - previous previous frame
*
* Previous frame pointers are null when no valid history exists of that age.
*/
mainHistory = [null, null, null],
mainStreamIsValid = false,
gpsHomeHistory = new Array(2), // 0 - space to decode new frames into, 1 - previous frame
gpsHomeIsValid = false,
//Because these events don't depend on previous events, we don't keep copies of the old state, just the current one:
lastEvent,
lastGPS,
lastSlow,
// How many intentionally un-logged frames did we skip over before we decoded the current frame?
lastSkippedFrames,
// Details about the last main frame that was successfully parsed
lastMainFrameIteration,
lastMainFrameTime,
//The actual log data stream we're reading:
stream;
//Public fields:
/* Information about the frame types the log contains, along with details on their fields.
* Each entry is an object with field details {encoding:[], predictor:[], name:[], count:0, signed:[]}
*/
this.frameDefs = {};
// Lets add the custom extensions
var completeSysConfig = $.extend({}, defaultSysConfig, defaultSysConfigExtension);
this.sysConfig = Object.create(completeSysConfig); // Object.create(defaultSysConfig);
/*
* Event handler of the signature (frameValid, frame, frameType, frameOffset, frameSize)
* called when a frame has been decoded.
*/
this.onFrameReady = null;
function mapFieldNamesToIndex(fieldNames) {
var
result = {};
for (var i = 0; i < fieldNames.length; i++) {
result[fieldNames[i]] = i;
}
return result;
}
/**
* Translates old field names in the given array to their modern equivalents and return the passed array.
*/
function translateLegacyFieldNames(names) {
for (var i = 0; i < names.length; i++) {
var
matches;
if ((matches = names[i].match(/^gyroData(.+)$/))) {
names[i] = "gyroADC" + matches[1];
}
}
return names;
}
/**
* Translates the name of a field to the parameter in sysConfig object equivalent
*
* fieldName Name of the field to translate
* returns The equivalent in the sysConfig object or the fieldName if not found
*/
function translateFieldName(fieldName) {
var translation = translationValues[fieldName];
if (typeof translation !== 'undefined') {
return translation;
} else {
return fieldName;
}
}
function parseHeaderLine() {
var
COLON = ":".charCodeAt(0),
fieldName, fieldValue,
lineStart, lineEnd, separatorPos = false,
matches,
i, c;
if (stream.peekChar() != ' ')
return;
//Skip the leading space
stream.readChar();
lineStart = stream.pos;
for (; stream.pos < lineStart + 1024 && stream.pos < stream.end; stream.pos++) {
if (separatorPos === false && stream.data[stream.pos] == COLON)
separatorPos = stream.pos;
if (stream.data[stream.pos] == NEWLINE || stream.data[stream.pos] === 0)
break;
}
if (stream.data[stream.pos] != NEWLINE || separatorPos === false)
return;
lineEnd = stream.pos;
fieldName = asciiArrayToString(stream.data.subarray(lineStart, separatorPos));
fieldValue = asciiArrayToString(stream.data.subarray(separatorPos + 1, lineEnd));
// Translate the fieldName to the sysConfig parameter name. The fieldName has been changing between versions
// In this way is easier to maintain the code
fieldName = translateFieldName(fieldName);
switch (fieldName) {
case "I interval":
that.sysConfig.frameIntervalI = parseInt(fieldValue, 10);
if (that.sysConfig.frameIntervalI < 1)
that.sysConfig.frameIntervalI = 1;
break;
case "P interval":
matches = fieldValue.match(/(\d+)\/(\d+)/);
if (matches) {
that.sysConfig.frameIntervalPNum = parseInt(matches[1], 10);
that.sysConfig.frameIntervalPDenom = parseInt(matches[2], 10);
} else {
that.sysConfig.frameIntervalPNum = 1;
that.sysConfig.frameIntervalPDenom = parseInt(fieldValue, 10);
}
break;
case "P denom":
case "P ratio":
// Don't do nothing with this, because is the same than frameIntervalI/frameIntervalPDenom so we don't need it
break;
case "Data version":
dataVersion = parseInt(fieldValue, 10);
break;
case "Firmware type":
switch (fieldValue) {
case "Cleanflight":
that.sysConfig.firmwareType = FIRMWARE_TYPE_CLEANFLIGHT;
$('html').removeClass('isBaseF');
$('html').addClass('isCF');
$('html').removeClass('isBF');
$('html').removeClass('isINAV');
break;
default:
that.sysConfig.firmwareType = FIRMWARE_TYPE_BASEFLIGHT;
$('html').addClass('isBaseF');
$('html').removeClass('isCF');
$('html').removeClass('isBF');
$('html').removeClass('isINAV');
}
break;
// Betaflight Log Header Parameters
case "minthrottle":
that.sysConfig[fieldName] = parseInt(fieldValue, 10);
that.sysConfig.motorOutput[0] = that.sysConfig[fieldName]; // by default, set the minMotorOutput to match minThrottle
break;
case "maxthrottle":
that.sysConfig[fieldName] = parseInt(fieldValue, 10);
that.sysConfig.motorOutput[1] = that.sysConfig[fieldName]; // by default, set the maxMotorOutput to match maxThrottle
break;
case "rcRate":
case "thrMid":
case "thrExpo":
case "dynThrPID":
case "tpa_breakpoint":
case "airmode_activate_throttle":
case "serialrx_provider":
case "looptime":
case "gyro_sync_denom":
case "pid_process_denom":
case "pidController":
case "yaw_p_limit":
case "dterm_average_count":
case "rollPitchItermResetRate":
case "yawItermResetRate":
case "rollPitchItermIgnoreRate":
case "yawItermIgnoreRate":
case "dterm_differentiator":
case "deltaMethod":
case "dynamic_dterm_threshold":
case "dynamic_pterm":
case "iterm_reset_offset":
case "deadband":
case "yaw_deadband":
case "gyro_lpf":
case "gyro_hardware_lpf":
case "gyro_32khz_hardware_lpf":
case "acc_lpf_hz":
case "acc_hardware":
case "baro_hardware":
case "mag_hardware":
case "gyro_cal_on_first_arm":
case "vbat_pid_compensation":
case "rc_smoothing":
case "rc_smoothing_auto_factor":
case "rc_smoothing_type":
case "rc_smoothing_debug_axis":
case "rc_smoothing_rx_average":
case "superExpoYawMode":
case "features":
case "dynamic_pid":
case "rc_interpolation":
case "rc_interpolation_channels":
case "rc_interpolation_interval":
case "unsynced_fast_pwm":
case "fast_pwm_protocol":
case "motor_pwm_rate":
case "vbatscale":
case "vbatref":
case "acc_1G":
case "dterm_filter_type":
case "dterm_filter2_type":
case "pidAtMinThrottle":
case "pidSumLimit":
case "pidSumLimitYaw":
case "anti_gravity_threshold":
case "itermWindupPointPercent":
case "ptermSRateWeight":
case "setpointRelaxRatio":
case "feedforward_transition":
case "dtermSetpointWeight":
case "gyro_soft_type":
case "gyro_soft2_type":
case "debug_mode":
case "anti_gravity_mode":
case "anti_gravity_gain":
case "abs_control_gain":
case "use_integrated_yaw":
case "d_min_gain":
case "d_min_advance":
case "dshot_bidir":
case "gyro_rpm_notch_harmonics":
case "gyro_rpm_notch_q":
case "gyro_rpm_notch_min":
case "dterm_rpm_notch_harmonics":
case "dterm_rpm_notch_q":
case "dterm_rpm_notch_min":
case "iterm_relax":
case "iterm_relax_type":
case "iterm_relax_cutoff":
case "dyn_notch_range":
case "dyn_notch_width_percent":
case "dyn_notch_q":
case "dyn_notch_min_hz":
case "dyn_notch_max_hz":
case "rates_type":
case "vbat_sag_compensation":
case "fields_disabled_mask":
that.sysConfig[fieldName] = parseInt(fieldValue, 10);
break;
case "rc_expo":
case "rc_rates":
if(stringHasComma(fieldValue)) {
that.sysConfig[fieldName] = parseCommaSeparatedString(fieldValue);
} else {
that.sysConfig[fieldName][0] = parseInt(fieldValue, 10);
that.sysConfig[fieldName][1] = parseInt(fieldValue, 10);
}
break;
case "rcYawExpo":
that.sysConfig["rc_expo"][2] = parseInt(fieldValue, 10);
break;
case "rcYawRate":
that.sysConfig["rc_rates"][2] = parseInt(fieldValue, 10);
break;
case "yawRateAccelLimit":
case "rateAccelLimit":
if((that.sysConfig.firmwareType == FIRMWARE_TYPE_BETAFLIGHT && semver.gte(that.sysConfig.firmwareVersion, '3.1.0')) ||
(that.sysConfig.firmwareType == FIRMWARE_TYPE_CLEANFLIGHT && semver.gte(that.sysConfig.firmwareVersion, '2.0.0'))) {
that.sysConfig[fieldName] = parseInt(fieldValue, 10)/1000;
} else {
that.sysConfig[fieldName] = parseInt(fieldValue, 10);
}
break;
case "yaw_lpf_hz":
case "gyro_lowpass_hz":
case "gyro_lowpass2_hz":
case "dterm_notch_hz":
case "dterm_notch_cutoff":
case "dterm_lpf_hz":
case "dterm_lpf2_hz":
if((that.sysConfig.firmwareType == FIRMWARE_TYPE_BETAFLIGHT && semver.gte(that.sysConfig.firmwareVersion, '3.0.1')) ||
(that.sysConfig.firmwareType == FIRMWARE_TYPE_CLEANFLIGHT && semver.gte(that.sysConfig.firmwareVersion, '2.0.0'))) {
that.sysConfig[fieldName] = parseInt(fieldValue, 10);
} else {
that.sysConfig[fieldName] = parseInt(fieldValue, 10) / 100.0;
}
break;
case "gyro_notch_hz":
case "gyro_notch_cutoff":
if((that.sysConfig.firmwareType == FIRMWARE_TYPE_BETAFLIGHT && semver.gte(that.sysConfig.firmwareVersion, '3.0.1')) ||
(that.sysConfig.firmwareType == FIRMWARE_TYPE_CLEANFLIGHT && semver.gte(that.sysConfig.firmwareVersion, '2.0.0'))) {
that.sysConfig[fieldName] = parseCommaSeparatedString(fieldValue);
} else {
that.sysConfig[fieldName] = parseInt(fieldValue, 10) / 100.0;
}
break;
case "digitalIdleOffset":
that.sysConfig[fieldName] = parseInt(fieldValue, 10) / 100.0;
/** Cleanflight Only log headers **/
case "dterm_cut_hz":
case "acc_cut_hz":
that.sysConfig[fieldName] = parseInt(fieldValue, 10);
break;
/** End of cleanflight only log headers **/
case "superExpoFactor":
if(stringHasComma(fieldValue)) {
var expoParams = parseCommaSeparatedString(fieldValue);
that.sysConfig.superExpoFactor = expoParams[0];
that.sysConfig.superExpoFactorYaw = expoParams[1];
} else {
that.sysConfig.superExpoFactor = parseInt(fieldValue, 10);
}
break;
/* CSV packed values */
case "rates":
case "rate_limits":
case "rollPID":
case "pitchPID":
case "yawPID":
case "altPID":
case "posPID":
case "posrPID":
case "navrPID":
case "levelPID":
case "velPID":
case "motorOutput":
case "rate_limits":
case "rc_smoothing_active_cutoffs":
case "rc_smoothing_cutoffs":
case "rc_smoothing_filter_type":
case "gyro_lowpass_dyn_hz":
case "dterm_lpf_dyn_hz":
case "d_min":
that.sysConfig[fieldName] = parseCommaSeparatedString(fieldValue);
break;
case "magPID":
that.sysConfig.magPID = parseCommaSeparatedString(fieldValue,3); //[parseInt(fieldValue, 10), null, null];
break;
case "feedforward_weight":
// Add it to the end of the rollPID, pitchPID and yawPID
var ffValues = parseCommaSeparatedString(fieldValue);
that.sysConfig["rollPID"].push(ffValues[0]);
that.sysConfig["pitchPID"].push(ffValues[1]);
that.sysConfig["yawPID"].push(ffValues[2]);
break;
/* End of CSV packed values */
case "vbatcellvoltage":
var vbatcellvoltageParams = parseCommaSeparatedString(fieldValue);
that.sysConfig.vbatmincellvoltage = vbatcellvoltageParams[0];
that.sysConfig.vbatwarningcellvoltage = vbatcellvoltageParams[1];
that.sysConfig.vbatmaxcellvoltage = vbatcellvoltageParams[2];
break;
case "currentMeter":
case "currentSensor":
var currentMeterParams = parseCommaSeparatedString(fieldValue);
that.sysConfig.currentMeterOffset = currentMeterParams[0];
that.sysConfig.currentMeterScale = currentMeterParams[1];
break;
case "gyro.scale":
case "gyro_scale":
that.sysConfig.gyroScale = hexToFloat(fieldValue);
/* Baseflight uses a gyroScale that'll give radians per microsecond as output, whereas Cleanflight produces degrees
* per second and leaves the conversion to radians per us to the IMU. Let's just convert Cleanflight's scale to
* match Baseflight so we can use Baseflight's IMU for both: */
if (that.sysConfig.firmwareType == FIRMWARE_TYPE_INAV ||
that.sysConfig.firmwareType == FIRMWARE_TYPE_CLEANFLIGHT ||
that.sysConfig.firmwareType == FIRMWARE_TYPE_BETAFLIGHT) {
that.sysConfig.gyroScale = that.sysConfig.gyroScale * (Math.PI / 180.0) * 0.000001;
}
break;
case "Firmware revision":
//TODO Unify this somehow...
// Extract the firmware revision in case of Betaflight/Raceflight/Cleanfligh 2.x/Other
var matches = fieldValue.match(/(.*flight).* (\d+)\.(\d+)(\.(\d+))*/i);
if(matches!=null) {
// Detecting Betaflight requires looking at the revision string
if (matches[1] === "Betaflight") {
that.sysConfig.firmwareType = FIRMWARE_TYPE_BETAFLIGHT;
$('html').removeClass('isBaseF');
$('html').removeClass('isCF');
$('html').addClass('isBF');
$('html').removeClass('isINAV');
}
that.sysConfig.firmware = parseFloat(matches[2] + '.' + matches[3]).toFixed(1);
that.sysConfig.firmwarePatch = (matches[5] != null)?parseInt(matches[5]):'0';
that.sysConfig.firmwareVersion = that.sysConfig.firmware + '.' + that.sysConfig.firmwarePatch;
} else {
/*
* Try to detect INAV
*/
var matches = fieldValue.match(/(INAV).* (\d+)\.(\d+).(\d+)*/i);
if(matches!=null) {
that.sysConfig.firmwareType = FIRMWARE_TYPE_INAV;
that.sysConfig.firmware = parseFloat(matches[2] + '.' + matches[3]);
that.sysConfig.firmwarePatch = (matches[5] != null)?parseInt(matches[5]):'';
//added class definition as the isBF, isCF etc classes are only used for colors and
//a few images in the css.
$('html').removeClass('isBaseF');
$('html').removeClass('isCF');
$('html').removeClass('isBF');
$('html').addClass('isINAV');
} else {
// Cleanflight 1.x and others
that.sysConfig.firmwareVersion = '0.0.0';
that.sysConfig.firmware = 0.0;
that.sysConfig.firmwarePatch = 0;
}
}
that.sysConfig[fieldName] = fieldValue;
break;
case "Product":
case "Blackbox version":
case "Firmware date":
case "Board information":
case "Craft name":
case "Log start datetime":
// These fields are not presently used for anything, ignore them here so we don't warn about unsupported headers
// Just Add them anyway
that.sysConfig[fieldName] = fieldValue;
break;
case "Device UID":
that.sysConfig.deviceUID = fieldValue;
break;
default:
if ((matches = fieldName.match(/^Field (.) (.+)$/))) {
var
frameName = matches[1],
frameInfo = matches[2],
frameDef;
if (!that.frameDefs[frameName]) {
that.frameDefs[frameName] = {
name: [],
nameToIndex: {},
count: 0,
signed: [],
predictor: [],
encoding: [],
};
}
frameDef = that.frameDefs[frameName];
switch (frameInfo) {
case "predictor":
frameDef.predictor = parseCommaSeparatedString(fieldValue);
break;
case "encoding":
frameDef.encoding = parseCommaSeparatedString(fieldValue);
break;
case "name":
frameDef.name = translateLegacyFieldNames(fieldValue.split(","));
frameDef.count = frameDef.name.length;
frameDef.nameToIndex = mapFieldNamesToIndex(frameDef.name);
/*
* We could survive with the `signed` header just being filled with zeros, so if it is absent
* then resize it to length.
*/
frameDef.signed.length = frameDef.count;
break;
case "signed":
frameDef.signed = parseCommaSeparatedString(fieldValue);
break;
default:
console.log("Saw unsupported field header \"" + fieldName + "\"");
}
} else {
console.log("Ignoring unsupported header \"" + fieldName + "\"");
if(that.sysConfig.unknownHeaders==null) that.sysConfig.unknownHeaders = new Array();
that.sysConfig.unknownHeaders.push({ name: fieldName, value: fieldValue });// Save the unknown headers
}
break;
}
}
function invalidateMainStream() {
mainStreamIsValid = false;
mainHistory[0] = mainHistoryRing ? mainHistoryRing[0]: null;
mainHistory[1] = null;
mainHistory[2] = null;
}
/**
* Use data from the given frame to update field statistics for the given frame type.
*/
function updateFieldStatistics(frameType, frame) {
var
i, fieldStats;
fieldStats = that.stats.frame[frameType].field;
for (i = 0; i < frame.length; i++) {
if (!fieldStats[i]) {
fieldStats[i] = {
max: frame[i],
min: frame[i]
};
} else {
fieldStats[i].max = frame[i] > fieldStats[i].max ? frame[i] : fieldStats[i].max;
fieldStats[i].min = frame[i] < fieldStats[i].min ? frame[i] : fieldStats[i].min;
}
}
}
function completeIntraframe(frameType, frameStart, frameEnd, raw) {
var acceptFrame = true;
// Do we have a previous frame to use as a reference to validate field values against?
if (!raw && lastMainFrameIteration != -1) {
/*
* Check that iteration count and time didn't move backwards, and didn't move forward too much.
*/
acceptFrame =
mainHistory[0][FlightLogParser.prototype.FLIGHT_LOG_FIELD_INDEX_ITERATION] >= lastMainFrameIteration
&& mainHistory[0][FlightLogParser.prototype.FLIGHT_LOG_FIELD_INDEX_ITERATION] < lastMainFrameIteration + MAXIMUM_ITERATION_JUMP_BETWEEN_FRAMES
&& mainHistory[0][FlightLogParser.prototype.FLIGHT_LOG_FIELD_INDEX_TIME] >= lastMainFrameTime
&& mainHistory[0][FlightLogParser.prototype.FLIGHT_LOG_FIELD_INDEX_TIME] < lastMainFrameTime + MAXIMUM_TIME_JUMP_BETWEEN_FRAMES;
}
if (acceptFrame) {
that.stats.intentionallyAbsentIterations += countIntentionallySkippedFramesTo(mainHistory[0][FlightLogParser.prototype.FLIGHT_LOG_FIELD_INDEX_ITERATION]);
lastMainFrameIteration = mainHistory[0][FlightLogParser.prototype.FLIGHT_LOG_FIELD_INDEX_ITERATION];
lastMainFrameTime = mainHistory[0][FlightLogParser.prototype.FLIGHT_LOG_FIELD_INDEX_TIME];
mainStreamIsValid = true;
updateFieldStatistics(frameType, mainHistory[0]);
} else {
invalidateMainStream();
}
if (that.onFrameReady)
that.onFrameReady(mainStreamIsValid, mainHistory[0], frameType, frameStart, frameEnd - frameStart);
// Rotate history buffers
// Both the previous and previous-previous states become the I-frame, because we can't look further into the past than the I-frame
mainHistory[1] = mainHistory[0];
mainHistory[2] = mainHistory[0];
// And advance the current frame into an empty space ready to be filled
if (mainHistory[0] == mainHistoryRing[0])
mainHistory[0] = mainHistoryRing[1];
else if (mainHistory[0] == mainHistoryRing[1])
mainHistory[0] = mainHistoryRing[2];
else
mainHistory[0] = mainHistoryRing[0];
}
/**
* Should a frame with the given index exist in this log (based on the user's selection of sampling rates)?
*/
function shouldHaveFrame(frameIndex)
{
return (frameIndex % that.sysConfig.frameIntervalI + that.sysConfig.frameIntervalPNum - 1)
% that.sysConfig.frameIntervalPDenom < that.sysConfig.frameIntervalPNum;
}
/**
* Attempt to parse the frame of into the supplied `current` buffer using the encoding/predictor
* definitions from `frameDefs`. The previous frame values are used for predictions.
*
* frameDef - The definition for the frame type being parsed (from this.frameDefs)
* raw - Set to true to disable predictions (and so store raw values)
* skippedFrames - Set to the number of field iterations that were skipped over by rate settings since the last frame.
*/
function parseFrame(frameDef, current, previous, previous2, skippedFrames, raw)
{
var
predictor = frameDef.predictor,
encoding = frameDef.encoding,
values = new Array(8),
i, j, groupCount;
i = 0;
while (i < frameDef.count) {
var
value;
if (predictor[i] == FLIGHT_LOG_FIELD_PREDICTOR_INC) {
current[i] = skippedFrames + 1;
if (previous)
current[i] += previous[i];
i++;
} else {
switch (encoding[i]) {
case FLIGHT_LOG_FIELD_ENCODING_SIGNED_VB:
value = stream.readSignedVB();
break;
case FLIGHT_LOG_FIELD_ENCODING_UNSIGNED_VB:
value = stream.readUnsignedVB();
break;
case FLIGHT_LOG_FIELD_ENCODING_NEG_14BIT:
value = -signExtend14Bit(stream.readUnsignedVB());
break;
case FLIGHT_LOG_FIELD_ENCODING_TAG8_4S16: