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