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@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 | 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 | 22:ef8aa9728013 | 5 | * Settings: 200Hz |
pHysiX | 22:ef8aa9728013 | 6 | * 200Hz <= PWM frequency <= 400Hz |
pHysiX | 30:d9b988f8d84f | 7 | * Refer to tasks.h to change PWM frequency |
pHysiX | 30:d9b988f8d84f | 8 | * Timing: 1500us |
pHysiX | 30:d9b988f8d84f | 9 | */ |
pHysiX | 1:43f8ac7ca6d7 | 10 | #include "tasks.h" |
pHysiX | 1:43f8ac7ca6d7 | 11 | #include "setup.h" |
pHysiX | 1:43f8ac7ca6d7 | 12 | |
pHysiX | 2:ab967d7b4346 | 13 | int ESCpower[4] = {0, 0, 0, 0}; |
pHysiX | 10:ef5fe86f67fe | 14 | int stallESC = 0; |
pHysiX | 10:ef5fe86f67fe | 15 | |
pHysiX | 14:267368c83b6a | 16 | bool armed = false; |
pHysiX | 21:b642c18eccd1 | 17 | bool ESC_check = false; |
pHysiX | 21:b642c18eccd1 | 18 | bool calibration_mode = false; |
pHysiX | 2:ab967d7b4346 | 19 | |
pHysiX | 34:228d87c45151 | 20 | |
pHysiX | 34:228d87c45151 | 21 | |
pHysiX | 34:228d87c45151 | 22 | |
pHysiX | 34:228d87c45151 | 23 | // ================== |
pHysiX | 34:228d87c45151 | 24 | // === ESC UPDATE === |
pHysiX | 34:228d87c45151 | 25 | // ================== |
pHysiX | 31:3dde2201e54d | 26 | //Timer |
pHysiX | 1:43f8ac7ca6d7 | 27 | void Task4(void const *argurment) |
pHysiX | 1:43f8ac7ca6d7 | 28 | { |
pHysiX | 31:3dde2201e54d | 29 | //Timer |
pHysiX | 14:267368c83b6a | 30 | if (armed) { |
pHysiX | 21:b642c18eccd1 | 31 | if (calibration_mode) { |
pHysiX | 19:bd88749c8db4 | 32 | for (int i = 0; i < 4; i++) |
pHysiX | 21:b642c18eccd1 | 33 | ESC[i].pulsewidth_us(RCCommand[3]); |
pHysiX | 32:7a9be7761c46 | 34 | } else if (RCCommand[3] > 1100) { |
pHysiX | 30:d9b988f8d84f | 35 | if (counterESC) { |
pHysiX | 21:b642c18eccd1 | 36 | for (int i = 0; i < 3; i++) |
pHysiX | 21:b642c18eccd1 | 37 | adjust[i] /= 2.0; |
pHysiX | 30:d9b988f8d84f | 38 | |
pHysiX | 32:7a9be7761c46 | 39 | int throttle = RCCommand[3] * 9/10; |
pHysiX | 32:7a9be7761c46 | 40 | |
pHysiX | 32:7a9be7761c46 | 41 | ESCpower[0] = constrainESC(throttle + (adjust[1]) + (adjust[2]) - adjust[0]); |
pHysiX | 32:7a9be7761c46 | 42 | ESCpower[1] = constrainESC(throttle + (adjust[1]) - (adjust[2]) + adjust[0]); |
pHysiX | 32:7a9be7761c46 | 43 | ESCpower[2] = constrainESC(throttle - (adjust[1]) - (adjust[2]) - adjust[0]); |
pHysiX | 32:7a9be7761c46 | 44 | ESCpower[3] = constrainESC(throttle - (adjust[1]) + (adjust[2]) + adjust[0]); |
pHysiX | 10:ef5fe86f67fe | 45 | |
pHysiX | 21:b642c18eccd1 | 46 | for (int i = 0; i < 4; i++) |
pHysiX | 21:b642c18eccd1 | 47 | ESC[i].pulsewidth_us(ESCpower[i]); |
pHysiX | 10:ef5fe86f67fe | 48 | |
pHysiX | 21:b642c18eccd1 | 49 | counterESC = false; |
pHysiX | 21:b642c18eccd1 | 50 | } else { |
pHysiX | 21:b642c18eccd1 | 51 | stallESC++; |
pHysiX | 21:b642c18eccd1 | 52 | if (stallESC > 1) { |
pHysiX | 21:b642c18eccd1 | 53 | imu.debugSerial.printf("ESC NOT UPDATED FAST ENOUGH!\n"); |
pHysiX | 21:b642c18eccd1 | 54 | stallESC = 0; |
pHysiX | 21:b642c18eccd1 | 55 | } |
pHysiX | 21:b642c18eccd1 | 56 | } |
pHysiX | 10:ef5fe86f67fe | 57 | } else { |
pHysiX | 21:b642c18eccd1 | 58 | for (int i = 0; i < 4; i++) { |
pHysiX | 21:b642c18eccd1 | 59 | ESCpower[i] = 980; |
pHysiX | 21:b642c18eccd1 | 60 | ESC[i].pulsewidth_us(ESCpower[i]); |
pHysiX | 10:ef5fe86f67fe | 61 | } |
pHysiX | 10:ef5fe86f67fe | 62 | } |
pHysiX | 10:ef5fe86f67fe | 63 | } else { |
pHysiX | 21:b642c18eccd1 | 64 | if (ESC_check) { |
pHysiX | 21:b642c18eccd1 | 65 | BT.printf("Need to ARM to check ESC output!\n"); |
pHysiX | 21:b642c18eccd1 | 66 | ESC_check = false; |
pHysiX | 21:b642c18eccd1 | 67 | } |
pHysiX | 21:b642c18eccd1 | 68 | |
pHysiX | 21:b642c18eccd1 | 69 | for (int i = 0; i < 4; i++) { |
pHysiX | 32:7a9be7761c46 | 70 | ESCpower[i] = 990; |
pHysiX | 10:ef5fe86f67fe | 71 | ESC[i].pulsewidth_us(ESCpower[i]); |
pHysiX | 21:b642c18eccd1 | 72 | } |
pHysiX | 10:ef5fe86f67fe | 73 | } |
pHysiX | 3:605fbcb54e75 | 74 | |
pHysiX | 31:3dde2201e54d | 75 | //if (ESC_check) |
pHysiX | 32:7a9be7761c46 | 76 | //BT.printf("%4d %4d %4d %4d\n", ESCpower[0], ESCpower[1], ESCpower[2], ESCpower[3]); |
pHysiX | 31:3dde2201e54d | 77 | //Timer |
pHysiX | 19:bd88749c8db4 | 78 | } |
pHysiX | 19:bd88749c8db4 | 79 | |
pHysiX | 34:228d87c45151 | 80 | |
pHysiX | 34:228d87c45151 | 81 | |
pHysiX | 34:228d87c45151 | 82 | |
pHysiX | 34:228d87c45151 | 83 | // ************************ |
pHysiX | 34:228d87c45151 | 84 | // *** Helper functions *** |
pHysiX | 34:228d87c45151 | 85 | // ************************ |
pHysiX | 21:b642c18eccd1 | 86 | int constrainESC(float input) |
pHysiX | 19:bd88749c8db4 | 87 | { |
pHysiX | 32:7a9be7761c46 | 88 | if (input < 1100.0) |
pHysiX | 32:7a9be7761c46 | 89 | return 1100; |
pHysiX | 21:b642c18eccd1 | 90 | else if (input > 2000.0) |
pHysiX | 19:bd88749c8db4 | 91 | return 2000; |
pHysiX | 19:bd88749c8db4 | 92 | else |
pHysiX | 21:b642c18eccd1 | 93 | return (int) input; |
pHysiX | 19:bd88749c8db4 | 94 | } |