Ian Hua / Quadcopter-mbedRTOS
Committer:
pHysiX
Date:
Mon May 19 14:16:47 2014 +0000
Revision:
50:8a0accb23007
Parent:
48:9dbdc4144f00
Child:
51:04c6af4319e1
Semaphores implemented, currently no hanging. Need to test flight

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 50:8a0accb23007 13 Semaphore sem_Task2_Master(1);
pHysiX 50:8a0accb23007 14
pHysiX 36:d95e3d6f2fc4 15 /* MPU6050 control/status variables: */
pHysiX 36:d95e3d6f2fc4 16 uint8_t mpuIntStatus; // holds actual interrupt status byte from MPU
pHysiX 36:d95e3d6f2fc4 17 uint16_t fifoCount; // count of all bytes currently in FIFO
pHysiX 36:d95e3d6f2fc4 18 uint8_t fifoBuffer[64]; // FIFO storage buffer
pHysiX 36:d95e3d6f2fc4 19
pHysiX 36:d95e3d6f2fc4 20 /* Orientation/motion variables: */
pHysiX 36:d95e3d6f2fc4 21 Quaternion q; // [w, x, y, z] quaternion container
pHysiX 36:d95e3d6f2fc4 22 VectorFloat gravity; // [x, y, z] gravity vector
pHysiX 38:ef65533cca32 23 float ypr_rad[3]; // [yaw, pitch, roll] yaw/pitch/roll container and gravity vector
pHysiX 50:8a0accb23007 24 volatile float ypr[3];
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 27:18b6580eb0b1 44 void Task2_Master(void const *argument)
pHysiX 27:18b6580eb0b1 45 {
pHysiX 50:8a0accb23007 46 while(1) {
pHysiX 50:8a0accb23007 47 sem_Task2_Master.wait();
pHysiX 50:8a0accb23007 48 AHRSSample();
pHysiX 36:d95e3d6f2fc4 49
pHysiX 50:8a0accb23007 50 if (armed) {
pHysiX 50:8a0accb23007 51 switch (mode) {
pHysiX 50:8a0accb23007 52 case RATE:
pHysiX 50:8a0accb23007 53 break;
pHysiX 28:aa72bd4ff103 54
pHysiX 50:8a0accb23007 55 case ATTITUDE:
pHysiX 50:8a0accb23007 56 default:
pHysiX 50:8a0accb23007 57 pitchPIDattitude.setProcessValue((int) (ypr[1] - ypr_offset[1]));
pHysiX 50:8a0accb23007 58 rollPIDattitude.setProcessValue((int) (ypr[2] - ypr_offset[2]));
pHysiX 50:8a0accb23007 59
pHysiX 50:8a0accb23007 60 pitchPIDattitude.setSetPoint(inputYPR[1]);
pHysiX 50:8a0accb23007 61 rollPIDattitude.setSetPoint(inputYPR[2]);
pHysiX 27:18b6580eb0b1 62
pHysiX 50:8a0accb23007 63 adjust_attitude[1] = pitchPIDattitude.compute();
pHysiX 50:8a0accb23007 64 adjust_attitude[2] = rollPIDattitude.compute();
pHysiX 50:8a0accb23007 65 adjust_attitude[2] *= -1;
pHysiX 30:d9b988f8d84f 66
pHysiX 50:8a0accb23007 67 sem_Task2_Slave.release();
pHysiX 50:8a0accb23007 68 break;
pHysiX 50:8a0accb23007 69 }
pHysiX 30:d9b988f8d84f 70 }
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 50:8a0accb23007 80 void Task2_Master_ISR(void const *argument)
pHysiX 50:8a0accb23007 81 {
pHysiX 50:8a0accb23007 82 sem_Task2_Master.release();
pHysiX 50:8a0accb23007 83 }
pHysiX 50:8a0accb23007 84
pHysiX 36:d95e3d6f2fc4 85 void AHRSSample(void)
pHysiX 36:d95e3d6f2fc4 86 {
pHysiX 36:d95e3d6f2fc4 87 //Timer
pHysiX 36:d95e3d6f2fc4 88 // reset interrupt flag and get INT_STATUS byte
pHysiX 48:9dbdc4144f00 89 mpuIntStatus = imu_nb.getIntStatus();
pHysiX 36:d95e3d6f2fc4 90
pHysiX 36:d95e3d6f2fc4 91 // get current FIFO count
pHysiX 48:9dbdc4144f00 92 fifoCount = imu_nb.getFIFOCount();
pHysiX 36:d95e3d6f2fc4 93 //imu.debugSerial.printf("FIFO Count: %d\n", fifoCount);
pHysiX 36:d95e3d6f2fc4 94
pHysiX 36:d95e3d6f2fc4 95 // check for overflow
pHysiX 36:d95e3d6f2fc4 96 // Only keep a max of 2 packets in buffer.
pHysiX 36:d95e3d6f2fc4 97 if ((mpuIntStatus & 0x10) || fifoCount > 84) {
pHysiX 36:d95e3d6f2fc4 98 // reset so we can continue cleanly
pHysiX 48:9dbdc4144f00 99 imu_nb.resetFIFO();
pHysiX 38:ef65533cca32 100 //imu.debugSerial.printf("FIFO overflow!");
pHysiX 38:ef65533cca32 101 //BT.printf("FIFO overflow!\n");
pHysiX 36:d95e3d6f2fc4 102
pHysiX 36:d95e3d6f2fc4 103 // otherwise, check for DMP data ready interrupt (this should happen frequently)
pHysiX 36:d95e3d6f2fc4 104 } else if (mpuIntStatus & 0x02) {
pHysiX 36:d95e3d6f2fc4 105 // wait for correct available data length, should be a VERY short wait
pHysiX 48:9dbdc4144f00 106 while (fifoCount < packetSize) fifoCount = imu_nb.getFIFOCount();
pHysiX 36:d95e3d6f2fc4 107
pHysiX 36:d95e3d6f2fc4 108 while (fifoCount > 41) {
pHysiX 36:d95e3d6f2fc4 109 // read a packet from FIFO
pHysiX 48:9dbdc4144f00 110 imu_nb.getFIFOBytes(fifoBuffer, packetSize);
pHysiX 36:d95e3d6f2fc4 111
pHysiX 36:d95e3d6f2fc4 112 // track FIFO count here in case there is > 1 packet available
pHysiX 36:d95e3d6f2fc4 113 // (this lets us immediately read more without waiting for an interrupt)
pHysiX 36:d95e3d6f2fc4 114 fifoCount -= packetSize;
pHysiX 36:d95e3d6f2fc4 115 }
pHysiX 36:d95e3d6f2fc4 116
pHysiX 36:d95e3d6f2fc4 117 // display YPR angles in degrees
pHysiX 48:9dbdc4144f00 118 imu_nb.dmpGetQuaternion(&q, fifoBuffer);
pHysiX 48:9dbdc4144f00 119 imu_nb.dmpGetGravity(&gravity, &q);
pHysiX 48:9dbdc4144f00 120 imu_nb.dmpGetYawPitchRoll(ypr_rad, &q, &gravity);
pHysiX 36:d95e3d6f2fc4 121
pHysiX 38:ef65533cca32 122 for (int i = 0; i < 3; i++)
pHysiX 38:ef65533cca32 123 ypr[i] = ypr_rad[i] * 180/M_PI;
pHysiX 36:d95e3d6f2fc4 124 }
pHysiX 36:d95e3d6f2fc4 125 //Timer
pHysiX 36:d95e3d6f2fc4 126 }