ESE350 project, Spring 2016, University of Pennsylvania
Dependencies: Adafruit9-DOf Receiver mbed-rtos mbed
main.cpp@29:ae765492fa8b, 2016-04-15 (annotated)
- Committer:
- ivo_david_michelle
- Date:
- Fri Apr 15 19:33:14 2016 +0000
- Revision:
- 29:ae765492fa8b
- Parent:
- 28:61f7356325c3
- Child:
- 30:4820042e67b5
refactored
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ivo_david_michelle | 0:4c04e4fd1310 | 1 | #include "mbed.h" |
ivo_david_michelle | 14:64b06476d943 | 2 | #include "rtos.h" |
ivo_david_michelle | 1:b87e95907a18 | 3 | #define _MBED_ |
ivo_david_michelle | 13:291ba30c7806 | 4 | //#include "controller.h" |
ivo_david_michelle | 3:828e82089564 | 5 | #include "sensor.h" |
ivo_david_michelle | 7:f3f94eadc5b5 | 6 | #include "quadcopter.h" |
ivo_david_michelle | 7:f3f94eadc5b5 | 7 | |
ivo_david_michelle | 0:4c04e4fd1310 | 8 | Serial pc(USBTX, USBRX); |
ivo_david_michelle | 15:90e07946186f | 9 | MRF24J40 mrf(p11, p12, p13, p14, p21); |
ivo_david_michelle | 15:90e07946186f | 10 | Quadcopter myQuadcopter(&pc, &mrf); // instantiate Quadcopter object |
ivo_david_michelle | 7:f3f94eadc5b5 | 11 | |
ivo_david_michelle | 6:6f3ffd97d808 | 12 | // pwm outputs for the 4 motors (motor 1 points into x direction, 2 into y direction, 3 -x direction, -y direction |
ivo_david_michelle | 15:90e07946186f | 13 | PwmOut motor_1(p23); |
ivo_david_michelle | 15:90e07946186f | 14 | PwmOut motor_2(p24); |
ivo_david_michelle | 15:90e07946186f | 15 | PwmOut motor_3(p25); |
ivo_david_michelle | 15:90e07946186f | 16 | PwmOut motor_4(p26); |
ivo_david_michelle | 15:90e07946186f | 17 | |
ivo_david_michelle | 22:92401a4fec13 | 18 | // to read the battery voltage |
ivo_david_michelle | 22:92401a4fec13 | 19 | AnalogIn battery(p20); |
ivo_david_michelle | 26:7f50323c0c0d | 20 | DigitalOut batteryLed(LED1); |
ivo_david_michelle | 26:7f50323c0c0d | 21 | |
ivo_david_michelle | 28:61f7356325c3 | 22 | int emergencyOff = 0; |
ivo_david_michelle | 4:3040d0f9e8c6 | 23 | |
ivo_david_michelle | 15:90e07946186f | 24 | void controller_thread(void const *args) |
ivo_david_michelle | 15:90e07946186f | 25 | { |
ivo_david_michelle | 26:7f50323c0c0d | 26 | while(emergencyOff != 1) { |
ivo_david_michelle | 23:04338a5ef404 | 27 | myQuadcopter.readSensorValues(); |
ivo_david_michelle | 19:39c144ca2a2f | 28 | |
ivo_david_michelle | 15:90e07946186f | 29 | myQuadcopter.controller(); |
ivo_david_michelle | 28:61f7356325c3 | 30 | motors motorsPwm = myQuadcopter.getPwm(); |
ivo_david_michelle | 18:a00d6b065c6b | 31 | |
ivo_david_michelle | 28:61f7356325c3 | 32 | motor_1 = motorsPwm.m1; |
ivo_david_michelle | 28:61f7356325c3 | 33 | motor_2 = motorsPwm.m2; |
ivo_david_michelle | 28:61f7356325c3 | 34 | motor_3 = motorsPwm.m3; |
ivo_david_michelle | 28:61f7356325c3 | 35 | motor_4 = motorsPwm.m4; |
ivo_david_michelle | 26:7f50323c0c0d | 36 | |
ivo_david_michelle | 22:92401a4fec13 | 37 | // pc.printf("m1: %f m2: %f m3: %f m4: %f \n\r", motorsPwm.m1, motorsPwm.m2, motorsPwm.m3, motorsPwm.m4); |
ivo_david_michelle | 15:90e07946186f | 38 | } |
ivo_david_michelle | 14:64b06476d943 | 39 | } |
ivo_david_michelle | 9:f1bd96708a21 | 40 | |
ivo_david_michelle | 15:90e07946186f | 41 | void rc_thread(void const *args) |
ivo_david_michelle | 15:90e07946186f | 42 | { |
ivo_david_michelle | 15:90e07946186f | 43 | while(true) { |
ivo_david_michelle | 15:90e07946186f | 44 | myQuadcopter.readRc(); |
ivo_david_michelle | 15:90e07946186f | 45 | } |
ivo_david_michelle | 14:64b06476d943 | 46 | } |
ivo_david_michelle | 14:64b06476d943 | 47 | |
ivo_david_michelle | 22:92401a4fec13 | 48 | void battery_thread(void const *args) |
ivo_david_michelle | 22:92401a4fec13 | 49 | { |
ivo_david_michelle | 22:92401a4fec13 | 50 | float threshold_voltage = 13.0; // desired lowest battery voltage |
ivo_david_michelle | 26:7f50323c0c0d | 51 | float emergencyVoltage = 12.5; // switch off motors below it |
ivo_david_michelle | 22:92401a4fec13 | 52 | float max_voltage = 14.8; // max voltage level of battery |
ivo_david_michelle | 22:92401a4fec13 | 53 | float saturating_voltage = 18.38; // voltage at which ADC == 1 |
ivo_david_michelle | 22:92401a4fec13 | 54 | float max_adc = 0.80522; // when battery is at 14.8V |
ivo_david_michelle | 22:92401a4fec13 | 55 | float threshold_adc = max_adc * threshold_voltage / max_voltage; |
ivo_david_michelle | 26:7f50323c0c0d | 56 | float emergency_adc = max_adc * emergencyVoltage / max_voltage; |
ivo_david_michelle | 26:7f50323c0c0d | 57 | |
ivo_david_michelle | 22:92401a4fec13 | 58 | while(true) { |
ivo_david_michelle | 22:92401a4fec13 | 59 | if (battery.read() < threshold_adc) { |
ivo_david_michelle | 22:92401a4fec13 | 60 | printf("low battery! %f\r\n", battery.read() * saturating_voltage); |
ivo_david_michelle | 29:ae765492fa8b | 61 | batteryLed = 1; |
ivo_david_michelle | 26:7f50323c0c0d | 62 | if (battery.read() < emergency_adc) { |
ivo_david_michelle | 29:ae765492fa8b | 63 | emergencyOff = 1; |
ivo_david_michelle | 29:ae765492fa8b | 64 | motor_1 = 0.1; |
ivo_david_michelle | 29:ae765492fa8b | 65 | motor_2 = 0.1; |
ivo_david_michelle | 29:ae765492fa8b | 66 | motor_3 = 0.1; |
ivo_david_michelle | 29:ae765492fa8b | 67 | motor_4 = 0.1; |
ivo_david_michelle | 26:7f50323c0c0d | 68 | } |
ivo_david_michelle | 22:92401a4fec13 | 69 | } |
ivo_david_michelle | 22:92401a4fec13 | 70 | Thread::wait(1000); // wait for some number of miliseconds |
ivo_david_michelle | 22:92401a4fec13 | 71 | } |
ivo_david_michelle | 22:92401a4fec13 | 72 | } |
ivo_david_michelle | 22:92401a4fec13 | 73 | |
ivo_david_michelle | 22:92401a4fec13 | 74 | |
ivo_david_michelle | 14:64b06476d943 | 75 | |
ivo_david_michelle | 15:90e07946186f | 76 | int main() |
ivo_david_michelle | 15:90e07946186f | 77 | { |
ivo_david_michelle | 29:ae765492fa8b | 78 | // ESCs requires a 100Hz frequency |
ivo_david_michelle | 29:ae765492fa8b | 79 | motor_1.period(0.01); |
ivo_david_michelle | 29:ae765492fa8b | 80 | motor_2.period(0.01); |
ivo_david_michelle | 29:ae765492fa8b | 81 | motor_3.period(0.01); |
ivo_david_michelle | 29:ae765492fa8b | 82 | motor_4.period(0.01); |
ivo_david_michelle | 22:92401a4fec13 | 83 | |
ivo_david_michelle | 27:11116aa69f32 | 84 | Thread threadR(rc_thread); |
ivo_david_michelle | 27:11116aa69f32 | 85 | double forceThreshold = -0.3; |
ivo_david_michelle | 27:11116aa69f32 | 86 | double forceRc = myQuadcopter.getForce(); |
ivo_david_michelle | 28:61f7356325c3 | 87 | |
ivo_david_michelle | 29:ae765492fa8b | 88 | // TODO figure out why forceRc occasionally goes below the threshold without input |
ivo_david_michelle | 28:61f7356325c3 | 89 | while (forceRc > forceThreshold) { // wait until Joystick is in starting position |
ivo_david_michelle | 27:11116aa69f32 | 90 | forceRc = myQuadcopter.getForce(); |
ivo_david_michelle | 28:61f7356325c3 | 91 | wait(1); |
ivo_david_michelle | 27:11116aa69f32 | 92 | } |
ivo_david_michelle | 27:11116aa69f32 | 93 | |
ivo_david_michelle | 22:92401a4fec13 | 94 | motor_1 = 0.1; |
ivo_david_michelle | 22:92401a4fec13 | 95 | motor_2 = 0.1; |
ivo_david_michelle | 22:92401a4fec13 | 96 | motor_3 = 0.1; |
ivo_david_michelle | 22:92401a4fec13 | 97 | motor_4 = 0.1; |
ivo_david_michelle | 4:3040d0f9e8c6 | 98 | |
ivo_david_michelle | 28:61f7356325c3 | 99 | wait(2); // hold startup duty cycle for 2 seconds |
ivo_david_michelle | 22:92401a4fec13 | 100 | |
ivo_david_michelle | 29:ae765492fa8b | 101 | // TODO assign priorities to threads, test if it really works as we expect |
ivo_david_michelle | 29:ae765492fa8b | 102 | Thread battery(battery_thread); |
ivo_david_michelle | 23:04338a5ef404 | 103 | Thread thread(controller_thread); |
ivo_david_michelle | 14:64b06476d943 | 104 | |
ivo_david_michelle | 29:ae765492fa8b | 105 | // TODO is this needed? |
ivo_david_michelle | 29:ae765492fa8b | 106 | while (1); |
ivo_david_michelle | 0:4c04e4fd1310 | 107 | } |