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/Task2_Slave.cpp@48:9dbdc4144f00, 2014-05-19 (annotated)
- Committer:
- pHysiX
- Date:
- Mon May 19 13:21:17 2014 +0000
- Revision:
- 48:9dbdc4144f00
- Parent:
- 38:ef65533cca32
- Child:
- 50:8a0accb23007
Altimeter and IMU changed to non-blocking I2C
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pHysiX | 27:18b6580eb0b1 | 1 | /* File: Task2_Slave.cpp |
pHysiX | 22:ef8aa9728013 | 2 | * Author: Trung Tin Ian HUA |
pHysiX | 22:ef8aa9728013 | 3 | * Date: May 2014 |
pHysiX | 36:d95e3d6f2fc4 | 4 | * Purpose: Thread2S: Slave PID control loop (rate) |
pHysiX | 36:d95e3d6f2fc4 | 5 | * Functions: Gyro sample |
pHysiX | 36:d95e3d6f2fc4 | 6 | * Settings: 400Hz |
pHysiX | 30:d9b988f8d84f | 7 | * Timing: 290us |
pHysiX | 25:a7cfe421cb4a | 8 | */ |
pHysiX | 26:4a3323ee36d5 | 9 | #include "Task2_Slave.h" |
pHysiX | 1:43f8ac7ca6d7 | 10 | #include "setup.h" |
pHysiX | 3:605fbcb54e75 | 11 | #include "PID.h" |
pHysiX | 3:605fbcb54e75 | 12 | |
pHysiX | 21:b642c18eccd1 | 13 | /* YPR Adjust */ |
pHysiX | 21:b642c18eccd1 | 14 | volatile float adjust[3] = {0.0, 0.0, 0.0}; |
pHysiX | 21:b642c18eccd1 | 15 | |
pHysiX | 3:605fbcb54e75 | 16 | int16_t gx, gy, gz; |
pHysiX | 10:ef5fe86f67fe | 17 | volatile int gyro[3] = {0, 0, 0}; |
pHysiX | 10:ef5fe86f67fe | 18 | |
pHysiX | 10:ef5fe86f67fe | 19 | bool counterESC = false; |
pHysiX | 1:43f8ac7ca6d7 | 20 | |
pHysiX | 34:228d87c45151 | 21 | |
pHysiX | 34:228d87c45151 | 22 | |
pHysiX | 34:228d87c45151 | 23 | // =============================== |
pHysiX | 34:228d87c45151 | 24 | // === GYRO SAMPLE & SLAVE PID === |
pHysiX | 34:228d87c45151 | 25 | // =============================== |
pHysiX | 31:3dde2201e54d | 26 | //Timer |
pHysiX | 27:18b6580eb0b1 | 27 | void Task2_Slave(void const *argument) |
pHysiX | 1:43f8ac7ca6d7 | 28 | { |
pHysiX | 31:3dde2201e54d | 29 | //Timer |
pHysiX | 36:d95e3d6f2fc4 | 30 | gyroSample(); |
pHysiX | 17:18c3bd016e49 | 31 | |
pHysiX | 31:3dde2201e54d | 32 | //Timer |
pHysiX | 30:d9b988f8d84f | 33 | if (armed) { |
pHysiX | 30:d9b988f8d84f | 34 | yawPIDrate.setSetPoint(inputYPR[0]); |
pHysiX | 25:a7cfe421cb4a | 35 | |
pHysiX | 30:d9b988f8d84f | 36 | switch (mode) { |
pHysiX | 30:d9b988f8d84f | 37 | case RATE: |
pHysiX | 30:d9b988f8d84f | 38 | pitchPIDrate.setSetPoint(inputYPR[1]); |
pHysiX | 30:d9b988f8d84f | 39 | rollPIDrate.setSetPoint(inputYPR[2]); |
pHysiX | 17:18c3bd016e49 | 40 | |
pHysiX | 30:d9b988f8d84f | 41 | yawPIDrate.setProcessValue(gyro[2]); |
pHysiX | 30:d9b988f8d84f | 42 | pitchPIDrate.setProcessValue(gyro[1]); |
pHysiX | 30:d9b988f8d84f | 43 | rollPIDrate.setProcessValue(gyro[0]); |
pHysiX | 30:d9b988f8d84f | 44 | |
pHysiX | 30:d9b988f8d84f | 45 | adjust[0] = yawPIDrate.compute(); |
pHysiX | 30:d9b988f8d84f | 46 | adjust[1] = pitchPIDrate.compute(); |
pHysiX | 30:d9b988f8d84f | 47 | adjust[2] = rollPIDrate.compute(); |
pHysiX | 25:a7cfe421cb4a | 48 | |
pHysiX | 30:d9b988f8d84f | 49 | counterESC = true; |
pHysiX | 30:d9b988f8d84f | 50 | break; |
pHysiX | 25:a7cfe421cb4a | 51 | |
pHysiX | 30:d9b988f8d84f | 52 | case ATTITUDE: |
pHysiX | 30:d9b988f8d84f | 53 | default: |
pHysiX | 30:d9b988f8d84f | 54 | pitchPIDrate.setSetPoint(adjust_attitude[1]); |
pHysiX | 30:d9b988f8d84f | 55 | rollPIDrate.setSetPoint(adjust_attitude[2]); |
pHysiX | 5:4879ef0e6d41 | 56 | |
pHysiX | 30:d9b988f8d84f | 57 | yawPIDrate.setProcessValue(gyro[2]); |
pHysiX | 30:d9b988f8d84f | 58 | pitchPIDrate.setProcessValue(gyro[1]); |
pHysiX | 30:d9b988f8d84f | 59 | rollPIDrate.setProcessValue(gyro[0]); |
pHysiX | 5:4879ef0e6d41 | 60 | |
pHysiX | 30:d9b988f8d84f | 61 | adjust[0] = yawPIDrate.compute(); |
pHysiX | 30:d9b988f8d84f | 62 | adjust[1] = pitchPIDrate.compute(); |
pHysiX | 30:d9b988f8d84f | 63 | adjust[2] = rollPIDrate.compute(); |
pHysiX | 21:b642c18eccd1 | 64 | |
pHysiX | 30:d9b988f8d84f | 65 | counterESC = true; |
pHysiX | 30:d9b988f8d84f | 66 | break; |
pHysiX | 30:d9b988f8d84f | 67 | } |
pHysiX | 31:3dde2201e54d | 68 | //Timer |
pHysiX | 25:a7cfe421cb4a | 69 | } |
pHysiX | 31:3dde2201e54d | 70 | //Timer |
pHysiX | 21:b642c18eccd1 | 71 | } |
pHysiX | 33:f88a6ee18103 | 72 | |
pHysiX | 36:d95e3d6f2fc4 | 73 | |
pHysiX | 36:d95e3d6f2fc4 | 74 | |
pHysiX | 36:d95e3d6f2fc4 | 75 | |
pHysiX | 34:228d87c45151 | 76 | // ************************ |
pHysiX | 34:228d87c45151 | 77 | // *** Helper functions *** |
pHysiX | 34:228d87c45151 | 78 | // ************************ |
pHysiX | 36:d95e3d6f2fc4 | 79 | void gyroSample(void) |
pHysiX | 36:d95e3d6f2fc4 | 80 | { |
pHysiX | 48:9dbdc4144f00 | 81 | imu_nb.getRotation(&gx, &gy, &gz); |
pHysiX | 36:d95e3d6f2fc4 | 82 | gyro[0] = gx + 60; |
pHysiX | 36:d95e3d6f2fc4 | 83 | gyro[1] = gy - 15; |
pHysiX | 38:ef65533cca32 | 84 | gyro[2] = gz - 8; |
pHysiX | 36:d95e3d6f2fc4 | 85 | |
pHysiX | 36:d95e3d6f2fc4 | 86 | for (int i = 0; i < 3; i++) |
pHysiX | 36:d95e3d6f2fc4 | 87 | gyro[i] /= (float) 32.8; |
pHysiX | 36:d95e3d6f2fc4 | 88 | } |