20160815 計器最新版

Dependencies:   SDFileSystem mbed

Fork of keiki2016verRtos by albatross

Committer:
taurin
Date:
Fri Mar 25 01:22:17 2016 +0000
Revision:
5:1b3c8e5ee99e
Parent:
4:a863a092141c
Child:
6:98a63127aa11
3/25 ??

Who changed what in which revision?

UserRevisionLine numberNew contents of line
taurin 1:5ec2409854da 1 //計器プログラム
taurin 0:085b2c5e3254 2 #include "mbed.h"
taurin 0:085b2c5e3254 3 #include "Fusokukei.h"
taurin 0:085b2c5e3254 4 #include "MPU6050.h"
taurin 0:085b2c5e3254 5 #include "SDFileSystem.h"
taurin 0:085b2c5e3254 6
taurin 0:085b2c5e3254 7 #define KX_VALUE_MIN 0.4
taurin 0:085b2c5e3254 8 #define KX_VALUE_MAX 0.8
taurin 1:5ec2409854da 9 #define SOUDA_DATAS_NUM 13
taurin 5:1b3c8e5ee99e 10 #define WRITE_DATAS_NUM 20
taurin 0:085b2c5e3254 11 #define MPU_LOOP_TIME 0.01
taurin 0:085b2c5e3254 12 #define AIR_LOOP_TIME 0.01
taurin 3:8dc516be2e7e 13 #define WRITE_DATAS_LOOP_TIME 1
taurin 3:8dc516be2e7e 14 #define ROLL_R_MAX_DEG 2
taurin 3:8dc516be2e7e 15 #define ROLL_L_MAX_DEG 2
taurin 3:8dc516be2e7e 16 #define MPU_DELT_MIN 500
taurin 3:8dc516be2e7e 17 #define SD_WRITE_NUM 10
taurin 4:a863a092141c 18 #define INIT_SERVO_PERIOD_MS 20
taurin 0:085b2c5e3254 19
taurin 0:085b2c5e3254 20 Serial pc(USBTX,USBRX);
taurin 0:085b2c5e3254 21 Serial soudaSerial(p13,p14);
taurin 1:5ec2409854da 22 Serial twe(p9,p10);
taurin 0:085b2c5e3254 23 Ticker writeDatasTicker;
taurin 3:8dc516be2e7e 24 Timer writeTimer;
taurin 0:085b2c5e3254 25
taurin 0:085b2c5e3254 26 InterruptIn FusokukeiPin(p30);
taurin 0:085b2c5e3254 27 Ticker FusokukeiTicker;
taurin 0:085b2c5e3254 28 Fusokukei air;
taurin 0:085b2c5e3254 29 volatile int air_kaitensu= 0;
taurin 0:085b2c5e3254 30
taurin 0:085b2c5e3254 31 float sum = 0;
taurin 0:085b2c5e3254 32 uint32_t sumCount = 0;
taurin 0:085b2c5e3254 33 MPU6050 mpu6050;
taurin 0:085b2c5e3254 34 Timer t;
taurin 0:085b2c5e3254 35 Ticker mpu6050Ticker;
taurin 0:085b2c5e3254 36
taurin 0:085b2c5e3254 37 AnalogIn kx_X(p17);
taurin 0:085b2c5e3254 38 AnalogIn kx_Y(p16);
taurin 0:085b2c5e3254 39 AnalogIn kx_Z(p15);
taurin 3:8dc516be2e7e 40 float KX_X,KX_Y,KX_Z;
taurin 0:085b2c5e3254 41
taurin 0:085b2c5e3254 42 DigitalOut RollAlarmR(p20);
taurin 0:085b2c5e3254 43 DigitalOut RollAlarmL(p19);
taurin 0:085b2c5e3254 44 DigitalOut led(LED1);
taurin 1:5ec2409854da 45 DigitalOut led2(LED2);
taurin 0:085b2c5e3254 46
taurin 0:085b2c5e3254 47 SDFileSystem sd(p5, p6, p7, p8, "sd");
taurin 0:085b2c5e3254 48 FILE* fp;
taurin 0:085b2c5e3254 49
taurin 5:1b3c8e5ee99e 50 PwmOut kisokuServo(p22);
taurin 5:1b3c8e5ee99e 51 PwmOut geikakuServo(p25);
taurin 4:a863a092141c 52
taurin 0:085b2c5e3254 53 char soudaDatas[SOUDA_DATAS_NUM];
taurin 3:8dc516be2e7e 54 float writeDatas[SD_WRITE_NUM][WRITE_DATAS_NUM];
taurin 3:8dc516be2e7e 55 volatile int write_datas_index = 0;
taurin 0:085b2c5e3254 56
taurin 0:085b2c5e3254 57 void air_countUp();
taurin 0:085b2c5e3254 58 void call_calcAirSpeed();
taurin 0:085b2c5e3254 59 void init();
taurin 0:085b2c5e3254 60 void FusokukeiInit();
taurin 0:085b2c5e3254 61 void MpuInit();
taurin 0:085b2c5e3254 62 void mpuProcessing();
taurin 0:085b2c5e3254 63 void SdInit();
taurin 0:085b2c5e3254 64 void DataReceiveFromSouda();
taurin 0:085b2c5e3254 65 void WriteDatas();
taurin 0:085b2c5e3254 66 float calcAttackAngle();
taurin 0:085b2c5e3254 67 float calcKXdeg(float x);
taurin 0:085b2c5e3254 68
taurin 0:085b2c5e3254 69 void air_countUp(){
taurin 0:085b2c5e3254 70 air_kaitensu++;
taurin 0:085b2c5e3254 71 }
taurin 0:085b2c5e3254 72
taurin 0:085b2c5e3254 73 void call_calcAirSpeed(){
taurin 0:085b2c5e3254 74 air.calcAirSpeed(air_kaitensu);
taurin 0:085b2c5e3254 75 air_kaitensu = 0;
taurin 0:085b2c5e3254 76 }
taurin 0:085b2c5e3254 77
taurin 0:085b2c5e3254 78 void init(){
taurin 1:5ec2409854da 79 twe.baud(115200);
taurin 3:8dc516be2e7e 80 writeTimer.start();
taurin 5:1b3c8e5ee99e 81 kisokuServo.period_ms(INIT_SERVO_PERIOD_MS);
taurin 5:1b3c8e5ee99e 82 geikakuServo.period_ms(INIT_SERVO_PERIOD_MS);
taurin 0:085b2c5e3254 83 FusokukeiInit();
taurin 0:085b2c5e3254 84 MpuInit();
taurin 1:5ec2409854da 85 SdInit();
taurin 3:8dc516be2e7e 86 //writeDatasTicker.attach(&WriteDatas,1);
taurin 1:5ec2409854da 87 //soudaSerial.attach(&DataReceiveFromSouda, Serial::RxIrq);
taurin 0:085b2c5e3254 88 }
taurin 0:085b2c5e3254 89
taurin 0:085b2c5e3254 90 void FusokukeiInit(){
taurin 0:085b2c5e3254 91 FusokukeiPin.rise(air_countUp);
taurin 0:085b2c5e3254 92 FusokukeiTicker.attach(&call_calcAirSpeed, AIR_LOOP_TIME);
taurin 0:085b2c5e3254 93 }
taurin 0:085b2c5e3254 94
taurin 0:085b2c5e3254 95 void MpuInit(){
taurin 0:085b2c5e3254 96 i2c.frequency(400000); // use fast (400 kHz) I2C
taurin 0:085b2c5e3254 97 t.start();
taurin 0:085b2c5e3254 98 uint8_t whoami = mpu6050.readByte(MPU6050_ADDRESS, WHO_AM_I_MPU6050); // Read WHO_AM_I register for MPU-6050
taurin 0:085b2c5e3254 99 if (whoami == 0x68) { // WHO_AM_I should always be 0x68
taurin 0:085b2c5e3254 100 wait(1);
taurin 0:085b2c5e3254 101 mpu6050.MPU6050SelfTest(SelfTest); // Start by performing self test and reporting values
taurin 0:085b2c5e3254 102 wait(1);
taurin 0:085b2c5e3254 103 if(SelfTest[0] < 1.0f && SelfTest[1] < 1.0f && SelfTest[2] < 1.0f && SelfTest[3] < 1.0f && SelfTest[4] < 1.0f && SelfTest[5] < 1.0f) {
taurin 0:085b2c5e3254 104 mpu6050.resetMPU6050(); // Reset registers to default in preparation for device calibration
taurin 0:085b2c5e3254 105 mpu6050.calibrateMPU6050(gyroBias, accelBias); // Calibrate gyro and accelerometers, load biases in bias registers
taurin 0:085b2c5e3254 106 mpu6050.initMPU6050(); //pc.printf("MPU6050 initialized for active data mode....\n\r"); // Initialize device for active mode read of acclerometer, gyroscope, and temperature
taurin 0:085b2c5e3254 107 wait(2);
taurin 0:085b2c5e3254 108 } else {
taurin 0:085b2c5e3254 109 }
taurin 0:085b2c5e3254 110 } else {
taurin 5:1b3c8e5ee99e 111 //pc.printf("out\n\r"); // Loop forever if communication doesn't happen
taurin 0:085b2c5e3254 112 }
taurin 0:085b2c5e3254 113 }
taurin 0:085b2c5e3254 114
taurin 4:a863a092141c 115 double calcPulse(int deg){
taurin 4:a863a092141c 116 return (0.0006+(deg/180.0)*(0.00235-0.00045));
taurin 4:a863a092141c 117
taurin 4:a863a092141c 118 }
taurin 4:a863a092141c 119
taurin 0:085b2c5e3254 120 void mpuProcessing(){
taurin 0:085b2c5e3254 121 if(mpu6050.readByte(MPU6050_ADDRESS, INT_STATUS) & 0x01) { // check if data ready interrupt
taurin 3:8dc516be2e7e 122 mpu6050.readAccelData(accelCount); // Read the x/y/z adc values
taurin 3:8dc516be2e7e 123 mpu6050.getAres();
taurin 3:8dc516be2e7e 124 ax = (float)accelCount[0]*aRes - accelBias[0]; // get actual g value, this depends on scale being set
taurin 3:8dc516be2e7e 125 ay = (float)accelCount[1]*aRes - accelBias[1];
taurin 3:8dc516be2e7e 126 az = (float)accelCount[2]*aRes - accelBias[2];
taurin 3:8dc516be2e7e 127 mpu6050.readGyroData(gyroCount); // Read the x/y/z adc values
taurin 3:8dc516be2e7e 128 mpu6050.getGres();
taurin 3:8dc516be2e7e 129 gx = (float)gyroCount[0]*gRes; // - gyroBias[0]; // get actual gyro value, this depends on scale being set
taurin 3:8dc516be2e7e 130 gy = (float)gyroCount[1]*gRes; // - gyroBias[1];
taurin 3:8dc516be2e7e 131 gz = (float)gyroCount[2]*gRes; // - gyroBias[2];
taurin 3:8dc516be2e7e 132 tempCount = mpu6050.readTempData(); // Read the x/y/z adc values
taurin 3:8dc516be2e7e 133 temperature = (tempCount) / 340. + 36.53; // Temperature in degrees Centigrade
taurin 3:8dc516be2e7e 134 }
taurin 3:8dc516be2e7e 135 Now = t.read_us();
taurin 3:8dc516be2e7e 136 deltat = (float)((Now - lastUpdate)/1000000.0f) ; // set integration time by time elapsed since last filter update
taurin 3:8dc516be2e7e 137 lastUpdate = Now;
taurin 3:8dc516be2e7e 138 sum += deltat;
taurin 3:8dc516be2e7e 139 sumCount++;
taurin 3:8dc516be2e7e 140 if(lastUpdate - firstUpdate > 10000000.0f) {
taurin 3:8dc516be2e7e 141 beta = 0.04; // decrease filter gain after stabilized
taurin 3:8dc516be2e7e 142 zeta = 0.015; // increasey bias drift gain after stabilized
taurin 3:8dc516be2e7e 143 }
taurin 3:8dc516be2e7e 144 mpu6050.MadgwickQuaternionUpdate(ax, ay, az, gx*PI/180.0f, gy*PI/180.0f, gz*PI/180.0f);
taurin 3:8dc516be2e7e 145 delt_t = t.read_ms() - count;
taurin 3:8dc516be2e7e 146 if (delt_t > MPU_DELT_MIN) {
taurin 3:8dc516be2e7e 147 yaw = atan2(2.0f * (q[1] * q[2] + q[0] * q[3]), q[0] * q[0] + q[1] * q[1] - q[2] * q[2] - q[3] * q[3]);
taurin 3:8dc516be2e7e 148 pitch = -asin(2.0f * (q[1] * q[3] - q[0] * q[2]));
taurin 3:8dc516be2e7e 149 roll = atan2(2.0f * (q[0] * q[1] + q[2] * q[3]), q[0] * q[0] - q[1] * q[1] - q[2] * q[2] + q[3] * q[3]);
taurin 3:8dc516be2e7e 150 pitch *= 180.0f / PI;
taurin 3:8dc516be2e7e 151 yaw *= 180.0f / PI;
taurin 3:8dc516be2e7e 152 roll *= 180.0f / PI;
taurin 3:8dc516be2e7e 153 myled= !myled;
taurin 3:8dc516be2e7e 154 count = t.read_ms();
taurin 3:8dc516be2e7e 155 sum = 0;
taurin 3:8dc516be2e7e 156 sumCount = 0;
taurin 0:085b2c5e3254 157 }
taurin 0:085b2c5e3254 158 }
taurin 0:085b2c5e3254 159
taurin 0:085b2c5e3254 160 void SdInit(){
taurin 0:085b2c5e3254 161 mkdir("/sd/mydir", 0777);
taurin 0:085b2c5e3254 162 fp = fopen("/sd/mydir/sdtest2.csv", "w");
taurin 0:085b2c5e3254 163 if(fp == NULL) {
taurin 0:085b2c5e3254 164 error("Could not open file for write\n");
taurin 0:085b2c5e3254 165 }
taurin 0:085b2c5e3254 166 fprintf(fp, "Hello fun SD Card World!\n\r");
taurin 0:085b2c5e3254 167 fclose(fp);
taurin 0:085b2c5e3254 168 }
taurin 0:085b2c5e3254 169
taurin 0:085b2c5e3254 170 void DataReceiveFromSouda(){
taurin 1:5ec2409854da 171 led2 = !led2;
taurin 5:1b3c8e5ee99e 172 //pc.printf("received\n\r");
taurin 1:5ec2409854da 173 for(int i = 0; i < SOUDA_DATAS_NUM; i++){
taurin 1:5ec2409854da 174 soudaDatas[i] = soudaSerial.getc();
taurin 0:085b2c5e3254 175 }
taurin 0:085b2c5e3254 176 }
taurin 0:085b2c5e3254 177
taurin 3:8dc516be2e7e 178 void SDprintf(){
taurin 1:5ec2409854da 179 fp = fopen("/sd/mydir/sdtest.csv", "a");
taurin 1:5ec2409854da 180 if(fp == NULL) {
taurin 1:5ec2409854da 181 error("Could not open file for write\n");
taurin 1:5ec2409854da 182 }
taurin 3:8dc516be2e7e 183 for(int i = 0; i < SD_WRITE_NUM; i++){
taurin 3:8dc516be2e7e 184 for(int j = 0; j < WRITE_DATAS_NUM; j++){
taurin 5:1b3c8e5ee99e 185 fprintf(fp,"%f,", writeDatas[i][j]);
taurin 5:1b3c8e5ee99e 186 //if(i%7==0){
taurin 5:1b3c8e5ee99e 187 // fprintf(fp,"\n\r");
taurin 5:1b3c8e5ee99e 188 // }
taurin 3:8dc516be2e7e 189 }
taurin 3:8dc516be2e7e 190 }
taurin 3:8dc516be2e7e 191 //for(int i = 0; i < SOUDA_DATAS_NUM; i++){
taurin 3:8dc516be2e7e 192 // fprintf(fp,"%i ",soudaDatas[i]);
taurin 3:8dc516be2e7e 193 // }
taurin 3:8dc516be2e7e 194 // fprintf(fp, "p:%f,r:%f,y:%f\n",pitch,roll,yaw);
taurin 3:8dc516be2e7e 195 // fprintf(fp, "gx:%f,gy:%f,gz:%f\n",calcKXdeg(kx_X.read()),calcKXdeg(KX_Y),calcKXdeg(KX_Z));
taurin 3:8dc516be2e7e 196 // fprintf(fp, "as:%f\n",airSpeed);
taurin 5:1b3c8e5ee99e 197 fprintf(fp,"\n\r");
taurin 3:8dc516be2e7e 198 fclose(fp);
taurin 3:8dc516be2e7e 199 }
taurin 3:8dc516be2e7e 200
taurin 3:8dc516be2e7e 201 void WriteDatas(){
taurin 3:8dc516be2e7e 202 int i;
taurin 3:8dc516be2e7e 203 for(i = 0; i < SOUDA_DATAS_NUM; i++){
taurin 3:8dc516be2e7e 204 writeDatas[write_datas_index][i] = (float)soudaDatas[i];
taurin 1:5ec2409854da 205 }
taurin 3:8dc516be2e7e 206 writeDatas[write_datas_index][i++] = calcKXdeg(kx_X.read());
taurin 3:8dc516be2e7e 207 writeDatas[write_datas_index][i++] = calcKXdeg(kx_Y.read());
taurin 3:8dc516be2e7e 208 writeDatas[write_datas_index][i++] = calcKXdeg(kx_Z.read());
taurin 3:8dc516be2e7e 209 writeDatas[write_datas_index][i++] = pitch;
taurin 3:8dc516be2e7e 210 writeDatas[write_datas_index][i++] = roll;
taurin 3:8dc516be2e7e 211 writeDatas[write_datas_index][i++] = yaw;
taurin 3:8dc516be2e7e 212 writeDatas[write_datas_index][i++] = airSpeed;
taurin 5:1b3c8e5ee99e 213 //writeDatas[write_datas_index][i++] = writeTimer.read();
taurin 3:8dc516be2e7e 214 for(i = 0; i < WRITE_DATAS_NUM; i++){
taurin 3:8dc516be2e7e 215 pc.printf("%f ", writeDatas[write_datas_index][i]);
taurin 5:1b3c8e5ee99e 216 twe.printf("%f,", writeDatas[write_datas_index][i]);
taurin 3:8dc516be2e7e 217 }
taurin 5:1b3c8e5ee99e 218 pc.printf("\n\r");
taurin 5:1b3c8e5ee99e 219 twe.printf("\n\r");
taurin 4:a863a092141c 220 if(write_datas_index == SD_WRITE_NUM-1){
taurin 3:8dc516be2e7e 221 SDprintf();
taurin 3:8dc516be2e7e 222 write_datas_index=0;
taurin 3:8dc516be2e7e 223 }
taurin 3:8dc516be2e7e 224 else{
taurin 3:8dc516be2e7e 225 write_datas_index++;
taurin 3:8dc516be2e7e 226 }
taurin 3:8dc516be2e7e 227 //for(int i = 0; i < SOUDA_DATAS_NUM; i++){
taurin 3:8dc516be2e7e 228 // pc.printf("%i ",soudaDatas[i]);
taurin 3:8dc516be2e7e 229 // twe.printf("%i ",soudaDatas[i]);
taurin 3:8dc516be2e7e 230 // }
taurin 3:8dc516be2e7e 231 // pc.printf("\n\r");
taurin 3:8dc516be2e7e 232 // twe.printf("\n\r");
taurin 3:8dc516be2e7e 233 // twe.printf("%f,%f,%f\n\r",pitch,roll,yaw);
taurin 3:8dc516be2e7e 234 // twe.printf("%f,%f,%f\n\r",calcKXdeg(kx_X.read()),calcKXdeg(KX_Y),calcKXdeg(KX_Z));
taurin 3:8dc516be2e7e 235 // twe.printf("%f\n\r",airSpeed);
taurin 3:8dc516be2e7e 236 // pc.printf("%f,%f,%f\n\r",pitch,roll,yaw);
taurin 3:8dc516be2e7e 237 // pc.printf("%f,%f,%f\n\r",calcKXdeg(kx_X.read()),calcKXdeg(KX_Y),calcKXdeg(KX_Z));
taurin 3:8dc516be2e7e 238 // pc.printf("%f\n\r",airSpeed);
taurin 3:8dc516be2e7e 239 // SDprintf();
taurin 0:085b2c5e3254 240 }
taurin 0:085b2c5e3254 241
taurin 0:085b2c5e3254 242 float calcKXdeg(float x){
taurin 0:085b2c5e3254 243 return -310.54*x+156.65;
taurin 0:085b2c5e3254 244 }
taurin 0:085b2c5e3254 245
taurin 0:085b2c5e3254 246 float calcAttackAngle(){
taurin 0:085b2c5e3254 247 return pitch-calcKXdeg(kx_Z.read());
taurin 0:085b2c5e3254 248 }
taurin 0:085b2c5e3254 249
taurin 0:085b2c5e3254 250 void RollAlarm(){
taurin 0:085b2c5e3254 251 if((roll < 0) && (roll > ROLL_L_MAX_DEG-180)){
taurin 0:085b2c5e3254 252 RollAlarmL = 1;
taurin 0:085b2c5e3254 253 }
taurin 0:085b2c5e3254 254 else{
taurin 0:085b2c5e3254 255 RollAlarmL = 0;
taurin 0:085b2c5e3254 256 }
taurin 0:085b2c5e3254 257
taurin 0:085b2c5e3254 258 if((roll > 0) && (roll < 180-ROLL_R_MAX_DEG)){
taurin 0:085b2c5e3254 259 RollAlarmR = 1;
taurin 0:085b2c5e3254 260 }
taurin 0:085b2c5e3254 261 else{
taurin 0:085b2c5e3254 262 RollAlarmR = 0;
taurin 0:085b2c5e3254 263 }
taurin 0:085b2c5e3254 264 }
taurin 0:085b2c5e3254 265
taurin 4:a863a092141c 266 void WriteServo(){
taurin 5:1b3c8e5ee99e 267 kisokuServo.pulsewidth(calcPulse(airSpeed*10));
taurin 5:1b3c8e5ee99e 268 //geikakuServo.pulsewidth(calcPulse(pitch*0.13));
taurin 5:1b3c8e5ee99e 269
taurin 5:1b3c8e5ee99e 270 //kisokuServo.pulsewidth(calcPulse(0));
taurin 5:1b3c8e5ee99e 271 //geikakuServo.pulsewidth(calcPulse(0));
taurin 4:a863a092141c 272 }
taurin 4:a863a092141c 273
taurin 0:085b2c5e3254 274 int main(){
taurin 0:085b2c5e3254 275 init();
taurin 0:085b2c5e3254 276 while(1){
taurin 0:085b2c5e3254 277 mpuProcessing();
taurin 0:085b2c5e3254 278 RollAlarm();
taurin 1:5ec2409854da 279 DataReceiveFromSouda();
taurin 3:8dc516be2e7e 280 WriteDatas();
taurin 4:a863a092141c 281 WriteServo();
taurin 0:085b2c5e3254 282 }
taurin 0:085b2c5e3254 283 }