![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
bio robot
Dependencies: MPU6050-DMP QEI_hw mbed-rpc mbed
Fork of MPU6050_Example by
Controls/Controls.h@14:d620415259b1, 2015-12-09 (annotated)
- Committer:
- amandaghassaei
- Date:
- Wed Dec 09 03:40:38 2015 +0000
- Revision:
- 14:d620415259b1
- Parent:
- 13:64d337c5114e
- Child:
- 15:d88f10b3b5f8
renaming gains variable
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 | 12:49813131dd15 | 46 | setTargetPosition(6); |
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 | 8:1a3a69fecedf | 97 | Motor motor; |
amandaghassaei | 11:711d3c207e8c | 98 | void setTheta(float theta){ |
amandaghassaei | 11:711d3c207e8c | 99 | _manualTheta = theta; |
amandaghassaei | 9:1d9b24d7ac77 | 100 | }; |
amandaghassaei | 10:769cc457c3a4 | 101 | |
amandaghassaei | 11:711d3c207e8c | 102 | float _manualTheta; |
amandaghassaei | 7:7efcd3bf3302 | 103 | |
amandaghassaei | 8:1a3a69fecedf | 104 | //imu |
amandaghassaei | 8:1a3a69fecedf | 105 | MyMPU6050 myMPU6050_1; |
amandaghassaei | 8:1a3a69fecedf | 106 | MyMPU6050 myMPU6050_2; |
amandaghassaei | 10:769cc457c3a4 | 107 | |
amandaghassaei | 10:769cc457c3a4 | 108 | void updateIMUS(){ |
amandaghassaei | 10:769cc457c3a4 | 109 | getActiveIMU()->loop(); |
amandaghassaei | 11:711d3c207e8c | 110 | updateThetas(); |
amandaghassaei | 10:769cc457c3a4 | 111 | } |
amandaghassaei | 7:7efcd3bf3302 | 112 | |
amandaghassaei | 7:7efcd3bf3302 | 113 | void loop(){ |
amandaghassaei | 10:769cc457c3a4 | 114 | |
amandaghassaei | 13:64d337c5114e | 115 | getActiveIMU()->disableInterrupt(); |
amandaghassaei | 13:64d337c5114e | 116 | |
amandaghassaei | 12:49813131dd15 | 117 | float tau = calcTau(_z, _parameters, &gains, &target, _pc); |
amandaghassaei | 9:1d9b24d7ac77 | 118 | motor.setTorque(tau); |
amandaghassaei | 10:769cc457c3a4 | 119 | |
amandaghassaei | 13:64d337c5114e | 120 | getActiveIMU()->enableInterrupt(); |
amandaghassaei | 10:769cc457c3a4 | 121 | } |
amandaghassaei | 10:769cc457c3a4 | 122 | |
amandaghassaei | 10:769cc457c3a4 | 123 | MyMPU6050* getActiveIMU(){ |
amandaghassaei | 10:769cc457c3a4 | 124 | if (_inverted) return &myMPU6050_2; |
amandaghassaei | 10:769cc457c3a4 | 125 | return &myMPU6050_1; |
amandaghassaei | 10:769cc457c3a4 | 126 | } |
amandaghassaei | 10:769cc457c3a4 | 127 | |
amandaghassaei | 8:1a3a69fecedf | 128 | float getTheta1(){ |
amandaghassaei | 8:1a3a69fecedf | 129 | return _z[0]; |
amandaghassaei | 7:7efcd3bf3302 | 130 | } |
amandaghassaei | 8:1a3a69fecedf | 131 | float getDTheta1(){ |
amandaghassaei | 8:1a3a69fecedf | 132 | return _z[2]; |
amandaghassaei | 8:1a3a69fecedf | 133 | } |
amandaghassaei | 8:1a3a69fecedf | 134 | float getTheta2(){ |
amandaghassaei | 8:1a3a69fecedf | 135 | return _z[1]; |
amandaghassaei | 8:1a3a69fecedf | 136 | } |
amandaghassaei | 8:1a3a69fecedf | 137 | float getDTheta2(){ |
amandaghassaei | 8:1a3a69fecedf | 138 | return _z[3]; |
amandaghassaei | 8:1a3a69fecedf | 139 | } |
amandaghassaei | 8:1a3a69fecedf | 140 | |
amandaghassaei | 8:1a3a69fecedf | 141 | |
amandaghassaei | 7:7efcd3bf3302 | 142 | |
amandaghassaei | 7:7efcd3bf3302 | 143 | private: |
amandaghassaei | 7:7efcd3bf3302 | 144 | |
amandaghassaei | 9:1d9b24d7ac77 | 145 | Serial *_pc; |
amandaghassaei | 9:1d9b24d7ac77 | 146 | |
amandaghassaei | 8:1a3a69fecedf | 147 | float _parameters[10]; |
amandaghassaei | 10:769cc457c3a4 | 148 | volatile float _z[4];//theta1, theta2, dtheta2, dtheta2 |
amandaghassaei | 14:d620415259b1 | 149 | |
amandaghassaei | 8:1a3a69fecedf | 150 | void updateThetas(){ |
amandaghassaei | 8:1a3a69fecedf | 151 | _z[0] = _getTheta1(); |
amandaghassaei | 8:1a3a69fecedf | 152 | _z[2] = _getDTheta1(); |
amandaghassaei | 8:1a3a69fecedf | 153 | _z[1] = _getTheta2(); |
amandaghassaei | 8:1a3a69fecedf | 154 | _z[3] = _getDTheta2(); |
amandaghassaei | 8:1a3a69fecedf | 155 | |
amandaghassaei | 8:1a3a69fecedf | 156 | } |
amandaghassaei | 8:1a3a69fecedf | 157 | float _getTheta1(){ |
amandaghassaei | 10:769cc457c3a4 | 158 | return getActiveIMU()->getTheta(); |
amandaghassaei | 8:1a3a69fecedf | 159 | } |
amandaghassaei | 8:1a3a69fecedf | 160 | float _getDTheta1(){ |
amandaghassaei | 10:769cc457c3a4 | 161 | return getActiveIMU()->getDTheta(); |
amandaghassaei | 8:1a3a69fecedf | 162 | } |
amandaghassaei | 8:1a3a69fecedf | 163 | float _getTheta2(){ |
amandaghassaei | 8:1a3a69fecedf | 164 | if (_inverted) return -motor.getTheta(); |
amandaghassaei | 8:1a3a69fecedf | 165 | return motor.getTheta(); |
amandaghassaei | 8:1a3a69fecedf | 166 | } |
amandaghassaei | 8:1a3a69fecedf | 167 | float _getDTheta2(){ |
amandaghassaei | 8:1a3a69fecedf | 168 | if (_inverted) return -motor.getDTheta(); |
amandaghassaei | 8:1a3a69fecedf | 169 | return motor.getDTheta(); |
amandaghassaei | 8:1a3a69fecedf | 170 | } |
amandaghassaei | 8:1a3a69fecedf | 171 | |
amandaghassaei | 8:1a3a69fecedf | 172 | bool _inverted; |
amandaghassaei | 7:7efcd3bf3302 | 173 | |
amandaghassaei | 7:7efcd3bf3302 | 174 | }; |
amandaghassaei | 7:7efcd3bf3302 | 175 | |
amandaghassaei | 7:7efcd3bf3302 | 176 | #endif |