Ian Hua / Quadcopter-mbedRTOS
Committer:
pHysiX
Date:
Wed May 14 12:42:39 2014 +0000
Revision:
39:02782ad251db
Parent:
38:ef65533cca32
Child:
47:89a7077a70d3
Child:
48:9dbdc4144f00
PID tuned; Frequency of Attitude and Rate PID decreased; ESC frequency decreased; RTOS back to stable; Increased stick gains

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 27:18b6580eb0b1 67
pHysiX 30:d9b988f8d84f 68 //counterTask2Master = true;
pHysiX 31:3dde2201e54d 69 //Timer
pHysiX 30:d9b988f8d84f 70 break;
pHysiX 30:d9b988f8d84f 71 }
pHysiX 31:3dde2201e54d 72 //Timer
pHysiX 27:18b6580eb0b1 73 }
pHysiX 27:18b6580eb0b1 74 }
pHysiX 33:f88a6ee18103 75
pHysiX 36:d95e3d6f2fc4 76
pHysiX 36:d95e3d6f2fc4 77
pHysiX 36:d95e3d6f2fc4 78
pHysiX 34:228d87c45151 79 // ************************
pHysiX 34:228d87c45151 80 // *** Helper functions ***
pHysiX 34:228d87c45151 81 // ************************
pHysiX 36:d95e3d6f2fc4 82 void AHRSSample(void)
pHysiX 36:d95e3d6f2fc4 83 {
pHysiX 36:d95e3d6f2fc4 84 //Timer
pHysiX 36:d95e3d6f2fc4 85 // reset interrupt flag and get INT_STATUS byte
pHysiX 36:d95e3d6f2fc4 86 mpuIntStatus = imu.getIntStatus();
pHysiX 36:d95e3d6f2fc4 87
pHysiX 36:d95e3d6f2fc4 88 // get current FIFO count
pHysiX 36:d95e3d6f2fc4 89 fifoCount = imu.getFIFOCount();
pHysiX 36:d95e3d6f2fc4 90 //imu.debugSerial.printf("FIFO Count: %d\n", fifoCount);
pHysiX 36:d95e3d6f2fc4 91
pHysiX 36:d95e3d6f2fc4 92 // check for overflow
pHysiX 36:d95e3d6f2fc4 93 // Only keep a max of 2 packets in buffer.
pHysiX 36:d95e3d6f2fc4 94 if ((mpuIntStatus & 0x10) || fifoCount > 84) {
pHysiX 36:d95e3d6f2fc4 95 // reset so we can continue cleanly
pHysiX 36:d95e3d6f2fc4 96 imu.resetFIFO();
pHysiX 38:ef65533cca32 97 //imu.debugSerial.printf("FIFO overflow!");
pHysiX 38:ef65533cca32 98 //BT.printf("FIFO overflow!\n");
pHysiX 36:d95e3d6f2fc4 99
pHysiX 36:d95e3d6f2fc4 100 // otherwise, check for DMP data ready interrupt (this should happen frequently)
pHysiX 36:d95e3d6f2fc4 101 } else if (mpuIntStatus & 0x02) {
pHysiX 36:d95e3d6f2fc4 102 // wait for correct available data length, should be a VERY short wait
pHysiX 36:d95e3d6f2fc4 103 while (fifoCount < packetSize) fifoCount = imu.getFIFOCount();
pHysiX 36:d95e3d6f2fc4 104
pHysiX 36:d95e3d6f2fc4 105 while (fifoCount > 41) {
pHysiX 36:d95e3d6f2fc4 106 // read a packet from FIFO
pHysiX 36:d95e3d6f2fc4 107 imu.getFIFOBytes(fifoBuffer, packetSize);
pHysiX 36:d95e3d6f2fc4 108
pHysiX 36:d95e3d6f2fc4 109 // track FIFO count here in case there is > 1 packet available
pHysiX 36:d95e3d6f2fc4 110 // (this lets us immediately read more without waiting for an interrupt)
pHysiX 36:d95e3d6f2fc4 111 fifoCount -= packetSize;
pHysiX 36:d95e3d6f2fc4 112 }
pHysiX 36:d95e3d6f2fc4 113
pHysiX 36:d95e3d6f2fc4 114 // display YPR angles in degrees
pHysiX 36:d95e3d6f2fc4 115 imu.dmpGetQuaternion(&q, fifoBuffer);
pHysiX 36:d95e3d6f2fc4 116 imu.dmpGetGravity(&gravity, &q);
pHysiX 38:ef65533cca32 117 imu.dmpGetYawPitchRoll(ypr_rad, &q, &gravity);
pHysiX 36:d95e3d6f2fc4 118
pHysiX 38:ef65533cca32 119 for (int i = 0; i < 3; i++)
pHysiX 38:ef65533cca32 120 ypr[i] = ypr_rad[i] * 180/M_PI;
pHysiX 36:d95e3d6f2fc4 121
pHysiX 36:d95e3d6f2fc4 122 /*
pHysiX 36:d95e3d6f2fc4 123 if (compass.getDataReady()) {
pHysiX 36:d95e3d6f2fc4 124 // compass.getValues(&compass_x, &compass_y, &compass_z);
pHysiX 36:d95e3d6f2fc4 125 heading = compass.getHeadingXY() * 180/M_PI;
pHysiX 36:d95e3d6f2fc4 126 }
pHysiX 36:d95e3d6f2fc4 127
pHysiX 36:d95e3d6f2fc4 128 ypr[0] *= 0.98;
pHysiX 36:d95e3d6f2fc4 129 ypr[0] += 0.02*heading;
pHysiX 36:d95e3d6f2fc4 130 */
pHysiX 36:d95e3d6f2fc4 131 }
pHysiX 36:d95e3d6f2fc4 132 //Timer
pHysiX 36:d95e3d6f2fc4 133 }