Dynamixel  2.9.5
RoadNarrows Robotics Dynamixel Package
DynaPidSpeed.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: Dynamixel
4 //
5 // Library: librnr_dynamixel
6 //
7 // File: DynaPidSpeed.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 Speed PID Class.
15  *
16  * The speed PID controls a servo to a speed setpoint.
17  *
18  * \author Robin Knight (robin.knight@roadnarrows.com)
19  *
20  * \copyright
21  * \h_copy 2011-2018. RoadNarrows LLC.\n
22  * http://www.roadnarrows.com\n
23  * All Rights Reserved
24  */
25 /*
26  * @EulaBegin@
27  *
28  * Unless otherwise stated explicitly, all materials contained are copyrighted
29  * and may not be used without RoadNarrows LLC's written consent,
30  * except as provided in these terms and conditions or in the copyright
31  * notice (documents and software) or other proprietary notice provided with
32  * the relevant materials.
33  *
34  * IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY
35  * MEMBERS/EMPLOYEES/CONTRACTORS OF ROADNARROWS OR DISTRIBUTORS OF THIS SOFTWARE
36  * BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR
37  * CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
38  * DOCUMENTATION, EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN
39  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * THE AUTHORS AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
42  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
43  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
44  * "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
45  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
46  *
47  * @EulaEnd@
48  */
49 ////////////////////////////////////////////////////////////////////////////////
50 
51 #ifndef _DYNA_PID_SPEED_H
52 #define _DYNA_PID_SPEED_H
53 
54 #include <cstring>
55 #include <iostream>
56 #include <fstream>
57 #include <vector>
58 #include <map>
59 
60 #include "rnr/rnrconfig.h"
61 #include "rnr/log.h"
62 
63 #include "Dynamixel/Dynamixel.h"
64 #include "Dynamixel/DynaPid.h"
65 
66 using namespace std;
67 
68 
69 // -----------------------------------------------------------------------------
70 // DynaPidSpeed Class
71 // -----------------------------------------------------------------------------
72 
73 /*!
74  * \brief Speed PID class.
75  */
76 class DynaPidSpeed : public DynaPid
77 {
78 public:
79  static const double SpeedPidKpDft; ///< default Kp constant
80  static const double SpeedPidKiDft; ///< default Ki constant
81  static const double SpeedPidKdDft; ///< default Kd constant
82 
83  /*!
84  * \brief Default constructor.
85  *
86  * \param fKp Speed PID proportional constant.
87  * \param fKi Speed PID integral constant.
88  * \param fKd Speed PID derivative constant.
89  * \param fWi Speed PID integral sum of errors weight constant.
90  */
91  DynaPidSpeed(double fKp=SpeedPidKpDft,
92  double fKi=SpeedPidKiDft,
93  double fKd=SpeedPidKdDft,
94  double fWi=DynaPid::WiSumErrDft) :
95  DynaPid(fKp, fKi, fKd, fWi)
96  {
99  SetStartingPos(0);
100  }
101 
102  /*!
103  * \brief Copy constructor.
104  */
106  DynaPid(src.m_fKp, src.m_fKi, src.m_fKd, src.m_fWi)
107  {
108  SetServoParams(src.m_nMode, src.m_nSpeedMin, src.m_nSpeedMax,
109  src.m_nOdModulo);
110  SetStartingPos(src.m_nPos[0]);
111  }
112 
113  /*!
114  * \brief Default destructor.
115  */
116  virtual ~DynaPidSpeed() { }
117 
118  /*!
119  * \brief Set servo paramaters required for speed PID.
120  *
121  * \param nMode Servo mode. (See \ref dyna_servo_mode).
122  * \param nSpeedMin Servo minimum raw speed.
123  * \param nSpeedMax Servo maximum raw speed.
124  * \param nOdModulo Odometer roll-over modulo.
125  */
126  virtual void SetServoParams(int nMode,
127  int nSpeedMin,
128  int nSpeedMax,
129  int nOdModulo)
130  {
131  m_nMode = nMode;
132  m_nSpeedMin = nSpeedMin;
133  m_nSpeedMax = nSpeedMax;
134  m_nOdModulo = nOdModulo;
135  }
136 
137  /*!
138  * \brief Set speed PID starting positions.
139  *
140  * \param uStartPos Starting servo position.
141  */
142  virtual void SetStartingPos(uint_t uStartPos)
143  {
144  m_nPos[0] = (int)uStartPos;
145  m_nPos[1] = (int)uStartPos;
146  }
147 
148  /*! \brief Convert the current and previous positions measured over delta time
149  * to dp/dt.
150  *
151  * Since there are two solutions to delta position for servos in continuous
152  * mode, the smallest delta is taken. This assumes that the position sampling
153  * rate is fast enough not to cause ambiguity.
154  *
155  * \param nCurPos Current servo position.
156  * \param nPrevPos Current servo position.
157  * \param dt Delta time between previous and current positions
158  * (seconds).
159  *
160  * \return dp/dt.
161  */
162  virtual double CalcDpDt(uint_t uCurPos, uint_t uPrevPos, double dt)
163  {
164  return CalcDpDt((int)uCurPos, (int)uPrevPos, dt);
165  }
166 
167  /*!
168  * \brief Convert the current and previous positions measured over delta time
169  * to dp/dt.
170  *
171  * Since there are two solutions to delta position for servos in continuous
172  * mode, the smallest delta is taken. This assumes that the position sampling
173  * rate is fast enough not to cause ambiguity.
174  *
175  * \param nCurPos Current servo position.
176  * \param nPrevPos Current servo position.
177  * \param dt Delta time between previous and current positions
178  * (seconds).
179  *
180  * \return dp/dt.
181  */
182  virtual double CalcDpDt(int nCurPos, int nPrevPos, double dt);
183 
184  /*!
185  * \brief Specify the setpoint.
186  *
187  * The PID can also be reset. This PID technique is used when a new setpoint
188  * causes the PID to behave badly, normally through integral windup. The
189  * reset:
190  * o Zeros accumulated sum of errors.
191  *
192  * \param fDpDt Speed dp/dt where dp = delta position, dt = delta time.
193  * \param bUnwind Do [not] reset controller integral sum of errors term
194  */
195  virtual void SpecifySetPoint(double fDpDt, bool bUnwind = false);
196 
197  /*!
198  * \brief Apply PID control.
199  *
200  * \param uCurPos Current servo position.
201  * \param dt Delta time (seconds).
202  *
203  * \return Output servo raw speed.
204  */
205  virtual double Control(uint_t uCurPos, double dt)
206  {
207  return DynaPid::Control(toPV(uCurPos, dt), dt);
208  }
209 
210  /*!
211  * \brief Assignment operator.
212  *
213  * \param rhs Right hand side object.
214  *
215  * \return Returns *this.
216  */
218  {
219  m_fWi = rhs.m_fWi;
220  SetConstants(rhs.m_fKp, rhs.m_fKi, rhs.m_fKd);
221  SetServoParams(rhs.m_nMode, rhs.m_nSpeedMin, rhs.m_nSpeedMax,
222  rhs.m_nOdModulo);
223  SetStartingPos(rhs.m_nPos[0]);
224  InitControl();
225  return *this;
226  }
227 
228 protected:
229  int m_nPos[2]; ///< servo current (0) and previous (1) positions
230  int m_nMode; ///< servo mode
231  int m_nSpeedMin; ///< servo minimum raw value
232  int m_nSpeedMax; ///< servo maximum raw value
233  int m_nOdModulo; ///< servo odometer rollover modulo
234 
235  /*! \brief Save current position and convert to dp/dt process variable.
236  *
237  * \param uCurPos Current servo position.
238  * \param dt Delta time between previous and current positions (seconds).
239  *
240  * \return Process variable (PV).
241  */
242  virtual double toPV(uint_t uCurPos, double dt)
243  {
244  // age
245  m_nPos[1] = m_nPos[0];
246  m_nPos[0] = (int)uCurPos;
247 
248  return CalcDpDt(m_nPos[0], m_nPos[1], dt);
249  }
250 
251  /*!
252  * \brief Convert control variable to raw servo speed.
253  *
254  * \return Output.
255  */
256  virtual double toOutput();
257 };
258 
259 
260 #endif // _DYNA_PID_SPEED_H
double m_fKd
derivative constant
Definition: DynaPid.h:258
virtual double Control(double fPV, double dt)
Apply PID control.
Definition: DynaPid.cxx:83
#define DYNA_SPEED_MIN_CTL
mininum raw speed with control
Definition: Dynamixel.h:263
static const double WiSumErrDft
default sum error weight
Definition: DynaPid.h:110
#define DYNA_MODE_SERVO
servo mode with limited rotation
Definition: Dynamixel.h:170
static const double SpeedPidKdDft
default Kd constant
Definition: DynaPidSpeed.h:81
int m_nSpeedMin
servo minimum raw value
Definition: DynaPidSpeed.h:231
virtual double toPV(uint_t uCurPos, double dt)
Save current position and convert to dp/dt process variable.
Definition: DynaPidSpeed.h:242
virtual void SetServoParams(int nMode, int nSpeedMin, int nSpeedMax, int nOdModulo)
Set servo paramaters required for speed PID.
Definition: DynaPidSpeed.h:126
static const double SpeedPidKpDft
default Kp constant
Definition: DynaPidSpeed.h:79
DynaPidSpeed(const DynaPidSpeed &src)
Copy constructor.
Definition: DynaPidSpeed.h:105
static const double SpeedPidKiDft
default Ki constant
Definition: DynaPidSpeed.h:80
#define DYNA_SPEED_MAX_CTL
maxinum raw speed with control
Definition: Dynamixel.h:264
int m_nOdModulo
servo odometer rollover modulo
Definition: DynaPidSpeed.h:233
#define DYNA_POS_MODULO
servo position modulo [0-max]
Definition: Dynamixel.h:246
virtual double Control(uint_t uCurPos, double dt)
Apply PID control.
Definition: DynaPidSpeed.h:205
double m_fKp
proportional constant
Definition: DynaPid.h:256
Speed PID class.
Definition: DynaPidSpeed.h:76
The Dynamixel PID Base Class.
int m_nMode
servo mode
Definition: DynaPidSpeed.h:230
virtual void SetStartingPos(uint_t uStartPos)
Set speed PID starting positions.
Definition: DynaPidSpeed.h:142
virtual ~DynaPidSpeed()
Default destructor.
Definition: DynaPidSpeed.h:116
virtual double CalcDpDt(uint_t uCurPos, uint_t uPrevPos, double dt)
Convert the current and previous positions measured over delta time to dp/dt.
Definition: DynaPidSpeed.h:162
RoadNarrows Dynamixel Top-Level Package Header File.
Proportional–Integral–Derivative Controller for Dynamixel servos.
Definition: DynaPid.h:104
double m_fWi
weighted sum of errors moving average constant
Definition: DynaPid.h:259
DynaPidSpeed & operator=(const DynaPidSpeed &rhs)
Assignment operator.
Definition: DynaPidSpeed.h:217
int m_nPos[2]
servo current (0) and previous (1) positions
Definition: DynaPidSpeed.h:229
int m_nSpeedMax
servo maximum raw value
Definition: DynaPidSpeed.h:232
double m_fKi
integral constant
Definition: DynaPid.h:257
DynaPidSpeed(double fKp=SpeedPidKpDft, double fKi=SpeedPidKiDft, double fKd=SpeedPidKdDft, double fWi=DynaPid::WiSumErrDft)
Default constructor.
Definition: DynaPidSpeed.h:91