Dynamixel  2.9.5
RoadNarrows Robotics Dynamixel Package
DynaPidPos.cxx
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: Dynamixel
4 //
5 // Library: libDynamixel
6 //
7 // File: DynaPidPos.cxx
8 //
9 /*! \file
10  *
11  * $LastChangedDate: 2015-01-12 10:56:06 -0700 (Mon, 12 Jan 2015) $
12  * $Rev: 3845 $
13  *
14  * The position PID controls to a servo's odometer position setpoint.
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/DynaOlio.h"
58 #include "Dynamixel/DynaPid.h"
59 #include "Dynamixel/DynaPidPos.h"
60 
61 using namespace std;
62 
63 
64 // -----------------------------------------------------------------------------
65 // DynaPidPos Class
66 // -----------------------------------------------------------------------------
67 
68 const double DynaPidPos::TuneMaxSpeedDelta = 1000.0; ///< max speed chg deltas
69 const double DynaPidPos::TuneAbsMinSpeed = 0.0; ///< absolute minimum speed
70 
71 void DynaPidPos::SpecifySetPoint(int nOdPosStart,
72  int nOdPosGoal,
73  int nSpeedLim,
74  int nCurSpeed,
75  bool bIsReversed,
76  bool bUnwind)
77 {
78  DynaPid::SpecifySetPoint((double)nOdPosGoal, bUnwind);
79 
80  m_nOdPosStart = nOdPosStart;
81  m_nOdPosGoal = nOdPosGoal;
82  m_nSpeedLim = nSpeedLim;
83  m_nOdDir = bIsReversed? -1: 1;
84 
85  m_nOdPosCur = nOdPosStart;
86 
87  m_fSpeedAbsMax = m_nSpeedLim >= 0? (double)m_nSpeedLim: -(double)m_nSpeedLim;
88  m_fPrevSpeed = (double)nCurSpeed;
89 }
90 
91 double DynaPidPos::error(double fPVOdPosCur)
92 {
93  m_nOdPosCur = (int)fPVOdPosCur;
94 
95  return (double)(m_nOdPosGoal - m_nOdPosCur);
96 }
97 
98 double DynaPidPos::toOutput(double fCVSpeed)
99 {
100  double fSpeed;
101 
102  fSpeed = fCVSpeed * (double)m_nOdDir;
103 
104  //
105  // Cap speed to the absolute maximum which is the target goal speed.
106  //
107  if( fSpeed > m_fSpeedAbsMax )
108  {
109  fSpeed = m_fSpeedAbsMax;
110  }
111  else if( fSpeed < -m_fSpeedAbsMax )
112  {
113  fSpeed = -m_fSpeedAbsMax;
114  }
115 
116  //
117  // If there is an absolute lower speed, then cap as necessary.
118  //
119  if( TuneAbsMinSpeed > 0.0 )
120  {
121  if( (fSpeed > 0.0) && (fSpeed < TuneAbsMinSpeed) )
122  {
123  fSpeed = TuneAbsMinSpeed;
124  }
125  else if( (fSpeed < 0.0) && (fSpeed > -TuneAbsMinSpeed) )
126  {
127  fSpeed = -TuneAbsMinSpeed;
128  }
129  }
130 
131  //
132  // Cap the acceleration so that jerkiness and oscillations are minimized.
133  //
134  if( fabs(fSpeed-m_fPrevSpeed) > TuneMaxSpeedDelta )
135  {
136  if( m_fPrevSpeed > fSpeed )
137  {
138  fSpeed = m_fPrevSpeed - TuneMaxSpeedDelta;
139  }
140  else
141  {
142  fSpeed = m_fPrevSpeed + TuneMaxSpeedDelta;
143  }
144  }
145 
146  m_fPrevSpeed = fSpeed;
147 
148  return fSpeed;
149 }
virtual double toOutput(double fCVSpeed)
Convert control variable to application-specific output value.
Definition: DynaPidPos.cxx:98
virtual void SpecifySetPoint(int nOdPosStart, int nOdPosGoal, int nSpeedLim, int nCurSpeed, bool bIsReversed, bool bUnwind=false)
Specify position setpoint.
Definition: DynaPidPos.cxx:71
virtual void SpecifySetPoint(double fSP, bool bUnwind=false)
Specify the setpoint.
Definition: DynaPid.cxx:71
static const double TuneMaxSpeedDelta
max speed change deltas
Definition: DynaPidPos.h:77
Miscellaneous collection of useful utilities.
The Dynamixel Position PID Class.
The Dynamixel PID Base Class.
RoadNarrows Dynamixel Top-Level Package Header File.
static const double TuneAbsMinSpeed
absolute minimum speed
Definition: DynaPidPos.h:78
virtual double error(double fPVodPosCur)
Calculate error from goal postion setpoint and current position variable value.
Definition: DynaPidPos.cxx:91