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/Task4.cpp@36:d95e3d6f2fc4, 2014-05-13 (annotated)
- Committer:
- pHysiX
- Date:
- Tue May 13 04:05:34 2014 +0000
- Revision:
- 36:d95e3d6f2fc4
- Parent:
- 34:228d87c45151
- Child:
- 37:29feef05d848
Telemetry output completely changed; Code tidied
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pHysiX | 22:ef8aa9728013 | 1 | /* File: Task4.h |
pHysiX | 22:ef8aa9728013 | 2 | * Author: Trung Tin Ian HUA |
pHysiX | 22:ef8aa9728013 | 3 | * Date: May 2014 |
pHysiX | 22:ef8aa9728013 | 4 | * Purpose: Thread4: ESC pulsewidth update. Note this is INDEPENDENT of the pulse frequency. |
pHysiX | 36:d95e3d6f2fc4 | 5 | * Settings: 400Hz |
pHysiX | 22:ef8aa9728013 | 6 | * 200Hz <= PWM frequency <= 400Hz |
pHysiX | 36:d95e3d6f2fc4 | 7 | * Frequency: 400Hz |
pHysiX | 30:d9b988f8d84f | 8 | * Refer to tasks.h to change PWM frequency |
pHysiX | 36:d95e3d6f2fc4 | 9 | * Timing: //1500us |
pHysiX | 30:d9b988f8d84f | 10 | */ |
pHysiX | 1:43f8ac7ca6d7 | 11 | #include "tasks.h" |
pHysiX | 1:43f8ac7ca6d7 | 12 | #include "setup.h" |
pHysiX | 1:43f8ac7ca6d7 | 13 | |
pHysiX | 2:ab967d7b4346 | 14 | int ESCpower[4] = {0, 0, 0, 0}; |
pHysiX | 10:ef5fe86f67fe | 15 | int stallESC = 0; |
pHysiX | 10:ef5fe86f67fe | 16 | |
pHysiX | 14:267368c83b6a | 17 | bool armed = false; |
pHysiX | 21:b642c18eccd1 | 18 | bool ESC_check = false; |
pHysiX | 21:b642c18eccd1 | 19 | bool calibration_mode = false; |
pHysiX | 2:ab967d7b4346 | 20 | |
pHysiX | 34:228d87c45151 | 21 | |
pHysiX | 34:228d87c45151 | 22 | |
pHysiX | 34:228d87c45151 | 23 | |
pHysiX | 34:228d87c45151 | 24 | // ================== |
pHysiX | 34:228d87c45151 | 25 | // === ESC UPDATE === |
pHysiX | 34:228d87c45151 | 26 | // ================== |
pHysiX | 31:3dde2201e54d | 27 | //Timer |
pHysiX | 1:43f8ac7ca6d7 | 28 | void Task4(void const *argurment) |
pHysiX | 1:43f8ac7ca6d7 | 29 | { |
pHysiX | 31:3dde2201e54d | 30 | //Timer |
pHysiX | 14:267368c83b6a | 31 | if (armed) { |
pHysiX | 21:b642c18eccd1 | 32 | if (calibration_mode) { |
pHysiX | 19:bd88749c8db4 | 33 | for (int i = 0; i < 4; i++) |
pHysiX | 21:b642c18eccd1 | 34 | ESC[i].pulsewidth_us(RCCommand[3]); |
pHysiX | 32:7a9be7761c46 | 35 | } else if (RCCommand[3] > 1100) { |
pHysiX | 30:d9b988f8d84f | 36 | if (counterESC) { |
pHysiX | 21:b642c18eccd1 | 37 | for (int i = 0; i < 3; i++) |
pHysiX | 21:b642c18eccd1 | 38 | adjust[i] /= 2.0; |
pHysiX | 30:d9b988f8d84f | 39 | |
pHysiX | 32:7a9be7761c46 | 40 | int throttle = RCCommand[3] * 9/10; |
pHysiX | 32:7a9be7761c46 | 41 | |
pHysiX | 32:7a9be7761c46 | 42 | ESCpower[0] = constrainESC(throttle + (adjust[1]) + (adjust[2]) - adjust[0]); |
pHysiX | 32:7a9be7761c46 | 43 | ESCpower[1] = constrainESC(throttle + (adjust[1]) - (adjust[2]) + adjust[0]); |
pHysiX | 32:7a9be7761c46 | 44 | ESCpower[2] = constrainESC(throttle - (adjust[1]) - (adjust[2]) - adjust[0]); |
pHysiX | 32:7a9be7761c46 | 45 | ESCpower[3] = constrainESC(throttle - (adjust[1]) + (adjust[2]) + adjust[0]); |
pHysiX | 10:ef5fe86f67fe | 46 | |
pHysiX | 21:b642c18eccd1 | 47 | for (int i = 0; i < 4; i++) |
pHysiX | 21:b642c18eccd1 | 48 | ESC[i].pulsewidth_us(ESCpower[i]); |
pHysiX | 10:ef5fe86f67fe | 49 | |
pHysiX | 21:b642c18eccd1 | 50 | counterESC = false; |
pHysiX | 21:b642c18eccd1 | 51 | } else { |
pHysiX | 21:b642c18eccd1 | 52 | stallESC++; |
pHysiX | 21:b642c18eccd1 | 53 | if (stallESC > 1) { |
pHysiX | 21:b642c18eccd1 | 54 | imu.debugSerial.printf("ESC NOT UPDATED FAST ENOUGH!\n"); |
pHysiX | 36:d95e3d6f2fc4 | 55 | BT.printf("ESC NOT UPDATED FAST ENOUGH!\n"); |
pHysiX | 21:b642c18eccd1 | 56 | stallESC = 0; |
pHysiX | 21:b642c18eccd1 | 57 | } |
pHysiX | 21:b642c18eccd1 | 58 | } |
pHysiX | 10:ef5fe86f67fe | 59 | } else { |
pHysiX | 21:b642c18eccd1 | 60 | for (int i = 0; i < 4; i++) { |
pHysiX | 21:b642c18eccd1 | 61 | ESCpower[i] = 980; |
pHysiX | 21:b642c18eccd1 | 62 | ESC[i].pulsewidth_us(ESCpower[i]); |
pHysiX | 10:ef5fe86f67fe | 63 | } |
pHysiX | 10:ef5fe86f67fe | 64 | } |
pHysiX | 10:ef5fe86f67fe | 65 | } else { |
pHysiX | 21:b642c18eccd1 | 66 | if (ESC_check) { |
pHysiX | 21:b642c18eccd1 | 67 | BT.printf("Need to ARM to check ESC output!\n"); |
pHysiX | 21:b642c18eccd1 | 68 | ESC_check = false; |
pHysiX | 21:b642c18eccd1 | 69 | } |
pHysiX | 21:b642c18eccd1 | 70 | |
pHysiX | 21:b642c18eccd1 | 71 | for (int i = 0; i < 4; i++) { |
pHysiX | 32:7a9be7761c46 | 72 | ESCpower[i] = 990; |
pHysiX | 10:ef5fe86f67fe | 73 | ESC[i].pulsewidth_us(ESCpower[i]); |
pHysiX | 21:b642c18eccd1 | 74 | } |
pHysiX | 10:ef5fe86f67fe | 75 | } |
pHysiX | 31:3dde2201e54d | 76 | //Timer |
pHysiX | 19:bd88749c8db4 | 77 | } |
pHysiX | 19:bd88749c8db4 | 78 | |
pHysiX | 34:228d87c45151 | 79 | |
pHysiX | 34:228d87c45151 | 80 | |
pHysiX | 34:228d87c45151 | 81 | |
pHysiX | 34:228d87c45151 | 82 | // ************************ |
pHysiX | 34:228d87c45151 | 83 | // *** Helper functions *** |
pHysiX | 34:228d87c45151 | 84 | // ************************ |
pHysiX | 21:b642c18eccd1 | 85 | int constrainESC(float input) |
pHysiX | 19:bd88749c8db4 | 86 | { |
pHysiX | 32:7a9be7761c46 | 87 | if (input < 1100.0) |
pHysiX | 32:7a9be7761c46 | 88 | return 1100; |
pHysiX | 21:b642c18eccd1 | 89 | else if (input > 2000.0) |
pHysiX | 19:bd88749c8db4 | 90 | return 2000; |
pHysiX | 19:bd88749c8db4 | 91 | else |
pHysiX | 21:b642c18eccd1 | 92 | return (int) input; |
pHysiX | 19:bd88749c8db4 | 93 | } |