Robot Simulator of the Robotics Group for Self-Organization of Control  0.8.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
oneaxisservo.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2011 LpzRobots development team *
3  * Georg Martius <georg dot martius at web dot de> *
4  * Frank Guettler <guettler at informatik dot uni-leipzig dot de *
5  * Frank Hesse <frank at nld dot ds dot mpg dot de> *
6  * Ralf Der <ralfder at mis dot mpg dot de> *
7  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  * This program is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16  * GNU General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this program; if not, write to the *
20  * Free Software Foundation, Inc., *
21  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
22  * *
23  ***************************************************************************/
24 #ifndef __SERVO1_H
25 #define __SERVO1_H
26 
27 #include "joint.h"
28 #include "sensor.h"
29 #include "motor.h"
30 #include "pid.h"
31 #include "angularmotor.h"
32 #include <selforg/controller_misc.h>
33 
34 namespace lpzrobots {
35 
36  /** general servo motor to achieve position control
37  */
38  class OneAxisServo : virtual public Sensor, virtual public Motor {
39  public:
40  /** min and max values are understood as travel bounds. Min should be less than 0.*/
41 
42  OneAxisServo(OneAxisJoint* joint, double _min, double _max,
43  double power, double damp=0.2, double integration=2,
44  double maxVel=10.0,
45  double jointLimit = 1.3, bool minmaxCheck = true);
46 
47  virtual ~OneAxisServo();
48 
49  /** sets the set point of the servo.
50  Position must be between -1 and 1. It is scaled to fit into min, max
51  */
52  virtual void set(double pos);
53 
54  /** returns the position of the joint in the range [-1, 1] (scaled by min, max)*/
55  virtual double get() const {
56  double pos = joint->getPosition1();
57  if(pos > 0){
58  pos /= max;
59  }else{
60  pos /= -min;
61  }
62  return pos;
63  }
64 
65  // --- Sensor interface ---
66  virtual void init(Primitive* own, Joint* joint = 0) override { // and Motor interface
67  if(joint!=0) {
68  this->joint=dynamic_cast<OneAxisJoint*>(joint);
69  }
70  assert(this->joint);
71  }
72 
73  virtual bool sense(const GlobalData& globaldata) override { return true;};
74  virtual int getSensorNumber() const override {
75  return 1;
76  }
77  virtual std::list<sensor> getList() const { return getListOfArray();};
78  virtual int get(sensor* sensors, int length) const {
79  assert(length>0);
80  sensors[0]=get();
81  return 1;
82  }
83 
84  // --- Motor interface ---
85  virtual int getMotorNumber() const override { return 1;};
86 
87  virtual bool act(GlobalData& globaldata) override {
88  // here we should apply the forces etc, but due to backwards compatibility this remains in set()
89  // which is also called each timestep.
90  return true;
91  };
92 
93  /** sends the action commands to the motor.
94  It returns the number of used values. (should be equal to
95  getMotorNumber)
96  */
97  virtual int set(const motor* values, int length) override {
98  assert(length>0);
99  set(values[0]);
100  return 1;
101  };
102 
103 
104  // --- Parameters ---
105  virtual void setMinMax(double _min, double _max){
106  min=_min;
107  max=_max;
108  joint->setParam(dParamLoStop, min - abs(min) * (jointLimit-1));
109  joint->setParam(dParamHiStop, max + abs(max) * (jointLimit-1));
110  }
111 
112  /** adjusts the power of the servo*/
113  virtual void setPower(double power) {
114  pid.KP = power;
115  };
116 
117  /** returns the power of the servo*/
118  virtual double getPower() {
119  return pid.KP;
120  };
121 
122  /** returns the damping of the servo*/
123  virtual double getDamping() {
124  return pid.KD;
125  }
126  /** sets the damping of the servo*/
127  virtual void setDamping(double damp) {
128  pid.KD = damp;
129  };
130 
131  /** returns the integration term of the PID controller of the servo*/
132  virtual double& offsetCanceling() {
133  return pid.KI;
134  };
135 
136  /** adjusts maximal speed of servo*/
137  virtual void setMaxVel(double maxVel) {
138  this->maxVel = maxVel;
139  };
140  /** adjusts maximal speed of servo*/
141  virtual double getMaxVel() {
142  return maxVel;
143  };
144 
145 
146  protected:
148  double min;
149  double max;
151  double maxVel;
152  double jointLimit; ///< joint limit with respect to servo limit
153  };
154 
158 
159 
160 
161  /** general servo motor to achieve position control with zero position centered
162  */
164  public:
165  /** min and max values are understood as travel bounds.
166  The zero position is (max-min)/2
167  */
168  OneAxisServoCentered(OneAxisJoint* joint, double _min, double _max,
169  double power, double damp=0.2, double integration=2,
170  double maxVel=10.0, double jointLimit = 1.3);
171 
173 
174  /** sets the set point of the servo.
175  Position must be between -1 and 1. It is scaled to fit into min, max,
176  however 0 is just in the center of min and max
177  */
178  virtual void set(double pos) override ;
179 
180  /** returns the position of the slider in ranges [-1, 1] (scaled by min, max, centered)*/
181  virtual double get() const override {
182  double pos = joint->getPosition1();
183 
184  return 2*(pos-min)/(max-min) - 1;
185  }
186 
187  };
188 
189  /** general servo motor to achieve position control.
190  * It internally controls the velocity of the motor (much more stable)
191  * with centered zero position.
192  * The amount of body feeling can be adjusted by the damping parameter
193  * which is understood as a stiffness parameter
194  */
195  class OneAxisServoVel : public OneAxisServo {
196  public:
197  /** min and max values are understood as travel bounds.
198  The zero position is (max-min)/2
199  @param power is the maximal torque the servo can generate
200  @param maxVel is understood as a speed parameter of the servo.
201  @param damp adjusts the power of the servo in dependence of the distance
202  to the set point (current control error).
203  This regulates the stiffness and the body feeling
204  0: the servo has no power at the set point (maximal body feeling);
205  1: is servo has full power at the set point: maximal stiffness, perfectly damped.
206  */
207  OneAxisServoVel(const OdeHandle& odeHandle,
208  OneAxisJoint* joint, double _min, double _max,
209  double power, double damp=0.05, double maxVel=20,
210  double jointLimit = 1.3);
211 
212  virtual ~OneAxisServoVel();
213 
214  virtual void init(Primitive* own, Joint* joint = 0) override {
215  if(joint) { assert(joint==this->joint); } // we cannot attach the servo to a new joint
216  }
217 
218  /** adjusts the power of the servo*/
219  virtual void setPower(double _power) override;
220 
221  /** returns the power of the servo*/
222  virtual double getPower() override {
223  return power;
224  };
225  virtual double getDamping() override {
226  return damp;
227  };
228  virtual void setDamping(double _damp) override {
229  damp = clip(_damp,0.0,1.0);
230  };
231  /** offetCanceling does not exist for this type of servo */
232  virtual double& offsetCanceling() override {
233  dummy=0;
234  return dummy;
235  };
236 
237  /** adjusts maximal speed of servo*/
238  virtual void setMaxVel(double maxVel) override {
239  this->maxVel = maxVel;
240  pid.KP=maxVel/2;
241  };
242  /** adjusts maximal speed of servo*/
243  virtual double getMaxVel() override {
244  return maxVel;
245  };
246 
247  /** sets the set point of the servo.
248  Position must be between -1 and 1. It is scaled to fit into min, max,
249  however 0 is just in the center of min and max
250  */
251  virtual void set(double pos) override ;
252 
253  /** returns the position of the servo in ranges [-1, 1] (scaled by min, max, centered)*/
254  virtual double get() const override {
255  double pos = joint->getPosition1();
256  return 2*(pos-min)/(max-min) - 1;
257  }
258  protected:
260  double dummy;
261  double power;
262  double damp;
263  };
264 
265 
266  /** Servo for sliders to achieve position control. Like OneAxisServoVel but
267  * suitable for sliders
268  * @see OneAxisServoVel
269  */
270  class SliderServoVel : public OneAxisServo {
271  public:
272  /** min and max values are understood as travel bounds.
273  The zero position is (max-min)/2
274  @param power is the maximal torque the servo can generate
275  @param maxVel is understood as a speed parameter of the servo.
276  @param damp adjusts the power of the servo in dependence of the distance
277  to the set point (current control error).
278  This regulates the stiffness and the body feeling
279  0: the servo has no power at the set point (maximal body feeling);
280  1: is servo has full power at the set point: maximal stiffness, perfectly damped.
281  */
282  SliderServoVel(const OdeHandle& odeHandle,
283  OneAxisJoint* joint, double _min, double _max,
284  double power, double damp=0.05, double maxVel=20,
285  double jointLimit = 1.3);
286 
287  virtual ~SliderServoVel();
288 
289  /** adjusts the power of the servo*/
290  virtual void setPower(double _power) override;
291 
292  /** returns the power of the servo*/
293  virtual double getPower() override {
294  return power;
295  };
296  virtual double getDamping() override {
297  return damp;
298  };
299  virtual void setDamping(double _damp) override {
300  damp = clip(_damp,0.0,1.0);
301  };
302  /** offetCanceling does not exist for this type of servo */
303  virtual double& offsetCanceling() override {
304  dummy=0;
305  return dummy;
306  };
307 
308  /** adjusts maximal speed of servo*/
309  virtual void setMaxVel(double maxVel) override {
310  this->maxVel = maxVel;
311  pid.KP=maxVel/2;
312  };
313  /** adjusts maximal speed of servo*/
314  virtual double getMaxVel()override {
315  return maxVel;
316  };
317 
318  /** sets the set point of the servo.
319  Position must be between -1 and 1. It is scaled to fit into min, max,
320  however 0 is just in the center of min and max
321  */
322  virtual void set(double pos) override ;
323 
324  /** returns the position of the servo in ranges [-1, 1] (scaled by min, max, centered)*/
325  virtual double get() const override {
326  double pos = joint->getPosition1();
327  return 2*(pos-min)/(max-min) - 1;
328  }
329 
330  protected:
331  double dummy;
332  double power;
333  double damp;
334  };
335 
336 }
337 #endif
virtual double & offsetCanceling()
returns the integration term of the PID controller of the servo
Definition: oneaxisservo.h:132
virtual double & offsetCanceling() override
offetCanceling does not exist for this type of servo
Definition: oneaxisservo.h:232
Data structure for accessing the ODE.
Definition: odehandle.h:44
Servo for sliders to achieve position control.
Definition: oneaxisservo.h:270
OneAxisServo HingeServo
Definition: oneaxisservo.h:156
double KI
Definition: pid.h:45
double power(void *c, double x)
returns x the power c (as a double)
Definition: controller_misc.cpp:17
double damp
Definition: oneaxisservo.h:333
general servo motor to achieve position control with zero position centered
Definition: oneaxisservo.h:163
virtual double getPosition1() const =0
double dummy
Definition: oneaxisservo.h:260
double damp
Definition: oneaxisservo.h:262
Definition: joint.h:41
double dummy
Definition: oneaxisservo.h:331
virtual int getSensorNumber() const override
returns the number of sensors values produced by this sensor
Definition: oneaxisservo.h:74
double sensor
Definition: types.h:29
OneAxisServo Hinge2Servo
Definition: oneaxisservo.h:157
Definition: joint.h:108
OneAxisServoVel(const OdeHandle &odeHandle, OneAxisJoint *joint, double _min, double _max, double power, double damp=0.05, double maxVel=20, double jointLimit=1.3)
min and max values are understood as travel bounds.
Definition: oneaxisservo.cpp:85
virtual double getPower() override
returns the power of the servo
Definition: oneaxisservo.h:293
virtual void setDamping(double _damp) override
sets the damping of the servo
Definition: oneaxisservo.h:299
SliderServoVel(const OdeHandle &odeHandle, OneAxisJoint *joint, double _min, double _max, double power, double damp=0.05, double maxVel=20, double jointLimit=1.3)
min and max values are understood as travel bounds.
Definition: oneaxisservo.cpp:125
virtual void init(Primitive *own, Joint *joint=0) override
initialises motor with body of robot
Definition: oneaxisservo.h:66
virtual void setMaxVel(double maxVel) override
adjusts maximal speed of servo
Definition: oneaxisservo.h:309
AngularMotor1Axis motor
Definition: oneaxisservo.h:259
OneAxisJoint * joint
Definition: oneaxisservo.h:143
virtual double getMaxVel() override
adjusts maximal speed of servo
Definition: oneaxisservo.h:314
Abstract class for sensors that can be plugged into a robot.
Definition: sensor.h:43
virtual void init(Primitive *own, Joint *joint=0) override
initialises motor with body of robot
Definition: oneaxisservo.h:214
virtual void setMinMax(double _min, double _max)
Definition: oneaxisservo.h:105
T abs(T v)
absolute function for all types
Definition: stl_adds.h:35
virtual double getDamping()
returns the damping of the servo
Definition: oneaxisservo.h:123
virtual void set(double pos)
sets the set point of the servo.
Definition: oneaxisservo.cpp:45
virtual ~OneAxisServoVel()
Definition: oneaxisservo.cpp:97
virtual void setMaxVel(double maxVel)
adjusts maximal speed of servo
Definition: oneaxisservo.h:137
virtual void set(double pos) override
sets the set point of the servo.
Definition: oneaxisservo.cpp:70
Abstact base class for attachable motors.
Definition: motor.h:35
Interface class for primitives represented in the physical and graphical world.
Definition: primitive.h:80
virtual bool act(GlobalData &globaldata) override
performs the actions, This is usually called in doInternalStuff() from the robot
Definition: oneaxisservo.h:87
double max
Definition: oneaxisservo.h:149
Data structure holding all essential global information.
Definition: globaldata.h:57
Definition: pid.h:29
virtual void set(double pos) override
sets the set point of the servo.
Definition: oneaxisservo.cpp:104
double clip(double r, double x)
clipping function for mapP
Definition: controller_misc.cpp:39
virtual bool sense(const GlobalData &globaldata) override
performs sense action
Definition: oneaxisservo.h:73
virtual ~OneAxisServo()
Definition: oneaxisservo.cpp:43
virtual int set(const motor *values, int length) override
sends the action commands to the motor.
Definition: oneaxisservo.h:97
virtual ~SliderServoVel()
Definition: oneaxisservo.cpp:136
virtual double getMaxVel() override
adjusts maximal speed of servo
Definition: oneaxisservo.h:243
general servo motor to achieve position control.
Definition: oneaxisservo.h:195
virtual double getMaxVel()
adjusts maximal speed of servo
Definition: oneaxisservo.h:141
double maxVel
Definition: oneaxisservo.h:151
double jointLimit
joint limit with respect to servo limit
Definition: oneaxisservo.h:152
virtual void setDamping(double _damp) override
sets the damping of the servo
Definition: oneaxisservo.h:228
virtual void set(double pos) override
sets the set point of the servo.
Definition: oneaxisservo.cpp:142
double min
Definition: oneaxisservo.h:148
OneAxisServo(OneAxisJoint *joint, double _min, double _max, double power, double damp=0.2, double integration=2, double maxVel=10.0, double jointLimit=1.3, bool minmaxCheck=true)
min and max values are understood as travel bounds.
Definition: oneaxisservo.cpp:31
PID pid
Definition: oneaxisservo.h:150
double motor
Definition: types.h:30
double KD
Definition: pid.h:46
virtual ~OneAxisServoCentered()
Definition: oneaxisservo.h:172
OneAxisServoCentered(OneAxisJoint *joint, double _min, double _max, double power, double damp=0.2, double integration=2, double maxVel=10.0, double jointLimit=1.3)
min and max values are understood as travel bounds.
Definition: oneaxisservo.cpp:63
virtual double getDamping() override
returns the damping of the servo
Definition: oneaxisservo.h:225
virtual double getDamping() override
returns the damping of the servo
Definition: oneaxisservo.h:296
virtual void setParam(int parameter, double value)=0
sets the ODE joint parameter (see ODE manual)
virtual std::list< sensor > getList() const
returns a list of sensor values (usually in the range [-1,1] ) This function should be overloaded...
Definition: oneaxisservo.h:77
virtual void setMaxVel(double maxVel) override
adjusts maximal speed of servo
Definition: oneaxisservo.h:238
double power
Definition: oneaxisservo.h:332
virtual int getMotorNumber() const override
return the dimensionality of this motor
Definition: oneaxisservo.h:85
virtual double getPower() override
returns the power of the servo
Definition: oneaxisservo.h:222
double KP
Definition: pid.h:44
virtual void setPower(double _power) override
adjusts the power of the servo
Definition: oneaxisservo.cpp:138
double power
Definition: oneaxisservo.h:261
virtual void setPower(double power)
adjusts the power of the servo
Definition: oneaxisservo.h:113
virtual double & offsetCanceling() override
offetCanceling does not exist for this type of servo
Definition: oneaxisservo.h:303
virtual void setPower(double _power) override
adjusts the power of the servo
Definition: oneaxisservo.cpp:99
Angular motor for OneAxisJoints.
Definition: angularmotor.h:111
virtual double getPower()
returns the power of the servo
Definition: oneaxisservo.h:118
general servo motor to achieve position control
Definition: oneaxisservo.h:38
std::list< sensor > getListOfArray() const
helper function for performance implementation of list<> get() based on array-get ...
Definition: sensor.h:99
virtual void setDamping(double damp)
sets the damping of the servo
Definition: oneaxisservo.h:127
OneAxisServo SliderServo
Definition: oneaxisservo.h:155