2018年度計器mbed用プログラム
Dependencies: BufferedSoftSerial2 INA226_ver1 mbed-rtos mbed SDFileSystem-RTOS
Fork of keiki2017 by
main.cpp
- Committer:
- tsumagari
- Date:
- 2017-02-19
- Branch:
- Thread-gyogetsuMPU
- Revision:
- 37:34aaa1951390
- Parent:
- 34:c46f2f687c7b
- Child:
- 38:32f483b0a77f
File content as of revision 37:34aaa1951390:
//計器プログラム #include "mbed.h" #include "rtos.h" #include "Fusokukei.h" #include "MPU6050.h" #include "BufferedSoftSerial.h" #include "Cadence.h" #define SOUDA_DATAS_NUM 24 //(yokutan 7 + input 5)*2 #define WRITE_DATAS_NUM 20 #define SD_WRITE_NUM 1 #define MPU_LOOP_TIME 0.01 #define AIR_LOOP_TIME 0.01 #define WRITE_DATAS_LOOP_TIME 1 #define ROLL_R_MAX_DEG 2 #define ROLL_L_MAX_DEG 2 #define MPU_DELT_MIN 250 #define INIT_SERVO_PERIOD_MS 20 //-----------------------------------(resetInterrupt def) extern "C" void mbed_reset(); InterruptIn resetPin(p26); Timer resetTimeCount; void resetInterrupt(){ while(resetPin){ resetTimeCount.start(); if(resetTimeCount.read()>3) mbed_reset(); } resetTimeCount.reset(); } //------------------------------------------------------- RawSerial pc(USBTX,USBRX); RawSerial android(p9,p10); BufferedSoftSerial soudaSerial(p17,p18); BufferedSoftSerial twe(p11,p12); Cadence cadence_twe(p13,p14); //Ticker cadenceUpdateTicker; //Ticker writeDatasTicker; //Timer writeTimer; InterruptIn FusokukeiPin(p23); Ticker FusokukeiTicker; Fusokukei air; volatile int air_kaitensu= 0; //Timer sonarTimer; AnalogIn sonarPin(p15); //InterruputIn sonarPin(p15); //double sonarDistTime double sonarDist; float sonarV; float sum = 0; uint32_t sumCount = 0; MPU6050 mpu6050; Timer t; //Ticker mpu6050Ticker; DigitalOut RollAlarmR(p20); DigitalOut RollAlarmL(p19); DigitalOut led(LED1); DigitalOut led2(LED2); char soudaDatas[SOUDA_DATAS_NUM]; float writeDatas[SD_WRITE_NUM][WRITE_DATAS_NUM]; volatile int write_datas_index = 0; void air_countUp(); void call_calcAirSpeed(); void sonarInterruptStart(); void sonarInterruptStop(); void updateCadence(void const *arg); void init(); void FusokukeiInit(); void MpuInit(); void mpuProcessing(void const *arg); void DataReceiveFromSouda(void const *arg); void WriteDatas(); float calcAttackAngle(); float calcKXdeg(float x); void air_countUp(){ air_kaitensu++; } void call_calcAirSpeed(){ air.calcAirSpeed(air_kaitensu); air_kaitensu = 0; } void sonarInterruptStart(){ // sonarTimer.start(); } void sonarInterruptStop(){ // sonarTimer.stop(); // sonarDistTime = sonarTimer.read_us(); // sonarTimer.reset(); // sonarDist = sonarDistTime*0.018624 - 13.511; } void sonarCalc(){ sonarV = 0; for(int i = 0; i<20; i++){ sonarV += sonarPin.read(); wait(0.01); } sonarDist = (sonarV/20)*2064.5;// volt*3.3*1000/1.6 (電圧/距離:3.2mV/2cm) } void updateCadence(/*void const *arg*/){ // while(1){ cadence_twe.readData(); // Thread::wait(5); // } } void init(){ //--------------------------------------(resetInterrupt init) resetPin.rise(resetInterrupt); resetPin.mode(PullDown); //----------------------------------------------------------- twe.baud(19200);//BufferedSoftSerialでは19200が上限。twelite側でもBPS無効化が必要 //writeTimer.start(); FusokukeiInit(); // MpuInit(); //writeDatasTicker.attach(&WriteDatas,1); // cadenceUpdateTicker.attach(&updateCadence, 1); //-----for InterruptMode of sonar---------------------------- // sonarPin.rise(&sonarInterruptStart); // sonarPin.fall(&sonarInterruptStop); //----------------------------------------------------------- } void FusokukeiInit(){ FusokukeiPin.rise(air_countUp); FusokukeiTicker.attach(&call_calcAirSpeed, AIR_LOOP_TIME); } void MpuInit(){ i2c.frequency(400000); // use fast (400 kHz) I2C t.start(); uint8_t whoami = mpu6050.readByte(MPU6050_ADDRESS, WHO_AM_I_MPU6050); // Read WHO_AM_I register for MPU-6050 if (whoami == 0x68) { // WHO_AM_I should always be 0x68 Thread::wait(100); mpu6050.MPU6050SelfTest(SelfTest); // Start by performing self test and reporting values Thread::wait(100); 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) { mpu6050.resetMPU6050(); // Reset registers to default in preparation for device calibration mpu6050.calibrateMPU6050(gyroBias, accelBias); // Calibrate gyro and accelerometers, load biases in bias registers mpu6050.initMPU6050(); //pc.printf("MPU6050 initialized for active data mode....\n\r"); // Initialize device for active mode read of acclerometer, gyroscope, and temperature Thread::wait(200); } else { } } else { //pc.printf("out\n\r"); // Loop forever if communication doesn't happen } } double calcPulse(int deg){ return (0.0006+(deg/180.0)*(0.00235-0.00045)); } void mpuProcessing(void const *arg){ MpuInit(); while(1){ if(mpu6050.readByte(MPU6050_ADDRESS, INT_STATUS) & 0x01) { // check if data ready interrupt mpu6050.readAccelData(accelCount); // Read the x/y/z adc values mpu6050.getAres(); ax = (float)accelCount[0]*aRes - accelBias[0]; // get actual g value, this depends on scale being set ay = (float)accelCount[1]*aRes - accelBias[1]; az = (float)accelCount[2]*aRes - accelBias[2]; mpu6050.readGyroData(gyroCount); // Read the x/y/z adc values mpu6050.getGres(); gx = (float)gyroCount[0]*gRes; // - gyroBias[0]; // get actual gyro value, this depends on scale being set gy = (float)gyroCount[1]*gRes; // - gyroBias[1]; gz = (float)gyroCount[2]*gRes; // - gyroBias[2]; tempCount = mpu6050.readTempData(); // Read the x/y/z adc values temperature = (tempCount) / 340. + 36.53; // Temperature in degrees Centigrade } Now = t.read_us(); deltat = (float)((Now - lastUpdate)/1000000.0f) ; // set integration time by time elapsed since last filter update lastUpdate = Now; sum += deltat; sumCount++; if(lastUpdate - firstUpdate > 10000000.0f) { beta = 0.04; // decrease filter gain after stabilized zeta = 0.015; // increasey bias drift gain after stabilized } mpu6050.MadgwickQuaternionUpdate(ax, ay, az, gx*PI/180.0f, gy*PI/180.0f, gz*PI/180.0f); delt_t = t.read_ms() - count; if (delt_t > MPU_DELT_MIN) { 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]); pitch = -asin(2.0f * (q[1] * q[3] - q[0] * q[2])); 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]); pitch *= 180.0f / PI; yaw *= 180.0f / PI; roll *= 180.0f / PI; myled= !myled; count = t.read_ms(); sum = 0; sumCount = 0; } Thread::wait(0.01); }//while(1) } void DataReceiveFromSouda(/*void const *arg*/){ // while(1){ if(soudaSerial.readable()){ led2 = !led2; char c = soudaSerial.getc(); while( c != ';' ){ c = soudaSerial.getc(); } for(int i = 0; i < SOUDA_DATAS_NUM; i++){ soudaDatas[i] = soudaSerial.getc(); } }//if // }//while(1) } void WriteDatas(){ int i; for(i = 0; i < SOUDA_DATAS_NUM; i++){ //writeDatas[write_datas_index][i] = 0.0; writeDatas[write_datas_index][i] = (float)soudaDatas[i]; } writeDatas[write_datas_index][i++] = pitch; writeDatas[write_datas_index][i++] = roll; writeDatas[write_datas_index][i++] = yaw; writeDatas[write_datas_index][i++] = airSpeed; writeDatas[write_datas_index][i++] = sonarDist; writeDatas[write_datas_index][i++] = cadence_twe.cadence; //writeDatas[write_datas_index][i++] = writeTimer.read(); //for(i = 0; i < WRITE_DATAS_NUM; i++){ // pc.printf("%f ", writeDatas[write_datas_index][i]); // twe.printf("%f,", writeDatas[write_datas_index][i]); // } // pc.printf("\n\r"); // twe.printf("\n\r"); // if(write_datas_index == SD_WRITE_NUM-1){ // write_datas_index=0; // } // else{ // write_datas_index++; // } for(int i = 0; i < SOUDA_DATAS_NUM; i++){ pc.printf("%i ",soudaDatas[i]); twe.printf("%i,",soudaDatas[i]); // android.printf("%i,",soudaDatas[i]); } //pc.printf("\n\r"); twe.printf("%f,%f,%f,",pitch,roll,yaw); twe.printf("%f,%f,%f\r\n",airSpeed,sonarDist,cadence_twe.cadence); pc.printf("%f,%f,%f\n\r",pitch,roll,yaw); //pc.printf("%f,%f,%f\n\r",calcKXdeg(kx_X.read()),calcKXdeg(KX_Y),calcKXdeg(KX_Z)); pc.printf("%f,%f,%s\n\r",airSpeed,sonarDist,cadence_twe.myBuff); pc.printf(cadence_twe.strData.c_str()); pc.putc(cadence_twe.strV[0]); if(android.writeable()){ // for(int i = 0; i<SOUDA_DATAS_NUM; i++){ // android.printf("%i,",soudaDatas[i]); // } // android.printf("%f,%f,%f,",pitch,roll,yaw); // android.printf("%f,%f,\r\n",airSpeed,sonarDist); android.printf("%f,%f,test\n\r",roll,airSpeed); } //SDprintf(); } void WriteDatasF(){ pc.printf("airSpeed:%f\n\r",airSpeed); } //float calcKXdeg(float x){ // return -310.54*x+156.65; //} //float calcAttackAngle(){ // return pitch-calcKXdeg(kx_Z.read()); //} void RollAlarm(){ if((roll < -ROLL_L_MAX_DEG ) && (roll > ROLL_L_MAX_DEG-180)){ RollAlarmL = 1; } else{ RollAlarmL = 0; } if((roll > ROLL_R_MAX_DEG) && (roll < 180-ROLL_R_MAX_DEG)){ RollAlarmR = 1; } else{ RollAlarmR = 0; } } void WriteServo(){ //kisokuServo.pulsewidth(calcPulse(airSpeed*10)); // kisokuServo.pulsewidth(calcPulse(9*airSpeed)); if(pitch<0){ // geikakuServo.pulsewidth(calcPulse(0)); } else{ // geikakuServo.pulsewidth(calcPulse(abs(pitch*90/13.0))); } //pc.printf("a:%f",airSpeed); //pc.printf("p:%f\r\n",pitch); //kisokuServo.pulsewidth(calcPulse(0)); //geikakuServo.pulsewidth(calcPulse(0)); } int main(){ // Thread cadence_thread(&updateCadence); // Thread mpu_thread(&mpuProcessing); // Thread soudaSerial_thread(&DataReceiveFromSouda); init(); while(1){ pc.printf("test\n\r"); // mpuProcessing(); sonarCalc(); RollAlarm(); DataReceiveFromSouda(); updateCadence(); WriteDatas(); // WriteServo(); } }