Dynamixel  2.9.5
RoadNarrows Robotics Dynamixel Package
DynaPidPos.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: Dynamixel
4 //
5 // Library: librnr_dynamixel
6 //
7 // File: DynaPidPos.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 Position PID 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_POS_H
50 #define _DYNA_PID_POS_H
51 
52 #include <math.h>
53 
54 #include <cstring>
55 #include <iostream>
56 #include <fstream>
57 
58 #include "rnr/rnrconfig.h"
59 #include "rnr/log.h"
60 
61 #include "Dynamixel/Dynamixel.h"
62 #include "Dynamixel/DynaPid.h"
63 
64 using namespace std;
65 
66 
67 // -----------------------------------------------------------------------------
68 // DynaPidPos Class
69 // -----------------------------------------------------------------------------
70 
71 /*!
72  * \brief Position PID class.
73  */
74 class DynaPidPos : public DynaPid
75 {
76 public:
77  static const double TuneMaxSpeedDelta; ///< max speed change deltas
78  static const double TuneAbsMinSpeed; ///< absolute minimum speed
79 
80  /*!
81  * \brief Default constructor.
82  *
83  * \param fKp PID proportional constant.
84  * \param fKi PID integral constant.
85  * \param fKd PID derivative constant.
86  * \param fWi PID integral sum of errors weight constant.
87  */
88  DynaPidPos(double fKp=0.5, // good default value
89  double fKi=0.0, // good default value
90  double fKd=0.005, // good default value
91  double fWi=0.25) :
92  DynaPid(fKp, fKi, fKd, fWi)
93  {
94  initParams();
95  }
96 
97  /*!
98  * \brief Copy constructor.
99  */
100  DynaPidPos(const DynaPid &src) : DynaPid(src)
101  {
102  initParams();
103  }
104 
105  /*!
106  * \brief Default destructor.
107  */
108  virtual ~DynaPidPos() { }
109 
110  /*!
111  * \brief Assignment operator.
112  */
114  {
115  m_fWi = rhs.m_fWi;
116  SetConstants(rhs.m_fKp, rhs.m_fKi, rhs.m_fKd);
117  InitControl();
118  initParams();
119  return *this;
120  }
121 
122  /*!
123  * \brief Specify position setpoint.
124  *
125  * The PID can also be unwound. This PID technique is used when a new setpoint
126  * causes the PID to behave badly, normally through integral windup. Unwinding
127  * resets the accumulated sum of errors integral term to zero.
128  *
129  * \param nOdPosStart Starting position (odometer ticks).
130  * \param nOdPosGoal New goal position setpoint (odometer ticks).
131  * \param nSpeedLim Speed limit (raw).
132  * \param nCurSpeed Current servo speed (raw).
133  * \param bIsReversed Odometer is [not] reversed from encoder.
134  * \param bUnwind Do [not] reset controller integral sum of errors term
135  */
136  virtual void SpecifySetPoint(int nOdPosStart,
137  int nOdPosGoal,
138  int nSpeedLim,
139  int nCurSpeed,
140  bool bIsReversed,
141  bool bUnwind = false);
142 
143 protected:
144  int m_nOdPosStart; ///< starting odometer position
145  int m_nOdPosGoal; ///< goal ing odometer position (set point)
146  int m_nSpeedLim; ///< speed limit (max neg/pos speed)
147  int m_nOdDir; ///< odometer direction
148  int m_nOdPosCur; ///< current odometer position
149  double m_fSpeedAbsMax; ///< absolute speed limit >= 0
150  double m_fPrevSpeed; ///< previous speed
151 
152  /*!
153  * \brief Initialize parameters.
154  */
155  void initParams()
156  {
157  m_nOdPosStart = 0;
158  m_nOdPosGoal = 0;
159  m_nSpeedLim = 0;
160  m_nOdDir = 1;
161  m_nOdPosCur = 0;
162  m_fSpeedAbsMax = 0.0;
163  m_fPrevSpeed = 0.0;
164  }
165 
166  /*!
167  * \brief Calculate error from goal postion setpoint and current position
168  * \process variable value.
169  *
170  * \param fPVOdPosCur Current odometer position process variable value.
171  *
172  * \return Error.
173  */
174  virtual double error(double fPVodPosCur);
175 
176  /*!
177  * \brief Convert control variable to application-specific output value.
178  *
179  * \param fCVSpeed Speed control variable value.
180  *
181  * \return Output.
182  */
183  virtual double toOutput(double fCVSpeed);
184 };
185 
186 #endif // _DYNA_PID_POS_H
double m_fKd
derivative constant
Definition: DynaPid.h:258
int m_nOdPosStart
starting odometer position
Definition: DynaPidPos.h:144
DynaPidPos & operator=(const DynaPidPos &rhs)
Assignment operator.
Definition: DynaPidPos.h:113
void initParams()
Initialize parameters.
Definition: DynaPidPos.h:155
static const double TuneMaxSpeedDelta
max speed change deltas
Definition: DynaPidPos.h:77
int m_nOdDir
odometer direction
Definition: DynaPidPos.h:147
double m_fSpeedAbsMax
absolute speed limit >= 0
Definition: DynaPidPos.h:149
DynaPidPos(const DynaPid &src)
Copy constructor.
Definition: DynaPidPos.h:100
int m_nSpeedLim
speed limit (max neg/pos speed)
Definition: DynaPidPos.h:146
double m_fKp
proportional constant
Definition: DynaPid.h:256
The Dynamixel PID Base Class.
int m_nOdPosGoal
goal ing odometer position (set point)
Definition: DynaPidPos.h:145
Position PID class.
Definition: DynaPidPos.h:74
RoadNarrows Dynamixel Top-Level Package Header File.
static const double TuneAbsMinSpeed
absolute minimum speed
Definition: DynaPidPos.h:78
Proportional–Integral–Derivative Controller for Dynamixel servos.
Definition: DynaPid.h:104
int m_nOdPosCur
current odometer position
Definition: DynaPidPos.h:148
double m_fWi
weighted sum of errors moving average constant
Definition: DynaPid.h:259
double m_fKi
integral constant
Definition: DynaPid.h:257
double m_fPrevSpeed
previous speed
Definition: DynaPidPos.h:150
DynaPidPos(double fKp=0.5, double fKi=0.0, double fKd=0.005, double fWi=0.25)
Default constructor.
Definition: DynaPidPos.h:88
virtual ~DynaPidPos()
Default destructor.
Definition: DynaPidPos.h:108