18 template <
typename CONTROL_VAR_TYPE>
19 inline PIDLoop<CONTROL_VAR_TYPE>::ControlParams::ControlParams (ValueType p, ValueType i, ValueType d)
25 template <
typename CONTROL_VAR_TYPE>
30 out <<
"P: "sv << P <<
"',"sv;
31 out <<
"I: "sv << I <<
"',"sv;
42 template <
typename CONTROL_VAR_TYPE>
44 const function<
void (ValueType o)>& outputFunction, ValueType initialSetPoint)
45 : fPIDParams_{pidParams}
46 , fUpdatePeriod_{updatePeriod}
47 , fMeasureFunction_{measureFunction}
48 , fOutputFunction_{outputFunction}
50 Require (updatePeriod > 0);
51 fUpdatableParams_.rwget ()->fSetPoint_ = (initialSetPoint);
53 template <
typename CONTROL_VAR_TYPE>
55 const function<ValueType ()>& measureFunction,
const function<
void (ValueType o)>& outputFunction,
56 ValueType initialSetPoint)
57 :
PIDLoop{pidParams, updatePeriod, measureFunction, outputFunction, initialSetPoint}
61 template <
typename CONTROL_VAR_TYPE>
65 fThread_->AbortAndWaitForDone ();
68 template <
typename CONTROL_VAR_TYPE>
71 return fUpdatableParams_->fSetPoint_;
73 template <
typename CONTROL_VAR_TYPE>
76 if (sp != fUpdatableParams_->fSetPoint_) {
78 fUpdatableParams_ = {sp, ValueType{}, ValueType{}};
81 template <
typename CONTROL_VAR_TYPE>
86 template <
typename CONTROL_VAR_TYPE>
89 return fUpdatePeriod_;
91 template <
typename CONTROL_VAR_TYPE>
98 ValueType measuredValue = fMeasureFunction_ ();
99 ValueType setPoint = fUpdatableParams_->fSetPoint_;
100 ValueType error = setPoint - measuredValue;
101 ValueType derivative = (error - fUpdatableParams_->fPrevError_) / fUpdatePeriod_;
103 auto updateUpdatableParams = fUpdatableParams_.
rwget ();
104 updateUpdatableParams->fIntegral_ += error * fUpdatePeriod_;
105 updateUpdatableParams->fPrevError_ = error;
107 ValueType outputFunctionArgument = fPIDParams_.P * error + fPIDParams_.I * fUpdatableParams_->fIntegral_ + fPIDParams_.D * derivative;
108 fOutputFunction_ (outputFunctionArgument);
109 nextRunAt += fUpdatePeriod_;
110#if Stroika_Foundation_Execution_PIDLoop_USE_NOISY_TRACE_IN_THIS_MODULE_
111 DbgTrace (
"Completed PIDLoop update: set-point={}, measuredValue{}, error={}, derivative={}, integral={}, outputFunctionArgument={}, nextRunAt={}"_f,
112 setPoint, measuredValue, error, derivative, fUpdatableParams_->fIntegral_, outputFunctionArgument, nextRunAt);
115 catch (
const Thread::AbortException&) {
119 DbgTrace (L
"Suppressing exception in PIDLoop: {}"_f, current_exception ());
123 template <
typename CONTROL_VAR_TYPE>
126 Require (not fThread_.has_value ());
chrono::duration< double > DurationSeconds
chrono::duration<double> - a time span (length of time) measured in seconds, but high precision.
Similar to String, but intended to more efficiently construct a String. Mutable type (String is large...
nonvirtual String str() const
String is like std::u32string, except it is much easier to use, often much more space efficient,...
nonvirtual Thread::Ptr RunInThread()
nonvirtual void RunDirectly()
nonvirtual ControlParams GetControlParams() const
nonvirtual void SetSetPoint(ValueType sp)
nonvirtual WritableReference rwget()
get a read-write smart pointer to the underlying Synchronized<> object, holding the full lock the who...
Thread::Ptr is a (unsynchronized) smart pointer referencing an internally synchronized std::thread ob...
Ptr New(const function< void()> &fun2CallOnce, const optional< Characters::String > &name, const optional< Configuration > &configuration)
void SleepUntil(Time::TimePointSeconds untilTickCount)