Dynamixel  2.9.5
RoadNarrows Robotics Dynamixel Package
DynaPidSpeed.cxx
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: Dynamixel
4 //
5 // Library: libDynamixel
6 //
7 // File: DynaPidSpeed.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 Speed PID Class.
15  *
16  * The speed PID controls to a servo's 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 #include <cstring>
52 #include <iostream>
53 #include <fstream>
54 #include <vector>
55 
56 #include "rnr/rnrconfig.h"
57 #include "rnr/log.h"
58 
59 #include "Dynamixel/Dynamixel.h"
60 #include "Dynamixel/DynaPid.h"
61 #include "Dynamixel/DynaPidSpeed.h"
62 #include "Dynamixel/DynaOlio.h"
63 
64 
65 using namespace std;
66 
67 
68 // -----------------------------------------------------------------------------
69 // DynaPidSpeed Class
70 // -----------------------------------------------------------------------------
71 
72 #if 0 // RDK move to DynaServo
73  {
74  m_pServo = pServo;
75 
76  m_nSpeedMax = (int)m_pServo->GetSpecification().m_uRawSpeedMax;
77 
78  if( m_pServo->GetServoMode() == DYNA_MODE_SERVO )
79  {
80  // cannot set to 0 which is, unfortunately, the default full speed value
81  m_nSpeedMin = DYNA_SPEED_MIN_CTL;
82  }
83  else
84  {
85  m_nSpeedMin = -m_nSpeedMax;
86  }
87 
88  m_nOdModulo = m_pServo->GetOdometerModulo();
89  }
90 #endif // 0
91 
92 const double DynaPidSpeed::SpeedPidKpDft = 0.5; ///< default Kp constant
93 const double DynaPidSpeed::SpeedPidKiDft = 1.0; ///< default Ki constant
94 const double DynaPidSpeed::SpeedPidKdDft = 0.1; ///< default Kd constant
95 
96 void DynaPidSpeed::SpecifySetPoint(double fDpDt, bool bUnwind)
97 {
98  DynaPid::SpecifySetPoint(fDpDt, bUnwind);
99 }
100 
101 double DynaPidSpeed::CalcDpDt(int nCurPos, int nPrevPos, double dt)
102 {
103  int nDp; // delta position
104  double fDpDt; // dp/dt where dp = delta raw position, dt = delta time
105 
106  nDp = nCurPos - nPrevPos;
107 
108  //
109  // The two positions straddle the roll-over point.
110  //
111  if( (m_nMode == DYNA_MODE_CONTINUOUS) && (iabs(nDp) > m_nOdModulo/2) )
112  {
113  if( nDp < 0 )
114  {
115  nDp = m_nOdModulo + nDp;
116  }
117  else
118  {
119  nDp = nDp - m_nOdModulo;
120  }
121  }
122 
123  if( dt > 0.0 )
124  {
125  fDpDt = ((double)(nDp))/dt;
126  }
127  else
128  {
129  fDpDt = 0.0;
130  }
131 
132  return fDpDt;
133 }
134 
136 {
137  if( m_fCV < (double)m_nSpeedMin )
138  {
139  return (double)m_nSpeedMin;
140  }
141  else if( m_fCV > (double)m_nSpeedMax )
142  {
143  return (double)m_nSpeedMax;
144  }
145  else
146  {
147  return m_fCV;
148  }
149 }
virtual double toOutput()
Convert control variable to raw servo speed.
#define DYNA_SPEED_MIN_CTL
mininum raw speed with control
Definition: Dynamixel.h:263
#define DYNA_MODE_SERVO
servo mode with limited rotation
Definition: Dynamixel.h:170
static const double SpeedPidKdDft
default Kd constant
Definition: DynaPidSpeed.h:81
virtual void SpecifySetPoint(double fSP, bool bUnwind=false)
Specify the setpoint.
Definition: DynaPid.cxx:71
The Dynamixel Speed PID Class.
INLINE_IN_H int iabs(int a)
Return absolute value of a.
Definition: DynaOlio.h:91
Miscellaneous collection of useful utilities.
static const double SpeedPidKpDft
default Kp constant
Definition: DynaPidSpeed.h:79
static const double SpeedPidKiDft
default Ki constant
Definition: DynaPidSpeed.h:80
The Dynamixel PID Base Class.
#define DYNA_MODE_CONTINUOUS
continuous mode with/without position
Definition: Dynamixel.h:171
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
virtual void SpecifySetPoint(double fDpDt, bool bUnwind=false)
Specify the setpoint.
RoadNarrows Dynamixel Top-Level Package Header File.