無線ロガー用にトワイライト周辺の書き出しだけを変更した分

Dependencies:   SDFileSystem mbed

Fork of keiki2016ver4 by albatross

Committer:
taurin
Date:
Thu Mar 24 06:42:22 2016 +0000
Revision:
3:8dc516be2e7e
Parent:
2:5a1a638f5fe6
Child:
4:a863a092141c
3/24 ??; ?;

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