rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
Public Member Functions | Data Fields | Protected Member Functions | Protected Attributes
Pid Class Reference

#include <efi_pid.h>

Inheritance diagram for Pid:
Inheritance graph
[legend]
Collaboration diagram for Pid:
Collaboration graph
[legend]

Public Member Functions

 Pid ()
 
 Pid (pid_s *parameters)
 
void initPidClass (pid_s *parameters)
 
bool isSame (const pid_s *parameters) const
 
float getOutput (float target, float input)
 
virtual float getOutput (float target, float input, float dTime)
 
float getUnclampedOutput (float target, float input, float dTime)
 
void updateFactors (float pFactor, float iFactor, float dFactor)
 
virtual void reset ()
 
float getP () const
 
float getI () const
 
float getD () const
 
float getOffset () const
 
float getMinValue () const
 
float getIntegration (void) const
 
float getPrevError (void) const
 
void setErrorAmplification (float coef)
 
void postState (pid_status_s &pidStatus) const
 
void showPidStatus (const char *msg) const
 
void sleep ()
 

Data Fields

int resetCounter
 
float iTermMin = -1000000.0
 
float iTermMax = 1000000.0
 
- Data Fields inherited from pid_state_s
float iTerm = (float)0
 
float dTerm = (float)0
 
float target = (float)0
 
float input = (float)0
 
float output = (float)0
 
float errorAmplificationCoef = (float)0
 
float previousError = (float)0
 

Protected Member Functions

virtual void updateITerm (float value)
 

Protected Attributes

pid_sparameters = nullptr
 

Detailed Description

default basic implementation also known as PidParallelController

Definition at line 34 of file efi_pid.h.

Constructor & Destructor Documentation

◆ Pid() [1/2]

Pid::Pid ( )

Definition at line 16 of file efi_pid.cpp.

16 {
17 initPidClass(nullptr);
18}
void initPidClass(pid_s *parameters)
Definition efi_pid.cpp:24
Here is the call graph for this function:

◆ Pid() [2/2]

Pid::Pid ( pid_s parameters)
explicit

Definition at line 20 of file efi_pid.cpp.

20 {
21 initPidClass(p_parameters);
22}
Here is the call graph for this function:

Member Function Documentation

◆ getD()

float Pid::getD ( ) const

Definition at line 126 of file efi_pid.cpp.

126 {
127 return parameters->dFactor;
128}
pid_s * parameters
Definition efi_pid.h:71

◆ getI()

float Pid::getI ( ) const

Definition at line 114 of file efi_pid.cpp.

◆ getIntegration()

float Pid::getIntegration ( void  ) const

Definition at line 122 of file efi_pid.cpp.

122 {
123 return iTerm;
124}

◆ getMinValue()

float Pid::getMinValue ( void  ) const

Definition at line 134 of file efi_pid.cpp.

Referenced by getOutput(), and PidIndustrial::limitOutput().

Here is the caller graph for this function:

◆ getOffset()

float Pid::getOffset ( void  ) const

Definition at line 130 of file efi_pid.cpp.

Referenced by PidIndustrial::getOutput(), getUnclampedOutput(), and showPidStatus().

Here is the caller graph for this function:

◆ getOutput() [1/2]

float Pid::getOutput ( float  p_target,
float  p_input 
)

This version of the method takes dTime from pid_s

Parameters
Controllerinput / process output
Returns
Output from the PID controller / the input to the process
Parameters
Controllerinput / process output
Returns
Output from the PID controller / the input to the process

Definition at line 56 of file efi_pid.cpp.

56 {
57 efiAssert(ObdCode::OBD_PCM_Processor_Fault, parameters != nullptr, "PID::getOutput nullptr", 0);
58 float dTime = MS2SEC(GET_PERIOD_LIMITED(parameters));
59 return getOutput(p_target, p_input, dTime);
60}
float getOutput(float target, float input)
Definition efi_pid.cpp:56
@ OBD_PCM_Processor_Fault

Referenced by LuaPid::get(), VvtController::getClosedLoop(), AlternatorController::getClosedLoop(), EtbController::getClosedLoop(), BoostController::getClosedLoopImpl(), IdleController::getIdleTimingAdjustment(), and getOutput().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getOutput() [2/2]

float Pid::getOutput ( float  p_target,
float  p_input,
float  dTime 
)
virtual
Parameters
dTimeseconds probably? :)

Reimplemented in PidCic, PidIndustrial, PidCic, and PidIndustrial.

Definition at line 84 of file efi_pid.cpp.

84 {
85 float l_output = getUnclampedOutput(p_target, p_input, dTime);
86
87 if (l_output > parameters->maxValue) {
88 l_output = parameters->maxValue;
89 } else if (l_output < getMinValue()) {
90 l_output = getMinValue();
91 }
92 output = l_output;
93 return output;
94}
float getUnclampedOutput(float target, float input, float dTime)
Definition efi_pid.cpp:62
float getMinValue() const
Definition efi_pid.cpp:134
Here is the call graph for this function:

◆ getP()

float Pid::getP ( ) const

Definition at line 110 of file efi_pid.cpp.

◆ getPrevError()

float Pid::getPrevError ( void  ) const

Definition at line 118 of file efi_pid.cpp.

118 {
119 return previousError;
120}

◆ getUnclampedOutput()

float Pid::getUnclampedOutput ( float  target,
float  input,
float  dTime 
)

Definition at line 62 of file efi_pid.cpp.

62 {
63 target = p_target;
64 input = p_input;
65 float error = (target - input) * errorAmplificationCoef;
66
67 float pTerm = parameters->pFactor * error;
68 updateITerm(parameters->iFactor * dTime * error);
69 dTerm = parameters->dFactor / dTime * (error - previousError);
70
71 previousError = error;
72
73 if (dTime <=0) {
74 warning(ObdCode::CUSTOM_PID_DTERM, "PID: unexpected dTime");
75 return pTerm + getOffset();
76 }
77
78 return pTerm + iTerm + dTerm + getOffset();
79}
virtual void updateITerm(float value)
Definition efi_pid.cpp:178
float getOffset() const
Definition efi_pid.cpp:130
bool warning(ObdCode code, const char *fmt,...)
@ CUSTOM_PID_DTERM

Referenced by getOutput(), and PidCic::getOutput().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ initPidClass()

void Pid::initPidClass ( pid_s parameters)

Definition at line 24 of file efi_pid.cpp.

24 {
25 parameters = p_parameters;
26 resetCounter = 0;
27
28 Pid::reset();
29}
virtual void reset()
Definition efi_pid.cpp:103
int resetCounter
Definition efi_pid.h:66

Referenced by AlternatorController::AlternatorController(), IdleController::init(), VvtController::init(), EtbController::init(), BoostController::init(), Pid(), and Pid().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ isSame()

bool Pid::isSame ( const pid_s parameters) const

Definition at line 39 of file efi_pid.cpp.

39 {
40 if (!parameters) {
41 // this 'null' could happen on first execution during initialization
42 return false;
43 }
44 efiAssert(ObdCode::OBD_PCM_Processor_Fault, p_parameters != nullptr, "PID::isSame nullptr", false);
45 return isClose(parameters->pFactor, p_parameters->pFactor)
46 && isClose(parameters->iFactor, p_parameters->iFactor)
47 && isClose(parameters->dFactor, p_parameters->dFactor)
48 && isClose(parameters->offset, p_parameters->offset)
49 && isClose(parameters->periodMs, p_parameters->periodMs);
50}
static bool isClose(float a, float b)
Definition efi_pid.cpp:31

Referenced by BoostController::onConfigurationChange(), VvtController::onConfigurationChange(), IdleController::onConfigurationChange(), AlternatorController::onConfigurationChange(), and EtbController::onConfigurationChange().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ postState()

void Pid::postState ( pid_status_s pidStatus) const

Definition at line 144 of file efi_pid.cpp.

144 {
145 pidStatus.output = output;
146 pidStatus.error = previousError;
147 pidStatus.pTerm = parameters == nullptr ? 0 : parameters->pFactor * previousError;
148 pidStatus.iTerm = iTerm;
149 pidStatus.dTerm = dTerm;
150}
scaled_channel< int16_t, 100, 1 > dTerm
scaled_channel< int16_t, 100, 1 > error
scaled_channel< int16_t, 100, 1 > output
scaled_channel< int16_t, 100, 1 > iTerm

Referenced by EtbController::checkStatus(), VvtController::getClosedLoop(), BoostController::getClosedLoop(), IdleController::getIdlePosition(), and AlternatorController::onFastCallback().

Here is the caller graph for this function:

◆ reset()

void Pid::reset ( void  )
virtual

◆ setErrorAmplification()

void Pid::setErrorAmplification ( float  coef)

Definition at line 138 of file efi_pid.cpp.

138 {
140}

Referenced by VvtController::getClosedLoop(), and IdleController::getIdleTimingAdjustment().

Here is the caller graph for this function:

◆ showPidStatus()

void Pid::showPidStatus ( const char msg) const

Definition at line 160 of file efi_pid.cpp.

160 {
161 efiPrintf("%s settings: offset=%f P=%.5f I=%.5f D=%.5f period=%dms",
162 msg,
163 getOffset(),
168
169 efiPrintf("%s status: value=%.2f input=%.2f/target=%.2f iTerm=%.5f dTerm=%.5f",
170 msg,
171 output,
172 input,
173 target,
174 iTerm, dTerm);
175
176}

Referenced by EtbController::showStatus().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sleep()

void Pid::sleep ( )

Definition at line 153 of file efi_pid.cpp.

153 {
154#if !EFI_UNIT_TEST
155 int periodMs = maxI(10, parameters->periodMs);
156 chThdSleepMilliseconds(periodMs);
157#endif /* EFI_UNIT_TEST */
158}

◆ updateFactors()

void Pid::updateFactors ( float  pFactor,
float  iFactor,
float  dFactor 
)

Definition at line 96 of file efi_pid.cpp.

96 {
97 parameters->pFactor = pFactor;
98 parameters->iFactor = iFactor;
99 parameters->dFactor = dFactor;
100 reset();
101}

Referenced by applyPidSettings().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ updateITerm()

void Pid::updateITerm ( float  value)
protectedvirtual

If we have exceeded the ability of the controlled device to hit target, the I factor will keep accumulating and approach infinity. Here we limit the I-term #353

Reimplemented in PidCic.

Definition at line 178 of file efi_pid.cpp.

178 {
179 iTerm += value;
180 /**
181 * If we have exceeded the ability of the controlled device to hit target, the I factor will keep accumulating and approach infinity.
182 * Here we limit the I-term #353
183 */
184 if (iTerm > parameters->maxValue * 100) {
185 iTerm = parameters->maxValue * 100;
186 }
187 if (iTerm > iTermMax) {
188 iTerm = iTermMax;
189 }
190
191 // this is kind of a hack. a proper fix would be having separate additional settings 'maxIValue' and 'minIValye'
192 if (iTerm < -parameters->maxValue * 100)
193 iTerm = -parameters->maxValue * 100;
194 if (iTerm < iTermMin) {
195 iTerm = iTermMin;
196 }
197}
float iTermMax
Definition efi_pid.h:69
float iTermMin
Definition efi_pid.h:68

Referenced by PidIndustrial::getOutput(), and getUnclampedOutput().

Here is the caller graph for this function:

Field Documentation

◆ iTermMax

float Pid::iTermMax = 1000000.0

◆ iTermMin

float Pid::iTermMin = -1000000.0

◆ parameters

pid_s* Pid::parameters = nullptr
protected

◆ resetCounter

int Pid::resetCounter

Definition at line 66 of file efi_pid.h.

Referenced by initPidClass(), and reset().


The documentation for this class was generated from the following files: