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 #ifndef __SERVO1_H
00048 #define __SERVO1_H
00049
00050 #include "joint.h"
00051 #include "pid.h"
00052
00053 namespace lpzrobots {
00054
00055
00056
00057 class OneAxisServo {
00058 public:
00059
00060
00061 OneAxisServo(OneAxisJoint* joint, double _min, double _max,
00062 double power, double damp=0.2, double integration=2, double maxVel=10.0)
00063 : joint(joint), pid(power, integration, damp), maxVel(maxVel) {
00064 assert(joint);
00065 setMinMax(_min,_max);
00066 assert(min<max);
00067 assert(min <= 0);
00068 assert(max >= 0);
00069 assert(power>=0 && damp >=0 && integration >=0);
00070 }
00071
00072 virtual ~OneAxisServo(){}
00073
00074
00075
00076
00077 virtual void set(double pos){
00078 if(pos > 0){
00079 pos *= max;
00080 }else{
00081 pos *= -min;
00082 }
00083 pid.setTargetPosition(pos);
00084
00085 double force = pid.step(joint->getPosition1(), joint->odeHandle.getTime());
00086 force = std::min(pid.KP, std::max(-pid.KP,force));
00087 joint->addForce1(force);
00088 joint->getPart1()->limitLinearVel(maxVel);
00089 joint->getPart2()->limitLinearVel(maxVel);
00090 }
00091
00092
00093 virtual double get(){
00094 double pos = joint->getPosition1();
00095 if(pos > 0){
00096 pos /= max;
00097 }else{
00098 pos /= -min;
00099 }
00100 return pos;
00101 }
00102
00103 virtual void setMinMax(double _min, double _max){
00104 min=_min;
00105 max=_max;
00106 joint->setParam(dParamLoStop, min * 1.3);
00107 joint->setParam(dParamHiStop, max * 1.3);
00108 }
00109
00110
00111 virtual void setPower(double power) {
00112 pid.KP = power;
00113 };
00114
00115 virtual double& power() {
00116 return pid.KP;
00117 };
00118
00119
00120 virtual double& damping() {
00121 return pid.KD;
00122 };
00123
00124 virtual double& offsetCanceling() {
00125 return pid.KI;
00126 };
00127
00128 private:
00129 OneAxisJoint* joint;
00130 double min;
00131 double max;
00132 PID pid;
00133 double maxVel;
00134 };
00135
00136 typedef OneAxisServo SliderServo;
00137 typedef OneAxisServo HingeServo;
00138 typedef OneAxisServo Hinge2Servo;
00139
00140 }
00141 #endif