Ian Hua / Quadcopter-mbedRTOS
Committer:
pHysiX
Date:
Thu May 08 13:00:50 2014 +0000
Revision:
24:54a8cdf17378
Parent:
22:ef8aa9728013
Child:
27:18b6580eb0b1
Stable mode works. Already begun to start implementing MODE SWITCHING

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pHysiX 22:ef8aa9728013 1 /* File: Task1.cpp
pHysiX 22:ef8aa9728013 2 * Author: Trung Tin Ian HUA
pHysiX 22:ef8aa9728013 3 * Date: May 2014
pHysiX 22:ef8aa9728013 4 * Purpose: Thread1: Code to read Yaw Pitch Roll angles from MPU6050 DMP.
pHysiX 22:ef8aa9728013 5 * Settings: 100Hz
pHysiX 22:ef8aa9728013 6 */
pHysiX 2:ab967d7b4346 7
pHysiX 1:43f8ac7ca6d7 8 #include "Task1.h"
pHysiX 1:43f8ac7ca6d7 9 #include "setup.h"
pHysiX 1:43f8ac7ca6d7 10
pHysiX 2:ab967d7b4346 11 /* MPU6050 control/status variables: */
pHysiX 2:ab967d7b4346 12 uint8_t mpuIntStatus; // holds actual interrupt status byte from MPU
pHysiX 2:ab967d7b4346 13 uint16_t fifoCount; // count of all bytes currently in FIFO
pHysiX 21:b642c18eccd1 14 uint8_t fifoBuffer[64]; // FIFO storage buffer
pHysiX 1:43f8ac7ca6d7 15
pHysiX 2:ab967d7b4346 16 /* Orientation/motion variables: */
pHysiX 2:ab967d7b4346 17 Quaternion q; // [w, x, y, z] quaternion container
pHysiX 2:ab967d7b4346 18 VectorFloat gravity; // [x, y, z] gravity vector
pHysiX 2:ab967d7b4346 19 float ypr[3]; // [yaw, pitch, roll] yaw/pitch/roll container and gravity vector
pHysiX 24:54a8cdf17378 20 float ypr_1[3];
pHysiX 22:ef8aa9728013 21
pHysiX 22:ef8aa9728013 22 float altitude, temperature;
pHysiX 22:ef8aa9728013 23 bool counterTask1 = false;
pHysiX 22:ef8aa9728013 24
pHysiX 22:ef8aa9728013 25 #ifndef M_PI
pHysiX 22:ef8aa9728013 26 #define M_PI 3.1415
pHysiX 22:ef8aa9728013 27 #endif
pHysiX 12:953d25061417 28
pHysiX 12:953d25061417 29 #ifdef ENABLE_COMPASS
pHysiX 12:953d25061417 30 //int compassX, compassY, compassZ;
pHysiX 12:953d25061417 31 double heading = 0;
pHysiX 12:953d25061417 32 #endif
pHysiX 12:953d25061417 33
pHysiX 1:43f8ac7ca6d7 34 // ================================================================
pHysiX 12:953d25061417 35 // === YPR ROUTINE ===
pHysiX 2:ab967d7b4346 36 // ================================================================
pHysiX 1:43f8ac7ca6d7 37 void Task1(void const *argument)
pHysiX 1:43f8ac7ca6d7 38 {
pHysiX 1:43f8ac7ca6d7 39 // reset interrupt flag and get INT_STATUS byte
pHysiX 1:43f8ac7ca6d7 40 mpuIntStatus = imu.getIntStatus();
pHysiX 1:43f8ac7ca6d7 41
pHysiX 1:43f8ac7ca6d7 42 // get current FIFO count
pHysiX 1:43f8ac7ca6d7 43 fifoCount = imu.getFIFOCount();
pHysiX 16:9072cd6fa8d1 44 //imu.debugSerial.printf("FIFO Count: %d\n", fifoCount);
pHysiX 1:43f8ac7ca6d7 45
pHysiX 22:ef8aa9728013 46 // check for overflow
pHysiX 22:ef8aa9728013 47 // Only keep a max of 2 packets in buffer.
pHysiX 18:af657c4c3944 48 if ((mpuIntStatus & 0x10) || fifoCount > 84) {
pHysiX 1:43f8ac7ca6d7 49 // reset so we can continue cleanly
pHysiX 1:43f8ac7ca6d7 50 imu.resetFIFO();
pHysiX 1:43f8ac7ca6d7 51 imu.debugSerial.printf("FIFO overflow!");
pHysiX 1:43f8ac7ca6d7 52
pHysiX 1:43f8ac7ca6d7 53 // otherwise, check for DMP data ready interrupt (this should happen frequently)
pHysiX 1:43f8ac7ca6d7 54 } else if (mpuIntStatus & 0x02) {
pHysiX 1:43f8ac7ca6d7 55 // wait for correct available data length, should be a VERY short wait
pHysiX 1:43f8ac7ca6d7 56 while (fifoCount < packetSize) fifoCount = imu.getFIFOCount();
pHysiX 1:43f8ac7ca6d7 57
pHysiX 21:b642c18eccd1 58 while (fifoCount > 41) {
pHysiX 21:b642c18eccd1 59 // read a packet from FIFO
pHysiX 21:b642c18eccd1 60 imu.getFIFOBytes(fifoBuffer, packetSize);
pHysiX 1:43f8ac7ca6d7 61
pHysiX 21:b642c18eccd1 62 // track FIFO count here in case there is > 1 packet available
pHysiX 21:b642c18eccd1 63 // (this lets us immediately read more without waiting for an interrupt)
pHysiX 21:b642c18eccd1 64 fifoCount -= packetSize;
pHysiX 18:af657c4c3944 65 }
pHysiX 1:43f8ac7ca6d7 66
pHysiX 1:43f8ac7ca6d7 67 // display YPR angles in degrees
pHysiX 1:43f8ac7ca6d7 68 imu.dmpGetQuaternion(&q, fifoBuffer);
pHysiX 1:43f8ac7ca6d7 69 imu.dmpGetGravity(&gravity, &q);
pHysiX 1:43f8ac7ca6d7 70 imu.dmpGetYawPitchRoll(ypr, &q, &gravity);
pHysiX 21:b642c18eccd1 71
pHysiX 21:b642c18eccd1 72 ypr[0] = ypr[0] * 180/M_PI;
pHysiX 21:b642c18eccd1 73 ypr[1] = ypr[1] * 180/M_PI;
pHysiX 21:b642c18eccd1 74 ypr[2] = ypr[2] * 180/M_PI;
pHysiX 12:953d25061417 75
pHysiX 12:953d25061417 76 /*
pHysiX 22:ef8aa9728013 77 if (compass.getDataReady()) {
pHysiX 22:ef8aa9728013 78 // compass.getValues(&compass_x, &compass_y, &compass_z);
pHysiX 22:ef8aa9728013 79 heading = compass.getHeadingXY() * 180/M_PI;
pHysiX 22:ef8aa9728013 80 }
pHysiX 12:953d25061417 81
pHysiX 22:ef8aa9728013 82 ypr[0] *= 0.98;
pHysiX 22:ef8aa9728013 83 ypr[0] += 0.02*heading;
pHysiX 22:ef8aa9728013 84 */
pHysiX 10:ef5fe86f67fe 85
pHysiX 3:605fbcb54e75 86 if (box_demo)
pHysiX 21:b642c18eccd1 87 BT.printf("\nY%3.2f\nP%3.2f\nR%3.2f\n", ypr[0] - ypr_offset[0],
pHysiX 21:b642c18eccd1 88 ypr[1] - ypr_offset[1], ypr[2] - ypr_offset[2]);
pHysiX 17:18c3bd016e49 89
pHysiX 21:b642c18eccd1 90 counterTask1 = true;
pHysiX 1:43f8ac7ca6d7 91 }
pHysiX 2:ab967d7b4346 92 }