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