2015-07-10 06:01:56 -07:00
|
|
|
/**
|
|
|
|
* @file electronic_throttle.h
|
|
|
|
*
|
|
|
|
* @date Dec 7, 2013
|
2020-01-07 21:02:40 -08:00
|
|
|
* @author Andrey Belomutskiy, (c) 2012-2020
|
2015-07-10 06:01:56 -07:00
|
|
|
*/
|
|
|
|
|
2019-10-18 16:39:06 -07:00
|
|
|
#pragma once
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2019-04-24 20:46:49 -07:00
|
|
|
// https://en.wikipedia.org/wiki/Nyquist%E2%80%93Shannon_sampling_theorem
|
|
|
|
#define DEFAULT_ETB_LOOP_FREQUENCY 200
|
|
|
|
#define DEFAULT_ETB_PWM_FREQUENCY 300
|
|
|
|
|
2017-05-27 20:01:41 -07:00
|
|
|
#include "engine.h"
|
2020-04-19 14:18:47 -07:00
|
|
|
#include "closed_loop_controller.h"
|
|
|
|
#include "expected.h"
|
2019-09-22 14:58:27 -07:00
|
|
|
#include "periodic_task.h"
|
|
|
|
|
2019-11-22 17:53:54 -08:00
|
|
|
class DcMotor;
|
2019-12-10 16:37:04 -08:00
|
|
|
class Logging;
|
2019-11-20 21:49:38 -08:00
|
|
|
|
2020-04-19 17:32:41 -07:00
|
|
|
class IEtbController : public PeriodicTimerController, public ClosedLoopController<percent_t, percent_t> {
|
2019-09-22 14:58:27 -07:00
|
|
|
public:
|
|
|
|
DECLARE_ENGINE_PTR;
|
2020-04-20 11:34:45 -07:00
|
|
|
virtual void init(DcMotor *motor, int ownIndex, pid_s *pidParameters, const ValueProvider3D* pedalMap) = 0;
|
2019-12-13 10:52:34 -08:00
|
|
|
virtual void reset() = 0;
|
2020-04-20 13:26:35 -07:00
|
|
|
virtual void setIdlePosition(percent_t pos) = 0;
|
2019-12-13 10:52:34 -08:00
|
|
|
};
|
|
|
|
|
2020-04-19 17:32:41 -07:00
|
|
|
class EtbController final : public IEtbController {
|
2019-12-13 10:52:34 -08:00
|
|
|
public:
|
2020-04-20 11:34:45 -07:00
|
|
|
void init(DcMotor *motor, int ownIndex, pid_s *pidParameters, const ValueProvider3D* pedalMap) override;
|
2020-04-20 13:26:35 -07:00
|
|
|
void setIdlePosition(percent_t pos) override;
|
2019-09-22 14:58:27 -07:00
|
|
|
|
2019-12-10 16:37:04 -08:00
|
|
|
// PeriodicTimerController implementation
|
2019-09-22 14:58:27 -07:00
|
|
|
int getPeriodMs() override;
|
|
|
|
void PeriodicTask() override;
|
2019-12-13 10:52:34 -08:00
|
|
|
void reset() override;
|
2019-12-10 16:37:04 -08:00
|
|
|
|
|
|
|
// Called when the configuration may have changed. Controller will
|
|
|
|
// reset if necessary.
|
|
|
|
void onConfigurationChange(pid_s* previousConfiguration);
|
|
|
|
|
|
|
|
// Print this throttle's status.
|
|
|
|
void showStatus(Logging* logger);
|
|
|
|
|
2020-04-19 14:18:47 -07:00
|
|
|
// Helpers for individual parts of throttle control
|
|
|
|
expected<percent_t> observePlant() const override;
|
|
|
|
expected<percent_t> getSetpoint() const override;
|
|
|
|
|
|
|
|
expected<percent_t> getOpenLoop(percent_t target) const override;
|
|
|
|
expected<percent_t> getClosedLoop(percent_t setpoint, percent_t target) override;
|
2020-04-22 19:22:28 -07:00
|
|
|
expected<percent_t> getClosedLoopAutotune(percent_t actualThrottlePosition);
|
2020-04-19 14:18:47 -07:00
|
|
|
|
|
|
|
void setOutput(expected<percent_t> outputValue) override;
|
|
|
|
|
2019-12-10 16:37:04 -08:00
|
|
|
// Used to inspect the internal PID controller's state
|
|
|
|
const pid_state_s* getPidState() const { return &m_pid; };
|
2019-11-22 17:53:54 -08:00
|
|
|
|
|
|
|
private:
|
2020-04-20 11:34:45 -07:00
|
|
|
int m_myIndex = 0;
|
|
|
|
DcMotor *m_motor = nullptr;
|
2019-12-10 16:37:04 -08:00
|
|
|
Pid m_pid;
|
|
|
|
bool m_shouldResetPid = false;
|
2020-04-11 19:15:49 -07:00
|
|
|
|
2020-04-20 11:34:45 -07:00
|
|
|
// Pedal -> target map
|
|
|
|
const ValueProvider3D* m_pedalMap = nullptr;
|
|
|
|
|
2020-04-20 13:26:35 -07:00
|
|
|
float m_idlePosition = 0;
|
|
|
|
|
2020-04-11 19:15:49 -07:00
|
|
|
// Autotune helpers
|
|
|
|
bool m_lastIsPositive = false;
|
|
|
|
efitick_t m_cycleStartTime = 0;
|
|
|
|
float m_minCycleTps = 0;
|
|
|
|
float m_maxCycleTps = 0;
|
2020-04-11 19:28:50 -07:00
|
|
|
float m_a = 0;
|
|
|
|
float m_tu = 0;
|
2019-09-22 14:58:27 -07:00
|
|
|
};
|
|
|
|
|
2019-09-21 21:16:46 -07:00
|
|
|
void initElectronicThrottle(DECLARE_ENGINE_PARAMETER_SIGNATURE);
|
2020-04-01 21:37:02 -07:00
|
|
|
void doInitElectronicThrottle(DECLARE_ENGINE_PARAMETER_SIGNATURE);
|
2020-04-20 13:26:35 -07:00
|
|
|
void setEtbIdlePosition(percent_t pos DECLARE_ENGINE_PARAMETER_SUFFIX);
|
2019-12-13 10:52:34 -08:00
|
|
|
|
2019-09-21 21:16:46 -07:00
|
|
|
void setDefaultEtbBiasCurve(DECLARE_CONFIG_PARAMETER_SIGNATURE);
|
|
|
|
void setDefaultEtbParameters(DECLARE_CONFIG_PARAMETER_SIGNATURE);
|
|
|
|
void setBoschVNH2SP30Curve(DECLARE_CONFIG_PARAMETER_SIGNATURE);
|
2017-01-06 14:01:28 -08:00
|
|
|
void setEtbPFactor(float value);
|
|
|
|
void setEtbIFactor(float value);
|
2018-09-24 20:57:03 -07:00
|
|
|
void setEtbDFactor(float value);
|
2018-11-26 19:17:16 -08:00
|
|
|
void setEtbOffset(int value);
|
2019-07-12 04:48:28 -07:00
|
|
|
void setThrottleDutyCycle(percent_t level);
|
2017-05-27 20:01:41 -07:00
|
|
|
void onConfigurationChangeElectronicThrottleCallback(engine_configuration_s *previousConfiguration);
|
2019-03-10 09:58:27 -07:00
|
|
|
void unregisterEtbPins();
|