00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064 #ifndef __SERVO1_H
00065 #define __SERVO1_H
00066
00067 #include "joint.h"
00068 #include "pid.h"
00069 #include "angularmotor.h"
00070 #include <selforg/controller_misc.h>
00071
00072 namespace lpzrobots {
00073
00074
00075
00076 class OneAxisServo {
00077 public:
00078
00079
00080 OneAxisServo(OneAxisJoint* joint, double _min, double _max,
00081 double power, double damp=0.2, double integration=2, double maxVel=10.0,
00082 double jointLimit = 1.3, bool minmaxCheck = true)
00083 : joint(joint), pid(power, integration, damp), maxVel(maxVel), jointLimit(jointLimit) {
00084 assert(joint);
00085 setMinMax(_min,_max);
00086 assert(min<max);
00087 assert(!minmaxCheck || min <= 0);
00088 assert(!minmaxCheck || max >= 0);
00089 assert(power>=0 && damp >=0 && integration >=0);
00090 }
00091
00092 virtual ~OneAxisServo(){}
00093
00094
00095
00096
00097 virtual void set(double pos){
00098 pos = clip(pos, -1.0, 1.0);
00099 if(pos > 0){
00100 pos *= max;
00101 }else{
00102 pos *= -min;
00103 }
00104 pid.setTargetPosition(pos);
00105
00106 double force = pid.step(joint->getPosition1(), joint->odeHandle.getTime());
00107 force = std::min(pid.KP, std::max(-pid.KP,force));
00108 joint->addForce1(force);
00109 if(maxVel>0){
00110 joint->getPart1()->limitLinearVel(maxVel);
00111 joint->getPart2()->limitLinearVel(maxVel);
00112 }
00113 }
00114
00115
00116 virtual double get(){
00117 double pos = joint->getPosition1();
00118 if(pos > 0){
00119 pos /= max;
00120 }else{
00121 pos /= -min;
00122 }
00123 return pos;
00124 }
00125
00126 virtual void setMinMax(double _min, double _max){
00127 min=_min;
00128 max=_max;
00129 joint->setParam(dParamLoStop, min * jointLimit);
00130 joint->setParam(dParamHiStop, max * jointLimit);
00131 }
00132
00133
00134 virtual void setPower(double power) {
00135 pid.KP = power;
00136 };
00137
00138
00139 virtual double getPower() {
00140 return pid.KP;
00141 };
00142
00143
00144 virtual double& damping() {
00145 return pid.KD;
00146 };
00147
00148 virtual double& offsetCanceling() {
00149 return pid.KI;
00150 };
00151
00152
00153 virtual void setMaxVel(double maxVel) {
00154 this->maxVel = maxVel;
00155 };
00156
00157 virtual double getMaxVel() {
00158 return maxVel;
00159 };
00160
00161
00162 protected:
00163 OneAxisJoint* joint;
00164 double min;
00165 double max;
00166 PID pid;
00167 double maxVel;
00168 double jointLimit;
00169 };
00170
00171 typedef OneAxisServo SliderServo;
00172 typedef OneAxisServo HingeServo;
00173 typedef OneAxisServo Hinge2Servo;
00174
00175
00176
00177
00178
00179 class OneAxisServoCentered : public OneAxisServo {
00180 public:
00181
00182
00183
00184 OneAxisServoCentered(OneAxisJoint* joint, double _min, double _max,
00185 double power, double damp=0.2, double integration=2,
00186 double maxVel=10.0, double jointLimit = 1.3)
00187 : OneAxisServo(joint, _min, _max, power, damp, integration, maxVel, jointLimit, false){
00188 }
00189 virtual ~OneAxisServoCentered(){}
00190
00191
00192
00193
00194
00195 virtual void set(double pos){
00196 pos = clip(pos, -1.0, 1.0);
00197 pos = (pos+1)*(max-min)/2 + min;
00198
00199 pid.setTargetPosition(pos);
00200 double force = pid.stepNoCutoff(joint->getPosition1(), joint->odeHandle.getTime());
00201 force = clip(force,-10*pid.KP, 10*pid.KP);
00202 joint->addForce1(force);
00203 if(maxVel>0){
00204 joint->getPart1()->limitLinearVel(maxVel);
00205 joint->getPart2()->limitLinearVel(maxVel);
00206 }
00207 }
00208
00209 virtual double get(){
00210 double pos = joint->getPosition1();
00211
00212 return 2*(pos-min)/(max-min) - 1;
00213 }
00214
00215 };
00216
00217
00218
00219
00220
00221 class OneAxisServoVel : public OneAxisServo {
00222 public:
00223
00224
00225
00226 OneAxisServoVel(const OdeHandle& odeHandle,
00227 OneAxisJoint* joint, double _min, double _max,
00228 double power, double damp=0.01, double maxVel=20, double jointLimit = 1.3)
00229 : OneAxisServo(joint, _min, _max, maxVel/2, damp, 0, 0, jointLimit, false),
00230 motor(odeHandle, joint, power)
00231 {
00232 }
00233
00234 virtual ~OneAxisServoVel(){}
00235
00236
00237 virtual void setPower(double power) {
00238 motor.setPower(power);
00239 };
00240
00241 virtual double getPower() {
00242 return motor.getPower();
00243 };
00244
00245 virtual double& offsetCanceling() {
00246 dummy=0;
00247 return dummy;
00248 };
00249
00250
00251 virtual void setMaxVel(double maxVel) {
00252 this->maxVel = maxVel;
00253 pid.KP=maxVel/2;
00254 };
00255
00256 virtual double getMaxVel() {
00257 return maxVel;
00258 };
00259
00260
00261
00262
00263
00264
00265 virtual void set(double pos){
00266 pos = clip(pos, -1.0, 1.0);
00267 pos = (pos+1)*(max-min)/2 + min;
00268 pid.setTargetPosition(pos);
00269 double vel = pid.stepNoCutoff(joint->getPosition1(), joint->odeHandle.getTime());
00270 motor.set(0, vel);
00271 }
00272
00273
00274 virtual double get(){
00275 double pos = joint->getPosition1();
00276 return 2*(pos-min)/(max-min) - 1;
00277 }
00278 protected:
00279 AngularMotor1Axis motor;
00280 double dummy;
00281 };
00282
00283 }
00284 #endif