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.
RTOS-Threads/src/Task1.cpp@3:605fbcb54e75, 2014-04-29 (annotated)
- Committer:
- pHysiX
- Date:
- Tue Apr 29 14:53:32 2014 +0000
- Revision:
- 3:605fbcb54e75
- Parent:
- 2:ab967d7b4346
- Child:
- 5:4879ef0e6d41
Fully implemented system. Need to test stability of RTOS, and to make sure that values are correct. ; Rate Mode only.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pHysiX | 2:ab967d7b4346 | 1 | /* Sample & YPR */ |
pHysiX | 2:ab967d7b4346 | 2 | |
pHysiX | 1:43f8ac7ca6d7 | 3 | #include "Task1.h" |
pHysiX | 1:43f8ac7ca6d7 | 4 | #include "setup.h" |
pHysiX | 1:43f8ac7ca6d7 | 5 | |
pHysiX | 2:ab967d7b4346 | 6 | /* MPU6050 control/status variables: */ |
pHysiX | 2:ab967d7b4346 | 7 | uint8_t mpuIntStatus; // holds actual interrupt status byte from MPU |
pHysiX | 2:ab967d7b4346 | 8 | uint16_t fifoCount; // count of all bytes currently in FIFO |
pHysiX | 2:ab967d7b4346 | 9 | uint8_t fifoBuffer[64]; // FIFO storage buffer |
pHysiX | 1:43f8ac7ca6d7 | 10 | |
pHysiX | 2:ab967d7b4346 | 11 | /* Orientation/motion variables: */ |
pHysiX | 2:ab967d7b4346 | 12 | Quaternion q; // [w, x, y, z] quaternion container |
pHysiX | 2:ab967d7b4346 | 13 | VectorFloat gravity; // [x, y, z] gravity vector |
pHysiX | 2:ab967d7b4346 | 14 | float ypr[3]; // [yaw, pitch, roll] yaw/pitch/roll container and gravity vector |
pHysiX | 1:43f8ac7ca6d7 | 15 | |
pHysiX | 1:43f8ac7ca6d7 | 16 | #ifndef M_PI |
pHysiX | 1:43f8ac7ca6d7 | 17 | #define M_PI 3.1415 |
pHysiX | 1:43f8ac7ca6d7 | 18 | #endif |
pHysiX | 1:43f8ac7ca6d7 | 19 | |
pHysiX | 1:43f8ac7ca6d7 | 20 | // ================================================================ |
pHysiX | 1:43f8ac7ca6d7 | 21 | // === INTERRUPT DETECTION ROUTINE === |
pHysiX | 1:43f8ac7ca6d7 | 22 | // ================================================================ |
pHysiX | 1:43f8ac7ca6d7 | 23 | volatile bool mpuInterrupt = false; // indicates whether MPU interrupt pin has gone high |
pHysiX | 1:43f8ac7ca6d7 | 24 | void dmpDataReady() |
pHysiX | 1:43f8ac7ca6d7 | 25 | { |
pHysiX | 1:43f8ac7ca6d7 | 26 | mpuInterrupt = true; |
pHysiX | 1:43f8ac7ca6d7 | 27 | } |
pHysiX | 2:ab967d7b4346 | 28 | |
pHysiX | 2:ab967d7b4346 | 29 | |
pHysiX | 2:ab967d7b4346 | 30 | // ================================================================ |
pHysiX | 2:ab967d7b4346 | 31 | // === SAMPLE & CONVERSION ROUTINE === |
pHysiX | 2:ab967d7b4346 | 32 | // ================================================================ |
pHysiX | 1:43f8ac7ca6d7 | 33 | void Task1(void const *argument) |
pHysiX | 1:43f8ac7ca6d7 | 34 | { |
pHysiX | 1:43f8ac7ca6d7 | 35 | // reset interrupt flag and get INT_STATUS byte |
pHysiX | 1:43f8ac7ca6d7 | 36 | mpuInterrupt = false; |
pHysiX | 1:43f8ac7ca6d7 | 37 | mpuIntStatus = imu.getIntStatus(); |
pHysiX | 1:43f8ac7ca6d7 | 38 | |
pHysiX | 1:43f8ac7ca6d7 | 39 | // get current FIFO count |
pHysiX | 1:43f8ac7ca6d7 | 40 | fifoCount = imu.getFIFOCount(); |
pHysiX | 1:43f8ac7ca6d7 | 41 | |
pHysiX | 1:43f8ac7ca6d7 | 42 | // check for overflow (this should never happen unless our code is too inefficient) |
pHysiX | 1:43f8ac7ca6d7 | 43 | if ((mpuIntStatus & 0x10) || fifoCount == 1024) { |
pHysiX | 1:43f8ac7ca6d7 | 44 | // reset so we can continue cleanly |
pHysiX | 1:43f8ac7ca6d7 | 45 | imu.resetFIFO(); |
pHysiX | 1:43f8ac7ca6d7 | 46 | imu.debugSerial.printf("FIFO overflow!"); |
pHysiX | 1:43f8ac7ca6d7 | 47 | |
pHysiX | 1:43f8ac7ca6d7 | 48 | // otherwise, check for DMP data ready interrupt (this should happen frequently) |
pHysiX | 1:43f8ac7ca6d7 | 49 | } else if (mpuIntStatus & 0x02) { |
pHysiX | 1:43f8ac7ca6d7 | 50 | // wait for correct available data length, should be a VERY short wait |
pHysiX | 1:43f8ac7ca6d7 | 51 | while (fifoCount < packetSize) fifoCount = imu.getFIFOCount(); |
pHysiX | 1:43f8ac7ca6d7 | 52 | |
pHysiX | 1:43f8ac7ca6d7 | 53 | // read a packet from FIFO |
pHysiX | 1:43f8ac7ca6d7 | 54 | imu.getFIFOBytes(fifoBuffer, packetSize); |
pHysiX | 1:43f8ac7ca6d7 | 55 | |
pHysiX | 1:43f8ac7ca6d7 | 56 | // track FIFO count here in case there is > 1 packet available |
pHysiX | 1:43f8ac7ca6d7 | 57 | // (this lets us immediately read more without waiting for an interrupt) |
pHysiX | 1:43f8ac7ca6d7 | 58 | fifoCount -= packetSize; |
pHysiX | 1:43f8ac7ca6d7 | 59 | |
pHysiX | 1:43f8ac7ca6d7 | 60 | // display YPR angles in degrees |
pHysiX | 1:43f8ac7ca6d7 | 61 | imu.dmpGetQuaternion(&q, fifoBuffer); |
pHysiX | 1:43f8ac7ca6d7 | 62 | imu.dmpGetGravity(&gravity, &q); |
pHysiX | 1:43f8ac7ca6d7 | 63 | imu.dmpGetYawPitchRoll(ypr, &q, &gravity); |
pHysiX | 1:43f8ac7ca6d7 | 64 | ypr[0] *= 180/M_PI; |
pHysiX | 1:43f8ac7ca6d7 | 65 | ypr[1] *= 180/M_PI; |
pHysiX | 1:43f8ac7ca6d7 | 66 | ypr[2] *= 180/M_PI; |
pHysiX | 1:43f8ac7ca6d7 | 67 | |
pHysiX | 3:605fbcb54e75 | 68 | if (box_demo) |
pHysiX | 3:605fbcb54e75 | 69 | //BT.printf("YPR\t%3.2f\t%3.2f\t%3.2f\n", ypr[0]*180/M_PI, ypr[1]*180/M_PI, ypr[2]*180/M_PI); |
pHysiX | 3:605fbcb54e75 | 70 | BT.printf("Y%3.2f\nP%3.2f\nR%3.2f\n", ypr[0] - ypr_offset[0], ypr[1] - ypr_offset[1], ypr[2] - ypr_offset[2]); |
pHysiX | 1:43f8ac7ca6d7 | 71 | } |
pHysiX | 1:43f8ac7ca6d7 | 72 | |
pHysiX | 2:ab967d7b4346 | 73 | //LED[1] = !LED[1]; |
pHysiX | 2:ab967d7b4346 | 74 | } |