Dynamixel  2.9.5
RoadNarrows Robotics Dynamixel Package
DynaPid.cxx
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: Dynamixel
4 //
5 // Library: libDynamixel
6 //
7 // File: DynaPid.cxx
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 #include <cstring>
50 #include <iostream>
51 #include <fstream>
52 
53 #include "rnr/rnrconfig.h"
54 #include "rnr/log.h"
55 
56 #include "Dynamixel/Dynamixel.h"
57 #include "Dynamixel/DynaPid.h"
58 
59 using namespace std;
60 
61 
62 // -----------------------------------------------------------------------------
63 // DynaPid Class
64 // -----------------------------------------------------------------------------
65 
66 const double DynaPid::PidKpDft = 0.5; ///< default Kp constant
67 const double DynaPid::PidKiDft = 1.0; ///< default Ki constant
68 const double DynaPid::PidKdDft = 0.1; ///< default Kd constant
69 const double DynaPid::WiSumErrDft = 0.1; ///< default sum error weight
70 
71 void DynaPid::SpecifySetPoint(double fSP, bool bUnwind)
72 {
73  m_fSP = fSP;
74 
75  m_bHasPrev = false;
76 
77  if( bUnwind )
78  {
79  m_fErrSum = 0.0;
80  }
81 }
82 
83 double DynaPid::Control(double fPV, double dt)
84 {
85  double fErrDt; // change in error w.r.t delta time
86 
87  // new process variable value
88  m_fPV = fPV;
89 
90  // error
91  m_fErr = error(m_fPV);
92 
93  if( !m_bHasPrev )
94  {
95  m_fErrPrev = m_fErr;
96  m_bHasPrev = true;
97  }
98 
99  // weigthed sum of errors moving average
100  m_fErrSum = (1.0 - m_fWi) * m_fErrSum + m_fWi * (m_fErr * dt);
101 
102  // delta error
103  if( dt > 0.0 )
104  {
105  fErrDt = (m_fErr - m_fErrPrev)/dt;
106  }
107  else
108  {
109  fErrDt = 0.0;
110  }
111 
112  m_fErrPrev = m_fErr;
113 
114  // new control variable value
115  m_fCV = (m_fKp * m_fErr) + (m_fKi * m_fErrSum) + (m_fKd * fErrDt);
116 
117  m_fOutput = toOutput(m_fCV);
118 
119  return m_fOutput;
120 }
virtual double Control(double fPV, double dt)
Apply PID control.
Definition: DynaPid.cxx:83
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 SpecifySetPoint(double fSP, bool bUnwind=false)
Specify the setpoint.
Definition: DynaPid.cxx:71
static const double PidKdDft
default Kd constant
Definition: DynaPid.h:109
The Dynamixel PID Base Class.
RoadNarrows Dynamixel Top-Level Package Header File.
static const double PidKiDft
default Ki constant
Definition: DynaPid.h:108