bio robot

Dependencies:   MPU6050-DMP QEI_hw mbed-rpc mbed

Fork of MPU6050_Example by Shundo Kishi

Committer:
amandaghassaei
Date:
Wed Dec 09 03:08:30 2015 +0000
Revision:
12:49813131dd15
Parent:
11:711d3c207e8c
Child:
13:64d337c5114e
simple targeting;

Who changed what in which revision?

UserRevisionLine numberNew 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 10:769cc457c3a4 44 _lastTorque = motor.getTorque();
amandaghassaei 11:711d3c207e8c 45 _manualTheta = 0;
amandaghassaei 12:49813131dd15 46
amandaghassaei 12:49813131dd15 47 setTargetPosition(6);
amandaghassaei 7:7efcd3bf3302 48 }
amandaghassaei 9:1d9b24d7ac77 49
amandaghassaei 9:1d9b24d7ac77 50 void setInverted(bool inverted){
amandaghassaei 9:1d9b24d7ac77 51 _inverted = inverted;
amandaghassaei 9:1d9b24d7ac77 52 if (!_inverted) {
amandaghassaei 9:1d9b24d7ac77 53 myMPU6050_2.disable();
amandaghassaei 9:1d9b24d7ac77 54 myMPU6050_1.enable();
amandaghassaei 9:1d9b24d7ac77 55 } else {
amandaghassaei 9:1d9b24d7ac77 56 myMPU6050_1.disable();
amandaghassaei 9:1d9b24d7ac77 57 myMPU6050_2.enable();
amandaghassaei 9:1d9b24d7ac77 58 }
amandaghassaei 9:1d9b24d7ac77 59 }
amandaghassaei 9:1d9b24d7ac77 60
amandaghassaei 9:1d9b24d7ac77 61 void setPC(Serial *pc){
amandaghassaei 9:1d9b24d7ac77 62 _pc = pc;
amandaghassaei 9:1d9b24d7ac77 63 motor.setPC(pc);
amandaghassaei 9:1d9b24d7ac77 64 gains.setPC(pc);
amandaghassaei 9:1d9b24d7ac77 65 target.setPC(pc);
amandaghassaei 9:1d9b24d7ac77 66 myMPU6050_1.setPC(pc);
amandaghassaei 9:1d9b24d7ac77 67 myMPU6050_2.setPC(pc);
amandaghassaei 9:1d9b24d7ac77 68 }
amandaghassaei 7:7efcd3bf3302 69
amandaghassaei 7:7efcd3bf3302 70 Gains gains;
amandaghassaei 9:1d9b24d7ac77 71 void setSwingUpK(float k){
amandaghassaei 9:1d9b24d7ac77 72 gains.setSwingUpK(k);
amandaghassaei 9:1d9b24d7ac77 73 };
amandaghassaei 9:1d9b24d7ac77 74 void setSwingUpD(float d){
amandaghassaei 9:1d9b24d7ac77 75 gains.setSwingUpD(d);
amandaghassaei 9:1d9b24d7ac77 76 };
amandaghassaei 10:769cc457c3a4 77 void setCurrentP(float p){
amandaghassaei 10:769cc457c3a4 78 gains.setCurrentP(p);
amandaghassaei 9:1d9b24d7ac77 79 };
amandaghassaei 10:769cc457c3a4 80 void setCurrentD(float d){
amandaghassaei 10:769cc457c3a4 81 gains.setCurrentD(d);
amandaghassaei 9:1d9b24d7ac77 82 };
amandaghassaei 9:1d9b24d7ac77 83 float getSwingUpK(){
amandaghassaei 9:1d9b24d7ac77 84 return gains.getSwingUpK();
amandaghassaei 9:1d9b24d7ac77 85 };
amandaghassaei 9:1d9b24d7ac77 86 float getSwingUpD(){
amandaghassaei 9:1d9b24d7ac77 87 return gains.getSwingUpD();
amandaghassaei 9:1d9b24d7ac77 88 };
amandaghassaei 10:769cc457c3a4 89 float getCurrentP(){
amandaghassaei 10:769cc457c3a4 90 return gains.getCurrentP();
amandaghassaei 9:1d9b24d7ac77 91 };
amandaghassaei 10:769cc457c3a4 92 float getCurrentD(){
amandaghassaei 10:769cc457c3a4 93 return gains.getCurrentD();
amandaghassaei 9:1d9b24d7ac77 94 };
amandaghassaei 9:1d9b24d7ac77 95
amandaghassaei 7:7efcd3bf3302 96 Target target;
amandaghassaei 9:1d9b24d7ac77 97 void setTargetPosition(int position){
amandaghassaei 12:49813131dd15 98 target.setPosition(position, _parameters);
amandaghassaei 9:1d9b24d7ac77 99 };
amandaghassaei 9:1d9b24d7ac77 100 int getTargetPosition(){
amandaghassaei 9:1d9b24d7ac77 101 return target.getPosition();
amandaghassaei 9:1d9b24d7ac77 102 };
amandaghassaei 8:1a3a69fecedf 103
amandaghassaei 8:1a3a69fecedf 104 Motor motor;
amandaghassaei 11:711d3c207e8c 105 void setTheta(float theta){
amandaghassaei 11:711d3c207e8c 106 _manualTheta = theta;
amandaghassaei 9:1d9b24d7ac77 107 };
amandaghassaei 10:769cc457c3a4 108
amandaghassaei 11:711d3c207e8c 109 float _manualTheta;
amandaghassaei 7:7efcd3bf3302 110
amandaghassaei 8:1a3a69fecedf 111 //imu
amandaghassaei 8:1a3a69fecedf 112 MyMPU6050 myMPU6050_1;
amandaghassaei 8:1a3a69fecedf 113 MyMPU6050 myMPU6050_2;
amandaghassaei 10:769cc457c3a4 114
amandaghassaei 10:769cc457c3a4 115 void updateIMUS(){
amandaghassaei 10:769cc457c3a4 116 getActiveIMU()->loop();
amandaghassaei 11:711d3c207e8c 117 updateThetas();
amandaghassaei 10:769cc457c3a4 118 }
amandaghassaei 7:7efcd3bf3302 119
amandaghassaei 7:7efcd3bf3302 120 void loop(){
amandaghassaei 10:769cc457c3a4 121
amandaghassaei 10:769cc457c3a4 122 // getActiveIMU().disableInterrupt();
amandaghassaei 11:711d3c207e8c 123 // float z[4] = {1,2,3,4};
amandaghassaei 11:711d3c207e8c 124 // _pc->printf("%f\n", calcTau(z, _parameters, &gains, _pc));
amandaghassaei 8:1a3a69fecedf 125
amandaghassaei 12:49813131dd15 126 float tau = calcTau(_z, _parameters, &gains, &target, _pc);
amandaghassaei 10:769cc457c3a4 127 // float tau = getTheta1();
amandaghassaei 10:769cc457c3a4 128
amandaghassaei 9:1d9b24d7ac77 129 motor.setTorque(tau);
amandaghassaei 10:769cc457c3a4 130
amandaghassaei 10:769cc457c3a4 131 // getActiveIMU().enableInterrupt();
amandaghassaei 10:769cc457c3a4 132 }
amandaghassaei 10:769cc457c3a4 133
amandaghassaei 10:769cc457c3a4 134 MyMPU6050* getActiveIMU(){
amandaghassaei 10:769cc457c3a4 135 if (_inverted) return &myMPU6050_2;
amandaghassaei 10:769cc457c3a4 136 return &myMPU6050_1;
amandaghassaei 10:769cc457c3a4 137 }
amandaghassaei 10:769cc457c3a4 138
amandaghassaei 10:769cc457c3a4 139 float pdTorque(float desiredTorque, float deltaT){
amandaghassaei 10:769cc457c3a4 140 float torque = motor.getTorque();
amandaghassaei 10:769cc457c3a4 141 float newTorque = gains.getCurrentP()*(desiredTorque-torque) + gains.getCurrentD()*(torque-_lastTorque)/deltaT;
amandaghassaei 10:769cc457c3a4 142 _lastTorque = torque;//update _lastTorque
amandaghassaei 10:769cc457c3a4 143 return newTorque;
amandaghassaei 8:1a3a69fecedf 144 }
amandaghassaei 8:1a3a69fecedf 145
amandaghassaei 8:1a3a69fecedf 146 float getTheta1(){
amandaghassaei 8:1a3a69fecedf 147 return _z[0];
amandaghassaei 7:7efcd3bf3302 148 }
amandaghassaei 8:1a3a69fecedf 149 float getDTheta1(){
amandaghassaei 8:1a3a69fecedf 150 return _z[2];
amandaghassaei 8:1a3a69fecedf 151 }
amandaghassaei 8:1a3a69fecedf 152 float getTheta2(){
amandaghassaei 8:1a3a69fecedf 153 return _z[1];
amandaghassaei 8:1a3a69fecedf 154 }
amandaghassaei 8:1a3a69fecedf 155 float getDTheta2(){
amandaghassaei 8:1a3a69fecedf 156 return _z[3];
amandaghassaei 8:1a3a69fecedf 157 }
amandaghassaei 8:1a3a69fecedf 158
amandaghassaei 8:1a3a69fecedf 159
amandaghassaei 7:7efcd3bf3302 160
amandaghassaei 7:7efcd3bf3302 161 private:
amandaghassaei 7:7efcd3bf3302 162
amandaghassaei 9:1d9b24d7ac77 163 Serial *_pc;
amandaghassaei 9:1d9b24d7ac77 164
amandaghassaei 8:1a3a69fecedf 165 float _parameters[10];
amandaghassaei 10:769cc457c3a4 166 volatile float _z[4];//theta1, theta2, dtheta2, dtheta2
amandaghassaei 10:769cc457c3a4 167
amandaghassaei 10:769cc457c3a4 168 float _lastTorque;
amandaghassaei 8:1a3a69fecedf 169
amandaghassaei 8:1a3a69fecedf 170 void updateThetas(){
amandaghassaei 8:1a3a69fecedf 171 _z[0] = _getTheta1();
amandaghassaei 8:1a3a69fecedf 172 _z[2] = _getDTheta1();
amandaghassaei 8:1a3a69fecedf 173 _z[1] = _getTheta2();
amandaghassaei 8:1a3a69fecedf 174 _z[3] = _getDTheta2();
amandaghassaei 8:1a3a69fecedf 175
amandaghassaei 8:1a3a69fecedf 176 }
amandaghassaei 8:1a3a69fecedf 177 float _getTheta1(){
amandaghassaei 10:769cc457c3a4 178 return getActiveIMU()->getTheta();
amandaghassaei 8:1a3a69fecedf 179 }
amandaghassaei 8:1a3a69fecedf 180 float _getDTheta1(){
amandaghassaei 10:769cc457c3a4 181 return getActiveIMU()->getDTheta();
amandaghassaei 8:1a3a69fecedf 182 }
amandaghassaei 8:1a3a69fecedf 183 float _getTheta2(){
amandaghassaei 8:1a3a69fecedf 184 if (_inverted) return -motor.getTheta();
amandaghassaei 8:1a3a69fecedf 185 return motor.getTheta();
amandaghassaei 8:1a3a69fecedf 186 }
amandaghassaei 8:1a3a69fecedf 187 float _getDTheta2(){
amandaghassaei 8:1a3a69fecedf 188 if (_inverted) return -motor.getDTheta();
amandaghassaei 8:1a3a69fecedf 189 return motor.getDTheta();
amandaghassaei 8:1a3a69fecedf 190 }
amandaghassaei 8:1a3a69fecedf 191
amandaghassaei 8:1a3a69fecedf 192 bool _inverted;
amandaghassaei 7:7efcd3bf3302 193
amandaghassaei 7:7efcd3bf3302 194 };
amandaghassaei 7:7efcd3bf3302 195
amandaghassaei 7:7efcd3bf3302 196 #endif