ESE350 project, Spring 2016, University of Pennsylvania

Dependencies:   Adafruit9-DOf Receiver mbed-rtos mbed

Committer:
ivo_david_michelle
Date:
Sun Apr 24 17:15:20 2016 +0000
Revision:
34:eaea0ae92dfa
Parent:
31:d473eacfc271
Child:
36:40b134328376
testing with kalman

Who changed what in which revision?

UserRevisionLine numberNew 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 34:eaea0ae92dfa 10 Timer timer; // timer ;)
ivo_david_michelle 34:eaea0ae92dfa 11 Quadcopter myQuadcopter(&pc, &mrf, &timer); // instantiate Quadcopter object
ivo_david_michelle 7:f3f94eadc5b5 12
ivo_david_michelle 6:6f3ffd97d808 13 // 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 14 PwmOut motor_1(p23);
ivo_david_michelle 15:90e07946186f 15 PwmOut motor_2(p24);
ivo_david_michelle 15:90e07946186f 16 PwmOut motor_3(p25);
ivo_david_michelle 15:90e07946186f 17 PwmOut motor_4(p26);
ivo_david_michelle 15:90e07946186f 18
ivo_david_michelle 22:92401a4fec13 19 // to read the battery voltage
ivo_david_michelle 22:92401a4fec13 20 AnalogIn battery(p20);
ivo_david_michelle 26:7f50323c0c0d 21 DigitalOut batteryLed(LED1);
ivo_david_michelle 30:4820042e67b5 22 DigitalOut shutDownLed(LED2);
ivo_david_michelle 26:7f50323c0c0d 23
ivo_david_michelle 28:61f7356325c3 24 int emergencyOff = 0;
ivo_david_michelle 30:4820042e67b5 25 //int lowThrust= {0,0,0};
ivo_david_michelle 30:4820042e67b5 26 int nLowThrust = 0;
ivo_david_michelle 30:4820042e67b5 27
ivo_david_michelle 30:4820042e67b5 28 void emergencyShutdown()
ivo_david_michelle 30:4820042e67b5 29 {
ivo_david_michelle 30:4820042e67b5 30 emergencyOff = 1;
ivo_david_michelle 30:4820042e67b5 31 motor_1 = 0.1;
ivo_david_michelle 30:4820042e67b5 32 motor_2 = 0.1;
ivo_david_michelle 30:4820042e67b5 33 motor_3 = 0.1;
ivo_david_michelle 30:4820042e67b5 34 motor_4 = 0.1;
ivo_david_michelle 30:4820042e67b5 35 }
ivo_david_michelle 30:4820042e67b5 36
ivo_david_michelle 30:4820042e67b5 37 int getLowThrust(double threshold)
ivo_david_michelle 30:4820042e67b5 38 {
ivo_david_michelle 30:4820042e67b5 39 double force = myQuadcopter.getForce();
ivo_david_michelle 30:4820042e67b5 40 if (force < threshold) { // if low thrust signal is detected
ivo_david_michelle 30:4820042e67b5 41 nLowThrust++;
ivo_david_michelle 30:4820042e67b5 42 printf("Negative thrust! %f, nLowThrust %d\r\n",myQuadcopter.getForce(),nLowThrust);
ivo_david_michelle 30:4820042e67b5 43 if (nLowThrust > 5) {
ivo_david_michelle 30:4820042e67b5 44 return 1;
ivo_david_michelle 30:4820042e67b5 45 }
ivo_david_michelle 30:4820042e67b5 46 } else {
ivo_david_michelle 30:4820042e67b5 47 nLowThrust = 0;
ivo_david_michelle 30:4820042e67b5 48 }
ivo_david_michelle 30:4820042e67b5 49 return 0;
ivo_david_michelle 30:4820042e67b5 50 }
ivo_david_michelle 4:3040d0f9e8c6 51
ivo_david_michelle 15:90e07946186f 52 void controller_thread(void const *args)
ivo_david_michelle 15:90e07946186f 53 {
ivo_david_michelle 26:7f50323c0c0d 54 while(emergencyOff != 1) {
ivo_david_michelle 30:4820042e67b5 55 // printf(" thrust: %f\r\n",myQuadcopter.getForce());
ivo_david_michelle 30:4820042e67b5 56
ivo_david_michelle 34:eaea0ae92dfa 57 int shutdown = getLowThrust(-0.3);
ivo_david_michelle 30:4820042e67b5 58 if (shutdown==1) {
ivo_david_michelle 30:4820042e67b5 59 emergencyShutdown();
ivo_david_michelle 30:4820042e67b5 60 printf("too long negative thrust! %f\r\n",myQuadcopter.getForce());
ivo_david_michelle 30:4820042e67b5 61 shutDownLed = 1;
ivo_david_michelle 30:4820042e67b5 62 break;
ivo_david_michelle 30:4820042e67b5 63 }
ivo_david_michelle 30:4820042e67b5 64
ivo_david_michelle 23:04338a5ef404 65 myQuadcopter.readSensorValues();
ivo_david_michelle 19:39c144ca2a2f 66
ivo_david_michelle 34:eaea0ae92dfa 67 myQuadcopter.controller();
ivo_david_michelle 28:61f7356325c3 68 motors motorsPwm = myQuadcopter.getPwm();
ivo_david_michelle 18:a00d6b065c6b 69
ivo_david_michelle 28:61f7356325c3 70 motor_1 = motorsPwm.m1;
ivo_david_michelle 28:61f7356325c3 71 motor_2 = motorsPwm.m2;
ivo_david_michelle 28:61f7356325c3 72 motor_3 = motorsPwm.m3;
ivo_david_michelle 28:61f7356325c3 73 motor_4 = motorsPwm.m4;
ivo_david_michelle 26:7f50323c0c0d 74
ivo_david_michelle 22:92401a4fec13 75 // 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 76 }
ivo_david_michelle 14:64b06476d943 77 }
ivo_david_michelle 9:f1bd96708a21 78
ivo_david_michelle 15:90e07946186f 79 void rc_thread(void const *args)
ivo_david_michelle 15:90e07946186f 80 {
ivo_david_michelle 15:90e07946186f 81 while(true) {
ivo_david_michelle 15:90e07946186f 82 myQuadcopter.readRc();
ivo_david_michelle 15:90e07946186f 83 }
ivo_david_michelle 14:64b06476d943 84 }
ivo_david_michelle 14:64b06476d943 85
ivo_david_michelle 22:92401a4fec13 86 void battery_thread(void const *args)
ivo_david_michelle 22:92401a4fec13 87 {
ivo_david_michelle 22:92401a4fec13 88 float threshold_voltage = 13.0; // desired lowest battery voltage
ivo_david_michelle 26:7f50323c0c0d 89 float emergencyVoltage = 12.5; // switch off motors below it
ivo_david_michelle 22:92401a4fec13 90 float max_voltage = 14.8; // max voltage level of battery
ivo_david_michelle 22:92401a4fec13 91 float saturating_voltage = 18.38; // voltage at which ADC == 1
ivo_david_michelle 22:92401a4fec13 92 float max_adc = 0.80522; // when battery is at 14.8V
ivo_david_michelle 22:92401a4fec13 93 float threshold_adc = max_adc * threshold_voltage / max_voltage;
ivo_david_michelle 26:7f50323c0c0d 94 float emergency_adc = max_adc * emergencyVoltage / max_voltage;
ivo_david_michelle 26:7f50323c0c0d 95
ivo_david_michelle 22:92401a4fec13 96 while(true) {
ivo_david_michelle 22:92401a4fec13 97 if (battery.read() < threshold_adc) {
ivo_david_michelle 22:92401a4fec13 98 printf("low battery! %f\r\n", battery.read() * saturating_voltage);
ivo_david_michelle 29:ae765492fa8b 99 batteryLed = 1;
ivo_david_michelle 26:7f50323c0c0d 100 if (battery.read() < emergency_adc) {
ivo_david_michelle 30:4820042e67b5 101 emergencyShutdown();
ivo_david_michelle 30:4820042e67b5 102 break;
ivo_david_michelle 26:7f50323c0c0d 103 }
ivo_david_michelle 22:92401a4fec13 104 }
ivo_david_michelle 22:92401a4fec13 105 Thread::wait(1000); // wait for some number of miliseconds
ivo_david_michelle 22:92401a4fec13 106 }
ivo_david_michelle 22:92401a4fec13 107 }
ivo_david_michelle 22:92401a4fec13 108
ivo_david_michelle 15:90e07946186f 109 int main()
ivo_david_michelle 15:90e07946186f 110 {
ivo_david_michelle 29:ae765492fa8b 111 // ESCs requires a 100Hz frequency
ivo_david_michelle 29:ae765492fa8b 112 motor_1.period(0.01);
ivo_david_michelle 29:ae765492fa8b 113 motor_2.period(0.01);
ivo_david_michelle 29:ae765492fa8b 114 motor_3.period(0.01);
ivo_david_michelle 29:ae765492fa8b 115 motor_4.period(0.01);
ivo_david_michelle 22:92401a4fec13 116
ivo_david_michelle 27:11116aa69f32 117 Thread threadR(rc_thread);
ivo_david_michelle 30:4820042e67b5 118
ivo_david_michelle 34:eaea0ae92dfa 119 int startLoop = 0;
ivo_david_michelle 28:61f7356325c3 120
ivo_david_michelle 30:4820042e67b5 121 while (!startLoop) { // wait until Joystick is in starting position
ivo_david_michelle 34:eaea0ae92dfa 122 startLoop = getLowThrust(-0.3);
ivo_david_michelle 27:11116aa69f32 123 }
ivo_david_michelle 30:4820042e67b5 124 nLowThrust = 0; // reset for later use in controller.
ivo_david_michelle 27:11116aa69f32 125
ivo_david_michelle 22:92401a4fec13 126 motor_1 = 0.1;
ivo_david_michelle 22:92401a4fec13 127 motor_2 = 0.1;
ivo_david_michelle 22:92401a4fec13 128 motor_3 = 0.1;
ivo_david_michelle 22:92401a4fec13 129 motor_4 = 0.1;
ivo_david_michelle 4:3040d0f9e8c6 130
ivo_david_michelle 30:4820042e67b5 131 wait(3); // hold startup duty cycle for 2 seconds
ivo_david_michelle 22:92401a4fec13 132
ivo_david_michelle 29:ae765492fa8b 133 // TODO assign priorities to threads, test if it really works as we expect
ivo_david_michelle 29:ae765492fa8b 134 Thread battery(battery_thread);
ivo_david_michelle 23:04338a5ef404 135 Thread thread(controller_thread);
ivo_david_michelle 14:64b06476d943 136
ivo_david_michelle 29:ae765492fa8b 137 // TODO is this needed?
ivo_david_michelle 29:ae765492fa8b 138 while (1);
ivo_david_michelle 0:4c04e4fd1310 139 }