Ian Hua / Quadcopter-mbedRTOS
Committer:
pHysiX
Date:
Mon May 19 14:16:47 2014 +0000
Revision:
50:8a0accb23007
Parent:
44:4be5c01c6de2
Child:
51:04c6af4319e1
Semaphores implemented, currently no hanging. Need to test flight

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 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 50:8a0accb23007 9 * Timing:
pHysiX 30:d9b988f8d84f 10 */
pHysiX 1:43f8ac7ca6d7 11 #include "tasks.h"
pHysiX 1:43f8ac7ca6d7 12 #include "setup.h"
pHysiX 1:43f8ac7ca6d7 13
pHysiX 50:8a0accb23007 14 Semaphore sem_Task4(1);
pHysiX 50:8a0accb23007 15
pHysiX 44:4be5c01c6de2 16 volatile int ESCpower[4] = {0, 0, 0, 0};
pHysiX 10:ef5fe86f67fe 17 int stallESC = 0;
pHysiX 10:ef5fe86f67fe 18
pHysiX 14:267368c83b6a 19 bool armed = false;
pHysiX 21:b642c18eccd1 20 bool ESC_check = false;
pHysiX 21:b642c18eccd1 21 bool calibration_mode = false;
pHysiX 2:ab967d7b4346 22
pHysiX 34:228d87c45151 23
pHysiX 34:228d87c45151 24
pHysiX 34:228d87c45151 25
pHysiX 34:228d87c45151 26 // ==================
pHysiX 34:228d87c45151 27 // === ESC UPDATE ===
pHysiX 34:228d87c45151 28 // ==================
pHysiX 1:43f8ac7ca6d7 29 void Task4(void const *argurment)
pHysiX 1:43f8ac7ca6d7 30 {
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 37:29feef05d848 52 //stallESC++;
pHysiX 37:29feef05d848 53 //if (stallESC > 1) {
pHysiX 37:29feef05d848 54 // imu.debugSerial.printf("ESC NOT UPDATED FAST ENOUGH!\n");
pHysiX 37:29feef05d848 55 // BT.printf("ESC NOT UPDATED FAST ENOUGH!\n");
pHysiX 37:29feef05d848 56 // stallESC = 0;
pHysiX 37:29feef05d848 57 //}
pHysiX 21:b642c18eccd1 58 }
pHysiX 10:ef5fe86f67fe 59 } else {
pHysiX 21:b642c18eccd1 60 for (int i = 0; i < 4; i++) {
pHysiX 38:ef65533cca32 61 ESCpower[i] = 1000;
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 19:bd88749c8db4 76 }
pHysiX 19:bd88749c8db4 77
pHysiX 34:228d87c45151 78
pHysiX 34:228d87c45151 79
pHysiX 34:228d87c45151 80
pHysiX 34:228d87c45151 81 // ************************
pHysiX 34:228d87c45151 82 // *** Helper functions ***
pHysiX 34:228d87c45151 83 // ************************
pHysiX 50:8a0accb23007 84 void Task4_ISR(void const *argument)
pHysiX 50:8a0accb23007 85 {
pHysiX 50:8a0accb23007 86 //sem_Task4.release();
pHysiX 50:8a0accb23007 87 }
pHysiX 50:8a0accb23007 88
pHysiX 21:b642c18eccd1 89 int constrainESC(float input)
pHysiX 19:bd88749c8db4 90 {
pHysiX 32:7a9be7761c46 91 if (input < 1100.0)
pHysiX 32:7a9be7761c46 92 return 1100;
pHysiX 21:b642c18eccd1 93 else if (input > 2000.0)
pHysiX 19:bd88749c8db4 94 return 2000;
pHysiX 19:bd88749c8db4 95 else
pHysiX 21:b642c18eccd1 96 return (int) input;
pHysiX 19:bd88749c8db4 97 }