Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: Servo mbed-rtos mbed
Fork of Turtlecase by
main.cpp
00001 ////////////////////////////////////////////////////////////////// 00002 // project: TurtleBot Project // 00003 // code v.: 1.0 // 00004 // board : NUCLEO-F303KB // 00005 // date : 20/2/2018 // 00006 // code by: Coding on Earth by Humans // 00007 ////////////////////////////////////////////////////////////////// 00008 00009 ///////////////////////// init //////////////////////////////// 00010 ////////////////////////////////////////////////////////////////// 00011 #include "mbed.h" 00012 #include "rtos.h" 00013 00014 Serial pc(USBTX, USBRX); 00015 00016 Thread thread1; //control servo left 00017 Thread thread2; //control servo right 00018 Thread thread3; //read data from IMU 00019 00020 ///////////////////////// IMU //////////////////////////////// 00021 ////////////////////////////////////////////////////////////////// 00022 #include "MPU9250.h" 00023 00024 float sum = 0; 00025 uint32_t sumCount = 0; 00026 char buffer[14]; 00027 float origin = 0; 00028 00029 MPU9250 mpu9250; 00030 Timer t; 00031 00032 00033 ///////////////////////// Servo //////////////////////////////// 00034 ////////////////////////////////////////////////////////////////// 00035 #include "Servo.h" 00036 Servo Servo1(D10); 00037 Servo Servo2(D6); 00038 Servo Servo3(D8); 00039 Servo Servo4(D9); 00040 /* 00041 int pos_up_start; 00042 int pos_up_end; 00043 int pos_down_start; 00044 int pos_down_end;*/ 00045 00046 int pos_down_start = 1400; 00047 int pos_down_end = 1600; 00048 int pos_up_start = 1000; 00049 int pos_up_end = 1600; 00050 00051 ///////////////////////// prototype func /////////////////////// 00052 ////////////////////////////////////////////////////////////////// 00053 void myservoLeft(); 00054 void myservoRight(); 00055 void IMU(); 00056 00057 ///////////////////////// main //////////////////////////// 00058 ////////////////////////////////////////////////////////////////// 00059 int main() 00060 { 00061 thread1.start(myservoLeft); 00062 thread2.start(myservoRight); 00063 IMU(); 00064 /* while(1) 00065 { 00066 printf("Hello World! Turtlebot is READY\n"); 00067 printf("case 1-5\n"); 00068 switch(pc.getc()) 00069 { 00070 case '1': 00071 pos_down_start = 1400; 00072 pos_down_end = 1700; 00073 pos_up_start = 1000; 00074 pos_up_end = 1700; 00075 break; 00076 case '2': 00077 pos_down_start = 1400; 00078 pos_down_end = 1600; 00079 pos_up_start = 1000; 00080 pos_up_end = 1600; 00081 break; 00082 case '3': 00083 pos_down_start = 1400; 00084 pos_down_end = 1650; 00085 pos_up_start = 1000; 00086 pos_up_end = 1500; 00087 break; 00088 case '4': 00089 pos_down_start = 1400; 00090 pos_down_end = 1700; 00091 pos_up_start = 1000; 00092 pos_up_end = 1650; 00093 break; 00094 case '5': 00095 pos_down_start = 1400; 00096 pos_down_end = 1600; 00097 pos_up_start = 1000; 00098 pos_up_end = 1550; 00099 break; 00100 } 00101 printf("position down motor start = %d\n", pos_down_start); 00102 printf("position down motor end = %d\n", pos_down_end); 00103 printf("position up motor start = %d\n", pos_up_start); 00104 printf("position up motor end = %d\n", pos_up_end); 00105 thread1.start(myservoLeft); 00106 thread2.start(myservoRight); 00107 thread3.start(IMU); 00108 } */ 00109 } 00110 00111 00112 ///////////////////////// myservoLeft ///////////////////////// 00113 ////////////////////////////////////////////////////////////////// 00114 void myservoLeft() 00115 { 00116 for(int n = 0; n <= 5; n += 1) 00117 { 00118 Servo1.Enable(1000,20000); 00119 Servo2.Disable(); 00120 00121 for (int pos = pos_down_start; pos <= pos_down_end; pos += 5) 00122 { 00123 Servo1.SetPosition(pos); 00124 wait(0.01); 00125 } 00126 00127 Servo2.Enable(1000,20000); 00128 Servo1.Disable(); 00129 00130 for (int pos = pos_up_start; pos <= pos_up_end; pos += 5) 00131 { 00132 Servo2.SetPosition(pos); 00133 wait(0.01); 00134 } 00135 00136 Servo1.Enable(1000,20000); 00137 Servo2.Disable(); 00138 00139 for (int pos = pos_down_end; pos >= pos_down_start; pos -= 5) 00140 { 00141 Servo1.SetPosition(pos); 00142 wait(0.01); 00143 } 00144 00145 Servo2.Enable(1000,20000); 00146 Servo1.Disable(); 00147 00148 for (int pos = pos_up_end; pos >= pos_up_start; pos -= 5) 00149 { 00150 Servo2.SetPosition(pos); 00151 wait(0.01); 00152 } 00153 } 00154 } 00155 00156 ///////////////////////// myservoRight /////////////////////// 00157 ////////////////////////////////////////////////////////////////// 00158 void myservoRight() 00159 { 00160 for(int n = 0; n <= 5; n += 1) 00161 { 00162 Servo3.Enable(1000,20000); 00163 Servo4.Disable(); 00164 00165 for (int pos = pos_down_start; pos <= pos_down_end; pos += 5) 00166 { 00167 Servo3.SetPosition(pos); 00168 wait(0.01); 00169 } 00170 00171 Servo4.Enable(1000,20000); 00172 Servo3.Disable(); 00173 00174 for (int pos = pos_up_start; pos <= pos_up_end; pos += 5) 00175 { 00176 Servo4.SetPosition(pos); 00177 wait(0.01); 00178 } 00179 00180 Servo3.Enable(1000,20000); 00181 Servo4.Disable(); 00182 00183 for (int pos = pos_down_end; pos >= pos_down_start; pos -= 5) 00184 { 00185 Servo3.SetPosition(pos); 00186 wait(0.01); 00187 } 00188 00189 Servo4.Enable(1000,20000); 00190 Servo3.Disable(); 00191 00192 for (int pos = pos_up_end; pos >= pos_up_start; pos -= 5) 00193 { 00194 Servo4.SetPosition(pos); 00195 wait(0.01); 00196 } 00197 } 00198 } 00199 00200 ///////////////////////// IMU /////////////////////// 00201 ////////////////////////////////////////////////////////////////// 00202 void IMU() 00203 { 00204 //Set up I2C 00205 i2c.frequency(400000); // use fast (400 kHz) I2C 00206 00207 //pc.printf("CPU SystemCoreClock is %d Hz\r\n", SystemCoreClock); 00208 t.start(); 00209 00210 // Read the WHO_AM_I register, this is a good test of communication 00211 uint8_t whoami = mpu9250.readByte(MPU9250_ADDRESS, WHO_AM_I_MPU9250); // Read WHO_AM_I register for MPU-9250 00212 //pc.printf("I AM 0x%x\n\r", whoami); pc.printf("I SHOULD BE 0x68\n\r"); 00213 00214 if (whoami == 0x73 ) // WHO_AM_I should always be 0x68 00215 { 00216 //pc.printf("MPU9250 WHO_AM_I is 0x%x\n\r", whoami); 00217 //pc.printf("MPU9250 is online...\n\r"); 00218 sprintf(buffer, "0x%x", whoami); 00219 wait(1); 00220 00221 mpu9250.resetMPU9250(); // Reset registers to default in preparation for device calibration 00222 mpu9250.MPU9250SelfTest(SelfTest); // Start by performing self test and reporting values 00223 //pc.printf("x-axis self test: acceleration trim within : %f % of factory value\n\r", SelfTest[0]); 00224 //pc.printf("y-axis self test: acceleration trim within : %f % of factory value\n\r", SelfTest[1]); 00225 //pc.printf("z-axis self test: acceleration trim within : %f % of factory value\n\r", SelfTest[2]); 00226 //pc.printf("x-axis self test: gyration trim within : %f % of factory value\n\r", SelfTest[3]); 00227 //pc.printf("y-axis self test: gyration trim within : %f % of factory value\n\r", SelfTest[4]); 00228 //pc.printf("z-axis self test: gyration trim within : %f % of factory value\n\r", SelfTest[5]); 00229 mpu9250.calibrateMPU9250(gyroBias, accelBias); // Calibrate gyro and accelerometers, load biases in bias registers 00230 //pc.printf("x gyro bias = %f\n\r", gyroBias[0]); 00231 //pc.printf("y gyro bias = %f\n\r", gyroBias[1]); 00232 //pc.printf("z gyro bias = %f\n\r", gyroBias[2]); 00233 //pc.printf("x accel bias = %f\n\r", accelBias[0]); 00234 //pc.printf("y accel bias = %f\n\r", accelBias[1]); 00235 //pc.printf("z accel bias = %f\n\r", accelBias[2]); 00236 wait(2); 00237 mpu9250.initMPU9250(); 00238 //pc.printf("MPU9250 initialized for active data mode....\n\r"); // Initialize device for active mode read of acclerometer, gyroscope, and temperature 00239 mpu9250.initAK8963(magCalibration); 00240 //pc.printf("AK8963 initialized for active data mode....\n\r"); // Initialize device for active mode read of magnetometer 00241 //pc.printf("Accelerometer full-scale range = %f g\n\r", 2.0f*(float)(1<<Ascale)); 00242 //pc.printf("Gyroscope full-scale range = %f deg/s\n\r", 250.0f*(float)(1<<Gscale)); 00243 00244 if(Mscale == 0) pc.printf("Magnetometer resolution = 14 bits\n\r"); 00245 if(Mscale == 1) pc.printf("Magnetometer resolution = 16 bits\n\r"); 00246 if(Mmode == 2) pc.printf("Magnetometer ODR = 8 Hz\n\r"); 00247 if(Mmode == 6) pc.printf("Magnetometer ODR = 100 Hz\n\r"); 00248 wait(1); 00249 } 00250 else 00251 { 00252 pc.printf("Could not connect to MPU9250: \n\r"); 00253 pc.printf("%#x \n", whoami); 00254 00255 sprintf(buffer, "WHO_AM_I 0x%x", whoami); 00256 00257 while(1) ; // Loop forever if communication doesn't happen 00258 } 00259 00260 mpu9250.getAres(); // Get accelerometer sensitivity 00261 mpu9250.getGres(); // Get gyro sensitivity 00262 mpu9250.getMres(); // Get magnetometer sensitivity 00263 //pc.printf("Accelerometer sensitivity is %f LSB/g \n\r", 1.0f/aRes); 00264 //pc.printf("Gyroscope sensitivity is %f LSB/deg/s \n\r", 1.0f/gRes); 00265 //pc.printf("Magnetometer sensitivity is %f LSB/G \n\r", 1.0f/mRes); 00266 magbias[0] = +470.; // User environmental x-axis correction in milliGauss, should be automatically calculated 00267 magbias[1] = +120.; // User environmental x-axis correction in milliGauss 00268 magbias[2] = +125.; // User environmental x-axis correction in milliGauss 00269 00270 while(1) 00271 { 00272 // If intPin goes high, all data registers have new data 00273 if(mpu9250.readByte(MPU9250_ADDRESS, INT_STATUS) & 0x01) { // On interrupt, check if data ready interrupt 00274 00275 mpu9250.readAccelData(accelCount); // Read the x/y/z adc values 00276 // Now we'll calculate the accleration value into actual g's 00277 ax = (float)accelCount[0]*aRes - accelBias[0]; // get actual g value, this depends on scale being set 00278 ay = (float)accelCount[1]*aRes - accelBias[1]; 00279 az = (float)accelCount[2]*aRes - accelBias[2]; 00280 00281 mpu9250.readGyroData(gyroCount); // Read the x/y/z adc values 00282 // Calculate the gyro value into actual degrees per second 00283 gx = (float)gyroCount[0]*gRes - gyroBias[0]; // get actual gyro value, this depends on scale being set 00284 gy = (float)gyroCount[1]*gRes - gyroBias[1]; 00285 gz = (float)gyroCount[2]*gRes - gyroBias[2]; 00286 00287 mpu9250.readMagData(magCount); // Read the x/y/z adc values 00288 // Calculate the magnetometer values in milliGauss 00289 // Include factory calibration per data sheet and user environmental corrections 00290 mx = (float)magCount[0]*mRes*magCalibration[0] - magbias[0]; // get actual magnetometer value, this depends on scale being set 00291 my = (float)magCount[1]*mRes*magCalibration[1] - magbias[1]; 00292 mz = (float)magCount[2]*mRes*magCalibration[2] - magbias[2]; 00293 } 00294 00295 Now = t.read_us(); 00296 deltat = (float)((Now - lastUpdate)/1000000.0f) ; // set integration time by time elapsed since last filter update 00297 lastUpdate = Now; 00298 00299 sum += deltat; 00300 sumCount++; 00301 00302 //if(lastUpdate - firstUpdate > 10000000.0f) 00303 //{ 00304 //beta = 0.04; // decrease filter gain after stabilized 00305 //zeta = 0.015; // increasey bias drift gain after stabilized 00306 //} 00307 00308 //Pass gyro rate as rad/s 00309 //mpu9250.MadgwickQuaternionUpdate(ax, ay, az, gx*PI/180.0f, gy*PI/180.0f, gz*PI/180.0f, my, mx, mz); 00310 mpu9250.MahonyQuaternionUpdate(ax, ay, az, gx*PI/180.0f, gy*PI/180.0f, gz*PI/180.0f, my, mx, mz); 00311 00312 //Serial print and/or display at 0.5 s rate independent of data rates 00313 delt_t = t.read_ms() - count; 00314 00315 if (delt_t > 50) 00316 { // update LCD once per half-second independent of read rate 00317 00318 //pc.printf("ax = %f", 1000*ax); 00319 //pc.printf(" ay = %f", 1000*ay); 00320 //pc.printf(" az = %f mg\n\r", 1000*az); 00321 00322 //pc.printf("gx = %f", gx); 00323 //pc.printf(" gy = %f", gy); 00324 //pc.printf(" gz = %f deg/s\n\r", gz); 00325 00326 //pc.printf("gx = %f", mx); 00327 //pc.printf(" gy = %f", my); 00328 //pc.printf(" gz = %f mG\n\r", mz); 00329 00330 //tempCount = mpu9250.readTempData(); // Read the adc values 00331 //temperature = ((float) tempCount) / 333.87f + 21.0f; // Temperature in degrees Centigrade 00332 //pc.printf(" temperature = %f C\n\r", temperature); 00333 00334 //pc.printf("q0 = %f\n\r", q[0]); 00335 //pc.printf("q1 = %f\n\r", q[1]); 00336 //pc.printf("q2 = %f\n\r", q[2]); 00337 //pc.printf("q3 = %f\n\r", q[3]); 00338 00339 00340 // Define output variables from updated quaternion---these are Tait-Bryan angles, commonly used in aircraft orientation. 00341 // In this coordinate system, the positive z-axis is down toward Earth. 00342 // 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. 00343 // Pitch is angle between sensor x-axis and Earth ground plane, toward the Earth is positive, up toward the sky is negative. 00344 // Roll is angle between sensor y-axis and Earth ground plane, y-axis up is positive roll. 00345 // These arise from the definition of the homogeneous rotation matrix constructed from quaternions. 00346 // Tait-Bryan angles as well as Euler angles are non-commutative; that is, the get the correct orientation the rotations must be 00347 // applied in the correct order which for this configuration is yaw, pitch, and then roll. 00348 // For more see http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles which has additional links. 00349 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]); 00350 pitch = -asin(2.0f * (q[1] * q[3] - q[0] * q[2])); 00351 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]); 00352 pitch *= 180.0f / PI; 00353 yaw *= 180.0f / PI; 00354 yaw -= 13.8f; // Declination at Danville, California is 13 degrees 48 minutes and 47 seconds on 2014-04-04 00355 roll *= 180.0f / PI; 00356 00357 pc.printf("%f %f %f %f \n\r",roll, pitch, yaw, origin); 00358 //pc.printf("average rate = %f\n\r", (float) sumCount/sum); 00359 //sprintf(buffer, "YPR: %f %f %f", yaw, pitch, roll); 00360 //lcd.printString(buffer, 0, 4); 00361 //sprintf(buffer, "rate = %f", (float) sumCount/sum); 00362 //lcd.printString(buffer, 0, 5); 00363 00364 myled= !myled; 00365 count = t.read_ms(); 00366 00367 if(count > 1<<21) 00368 { 00369 t.start(); // start the timer over again if ~30 minutes has passed 00370 count = 0; 00371 deltat= 0; 00372 lastUpdate = t.read_us(); 00373 } 00374 sum = 0; 00375 sumCount = 0; 00376 } 00377 } 00378 }
Generated on Wed Jul 13 2022 07:14:08 by
1.7.2
