20160815 計器最新版
Dependencies: SDFileSystem mbed
Fork of keiki2016verRtos by
main.cpp@2:5a1a638f5fe6, 2016-02-25 (annotated)
- Committer:
- taurin
- Date:
- Thu Feb 25 09:13:46 2016 +0000
- Revision:
- 2:5a1a638f5fe6
- Parent:
- 1:5ec2409854da
- Child:
- 3:8dc516be2e7e
aaaa
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:085b2c5e3254 | 10 | #define WRITE_DATAS_NUM 28 |
taurin | 0:085b2c5e3254 | 11 | #define MPU_LOOP_TIME 0.01 |
taurin | 0:085b2c5e3254 | 12 | #define AIR_LOOP_TIME 0.01 |
taurin | 0:085b2c5e3254 | 13 | #define WRITE_DATAS_LOOP_TIME 0.5 |
taurin | 0:085b2c5e3254 | 14 | #define ROLL_R_MAX_DEG 5 |
taurin | 0:085b2c5e3254 | 15 | #define ROLL_L_MAX_DEG 5 |
taurin | 0:085b2c5e3254 | 16 | |
taurin | 0:085b2c5e3254 | 17 | Serial pc(USBTX,USBRX); |
taurin | 0:085b2c5e3254 | 18 | Serial soudaSerial(p13,p14); |
taurin | 1:5ec2409854da | 19 | Serial twe(p9,p10); |
taurin | 0:085b2c5e3254 | 20 | Ticker writeDatasTicker; |
taurin | 0:085b2c5e3254 | 21 | |
taurin | 0:085b2c5e3254 | 22 | InterruptIn FusokukeiPin(p30); |
taurin | 0:085b2c5e3254 | 23 | Ticker FusokukeiTicker; |
taurin | 0:085b2c5e3254 | 24 | Fusokukei air; |
taurin | 0:085b2c5e3254 | 25 | volatile int air_kaitensu= 0; |
taurin | 0:085b2c5e3254 | 26 | |
taurin | 0:085b2c5e3254 | 27 | float sum = 0; |
taurin | 0:085b2c5e3254 | 28 | uint32_t sumCount = 0; |
taurin | 0:085b2c5e3254 | 29 | MPU6050 mpu6050; |
taurin | 0:085b2c5e3254 | 30 | Timer t; |
taurin | 0:085b2c5e3254 | 31 | Ticker mpu6050Ticker; |
taurin | 0:085b2c5e3254 | 32 | |
taurin | 0:085b2c5e3254 | 33 | AnalogIn kx_X(p17); |
taurin | 0:085b2c5e3254 | 34 | AnalogIn kx_Y(p16); |
taurin | 0:085b2c5e3254 | 35 | AnalogIn kx_Z(p15); |
taurin | 0:085b2c5e3254 | 36 | |
taurin | 0:085b2c5e3254 | 37 | DigitalOut RollAlarmR(p20); |
taurin | 0:085b2c5e3254 | 38 | DigitalOut RollAlarmL(p19); |
taurin | 0:085b2c5e3254 | 39 | DigitalOut led(LED1); |
taurin | 1:5ec2409854da | 40 | DigitalOut led2(LED2); |
taurin | 0:085b2c5e3254 | 41 | |
taurin | 0:085b2c5e3254 | 42 | SDFileSystem sd(p5, p6, p7, p8, "sd"); |
taurin | 0:085b2c5e3254 | 43 | FILE* fp; |
taurin | 0:085b2c5e3254 | 44 | |
taurin | 0:085b2c5e3254 | 45 | char soudaDatas[SOUDA_DATAS_NUM]; |
taurin | 0:085b2c5e3254 | 46 | char writeDatas[WRITE_DATAS_NUM]; |
taurin | 0:085b2c5e3254 | 47 | |
taurin | 0:085b2c5e3254 | 48 | void air_countUp(); |
taurin | 0:085b2c5e3254 | 49 | void call_calcAirSpeed(); |
taurin | 0:085b2c5e3254 | 50 | void init(); |
taurin | 0:085b2c5e3254 | 51 | void FusokukeiInit(); |
taurin | 0:085b2c5e3254 | 52 | void MpuInit(); |
taurin | 0:085b2c5e3254 | 53 | void mpuProcessing(); |
taurin | 0:085b2c5e3254 | 54 | void SdInit(); |
taurin | 0:085b2c5e3254 | 55 | void DataReceiveFromSouda(); |
taurin | 0:085b2c5e3254 | 56 | void WriteDatas(); |
taurin | 0:085b2c5e3254 | 57 | float calcAttackAngle(); |
taurin | 0:085b2c5e3254 | 58 | float calcKXdeg(float x); |
taurin | 0:085b2c5e3254 | 59 | |
taurin | 0:085b2c5e3254 | 60 | void air_countUp(){ |
taurin | 0:085b2c5e3254 | 61 | air_kaitensu++; |
taurin | 0:085b2c5e3254 | 62 | } |
taurin | 0:085b2c5e3254 | 63 | |
taurin | 0:085b2c5e3254 | 64 | void call_calcAirSpeed(){ |
taurin | 0:085b2c5e3254 | 65 | air.calcAirSpeed(air_kaitensu); |
taurin | 0:085b2c5e3254 | 66 | air_kaitensu = 0; |
taurin | 0:085b2c5e3254 | 67 | } |
taurin | 0:085b2c5e3254 | 68 | |
taurin | 0:085b2c5e3254 | 69 | void init(){ |
taurin | 1:5ec2409854da | 70 | twe.baud(115200); |
taurin | 0:085b2c5e3254 | 71 | FusokukeiInit(); |
taurin | 0:085b2c5e3254 | 72 | MpuInit(); |
taurin | 1:5ec2409854da | 73 | SdInit(); |
taurin | 1:5ec2409854da | 74 | writeDatasTicker.attach(&WriteDatas,0.25); |
taurin | 1:5ec2409854da | 75 | //soudaSerial.attach(&DataReceiveFromSouda, Serial::RxIrq); |
taurin | 0:085b2c5e3254 | 76 | } |
taurin | 0:085b2c5e3254 | 77 | |
taurin | 0:085b2c5e3254 | 78 | void FusokukeiInit(){ |
taurin | 0:085b2c5e3254 | 79 | FusokukeiPin.rise(air_countUp); |
taurin | 0:085b2c5e3254 | 80 | FusokukeiTicker.attach(&call_calcAirSpeed, AIR_LOOP_TIME); |
taurin | 0:085b2c5e3254 | 81 | } |
taurin | 0:085b2c5e3254 | 82 | |
taurin | 0:085b2c5e3254 | 83 | void MpuInit(){ |
taurin | 0:085b2c5e3254 | 84 | i2c.frequency(400000); // use fast (400 kHz) I2C |
taurin | 0:085b2c5e3254 | 85 | t.start(); |
taurin | 0:085b2c5e3254 | 86 | uint8_t whoami = mpu6050.readByte(MPU6050_ADDRESS, WHO_AM_I_MPU6050); // Read WHO_AM_I register for MPU-6050 |
taurin | 0:085b2c5e3254 | 87 | if (whoami == 0x68) { // WHO_AM_I should always be 0x68 |
taurin | 0:085b2c5e3254 | 88 | wait(1); |
taurin | 0:085b2c5e3254 | 89 | mpu6050.MPU6050SelfTest(SelfTest); // Start by performing self test and reporting values |
taurin | 0:085b2c5e3254 | 90 | wait(1); |
taurin | 0:085b2c5e3254 | 91 | 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 | 92 | mpu6050.resetMPU6050(); // Reset registers to default in preparation for device calibration |
taurin | 0:085b2c5e3254 | 93 | mpu6050.calibrateMPU6050(gyroBias, accelBias); // Calibrate gyro and accelerometers, load biases in bias registers |
taurin | 0:085b2c5e3254 | 94 | 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 | 95 | wait(2); |
taurin | 0:085b2c5e3254 | 96 | } else { |
taurin | 0:085b2c5e3254 | 97 | } |
taurin | 0:085b2c5e3254 | 98 | } else { |
taurin | 0:085b2c5e3254 | 99 | pc.printf("out\n\r"); // Loop forever if communication doesn't happen |
taurin | 0:085b2c5e3254 | 100 | } |
taurin | 0:085b2c5e3254 | 101 | } |
taurin | 0:085b2c5e3254 | 102 | |
taurin | 0:085b2c5e3254 | 103 | void mpuProcessing(){ |
taurin | 0:085b2c5e3254 | 104 | if(mpu6050.readByte(MPU6050_ADDRESS, INT_STATUS) & 0x01) { // check if data ready interrupt |
taurin | 0:085b2c5e3254 | 105 | mpu6050.readAccelData(accelCount); // Read the x/y/z adc values |
taurin | 0:085b2c5e3254 | 106 | mpu6050.getAres(); |
taurin | 0:085b2c5e3254 | 107 | ax = (float)accelCount[0]*aRes - accelBias[0]; // get actual g value, this depends on scale being set |
taurin | 0:085b2c5e3254 | 108 | ay = (float)accelCount[1]*aRes - accelBias[1]; |
taurin | 0:085b2c5e3254 | 109 | az = (float)accelCount[2]*aRes - accelBias[2]; |
taurin | 0:085b2c5e3254 | 110 | mpu6050.readGyroData(gyroCount); // Read the x/y/z adc values |
taurin | 0:085b2c5e3254 | 111 | mpu6050.getGres(); |
taurin | 0:085b2c5e3254 | 112 | gx = (float)gyroCount[0]*gRes; // - gyroBias[0]; // get actual gyro value, this depends on scale being set |
taurin | 0:085b2c5e3254 | 113 | gy = (float)gyroCount[1]*gRes; // - gyroBias[1]; |
taurin | 0:085b2c5e3254 | 114 | gz = (float)gyroCount[2]*gRes; // - gyroBias[2]; |
taurin | 0:085b2c5e3254 | 115 | tempCount = mpu6050.readTempData(); // Read the x/y/z adc values |
taurin | 0:085b2c5e3254 | 116 | temperature = (tempCount) / 340. + 36.53; // Temperature in degrees Centigrade |
taurin | 0:085b2c5e3254 | 117 | } |
taurin | 0:085b2c5e3254 | 118 | Now = t.read_us(); |
taurin | 0:085b2c5e3254 | 119 | deltat = (float)((Now - lastUpdate)/1000000.0f) ; // set integration time by time elapsed since last filter update |
taurin | 0:085b2c5e3254 | 120 | lastUpdate = Now; |
taurin | 0:085b2c5e3254 | 121 | sum += deltat; |
taurin | 0:085b2c5e3254 | 122 | sumCount++; |
taurin | 0:085b2c5e3254 | 123 | if(lastUpdate - firstUpdate > 10000000.0f) { |
taurin | 0:085b2c5e3254 | 124 | beta = 0.04; // decrease filter gain after stabilized |
taurin | 0:085b2c5e3254 | 125 | zeta = 0.015; // increasey bias drift gain after stabilized |
taurin | 0:085b2c5e3254 | 126 | } |
taurin | 0:085b2c5e3254 | 127 | mpu6050.MadgwickQuaternionUpdate(ax, ay, az, gx*PI/180.0f, gy*PI/180.0f, gz*PI/180.0f); |
taurin | 0:085b2c5e3254 | 128 | delt_t = t.read_ms() - count; |
taurin | 2:5a1a638f5fe6 | 129 | if (delt_t > 800) { // update LCD once per half-second independent of read rate |
taurin | 0:085b2c5e3254 | 130 | 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 | 0:085b2c5e3254 | 131 | pitch = -asin(2.0f * (q[1] * q[3] - q[0] * q[2])); |
taurin | 0:085b2c5e3254 | 132 | 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 | 0:085b2c5e3254 | 133 | pitch *= 180.0f / PI; |
taurin | 0:085b2c5e3254 | 134 | yaw *= 180.0f / PI; |
taurin | 0:085b2c5e3254 | 135 | roll *= 180.0f / PI; |
taurin | 0:085b2c5e3254 | 136 | myled= !myled; |
taurin | 0:085b2c5e3254 | 137 | count = t.read_ms(); |
taurin | 0:085b2c5e3254 | 138 | sum = 0; |
taurin | 0:085b2c5e3254 | 139 | sumCount = 0; |
taurin | 0:085b2c5e3254 | 140 | } |
taurin | 0:085b2c5e3254 | 141 | } |
taurin | 0:085b2c5e3254 | 142 | |
taurin | 0:085b2c5e3254 | 143 | void SdInit(){ |
taurin | 0:085b2c5e3254 | 144 | mkdir("/sd/mydir", 0777); |
taurin | 0:085b2c5e3254 | 145 | fp = fopen("/sd/mydir/sdtest2.csv", "w"); |
taurin | 0:085b2c5e3254 | 146 | if(fp == NULL) { |
taurin | 0:085b2c5e3254 | 147 | error("Could not open file for write\n"); |
taurin | 0:085b2c5e3254 | 148 | } |
taurin | 0:085b2c5e3254 | 149 | fprintf(fp, "Hello fun SD Card World!\n\r"); |
taurin | 0:085b2c5e3254 | 150 | fclose(fp); |
taurin | 0:085b2c5e3254 | 151 | } |
taurin | 0:085b2c5e3254 | 152 | |
taurin | 0:085b2c5e3254 | 153 | void DataReceiveFromSouda(){ |
taurin | 1:5ec2409854da | 154 | led2 = !led2; |
taurin | 1:5ec2409854da | 155 | pc.printf("received\n\r"); |
taurin | 1:5ec2409854da | 156 | for(int i = 0; i < SOUDA_DATAS_NUM; i++){ |
taurin | 1:5ec2409854da | 157 | soudaDatas[i] = soudaSerial.getc(); |
taurin | 0:085b2c5e3254 | 158 | } |
taurin | 0:085b2c5e3254 | 159 | } |
taurin | 0:085b2c5e3254 | 160 | |
taurin | 0:085b2c5e3254 | 161 | void WriteDatas(){ |
taurin | 1:5ec2409854da | 162 | pc.printf("write\n\r"); |
taurin | 1:5ec2409854da | 163 | for(int i = 0; i < SOUDA_DATAS_NUM; i++){ |
taurin | 1:5ec2409854da | 164 | pc.printf("%i ",soudaDatas[i]); |
taurin | 1:5ec2409854da | 165 | twe.printf("%i ",soudaDatas[i]); |
taurin | 1:5ec2409854da | 166 | } |
taurin | 1:5ec2409854da | 167 | pc.printf("\n\r"); |
taurin | 1:5ec2409854da | 168 | twe.printf("\n\r"); |
taurin | 1:5ec2409854da | 169 | twe.printf("%f,%f,%f\n\r",pitch,roll,yaw); |
taurin | 1:5ec2409854da | 170 | // twe.printf("%f,%f,%f\n\r",gx,gy,gz); |
taurin | 1:5ec2409854da | 171 | // twe.printf("%f\n\r",airSpeed); |
taurin | 1:5ec2409854da | 172 | pc.printf("%f,%f,%f\n\r",pitch,roll,yaw); |
taurin | 1:5ec2409854da | 173 | pc.printf("%f,%f,%f\n\r",gx,gy,gz); |
taurin | 1:5ec2409854da | 174 | pc.printf("%f\n\r",airSpeed); |
taurin | 1:5ec2409854da | 175 | fp = fopen("/sd/mydir/sdtest.csv", "a"); |
taurin | 1:5ec2409854da | 176 | if(fp == NULL) { |
taurin | 1:5ec2409854da | 177 | error("Could not open file for write\n"); |
taurin | 1:5ec2409854da | 178 | } |
taurin | 1:5ec2409854da | 179 | for(int i = 0; i < SOUDA_DATAS_NUM; i++){ |
taurin | 1:5ec2409854da | 180 | fprintf(fp,"%i ",soudaDatas[i]); |
taurin | 1:5ec2409854da | 181 | } |
taurin | 1:5ec2409854da | 182 | fprintf(fp, "p:%f,r:%f,y:%f\n",pitch,roll,yaw); |
taurin | 1:5ec2409854da | 183 | fprintf(fp, "gx:%f,gy:%f,gz:%f\n",gx,gy,gz); |
taurin | 1:5ec2409854da | 184 | fprintf(fp, "as:%f\n",airSpeed); |
taurin | 1:5ec2409854da | 185 | fclose(fp); |
taurin | 0:085b2c5e3254 | 186 | } |
taurin | 0:085b2c5e3254 | 187 | |
taurin | 0:085b2c5e3254 | 188 | float calcKXdeg(float x){ |
taurin | 0:085b2c5e3254 | 189 | return -310.54*x+156.65; |
taurin | 0:085b2c5e3254 | 190 | } |
taurin | 0:085b2c5e3254 | 191 | |
taurin | 0:085b2c5e3254 | 192 | float calcAttackAngle(){ |
taurin | 0:085b2c5e3254 | 193 | return pitch-calcKXdeg(kx_Z.read()); |
taurin | 0:085b2c5e3254 | 194 | } |
taurin | 0:085b2c5e3254 | 195 | |
taurin | 0:085b2c5e3254 | 196 | void kxRead(){ |
taurin | 0:085b2c5e3254 | 197 | gx = kx_X.read(); |
taurin | 0:085b2c5e3254 | 198 | gy = kx_Y.read(); |
taurin | 0:085b2c5e3254 | 199 | gz = kx_Z.read(); |
taurin | 0:085b2c5e3254 | 200 | } |
taurin | 0:085b2c5e3254 | 201 | |
taurin | 0:085b2c5e3254 | 202 | void RollAlarm(){ |
taurin | 0:085b2c5e3254 | 203 | if((roll < 0) && (roll > ROLL_L_MAX_DEG-180)){ |
taurin | 0:085b2c5e3254 | 204 | RollAlarmL = 1; |
taurin | 0:085b2c5e3254 | 205 | } |
taurin | 0:085b2c5e3254 | 206 | else{ |
taurin | 0:085b2c5e3254 | 207 | RollAlarmL = 0; |
taurin | 0:085b2c5e3254 | 208 | } |
taurin | 0:085b2c5e3254 | 209 | |
taurin | 0:085b2c5e3254 | 210 | if((roll > 0) && (roll < 180-ROLL_R_MAX_DEG)){ |
taurin | 0:085b2c5e3254 | 211 | RollAlarmR = 1; |
taurin | 0:085b2c5e3254 | 212 | } |
taurin | 0:085b2c5e3254 | 213 | else{ |
taurin | 0:085b2c5e3254 | 214 | RollAlarmR = 0; |
taurin | 0:085b2c5e3254 | 215 | } |
taurin | 0:085b2c5e3254 | 216 | } |
taurin | 0:085b2c5e3254 | 217 | |
taurin | 0:085b2c5e3254 | 218 | int main(){ |
taurin | 0:085b2c5e3254 | 219 | init(); |
taurin | 0:085b2c5e3254 | 220 | while(1){ |
taurin | 0:085b2c5e3254 | 221 | mpuProcessing(); |
taurin | 0:085b2c5e3254 | 222 | kxRead(); |
taurin | 0:085b2c5e3254 | 223 | RollAlarm(); |
taurin | 1:5ec2409854da | 224 | DataReceiveFromSouda(); |
taurin | 0:085b2c5e3254 | 225 | } |
taurin | 0:085b2c5e3254 | 226 | } |