for hige

Fork of YNU_MPU_1 by yal kaiyo

Committer:
khoshino
Date:
Tue Jan 08 12:22:43 2013 +0000
Revision:
4:037fab837823
Parent:
3:17236ad0ff1e
Child:
5:192835ac6106
add vertical guidance

Who changed what in which revision?

UserRevisionLine numberNew contents of line
higedura 0:9aa9be24636a 1 #include "mbed.h"
higedura 0:9aa9be24636a 2 #include "SDFileSystem.h"
higedura 0:9aa9be24636a 3
higedura 0:9aa9be24636a 4 #define pi 3.1416
higedura 0:9aa9be24636a 5 #define data 6
higedura 0:9aa9be24636a 6
khoshino 2:30f96c159d9c 7 #define zer 0.0
khoshino 2:30f96c159d9c 8 #define hal 0.5
khoshino 2:30f96c159d9c 9 #define one 1.0
khoshino 2:30f96c159d9c 10 #define dtr 0.01745
khoshino 2:30f96c159d9c 11 #define rtd 57.3
khoshino 2:30f96c159d9c 12 //parameter//
khoshino 2:30f96c159d9c 13 #define dt1 0.2 // 1/N
khoshino 2:30f96c159d9c 14 #define hdt 0.1 // 1/2N
khoshino 2:30f96c159d9c 15 #define md 6 //kizami N
khoshino 2:30f96c159d9c 16 #define mt 7 //md+1
khoshino 2:30f96c159d9c 17 #define mx 4
khoshino 2:30f96c159d9c 18 #define mv 8
khoshino 2:30f96c159d9c 19 #define mh 56 //mt*mv
khoshino 2:30f96c159d9c 20
higedura 0:9aa9be24636a 21 Serial pc(USBTX, USBRX);
higedura 0:9aa9be24636a 22 DigitalOut led1(LED1);
higedura 0:9aa9be24636a 23 DigitalOut led2(LED2);
higedura 0:9aa9be24636a 24 DigitalOut led3(LED3);
higedura 0:9aa9be24636a 25 DigitalOut led4(LED4);
higedura 0:9aa9be24636a 26
higedura 0:9aa9be24636a 27 SDFileSystem sd(p5, p6, p7, p8, "sd");
higedura 0:9aa9be24636a 28 Serial mavc(p9, p10);
higedura 0:9aa9be24636a 29 DigitalIn stop_sd(p11);
higedura 0:9aa9be24636a 30
higedura 0:9aa9be24636a 31 int main() {
higedura 1:ca296cfa603b 32
higedura 1:ca296cfa603b 33 // ******************** TORATANI ********************
higedura 1:ca296cfa603b 34 //pc.baud(921600);
higedura 0:9aa9be24636a 35 mavc.baud(115200);
higedura 0:9aa9be24636a 36
khoshino 4:037fab837823 37 int flag = 0;
khoshino 4:037fab837823 38
higedura 0:9aa9be24636a 39 char buf_char;
higedura 0:9aa9be24636a 40 unsigned int buf_hex[30] = {0};
higedura 0:9aa9be24636a 41 unsigned int buf_dec[10] = {0};
higedura 0:9aa9be24636a 42
higedura 0:9aa9be24636a 43 // rx
higedura 0:9aa9be24636a 44 double acc[3] = {0};
higedura 0:9aa9be24636a 45 double gyro[3] = {0};
higedura 0:9aa9be24636a 46 double azi;
higedura 0:9aa9be24636a 47 double alt;
higedura 0:9aa9be24636a 48 double GPS[2] = {0};
higedura 0:9aa9be24636a 49
higedura 0:9aa9be24636a 50 // tx
higedura 0:9aa9be24636a 51 double com[3] = {0};
higedura 0:9aa9be24636a 52 char H;
higedura 0:9aa9be24636a 53 char D[data];
higedura 0:9aa9be24636a 54 char test[9];
higedura 0:9aa9be24636a 55 H = 0x02; // header of dateset2
higedura 0:9aa9be24636a 56 D[0] = 0xff; D[1] = 0xff; // p_com
higedura 0:9aa9be24636a 57 D[2] = 0x7f; D[3] = 0xff; // q_com
higedura 0:9aa9be24636a 58 D[4] = 0x00; D[5] = 0x00; // r_com
higedura 1:ca296cfa603b 59 // ******************** TORATANI ********************
higedura 0:9aa9be24636a 60
khoshino 2:30f96c159d9c 61 // ******************** HOSHINO *********************
khoshino 2:30f96c159d9c 62 float time = 0; //time
khoshino 4:037fab837823 63 float xis = 0;
khoshino 4:037fab837823 64 float ets = 0;
khoshino 2:30f96c159d9c 65 float psis = 0;
khoshino 2:30f96c159d9c 66 float x0 = 0;
khoshino 2:30f96c159d9c 67 float y0 = 0;
khoshino 4:037fab837823 68 float drf = 800; //syuutandaunrenji
khoshino 4:037fab837823 69 float ht0 = 200;
khoshino 4:037fab837823 70 float htf = 10;
khoshino 2:30f96c159d9c 71 float gm0 = 20*pi/180;
khoshino 2:30f96c159d9c 72 float gmf = 5*pi/180;
khoshino 4:037fab837823 73 float b0 = ht0;
khoshino 2:30f96c159d9c 74 float b1 = tan(gm0);
khoshino 4:037fab837823 75 float b2 = (3*(htf-ht0)-drf*(2*tan(gm0)+tan(gmf)))/(drf*drf);
khoshino 4:037fab837823 76 float b3 = -(2*(htf-ht0)-drf*(tan(gm0)+tan(gmf)))/(drf*drf*drf);
khoshino 4:037fab837823 77 float htc = 0;
khoshino 4:037fab837823 78 float x = 0; //down range
khoshino 2:30f96c159d9c 79 float ua = 10; //initial horizontal velosity
khoshino 4:037fab837823 80 float xis1 = 0;
khoshino 4:037fab837823 81 float ets1 = 0;
khoshino 4:037fab837823 82 float xir = 0;
khoshino 4:037fab837823 83 float etr = 0;
khoshino 2:30f96c159d9c 84 float drc = 0;
khoshino 2:30f96c159d9c 85 float dr = 0;
khoshino 4:037fab837823 86 float dt = 0.1;
khoshino 4:037fab837823 87 float psr = 0;
khoshino 4:037fab837823 88 float htr = 0;
khoshino 4:037fab837823 89 float avp = 0;
khoshino 4:037fab837823 90 float avq = 0;
khoshino 4:037fab837823 91 float avr = 0;
khoshino 2:30f96c159d9c 92 float s = 0;
khoshino 2:30f96c159d9c 93
khoshino 2:30f96c159d9c 94 //homotopy
khoshino 2:30f96c159d9c 95 float rps0 = 90.0;
khoshino 4:037fab837823 96 float rxi0 = -600.0; //terminal xi
khoshino 4:037fab837823 97 float ret0 = -200.0; //terminal et
khoshino 2:30f96c159d9c 98 float ps0 = rps0*dtr;
khoshino 2:30f96c159d9c 99 float xi0 = rxi0/drf;
khoshino 2:30f96c159d9c 100 float et0 = ret0/drf;
khoshino 2:30f96c159d9c 101 float eps = 0.00000001;
khoshino 2:30f96c159d9c 102 float ueps = 0.000001;
khoshino 2:30f96c159d9c 103 float ier = 0.0;
khoshino 2:30f96c159d9c 104 float sum = 0.0;
khoshino 4:037fab837823 105 float ph0[mh] = {0};
khoshino 4:037fab837823 106 float phi[mh] = {0};
khoshino 4:037fab837823 107 float fn[mh] = {0};
khoshino 4:037fab837823 108 float wk[mh] = {0};
khoshino 2:30f96c159d9c 109 int iwk[mh] = {0};
khoshino 4:037fab837823 110 float dph1[mh] = {0};
khoshino 4:037fab837823 111 float dph2[mh] = {0};
khoshino 4:037fab837823 112 float dph3[mh] = {0};
khoshino 4:037fab837823 113 float dph4[mh] = {0};
khoshino 4:037fab837823 114 float fph[mh][mh] = {0};
khoshino 4:037fab837823 115 float fpi[mh][mh] = {0};
khoshino 4:037fab837823 116 float x1[4][mt] = {0};
khoshino 4:037fab837823 117 float rl[4][mt] = {0};
khoshino 2:30f96c159d9c 118 float qmax=0.0;
khoshino 2:30f96c159d9c 119 float rdet =1.0;
khoshino 2:30f96c159d9c 120 float idet =0.0;
khoshino 2:30f96c159d9c 121 float t = 0.0;
khoshino 4:037fab837823 122 float phf[mh]={0};
khoshino 2:30f96c159d9c 123 //********************* HOSHINO *********************
khoshino 2:30f96c159d9c 124
khoshino 4:037fab837823 125 //********************* OHTSU ***********************
khoshino 4:037fab837823 126 //********************* OHTSU ***********************
khoshino 4:037fab837823 127
higedura 0:9aa9be24636a 128 FILE *fp = fopen("/sd/sdtest.txt", "w");
higedura 0:9aa9be24636a 129 if(fp == NULL) {
higedura 0:9aa9be24636a 130 error("Could not open file for write\n");
higedura 0:9aa9be24636a 131 }
higedura 0:9aa9be24636a 132
khoshino 4:037fab837823 133 while(flag<120){ //flag tukuru // for 2 minuts
higedura 0:9aa9be24636a 134
higedura 0:9aa9be24636a 135 // ******************** sequence No.1 ********************
higedura 0:9aa9be24636a 136 buf_char = mavc.getc();
higedura 0:9aa9be24636a 137 buf_hex[0] = (unsigned int)buf_char;
higedura 0:9aa9be24636a 138 for(int i=0;i<29;i++){ buf_hex[29-i] = buf_hex[28-i]; }
higedura 0:9aa9be24636a 139 // !!! switch bun ni suru
higedura 0:9aa9be24636a 140 if(buf_hex[29]==0x40 && buf_hex[28]==0x43 && buf_hex[27]==0x4D && buf_hex[26]==0x0D){
higedura 0:9aa9be24636a 141 // ******************** sequence No.1 ********************
higedura 0:9aa9be24636a 142
higedura 0:9aa9be24636a 143 // ******************** sequence No.2 ********************
higedura 0:9aa9be24636a 144 // send to mavc
higedura 0:9aa9be24636a 145 mavc.putc(H);
higedura 0:9aa9be24636a 146 for(int i=0;i<data;i++){ mavc.putc(D[i]); }
higedura 0:9aa9be24636a 147 //for(int i=0;i<9;i++){ mavc.putc(test[i]); }
higedura 0:9aa9be24636a 148 // ******************** sequence No.2 ********************
higedura 0:9aa9be24636a 149
higedura 0:9aa9be24636a 150 // ******************** sequence No.3 ********************
higedura 0:9aa9be24636a 151 // buf | 25 | 24 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 |
higedura 0:9aa9be24636a 152 // |header| AccX | AccY | AccZ | GyroX | GyroY | GyroZ | Azimuth | Altitude| GPSX | GPSY |
higedura 0:9aa9be24636a 153 // | H | D[1] | D[2] | D[3] | D[4] | D[5] | D[6] | D[7] | D[8] | D[9] | D[10] |
higedura 0:9aa9be24636a 154 // transrating hex to dec
higedura 0:9aa9be24636a 155 for(int i=0;i<10;i++){ buf_dec[i] = buf_hex[24-i*2]*256+buf_hex[23-i*2]; }
higedura 0:9aa9be24636a 156 // wakariyasuku surutame bunnkatsu, honnrai ha matrix ga yoi
higedura 0:9aa9be24636a 157 // -> saisyuteki niha matrix ni simasu?
higedura 0:9aa9be24636a 158 for(int i=0;i<3;i++){ acc[i] = ((double)buf_dec[i]-32768)/65535*20*9.8; }
higedura 0:9aa9be24636a 159 for(int i=0;i<3;i++){ gyro[i] = ((double)buf_dec[i+3]-32768)/65535*600/180*pi; }
higedura 0:9aa9be24636a 160 azi = (double)buf_dec[6]/65535*2*pi;
higedura 0:9aa9be24636a 161 alt = ((double)buf_dec[7]-32768)*0.1;
higedura 0:9aa9be24636a 162 for(int i=0;i<2;i++){ GPS[i] = ((double)buf_dec[i+8]-32768)*0.1; }
higedura 0:9aa9be24636a 163 // ******************** sequence No.3 ********************
higedura 0:9aa9be24636a 164
khoshino 2:30f96c159d9c 165 // ******************** initialization *******************
khoshino 2:30f96c159d9c 166 if(t == 0){
khoshino 4:037fab837823 167 x0 = GPS[0];
khoshino 4:037fab837823 168 y0 = GPS[1];
khoshino 4:037fab837823 169 ps0 = azi;
khoshino 2:30f96c159d9c 170 }
khoshino 4:037fab837823 171 xis = GPS[0] - x0;
khoshino 4:037fab837823 172 ets = GPS[1] - y0;
khoshino 4:037fab837823 173 psr = azi - ps0;
khoshino 4:037fab837823 174 htr = alt;
khoshino 4:037fab837823 175 avp = gyro[0];
khoshino 4:037fab837823 176 avq = gyro[1];
khoshino 4:037fab837823 177 avr = gyro[2];
khoshino 2:30f96c159d9c 178 // ******************** initialization *******************
khoshino 2:30f96c159d9c 179
khoshino 2:30f96c159d9c 180 // ******************** interpolation ********************
khoshino 2:30f96c159d9c 181 dr = dr + ua * dt;
khoshino 4:037fab837823 182 xir = xir + ua * dt * cos(psr);
khoshino 4:037fab837823 183 etr = etr + ua * dt * sin(psr);
khoshino 2:30f96c159d9c 184
khoshino 4:037fab837823 185 if (xis != xis1 || fabs(xis-xis1)<50) {
khoshino 4:037fab837823 186 s = (xis-xis1)*(xis-xis1)+(ets-ets1)*(ets-ets1);
khoshino 2:30f96c159d9c 187 ua = 0.5*(ua+sqrt(s));
khoshino 2:30f96c159d9c 188 drc = drc+ua;
khoshino 2:30f96c159d9c 189 dr= drc;
khoshino 4:037fab837823 190 xir=xis;
khoshino 4:037fab837823 191 etr=ets;
khoshino 2:30f96c159d9c 192 }
khoshino 4:037fab837823 193 xis1=xis;
khoshino 4:037fab837823 194 ets1=ets;
khoshino 2:30f96c159d9c 195 // ******************** interpolation ********************
khoshino 2:30f96c159d9c 196
khoshino 2:30f96c159d9c 197 // ******************** guidance law WO homo *************
khoshino 4:037fab837823 198 htc = b0 + b1*dr + b2*dr*dr + b3*dr*dr*dr;
khoshino 2:30f96c159d9c 199
khoshino 2:30f96c159d9c 200 /*
khoshino 2:30f96c159d9c 201 c = drp-dr[i];
khoshino 2:30f96c159d9c 202 c1= dr[i+1]-drp;
khoshino 2:30f96c159d9c 203
khoshino 2:30f96c159d9c 204 if(c >=0 && c1 >= 0)
khoshino 2:30f96c159d9c 205 {
khoshino 2:30f96c159d9c 206 c0=c/(dr[i+1]-dr[i]);
khoshino 2:30f96c159d9c 207 c1=c1/(dr[i+1]-dr[i]);
khoshino 2:30f96c159d9c 208 psc=c0*ps[i+1]+c1*ps[i];
khoshino 2:30f96c159d9c 209 xic=c0*xi[i+1]+c1*xi[i];
khoshino 2:30f96c159d9c 210 etc=c0*et[i+1]+c1*et[i];
khoshino 2:30f96c159d9c 211 break;
khoshino 2:30f96c159d9c 212 }
khoshino 4:037fab837823 213 *///kurikaesituika
khoshino 2:30f96c159d9c 214
khoshino 2:30f96c159d9c 215 // ******************** guidance law WO homo *************
khoshino 2:30f96c159d9c 216
khoshino 2:30f96c159d9c 217 // ******************** control law **********************
higedura 0:9aa9be24636a 218 // transrating dec to hex
higedura 0:9aa9be24636a 219 //com
khoshino 2:30f96c159d9c 220 // ******************** control law **********************
higedura 0:9aa9be24636a 221
higedura 3:17236ad0ff1e 222 // !!! HOSHINO !!! fprintf add guidance, velocity and control
higedura 3:17236ad0ff1e 223 fprintf(fp, "%7.4f %7.4f %7.4f %7.3f %7.3f %7.3f %8.5f %8.1f %8.1f %8.1f\r\n", acc[0], acc[1], acc[2], gyro[0], gyro[1], gyro[2], azi, alt, GPS[0], GPS[1]);
khoshino 4:037fab837823 224
khoshino 4:037fab837823 225 flag++;
khoshino 4:037fab837823 226
higedura 0:9aa9be24636a 227 }
higedura 3:17236ad0ff1e 228
higedura 3:17236ad0ff1e 229 time = time + 0.1;
higedura 3:17236ad0ff1e 230
higedura 0:9aa9be24636a 231 }
higedura 0:9aa9be24636a 232
higedura 0:9aa9be24636a 233 fclose(fp);
higedura 0:9aa9be24636a 234 led1 = 1; led2 = 1; led3 = 1; led4 = 1;
higedura 0:9aa9be24636a 235
higedura 0:9aa9be24636a 236 }