Ian Hua / Quadcopter-mbedRTOS
Committer:
pHysiX
Date:
Thu May 08 13:00:50 2014 +0000
Revision:
24:54a8cdf17378
Parent:
22:ef8aa9728013
Child:
25:a7cfe421cb4a
Stable mode works. Already begun to start implementing MODE SWITCHING

Who changed what in which revision?

UserRevisionLine numberNew 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 24:54a8cdf17378 52 //pitchPIDstable.reset();
pHysiX 24:54a8cdf17378 53 //rollPIDstable.reset();
pHysiX 24:54a8cdf17378 54 //yawPIDrate.reset();
pHysiX 24:54a8cdf17378 55 //pitchPIDrate.reset();
pHysiX 24:54a8cdf17378 56 //rollPIDrate.reset();
pHysiX 10:ef5fe86f67fe 57 }
pHysiX 10:ef5fe86f67fe 58 }
pHysiX 10:ef5fe86f67fe 59 } else {
pHysiX 21:b642c18eccd1 60 if (ESC_check) {
pHysiX 21:b642c18eccd1 61 BT.printf("Need to ARM to check ESC output!\n");
pHysiX 21:b642c18eccd1 62 ESC_check = false;
pHysiX 21:b642c18eccd1 63 }
pHysiX 21:b642c18eccd1 64
pHysiX 21:b642c18eccd1 65 for (int i = 0; i < 4; i++) {
pHysiX 21:b642c18eccd1 66 ESCpower[i] = 980;
pHysiX 10:ef5fe86f67fe 67 ESC[i].pulsewidth_us(ESCpower[i]);
pHysiX 21:b642c18eccd1 68 }
pHysiX 10:ef5fe86f67fe 69 }
pHysiX 3:605fbcb54e75 70
pHysiX 21:b642c18eccd1 71 if (ESC_check)
pHysiX 21:b642c18eccd1 72 BT.printf("%4d %4d %4d %4d\n", ESCpower[0], ESCpower[1], ESCpower[2], ESCpower[3]);
pHysiX 19:bd88749c8db4 73 }
pHysiX 19:bd88749c8db4 74
pHysiX 21:b642c18eccd1 75 int constrainESC(float input)
pHysiX 19:bd88749c8db4 76 {
pHysiX 21:b642c18eccd1 77 if (input < 1150.0)
pHysiX 21:b642c18eccd1 78 return 1150;
pHysiX 21:b642c18eccd1 79 else if (input > 2000.0)
pHysiX 19:bd88749c8db4 80 return 2000;
pHysiX 19:bd88749c8db4 81 else
pHysiX 21:b642c18eccd1 82 return (int) input;
pHysiX 19:bd88749c8db4 83 }