Dynamixel  2.9.5
RoadNarrows Robotics Dynamixel Package
DynaPid.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: Dynamixel
4 //
5 // Library: librnr_dynamixel
6 //
7 // File: DynaPid.h
8 //
9 /*! \file
10  *
11  * $LastChangedDate: 2015-01-12 10:56:06 -0700 (Mon, 12 Jan 2015) $
12  * $Rev: 3845 $
13  *
14  * \brief The Dynamixel PID Base Class.
15  *
16  * \author Robin Knight (robin.knight@roadnarrows.com)
17  *
18  * \copyright
19  * \h_copy 2011-2018. RoadNarrows LLC.\n
20  * http://www.roadnarrows.com\n
21  * All Rights Reserved
22  */
23 /*
24  * @EulaBegin@
25  *
26  * Unless otherwise stated explicitly, all materials contained are copyrighted
27  * and may not be used without RoadNarrows LLC's written consent,
28  * except as provided in these terms and conditions or in the copyright
29  * notice (documents and software) or other proprietary notice provided with
30  * the relevant materials.
31  *
32  * IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY
33  * MEMBERS/EMPLOYEES/CONTRACTORS OF ROADNARROWS OR DISTRIBUTORS OF THIS SOFTWARE
34  * BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR
35  * CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
36  * DOCUMENTATION, EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN
37  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38  *
39  * THE AUTHORS AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
40  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
41  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
42  * "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
43  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
44  *
45  * @EulaEnd@
46  */
47 ////////////////////////////////////////////////////////////////////////////////
48 
49 #ifndef _DYNA_PID_H
50 #define _DYNA_PID_H
51 
52 #include <cstring>
53 #include <iostream>
54 #include <fstream>
55 
56 #include "rnr/rnrconfig.h"
57 #include "rnr/log.h"
58 
59 #include "Dynamixel/Dynamixel.h"
60 
61 using namespace std;
62 
63 
64 // -----------------------------------------------------------------------------
65 // DynaPid Class
66 // -----------------------------------------------------------------------------
67 
68 /*!
69  * \brief Proportional–Integral–Derivative Controller for Dynamixel servos.
70  *
71  * \par The Servo PID:
72  * \termblock
73  * \term setpoint (SP) \termdata = \termdata application specific \endterm
74  * \term process variable (PV) \termdata =
75  * \termdata application specific \endterm
76  * \term control variable (CV) \termdata =
77  * \termdata application specific \endterm
78  * \endtermblock
79  *
80  * \par The PID Control Loop:
81  * \termblock
82  * \term CV \termdata = \termdata
83  * K<sub>p</sub> * err<sub>j</sub> +
84  * K<sub>i</sub> * \h_sigma<sup>j</sup>err<sub>k</sub> * dt<sub>k</sub> +
85  * K<sub>d</sub> * (err<sub>j</sub> - err<sub>j-1</sub>) / dt<sub>j</sub>
86  * \endterm
87  * \term output \termdata = \termdata converted control variable \endterm
88  * \endtermblock
89  * \n
90  * <b>where:</b>
91  * \termblock
92  * \term j \termdata this time step \endterm
93  * \term j-1 \termdata previous time step \endterm
94  * \term dt<sub>k</sub> \termdata
95  * delta time between time steps k-1 and k \endterm
96  * \term err<sub>k</sub> \termdata error = SP - PV<sub>k</sub> \endterm
97  * \term PV<sub>k</sub>
98  * \termdata process variable at time step k \endterm
99  * \term K<sub>p</sub> \termdata proportional constant \endterm
100  * \term K<sub>i</sub> \termdata integral constant \endterm
101  * \term K<sub>d</sub> \termdata derivative constant \endterm
102  * \endtermblock
103  */
104 class DynaPid
105 {
106 public:
107  static const double PidKpDft; ///< default Kp constant
108  static const double PidKiDft; ///< default Ki constant
109  static const double PidKdDft; ///< default Kd constant
110  static const double WiSumErrDft; ///< default sum error weight
111 
112  /*!
113  * \brief Default constructor.
114  *
115  * \param fKp PID proportional constant.
116  * \param fKi PID integral constant.
117  * \param fKd PID derivative constant.
118  * \param fWi PID integral sum of errors weight constant.
119  */
120  DynaPid(double fKp=PidKpDft,
121  double fKi=PidKiDft,
122  double fKd=PidKdDft,
123  double fWi=WiSumErrDft)
124  {
125  m_fWi = fWi;
126  SetConstants(fKp, fKi, fKd);
127  InitControl();
128  }
129 
130  /*!
131  * \brief Copy constructor.
132  */
133  DynaPid(const DynaPid &src)
134  {
135  m_fWi = src.m_fWi;
136  SetConstants(src.m_fKp, src.m_fKi, src.m_fKd);
137  InitControl();
138  }
139 
140  /*!
141  * \brief Default destructor.
142  */
143  virtual ~DynaPid() { }
144 
145  /*!
146  * \brief Set PID constants.
147  *
148  * \param fKp PID proportional constant.
149  * \param fKi PID integral constant.
150  * \param fKd PID derivative constant.
151  */
152  virtual void SetConstants(double fKp, double fKi, double fKd)
153  {
154  m_fKp = fKp;
155  m_fKi = fKi;
156  m_fKd = fKd;
157  }
158 
159  /*!
160  * \brief Get PID proportional constant.
161  * \return K<sub>p</sub>
162  */
163  double GetKp() const { return m_fKp; }
164 
165  /*!
166  * \brief Get PID integral constant.
167  * \return K<sub>i</sub>
168  */
169  double GetKi() const { return m_fKi; }
170 
171  /*!
172  * \brief Get PID derivative constant.
173  * \return K<sub>d</sub>
174  */
175  double GetKd() const { return m_fKd; }
176 
177  /*!
178  * \brief Get PID setpoint.
179  * \return SP.
180  */
181  double GetSP() const { return m_fSP; }
182 
183  /*!
184  * \brief Get PID control variable.
185  * \return CV.
186  */
187  double GetCV() const { return m_fCV; }
188 
189  /*!
190  * \brief Get PID output.
191  * \return Output.
192  */
193  double GetOutput() const { return m_fOutput; }
194 
195  /*!
196  * \brief Get current error
197  * \return Error.
198  */
199  double GetError() const { return m_fErr; }
200 
201  /*!
202  * \brief Initialize the PID control variables.
203  */
204  virtual void InitControl()
205  {
206  m_fSP = 0.0;
207  m_fPV = 0.0;
208  m_fErrPrev = 0.0;
209  m_bHasPrev = false;
210  m_fErr = 0.0;
211  m_fErrSum = 0.0;
212  m_fCV = 0.0;
213  m_fOutput = 0.0;
214  }
215 
216  /*!
217  * \brief Specify the setpoint.
218  *
219  * The PID can also be unwound. This PID technique is used when a new setpoint
220  * causes the PID to behave badly, normally through integral windup. Unwinding
221  * resets the accumulated sum of errors integral term to zero.
222  *
223  * \param fSP New setpoint.
224  * \param bUnwind Do [not] reset controller integral sum of errors term
225  */
226  virtual void SpecifySetPoint(double fSP, bool bUnwind = false);
227 
228  /*!
229  * \brief Apply PID control.
230  *
231  * \param fPV Process variable.
232  * \param dt Delta time step (seconds).
233  *
234  * \return Output.
235  */
236  virtual double Control(double fPV, double dt);
237 
238  /*!
239  * \brief Assignment operator.
240  *
241  * \param rhs Right hand side object.
242  *
243  * \return Returns *this.
244  */
245  DynaPid &operator=(const DynaPid &rhs)
246  {
247  m_fWi = rhs.m_fWi;
248  SetConstants(rhs.m_fKp, rhs.m_fKi, rhs.m_fKd);
249  InitControl();
250  return *this;
251  }
252 
253 protected:
254  double m_fSP; ///< setpoint
255  double m_fPV; ///< process variable
256  double m_fKp; ///< proportional constant
257  double m_fKi; ///< integral constant
258  double m_fKd; ///< derivative constant
259  double m_fWi; ///< weighted sum of errors moving average constant
260  bool m_bHasPrev; ///< has previous error
261  double m_fErrPrev; ///< previous error
262  double m_fErr; ///< error
263  double m_fErrSum; ///< sum of errors
264  double m_fCV; ///< control variable value
265  double m_fOutput; ///< control variable output (could be same as CV)
266 
267  /*!
268  * \brief Calculate error from setpoint and current process variable value.
269  *
270  * \param fPV Process variable value.
271  *
272  * \return Error.
273  */
274  virtual double error(double fPV)
275  {
276  return m_fSP - fPV;
277  }
278 
279  /*!
280  * \brief Convert control variable to application-specific output value.
281  *
282  * \param fCV Control variable value.
283  *
284  * \return Output.
285  */
286  virtual double toOutput(double fCV)
287  {
288  return fCV;
289  }
290 };
291 
292 
293 #endif // _DYNA_PID_H
double m_fOutput
control variable output (could be same as CV)
Definition: DynaPid.h:265
virtual void InitControl()
Initialize the PID control variables.
Definition: DynaPid.h:204
double m_fKd
derivative constant
Definition: DynaPid.h:258
double GetError() const
Get current error.
Definition: DynaPid.h:199
static const double PidKpDft
default Kp constant
Definition: DynaPid.h:107
static const double WiSumErrDft
default sum error weight
Definition: DynaPid.h:110
virtual void SetConstants(double fKp, double fKi, double fKd)
Set PID constants.
Definition: DynaPid.h:152
double GetSP() const
Get PID setpoint.
Definition: DynaPid.h:181
double GetKd() const
Get PID derivative constant.
Definition: DynaPid.h:175
virtual ~DynaPid()
Default destructor.
Definition: DynaPid.h:143
double m_fErrPrev
previous error
Definition: DynaPid.h:261
virtual double toOutput(double fCV)
Convert control variable to application-specific output value.
Definition: DynaPid.h:286
static const double PidKdDft
default Kd constant
Definition: DynaPid.h:109
double m_fKp
proportional constant
Definition: DynaPid.h:256
bool m_bHasPrev
has previous error
Definition: DynaPid.h:260
virtual double error(double fPV)
Calculate error from setpoint and current process variable value.
Definition: DynaPid.h:274
DynaPid & operator=(const DynaPid &rhs)
Assignment operator.
Definition: DynaPid.h:245
DynaPid(double fKp=PidKpDft, double fKi=PidKiDft, double fKd=PidKdDft, double fWi=WiSumErrDft)
Default constructor.
Definition: DynaPid.h:120
double m_fPV
process variable
Definition: DynaPid.h:255
double m_fErrSum
sum of errors
Definition: DynaPid.h:263
double GetCV() const
Get PID control variable.
Definition: DynaPid.h:187
double GetOutput() const
Get PID output.
Definition: DynaPid.h:193
double m_fErr
error
Definition: DynaPid.h:262
RoadNarrows Dynamixel Top-Level Package Header File.
Proportional–Integral–Derivative Controller for Dynamixel servos.
Definition: DynaPid.h:104
double m_fSP
setpoint
Definition: DynaPid.h:254
double m_fWi
weighted sum of errors moving average constant
Definition: DynaPid.h:259
DynaPid(const DynaPid &src)
Copy constructor.
Definition: DynaPid.h:133
double m_fKi
integral constant
Definition: DynaPid.h:257
double GetKi() const
Get PID integral constant.
Definition: DynaPid.h:169
static const double PidKiDft
default Ki constant
Definition: DynaPid.h:108
double GetKp() const
Get PID proportional constant.
Definition: DynaPid.h:163
double m_fCV
control variable value
Definition: DynaPid.h:264