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@31:3dde2201e54d, 2014-05-12 (annotated)
- Committer:
- pHysiX
- Date:
- Mon May 12 05:12:19 2014 +0000
- Revision:
- 31:3dde2201e54d
- Parent:
- 30:d9b988f8d84f
- Child:
- 32:7a9be7761c46
Rearranged threads and approach
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 | 31:3dde2201e54d | 20 | //Timer |
pHysiX | 1:43f8ac7ca6d7 | 21 | void Task4(void const *argurment) |
pHysiX | 1:43f8ac7ca6d7 | 22 | { |
pHysiX | 31:3dde2201e54d | 23 | //Timer |
pHysiX | 14:267368c83b6a | 24 | if (armed) { |
pHysiX | 21:b642c18eccd1 | 25 | if (calibration_mode) { |
pHysiX | 19:bd88749c8db4 | 26 | for (int i = 0; i < 4; i++) |
pHysiX | 21:b642c18eccd1 | 27 | ESC[i].pulsewidth_us(RCCommand[3]); |
pHysiX | 21:b642c18eccd1 | 28 | } else if (RCCommand[3] > 1150) { |
pHysiX | 30:d9b988f8d84f | 29 | if (counterESC) { |
pHysiX | 21:b642c18eccd1 | 30 | for (int i = 0; i < 3; i++) |
pHysiX | 21:b642c18eccd1 | 31 | adjust[i] /= 2.0; |
pHysiX | 30:d9b988f8d84f | 32 | |
pHysiX | 30:d9b988f8d84f | 33 | ESCpower[0] = constrainESC((RCCommand[3]) + (adjust[1]) + (adjust[2]) - adjust[0]); |
pHysiX | 30:d9b988f8d84f | 34 | ESCpower[1] = constrainESC((RCCommand[3]) + (adjust[1]) - (adjust[2]) + adjust[0]); |
pHysiX | 30:d9b988f8d84f | 35 | ESCpower[2] = constrainESC((RCCommand[3]) - (adjust[1]) - (adjust[2]) - adjust[0]); |
pHysiX | 30:d9b988f8d84f | 36 | ESCpower[3] = constrainESC((RCCommand[3]) - (adjust[1]) + (adjust[2]) + adjust[0]); |
pHysiX | 10:ef5fe86f67fe | 37 | |
pHysiX | 21:b642c18eccd1 | 38 | for (int i = 0; i < 4; i++) |
pHysiX | 21:b642c18eccd1 | 39 | ESC[i].pulsewidth_us(ESCpower[i]); |
pHysiX | 10:ef5fe86f67fe | 40 | |
pHysiX | 21:b642c18eccd1 | 41 | counterESC = false; |
pHysiX | 21:b642c18eccd1 | 42 | } else { |
pHysiX | 21:b642c18eccd1 | 43 | stallESC++; |
pHysiX | 3:605fbcb54e75 | 44 | |
pHysiX | 21:b642c18eccd1 | 45 | if (stallESC > 1) { |
pHysiX | 21:b642c18eccd1 | 46 | imu.debugSerial.printf("ESC NOT UPDATED FAST ENOUGH!\n"); |
pHysiX | 21:b642c18eccd1 | 47 | stallESC = 0; |
pHysiX | 21:b642c18eccd1 | 48 | } |
pHysiX | 21:b642c18eccd1 | 49 | } |
pHysiX | 10:ef5fe86f67fe | 50 | } else { |
pHysiX | 21:b642c18eccd1 | 51 | for (int i = 0; i < 4; i++) { |
pHysiX | 21:b642c18eccd1 | 52 | ESCpower[i] = 980; |
pHysiX | 21:b642c18eccd1 | 53 | ESC[i].pulsewidth_us(ESCpower[i]); |
pHysiX | 10:ef5fe86f67fe | 54 | } |
pHysiX | 10:ef5fe86f67fe | 55 | } |
pHysiX | 10:ef5fe86f67fe | 56 | } else { |
pHysiX | 21:b642c18eccd1 | 57 | if (ESC_check) { |
pHysiX | 21:b642c18eccd1 | 58 | BT.printf("Need to ARM to check ESC output!\n"); |
pHysiX | 21:b642c18eccd1 | 59 | ESC_check = false; |
pHysiX | 21:b642c18eccd1 | 60 | } |
pHysiX | 21:b642c18eccd1 | 61 | |
pHysiX | 21:b642c18eccd1 | 62 | for (int i = 0; i < 4; i++) { |
pHysiX | 21:b642c18eccd1 | 63 | ESCpower[i] = 980; |
pHysiX | 10:ef5fe86f67fe | 64 | ESC[i].pulsewidth_us(ESCpower[i]); |
pHysiX | 21:b642c18eccd1 | 65 | } |
pHysiX | 10:ef5fe86f67fe | 66 | } |
pHysiX | 3:605fbcb54e75 | 67 | |
pHysiX | 31:3dde2201e54d | 68 | //if (ESC_check) |
pHysiX | 31:3dde2201e54d | 69 | // BT.printf("%4d %4d %4d %4d\n", ESCpower[0], ESCpower[1], ESCpower[2], ESCpower[3]); |
pHysiX | 30:d9b988f8d84f | 70 | |
pHysiX | 31:3dde2201e54d | 71 | //Timer |
pHysiX | 19:bd88749c8db4 | 72 | } |
pHysiX | 19:bd88749c8db4 | 73 | |
pHysiX | 21:b642c18eccd1 | 74 | int constrainESC(float input) |
pHysiX | 19:bd88749c8db4 | 75 | { |
pHysiX | 21:b642c18eccd1 | 76 | if (input < 1150.0) |
pHysiX | 21:b642c18eccd1 | 77 | return 1150; |
pHysiX | 21:b642c18eccd1 | 78 | else if (input > 2000.0) |
pHysiX | 19:bd88749c8db4 | 79 | return 2000; |
pHysiX | 19:bd88749c8db4 | 80 | else |
pHysiX | 21:b642c18eccd1 | 81 | return (int) input; |
pHysiX | 19:bd88749c8db4 | 82 | } |