bio robot
Dependencies: MPU6050-DMP QEI_hw mbed-rpc mbed
Fork of MPU6050_Example by
Controls/Dynamics.cpp
- Committer:
- amandaghassaei
- Date:
- 2015-12-10
- Revision:
- 15:d88f10b3b5f8
- Parent:
- 14:d620415259b1
- Child:
- 16:5b19be27f08a
File content as of revision 15:d88f10b3b5f8:
#include "Dynamics.h" #include <math.h> # define M_PI 3.14159265358979323846 float calcTau(volatile float z[4], float p[10], Gains *gains, Target *target, Serial *pc){ float th1 = z[0]; float th2 = z[1]; float dth1 = z[2]; float dth2 = z[3]; float A[2][2]; getMassMatrix(A, z, p); float AHat = A[1][1]-A[1][0]*A[0][1]/A[0][0]; float corrCentripComp[2]; getCoriolisCentrip(corrCentripComp, z, p); float corrCentripCompHat = corrCentripComp[1]-A[1][0]*corrCentripComp[0]/A[0][0]; float gravityComp[2]; getGravity(gravityComp, z, p); float gravityCompHat = gravityComp[1]-A[1][0]*gravityComp[0]/A[0][0]; float K = gains->getSwingUpK(); float D = gains->getSwingUpD(); float force; if (getEnergy(z, p) > target->getTargetEnergy()) { float th2Des = target->getTheta2ForTarget(z); force = K*(th2Des - th2) - D*dth2; } else { float softLimit = 1.5;//2.5;//143 degrees float th2Des = thetaDesiredForSwingUp(-softLimit, softLimit, z); th2Des = obstacleAvoidance(z, p, th2Des); force = K*(th2Des - th2) - D*dth2; } return force;// + corrCentripCompHat + gravityCompHat;//gains->getDesiredThetaP()*AHat* } float obstacleAvoidance(volatile float z[4], float p[10], float theta){ float armLength = p[0]; float latticePitch = p[9]; float safeRad = 0.07; float th2MinMin = M_PI-2.0*asin((latticePitch-safeRad)/(2.0*armLength)); float th2MinMax = M_PI-2.0*asin((latticePitch*sqrt(2.0)-safeRad)/(2.0*armLength)); float th2MinAvg = (th2MinMin+th2MinMax)/2.0; float th2MinAmp = (th2MinMin-th2MinAvg)/2.0; float th1 = z[0]; float th2 = z[1]; float direction = 1; if (th2<0) direction = -1; float th2MinPhase = direction*th2MinMin; float th2Min = th2MinMin+th2MinAmp*cos(4.0*(th1+th2MinPhase)); if (direction*theta < th2Min) return direction*th2Min; return theta; } float thetaDesiredForSwingUp(float rangeMin, float rangeMax, volatile float z[4]){ float th1 = z[0]; float dth1 = z[2]; int numTurns = fix(th1/(2*M_PI)); float th1Rel = th1-numTurns*2*M_PI; if (dth1<0) return rangeMin*abs(cos(th1Rel/2.0));//-abs(th1Rel));//*cos(th1) return rangeMax*abs(cos(th1Rel/2.0)); } int fix(float val){//round toward zero return val > 0 ? floor(val) : ceil(val); }