bio robot
Dependencies: MPU6050-DMP QEI_hw mbed-rpc mbed
Fork of MPU6050_Example by
Controls/Dynamics.cpp@17:8a0e647cf551, 2015-12-11 (annotated)
- Committer:
- amandaghassaei
- Date:
- Fri Dec 11 00:44:45 2015 +0000
- Revision:
- 17:8a0e647cf551
- Parent:
- 16:5b19be27f08a
- Child:
- 18:0cfe72d8a006
eod
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
amandaghassaei | 8:1a3a69fecedf | 1 | #include "Dynamics.h" |
amandaghassaei | 8:1a3a69fecedf | 2 | #include <math.h> |
amandaghassaei | 8:1a3a69fecedf | 3 | # define M_PI 3.14159265358979323846 |
amandaghassaei | 8:1a3a69fecedf | 4 | |
amandaghassaei | 12:49813131dd15 | 5 | float calcTau(volatile float z[4], float p[10], Gains *gains, Target *target, Serial *pc){ |
amandaghassaei | 8:1a3a69fecedf | 6 | |
amandaghassaei | 8:1a3a69fecedf | 7 | float th1 = z[0]; |
amandaghassaei | 8:1a3a69fecedf | 8 | float th2 = z[1]; |
amandaghassaei | 8:1a3a69fecedf | 9 | float dth1 = z[2]; |
amandaghassaei | 8:1a3a69fecedf | 10 | float dth2 = z[3]; |
amandaghassaei | 8:1a3a69fecedf | 11 | |
amandaghassaei | 11:711d3c207e8c | 12 | float A[2][2]; |
amandaghassaei | 11:711d3c207e8c | 13 | getMassMatrix(A, z, p); |
amandaghassaei | 11:711d3c207e8c | 14 | float AHat = A[1][1]-A[1][0]*A[0][1]/A[0][0]; |
amandaghassaei | 11:711d3c207e8c | 15 | |
amandaghassaei | 11:711d3c207e8c | 16 | float corrCentripComp[2]; |
amandaghassaei | 11:711d3c207e8c | 17 | getCoriolisCentrip(corrCentripComp, z, p); |
amandaghassaei | 11:711d3c207e8c | 18 | float corrCentripCompHat = corrCentripComp[1]-A[1][0]*corrCentripComp[0]/A[0][0]; |
amandaghassaei | 11:711d3c207e8c | 19 | |
amandaghassaei | 11:711d3c207e8c | 20 | float gravityComp[2]; |
amandaghassaei | 11:711d3c207e8c | 21 | getGravity(gravityComp, z, p); |
amandaghassaei | 11:711d3c207e8c | 22 | float gravityCompHat = gravityComp[1]-A[1][0]*gravityComp[0]/A[0][0]; |
amandaghassaei | 11:711d3c207e8c | 23 | |
amandaghassaei | 9:1d9b24d7ac77 | 24 | float K = gains->getSwingUpK(); |
amandaghassaei | 9:1d9b24d7ac77 | 25 | float D = gains->getSwingUpD(); |
amandaghassaei | 8:1a3a69fecedf | 26 | |
amandaghassaei | 15:d88f10b3b5f8 | 27 | float force; |
amandaghassaei | 16:5b19be27f08a | 28 | if (target->getTargetingStarted() || target->shouldSwitchToTargetingController(z, p)) { |
amandaghassaei | 16:5b19be27f08a | 29 | target->setTargetingStarted(true); |
amandaghassaei | 17:8a0e647cf551 | 30 | float th2Des = target->getFinalTh2(z); |
amandaghassaei | 17:8a0e647cf551 | 31 | force = K*(th2Des - th2) - D*dth2; |
amandaghassaei | 17:8a0e647cf551 | 32 | // force = target->calcTargetingForce(z, p, K, D); |
amandaghassaei | 15:d88f10b3b5f8 | 33 | } else { |
amandaghassaei | 15:d88f10b3b5f8 | 34 | float softLimit = 1.5;//2.5;//143 degrees |
amandaghassaei | 15:d88f10b3b5f8 | 35 | float th2Des = thetaDesiredForSwingUp(-softLimit, softLimit, z); |
amandaghassaei | 17:8a0e647cf551 | 36 | th2Des = obstacleAvoidance(z, p, th2Des); |
amandaghassaei | 16:5b19be27f08a | 37 | force = gains->getDesiredThetaP()*AHat*(K*(th2Des - th2) - D*dth2); |
amandaghassaei | 15:d88f10b3b5f8 | 38 | } |
amandaghassaei | 8:1a3a69fecedf | 39 | |
amandaghassaei | 17:8a0e647cf551 | 40 | return force;// + corrCentripCompHat + gravityCompHat; |
amandaghassaei | 8:1a3a69fecedf | 41 | } |
amandaghassaei | 8:1a3a69fecedf | 42 | |
amandaghassaei | 15:d88f10b3b5f8 | 43 | float obstacleAvoidance(volatile float z[4], float p[10], float theta){ |
amandaghassaei | 15:d88f10b3b5f8 | 44 | |
amandaghassaei | 15:d88f10b3b5f8 | 45 | float armLength = p[0]; |
amandaghassaei | 15:d88f10b3b5f8 | 46 | float latticePitch = p[9]; |
amandaghassaei | 15:d88f10b3b5f8 | 47 | |
amandaghassaei | 15:d88f10b3b5f8 | 48 | float safeRad = 0.07; |
amandaghassaei | 15:d88f10b3b5f8 | 49 | float th2MinMin = M_PI-2.0*asin((latticePitch-safeRad)/(2.0*armLength)); |
amandaghassaei | 15:d88f10b3b5f8 | 50 | float th2MinMax = M_PI-2.0*asin((latticePitch*sqrt(2.0)-safeRad)/(2.0*armLength)); |
amandaghassaei | 15:d88f10b3b5f8 | 51 | |
amandaghassaei | 15:d88f10b3b5f8 | 52 | float th2MinAvg = (th2MinMin+th2MinMax)/2.0; |
amandaghassaei | 15:d88f10b3b5f8 | 53 | float th2MinAmp = (th2MinMin-th2MinAvg)/2.0; |
amandaghassaei | 15:d88f10b3b5f8 | 54 | |
amandaghassaei | 15:d88f10b3b5f8 | 55 | float th1 = z[0]; |
amandaghassaei | 15:d88f10b3b5f8 | 56 | float th2 = z[1]; |
amandaghassaei | 15:d88f10b3b5f8 | 57 | |
amandaghassaei | 15:d88f10b3b5f8 | 58 | float direction = 1; |
amandaghassaei | 15:d88f10b3b5f8 | 59 | if (th2<0) direction = -1; |
amandaghassaei | 15:d88f10b3b5f8 | 60 | |
amandaghassaei | 15:d88f10b3b5f8 | 61 | float th2MinPhase = direction*th2MinMin; |
amandaghassaei | 15:d88f10b3b5f8 | 62 | float th2Min = th2MinMin+th2MinAmp*cos(4.0*(th1+th2MinPhase)); |
amandaghassaei | 15:d88f10b3b5f8 | 63 | |
amandaghassaei | 15:d88f10b3b5f8 | 64 | if (direction*theta < th2Min) return direction*th2Min; |
amandaghassaei | 15:d88f10b3b5f8 | 65 | return theta; |
amandaghassaei | 15:d88f10b3b5f8 | 66 | } |
amandaghassaei | 12:49813131dd15 | 67 | |
amandaghassaei | 13:64d337c5114e | 68 | float thetaDesiredForSwingUp(float rangeMin, float rangeMax, volatile float z[4]){ |
amandaghassaei | 12:49813131dd15 | 69 | |
amandaghassaei | 12:49813131dd15 | 70 | float th1 = z[0]; |
amandaghassaei | 12:49813131dd15 | 71 | float dth1 = z[2]; |
amandaghassaei | 16:5b19be27f08a | 72 | float th1Rel = boundTheta(th1); |
amandaghassaei | 8:1a3a69fecedf | 73 | |
amandaghassaei | 13:64d337c5114e | 74 | if (dth1<0) return rangeMin*abs(cos(th1Rel/2.0));//-abs(th1Rel));//*cos(th1) |
amandaghassaei | 13:64d337c5114e | 75 | return rangeMax*abs(cos(th1Rel/2.0)); |
amandaghassaei | 8:1a3a69fecedf | 76 | } |