bio robot
Dependencies: MPU6050-DMP QEI_hw mbed-rpc mbed
Fork of MPU6050_Example by
Controls/Controls.h@17:8a0e647cf551, 2015-12-11 (annotated)
- Committer:
- amandaghassaei
- Date:
- Fri Dec 11 00:44:45 2015 +0000
- Revision:
- 17:8a0e647cf551
- Parent:
- 15:d88f10b3b5f8
- Child:
- 18:0cfe72d8a006
eod
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
amandaghassaei | 7:7efcd3bf3302 | 1 | #ifndef Controls_h |
amandaghassaei | 7:7efcd3bf3302 | 2 | #define Controls_h |
amandaghassaei | 7:7efcd3bf3302 | 3 | |
amandaghassaei | 8:1a3a69fecedf | 4 | #include <math.h> |
amandaghassaei | 7:7efcd3bf3302 | 5 | #include "Gains.h" |
amandaghassaei | 7:7efcd3bf3302 | 6 | #include "Target.h" |
amandaghassaei | 8:1a3a69fecedf | 7 | #include "MyMPU6050.h" |
amandaghassaei | 8:1a3a69fecedf | 8 | #include "Motor.h" |
amandaghassaei | 8:1a3a69fecedf | 9 | #include "Dynamics.h" |
amandaghassaei | 7:7efcd3bf3302 | 10 | |
amandaghassaei | 10:769cc457c3a4 | 11 | #define controlTimerPeriod 0.001 |
amandaghassaei | 10:769cc457c3a4 | 12 | |
amandaghassaei | 8:1a3a69fecedf | 13 | class Controls: public CommDelegate{ |
amandaghassaei | 7:7efcd3bf3302 | 14 | |
amandaghassaei | 7:7efcd3bf3302 | 15 | public: |
amandaghassaei | 7:7efcd3bf3302 | 16 | |
amandaghassaei | 9:1d9b24d7ac77 | 17 | Controls():myMPU6050_1(p28, p27, p18), myMPU6050_2(p9, p10, p11){//I2C_SDA, I2C_SCL, int_pin |
amandaghassaei | 10:769cc457c3a4 | 18 | } |
amandaghassaei | 8:1a3a69fecedf | 19 | |
amandaghassaei | 10:769cc457c3a4 | 20 | void setup(){ |
amandaghassaei | 9:1d9b24d7ac77 | 21 | setInverted(false); |
amandaghassaei | 8:1a3a69fecedf | 22 | |
amandaghassaei | 11:711d3c207e8c | 23 | float m1 = 0.93159230; |
amandaghassaei | 11:711d3c207e8c | 24 | float m2 = 0.45433433; |
amandaghassaei | 11:711d3c207e8c | 25 | float l1 = 0.275;//length of links |
amandaghassaei | 8:1a3a69fecedf | 26 | float l2 = l1; |
amandaghassaei | 11:711d3c207e8c | 27 | float I1 = 0.03736067; |
amandaghassaei | 11:711d3c207e8c | 28 | float I2 = 0.01778165; |
amandaghassaei | 11:711d3c207e8c | 29 | float c1 = l1-0.08567346;//location of center of mass along link |
amandaghassaei | 11:711d3c207e8c | 30 | float c2 = 0.17594269; |
amandaghassaei | 8:1a3a69fecedf | 31 | float g = 9.81; |
amandaghassaei | 8:1a3a69fecedf | 32 | float lattice_pitch = 0.35; |
amandaghassaei | 8:1a3a69fecedf | 33 | _parameters[0] = l1; |
amandaghassaei | 8:1a3a69fecedf | 34 | _parameters[1] = l2; |
amandaghassaei | 8:1a3a69fecedf | 35 | _parameters[2] = c1; |
amandaghassaei | 8:1a3a69fecedf | 36 | _parameters[3] = c2; |
amandaghassaei | 8:1a3a69fecedf | 37 | _parameters[4] = m1; |
amandaghassaei | 8:1a3a69fecedf | 38 | _parameters[5] = m2; |
amandaghassaei | 8:1a3a69fecedf | 39 | _parameters[6] = I1; |
amandaghassaei | 8:1a3a69fecedf | 40 | _parameters[7] = I2; |
amandaghassaei | 8:1a3a69fecedf | 41 | _parameters[8] = g; |
amandaghassaei | 8:1a3a69fecedf | 42 | _parameters[9] = lattice_pitch; |
amandaghassaei | 8:1a3a69fecedf | 43 | |
amandaghassaei | 11:711d3c207e8c | 44 | _manualTheta = 0; |
amandaghassaei | 12:49813131dd15 | 45 | |
amandaghassaei | 15:d88f10b3b5f8 | 46 | setTargetPosition(6);//only 4 and 6 for now |
amandaghassaei | 7:7efcd3bf3302 | 47 | } |
amandaghassaei | 9:1d9b24d7ac77 | 48 | |
amandaghassaei | 9:1d9b24d7ac77 | 49 | void setInverted(bool inverted){ |
amandaghassaei | 9:1d9b24d7ac77 | 50 | _inverted = inverted; |
amandaghassaei | 9:1d9b24d7ac77 | 51 | if (!_inverted) { |
amandaghassaei | 9:1d9b24d7ac77 | 52 | myMPU6050_2.disable(); |
amandaghassaei | 9:1d9b24d7ac77 | 53 | myMPU6050_1.enable(); |
amandaghassaei | 9:1d9b24d7ac77 | 54 | } else { |
amandaghassaei | 9:1d9b24d7ac77 | 55 | myMPU6050_1.disable(); |
amandaghassaei | 9:1d9b24d7ac77 | 56 | myMPU6050_2.enable(); |
amandaghassaei | 9:1d9b24d7ac77 | 57 | } |
amandaghassaei | 9:1d9b24d7ac77 | 58 | } |
amandaghassaei | 9:1d9b24d7ac77 | 59 | |
amandaghassaei | 9:1d9b24d7ac77 | 60 | void setPC(Serial *pc){ |
amandaghassaei | 9:1d9b24d7ac77 | 61 | _pc = pc; |
amandaghassaei | 9:1d9b24d7ac77 | 62 | motor.setPC(pc); |
amandaghassaei | 9:1d9b24d7ac77 | 63 | gains.setPC(pc); |
amandaghassaei | 9:1d9b24d7ac77 | 64 | target.setPC(pc); |
amandaghassaei | 9:1d9b24d7ac77 | 65 | myMPU6050_1.setPC(pc); |
amandaghassaei | 9:1d9b24d7ac77 | 66 | myMPU6050_2.setPC(pc); |
amandaghassaei | 9:1d9b24d7ac77 | 67 | } |
amandaghassaei | 7:7efcd3bf3302 | 68 | |
amandaghassaei | 7:7efcd3bf3302 | 69 | Gains gains; |
amandaghassaei | 9:1d9b24d7ac77 | 70 | void setSwingUpK(float k){ |
amandaghassaei | 9:1d9b24d7ac77 | 71 | gains.setSwingUpK(k); |
amandaghassaei | 9:1d9b24d7ac77 | 72 | }; |
amandaghassaei | 9:1d9b24d7ac77 | 73 | void setSwingUpD(float d){ |
amandaghassaei | 9:1d9b24d7ac77 | 74 | gains.setSwingUpD(d); |
amandaghassaei | 9:1d9b24d7ac77 | 75 | }; |
amandaghassaei | 14:d620415259b1 | 76 | void setDesiredThetaP(float p){ |
amandaghassaei | 14:d620415259b1 | 77 | gains.setDesiredThetaP(p); |
amandaghassaei | 9:1d9b24d7ac77 | 78 | }; |
amandaghassaei | 9:1d9b24d7ac77 | 79 | float getSwingUpK(){ |
amandaghassaei | 9:1d9b24d7ac77 | 80 | return gains.getSwingUpK(); |
amandaghassaei | 9:1d9b24d7ac77 | 81 | }; |
amandaghassaei | 9:1d9b24d7ac77 | 82 | float getSwingUpD(){ |
amandaghassaei | 9:1d9b24d7ac77 | 83 | return gains.getSwingUpD(); |
amandaghassaei | 9:1d9b24d7ac77 | 84 | }; |
amandaghassaei | 14:d620415259b1 | 85 | float getDesiredThetaP(){ |
amandaghassaei | 14:d620415259b1 | 86 | return gains.getDesiredThetaP(); |
amandaghassaei | 9:1d9b24d7ac77 | 87 | }; |
amandaghassaei | 9:1d9b24d7ac77 | 88 | |
amandaghassaei | 7:7efcd3bf3302 | 89 | Target target; |
amandaghassaei | 9:1d9b24d7ac77 | 90 | void setTargetPosition(int position){ |
amandaghassaei | 12:49813131dd15 | 91 | target.setPosition(position, _parameters); |
amandaghassaei | 9:1d9b24d7ac77 | 92 | }; |
amandaghassaei | 9:1d9b24d7ac77 | 93 | int getTargetPosition(){ |
amandaghassaei | 9:1d9b24d7ac77 | 94 | return target.getPosition(); |
amandaghassaei | 9:1d9b24d7ac77 | 95 | }; |
amandaghassaei | 8:1a3a69fecedf | 96 | |
amandaghassaei | 17:8a0e647cf551 | 97 | void setTargetingK(float k){ |
amandaghassaei | 17:8a0e647cf551 | 98 | gains.setTargetingK(k); |
amandaghassaei | 17:8a0e647cf551 | 99 | }; |
amandaghassaei | 17:8a0e647cf551 | 100 | void setTargetingD(float d){ |
amandaghassaei | 17:8a0e647cf551 | 101 | gains.setTargetingD(d); |
amandaghassaei | 17:8a0e647cf551 | 102 | }; |
amandaghassaei | 17:8a0e647cf551 | 103 | float getTargetingK(){ |
amandaghassaei | 17:8a0e647cf551 | 104 | return gains.getTargetingK(); |
amandaghassaei | 17:8a0e647cf551 | 105 | }; |
amandaghassaei | 17:8a0e647cf551 | 106 | float getTargetingD(){ |
amandaghassaei | 17:8a0e647cf551 | 107 | return gains.getTargetingD(); |
amandaghassaei | 17:8a0e647cf551 | 108 | }; |
amandaghassaei | 17:8a0e647cf551 | 109 | |
amandaghassaei | 8:1a3a69fecedf | 110 | Motor motor; |
amandaghassaei | 11:711d3c207e8c | 111 | void setTheta(float theta){ |
amandaghassaei | 11:711d3c207e8c | 112 | _manualTheta = theta; |
amandaghassaei | 9:1d9b24d7ac77 | 113 | }; |
amandaghassaei | 10:769cc457c3a4 | 114 | |
amandaghassaei | 11:711d3c207e8c | 115 | float _manualTheta; |
amandaghassaei | 7:7efcd3bf3302 | 116 | |
amandaghassaei | 8:1a3a69fecedf | 117 | //imu |
amandaghassaei | 8:1a3a69fecedf | 118 | MyMPU6050 myMPU6050_1; |
amandaghassaei | 8:1a3a69fecedf | 119 | MyMPU6050 myMPU6050_2; |
amandaghassaei | 10:769cc457c3a4 | 120 | |
amandaghassaei | 10:769cc457c3a4 | 121 | void updateIMUS(){ |
amandaghassaei | 10:769cc457c3a4 | 122 | getActiveIMU()->loop(); |
amandaghassaei | 11:711d3c207e8c | 123 | updateThetas(); |
amandaghassaei | 10:769cc457c3a4 | 124 | } |
amandaghassaei | 7:7efcd3bf3302 | 125 | |
amandaghassaei | 7:7efcd3bf3302 | 126 | void loop(){ |
amandaghassaei | 10:769cc457c3a4 | 127 | |
amandaghassaei | 13:64d337c5114e | 128 | getActiveIMU()->disableInterrupt(); |
amandaghassaei | 13:64d337c5114e | 129 | |
amandaghassaei | 12:49813131dd15 | 130 | float tau = calcTau(_z, _parameters, &gains, &target, _pc); |
amandaghassaei | 9:1d9b24d7ac77 | 131 | motor.setTorque(tau); |
amandaghassaei | 10:769cc457c3a4 | 132 | |
amandaghassaei | 13:64d337c5114e | 133 | getActiveIMU()->enableInterrupt(); |
amandaghassaei | 10:769cc457c3a4 | 134 | } |
amandaghassaei | 10:769cc457c3a4 | 135 | |
amandaghassaei | 10:769cc457c3a4 | 136 | MyMPU6050* getActiveIMU(){ |
amandaghassaei | 10:769cc457c3a4 | 137 | if (_inverted) return &myMPU6050_2; |
amandaghassaei | 10:769cc457c3a4 | 138 | return &myMPU6050_1; |
amandaghassaei | 10:769cc457c3a4 | 139 | } |
amandaghassaei | 10:769cc457c3a4 | 140 | |
amandaghassaei | 8:1a3a69fecedf | 141 | float getTheta1(){ |
amandaghassaei | 8:1a3a69fecedf | 142 | return _z[0]; |
amandaghassaei | 7:7efcd3bf3302 | 143 | } |
amandaghassaei | 8:1a3a69fecedf | 144 | float getDTheta1(){ |
amandaghassaei | 8:1a3a69fecedf | 145 | return _z[2]; |
amandaghassaei | 8:1a3a69fecedf | 146 | } |
amandaghassaei | 8:1a3a69fecedf | 147 | float getTheta2(){ |
amandaghassaei | 8:1a3a69fecedf | 148 | return _z[1]; |
amandaghassaei | 8:1a3a69fecedf | 149 | } |
amandaghassaei | 8:1a3a69fecedf | 150 | float getDTheta2(){ |
amandaghassaei | 8:1a3a69fecedf | 151 | return _z[3]; |
amandaghassaei | 8:1a3a69fecedf | 152 | } |
amandaghassaei | 8:1a3a69fecedf | 153 | |
amandaghassaei | 8:1a3a69fecedf | 154 | |
amandaghassaei | 7:7efcd3bf3302 | 155 | |
amandaghassaei | 7:7efcd3bf3302 | 156 | private: |
amandaghassaei | 7:7efcd3bf3302 | 157 | |
amandaghassaei | 9:1d9b24d7ac77 | 158 | Serial *_pc; |
amandaghassaei | 9:1d9b24d7ac77 | 159 | |
amandaghassaei | 8:1a3a69fecedf | 160 | float _parameters[10]; |
amandaghassaei | 10:769cc457c3a4 | 161 | volatile float _z[4];//theta1, theta2, dtheta2, dtheta2 |
amandaghassaei | 14:d620415259b1 | 162 | |
amandaghassaei | 8:1a3a69fecedf | 163 | void updateThetas(){ |
amandaghassaei | 8:1a3a69fecedf | 164 | _z[0] = _getTheta1(); |
amandaghassaei | 8:1a3a69fecedf | 165 | _z[2] = _getDTheta1(); |
amandaghassaei | 8:1a3a69fecedf | 166 | _z[1] = _getTheta2(); |
amandaghassaei | 8:1a3a69fecedf | 167 | _z[3] = _getDTheta2(); |
amandaghassaei | 8:1a3a69fecedf | 168 | |
amandaghassaei | 8:1a3a69fecedf | 169 | } |
amandaghassaei | 8:1a3a69fecedf | 170 | float _getTheta1(){ |
amandaghassaei | 10:769cc457c3a4 | 171 | return getActiveIMU()->getTheta(); |
amandaghassaei | 8:1a3a69fecedf | 172 | } |
amandaghassaei | 8:1a3a69fecedf | 173 | float _getDTheta1(){ |
amandaghassaei | 10:769cc457c3a4 | 174 | return getActiveIMU()->getDTheta(); |
amandaghassaei | 8:1a3a69fecedf | 175 | } |
amandaghassaei | 8:1a3a69fecedf | 176 | float _getTheta2(){ |
amandaghassaei | 8:1a3a69fecedf | 177 | if (_inverted) return -motor.getTheta(); |
amandaghassaei | 8:1a3a69fecedf | 178 | return motor.getTheta(); |
amandaghassaei | 8:1a3a69fecedf | 179 | } |
amandaghassaei | 8:1a3a69fecedf | 180 | float _getDTheta2(){ |
amandaghassaei | 8:1a3a69fecedf | 181 | if (_inverted) return -motor.getDTheta(); |
amandaghassaei | 8:1a3a69fecedf | 182 | return motor.getDTheta(); |
amandaghassaei | 8:1a3a69fecedf | 183 | } |
amandaghassaei | 8:1a3a69fecedf | 184 | |
amandaghassaei | 8:1a3a69fecedf | 185 | bool _inverted; |
amandaghassaei | 7:7efcd3bf3302 | 186 | |
amandaghassaei | 7:7efcd3bf3302 | 187 | }; |
amandaghassaei | 7:7efcd3bf3302 | 188 | |
amandaghassaei | 7:7efcd3bf3302 | 189 | #endif |