Ian Hua / Quadcopter-mbedRTOS
Committer:
pHysiX
Date:
Sun May 18 09:05:41 2014 +0000
Revision:
47:89a7077a70d3
Parent:
39:02782ad251db
Broken 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 47:89a7077a70d3 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 47:89a7077a70d3 38 int altiUpdate = 0;
pHysiX 47:89a7077a70d3 39 volatile float altiDistance = 0.0;
pHysiX 47:89a7077a70d3 40 volatile float altiTemp = 0.0;
pHysiX 34:228d87c45151 41
pHysiX 34:228d87c45151 42
pHysiX 36:d95e3d6f2fc4 43
pHysiX 34:228d87c45151 44 // ===============================
pHysiX 34:228d87c45151 45 // === YPR SAMPLE & MASTER PID ===
pHysiX 34:228d87c45151 46 // ===============================
pHysiX 31:3dde2201e54d 47 //Timer
pHysiX 27:18b6580eb0b1 48 void Task2_Master(void const *argument)
pHysiX 27:18b6580eb0b1 49 {
pHysiX 31:3dde2201e54d 50 //Timer
pHysiX 36:d95e3d6f2fc4 51 AHRSSample();
pHysiX 36:d95e3d6f2fc4 52
pHysiX 47:89a7077a70d3 53 if (altiUpdate > 1) {
pHysiX 47:89a7077a70d3 54 altiDistance = altimeter.Altitude_m();
pHysiX 47:89a7077a70d3 55 altiTemp = altimeter.Temp_C();
pHysiX 47:89a7077a70d3 56 altiUpdate = 0;
pHysiX 47:89a7077a70d3 57 } else
pHysiX 47:89a7077a70d3 58 altiUpdate++;
pHysiX 47:89a7077a70d3 59
pHysiX 30:d9b988f8d84f 60 if (armed) {
pHysiX 30:d9b988f8d84f 61 switch (mode) {
pHysiX 30:d9b988f8d84f 62 case RATE:
pHysiX 30:d9b988f8d84f 63 break;
pHysiX 28:aa72bd4ff103 64
pHysiX 30:d9b988f8d84f 65 case ATTITUDE:
pHysiX 30:d9b988f8d84f 66 default:
pHysiX 31:3dde2201e54d 67 //Timer
pHysiX 39:02782ad251db 68 pitchPIDattitude.setProcessValue((int) (ypr[1] - ypr_offset[1]));
pHysiX 39:02782ad251db 69 rollPIDattitude.setProcessValue((int) (ypr[2] - ypr_offset[2]));
pHysiX 27:18b6580eb0b1 70
pHysiX 39:02782ad251db 71 pitchPIDattitude.setSetPoint(inputYPR[1]);
pHysiX 39:02782ad251db 72 rollPIDattitude.setSetPoint(inputYPR[2]);
pHysiX 30:d9b988f8d84f 73
pHysiX 39:02782ad251db 74 adjust_attitude[1] = pitchPIDattitude.compute();
pHysiX 39:02782ad251db 75 adjust_attitude[2] = rollPIDattitude.compute();
pHysiX 30:d9b988f8d84f 76 adjust_attitude[2] *= -1;
pHysiX 27:18b6580eb0b1 77
pHysiX 30:d9b988f8d84f 78 //counterTask2Master = true;
pHysiX 31:3dde2201e54d 79 //Timer
pHysiX 30:d9b988f8d84f 80 break;
pHysiX 30:d9b988f8d84f 81 }
pHysiX 31:3dde2201e54d 82 //Timer
pHysiX 27:18b6580eb0b1 83 }
pHysiX 27:18b6580eb0b1 84 }
pHysiX 33:f88a6ee18103 85
pHysiX 36:d95e3d6f2fc4 86
pHysiX 36:d95e3d6f2fc4 87
pHysiX 36:d95e3d6f2fc4 88
pHysiX 34:228d87c45151 89 // ************************
pHysiX 34:228d87c45151 90 // *** Helper functions ***
pHysiX 34:228d87c45151 91 // ************************
pHysiX 36:d95e3d6f2fc4 92 void AHRSSample(void)
pHysiX 36:d95e3d6f2fc4 93 {
pHysiX 36:d95e3d6f2fc4 94 //Timer
pHysiX 36:d95e3d6f2fc4 95 // reset interrupt flag and get INT_STATUS byte
pHysiX 36:d95e3d6f2fc4 96 mpuIntStatus = imu.getIntStatus();
pHysiX 36:d95e3d6f2fc4 97
pHysiX 36:d95e3d6f2fc4 98 // get current FIFO count
pHysiX 36:d95e3d6f2fc4 99 fifoCount = imu.getFIFOCount();
pHysiX 36:d95e3d6f2fc4 100 //imu.debugSerial.printf("FIFO Count: %d\n", fifoCount);
pHysiX 36:d95e3d6f2fc4 101
pHysiX 36:d95e3d6f2fc4 102 // check for overflow
pHysiX 36:d95e3d6f2fc4 103 // Only keep a max of 2 packets in buffer.
pHysiX 36:d95e3d6f2fc4 104 if ((mpuIntStatus & 0x10) || fifoCount > 84) {
pHysiX 36:d95e3d6f2fc4 105 // reset so we can continue cleanly
pHysiX 36:d95e3d6f2fc4 106 imu.resetFIFO();
pHysiX 38:ef65533cca32 107 //imu.debugSerial.printf("FIFO overflow!");
pHysiX 38:ef65533cca32 108 //BT.printf("FIFO overflow!\n");
pHysiX 36:d95e3d6f2fc4 109
pHysiX 36:d95e3d6f2fc4 110 // otherwise, check for DMP data ready interrupt (this should happen frequently)
pHysiX 36:d95e3d6f2fc4 111 } else if (mpuIntStatus & 0x02) {
pHysiX 36:d95e3d6f2fc4 112 // wait for correct available data length, should be a VERY short wait
pHysiX 36:d95e3d6f2fc4 113 while (fifoCount < packetSize) fifoCount = imu.getFIFOCount();
pHysiX 36:d95e3d6f2fc4 114
pHysiX 36:d95e3d6f2fc4 115 while (fifoCount > 41) {
pHysiX 36:d95e3d6f2fc4 116 // read a packet from FIFO
pHysiX 36:d95e3d6f2fc4 117 imu.getFIFOBytes(fifoBuffer, packetSize);
pHysiX 36:d95e3d6f2fc4 118
pHysiX 36:d95e3d6f2fc4 119 // track FIFO count here in case there is > 1 packet available
pHysiX 36:d95e3d6f2fc4 120 // (this lets us immediately read more without waiting for an interrupt)
pHysiX 36:d95e3d6f2fc4 121 fifoCount -= packetSize;
pHysiX 36:d95e3d6f2fc4 122 }
pHysiX 36:d95e3d6f2fc4 123
pHysiX 36:d95e3d6f2fc4 124 // display YPR angles in degrees
pHysiX 36:d95e3d6f2fc4 125 imu.dmpGetQuaternion(&q, fifoBuffer);
pHysiX 47:89a7077a70d3 126 //imu.dmpGetGravity(&gravity, &q);
pHysiX 47:89a7077a70d3 127 //imu.dmpGetYawPitchRoll(ypr_rad, &q, &gravity);
pHysiX 47:89a7077a70d3 128 imu.dmpGetEuler(ypr_rad, &q);
pHysiX 36:d95e3d6f2fc4 129
pHysiX 38:ef65533cca32 130 for (int i = 0; i < 3; i++)
pHysiX 38:ef65533cca32 131 ypr[i] = ypr_rad[i] * 180/M_PI;
pHysiX 36:d95e3d6f2fc4 132
pHysiX 36:d95e3d6f2fc4 133 /*
pHysiX 36:d95e3d6f2fc4 134 if (compass.getDataReady()) {
pHysiX 36:d95e3d6f2fc4 135 // compass.getValues(&compass_x, &compass_y, &compass_z);
pHysiX 36:d95e3d6f2fc4 136 heading = compass.getHeadingXY() * 180/M_PI;
pHysiX 36:d95e3d6f2fc4 137 }
pHysiX 36:d95e3d6f2fc4 138
pHysiX 36:d95e3d6f2fc4 139 ypr[0] *= 0.98;
pHysiX 36:d95e3d6f2fc4 140 ypr[0] += 0.02*heading;
pHysiX 36:d95e3d6f2fc4 141 */
pHysiX 36:d95e3d6f2fc4 142 }
pHysiX 36:d95e3d6f2fc4 143 //Timer
pHysiX 36:d95e3d6f2fc4 144 }