一応着地判定できます。
Dependencies: mbed
main.cpp
00001 #include "mbed.h" 00002 #include "math.h" 00003 #include "MPU9250.h" 00004 00005 float sum = 0; 00006 uint32_t sumCount = 0; 00007 char buffer[14]; 00008 MPU9250 mpu9250; 00009 Timer t; 00010 Serial pc(USBTX, USBRX); // tx, rx 00011 double acx,acy,acz; 00012 int k = 0,l = 0; 00013 00014 int main() 00015 { 00016 pc.baud(9600); 00017 00018 //Set up I2C 00019 i2c.frequency(400000); // use fast (400 kHz) I2C ← KPのは100kHzじゃなかった? 00020 00021 pc.printf("CPU SystemCoreClock is %d Hz\r\n", SystemCoreClock); 00022 00023 t.start(); 00024 00025 // Read the WHO_AM_I register, this is a good test of communication 00026 uint8_t whoami = mpu9250.readByte(MPU9250_ADDRESS, WHO_AM_I_MPU9250); // Read WHO_AM_I register for MPU-9250 00027 pc.printf("I AM 0x%x\n\r", whoami); pc.printf("I SHOULD BE 0x71\n\r"); 00028 00029 if (whoami == 0x71) // WHO_AM_I should always be 0x68 00030 { 00031 pc.printf("MPU9250 WHO_AM_I is 0x%x\n\r", whoami); 00032 pc.printf("MPU9250 is online...\n\r"); 00033 sprintf(buffer, "0x%x", whoami); 00034 wait(1); 00035 00036 mpu9250.resetMPU9250(); // Reset registers to default in preparation for device calibration 00037 mpu9250.MPU9250SelfTest(SelfTest); // Start by performing self test and reporting values 00038 pc.printf("x-axis self test: acceleration trim within : %f % of factory value\n\r", SelfTest[0]); 00039 pc.printf("y-axis self test: acceleration trim within : %f % of factory value\n\r", SelfTest[1]); 00040 pc.printf("z-axis self test: acceleration trim within : %f % of factory value\n\r", SelfTest[2]); 00041 pc.printf("x-axis self test: gyration trim within : %f % of factory value\n\r", SelfTest[3]); 00042 pc.printf("y-axis self test: gyration trim within : %f % of factory value\n\r", SelfTest[4]); 00043 pc.printf("z-axis self test: gyration trim within : %f % of factory value\n\r", SelfTest[5]); 00044 mpu9250.calibrateMPU9250(gyroBias, accelBias); // Calibrate gyro and accelerometers, load biases in bias registers 00045 pc.printf("x gyro bias = %f\n\r", gyroBias[0]); 00046 pc.printf("y gyro bias = %f\n\r", gyroBias[1]); 00047 pc.printf("z gyro bias = %f\n\r", gyroBias[2]); 00048 pc.printf("x accel bias = %f\n\r", accelBias[0]); 00049 pc.printf("y accel bias = %f\n\r", accelBias[1]); 00050 pc.printf("z accel bias = %f\n\r", accelBias[2]); 00051 wait(2); 00052 mpu9250.initMPU9250(); 00053 pc.printf("MPU9250 initialized for active data mode....\n\r"); // Initialize device for active mode read of acclerometer, gyroscope, and temperature 00054 mpu9250.initAK8963(magCalibration); 00055 pc.printf("AK8963 initialized for active data mode....\n\r"); // Initialize device for active mode read of magnetometer 00056 pc.printf("Accelerometer full-scale range = %f g\n\r", 2.0f*(float)(1<<Ascale)); 00057 pc.printf("Gyroscope full-scale range = %f deg/s\n\r", 250.0f*(float)(1<<Gscale)); 00058 if(Mscale == 0) pc.printf("Magnetometer resolution = 14 bits\n\r"); 00059 if(Mscale == 1) pc.printf("Magnetometer resolution = 16 bits\n\r"); 00060 if(Mmode == 2) pc.printf("Magnetometer ODR = 8 Hz\n\r"); 00061 if(Mmode == 6) pc.printf("Magnetometer ODR = 100 Hz\n\r"); 00062 wait(1); 00063 } 00064 else 00065 { 00066 pc.printf("Could not connect to MPU9250: \n\r"); 00067 pc.printf("%#x \n", whoami); 00068 sprintf(buffer, "WHO_AM_I 0x%x", whoami); 00069 00070 while(1) ; // Loop forever if communication doesn't happen 00071 } 00072 mpu9250.getAres(); // Get accelerometer sensitivity 00073 mpu9250.getGres(); // Get gyro sensitivity 00074 mpu9250.getMres(); // Get magnetometer sensitivity 00075 pc.printf("Accelerometer sensitivity is %f LSB/g \n\r", 1.0f/aRes); 00076 pc.printf("Gyroscope sensitivity is %f LSB/deg/s \n\r", 1.0f/gRes); 00077 pc.printf("Magnetometer sensitivity is %f LSB/G \n\r", 1.0f/mRes); 00078 magbias[0] = +470.; // User environmental x-axis correction in milliGauss, should be automatically calculated 00079 magbias[1] = +120.; // User environmental x-axis correction in milliGauss 00080 magbias[2] = +125.; // User environmental x-axis correction in milliGauss 00081 00082 while(1) { 00083 00084 // If intPin goes high, all data registers have new data 00085 if(mpu9250.readByte(MPU9250_ADDRESS, INT_STATUS) & 0x01) { // On interrupt, check if data ready interrupt 00086 00087 mpu9250.readAccelData(accelCount); // Read the x/y/z adc values 00088 // Now we'll calculate the accleration value into actual g's 00089 ax = (float)accelCount[0]*aRes - accelBias[0]; // get actual g value, this depends on scale being set 00090 ay = (float)accelCount[1]*aRes - accelBias[1]; 00091 az = (float)accelCount[2]*aRes - accelBias[2]; 00092 00093 mpu9250.readGyroData(gyroCount); // Read the x/y/z adc values 00094 // Calculate the gyro value into actual degrees per second 00095 gx = (float)gyroCount[0]*gRes - gyroBias[0]; // get actual gyro value, this depends on scale being set 00096 gy = (float)gyroCount[1]*gRes - gyroBias[1]; 00097 gz = (float)gyroCount[2]*gRes - gyroBias[2]; 00098 00099 mpu9250.readMagData(magCount); // Read the x/y/z adc values 00100 // Calculate the magnetometer values in milliGauss 00101 // Include factory calibration per data sheet and user environmental corrections 00102 mx = (float)magCount[0]*mRes*magCalibration[0] - magbias[0]; // get actual magnetometer value, this depends on scale being set 00103 my = (float)magCount[1]*mRes*magCalibration[1] - magbias[1]; 00104 mz = (float)magCount[2]*mRes*magCalibration[2] - magbias[2]; 00105 } 00106 00107 Now = t.read_us(); 00108 deltat = (float)((Now - lastUpdate)/1000000.0f) ; // set integration time by time elapsed since last filter update 00109 lastUpdate = Now; 00110 00111 sum += deltat; 00112 sumCount++; 00113 00114 // if(lastUpdate - firstUpdate > 10000000.0f) { 00115 // beta = 0.04; // decrease filter gain after stabilized 00116 // zeta = 0.015; // increasey bias drift gain after stabilized 00117 // } 00118 00119 // Pass gyro rate as rad/s 00120 // mpu9250.MadgwickQuaternionUpdate(ax, ay, az, gx*PI/180.0f, gy*PI/180.0f, gz*PI/180.0f, my, mx, mz); 00121 mpu9250.MahonyQuaternionUpdate(ax, ay, az, gx*PI/180.0f, gy*PI/180.0f, gz*PI/180.0f, my, mx, mz); 00122 //出力されるのはここから 00123 // Serial print and/or display at 0.5 s rate independent of data rates 00124 delt_t = t.read_ms() - count; 00125 if (delt_t > 500) { // update LCD once per half-second independent of read rate 00126 00127 acx = 1000*ax; 00128 acy = 1000*ay; 00129 acz = 1000*az; 00130 00131 pc.printf(" ax = %f", 1000*ax); 00132 pc.printf(" ay = %f", 1000*ay); 00133 pc.printf(" az = %f mg\n\r", 1000*az); 00134 00135 pc.printf(" gx = %f", gx); 00136 pc.printf(" gy = %f", gy); 00137 pc.printf(" gz = %f deg/s\n\r", gz); 00138 00139 pc.printf(" mx = %f", mx); 00140 pc.printf(" my = %f", my); 00141 pc.printf(" mz = %f mG\n\r", mz); 00142 00143 /*//温度/* 00144 tempCount = mpu9250.readTempData(); // Read the adc values 00145 temperature = ((float) tempCount) / 333.87f + 21.0f; // Temperature in degrees Centigrade 00146 pc.printf(" temperature = %f C\n\r", temperature); 00147 */ 00148 00149 pc.printf("q0 = %f\n\r", q[0]); 00150 pc.printf("q1 = %f\n\r", q[1]); 00151 pc.printf("q2 = %f\n\r", q[2]); 00152 pc.printf("q3 = %f\n\r", q[3]); 00153 00154 // Define output variables from updated quaternion---these are Tait-Bryan angles, commonly used in aircraft orientation. 00155 // In this coordinate system, the positive z-axis is down toward Earth. 00156 // Yaw is the angle between Sensor x-axis and Earth magnetic North (or true North if corrected for local declination, looking down on the sensor positive yaw is counterclockwise. 00157 // Pitch is angle between sensor x-axis and Earth ground plane, toward the Earth is positive, up toward the sky is negative. 00158 // Roll is angle between sensor y-axis and Earth ground plane, y-axis up is positive roll. 00159 // These arise from the definition of the homogeneous rotation matrix constructed from quaternions. 00160 // Tait-Bryan angles as well as Euler angles are non-commutative; that is, the get the correct orientation the rotations must be 00161 // applied in the correct order which for this configuration is yaw, pitch, and then roll. 00162 // For more see http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles which has additional links. 00163 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]); 00164 pitch = -asin(2.0f * (q[1] * q[3] - q[0] * q[2])); 00165 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]); 00166 pitch *= 180.0f / PI; 00167 yaw *= 180.0f / PI; 00168 yaw -= 13.8f; // Declination at Danville, California is 13 degrees 48 minutes and 47 seconds on 2014-04-04 00169 roll *= 180.0f / PI; 00170 00171 pc.printf("Yaw, Pitch, Roll: %f %f %f\n\r", yaw, pitch, roll); 00172 pc.printf("average rate = %f\n\r", (float) sumCount/sum); 00173 // sprintf(buffer, "YPR: %f %f %f", yaw, pitch, roll); 00174 // sprintf(buffer, "rate = %f", (float) sumCount/sum); 00175 00176 myled= !myled; 00177 count = t.read_ms(); 00178 00179 if(count > 1<<21) { 00180 t.start(); // start the timer over again if ~30 minutes has passed 00181 count = 0; 00182 deltat= 0; 00183 lastUpdate = t.read_us(); 00184 } 00185 sum = 0; 00186 sumCount = 0; 00187 00188 int flag = 0; 00189 //落下判定のつもり 00190 while(flag = (acz > 800 && acx < 150 && acx > -150 && acy < 300 && acy > -300)){ 00191 if(flag = 0)break; 00192 pc.printf("*********************\n\r"); 00193 mpu9250.getAres(); // Get accelerometer sensitivity 00194 mpu9250.getGres(); // Get gyro sensitivity 00195 mpu9250.getMres(); // Get magnetometer sensitivity 00196 pc.printf("Accelerometer sensitivity is %f LSB/g \n\r", 1.0f/aRes); 00197 pc.printf("Gyroscope sensitivity is %f LSB/deg/s \n\r", 1.0f/gRes); 00198 pc.printf("Magnetometer sensitivity is %f LSB/G \n\r", 1.0f/mRes); 00199 magbias[0] = +470.; // User environmental x-axis correction in milliGauss, should be automatically calculated 00200 magbias[1] = +120.; // User environmental x-axis correction in milliGauss 00201 magbias[2] = +125.; // User environmental x-axis correction in milliGauss 00202 00203 while(1) { 00204 00205 // If intPin goes high, all data registers have new data 00206 if(mpu9250.readByte(MPU9250_ADDRESS, INT_STATUS) & 0x01) { // On interrupt, check if data ready interrupt 00207 00208 mpu9250.readAccelData(accelCount); // Read the x/y/z adc values 00209 // Now we'll calculate the accleration value into actual g's 00210 ax = (float)accelCount[0]*aRes - accelBias[0]; // get actual g value, this depends on scale being set 00211 ay = (float)accelCount[1]*aRes - accelBias[1]; 00212 az = (float)accelCount[2]*aRes - accelBias[2]; 00213 00214 mpu9250.readGyroData(gyroCount); // Read the x/y/z adc values 00215 // Calculate the gyro value into actual degrees per second 00216 gx = (float)gyroCount[0]*gRes - gyroBias[0]; // get actual gyro value, this depends on scale being set 00217 gy = (float)gyroCount[1]*gRes - gyroBias[1]; 00218 gz = (float)gyroCount[2]*gRes - gyroBias[2]; 00219 00220 mpu9250.readMagData(magCount); // Read the x/y/z adc values 00221 // Calculate the magnetometer values in milliGauss 00222 // Include factory calibration per data sheet and user environmental corrections 00223 mx = (float)magCount[0]*mRes*magCalibration[0] - magbias[0]; // get actual magnetometer value, this depends on scale being set 00224 my = (float)magCount[1]*mRes*magCalibration[1] - magbias[1]; 00225 mz = (float)magCount[2]*mRes*magCalibration[2] - magbias[2]; 00226 } 00227 00228 Now = t.read_us(); 00229 deltat = (float)((Now - lastUpdate)/1000000.0f) ; // set integration time by time elapsed since last filter update 00230 lastUpdate = Now; 00231 00232 sum += deltat; 00233 sumCount++; 00234 00235 // if(lastUpdate - firstUpdate > 10000000.0f) { 00236 // beta = 0.04; // decrease filter gain after stabilized 00237 // zeta = 0.015; // increasey bias drift gain after stabilized 00238 // } 00239 00240 // Pass gyro rate as rad/s 00241 // mpu9250.MadgwickQuaternionUpdate(ax, ay, az, gx*PI/180.0f, gy*PI/180.0f, gz*PI/180.0f, my, mx, mz); 00242 mpu9250.MahonyQuaternionUpdate(ax, ay, az, gx*PI/180.0f, gy*PI/180.0f, gz*PI/180.0f, my, mx, mz); 00243 //出力されるのはここから 00244 // Serial print and/or display at 0.5 s rate independent of data rates 00245 delt_t = t.read_ms() - count; 00246 if (delt_t > 500) { // update LCD once per half-second independent of read rate 00247 00248 acx = 1000*ax; 00249 acy = 1000*ay; 00250 acz = 1000*az; 00251 00252 break; 00253 /* 00254 pc.printf(" ax = %f", 1000*ax); 00255 pc.printf(" ay = %f", 1000*ay); 00256 pc.printf(" az = %f mg\n\r", 1000*az); 00257 00258 pc.printf(" gx = %f", gx); 00259 pc.printf(" gy = %f", gy); 00260 pc.printf(" gz = %f deg/s\n\r", gz); 00261 00262 pc.printf(" mx = %f", mx); 00263 pc.printf(" my = %f", my); 00264 pc.printf(" mz = %f mG\n\r", mz); 00265 */ 00266 } 00267 00268 /* 平均値とる方向性もなしで 00269 double ac[3] = {0}; 00270 do{ 00271 for(l;l < 3;l++){ 00272 for(k;k < 30;k += 0){ 00273 ac[l] += sqrt(pow(acx,2.0) + pow(acy,2.0) + pow(acz,2.0)); 00274 if(k < 28){ 00275 k++; 00276 pc.printf("************%d巡目%d回目***********\n\r",l,k); 00277 goto Getdata; 00278 }else k++; 00279 } 00280 k = 0; 00281 ac[l] /= 30; 00282 pc.printf("平均値は・・・%f\n\r",ac[l]); 00283 } 00284 l = 0; 00285 }while(ac[1] > ac[0] && ac[1] < ac[2]); 00286 pc.printf("ループから抜けた\n\r"); 00287 /* while(1) { 00288 myled = 1; 00289 wait(0.2); 00290 myled = 0; 00291 wait(0.2); 00292 ac[0] = sqrt(pow(acx,2.0) + pow(acy,2.0) + pow(acz,2.0)); 00293 if(ac[0] > 500){ 00294 pc.printf("平均値は・・・%f\n\r",ac[0]); 00295 break; 00296 } 00297 } 00298 */ 00299 } 00300 } 00301 } 00302 } 00303 }
Generated on Fri Aug 5 2022 20:09:10 by 1.7.2