bio robot

Dependencies:   MPU6050-DMP QEI_hw mbed-rpc mbed

Fork of MPU6050_Example by Shundo Kishi

Controls/Dynamics.cpp

Committer:
amandaghassaei
Date:
2015-12-08
Revision:
11:711d3c207e8c
Parent:
10:769cc457c3a4
Child:
12:49813131dd15

File content as of revision 11:711d3c207e8c:

#include "Dynamics.h"
#include <math.h>
# define M_PI           3.14159265358979323846

float calcTau(volatile float z[4], float p[10], Gains *gains, Serial *pc){
    
    float th1 = z[0];
    float th2 = z[1];
    float dth1 = z[2];
    float dth2 = z[3];
    
    float E = getEnergy(z, p);
    
    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 th2Des = thetaDesired(2.2, th1, th2, dth1, dth2);
    
    float ddth2 = K*(th2Des - th2) - D*dth2;
    float tau = gains->getCurrentP()*AHat*ddth2 + corrCentripCompHat + gravityCompHat;

    return tau;
}

float thetaDesired(float range, float th1, float th2, float dth1, float dth2){
    
    int numTurns = fix(th1/(2*M_PI));
    float th1Rel = th1-numTurns*2*M_PI;
    
    return signNonZero(dth1)*(range-abs(th1Rel));//*cos(th1)
}

int fix(float val){//round toward zero
    return val > 0 ? floor(val) : ceil(val);
}

int signNonZero(float val){
    if (val < 0) return -1;
    return 1;
}