Carlo Collodi / kangaroo

Dependencies:   QEI mbed

Committer:
alexc89
Date:
Sun Dec 01 20:13:35 2013 +0000
Revision:
46:4497e945de6b
Parent:
45:0db0fc9f77b1
Child:
47:e01ba47d11cc
SLIP model implemented.  Jacobian being worked on.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
calamaridudeman 23:112c0be5a7f3 1 #include "mbed.h"
calamaridudeman 37:bf257a0154db 2 #include "Master.hpp"
calamaridudeman 22:4d85d989af08 3
calamaridudeman 23:112c0be5a7f3 4 Serial pc(USBTX, USBRX);
alexc89 45:0db0fc9f77b1 5 //Tick = 1200
calamaridudeman 37:bf257a0154db 6 QEI mEnc1(p25, p26, NC, 1200, QEI::X4_ENCODING); //hip
calamaridudeman 37:bf257a0154db 7 QEI mEnc2(p23, p24, NC, 1200, QEI::X4_ENCODING);//knee
calamaridudeman 37:bf257a0154db 8
calamaridudeman 43:68faf056ed5c 9 Motor m1(p15,p17,p18,p21,mEnc2);//hip p17 high is CCW
calamaridudeman 43:68faf056ed5c 10 Motor m2(p16,p19,p20,p22,mEnc1);//knee p19 high is CCW
calamaridudeman 25:8a34b8d6cc6e 11
alexc89 45:0db0fc9f77b1 12
alexc89 45:0db0fc9f77b1 13 //Tick = 500
calamaridudeman 43:68faf056ed5c 14 QEI bEnc1(p27, p28, p29, 500, QEI::X4_ENCODING); //track offset:-2.236814
calamaridudeman 43:68faf056ed5c 15 QEI bEnc2(p5, p6, p7, 500, QEI::X4_ENCODING); //body offset:1.770973
calamaridudeman 37:bf257a0154db 16
calamaridudeman 37:bf257a0154db 17 Kangaroo kankan(m1,m2,bEnc1,bEnc2);
calamaridudeman 37:bf257a0154db 18
calamaridudeman 43:68faf056ed5c 19 Ticker t;
calamaridudeman 43:68faf056ed5c 20
calamaridudeman 43:68faf056ed5c 21 AnalogIn aIn(p15);
alexc89 45:0db0fc9f77b1 22 DigitalOut Forward(p17);
alexc89 45:0db0fc9f77b1 23 DigitalOut Backward(p18);
alexc89 45:0db0fc9f77b1 24 PwmOut pwmOut(p21);
alexc89 45:0db0fc9f77b1 25
alexc89 45:0db0fc9f77b1 26
alexc89 45:0db0fc9f77b1 27 int landDetection(){
alexc89 45:0db0fc9f77b1 28 //Helper function to Detect If we have landed.
alexc89 45:0db0fc9f77b1 29 //NO Input
alexc89 45:0db0fc9f77b1 30 //Output: 0 = Flight Phase. 1 = Ground Phase.
alexc89 46:4497e945de6b 31 float th3 = bEnc1.getAngle();
alexc89 45:0db0fc9f77b1 32 float a = lg*sin(-th3)+h;
alexc89 46:4497e945de6b 33 float th1 = mEnc1.getAngle();
alexc89 46:4497e945de6b 34 float th2 = mEnc2.getAngle()-pi/2;
alexc89 46:4497e945de6b 35 float b = l1+l2*sin(th1)+l3*sin(th2-th1)+ l2*sin(th1);
alexc89 45:0db0fc9f77b1 36 return a<b;
alexc89 45:0db0fc9f77b1 37 }
calamaridudeman 43:68faf056ed5c 38
alexc89 46:4497e945de6b 39 int SLIP(float initLen){//SlIP Model
alexc89 46:4497e945de6b 40 //Get Length
alexc89 46:4497e945de6b 41 float th1 = mEnc1.getAngle();
alexc89 46:4497e945de6b 42 float th2 = mEnc2.getAngle()-pi/2;
alexc89 46:4497e945de6b 43 double y = l1+l2*sin(th1)+l3*sin(th2-th1)+ l2*sin(th1);
alexc89 46:4497e945de6b 44 double x = l2*cos(th1)+l3*cos(th2-th1)+ l2*cos(th1);
alexc89 46:4497e945de6b 45 float length = pow(pow(x,2.0)+pow(y,2.0),0.5);
alexc89 46:4497e945de6b 46 int ks = 0.1;
alexc89 46:4497e945de6b 47 float mag = (initLen-length)*ks;
alexc89 46:4497e945de6b 48 //Apply Forward Kinematics
alexc89 46:4497e945de6b 49 Point output = Point(-mag*x/length,-mag*y/length,0);
alexc89 46:4497e945de6b 50 Joints dtorque = invKinBody(output);
alexc89 46:4497e945de6b 51 //Apply Motor Torque Control.
alexc89 46:4497e945de6b 52 m1.setTorque(dtorque.t1);
alexc89 46:4497e945de6b 53 m2.setTorque(dtorque.t2);
alexc89 46:4497e945de6b 54 return 1;
alexc89 46:4497e945de6b 55 }
alexc89 46:4497e945de6b 56 int FLIGHT(float initM1, float initM2){
alexc89 46:4497e945de6b 57 //Pos Control
alexc89 46:4497e945de6b 58 m1.setPos(initM1);
alexc89 46:4497e945de6b 59 m2.setPos(initM2);
alexc89 46:4497e945de6b 60 //Awesome.
alexc89 46:4497e945de6b 61 return 1;
alexc89 46:4497e945de6b 62 }
calamaridudeman 37:bf257a0154db 63 int main() {
calamaridudeman 43:68faf056ed5c 64 Point p1(0,-.1,0);
calamaridudeman 43:68faf056ed5c 65 Point p2(0,-.6,0);
calamaridudeman 43:68faf056ed5c 66 Point p3(0,-.1,0);
calamaridudeman 43:68faf056ed5c 67 Point p[3]={p1, p2, p3};
calamaridudeman 43:68faf056ed5c 68 BezCurve curve(p, 3);
calamaridudeman 43:68faf056ed5c 69 curve.startCurve();
alexc89 45:0db0fc9f77b1 70 m1.setPos(0);
alexc89 46:4497e945de6b 71 m2.setPos(0);
alexc89 46:4497e945de6b 72
calamaridudeman 43:68faf056ed5c 73 kankan.start();
alexc89 46:4497e945de6b 74 //Initalize Control Varialbes
alexc89 45:0db0fc9f77b1 75 int Phase = 0;//Flight Phase
alexc89 46:4497e945de6b 76 int initLen = 0.17;
alexc89 46:4497e945de6b 77 float initM1 = -0.5;
alexc89 46:4497e945de6b 78 float initM2 = 0.5;
alexc89 46:4497e945de6b 79
alexc89 45:0db0fc9f77b1 80 while(true){
alexc89 45:0db0fc9f77b1 81 if (Phase == 0){//Flight Phase
alexc89 46:4497e945de6b 82 //Uses PD to Control
alexc89 46:4497e945de6b 83 //FLIGHT(initM1, initM2);
alexc89 46:4497e945de6b 84 m1.setPos(-0.5);
alexc89 45:0db0fc9f77b1 85 if(landDetection()){
alexc89 45:0db0fc9f77b1 86 //Landed going to Phase 1
alexc89 46:4497e945de6b 87 //Record initial Length.
alexc89 45:0db0fc9f77b1 88 Phase =1;
alexc89 46:4497e945de6b 89 float th1 = mEnc1.getAngle();
alexc89 46:4497e945de6b 90 float th2 = mEnc2.getAngle()-pi/2;
alexc89 46:4497e945de6b 91 double y = l1+l2*sin(th1)+l3*sin(th2-th1)+ l2*sin(th1);
alexc89 46:4497e945de6b 92 double x = l2*cos(th1)+l3*cos(th2-th1)+ l2*cos(th1);
alexc89 46:4497e945de6b 93 initLen = pow(pow(x,2.0)+pow(y,2.0),0.5);
alexc89 45:0db0fc9f77b1 94 }
alexc89 45:0db0fc9f77b1 95 }else{ //Land Phase
alexc89 46:4497e945de6b 96 //Uses SLIP Model to Control
alexc89 46:4497e945de6b 97 //SLIP(initLen);
alexc89 46:4497e945de6b 98 m1.setPos(-1);
alexc89 45:0db0fc9f77b1 99 if(!landDetection()){
alexc89 45:0db0fc9f77b1 100 //Lift Up going to Phase 0
alexc89 45:0db0fc9f77b1 101 Phase =0;
alexc89 45:0db0fc9f77b1 102 }
alexc89 45:0db0fc9f77b1 103
alexc89 45:0db0fc9f77b1 104 }
alexc89 45:0db0fc9f77b1 105 }
calamaridudeman 43:68faf056ed5c 106 wait(3);
calamaridudeman 43:68faf056ed5c 107
calamaridudeman 43:68faf056ed5c 108 /*while(!curve.isDone()){
calamaridudeman 43:68faf056ed5c 109 //kankan.testEncoders(pc);
calamaridudeman 43:68faf056ed5c 110 curve.incrementAlpha();
calamaridudeman 43:68faf056ed5c 111 Point end = curve.getPoint();
calamaridudeman 43:68faf056ed5c 112
calamaridudeman 43:68faf056ed5c 113 Joints motorset = invKinBody(end);
calamaridudeman 43:68faf056ed5c 114 //pc.printf("%f, %f, %f, %f\n", motorset.t1, motorset.t2, end.x, end.y);
calamaridudeman 43:68faf056ed5c 115
calamaridudeman 43:68faf056ed5c 116 m1.setPos(motorset.t1);
calamaridudeman 43:68faf056ed5c 117 m2.setPos(motorset.t2);
calamaridudeman 43:68faf056ed5c 118
calamaridudeman 43:68faf056ed5c 119 wait(.05);
calamaridudeman 43:68faf056ed5c 120 }
calamaridudeman 43:68faf056ed5c 121 */
calamaridudeman 23:112c0be5a7f3 122
calamaridudeman 43:68faf056ed5c 123 /*
calamaridudeman 43:68faf056ed5c 124 kankan.start();
calamaridudeman 43:68faf056ed5c 125 wait(1);
calamaridudeman 43:68faf056ed5c 126 //m1.setPos(-3.1415/6);
calamaridudeman 43:68faf056ed5c 127 //m2.setPos(3.1415/6);
calamaridudeman 43:68faf056ed5c 128 //m1.setTorque(2);
calamaridudeman 37:bf257a0154db 129
calamaridudeman 43:68faf056ed5c 130 //kankan.setPoint(Point(0,-.3,0));
calamaridudeman 43:68faf056ed5c 131 wait(5);
calamaridudeman 43:68faf056ed5c 132 */
calamaridudeman 43:68faf056ed5c 133 m1.stop();
calamaridudeman 43:68faf056ed5c 134 m2.stop();
calamaridudeman 43:68faf056ed5c 135
calamaridudeman 43:68faf056ed5c 136 /*Forward=1;
calamaridudeman 43:68faf056ed5c 137 Backward=0;
calamaridudeman 43:68faf056ed5c 138 pwmOut.period_us(500);
calamaridudeman 43:68faf056ed5c 139
calamaridudeman 43:68faf056ed5c 140
calamaridudeman 43:68faf056ed5c 141 pwmOut.write(.15);
calamaridudeman 43:68faf056ed5c 142
calamaridudeman 43:68faf056ed5c 143 wait(5);
calamaridudeman 43:68faf056ed5c 144 pwmOut.write(0);
calamaridudeman 43:68faf056ed5c 145
calamaridudeman 43:68faf056ed5c 146 */
calamaridudeman 37:bf257a0154db 147
calamaridudeman 37:bf257a0154db 148 //kankan.zero();
calamaridudeman 22:4d85d989af08 149 }