Motorcortex Control  version: 3.0.0
ctrl_pid.h
1 /*
2  * Developer : Philippe Piatkiewitz (philippe.piatkiewitz@vectioneer.com)
3  * All rights reserved. Copyright (c) 2014-2015 VECTIONEER.
4  */
5 
6 #ifndef MCX_CTRL3_PID_H
7 #define MCX_CTRL3_PID_H
8 
9 #include <cmath>
10 #include <mcx/core.h>
11 
12 namespace mcx {
13 
14 namespace control3 {
15 
52 class PID : public container::Module {
53 public:
54  PID();
55 
56  ~PID() override;
57 
62  inline void setError(double error);
63 
68  inline void setActual(double actual);
69 
74  inline double getOutput() const;
75 
80  inline void setKp(double kp);
81 
86  inline void setKi(double ki);
87 
92  inline void setKd(double kd);
93 
98  inline void setIMax(double iMax);
99 
104  inline void setIReset(bool iReset);
105 
106 private:
107  void create_(const char* name, parameter_server::Parameter* parameter_server, uint64_t dt_micro_s) override{};
108 
109  bool initPhase1_() override;
110 
111  bool initPhase2_() override { return true; }
112 
113  bool startOp_() override { return true; }
114 
115  bool iterateOp_(const container::TaskTime& system_time, container::UserTime* user_time) override;
116 
117  bool stopOp_() override { return true; }
118 
119 private:
120  double e_{0};
121  double a_{0};
122  double y_{0};
123  double kp_{0};
124  double ki_{0};
125  double kd_{0};
126 
127  double iMax_{0.1};
128  double iMin_{-0.1};
129  double iPositionErrorThresholdPos_{0};
130  double iPositionErrorThresholdNeg_{0};
131  double iActualVelocityThresholdPos_{std::numeric_limits<double>::infinity()};
132  double iActualVelocityThresholdNeg_{-std::numeric_limits<double>::infinity()};
133 
134  double laste_{0};
135  double lasta_{0};
136  double iTerm_{0};
137  double dTerm_{0};
138 
139  bool iReset_{false};
140 };
141 
142 inline void PID::setError(double error) { e_ = error; }
143 
144 inline void PID::setActual(double actual) { a_ = actual; }
145 
146 inline double PID::getOutput() const { return y_; }
147 
148 inline void PID::setKp(double kp) { kp_ = kp; }
149 
150 inline void PID::setKi(double ki) { ki_ = ki; }
151 
152 inline void PID::setKd(double kd) { kd_ = kd; }
153 
154 inline void PID::setIMax(double iMax) { iMax_ = fabs(iMax); }
155 
156 inline void PID::setIReset(bool iReset) { iReset_ = iReset; }
157 
158 } // namespace control3
159 
160 } // namespace mcx
161 
162 #endif
mcx::control3::PID::setError
void setError(double error)
Sets the error input signal.
Definition: ctrl_pid.h:142
mcx::control3::PID::setIReset
void setIReset(bool iReset)
Sets the reset signal that zeros the integral action.
Definition: ctrl_pid.h:156
mcx::control3::PID
The PID Control Object provides a PID controller with Proportional, Derivative and Integral action.
Definition: ctrl_pid.h:52
mcx::control3::PID::setKi
void setKi(double ki)
Sets the integral gain.
Definition: ctrl_pid.h:150
mcx::control3::PID::setKd
void setKd(double kd)
Sets the derivative gain.
Definition: ctrl_pid.h:152
mcx::control3::PID::setIMax
void setIMax(double iMax)
Sets the upper limit for the integral term.
Definition: ctrl_pid.h:154
mcx::control3::PID::setKp
void setKp(double kp)
Sets the proportional gain.
Definition: ctrl_pid.h:148
mcx::control3::PID::getOutput
double getOutput() const
Returns the output signal.
Definition: ctrl_pid.h:146
mcx::control3::PID::setActual
void setActual(double actual)
Sets the actual (feedback) signal.
Definition: ctrl_pid.h:144