rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
electronic_throttle.cpp
Go to the documentation of this file.
1/**
2 * @file electronic_throttle.cpp
3 * @brief Electronic Throttle driver
4 *
5 * @see test test_etb.cpp
6 *
7 * PPS=pedal position sensor=AcceleratorPedal
8 * TPS=throttle position sensor, this one is inside ETB=electronic throttle body
9 *
10 * Limited user documentation at https://github.com/rusefi/rusefi/wiki/HOWTO_electronic_throttle_body
11 *
12 *
13 * ETB is controlled according to pedal position input (pedal position sensor is a potentiometer)
14 * pedal 0% means pedal not pressed / idle
15 * pedal 100% means pedal all the way down
16 * (not TPS - not the one you can calibrate in TunerStudio)
17 *
18 *
19 * See also pid.cpp
20 *
21 * @date Dec 7, 2013
22 * @author Andrey Belomutskiy, (c) 2012-2020
23 *
24 * This file is part of rusEfi - see http://rusefi.com
25 *
26 * rusEfi is free software; you can redistribute it and/or modify it under the terms of
27 * the GNU General Public License as published by the Free Software Foundation; either
28 * version 3 of the License, or (at your option) any later version.
29 *
30 * rusEfi is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
31 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
33 *
34 * You should have received a copy of the GNU General Public License along with this program.
35 * If not, see <http://www.gnu.org/licenses/>.
36 */
37
38#include "pch.h"
39
41
42#if EFI_ELECTRONIC_THROTTLE_BODY
43
44#include "dc_motor.h"
45#include "dc_motors.h"
46#include "defaults.h"
47#include "tunerstudio.h"
49#include "transition_events.h"
50
51#if defined(HAS_OS_ACCESS)
52#error "Unexpected OS ACCESS HERE"
53#endif
54
55#if HW_PROTEUS
56#include "proteus_meta.h"
57#endif // HW_PROTEUS
58
59#ifndef ETB_MAX_COUNT
60#define ETB_MAX_COUNT 2
61#endif /* ETB_MAX_COUNT */
62
63#ifndef ETB_INTERMITTENT_LIMIT
64#define ETB_INTERMITTENT_LIMIT 50
65#endif
66
70
71constexpr float etbPeriodSeconds = 1.0f / ETB_LOOP_FREQUENCY;
72
73//static bool startupPositionError = false;
74
75//#define STARTUP_NEUTRAL_POSITION_ERROR_THRESHOLD 5
76
77static const float hardCodedetbHitachiBiasBins[8] = {0.0, 19.0, 21.0, 22.0, 23.0, 25.0, 30.0, 100.0};
78
79static const float hardCodedetbHitachiBiasValues[8] = {-18.0, -17.0, -15.0, 0.0, 16.0, 20.0, 20.0, 20.0};
80
81/* Generated by TS2C on Thu Aug 20 21:10:02 EDT 2020*/
86
88 switch(func) {
89 case DC_Throttle1: return SensorType::Tps1;
90 case DC_Throttle2: return SensorType::Tps2;
91 case DC_IdleValve: return SensorType::IdlePosition;
92 case DC_Wastegate: return SensorType::WastegatePosition;
93 default: return SensorType::Invalid;
94 }
95}
96
98 switch(func) {
99 case DC_Throttle1: return SensorType::Tps1;
100 case DC_Throttle2: return SensorType::Tps2;
101 case DC_IdleValve: return SensorType::IdlePosition;
102 case DC_Wastegate: return SensorType::WastegatePosition;
103 default: return SensorType::Invalid;
104 }
105}
106
108 switch(func) {
109 case DC_Throttle1: return SensorType::Tps1Primary;
110 case DC_Throttle2: return SensorType::Tps2Primary;
111 case DC_IdleValve: return SensorType::IdlePosition;
112 case DC_Wastegate: return SensorType::WastegatePosition;
113 default: return SensorType::Invalid;
114 }
115}
116
118 switch(func) {
119 case DC_Throttle1: return SensorType::Tps1Secondary;
120 case DC_Throttle2: return SensorType::Tps2Secondary;
121 /* No secondary sensors for Idle and EWG */
122 default: return SensorType::Invalid;
123 }
124}
125
126#if EFI_TUNER_STUDIO
128 switch (func) {
129 case DC_Throttle1: return TsCalMode::Tps1Min;
130 case DC_Throttle2: return TsCalMode::Tps2Min;
131 case DC_Wastegate: return TsCalMode::EwgPosMin;
132 default: return TsCalMode::None;
133 }
134}
135
137 switch (func) {
138 case DC_Throttle1: return TsCalMode::Tps1Max;
139 case DC_Throttle2: return TsCalMode::Tps2Max;
140 case DC_Wastegate: return TsCalMode::EwgPosMax;
141 default: return TsCalMode::None;
142 }
143}
144
146 switch (func) {
147 case DC_Throttle1: return TsCalMode::Tps1SecondaryMin;
148 case DC_Throttle2: return TsCalMode::Tps2SecondaryMin;
149 default: return TsCalMode::None;
150 }
151}
152
154 switch (func) {
155 case DC_Throttle1: return TsCalMode::Tps1SecondaryMax;
156 case DC_Throttle2: return TsCalMode::Tps2SecondaryMax;
157 default: return TsCalMode::None;
158 }
159}
160#endif // EFI_TUNER_STUDIO
161
162#define ETB_DUTY_LIMIT 0.9
163// this macro clamps both positive and negative percentages from about -100% to 100%
164#define ETB_PERCENT_TO_DUTY(x) (clampF(-ETB_DUTY_LIMIT, 0.01f * (x), ETB_DUTY_LIMIT))
165
166PUBLIC_API_WEAK bool isBoardAllowingLackOfPps() {
167 return false;
168}
169
170bool EtbController::init(dc_function_e function, DcMotor *motor, pid_s *pidParameters, const ValueProvider3D* pedalProvider) {
171 state = (uint8_t)EtbState::InInit;
172 if (function == DC_None) {
173 // if not configured, don't init.
174 state = (uint8_t)EtbState::NotEbt;
176 return false;
177 }
178
179 m_function = function;
181
182 // If we are a throttle, require redundant TPS sensor
183 if (isEtbMode()) {
184 // If no sensor is configured for this throttle, skip initialization.
185 if (!Sensor::hasSensor(functionToTpsSensor(function))) {
187 return false;
188 }
189
192 return false;
193 }
194 }
195
196 m_motor = motor;
197 m_pedalProvider = pedalProvider;
198
199 m_pid.initPidClass(pidParameters);
200
201#if !EFI_UNIT_TEST
202 if (isEtbMode()) {
205 } else {
206 // Some defaults from setDefaultEtbParameters(), find better values for EWG and Idle or add config options
207 m_pid.iTermMin = -30;
208 m_pid.iTermMax = 30;
209 }
210#endif
211
212 // Ignore 3% position error before complaining
214
216 return true;
217}
218
219#if EFI_UNIT_TEST
221#endif // EFI_UNIT_TEST
222
223void EtbController::reset(const char *reason) {
224 efiPrintf("ETB reset %s", reason);
225 m_shouldResetPid = true;
228#if EFI_UNIT_TEST
230#endif // EFI_UNIT_TEST
231
232}
233
234// todo: document why is EtbController not engine_module?
235void EtbController::onConfigurationChange(pid_s* previousConfiguration) {
236 if (m_motor && !m_pid.isSame(previousConfiguration)) {
237 efiPrintf(" ETB m_shouldResetPid");
238 m_shouldResetPid = true;
239 }
240
241 doInitElectronicThrottle(/*isStartupInit*/false);
242}
243
247
248expected<percent_t> EtbController::observePlant() {
249 expected<percent_t> plant = Sensor::get(m_positionSensor);
250 validPlantPosition = plant.Valid;
251 return plant;
252}
253
257
261
262expected<percent_t> EtbController::getSetpoint() {
263 switch (m_function) {
264 case DC_Throttle1:
265 case DC_Throttle2:
266 return getSetpointEtb();
267 case DC_IdleValve:
268 return getSetpointIdleValve();
269 case DC_Wastegate:
270 return getSetpointWastegate();
271 default:
272 return unexpected;
273 }
274}
275
276expected<percent_t> EtbController::getSetpointIdleValve() const {
277 // VW ETB idle mode uses an ETB only for idle (a mini-ETB sets the lower stop, and a normal cable
278 // can pull the throttle up off the stop.), so we directly control the throttle with the idle position.
279#if EFI_TUNER_STUDIO && (EFI_PROD_CODE || EFI_SIMULATOR)
280 // todo: where do we want to log this? engine->outputChannels.etbTarget = m_idlePosition;
281#endif // EFI_TUNER_STUDIO
282 return clampPercentValue(m_idlePosition);
283}
284
285expected<percent_t> EtbController::getSetpointWastegate() const {
287
288 return clampPercentValue(targetPosition);
289}
290
292 auto pedalPosition = Sensor::get(SensorType::AcceleratorPedal);
293 // If the pedal has failed, just use 0 position.
294 // This is safer than disabling throttle control - we can at least push the throttle closed
295 // and let the engine idle.
296 return clampPercentValue(pedalPosition.value_or(0));
297}
298
299PUBLIC_API_WEAK float boardAdjustEtbTarget(float currentEtbTarget) {
300 return currentEtbTarget;
301}
302
303expected<percent_t> EtbController::getSetpointEtb() {
304 // Autotune runs with 50% target position
305 if (m_isAutotune) {
306 return 50.0f;
307 }
308
309// // A few extra preconditions if throttle control is invalid
310// if (startupPositionError) {
311// return unexpected;
312// }
313
314 // If the pedal map hasn't been set, we can't provide a setpoint.
315 if (!m_pedalProvider) {
316 state = (uint8_t)EtbState::NoPedal;
317 return unexpected;
318 }
319
320 float sanitizedPedal = getSanitizedPedal();
321
323 percent_t preBoard = m_pedalProvider->getValue(rpm, sanitizedPedal);
326
327 percent_t etbIdlePosition = clampPercentValue(m_idlePosition);
328 percent_t etbIdleAddition = PERCENT_DIV * engineConfiguration->etbIdleThrottleRange * etbIdlePosition;
329
330 // Interpolate so that the idle adder just "compresses" the throttle's range upward.
331 // [0, 100] -> [idle, 100]
332 // 0% target from table -> idle position as target
333 // 100% target from table -> 100% target position
334 targetWithIdlePosition = interpolateClamped(0, etbIdleAddition, 100, 100, etbCurrentTarget);
335
337 // just an additional logging data point
338 adjustedEtbTarget = targetPosition;
339
340#if EFI_ANTILAG_SYSTEM
342 targetPosition += engineConfiguration->ALSEtbAdd;
343 }
344#endif /* EFI_ANTILAG_SYSTEM */
345
346 float vehicleSpeed = Sensor::getOrZero(SensorType::VehicleSpeed);
348 tcEtbDrop = tcEtbDropTable.getValue(wheelSlip, vehicleSpeed);
349
350 // Apply any adjustment that this throttle alone needs
351 // Clamped to +-10 to prevent anything too wild
352 trim = clampF(-10, getThrottleTrim(rpm, targetPosition), 10);
353 targetPosition += trim + tcEtbDrop;
354
355 // Clamp before rev limiter to avoid ineffective rev limit due to crazy out of range position target
356 targetPosition = clampPercentValue(targetPosition);
357
358 // Lastly, apply ETB rev limiter
359 auto etbRpmLimit = engineConfiguration->etbRevLimitStart;
360 if (etbRpmLimit != 0) {
361 auto fullyLimitedRpm = etbRpmLimit + engineConfiguration->etbRevLimitRange;
362
363 float targetPositionBefore = targetPosition;
364 // Linearly taper throttle to closed from the limit across the range
365 targetPosition = interpolateClamped(etbRpmLimit, targetPosition, fullyLimitedRpm, 0, rpm);
366
367 // rev limit active if the position was changed by rev limiter
368 etbRevLimitActive = std::abs(targetPosition - targetPositionBefore) > 0.1f;
369 }
370
371 float minPosition = engineConfiguration->etbMinimumPosition;
372
373 // Keep the throttle just barely off the lower stop, and less than the user-configured maximum
374 float maxPosition = engineConfiguration->etbMaximumPosition;
375 // Don't allow max position over 100
376 maxPosition = std::min(maxPosition, 100.0f);
377
378 targetPosition = clampF(minPosition, targetPosition, maxPosition);
379 m_adjustedTarget = targetPosition;
380
381 return targetPosition;
382}
383
384void EtbController::setLuaAdjustment(float adjustment) {
385 luaAdjustment = adjustment;
386 m_luaAdjustmentTimer.reset();
387}
388
389/**
390 * positive adjustment opens TPS, negative closes TPS
391 */
393 // If the lua position hasn't been set in 0.2 second, don't adjust!
394 // This avoids a stuck throttle due to hung/rogue/etc Lua script
395 if (m_luaAdjustmentTimer.getElapsedSeconds() > 0.2f) {
396 return 0;
397 } else {
398 return luaAdjustment;
399 }
400}
401
402percent_t EtbController2::getThrottleTrim(float rpm, percent_t targetPosition) const {
403 return m_throttle2Trim.getValue(rpm, targetPosition);
404}
405
406expected<percent_t> EtbController::getOpenLoop(percent_t target) {
407 // Don't apply open loop for idle valve, only real ETB or wastegate
408 switch(m_function){
409 case DC_Throttle1:
410 case DC_Throttle2: {
411 etbFeedForward = interpolate2d(target, config->etbBiasBins, config->etbBiasValues);
412 break;
413 }
414 case DC_Wastegate: {
416 break;
417 }
418 case DC_IdleValve: {
419 etbFeedForward = 0;
420 break;
421 }
422 default: { // or DC_None
423 etbFeedForward = 0;
424 }
425 }
426
427 return etbFeedForward;
428}
429
430expected<percent_t> EtbController::getClosedLoopAutotune(percent_t target, percent_t actualThrottlePosition) {
431 // Estimate gain at current position - this should be well away from the spring and in the linear region
432 // GetSetpoint sets this to 50%
433 bool isPositive = actualThrottlePosition > target;
434
435 float autotuneAmplitude = 20;
436
437 // End of cycle - record & reset
438 if (!isPositive && m_lastIsPositive) {
439 // Determine period
440 float tu = m_cycleTimer.getElapsedSecondsAndReset(getTimeNowNt());
441
442 // Determine amplitude
443 float a = m_maxCycleTps - m_minCycleTps;
444
445 // Filter - it's pretty noisy since the ultimate period is not very many loop periods
446 constexpr float alpha = 0.05;
447 m_a = alpha * a + (1 - alpha) * m_a;
448 m_tu = alpha * tu + (1 - alpha) * m_tu;
449
450 // Reset bounds
451 m_minCycleTps = 100;
452 m_maxCycleTps = 0;
453
454 // Math is for Åström–Hägglund (relay) auto tuning
455 // https://warwick.ac.uk/fac/cross_fac/iatl/reinvention/archive/volume5issue2/hornsey
456
457 // Amplitude of input (duty cycle %)
458 float b = 2 * autotuneAmplitude;
459
460 // Ultimate gain per A-H relay tuning rule
461 float ku = 4 * b / (CONST_PI * m_a);
462
463 // The multipliers below are somewhere near the "no overshoot"
464 // and "some overshoot" flavors of the Ziegler-Nichols method
465 // Kp
466 float kp = 0.35f * ku;
467 float ki = 0.25f * ku / m_tu;
468 float kd = 0.08f * ku * m_tu;
469
470 // Publish to TS state
471#if EFI_TUNER_STUDIO
472 // Every 5 cycles (of the throttle), cycle to the next value
473 if (m_autotuneCounter >= 5) {
475 m_autotuneCurrentParam = (m_autotuneCurrentParam + 1) % 3; // three ETB calibs: P-I-D
476 }
477
479
480 switch (m_autotuneCurrentParam) {
481 case 0:
483 break;
484 case 1:
486 break;
487 case 2:
489 break;
490 }
491
492 // Also output to debug channels if configured
493 if (engineConfiguration->debugMode == DBG_ETB_AUTOTUNE) {
494 // a - amplitude of output (TPS %)
496 // b - amplitude of input (Duty cycle %)
498 // Tu - oscillation period (seconds)
500
505 }
506#endif
507 // TODO: directly update PID settings in engineConfiguration
508 }
509
510 m_lastIsPositive = isPositive;
511
512 // Find the min/max of each cycle
513 if (actualThrottlePosition < m_minCycleTps) {
514 m_minCycleTps = actualThrottlePosition;
515 }
516
517 if (actualThrottlePosition > m_maxCycleTps) {
518 m_maxCycleTps = actualThrottlePosition;
519 }
520
521 // Bang-bang control the output to induce oscillation
522 return autotuneAmplitude * (isPositive ? -1 : 1);
523}
524
525expected<percent_t> EtbController::getClosedLoop(percent_t target, percent_t observation) {
526 if (m_shouldResetPid) {
527 m_pid.reset();
529 m_shouldResetPid = false;
530 }
531
532 if (m_isAutotune) {
533 state = (uint8_t)EtbState::Autotune;
534
536
537 return getClosedLoopAutotune(target, observation);
538 } else {
539 checkJam(target, observation);
540
541 integralError = m_targetErrorAccumulator.accumulate(target - observation);
542
543 float dt = m_cycleTimer.getElapsedSecondsAndReset(getTimeNowNt());
544 m_lastPidDtMs = dt * 1000.0;
545
546 // Normal case - use PID to compute closed loop part
547 return m_pid.getOutput(target, observation, dt);
548 }
549}
550
551void EtbController::setOutput(expected<percent_t> outputValue) {
552#if EFI_TUNER_STUDIO
553 // Only report first-throttle stats
554 if (m_function == DC_Throttle1) {
555 engine->outputChannels.etb1DutyCycle = outputValue.value_or(0);
556 }
557#endif
558
559 if (!m_motor) {
560 state = (uint8_t)EtbState::NoMotor;
561 return;
562 }
563
564 bool isEnabled;
565 if (!isEtbMode()) {
566 // technical debt: non-ETB usages of DC motor are still mixed into ETB controller?
567 state = (uint8_t)EtbState::NotEbt;
568 isEnabled = true;
569 } else if (!getLimpManager()->allowElectronicThrottle()) {
571 isEnabled = false;
573 state = (uint8_t)EtbState::Paused;
574 isEnabled = false;
575 } else if (!outputValue) {
576 state = (uint8_t)EtbState::NoOutput;
577 isEnabled = false;
578 } else {
579 state = (uint8_t)EtbState::Active;
580 isEnabled = true;
581 }
582
583 // If not ETB, or ETB is allowed, output is valid, and we aren't paused, output to motor.
584 if (isEnabled) {
585 m_motor->enable();
586 m_motor->set(ETB_PERCENT_TO_DUTY(outputValue.Value));
587 } else {
588 // Otherwise disable the motor.
589 m_motor->disable("no-ETB");
590 }
591}
592
594#if EFI_TUNER_STUDIO
595 // Only debug throttle #1
596 if (m_function == DC_Throttle1) {
598 } else if (m_function == DC_Wastegate) {
600 }
601#endif /* EFI_TUNER_STUDIO */
602
603 if (!isEtbMode()) {
604 // no validation for h-bridge or idle mode
605 return true;
606 }
607 // ETB-specific code belo. The whole mix-up between DC and ETB is shameful :(
608
609 // Only allow autotune with stopped engine, and on the first throttle
610 // Update local state about autotune
613 && m_function == DC_Throttle1;
614
615 bool shouldCheckSensorFunction = engine->module<SensorChecker>()->analogSensorsShouldWork();
616
617 if (!m_isAutotune && shouldCheckSensorFunction) {
618 bool isTpsError = !Sensor::get(m_positionSensor).Valid;
619
620 // If we have an error that's new, increment the counter
621 if (isTpsError && !hadTpsError) {
623 }
624
625 hadTpsError = isTpsError;
626
627 bool isPpsError = !Sensor::get(SensorType::AcceleratorPedal).Valid;
628
629 // If we have an error that's new, increment the counter
630 if (isPpsError && !hadPpsError) {
632 }
633
634 hadPpsError = isPpsError;
635 } else {
636 // Either sensors are expected to not work, or autotune is running, so reset the error counter
639 }
640
641 EtbStatus localReason = EtbStatus::None;
642 if (etbTpsErrorCounter > ETB_INTERMITTENT_LIMIT) {
643 localReason = EtbStatus::IntermittentTps;
644#if EFI_SHAFT_POSITION_INPUT
647 && !engine->etbAutoTune) {
648 localReason = EtbStatus::EngineStopped;
649#endif // EFI_SHAFT_POSITION_INPUT
650 } else if (etbPpsErrorCounter > ETB_INTERMITTENT_LIMIT) {
651 localReason = EtbStatus::IntermittentPps;
652 } else if (engine->engineState.lua.luaDisableEtb) {
653 localReason = EtbStatus::Lua;
654 } else if (!getLimpManager()->allowElectronicThrottle()) {
655 localReason = EtbStatus::JamDetected;
657 localReason = EtbStatus::Redundancy;
658 }
659
660 etbErrorCode = (int8_t)localReason;
661
662 return localReason == EtbStatus::None;
663}
664
666#if !EFI_UNIT_TEST
667 // If we didn't get initialized, fail fast
668 if (!m_motor) {
669 state = (uint8_t)EtbState::FailFast;
670 return;
671 }
672#endif // EFI_UNIT_TEST
673
674 bool isOk = checkStatus();
675
676 if (!isOk) {
677 // If engine is stopped and so configured, skip the ETB update entirely
678 // This is quieter and pulls less power than leaving it on all the time
679 m_motor->disable("etb status");
680 return;
681 }
682
684
685 if (isEtbMode() && !validPlantPosition) {
687 }
688}
689
690void EtbController::checkJam(percent_t setpoint, percent_t observation) {
691 float absError = std::abs(setpoint - observation);
692
693 auto jamDetectThreshold = engineConfiguration->etbJamDetectThreshold;
694 auto jamTimeout = engineConfiguration->etbJamTimeout;
695
696 if (jamDetectThreshold != 0 && jamTimeout != 0) {
697 auto nowNt = getTimeNowNt();
698
699 if (absError > jamDetectThreshold && engine->module<IgnitionController>()->getIgnState()) {
700 if (m_jamDetectTimer.hasElapsedSec(jamTimeout)) {
701 efiPrintf(" ************* ETB is jammed! ***************");
702 jamDetected = true;
703
705 }
706 } else {
707 m_jamDetectTimer.reset(nowNt);
708 jamDetected = false;
709 }
710
711 jamTimer = m_jamDetectTimer.getElapsedSeconds(nowNt);
712 }
713}
714
715#if !EFI_UNIT_TEST
716/**
717 * Things running on a timer (instead of a thread) don't participate it the RTOS's thread priority system,
718 * and operate essentially "first come first serve", which risks starvation.
719 * Since ETB is a safety critical device, we need the hard RTOS guarantee that it will be scheduled over other less important tasks.
720 */
722#endif // EFI_UNIT_TEST
723
724#include <utility>
725
726// real implementation (we mock for some unit tests)
729
731static_assert(ETB_COUNT == sizeof(etbControllers) / sizeof(EtbController*));
732
733void blinkEtbErrorCodes(bool blinkPhase) {
734 for (int i = 0;i<ETB_COUNT;i++) {
738 }
739 etbControllers[i]->etbErrorCodeBlinker = blinkPhase ? 0 : etbErrorCode;
740 }
741}
742
743#if !EFI_UNIT_TEST
744
745struct DcThread final : public PeriodicController<512> {
746 DcThread() : PeriodicController("DC", PRIO_ETB, ETB_LOOP_FREQUENCY) {}
747
748 void PeriodicTask(efitick_t) override {
749 // Simply update all controllers
750 for (int i = 0 ; i < ETB_COUNT; i++) {
751 auto controller = engine->etbControllers[i];
752 assertNotNullVoid(controller);
754 }
755 }
756};
757
758static DcThread dcThread CCM_OPTIONAL;
759
760#endif // !EFI_UNIT_TEST
761
762#if EFI_UNIT_TEST
764 for (int i = 0 ; i < ETB_COUNT; i++) {
765 if (auto controller = engine->etbControllers[i]) {
766 assertNotNullVoid(controller);
767 controller->reset("unit_test");
768 }
769 }
770 ebtResetCounter = 0;
771}
772#endif // EFI_UNIT_TEST
773
774void etbAutocal(dc_function_e function, bool reportToTs) {
775 for (size_t i = 0 ; i < ETB_COUNT; i++) {
776 /* TODO: use from engine, add getFunction() to base class */
777 //if (auto controller = engine->etbControllers[i]) {
778 if (auto controller = etbControllers[i]) {
779 assertNotNullVoid(controller);
780 if (controller->getFunction() == function) {
781 /* TODO: is it possible that we have several controllers with same function? */
782 controller->autoCalibrateTps(reportToTs);
783 // todo fix root cause! work-around: make sure not to write bad tune since that would brick requestBurn();
784 }
785 }
786 }
787}
788
789EtbStatus etbGetState(size_t throttleIndex)
790{
791 if (throttleIndex >= ETB_COUNT) {
793 }
794
795 return (EtbStatus)etbControllers[throttleIndex]->etbErrorCode;
796}
797
798/**
799 * This specific throttle has default position of about 7% open
800 */
801static const float boschBiasBins[] = {
802 0, 1, 5, 7, 14, 65, 66, 100
803};
804static const float boschBiasValues[] = {
805 -15, -15, -10, 0, 19, 20, 26, 28
806};
807
809 engineConfiguration->tpsMin = 890; // convert 12to10 bit (ADC/4)
810 engineConfiguration->tpsMax = 70; // convert 12to10 bit (ADC/4)
811
814
819}
820
825
828
829 setLinearCurve(config->pedalToTpsPedalBins, /*from*/0, /*to*/100, 1);
831
832 for (int pedalIndex = 0;pedalIndex<PEDAL_TO_TPS_SIZE;pedalIndex++) {
833 for (int rpmIndex = 0;rpmIndex<PEDAL_TO_TPS_RPM_SIZE;rpmIndex++) {
834 config->pedalToTpsTable[pedalIndex][rpmIndex] = config->pedalToTpsPedalBins[pedalIndex];
835 }
836 }
837
838 // Default is to run each throttle off its respective hbridge
839 engineConfiguration->etbFunctions[0] = DC_Throttle1;
840 engineConfiguration->etbFunctions[1] = DC_Throttle2;
841
842 engineConfiguration->etbFreq = DEFAULT_ETB_PWM_FREQUENCY;
843
844 // voltage, not ADC like with TPS
845 setPPSCalibration(0, 5, 5, 0);
846
848 1, // Kp
849 10, // Ki
850 0.05, // Kd
851 0, // offset
852 0, // Update rate, unused
853 -100, 100 // min/max
854 };
855
858
860// engineConfiguration->etbJamTimeout = 1;
861}
862
864 for (int i = 0; i < ETB_COUNT; i++) {
865 etbControllers[i]->onConfigurationChange(&previousConfiguration->etb);
866 }
867}
868
869static const float defaultBiasBins[] = {
870 0, 1, 2, 4, 7, 98, 99, 100
871};
872static const float defaultBiasValues[] = {
873 -20, -18, -17, 0, 20, 21, 22, 25
874};
875
882
884 // todo: we probably need an implementation here?!
885}
886
888 switch (function) {
889 case DC_Wastegate: return &engineConfiguration->etbWastegatePid;
890 default: return &engineConfiguration->etb;
891 }
892}
893
895 return &pedal2tpsMap;
896}
897
898void doInitElectronicThrottle(bool isStartupInit) {
899 bool anyEtbConfigured = false;
900
901 // todo: technical debt: we still have DC motor code initialization in ETB-specific file while DC motors are used not just as ETB
902 // like DC motor wastegate code flow should probably NOT go through electronic_throttle.cpp right?
903 // todo: rename etbFunctions to something-without-etb for same reason?
904 for (int i = 0 ; i < ETB_COUNT; i++) {
905 auto func = engineConfiguration->etbFunctions[i];
906 if (func == DC_None) {
907 // do not touch HW pins if function not selected, this way Lua can use DC motor hardware pins directly
908 continue;
909 }
910 auto motor = initDcMotor("ETB disable",
912
913 auto controller = engine->etbControllers[i];
914 criticalAssertVoid(controller != nullptr, "null ETB");
915
916 auto pid = getPidForDcFunction(func);
917
918 bool dcConfigured = controller->init(func, motor, pid, pedal2TpsProvider());
919 if (isStartupInit && dcConfigured) {
920 controller->reset("init");
921 }
922 anyEtbConfigured |= dcConfigured && controller->isEtbMode();
923 }
924
925 // It's not valid to have a PPS without any ETBs - check that at least one ETB was enabled along with the pedal
926 if (!anyEtbConfigured && Sensor::hasSensor(SensorType::AcceleratorPedalPrimary)) {
927 criticalError("A pedal position sensor was configured, but no electronic throttles are configured.");
928 }
929
930#if 0 && ! EFI_UNIT_TEST
931 percent_t startupThrottlePosition = getTPS();
932 if (std::abs(startupThrottlePosition - engineConfiguration->etbNeutralPosition) > STARTUP_NEUTRAL_POSITION_ERROR_THRESHOLD) {
933 /**
934 * Unexpected electronic throttle start-up position is worth a critical error
935 */
937 startupThrottlePosition,
939 startupPositionError = true;
940 }
941#endif /* EFI_UNIT_TEST */
942
943#if !EFI_UNIT_TEST
944 static bool started = false;
945 if (started == false) {
946 dcThread.start();
947 started = true;
948 }
949#endif
950}
951
953 if (hasFirmwareError()) {
954 return;
955 }
956
957 for (int i = 0; i < ETB_COUNT; i++) {
959 }
960
961#if EFI_PROD_CODE
962 addConsoleAction("etbautocal", [](){
963 efiPrintf("etbAutocal invoked");
964 etbAutocal(DC_Throttle1);
965 });
966
967 addConsoleAction("etbinfo", [](){
968 efiPrintf("etbAutoTune=%d", engine->etbAutoTune);
969 efiPrintf("TPS=%.2f", Sensor::getOrZero(SensorType::Tps1));
970
971 efiPrintf("ETB1 duty=%.2f",
973
974 efiPrintf("ETB freq=%d",
976
977 for (int i = 0; i < ETB_COUNT; i++) {
978 efiPrintf("ETB%d", i);
979 efiPrintf(" dir1=%s", hwPortname(engineConfiguration->etbIo[i].directionPin1));
980 efiPrintf(" dir2=%s", hwPortname(engineConfiguration->etbIo[i].directionPin2));
981 efiPrintf(" control=%s", hwPortname(engineConfiguration->etbIo[i].controlPin));
982 efiPrintf(" disable=%s", hwPortname(engineConfiguration->etbIo[i].disablePin));
984 }
985 });
986
987#endif /* EFI_PROD_CODE */
988
992
993 doInitElectronicThrottle(/*isStartupInit*/true);
994}
995
997 for (int i = 0; i < ETB_COUNT; i++) {
998 if (auto etb = engine->etbControllers[i]) {
999 assertNotNullVoid(etb);
1000 etb->setIdlePosition(pos);
1001 }
1002 }
1003}
1004
1006 for (int i = 0; i < ETB_COUNT; i++) {
1007 if (auto etb = engine->etbControllers[i]) {
1008 assertNotNullVoid(etb);
1009 etb->setWastegatePosition(pos);
1010 }
1011 }
1012}
1013
1015 for (int i = 0; i < ETB_COUNT; i++) {
1016 /* TODO: use from engine, add getFunction() to base class */
1017 //if (auto etb = engine->etbControllers[i]) {
1018 if (auto etb = etbControllers[i]) {
1019 assertNotNullVoid(etb);
1020 // try to adjust all ETB
1021 if (etb->getFunction() == DC_Throttle1 || etb->getFunction() == DC_Throttle2) {
1022 etb->setLuaAdjustment(pos);
1023 }
1024 }
1025 }
1026}
1027
1029 for (int i = 0; i < ETB_COUNT; i++) {
1030 /* TODO: use from engine, add getFunction() to base class */
1031 //if (auto etb = engine->etbControllers[i]) {
1032 if (auto etb = etbControllers[i]) {
1033 assertNotNullVoid(etb);
1034 // try to adjust all ETB
1035 if (etb->getFunction() == DC_Wastegate) {
1036 etb->setLuaAdjustment(pos);
1037 }
1038 }
1039 }
1040}
1041
1045
1065
1067#if HW_PROTEUS
1069
1070 // EFI_ADC_12: "Analog Volt 3"
1071 engineConfiguration->tps1_2AdcChannel = PROTEUS_IN_TPS1_2;
1072 // EFI_ADC_13: "Analog Volt 4"
1073 engineConfiguration->tps2_1AdcChannel = PROTEUS_IN_TPS2_1;
1074 // EFI_ADC_0: "Analog Volt 5"
1075 engineConfiguration->tps2_2AdcChannel = PROTEUS_IN_ANALOG_VOLT_5;
1076 setPPSInputs(PROTEUS_IN_ANALOG_VOLT_6, PROTEUS_IN_PPS2);
1077#endif // HW_PROTEUS
1078}
1079
1080#endif /* EFI_ELECTRONIC_THROTTLE_BODY */
1081
1082// So far used by FragmentEntry (LiveData)
1083template<>
1085#if EFI_ELECTRONIC_THROTTLE_BODY
1086 if (idx >= efi::size(etbControllers)) {
1087 return nullptr;
1088 }
1089
1090 return etbControllers[idx];
1091#else
1092 return nullptr;
1093#endif
1094}
Brushed or brushless DC motor interface.
Definition dc_motor.h:20
virtual bool set(float duty)=0
Sets the motor duty cycle.
virtual void enable()=0
virtual void disable(const char *msg)=0
TriggerCentral triggerCentral
Definition engine.h:318
bool etbAutoTune
Definition engine.h:296
EngineState engineState
Definition engine.h:344
AntilagSystemBase antilagController
Definition engine.h:228
IEtbController * etbControllers[ETB_COUNT]
Definition engine.h:126
TunerStudioOutputChannels outputChannels
Definition engine.h:109
constexpr auto & module()
Definition engine.h:200
float accumulate(float error)
void init(float ignoreError, float dt)
const ValueProvider3D & m_throttle2Trim
percent_t getThrottleTrim(float rpm, percent_t) const override
void setWastegatePosition(percent_t pos) override
float getLuaAdjustment() const
bool isEtbMode() const override
expected< percent_t > getSetpointIdleValve() const
expected< percent_t > getSetpointEtb()
void setIdlePosition(percent_t pos) override
void checkJam(percent_t setpoint, percent_t observation)
expected< percent_t > getSetpoint() override
ErrorAccumulator m_targetErrorAccumulator
virtual percent_t getThrottleTrim(float, percent_t) const
expected< percent_t > getClosedLoopAutotune(percent_t setpoint, percent_t actualThrottlePosition)
void update() override
bool init(dc_function_e function, DcMotor *motor, pid_s *pidParameters, const ValueProvider3D *pedalMap) override
void setLuaAdjustment(percent_t adjustment) override
expected< percent_t > getClosedLoop(percent_t setpoint, percent_t observation) override
expected< percent_t > getOpenLoop(percent_t target) override
void reset(const char *reason) override
void onConfigurationChange(pid_s *previousConfiguration)
const ValueProvider3D * m_pedalProvider
expected< percent_t > observePlant() override
expected< percent_t > getSetpointWastegate() const
void setOutput(expected< percent_t > outputValue) override
virtual bool getIgnState() const
void reportEtbProblem()
bool allowElectronicThrottle() const
void initTable(TValueInit(&table)[TRowNum][TColNum], const TXColumnInit(&columnBins)[TColNum], const TRowInit(&rowBins)[TRowNum])
Base class for a controller that needs to run periodically to perform work.
virtual void PeriodicTask(efitick_t nowNt)=0
Called periodically. Override this method to do work for your controller.
float iTermMax
Definition efi_pid.h:69
float iTermMin
Definition efi_pid.h:68
bool isSame(const pid_s *parameters) const
Definition efi_pid.cpp:39
void postState(pid_status_s &pidStatus) const
Definition efi_pid.cpp:144
void showPidStatus(const char *msg) const
Definition efi_pid.cpp:160
virtual void reset()
Definition efi_pid.cpp:103
void initPidClass(pid_s *parameters)
Definition efi_pid.cpp:24
float getOutput(float target, float input)
Definition efi_pid.cpp:56
virtual bool hasSensor() const
Definition sensor.h:141
virtual SensorResult get() const =0
virtual bool isRedundant() const
Definition sensor.h:155
static float getOrZero(SensorType type)
Definition sensor.h:83
bool engineMovedRecently(efitick_t nowNt) const
virtual float getValue(float xColumn, float yRow) const =0
void addConsoleAction(const char *token, Void callback)
Register console action without parameters.
DcMotor * initDcMotor(const char *disPinMsg, const dc_io &io, size_t index, bool useTwoWires)
Definition dc_motors.cpp:90
void showDcMotorInfo(int i)
void setPPSInputs(adc_channel_e pps1, adc_channel_e pps2)
void setPPSCalibration(float primaryUp, float primaryDown, float secondaryUp, float secondaryDown)
float interpolateClamped(float x1, float y1, float x2, float y2, float x)
efitick_t getTimeNowNt()
Definition efitime.cpp:19
void setDefaultEtbParameters()
void setToyota89281_33010_pedal_position_sensor()
static TsCalMode functionToCalModePriMin(dc_function_e func)
void setHitachiEtbBiasBins()
static const float boschBiasValues[]
int ebtResetCounter
void setEtbLuaAdjustment(percent_t pos)
void initElectronicThrottle()
void setHitachiEtbCalibration()
EtbImpl< EtbController2 > etb2(throttle2TrimTable)
constexpr float etbPeriodSeconds
void setEtbIdlePosition(percent_t pos)
static const float boschBiasBins[]
void etbAutocal(dc_function_e function, bool reportToTs)
void setEtbWastegatePosition(percent_t pos)
static Map3D< TRACTION_CONTROL_ETB_DROP_SLIP_SIZE, TRACTION_CONTROL_ETB_DROP_SPEED_SIZE, int8_t, uint16_t, uint8_t > tcEtbDropTable
const electronic_throttle_s * getLiveData(size_t idx)
void onConfigurationChangeElectronicThrottleCallback(engine_configuration_s *previousConfiguration)
static Map3D< ETB2_TRIM_SIZE, ETB2_TRIM_SIZE, int8_t, uint8_t, uint8_t > throttle2TrimTable
void blinkEtbErrorCodes(bool blinkPhase)
static const float defaultBiasValues[]
static SensorType functionToPositionSensor(dc_function_e func)
static pid_s * getPidForDcFunction(dc_function_e function)
EtbStatus etbGetState(size_t throttleIndex)
static TsCalMode functionToCalModeSecMin(dc_function_e func)
void unregisterEtbPins()
PUBLIC_API_WEAK bool isBoardAllowingLackOfPps()
static SensorType functionToTpsSensorPrimary(dc_function_e func)
static EtbController * etbControllers[]
PUBLIC_API_WEAK ValueProvider3D * pedal2TpsProvider()
static const float hardCodedetbHitachiBiasValues[8]
static DcThread dcThread CCM_OPTIONAL
static const float defaultBiasBins[]
static pedal2tps_t pedal2tpsMap
PUBLIC_API_WEAK float boardAdjustEtbTarget(float currentEtbTarget)
void setProteusHitachiEtbDefaults()
float getSanitizedPedal()
static SensorType functionToTpsSensor(dc_function_e func)
static TsCalMode functionToCalModePriMax(dc_function_e func)
static const float hardCodedetbHitachiBiasBins[8]
void etbPidReset()
void setEwgLuaAdjustment(percent_t pos)
EtbImpl< EtbController1 > etb1
static SensorType functionToTpsSensorSecondary(dc_function_e func)
static TsCalMode functionToCalModeSecMax(dc_function_e func)
void setBoschVAGETB()
void doInitElectronicThrottle(bool isStartupInit)
void setBoschVNH2SP30Curve()
void setDefaultEtbBiasCurve()
@ SuccessfulInit
@ LimpProhibited
LimpManager * getLimpManager()
Definition engine.cpp:596
static EngineAccessor engine
Definition engine.h:413
static constexpr persistent_config_s * config
static constexpr engine_configuration_s * engineConfiguration
void firmwareError(ObdCode code, const char *fmt,...)
EtbStatus
@ OBD_Throttle_Actuator_Control_Range_Performance_Bank_1
const char * hwPortname(brain_pin_e brainPin)
TsCalMode
@ Tps1SecondaryMax
@ Tps2SecondaryMax
@ Tps2SecondaryMin
@ Tps1SecondaryMin
dc_function_e
float percent_t
SensorType
Definition sensor_type.h:18
@ AcceleratorPedalPrimary
etbErrorCode("etbErrorCode", SensorCategory.SENSOR_INPUTS, FieldType.INT8, 1864, 1.0, -1.0, -1.0, "")
scaled_channel< uint16_t, 100, 1 > jamTimer
scaled_channel< int16_t, 100, 1 > m_adjustedTarget
uint8_t tractionControlSpeedBins[TRACTION_CONTROL_ETB_DROP_SPEED_SIZE]
scaled_channel< uint16_t, 100, 1 > tractionControlSlipBins[TRACTION_CONTROL_ETB_DROP_SLIP_SIZE]
int8_t tractionControlEtbDrop[TRACTION_CONTROL_ETB_DROP_SLIP_SIZE][TRACTION_CONTROL_ETB_DROP_SPEED_SIZE]
scaled_channel< int16_t, 100, 1 > etb1DutyCycle
scaled_channel< int16_t, 100, 1 > dcWastegateBiasValues[ETB_BIAS_CURVE_LENGTH]
uint8_t pedalToTpsTable[PEDAL_TO_TPS_SIZE][PEDAL_TO_TPS_RPM_SIZE]
scaled_channel< uint8_t, 1, 100 > throttle2TrimRpmBins[ETB2_TRIM_SIZE]
scaled_channel< uint8_t, 1, 100 > pedalToTpsRpmBins[PEDAL_TO_TPS_RPM_SIZE]
scaled_channel< int8_t, 10, 1 > throttle2TrimTable[ETB2_TRIM_SIZE][ETB2_TRIM_SIZE]
void setRpmTableBin(TValue(&array)[TSize])
void setLinearCurve(TValue(&array)[TSize], float from, float to, float precision=0.01f)
void onTransitionEvent(TransitionEvent event)
void tsCalibrationSetData(TsCalMode mode, float value, float value2, float timeoutMs)
maintainConstantValue implementation header