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