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@33:f88a6ee18103, 2014-05-12 (annotated)
- Committer:
- pHysiX
- Date:
- Mon May 12 13:49:43 2014 +0000
- Revision:
- 33:f88a6ee18103
- Parent:
- 32:7a9be7761c46
- Child:
- 34:228d87c45151
Tidied code
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 | 31:3dde2201e54d | 20 | //Timer |
pHysiX | 27:18b6580eb0b1 | 21 | void Task2_Slave(void const *argument) |
pHysiX | 1:43f8ac7ca6d7 | 22 | { |
pHysiX | 31:3dde2201e54d | 23 | //Timer |
pHysiX | 21:b642c18eccd1 | 24 | imu.getRotation(&gx, &gy, &gz); |
pHysiX | 21:b642c18eccd1 | 25 | gyro[0] = gx + 60; |
pHysiX | 21:b642c18eccd1 | 26 | gyro[1] = gy - 15; |
pHysiX | 21:b642c18eccd1 | 27 | gyro[2] = gz + 25; |
pHysiX | 17:18c3bd016e49 | 28 | |
pHysiX | 21:b642c18eccd1 | 29 | for (int i = 0; i < 3; i++) |
pHysiX | 21:b642c18eccd1 | 30 | gyro[i] /= (float) 32.8; |
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 | 33:f88a6ee18103 | 69 | |
pHysiX | 33:f88a6ee18103 | 70 | /* Telemetry output: */ |
pHysiX | 31:3dde2201e54d | 71 | if (adjust_check) |
pHysiX | 31:3dde2201e54d | 72 | BT.printf("%3.2f %3.2f %3.2f\n", adjust[0], adjust[1], adjust[2]); |
pHysiX | 25:a7cfe421cb4a | 73 | } |
pHysiX | 21:b642c18eccd1 | 74 | |
pHysiX | 10:ef5fe86f67fe | 75 | if (gyro_out) |
pHysiX | 21:b642c18eccd1 | 76 | BT.printf("%3d %3d %3d\n", (int) gyro[0], (int) gyro[1], (int) gyro[2]); |
pHysiX | 31:3dde2201e54d | 77 | //Timer |
pHysiX | 21:b642c18eccd1 | 78 | } |
pHysiX | 33:f88a6ee18103 | 79 | |
pHysiX | 33:f88a6ee18103 | 80 | // ======================== |
pHysiX | 33:f88a6ee18103 | 81 | // === Helper functions === |
pHysiX | 33:f88a6ee18103 | 82 | // ======================== |