2015-07-10 06:01:56 -07:00
|
|
|
/**
|
|
|
|
* @file pid.h
|
|
|
|
*
|
|
|
|
* @date Sep 16, 2014
|
2020-01-13 18:57:43 -08:00
|
|
|
* @author Andrey Belomutskiy, (c) 2012-2020
|
2015-07-10 06:01:56 -07:00
|
|
|
*/
|
|
|
|
|
2020-04-01 18:32:21 -07:00
|
|
|
#pragma once
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2019-09-01 10:56:46 -07:00
|
|
|
#include "pid_state_generated.h"
|
2017-05-25 05:49:04 -07:00
|
|
|
|
2017-12-24 18:17:10 -08:00
|
|
|
// See PidCic below
|
|
|
|
#define PID_AVG_BUF_SIZE_SHIFT 5
|
|
|
|
#define PID_AVG_BUF_SIZE (1<<PID_AVG_BUF_SIZE_SHIFT) // 32*sizeof(float)
|
|
|
|
|
2019-04-25 18:31:33 -07:00
|
|
|
#define NOT_TIME_BASED_PID 1
|
|
|
|
|
2019-04-25 15:49:16 -07:00
|
|
|
// minimal period 5m meaning maximum control frequency 200Hz
|
|
|
|
#define PID_MINIMAL_PERIOD_MS 5
|
|
|
|
|
|
|
|
#define GET_PERIOD_LIMITED(pid_s_ptr) maxI(PID_MINIMAL_PERIOD_MS, ((pid_s_ptr)->periodMs))
|
|
|
|
|
|
|
|
#define MS2SEC(x) (x * 0.001)
|
|
|
|
|
2019-11-22 12:55:38 -08:00
|
|
|
struct pid_s;
|
|
|
|
|
2020-07-10 11:19:51 -07:00
|
|
|
/**
|
|
|
|
* default basic implementation also known as PidParallelController
|
|
|
|
*/
|
2019-08-15 18:01:04 -07:00
|
|
|
class Pid : public pid_state_s {
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
public:
|
2020-11-18 16:51:51 -08:00
|
|
|
DECLARE_ENGINE_PTR;
|
|
|
|
|
2016-02-06 09:02:24 -08:00
|
|
|
Pid();
|
2019-09-01 15:41:51 -07:00
|
|
|
explicit Pid(pid_s *parameters);
|
|
|
|
void initPidClass(pid_s *parameters);
|
2019-11-22 12:55:38 -08:00
|
|
|
bool isSame(const pid_s *parameters) const;
|
2016-02-06 09:02:24 -08:00
|
|
|
|
2019-03-12 15:54:46 -07:00
|
|
|
/**
|
2019-07-21 14:05:41 -07:00
|
|
|
* This version of the method takes dTime from pid_s
|
2019-04-25 18:36:41 -07:00
|
|
|
*
|
2019-03-12 15:54:46 -07:00
|
|
|
* @param Controller input / process output
|
|
|
|
* @returns Output from the PID controller / the input to the process
|
|
|
|
*/
|
|
|
|
float getOutput(float target, float input);
|
|
|
|
virtual float getOutput(float target, float input, float dTime);
|
2017-12-24 18:17:10 -08:00
|
|
|
// doesn't limit the result (used in incremental CIC PID, see below)
|
2019-03-12 15:54:46 -07:00
|
|
|
float getUnclampedOutput(float target, float input, float dTime);
|
2015-07-10 06:01:56 -07:00
|
|
|
void updateFactors(float pFactor, float iFactor, float dFactor);
|
2017-12-24 18:17:10 -08:00
|
|
|
virtual void reset(void);
|
2019-06-08 06:51:36 -07:00
|
|
|
float getP(void) const;
|
|
|
|
float getI(void) const;
|
|
|
|
float getD(void) const;
|
2019-09-11 17:23:25 -07:00
|
|
|
virtual float getOffset(void) const;
|
|
|
|
virtual float getMinValue(void) const;
|
2019-06-08 06:51:36 -07:00
|
|
|
float getIntegration(void) const;
|
|
|
|
float getPrevError(void) const;
|
2018-03-30 05:42:13 -07:00
|
|
|
void setErrorAmplification(float coef);
|
2019-04-12 19:10:57 -07:00
|
|
|
#if EFI_TUNER_STUDIO
|
2019-12-16 17:36:40 -08:00
|
|
|
void postState(TunerStudioOutputChannels *tsOutputChannels) const;
|
|
|
|
void postState(TunerStudioOutputChannels *tsOutputChannels, int pMult) const;
|
2018-11-16 05:08:20 -08:00
|
|
|
#endif /* EFI_TUNER_STUDIO */
|
2021-04-18 17:02:32 -07:00
|
|
|
void showPidStatus(const char* msg) const;
|
2017-05-28 19:10:35 -07:00
|
|
|
void sleep();
|
2017-06-02 18:34:00 -07:00
|
|
|
int resetCounter;
|
2019-03-02 11:00:32 -08:00
|
|
|
// todo: move this to pid_s one day
|
|
|
|
float iTermMin = -1000000.0;
|
|
|
|
float iTermMax = 1000000.0;
|
2019-09-11 17:46:50 -07:00
|
|
|
protected:
|
2019-09-01 15:41:51 -07:00
|
|
|
pid_s *parameters;
|
2017-12-24 18:17:10 -08:00
|
|
|
virtual void updateITerm(float value);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2019-04-25 21:45:53 -07:00
|
|
|
* A PID implementation with a modified cascaded integrator-comb (CIC) filtering.
|
2017-12-24 18:17:10 -08:00
|
|
|
* Used for incremental auto-IAC control. See autoIdle() in idle_thread.cpp
|
2019-09-12 04:30:18 -07:00
|
|
|
* See pid_cic.md.
|
2017-12-24 18:26:42 -08:00
|
|
|
*
|
|
|
|
* https://rusefi.com/forum/viewtopic.php?f=9&t=1315
|
2017-12-24 18:17:10 -08:00
|
|
|
*/
|
|
|
|
class PidCic : public Pid {
|
|
|
|
|
|
|
|
public:
|
|
|
|
PidCic();
|
2019-06-08 06:51:36 -07:00
|
|
|
explicit PidCic(pid_s *pid);
|
2017-12-24 18:17:10 -08:00
|
|
|
|
2019-06-08 06:51:36 -07:00
|
|
|
void reset(void) override;
|
2019-04-25 21:45:53 -07:00
|
|
|
using Pid::getOutput;
|
2019-06-08 06:51:36 -07:00
|
|
|
float getOutput(float target, float input, float dTime) override;
|
2017-12-24 18:17:10 -08:00
|
|
|
|
|
|
|
private:
|
|
|
|
// Circular running-average buffer for I-term, used by CIC-like filter
|
|
|
|
float iTermBuf[PID_AVG_BUF_SIZE];
|
|
|
|
// Needed by averaging (smoothing) of iTerm sums
|
|
|
|
float iTermInvNum;
|
|
|
|
// Total PID iterations (>240 days max. for 10ms update period)
|
|
|
|
int totalItermCnt;
|
|
|
|
|
|
|
|
private:
|
2019-06-08 06:51:36 -07:00
|
|
|
void updateITerm(float value) override;
|
2015-07-10 06:01:56 -07:00
|
|
|
};
|
|
|
|
|
2019-11-10 10:04:27 -08:00
|
|
|
/**
|
|
|
|
* A PID with derivative filtering (backward differences) and integrator anti-windup.
|
|
|
|
* See: Wittenmark B., Astrom K., Arzen K. IFAC Professional Brief. Computer Control: An Overview.
|
|
|
|
* Two additional parameters used: derivativeFilterLoss and antiwindupFreq
|
|
|
|
* (If both are 0, then this controller is identical to PidParallelController)
|
2020-07-10 11:19:51 -07:00
|
|
|
*
|
|
|
|
* TODO: should PidIndustrial replace all usages of Pid/PidParallelController?
|
2019-11-10 10:04:27 -08:00
|
|
|
*/
|
|
|
|
class PidIndustrial : public Pid {
|
|
|
|
public:
|
2019-11-10 13:07:13 -08:00
|
|
|
PidIndustrial();
|
|
|
|
explicit PidIndustrial(pid_s *pid);
|
|
|
|
|
2019-11-10 10:04:27 -08:00
|
|
|
using Pid::getOutput;
|
|
|
|
float getOutput(float target, float input, float dTime) override;
|
|
|
|
|
|
|
|
public:
|
|
|
|
// todo: move this to pid_s one day
|
2019-12-16 17:36:40 -08:00
|
|
|
float antiwindupFreq = 0.0f; // = 1/ResetTime
|
|
|
|
float derivativeFilterLoss = 0.0f; // = 1/Gain
|
2019-11-10 10:04:27 -08:00
|
|
|
|
|
|
|
private:
|
|
|
|
float limitOutput(float v) const;
|
|
|
|
};
|