/
electronic_throttle.cpp
1133 lines (922 loc) · 32.9 KB
/
electronic_throttle.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
/**
* @file electronic_throttle.cpp
* @brief Electronic Throttle driver
*
* @see test test_etb.cpp
*
*
* Limited user documentation at https://github.com/rusefi/rusefi/wiki/HOWTO_electronic_throttle_body
*
*
* ETB is controlled according to pedal position input (pedal position sensor is a potentiometer)
* pedal 0% means pedal not pressed / idle
* pedal 100% means pedal all the way down
* (not TPS - not the one you can calibrate in TunerStudio)
*
*
* See also pid.cpp
*
* Relevant console commands:
*
* ETB_BENCH_ENGINE
* set engine_type 58
*
* enable verbose_etb
* disable verbose_etb
* etbinfo
* set mock_pedal_position X
*
*
* set debug_mode 17
* for PID outputs
*
* set etb_p X
* set etb_i X
* set etb_d X
* set etb_o X
*
* set_etb_duty X
*
* http://rusefi.com/forum/viewtopic.php?f=5&t=592
*
* @date Dec 7, 2013
* @author Andrey Belomutskiy, (c) 2012-2020
*
* This file is part of rusEfi - see http://rusefi.com
*
* rusEfi 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.
*
* rusEfi 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 "pch.h"
#include "electronic_throttle_impl.h"
#if EFI_ELECTRONIC_THROTTLE_BODY
#include "dc_motor.h"
#include "dc_motors.h"
#include "defaults.h"
#if defined(HAS_OS_ACCESS)
#error "Unexpected OS ACCESS HERE"
#endif
#if HW_PROTEUS
#include "proteus_meta.h"
#endif // HW_PROTEUS
#ifndef ETB_MAX_COUNT
#define ETB_MAX_COUNT 2
#endif /* ETB_MAX_COUNT */
static pedal2tps_t pedal2tpsMap{"p2t"};
static Map3D<ETB2_TRIM_SIZE, ETB2_TRIM_SIZE, int8_t, uint8_t, uint8_t> throttle2TrimTable{"t2t"};
static Map3D<TRACTION_CONTROL_ETB_DROP_SIZE, TRACTION_CONTROL_ETB_DROP_SIZE, int8_t, uint16_t, uint8_t> tcEtbDropTable{"tce"};
constexpr float etbPeriodSeconds = 1.0f / ETB_LOOP_FREQUENCY;
//static bool startupPositionError = false;
//#define STARTUP_NEUTRAL_POSITION_ERROR_THRESHOLD 5
static const float hardCodedetbHitachiBiasBins[8] = {0.0, 19.0, 21.0, 22.0, 23.0, 25.0, 30.0, 100.0};
static const float hardCodedetbHitachiBiasValues[8] = {-18.0, -17.0, -15.0, 0.0, 16.0, 20.0, 20.0, 20.0};
/* Generated by TS2C on Thu Aug 20 21:10:02 EDT 2020*/
void setHitachiEtbBiasBins() {
copyArray(config->etbBiasBins, hardCodedetbHitachiBiasBins);
copyArray(config->etbBiasValues, hardCodedetbHitachiBiasValues);
}
static SensorType functionToPositionSensor(dc_function_e func) {
switch(func) {
case DC_Throttle1: return SensorType::Tps1;
case DC_Throttle2: return SensorType::Tps2;
case DC_IdleValve: return SensorType::IdlePosition;
case DC_Wastegate: return SensorType::WastegatePosition;
default: return SensorType::Invalid;
}
}
static SensorType functionToTpsSensor(dc_function_e func) {
switch(func) {
case DC_Throttle1: return SensorType::Tps1;
default: return SensorType::Tps2;
}
}
static SensorType functionToTpsSensorPrimary(dc_function_e func) {
switch(func) {
case DC_Throttle1: return SensorType::Tps1Primary;
default: return SensorType::Tps2Primary;
}
}
static SensorType functionToTpsSensorSecondary(dc_function_e func) {
switch(func) {
case DC_Throttle1: return SensorType::Tps1Secondary;
default: return SensorType::Tps2Secondary;
}
}
#if EFI_TUNER_STUDIO
static TsCalMode functionToCalModePriMin(dc_function_e func) {
switch (func) {
case DC_Throttle1: return TsCalMode::Tps1Min;
default: return TsCalMode::Tps2Min;
}
}
static TsCalMode functionToCalModePriMax(dc_function_e func) {
switch (func) {
case DC_Throttle1: return TsCalMode::Tps1Max;
default: return TsCalMode::Tps2Max;
}
}
static TsCalMode functionToCalModeSecMin(dc_function_e func) {
switch (func) {
case DC_Throttle1: return TsCalMode::Tps1SecondaryMin;
default: return TsCalMode::Tps2SecondaryMin;
}
}
static TsCalMode functionToCalModeSecMax(dc_function_e func) {
switch (func) {
case DC_Throttle1: return TsCalMode::Tps1SecondaryMax;
default: return TsCalMode::Tps2SecondaryMax;
}
}
#endif // EFI_TUNER_STUDIO
static percent_t directPwmValue = NAN;
#define ETB_DUTY_LIMIT 0.9
// this macro clamps both positive and negative percentages from about -100% to 100%
#define ETB_PERCENT_TO_DUTY(x) (clampF(-ETB_DUTY_LIMIT, 0.01f * (x), ETB_DUTY_LIMIT))
bool EtbController::init(dc_function_e function, DcMotor *motor, pid_s *pidParameters, const ValueProvider3D* pedalMap, bool hasPedal) {
if (function == DC_None) {
// if not configured, don't init.
etbErrorCode = (int8_t)TpsState::None;
return false;
}
m_function = function;
m_positionSensor = functionToPositionSensor(function);
// If we are a throttle, require redundant TPS sensor
if (isEtbMode()) {
// We don't need to init throttles, so nothing to do here.
if (!hasPedal) {
etbErrorCode = (int8_t)TpsState::None;
return false;
}
// If no sensor is configured for this throttle, skip initialization.
if (!Sensor::hasSensor(functionToTpsSensor(function))) {
etbErrorCode = (int8_t)TpsState::TpsError;
return false;
}
if (!Sensor::isRedundant(m_positionSensor)) {
firmwareError(
ObdCode::OBD_TPS_Configuration,
"Use of electronic throttle requires %s to be redundant.",
Sensor::getSensorName(m_positionSensor)
);
etbErrorCode = (int8_t)TpsState::Redundancy;
return false;
}
if (!Sensor::isRedundant(SensorType::AcceleratorPedal)) {
firmwareError(
ObdCode::OBD_TPS_Configuration,
"Use of electronic throttle requires accelerator pedal to be redundant."
);
etbErrorCode = (int8_t)TpsState::Redundancy;
return false;
}
}
m_motor = motor;
m_pid.initPidClass(pidParameters);
m_pedalMap = pedalMap;
// Ignore 3% position error before complaining
m_errorAccumulator.init(3.0f, etbPeriodSeconds);
reset();
return true;
}
void EtbController::reset() {
m_shouldResetPid = true;
etbDutyRateOfChange = etbDutyAverage = 0;
m_dutyRocAverage.reset();
m_dutyAverage.reset();
etbTpsErrorCounter = 0;
etbPpsErrorCounter = 0;
}
void EtbController::onConfigurationChange(pid_s* previousConfiguration) {
if (m_motor && !m_pid.isSame(previousConfiguration)) {
m_shouldResetPid = true;
}
m_dutyRocAverage.init(engineConfiguration->etbRocExpAverageLength);
m_dutyAverage.init(engineConfiguration->etbExpAverageLength);
doInitElectronicThrottle();
}
void EtbController::showStatus() {
m_pid.showPidStatus("ETB");
}
expected<percent_t> EtbController::observePlant() {
return Sensor::get(m_positionSensor);
}
void EtbController::setIdlePosition(percent_t pos) {
m_idlePosition = pos;
}
void EtbController::setWastegatePosition(percent_t pos) {
m_wastegatePosition = pos;
}
expected<percent_t> EtbController::getSetpoint() {
switch (m_function) {
case DC_Throttle1:
case DC_Throttle2:
return getSetpointEtb();
case DC_IdleValve:
return getSetpointIdleValve();
case DC_Wastegate:
return getSetpointWastegate();
default:
return unexpected;
}
}
expected<percent_t> EtbController::getSetpointIdleValve() const {
// VW ETB idle mode uses an ETB only for idle (a mini-ETB sets the lower stop, and a normal cable
// can pull the throttle up off the stop.), so we directly control the throttle with the idle position.
#if EFI_TUNER_STUDIO && (EFI_PROD_CODE || EFI_SIMULATOR)
engine->outputChannels.etbTarget = m_idlePosition;
#endif // EFI_TUNER_STUDIO
return clampPercentValue(m_idlePosition);
}
expected<percent_t> EtbController::getSetpointWastegate() const {
return clampPercentValue(m_wastegatePosition);
}
float getSanitizedPedal() {
auto pedalPosition = Sensor::get(SensorType::AcceleratorPedal);
// If the pedal has failed, just use 0 position.
// This is safer than disabling throttle control - we can at least push the throttle closed
// and let the engine idle.
return clampPercentValue(pedalPosition.value_or(0));
}
expected<percent_t> EtbController::getSetpointEtb() {
// Autotune runs with 50% target position
if (m_isAutotune) {
return 50.0f;
}
// // A few extra preconditions if throttle control is invalid
// if (startupPositionError) {
// return unexpected;
// }
// If the pedal map hasn't been set, we can't provide a setpoint.
if (!m_pedalMap) {
return unexpected;
}
float sanitizedPedal = getSanitizedPedal();
float rpm = Sensor::getOrZero(SensorType::Rpm);
etbCurrentTarget = m_pedalMap->getValue(rpm, sanitizedPedal);
percent_t etbIdlePosition = clampPercentValue(m_idlePosition);
percent_t etbIdleAddition = PERCENT_DIV * engineConfiguration->etbIdleThrottleRange * etbIdlePosition;
// Interpolate so that the idle adder just "compresses" the throttle's range upward.
// [0, 100] -> [idle, 100]
// 0% target from table -> idle position as target
// 100% target from table -> 100% target position
idlePosition = interpolateClamped(0, etbIdleAddition, 100, 100, etbCurrentTarget);
percent_t targetPosition = idlePosition + getLuaAdjustment();
#if EFI_ANTILAG_SYSTEM
if (engine->antilagController.isAntilagCondition) {
targetPosition += engineConfiguration->ALSEtbAdd;
}
#endif /* EFI_ANTILAG_SYSTEM */
float vehicleSpeed = Sensor::getOrZero(SensorType::VehicleSpeed);
float wheelSlip = Sensor::getOrZero(SensorType::WheelSlipRatio);
tcEtbDrop = tcEtbDropTable.getValue(wheelSlip, vehicleSpeed);
// Apply any adjustment that this throttle alone needs
// Clamped to +-10 to prevent anything too wild
trim = clampF(-10, getThrottleTrim(rpm, targetPosition), 10);
targetPosition += trim + tcEtbDrop;
// Clamp before rev limiter to avoid ineffective rev limit due to crazy out of range position target
targetPosition = clampPercentValue(targetPosition);
// Lastly, apply ETB rev limiter
auto etbRpmLimit = engineConfiguration->etbRevLimitStart;
if (etbRpmLimit != 0) {
auto fullyLimitedRpm = etbRpmLimit + engineConfiguration->etbRevLimitRange;
float targetPositionBefore = targetPosition;
// Linearly taper throttle to closed from the limit across the range
targetPosition = interpolateClamped(etbRpmLimit, targetPosition, fullyLimitedRpm, 0, rpm);
// rev limit active if the position was changed by rev limiter
etbRevLimitActive = absF(targetPosition - targetPositionBefore) > 0.1f;
}
float minPosition = engineConfiguration->etbMinimumPosition;
// Keep the throttle just barely off the lower stop, and less than the user-configured maximum
float maxPosition = engineConfiguration->etbMaximumPosition;
// Don't allow max position over 100
maxPosition = minF(maxPosition, 100);
targetPosition = clampF(minPosition, targetPosition, maxPosition);
etbCurrentAdjustedTarget = targetPosition;
#if EFI_TUNER_STUDIO
if (m_function == DC_Throttle1) {
engine->outputChannels.etbTarget = targetPosition;
}
#endif // EFI_TUNER_STUDIO
return targetPosition;
}
void EtbController::setLuaAdjustment(float adjustment) {
luaAdjustment = adjustment;
m_luaAdjustmentTimer.reset();
}
float EtbController::getLuaAdjustment() const {
// If the lua position hasn't been set in 0.2 second, don't adjust!
// This avoids a stuck throttle due to hung/rogue/etc Lua script
if (m_luaAdjustmentTimer.getElapsedSeconds() > 0.2f) {
return 0;
} else {
return luaAdjustment;
}
}
percent_t EtbController2::getThrottleTrim(float rpm, percent_t targetPosition) const {
return m_throttle2Trim.getValue(rpm, targetPosition);
}
expected<percent_t> EtbController::getOpenLoop(percent_t target) {
// Don't apply open loop for wastegate/idle valve, only real ETB
if (m_function != DC_Wastegate
&& m_function != DC_IdleValve) {
etbFeedForward = interpolate2d(target, config->etbBiasBins, config->etbBiasValues);
} else {
etbFeedForward = 0;
}
return etbFeedForward;
}
expected<percent_t> EtbController::getClosedLoopAutotune(percent_t target, percent_t actualThrottlePosition) {
// Estimate gain at current position - this should be well away from the spring and in the linear region
// GetSetpoint sets this to 50%
bool isPositive = actualThrottlePosition > target;
float autotuneAmplitude = 20;
// End of cycle - record & reset
if (!isPositive && m_lastIsPositive) {
efitick_t now = getTimeNowNt();
// Determine period
float tu = m_autotuneCycleStart.getElapsedSecondsAndReset(now);
// Determine amplitude
float a = m_maxCycleTps - m_minCycleTps;
// Filter - it's pretty noisy since the ultimate period is not very many loop periods
constexpr float alpha = 0.05;
m_a = alpha * a + (1 - alpha) * m_a;
m_tu = alpha * tu + (1 - alpha) * m_tu;
// Reset bounds
m_minCycleTps = 100;
m_maxCycleTps = 0;
// Math is for Åström–Hägglund (relay) auto tuning
// https://warwick.ac.uk/fac/cross_fac/iatl/reinvention/archive/volume5issue2/hornsey
// Publish to TS state
#if EFI_TUNER_STUDIO
// Amplitude of input (duty cycle %)
float b = 2 * autotuneAmplitude;
// Ultimate gain per A-H relay tuning rule
float ku = 4 * b / (CONST_PI * m_a);
// The multipliers below are somewhere near the "no overshoot"
// and "some overshoot" flavors of the Ziegler-Nichols method
// Kp
float kp = 0.35f * ku;
float ki = 0.25f * ku / m_tu;
float kd = 0.08f * ku * m_tu;
// Every 5 cycles (of the throttle), cycle to the next value
if (m_autotuneCounter >= 5) {
m_autotuneCounter = 0;
m_autotuneCurrentParam = (m_autotuneCurrentParam + 1) % 3; // three ETB calibs: P-I-D
}
m_autotuneCounter++;
// Multiplex 3 signals on to the {mode, value} format
engine->outputChannels.calibrationMode = (uint8_t)static_cast<TsCalMode>((uint8_t)TsCalMode::EtbKp + m_autotuneCurrentParam);
switch (m_autotuneCurrentParam) {
case 0:
engine->outputChannels.calibrationValue = kp;
break;
case 1:
engine->outputChannels.calibrationValue = ki;
break;
case 2:
engine->outputChannels.calibrationValue = kd;
break;
}
// Also output to debug channels if configured
if (engineConfiguration->debugMode == DBG_ETB_AUTOTUNE) {
// a - amplitude of output (TPS %)
engine->outputChannels.debugFloatField1 = m_a;
// b - amplitude of input (Duty cycle %)
engine->outputChannels.debugFloatField2 = b;
// Tu - oscillation period (seconds)
engine->outputChannels.debugFloatField3 = m_tu;
engine->outputChannels.debugFloatField4 = ku;
engine->outputChannels.debugFloatField5 = kp;
engine->outputChannels.debugFloatField6 = ki;
engine->outputChannels.debugFloatField7 = kd;
}
#endif
}
m_lastIsPositive = isPositive;
// Find the min/max of each cycle
if (actualThrottlePosition < m_minCycleTps) {
m_minCycleTps = actualThrottlePosition;
}
if (actualThrottlePosition > m_maxCycleTps) {
m_maxCycleTps = actualThrottlePosition;
}
// Bang-bang control the output to induce oscillation
return autotuneAmplitude * (isPositive ? -1 : 1);
}
expected<percent_t> EtbController::getClosedLoop(percent_t target, percent_t observation) {
if (m_shouldResetPid) {
m_pid.reset();
m_shouldResetPid = false;
}
if (m_isAutotune) {
return getClosedLoopAutotune(target, observation);
} else {
// Check that we're not over the error limit
etbIntegralError = m_errorAccumulator.accumulate(target - observation);
// Allow up to 10 percent-seconds of error
if (etbIntegralError > 10.0f) {
// TODO: figure out how to handle uncalibrated ETB
//getLimpManager()->reportEtbProblem();
}
// Normal case - use PID to compute closed loop part
return m_pid.getOutput(target, observation, etbPeriodSeconds);
}
}
void EtbController::setOutput(expected<percent_t> outputValue) {
#if EFI_TUNER_STUDIO
// Only report first-throttle stats
if (m_function == DC_Throttle1) {
engine->outputChannels.etb1DutyCycle = outputValue.value_or(0);
}
#endif
if (!m_motor) {
return;
}
// If not ETB, or ETB is allowed, output is valid, and we aren't paused, output to motor.
if (!isEtbMode() ||
(getLimpManager()->allowElectronicThrottle()
&& outputValue
&& !engineConfiguration->pauseEtbControl)) {
m_motor->enable();
m_motor->set(ETB_PERCENT_TO_DUTY(outputValue.Value));
} else {
// Otherwise disable the motor.
m_motor->disable("no-ETB");
}
}
bool EtbController::checkStatus() {
#if EFI_TUNER_STUDIO
// Only debug throttle #1
if (m_function == DC_Throttle1) {
m_pid.postState(engine->outputChannels.etbStatus);
} else if (m_function == DC_Wastegate) {
m_pid.postState(engine->outputChannels.wastegateDcStatus);
}
#endif /* EFI_TUNER_STUDIO */
if (!isEtbMode()) {
// no validation for h-bridge or idle mode
return true;
}
// ETB-specific code belo. The whole mix-up between DC and ETB is shameful :(
m_pid.iTermMin = engineConfiguration->etb_iTermMin;
m_pid.iTermMax = engineConfiguration->etb_iTermMax;
// Only allow autotune with stopped engine, and on the first throttle
// Update local state about autotune
m_isAutotune = Sensor::getOrZero(SensorType::Rpm) == 0
&& engine->etbAutoTune
&& m_function == DC_Throttle1;
bool shouldCheckSensorFunction = engine->module<SensorChecker>()->analogSensorsShouldWork();
if (!m_isAutotune && shouldCheckSensorFunction) {
bool isTpsError = !Sensor::get(m_positionSensor).Valid;
// If we have an error that's new, increment the counter
if (isTpsError && !hadTpsError) {
etbTpsErrorCounter++;
}
hadTpsError = isTpsError;
bool isPpsError = !Sensor::get(SensorType::AcceleratorPedal).Valid;
// If we have an error that's new, increment the counter
if (isPpsError && !hadPpsError) {
etbPpsErrorCounter++;
}
hadPpsError = isPpsError;
} else {
// Either sensors are expected to not work, or autotune is running, so reset the error counter
etbTpsErrorCounter = 0;
etbPpsErrorCounter = 0;
}
#ifndef ETB_INTERMITTENT_LIMIT
#define ETB_INTERMITTENT_LIMIT 50
#endif
TpsState localReason = TpsState::None;
if (etbTpsErrorCounter > ETB_INTERMITTENT_LIMIT) {
localReason = TpsState::IntermittentTps;
#if EFI_SHAFT_POSITION_INPUT
} else if (engineConfiguration->disableEtbWhenEngineStopped && !engine->triggerCentral.engineMovedRecently()) {
localReason = TpsState::EngineStopped;
#endif // EFI_SHAFT_POSITION_INPUT
} else if (etbPpsErrorCounter > ETB_INTERMITTENT_LIMIT) {
localReason = TpsState::IntermittentPps;
} else if (engine->engineState.lua.luaDisableEtb) {
localReason = TpsState::Lua;
}
etbErrorCode = (int8_t)localReason;
return localReason == TpsState::None;
}
void EtbController::update() {
#if !EFI_UNIT_TEST
// If we didn't get initialized, fail fast
if (!m_motor) {
return;
}
#endif // EFI_UNIT_TEST
if (!cisnan(directPwmValue)) {
m_motor->set(directPwmValue);
etbErrorCode = (int8_t)TpsState::Manual;
return;
}
bool isOk = checkStatus();
if (!isOk) {
// If engine is stopped and so configured, skip the ETB update entirely
// This is quieter and pulls less power than leaving it on all the time
m_motor->disable("etb status");
return;
}
auto output = ClosedLoopController::update();
if (!output) {
return;
}
checkOutput(output.Value);
}
void EtbController::checkOutput(percent_t output) {
etbDutyAverage = m_dutyAverage.average(absF(output));
etbDutyRateOfChange = m_dutyRocAverage.average(absF(output - prevOutput));
prevOutput = output;
float integrator = absF(m_pid.getIntegration());
auto integratorLimit = engineConfiguration->etbJamIntegratorLimit;
if (integratorLimit != 0) {
auto nowNt = getTimeNowNt();
if (integrator > integratorLimit) {
if (m_jamDetectTimer.hasElapsedSec(engineConfiguration->etbJamTimeout)) {
// ETB is jammed!
jamDetected = true;
// TODO: do something about it!
}
} else {
m_jamDetectTimer.reset(getTimeNowNt());
jamDetected = false;
}
jamTimer = m_jamDetectTimer.getElapsedSeconds(nowNt);
}
}
void EtbController::autoCalibrateTps() {
// Only auto calibrate throttles
if (m_function == DC_Throttle1 || m_function == DC_Throttle2) {
m_isAutocal = true;
}
}
#if !EFI_UNIT_TEST
/**
* Things running on a timer (instead of a thread) don't participate it the RTOS's thread priority system,
* and operate essentially "first come first serve", which risks starvation.
* Since ETB is a safety critical device, we need the hard RTOS guarantee that it will be scheduled over other less important tasks.
*/
#include "periodic_thread_controller.h"
#else
#define chThdSleepMilliseconds(x) {}
#endif // EFI_UNIT_TEST
#include <utility>
template <typename TBase>
struct EtbImpl final : public TBase {
template <typename... TArgs>
EtbImpl(TArgs&&... args) : TBase(std::forward<TArgs>(args)...) { }
void update() override {
#if EFI_TUNER_STUDIO
if (TBase::m_isAutocal) {
// Don't allow if engine is running!
if (Sensor::getOrZero(SensorType::Rpm) > 0) {
TBase::m_isAutocal = false;
return;
}
auto motor = TBase::getMotor();
if (!motor) {
TBase::m_isAutocal = false;
return;
}
auto myFunction = TBase::getFunction();
// First grab open
motor->set(0.5f);
motor->enable();
chThdSleepMilliseconds(1000);
float primaryMax = Sensor::getRaw(functionToTpsSensorPrimary(myFunction));
float secondaryMax = Sensor::getRaw(functionToTpsSensorSecondary(myFunction));
// Let it return
motor->set(0);
chThdSleepMilliseconds(200);
// Now grab closed
motor->set(-0.5f);
chThdSleepMilliseconds(1000);
float primaryMin = Sensor::getRaw(functionToTpsSensorPrimary(myFunction));
float secondaryMin = Sensor::getRaw(functionToTpsSensorSecondary(myFunction));
// Finally disable and reset state
motor->disable("autotune");
// Check that the calibrate actually moved the throttle
if (absF(primaryMax - primaryMin) < 0.5f) {
firmwareError(ObdCode::OBD_TPS_Configuration, "Auto calibrate failed, check your wiring!\r\nClosed voltage: %.1fv Open voltage: %.1fv", primaryMin, primaryMax);
TBase::m_isAutocal = false;
return;
}
// Write out the learned values to TS, waiting briefly after setting each to let TS grab it
engine->outputChannels.calibrationMode = (uint8_t)functionToCalModePriMax(myFunction);
engine->outputChannels.calibrationValue = primaryMax * TPS_TS_CONVERSION;
chThdSleepMilliseconds(500);
engine->outputChannels.calibrationMode = (uint8_t)functionToCalModePriMin(myFunction);
engine->outputChannels.calibrationValue = primaryMin * TPS_TS_CONVERSION;
chThdSleepMilliseconds(500);
engine->outputChannels.calibrationMode = (uint8_t)functionToCalModeSecMax(myFunction);
engine->outputChannels.calibrationValue = secondaryMax * TPS_TS_CONVERSION;
chThdSleepMilliseconds(500);
engine->outputChannels.calibrationMode = (uint8_t)functionToCalModeSecMin(myFunction);
engine->outputChannels.calibrationValue = secondaryMin * TPS_TS_CONVERSION;
chThdSleepMilliseconds(500);
engine->outputChannels.calibrationMode = (uint8_t)TsCalMode::None;
TBase::m_isAutocal = false;
return;
}
#endif /* EFI_TUNER_STUDIO */
TBase::update();
}
};
// real implementation (we mock for some unit tests)
static EtbImpl<EtbController1> etb1;
static EtbImpl<EtbController2> etb2(throttle2TrimTable);
static_assert(ETB_COUNT == 2);
static EtbController* etbControllers[] = { &etb1, &etb2 };
#if !EFI_UNIT_TEST
struct DcThread final : public PeriodicController<512> {
DcThread() : PeriodicController("DC", PRIO_ETB, ETB_LOOP_FREQUENCY) {}
void PeriodicTask(efitick_t) override {
// Simply update all controllers
for (int i = 0 ; i < ETB_COUNT; i++) {
etbControllers[i]->update();
}
}
};
static DcThread dcThread CCM_OPTIONAL;
#endif // !EFI_UNIT_TEST
void etbPidReset() {
for (int i = 0 ; i < ETB_COUNT; i++) {
if (auto controller = engine->etbControllers[i]) {
controller->reset();
}
}
}
#if !EFI_UNIT_TEST
/**
* At the moment there are TWO ways to use this
* set_etb_duty X
* set etb X
* manual duty cycle control without PID. Percent value from 0 to 100
*/
void setThrottleDutyCycle(percent_t level) {
efiPrintf("setting ETB duty=%f%%", level);
if (cisnan(level)) {
directPwmValue = NAN;
return;
}
float dc = ETB_PERCENT_TO_DUTY(level);
directPwmValue = dc;
for (int i = 0 ; i < ETB_COUNT; i++) {
setDcMotorDuty(i, dc);
}
efiPrintf("duty ETB duty=%f", dc);
}
#endif /* EFI_PROD_CODE */
void etbAutocal(size_t throttleIndex) {
if (throttleIndex >= ETB_COUNT) {
return;
}
if (auto etb = engine->etbControllers[throttleIndex]) {
etb->autoCalibrateTps();
}
}
/**
* This specific throttle has default position of about 7% open
*/
static const float boschBiasBins[] = {
0, 1, 5, 7, 14, 65, 66, 100
};
static const float boschBiasValues[] = {
-15, -15, -10, 0, 19, 20, 26, 28
};
void setBoschVAGETB() {
// set tps_min 890
engineConfiguration->tpsMin = 890; // convert 12to10 bit (ADC/4)
// set tps_max 70
engineConfiguration->tpsMax = 70; // convert 12to10 bit (ADC/4)
engineConfiguration->tps1SecondaryMin = 102;
engineConfiguration->tps1SecondaryMax = 891;
engineConfiguration->etb.pFactor = 5.12;
engineConfiguration->etb.iFactor = 47;
engineConfiguration->etb.dFactor = 0.088;
engineConfiguration->etb.offset = 0;
}
void setBoschVNH2SP30Curve() {
copyArray(config->etbBiasBins, boschBiasBins);
copyArray(config->etbBiasValues, boschBiasValues);
}
void setDefaultEtbParameters() {
engineConfiguration->etbIdleThrottleRange = 15;
engineConfiguration->etbExpAverageLength = 50;
engineConfiguration->etbRocExpAverageLength = 50;
setLinearCurve(config->pedalToTpsPedalBins, /*from*/0, /*to*/100, 1);
setRpmTableBin(config->pedalToTpsRpmBins);
for (int pedalIndex = 0;pedalIndex<PEDAL_TO_TPS_SIZE;pedalIndex++) {
for (int rpmIndex = 0;rpmIndex<PEDAL_TO_TPS_SIZE;rpmIndex++) {
config->pedalToTpsTable[pedalIndex][rpmIndex] = config->pedalToTpsPedalBins[pedalIndex];
}
}
// Default is to run each throttle off its respective hbridge
engineConfiguration->etbFunctions[0] = DC_Throttle1;
engineConfiguration->etbFunctions[1] = DC_Throttle2;
engineConfiguration->etbFreq = DEFAULT_ETB_PWM_FREQUENCY;
// voltage, not ADC like with TPS
setPPSCalibration(0, 5, 5, 0);
engineConfiguration->etb = {
1, // Kp
10, // Ki
0.05, // Kd
0, // offset
0, // Update rate, unused
-100, 100 // min/max
};
engineConfiguration->etb_iTermMin = -30;
engineConfiguration->etb_iTermMax = 30;
}
void onConfigurationChangeElectronicThrottleCallback(engine_configuration_s *previousConfiguration) {
for (int i = 0; i < ETB_COUNT; i++) {
etbControllers[i]->onConfigurationChange(&previousConfiguration->etb);
}
}
static const float defaultBiasBins[] = {
0, 1, 2, 4, 7, 98, 99, 100
};
static const float defaultBiasValues[] = {
-20, -18, -17, 0, 20, 21, 22, 25
};
void setDefaultEtbBiasCurve() {
copyArray(config->etbBiasBins, defaultBiasBins);
copyArray(config->etbBiasValues, defaultBiasValues);
}
void unregisterEtbPins() {
// todo: we probably need an implementation here?!
}
static pid_s* getPidForDcFunction(dc_function_e function) {
switch (function) {
case DC_Wastegate: return &engineConfiguration->etbWastegatePid;
default: return &engineConfiguration->etb;
}
}
BOARD_WEAK ValueProvider3D* pedal2TpsProvider() {
return &pedal2tpsMap;
}
void doInitElectronicThrottle() {
bool hasPedal = Sensor::hasSensor(SensorType::AcceleratorPedalPrimary);
#if EFI_UNIT_TEST
printf("doInitElectronicThrottle %s\n", boolToString(hasPedal));
#endif // EFI_UNIT_TEST
// these status flags are consumed by TS see tunerstudio.template.ini TODO should those be outputs/live data not configuration?!
engineConfiguration->etb1configured = engineConfiguration->etb2configured = false;
// todo: technical debt: we still have DC motor code initialization in ETB-specific file while DC motors are used not just as ETB
// like DC motor wastegate code flow should probably NOT go through electronic_throttle.cpp right?
// todo: rename etbFunctions to something-without-etb for same reason?
for (int i = 0 ; i < ETB_COUNT; i++) {
auto func = engineConfiguration->etbFunctions[i];
if (func == DC_None) {
// do not touch HW pins if function not selected, this way Lua can use DC motor hardware pins directly
continue;
}
auto motor = initDcMotor("ETB disable",
engineConfiguration->etbIo[i], i, engineConfiguration->etb_use_two_wires);
auto controller = engine->etbControllers[i];
criticalAssertVoid(controller != nullptr, "null ETB");
auto pid = getPidForDcFunction(func);
bool dcConfigured = controller->init(func, motor, pid, pedal2TpsProvider(), hasPedal);
bool etbConfigured = dcConfigured && controller->isEtbMode();
if (i == 0) {
engineConfiguration->etb1configured = etbConfigured;
} else if (i == 1) {
engineConfiguration->etb2configured = etbConfigured;
}
}
if (!engineConfiguration->etb1configured && !engineConfiguration->etb2configured) {
// It's not valid to have a PPS without any ETBs - check that at least one ETB was enabled along with the pedal
if (hasPedal) {
criticalError("A pedal position sensor was configured, but no electronic throttles are configured.");
}
}
#if 0 && ! EFI_UNIT_TEST
percent_t startupThrottlePosition = getTPS();
if (absF(startupThrottlePosition - engineConfiguration->etbNeutralPosition) > STARTUP_NEUTRAL_POSITION_ERROR_THRESHOLD) {
/**
* Unexpected electronic throttle start-up position is worth a critical error
*/
firmwareError(ObdCode::OBD_Throttle_Actuator_Control_Range_Performance_Bank_1, "startup ETB position %.2f not %d",
startupThrottlePosition,
engineConfiguration->etbNeutralPosition);