Ian Hua / Quadcopter-mbedRTOS
Committer:
pHysiX
Date:
Mon May 19 13:21:17 2014 +0000
Revision:
48:9dbdc4144f00
Parent:
39:02782ad251db
Child:
50:8a0accb23007
Altimeter and IMU changed to non-blocking I2C

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pHysiX 27:18b6580eb0b1 1 /* File: Task2_Master.cpp
pHysiX 27:18b6580eb0b1 2 * Author: Trung Tin Ian HUA
pHysiX 27:18b6580eb0b1 3 * Date: May 2014
pHysiX 31:3dde2201e54d 4 * Purpose: Thread2M: Master PID control loop (attitude)
pHysiX 36:d95e3d6f2fc4 5 * Functions: AHRSSample: Read MPU6050 DMP and calculate YPR
pHysiX 36:d95e3d6f2fc4 6 * Settings: 200Hz
pHysiX 38:ef65533cca32 7 * Timing:
pHysiX 27:18b6580eb0b1 8 */
pHysiX 27:18b6580eb0b1 9 #include "Task2_Slave.h"
pHysiX 27:18b6580eb0b1 10 #include "setup.h"
pHysiX 27:18b6580eb0b1 11 #include "PID.h"
pHysiX 27:18b6580eb0b1 12
pHysiX 36:d95e3d6f2fc4 13 /* MPU6050 control/status variables: */
pHysiX 36:d95e3d6f2fc4 14 uint8_t mpuIntStatus; // holds actual interrupt status byte from MPU
pHysiX 36:d95e3d6f2fc4 15 uint16_t fifoCount; // count of all bytes currently in FIFO
pHysiX 36:d95e3d6f2fc4 16 uint8_t fifoBuffer[64]; // FIFO storage buffer
pHysiX 36:d95e3d6f2fc4 17
pHysiX 36:d95e3d6f2fc4 18 /* Orientation/motion variables: */
pHysiX 36:d95e3d6f2fc4 19 Quaternion q; // [w, x, y, z] quaternion container
pHysiX 36:d95e3d6f2fc4 20 VectorFloat gravity; // [x, y, z] gravity vector
pHysiX 38:ef65533cca32 21 float ypr_rad[3]; // [yaw, pitch, roll] yaw/pitch/roll container and gravity vector
pHysiX 38:ef65533cca32 22 volatile float ypr[3];
pHysiX 36:d95e3d6f2fc4 23
pHysiX 36:d95e3d6f2fc4 24 float altitude, temperature;
pHysiX 36:d95e3d6f2fc4 25
pHysiX 36:d95e3d6f2fc4 26 #ifndef M_PI
pHysiX 36:d95e3d6f2fc4 27 #define M_PI 3.1415
pHysiX 36:d95e3d6f2fc4 28 #endif
pHysiX 36:d95e3d6f2fc4 29
pHysiX 36:d95e3d6f2fc4 30 #ifdef ENABLE_COMPASS
pHysiX 36:d95e3d6f2fc4 31 //int compassX, compassY, compassZ;
pHysiX 36:d95e3d6f2fc4 32 double heading = 0;
pHysiX 36:d95e3d6f2fc4 33 #endif
pHysiX 36:d95e3d6f2fc4 34
pHysiX 27:18b6580eb0b1 35 /* YPR Adjust */
pHysiX 27:18b6580eb0b1 36 volatile float adjust_attitude[3] = {0.0, 0.0, 0.0};
pHysiX 27:18b6580eb0b1 37
pHysiX 34:228d87c45151 38
pHysiX 34:228d87c45151 39
pHysiX 36:d95e3d6f2fc4 40
pHysiX 34:228d87c45151 41 // ===============================
pHysiX 34:228d87c45151 42 // === YPR SAMPLE & MASTER PID ===
pHysiX 34:228d87c45151 43 // ===============================
pHysiX 31:3dde2201e54d 44 //Timer
pHysiX 27:18b6580eb0b1 45 void Task2_Master(void const *argument)
pHysiX 27:18b6580eb0b1 46 {
pHysiX 31:3dde2201e54d 47 //Timer
pHysiX 36:d95e3d6f2fc4 48 AHRSSample();
pHysiX 36:d95e3d6f2fc4 49
pHysiX 30:d9b988f8d84f 50 if (armed) {
pHysiX 30:d9b988f8d84f 51 switch (mode) {
pHysiX 30:d9b988f8d84f 52 case RATE:
pHysiX 30:d9b988f8d84f 53 break;
pHysiX 28:aa72bd4ff103 54
pHysiX 30:d9b988f8d84f 55 case ATTITUDE:
pHysiX 30:d9b988f8d84f 56 default:
pHysiX 31:3dde2201e54d 57 //Timer
pHysiX 39:02782ad251db 58 pitchPIDattitude.setProcessValue((int) (ypr[1] - ypr_offset[1]));
pHysiX 39:02782ad251db 59 rollPIDattitude.setProcessValue((int) (ypr[2] - ypr_offset[2]));
pHysiX 27:18b6580eb0b1 60
pHysiX 39:02782ad251db 61 pitchPIDattitude.setSetPoint(inputYPR[1]);
pHysiX 39:02782ad251db 62 rollPIDattitude.setSetPoint(inputYPR[2]);
pHysiX 30:d9b988f8d84f 63
pHysiX 39:02782ad251db 64 adjust_attitude[1] = pitchPIDattitude.compute();
pHysiX 39:02782ad251db 65 adjust_attitude[2] = rollPIDattitude.compute();
pHysiX 30:d9b988f8d84f 66 adjust_attitude[2] *= -1;
pHysiX 31:3dde2201e54d 67 //Timer
pHysiX 30:d9b988f8d84f 68 break;
pHysiX 30:d9b988f8d84f 69 }
pHysiX 31:3dde2201e54d 70 //Timer
pHysiX 27:18b6580eb0b1 71 }
pHysiX 27:18b6580eb0b1 72 }
pHysiX 33:f88a6ee18103 73
pHysiX 36:d95e3d6f2fc4 74
pHysiX 36:d95e3d6f2fc4 75
pHysiX 36:d95e3d6f2fc4 76
pHysiX 34:228d87c45151 77 // ************************
pHysiX 34:228d87c45151 78 // *** Helper functions ***
pHysiX 34:228d87c45151 79 // ************************
pHysiX 36:d95e3d6f2fc4 80 void AHRSSample(void)
pHysiX 36:d95e3d6f2fc4 81 {
pHysiX 36:d95e3d6f2fc4 82 //Timer
pHysiX 36:d95e3d6f2fc4 83 // reset interrupt flag and get INT_STATUS byte
pHysiX 48:9dbdc4144f00 84 mpuIntStatus = imu_nb.getIntStatus();
pHysiX 36:d95e3d6f2fc4 85
pHysiX 36:d95e3d6f2fc4 86 // get current FIFO count
pHysiX 48:9dbdc4144f00 87 fifoCount = imu_nb.getFIFOCount();
pHysiX 36:d95e3d6f2fc4 88 //imu.debugSerial.printf("FIFO Count: %d\n", fifoCount);
pHysiX 36:d95e3d6f2fc4 89
pHysiX 36:d95e3d6f2fc4 90 // check for overflow
pHysiX 36:d95e3d6f2fc4 91 // Only keep a max of 2 packets in buffer.
pHysiX 36:d95e3d6f2fc4 92 if ((mpuIntStatus & 0x10) || fifoCount > 84) {
pHysiX 36:d95e3d6f2fc4 93 // reset so we can continue cleanly
pHysiX 48:9dbdc4144f00 94 imu_nb.resetFIFO();
pHysiX 38:ef65533cca32 95 //imu.debugSerial.printf("FIFO overflow!");
pHysiX 38:ef65533cca32 96 //BT.printf("FIFO overflow!\n");
pHysiX 36:d95e3d6f2fc4 97
pHysiX 36:d95e3d6f2fc4 98 // otherwise, check for DMP data ready interrupt (this should happen frequently)
pHysiX 36:d95e3d6f2fc4 99 } else if (mpuIntStatus & 0x02) {
pHysiX 36:d95e3d6f2fc4 100 // wait for correct available data length, should be a VERY short wait
pHysiX 48:9dbdc4144f00 101 while (fifoCount < packetSize) fifoCount = imu_nb.getFIFOCount();
pHysiX 36:d95e3d6f2fc4 102
pHysiX 36:d95e3d6f2fc4 103 while (fifoCount > 41) {
pHysiX 36:d95e3d6f2fc4 104 // read a packet from FIFO
pHysiX 48:9dbdc4144f00 105 imu_nb.getFIFOBytes(fifoBuffer, packetSize);
pHysiX 36:d95e3d6f2fc4 106
pHysiX 36:d95e3d6f2fc4 107 // track FIFO count here in case there is > 1 packet available
pHysiX 36:d95e3d6f2fc4 108 // (this lets us immediately read more without waiting for an interrupt)
pHysiX 36:d95e3d6f2fc4 109 fifoCount -= packetSize;
pHysiX 36:d95e3d6f2fc4 110 }
pHysiX 36:d95e3d6f2fc4 111
pHysiX 36:d95e3d6f2fc4 112 // display YPR angles in degrees
pHysiX 48:9dbdc4144f00 113 imu_nb.dmpGetQuaternion(&q, fifoBuffer);
pHysiX 48:9dbdc4144f00 114 imu_nb.dmpGetGravity(&gravity, &q);
pHysiX 48:9dbdc4144f00 115 imu_nb.dmpGetYawPitchRoll(ypr_rad, &q, &gravity);
pHysiX 36:d95e3d6f2fc4 116
pHysiX 38:ef65533cca32 117 for (int i = 0; i < 3; i++)
pHysiX 38:ef65533cca32 118 ypr[i] = ypr_rad[i] * 180/M_PI;
pHysiX 36:d95e3d6f2fc4 119 }
pHysiX 36:d95e3d6f2fc4 120 //Timer
pHysiX 36:d95e3d6f2fc4 121 }