2020-12-06 22:27:27 -08:00
|
|
|
/**
|
|
|
|
* @file electronic_throttle_impl.h
|
|
|
|
*
|
|
|
|
* @date Dec 7, 2013
|
|
|
|
* @author Andrey Belomutskiy, (c) 2012-2020
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
// include the "public" ETB interface
|
|
|
|
#include "electronic_throttle.h"
|
|
|
|
|
|
|
|
#include "sensor.h"
|
2022-01-31 18:51:32 -08:00
|
|
|
#include "efi_pid.h"
|
2022-04-14 12:25:01 -07:00
|
|
|
#include "electronic_throttle_generated.h"
|
2020-12-06 22:27:27 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Hard code ETB update speed.
|
|
|
|
* Since this is a safety critical system with no real reason for a user to ever need to change the update rate,
|
|
|
|
* it's locked to 500hz, along with the ADC.
|
|
|
|
* https://en.wikipedia.org/wiki/Nyquist%E2%80%93Shannon_sampling_theorem
|
|
|
|
*/
|
|
|
|
#define ETB_LOOP_FREQUENCY 500
|
|
|
|
#define DEFAULT_ETB_PWM_FREQUENCY 800
|
|
|
|
|
2022-04-16 18:03:50 -07:00
|
|
|
class EtbController : public IEtbController, public electronic_throttle_s {
|
2020-12-06 22:27:27 -08:00
|
|
|
public:
|
2024-11-11 17:44:11 -08:00
|
|
|
bool init(dc_function_e function, DcMotor *motor, pid_s *pidParameters, const ValueProvider3D* pedalMap) override;
|
2020-12-06 22:27:27 -08:00
|
|
|
void setIdlePosition(percent_t pos) override;
|
|
|
|
void setWastegatePosition(percent_t pos) override;
|
|
|
|
void reset() override;
|
|
|
|
|
|
|
|
// Update the controller's state: read sensors, send output, etc
|
2021-06-25 17:58:38 -07:00
|
|
|
void update() override;
|
2020-12-06 22:27:27 -08:00
|
|
|
|
|
|
|
// Called when the configuration may have changed. Controller will
|
|
|
|
// reset if necessary.
|
|
|
|
void onConfigurationChange(pid_s* previousConfiguration);
|
2024-05-04 09:08:16 -07:00
|
|
|
|
2020-12-06 22:27:27 -08:00
|
|
|
// Print this throttle's status.
|
2021-04-18 17:02:32 -07:00
|
|
|
void showStatus();
|
2020-12-06 22:27:27 -08:00
|
|
|
|
|
|
|
// Helpers for individual parts of throttle control
|
2023-10-19 18:06:09 -07:00
|
|
|
expected<percent_t> observePlant() override;
|
2020-12-06 22:27:27 -08:00
|
|
|
|
2021-11-23 12:52:43 -08:00
|
|
|
expected<percent_t> getSetpoint() override;
|
2022-04-14 12:25:01 -07:00
|
|
|
expected<percent_t> getSetpointEtb();
|
2023-02-17 19:17:09 -08:00
|
|
|
expected<percent_t> getSetpointWastegate() const;
|
2020-12-06 22:27:27 -08:00
|
|
|
expected<percent_t> getSetpointIdleValve() const;
|
|
|
|
|
2021-11-23 12:52:43 -08:00
|
|
|
expected<percent_t> getOpenLoop(percent_t target) override;
|
2020-12-06 22:27:27 -08:00
|
|
|
expected<percent_t> getClosedLoop(percent_t setpoint, percent_t observation) override;
|
2021-03-15 05:54:55 -07:00
|
|
|
expected<percent_t> getClosedLoopAutotune(percent_t setpoint, percent_t actualThrottlePosition);
|
2020-12-06 22:27:27 -08:00
|
|
|
|
2024-09-17 16:39:16 -07:00
|
|
|
void checkJam(percent_t setpoint, percent_t observation);
|
2020-12-06 22:27:27 -08:00
|
|
|
|
2024-09-17 16:39:16 -07:00
|
|
|
void setOutput(expected<percent_t> outputValue) override;
|
2023-01-10 12:31:04 -08:00
|
|
|
|
2020-12-06 22:27:27 -08:00
|
|
|
// Used to inspect the internal PID controller's state
|
2023-01-10 12:31:04 -08:00
|
|
|
const pid_state_s& getPidState() const override { return m_pid; };
|
2020-12-06 22:27:27 -08:00
|
|
|
|
|
|
|
// Use the throttle to automatically calibrate the relevant throttle position sensor(s).
|
|
|
|
void autoCalibrateTps() override;
|
|
|
|
|
2022-03-15 17:04:49 -07:00
|
|
|
// Override if this throttle needs special per-throttle adjustment (bank-to-bank trim, for example)
|
|
|
|
virtual percent_t getThrottleTrim(float /*rpm*/, percent_t /*targetPosition*/) const {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-06-17 18:46:56 -07:00
|
|
|
float getCurrentTarget() const override {
|
|
|
|
return etbCurrentTarget;
|
|
|
|
}
|
|
|
|
|
2022-07-09 19:46:28 -07:00
|
|
|
// Lua throttle adjustment
|
|
|
|
void setLuaAdjustment(percent_t adjustment) override;
|
|
|
|
float getLuaAdjustment() const;
|
|
|
|
|
2022-11-29 08:42:09 -08:00
|
|
|
float prevOutput = 0;
|
|
|
|
|
2020-12-06 22:27:27 -08:00
|
|
|
protected:
|
|
|
|
// This is set if an automatic TPS calibration should be run
|
|
|
|
bool m_isAutocal = false;
|
|
|
|
|
2022-11-30 19:20:09 -08:00
|
|
|
bool hadTpsError = false;
|
|
|
|
bool hadPpsError = false;
|
|
|
|
|
2023-02-18 19:39:45 -08:00
|
|
|
dc_function_e getFunction() const { return m_function; }
|
2020-12-06 22:27:27 -08:00
|
|
|
DcMotor* getMotor() { return m_motor; }
|
|
|
|
|
|
|
|
private:
|
2023-02-18 19:39:45 -08:00
|
|
|
dc_function_e m_function = DC_None;
|
2020-12-06 22:27:27 -08:00
|
|
|
SensorType m_positionSensor = SensorType::Invalid;
|
|
|
|
DcMotor *m_motor = nullptr;
|
|
|
|
Pid m_pid;
|
|
|
|
bool m_shouldResetPid = false;
|
|
|
|
|
2023-06-01 11:09:30 -07:00
|
|
|
/**
|
|
|
|
* @return true if OK, false if should be disabled
|
|
|
|
*/
|
|
|
|
bool checkStatus();
|
2024-05-04 09:08:16 -07:00
|
|
|
bool isEtbMode() const override {
|
2023-06-01 11:09:30 -07:00
|
|
|
return m_function == DC_Throttle1 || m_function == DC_Throttle2;
|
|
|
|
}
|
2023-01-17 01:14:30 -08:00
|
|
|
|
2023-01-10 12:31:04 -08:00
|
|
|
Timer m_jamDetectTimer;
|
|
|
|
|
2020-12-06 22:27:27 -08:00
|
|
|
// Pedal -> target map
|
2024-06-17 18:08:10 -07:00
|
|
|
const ValueProvider3D* m_pedalProvider = nullptr;
|
2020-12-06 22:27:27 -08:00
|
|
|
|
|
|
|
float m_idlePosition = 0;
|
|
|
|
|
2023-02-17 19:25:02 -08:00
|
|
|
// This is set if automatic PID cal should be run
|
2021-03-15 05:54:55 -07:00
|
|
|
bool m_isAutotune = false;
|
|
|
|
|
2020-12-06 22:27:27 -08:00
|
|
|
// Autotune helpers
|
|
|
|
bool m_lastIsPositive = false;
|
2024-04-25 15:07:21 -07:00
|
|
|
Timer m_autotuneCycleStart;
|
2020-12-06 22:27:27 -08:00
|
|
|
float m_minCycleTps = 0;
|
|
|
|
float m_maxCycleTps = 0;
|
|
|
|
// Autotune measured parameters: gain and ultimate period
|
|
|
|
// These are set to correct order of magnitude starting points
|
|
|
|
// so we converge more quickly on the correct values
|
|
|
|
float m_a = 8;
|
2024-05-04 09:08:16 -07:00
|
|
|
float m_tu = 0.1f;
|
2020-12-06 22:27:27 -08:00
|
|
|
|
|
|
|
uint8_t m_autotuneCounter = 0;
|
|
|
|
uint8_t m_autotuneCurrentParam = 0;
|
2022-07-09 19:46:28 -07:00
|
|
|
|
|
|
|
Timer m_luaAdjustmentTimer;
|
2020-12-06 22:27:27 -08:00
|
|
|
};
|
2022-03-15 17:04:49 -07:00
|
|
|
|
2022-11-29 11:59:08 -08:00
|
|
|
void etbPidReset();
|
|
|
|
|
2022-03-15 17:04:49 -07:00
|
|
|
class EtbController1 : public EtbController { };
|
|
|
|
|
|
|
|
class EtbController2 : public EtbController {
|
|
|
|
public:
|
2022-08-28 05:28:20 -07:00
|
|
|
EtbController2(const ValueProvider3D& throttle2TrimTable)
|
|
|
|
: m_throttle2Trim(throttle2TrimTable)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-03-15 17:04:49 -07:00
|
|
|
percent_t getThrottleTrim(float rpm, percent_t /*targetPosition*/) const override;
|
2022-08-28 05:28:20 -07:00
|
|
|
|
|
|
|
private:
|
|
|
|
const ValueProvider3D& m_throttle2Trim;
|
2022-03-15 17:04:49 -07:00
|
|
|
};
|