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
twoaxisservo.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 __SERVO2_H
25 #define __SERVO2_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 for 2 axis joints
37  */
38  class TwoAxisServo : 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  TwoAxisServo(TwoAxisJoint* joint, double _min1, double _max1, double power1,
43  double _min2, double _max2, double power2,
44  double damp=0.2, double integration=2, double maxVel=10.0,
45  double jointLimit = 1.3, bool minmaxCheck=true);
46 
47  virtual ~TwoAxisServo();
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 pos1, double pos2);
53 
54  /** returns the position of the servo (joint) of 1. axis in ranges [-1, 1] (scaled by min1, max1)*/
55  virtual double get1() const {
56  double pos = joint->getPosition1();
57  if(pos > 0){
58  pos /= max1;
59  }else{
60  pos /= -min1;
61  }
62  return pos;
63  }
64 
65  /** returns the position of the servo (joint) of 2. axis in ranges [-1, 1] (scaled by min2, max2)*/
66  virtual double get2() const {
67  double pos = joint->getPosition2();
68  if(pos > 0){
69  pos /= max2;
70  }else{
71  pos /= -min2;
72  }
73  return pos;
74  }
75 
76  /** returns the positions of the joint in ranges [-1, 1] (scaled by min, max)*/
77  void get(double& p1, double& p2) const {
78  p1=get1();
79  p2=get2();
80  }
81 
82  // --- Sensor interface ---
83  virtual void init(Primitive* own, Joint* joint = 0) override { // and Motor interface
84  if(joint!=0) {
85  this->joint=dynamic_cast<TwoAxisJoint*>(joint);
86  }
87  assert(this->joint);
88  }
89 
90  virtual bool sense(const GlobalData& globaldata) override { return true;};
91  virtual int getSensorNumber() const override {
92  return 2;
93  }
94  virtual std::list<sensor> getList() const { return getListOfArray();};
95  virtual int get(sensor* sensors, int length) const {
96  assert(length>1);
97  sensors[0]=get1();
98  sensors[1]=get2();
99  return 2;
100  }
101 
102  // --- Motor interface ---
103  virtual int getMotorNumber() const override { return 2;};
104 
105  virtual bool act(GlobalData& globaldata) override {
106  // here we should apply the forces etc, but due to backwards compatibility this remains in set()
107  // which is also called each timestep.
108  return true;
109  };
110 
111  /** sends the action commands to the motor.
112  It returns the number of used values. (should be equal to
113  getMotorNumber)
114  */
115  virtual int set(const motor* values, int length) override {
116  assert(length>1);
117  set(values[0], values[1]);
118  return 2;
119  };
120 
121  // --- Parameters ---
122 
123  /** adjusts the power of the servo*/
124  virtual void setPower(double power1, double power2) {
125  pid1.KP = power1;
126  pid2.KP = power2;
127  };
128 
129  /** returns the power of the servo*/
130  virtual void setPower1(double power1) {
131  pid1.KP = power1;
132  };
133 
134  /** returns the power of the servo*/
135  virtual void setPower2(double power2) {
136  pid2.KP = power2;
137  };
138 
139  /** returns the power of the servo*/
140  virtual double getPower1() {
141  return pid1.KP;
142  };
143  /** returns the power of the servo*/
144  virtual double getPower2() {
145  return pid2.KP;
146  };
147 
148  /** returns the damping of the servo (axis 1) */
149  virtual double getDamping1() {
150  return pid1.KD;
151  };
152 
153  /** returns the damping of the servo (axis 2) */
154  virtual double getDamping2() {
155  return pid2.KD;
156  };
157 
158  virtual TwoAxisJoint* getJoint() {
159  return joint;
160  }
161 
162  /** sets the damping of the servo (axis 1) */
163  virtual void setDamping1(double damp) {
164  pid1.KD = damp;
165  };
166 
167  /** sets the damping of the servo (axis 1) */
168  virtual void setDamping2(double damp) {
169  pid2.KD = damp;
170  };
171 
172  /** sets the damping of the servo (both axis) */
173  virtual void setDamping(double _damp) {
174  setDamping1(_damp);
175  setDamping2(_damp);
176  };
177 
178  /** returns the damping of the servo*/
179  virtual double& offsetCanceling() {
180  return pid1.KI;
181  };
182 
183  virtual void setMinMax1(double _min, double _max){
184  min1=_min;
185  max1=_max;
186  joint->setParam(dParamLoStop, _min - abs(_min) * (jointLimit-1));
187  joint->setParam(dParamHiStop, _max + abs(_max) * (jointLimit-1));
188  }
189 
190  virtual void setMinMax2(double _min, double _max){
191  min2=_min;
192  max2=_max;
193  joint->setParam(dParamLoStop2, _min - abs(_min) * (jointLimit-1));
194  joint->setParam(dParamHiStop2, _max + abs(_max) * (jointLimit-1));
195  }
196 
197  /** adjusts maximal speed of servo*/
198  virtual void setMaxVel(double maxVel) {
199  this->maxVel = maxVel;
200  };
201  /** adjusts maximal speed of servo*/
202  virtual double getMaxVel() {
203  return maxVel;
204  };
205 
206 
207  protected:
209  double min1;
210  double max1;
211  double min2;
212  double max2;
215  double maxVel;
216  double jointLimit;
217  };
218 
220 
221 
222  /** general servo motor for 2 axis joints with zero position centered
223  */
225  public:
226  /** min and max values are understood as travel bounds.
227  The zero position is (max-min)/2
228  */
229  TwoAxisServoCentered(TwoAxisJoint* joint, double _min1, double _max1, double power1,
230  double _min2, double _max2, double power2,
231  double damp=0.2, double integration=2, double maxVel=10.0,
232  double jointLimit = 1.3);
233 
234  virtual ~TwoAxisServoCentered();
235 
236  /** sets the set point of the servo.
237  Position must be between -1 and 1. It is scaled to fit into min, max,
238  however 0 is just in the center of min and max
239  */
240  virtual void set(double pos1, double pos2) override ;
241 
242  /** returns the position of the servo (joint) of 1. axis in ranges [-1, 1]
243  (scaled by min1, max1, centered)*/
244  virtual double get1() const override {
245  double pos = joint->getPosition1();
246  return 2*(pos-min1)/(max1-min1) - 1;
247  }
248 
249 
250  /** returns the position of the servo (joint) of 2. axis in ranges [-1, 1]
251  (scaled by min2, max2, centered)*/
252  virtual double get2() const override {
253  double pos = joint->getPosition2();
254  return 2*(pos-min2)/(max2-min2) - 1;
255  }
256 
257  };
258 
259 
260  /** general servo motor to achieve position control for 2 axis joints
261  * that internally controls the velocity of the motor (much more stable)
262  * with centered zero position.
263  * The amount of body feeling can be adjusted by the damping parameter
264  * which is understood as a stiffness parameter
265  */
267  public:
268  /** min and max values are understood as travel bounds.
269  The zero position is (max-min)/2
270  @param power is the maximal torque the servo can generate
271  @param maxVel is understood as a speed parameter of the servo.
272  @param damp adjusts the power of the servo in dependence of the distance
273  to the set point. This regulates the stiffness and the body feeling
274  0: the servo has no power at the set point (maximal body feeling);
275  1: is servo has full power at the set point: perfectly damped and stiff.
276  */
277  TwoAxisServoVel(const OdeHandle& odeHandle,
278  TwoAxisJoint* joint, double _min1, double _max1, double power1,
279  double _min2, double _max2, double power2,
280  double damp=0.05, double maxVel=10.0, double jointLimit = 1.3);
281 
282  virtual ~TwoAxisServoVel();
283 
284  virtual void init(Primitive* own, Joint* joint = 0) override {
285  if(joint) { assert(joint==this->joint); } // we cannot attach the servo to a new joint
286  }
287 
288 
289  virtual void setPower(double _power1, double _power2) override;
290 
291  virtual void setPower1(double _power1) override;
292 
293  virtual void setPower2(double _power2) override;
294 
295  virtual double getPower1() override {
296  return power1;
297  };
298  virtual double getPower2() override {
299  return power2;
300  };
301 
302  virtual double getDamping1() override {
303  return damp;
304  };
305  virtual double getDamping2() override {
306  return damp;
307  };
308  virtual void setDamping1(double _damp) override {
309  damp = clip(_damp,0.0,1.0);
310  };
311  virtual void setDamping2(double _damp) override {
312  setDamping1(_damp);
313  };
314 
315  /** offetCanceling does not exist for this type of servo */
316  virtual double& offsetCanceling() override {
317  dummy=0;
318  return dummy;
319  };
320 
321  /** adjusts maximal speed of servo*/
322  virtual void setMaxVel(double maxVel) override {
323  this->maxVel = maxVel;
324  pid1.KP=maxVel/2;
325  pid2.KP=maxVel/2;
326  };
327  /** adjusts maximal speed of servo*/
328  virtual double getMaxVel() override {
329  return maxVel;
330  };
331 
332 
333  /** sets the set point of the servo.
334  Position must be between -1 and 1. It is scaled to fit into min, max,
335  however 0 is just in the center of min and max
336  */
337  virtual void set(double pos1, double pos2) override ;
338 
339  protected:
341  double dummy;
342  double damp;
343  double power1;
344  double power2;
345  };
346 
347 
348 }
349 #endif
virtual void setPower(double power1, double power2)
adjusts the power of the servo
Definition: twoaxisservo.h:124
virtual double getDamping2() override
returns the damping of the servo (axis 2)
Definition: twoaxisservo.h:305
double max1
Definition: twoaxisservo.h:210
virtual bool act(GlobalData &globaldata) override
performs the actions, This is usually called in doInternalStuff() from the robot
Definition: twoaxisservo.h:105
Data structure for accessing the ODE.
Definition: odehandle.h:44
virtual void set(double pos1, double pos2)
sets the set point of the servo.
Definition: twoaxisservo.cpp:48
double dummy
Definition: twoaxisservo.h:341
virtual double getDamping2()
returns the damping of the servo (axis 2)
Definition: twoaxisservo.h:154
double KI
Definition: pid.h:45
virtual std::list< sensor > getList() const
returns a list of sensor values (usually in the range [-1,1] ) This function should be overloaded...
Definition: twoaxisservo.h:94
Angular motor for TwoAxisJoints.
Definition: angularmotor.h:141
virtual TwoAxisJoint * getJoint()
Definition: twoaxisservo.h:158
virtual void setPower1(double _power1) override
returns the power of the servo
Definition: twoaxisservo.cpp:131
virtual double getPosition1() const =0
virtual double & offsetCanceling() override
offetCanceling does not exist for this type of servo
Definition: twoaxisservo.h:316
TwoAxisJoint * joint
Definition: twoaxisservo.h:204
general servo motor for 2 axis joints
Definition: twoaxisservo.h:38
virtual double getMaxVel()
adjusts maximal speed of servo
Definition: twoaxisservo.h:202
Definition: joint.h:41
PID pid2
Definition: twoaxisservo.h:214
virtual void setDamping(double _damp)
sets the damping of the servo (both axis)
Definition: twoaxisservo.h:173
virtual double getDamping1() override
returns the damping of the servo (axis 1)
Definition: twoaxisservo.h:302
virtual ~TwoAxisServoVel()
Definition: twoaxisservo.cpp:124
virtual void setDamping1(double damp)
sets the damping of the servo (axis 1)
Definition: twoaxisservo.h:163
virtual void set(double pos1, double pos2) override
sets the set point of the servo.
Definition: twoaxisservo.cpp:141
virtual void init(Primitive *own, Joint *joint=0) override
initialises motor with body of robot
Definition: twoaxisservo.h:284
double sensor
Definition: types.h:29
virtual double get2() const override
returns the position of the servo (joint) of 2.
Definition: twoaxisservo.h:252
virtual double getPower1() override
returns the power of the servo
Definition: twoaxisservo.h:295
virtual ~TwoAxisServoCentered()
Definition: twoaxisservo.cpp:87
double power2
Definition: twoaxisservo.h:344
general servo motor for 2 axis joints with zero position centered
Definition: twoaxisservo.h:224
virtual double getDamping1()
returns the damping of the servo (axis 1)
Definition: twoaxisservo.h:149
Abstract class for sensors that can be plugged into a robot.
Definition: sensor.h:43
T abs(T v)
absolute function for all types
Definition: stl_adds.h:35
virtual bool sense(const GlobalData &globaldata) override
performs sense action
Definition: twoaxisservo.h:90
virtual double getMaxVel() override
adjusts maximal speed of servo
Definition: twoaxisservo.h:328
TwoAxisServo UniversalServo
Definition: twoaxisservo.h:219
virtual void setPower1(double power1)
returns the power of the servo
Definition: twoaxisservo.h:130
virtual double getPosition2() const =0
virtual double & offsetCanceling()
returns the damping of the servo
Definition: twoaxisservo.h:179
virtual double getPower2()
returns the power of the servo
Definition: twoaxisservo.h:144
virtual void setMaxVel(double maxVel) override
adjusts maximal speed of servo
Definition: twoaxisservo.h:322
virtual void setDamping1(double _damp) override
sets the damping of the servo (axis 1)
Definition: twoaxisservo.h:308
double jointLimit
Definition: twoaxisservo.h:216
double maxVel
Definition: twoaxisservo.h:215
Abstact base class for attachable motors.
Definition: motor.h:35
virtual void setPower2(double power2)
returns the power of the servo
Definition: twoaxisservo.h:135
virtual void setPower2(double _power2) override
returns the power of the servo
Definition: twoaxisservo.cpp:136
Interface class for primitives represented in the physical and graphical world.
Definition: primitive.h:80
Definition: joint.h:128
virtual double get1() const
returns the position of the servo (joint) of 1.
Definition: twoaxisservo.h:55
virtual double getPower2() override
returns the power of the servo
Definition: twoaxisservo.h:298
TwoAxisServoCentered(TwoAxisJoint *joint, double _min1, double _max1, double power1, double _min2, double _max2, double power2, 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: twoaxisservo.cpp:78
Data structure holding all essential global information.
Definition: globaldata.h:57
Definition: pid.h:29
double clip(double r, double x)
clipping function for mapP
Definition: controller_misc.cpp:39
virtual int set(const motor *values, int length) override
sends the action commands to the motor.
Definition: twoaxisservo.h:115
double min1
Definition: twoaxisservo.h:209
double power1
Definition: twoaxisservo.h:343
double motor
Definition: types.h:30
virtual void set(double pos1, double pos2) override
sets the set point of the servo.
Definition: twoaxisservo.cpp:89
double KD
Definition: pid.h:46
virtual int getSensorNumber() const override
returns the number of sensors values produced by this sensor
Definition: twoaxisservo.h:91
virtual double getPower1()
returns the power of the servo
Definition: twoaxisservo.h:140
virtual void setMinMax1(double _min, double _max)
Definition: twoaxisservo.h:183
virtual void setMaxVel(double maxVel)
adjusts maximal speed of servo
Definition: twoaxisservo.h:198
virtual void setDamping2(double _damp) override
sets the damping of the servo (axis 1)
Definition: twoaxisservo.h:311
PID pid1
Definition: twoaxisservo.h:213
virtual void setParam(int parameter, double value)=0
sets the ODE joint parameter (see ODE manual)
virtual void setPower(double _power1, double _power2) override
adjusts the power of the servo
Definition: twoaxisservo.cpp:126
virtual double get2() const
returns the position of the servo (joint) of 2.
Definition: twoaxisservo.h:66
double KP
Definition: pid.h:44
virtual void init(Primitive *own, Joint *joint=0) override
initialises motor with body of robot
Definition: twoaxisservo.h:83
virtual int getMotorNumber() const override
return the dimensionality of this motor
Definition: twoaxisservo.h:103
virtual ~TwoAxisServo()
Definition: twoaxisservo.cpp:46
double max2
Definition: twoaxisservo.h:212
virtual double get1() const override
returns the position of the servo (joint) of 1.
Definition: twoaxisservo.h:244
AngularMotor2Axis motor
Definition: twoaxisservo.h:340
double min2
Definition: twoaxisservo.h:211
double damp
Definition: twoaxisservo.h:342
virtual void setDamping2(double damp)
sets the damping of the servo (axis 1)
Definition: twoaxisservo.h:168
general servo motor to achieve position control for 2 axis joints that internally controls the veloci...
Definition: twoaxisservo.h:266
std::list< sensor > getListOfArray() const
helper function for performance implementation of list<> get() based on array-get ...
Definition: sensor.h:99
virtual void setMinMax2(double _min, double _max)
Definition: twoaxisservo.h:190
TwoAxisServoVel(const OdeHandle &odeHandle, TwoAxisJoint *joint, double _min1, double _max1, double power1, double _min2, double _max2, double power2, double damp=0.05, double maxVel=10.0, double jointLimit=1.3)
min and max values are understood as travel bounds.
Definition: twoaxisservo.cpp:111
TwoAxisServo(TwoAxisJoint *joint, double _min1, double _max1, double power1, double _min2, double _max2, double power2, 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: twoaxisservo.cpp:30