ESE350 project, Spring 2016, University of Pennsylvania

Dependencies:   Adafruit9-DOf Receiver mbed-rtos mbed

Committer:
ivo_david_michelle
Date:
Mon Apr 11 16:41:56 2016 +0000
Revision:
22:92401a4fec13
Parent:
21:336faf452989
Child:
23:04338a5ef404
battery thread

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 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 4:3040d0f9e8c6 20
ivo_david_michelle 15:90e07946186f 21 void controller_thread(void const *args)
ivo_david_michelle 15:90e07946186f 22 {
ivo_david_michelle 15:90e07946186f 23 while(true) {
ivo_david_michelle 19:39c144ca2a2f 24 //myQuadcopter.readSensorValues();
ivo_david_michelle 19:39c144ca2a2f 25
ivo_david_michelle 15:90e07946186f 26 myQuadcopter.controller();
ivo_david_michelle 18:a00d6b065c6b 27 motors motorsPwm=myQuadcopter.getPwm();
ivo_david_michelle 18:a00d6b065c6b 28
ivo_david_michelle 21:336faf452989 29 motor_1=motorsPwm.m1;
ivo_david_michelle 21:336faf452989 30 motor_2=motorsPwm.m2;
ivo_david_michelle 21:336faf452989 31 motor_3=motorsPwm.m3;
ivo_david_michelle 18:a00d6b065c6b 32 motor_4=motorsPwm.m4;
ivo_david_michelle 22:92401a4fec13 33
ivo_david_michelle 22:92401a4fec13 34 // pc.printf("m1: %f m2: %f m3: %f m4: %f \n\r", motorsPwm.m1, motorsPwm.m2, motorsPwm.m3, motorsPwm.m4);
ivo_david_michelle 18:a00d6b065c6b 35
ivo_david_michelle 15:90e07946186f 36 Thread::wait(10);
ivo_david_michelle 15:90e07946186f 37 }
ivo_david_michelle 14:64b06476d943 38 }
ivo_david_michelle 9:f1bd96708a21 39
ivo_david_michelle 15:90e07946186f 40 void rc_thread(void const *args)
ivo_david_michelle 15:90e07946186f 41 {
ivo_david_michelle 15:90e07946186f 42 while(true) {
ivo_david_michelle 15:90e07946186f 43 myQuadcopter.readRc();
ivo_david_michelle 18:a00d6b065c6b 44 Thread::wait(50); // wait for some number of miliseconds
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 22:92401a4fec13 51 float max_voltage = 14.8; // max voltage level of battery
ivo_david_michelle 22:92401a4fec13 52 float saturating_voltage = 18.38; // voltage at which ADC == 1
ivo_david_michelle 22:92401a4fec13 53 float max_adc = 0.80522; // when battery is at 14.8V
ivo_david_michelle 22:92401a4fec13 54 float threshold_adc = max_adc * threshold_voltage / max_voltage;
ivo_david_michelle 22:92401a4fec13 55 while(true) {
ivo_david_michelle 22:92401a4fec13 56 if (battery.read() < threshold_adc) {
ivo_david_michelle 22:92401a4fec13 57 printf("low battery! %f\r\n", battery.read() * saturating_voltage);
ivo_david_michelle 22:92401a4fec13 58 }
ivo_david_michelle 22:92401a4fec13 59 Thread::wait(1000); // wait for some number of miliseconds
ivo_david_michelle 22:92401a4fec13 60 }
ivo_david_michelle 22:92401a4fec13 61 }
ivo_david_michelle 22:92401a4fec13 62
ivo_david_michelle 22:92401a4fec13 63
ivo_david_michelle 14:64b06476d943 64
ivo_david_michelle 15:90e07946186f 65 int main()
ivo_david_michelle 15:90e07946186f 66 {
ivo_david_michelle 14:64b06476d943 67 //Thread thread(controller_thread);
ivo_david_michelle 14:64b06476d943 68
ivo_david_michelle 14:64b06476d943 69 // get desired values from joystick (to be implemented)
ivo_david_michelle 14:64b06476d943 70 // myQuadcopter.setDes(...)
ivo_david_michelle 10:e7d1801e966a 71
ivo_david_michelle 22:92401a4fec13 72 motor_1.period(0.01); // motor requires a 2ms period
ivo_david_michelle 22:92401a4fec13 73 motor_2.period(0.01); // motor requires a 2ms period
ivo_david_michelle 22:92401a4fec13 74 motor_3.period(0.01); // motor requires a 2ms period
ivo_david_michelle 22:92401a4fec13 75 motor_4.period(0.01); // motor requires a 2ms period
ivo_david_michelle 22:92401a4fec13 76
ivo_david_michelle 22:92401a4fec13 77 //pc.printf("Pestingt.\n\r");
ivo_david_michelle 22:92401a4fec13 78
ivo_david_michelle 22:92401a4fec13 79
ivo_david_michelle 22:92401a4fec13 80
ivo_david_michelle 4:3040d0f9e8c6 81
ivo_david_michelle 4:3040d0f9e8c6 82 // startup procedure
ivo_david_michelle 4:3040d0f9e8c6 83 pc.printf("Type 's' to start up Motors, or anything else to abort.\n\r");
ivo_david_michelle 4:3040d0f9e8c6 84 char a= pc.getc();
ivo_david_michelle 4:3040d0f9e8c6 85 if (a!='s') {
ivo_david_michelle 4:3040d0f9e8c6 86 pc.printf("Aborting");
ivo_david_michelle 4:3040d0f9e8c6 87 return 0;
ivo_david_michelle 4:3040d0f9e8c6 88 };
ivo_david_michelle 13:291ba30c7806 89
ivo_david_michelle 22:92401a4fec13 90
ivo_david_michelle 22:92401a4fec13 91
ivo_david_michelle 12:422963993df5 92 // Duty cycle at beginning must be 50%
ivo_david_michelle 4:3040d0f9e8c6 93 pc.printf("Starting up ESCs\n\r");
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 4:3040d0f9e8c6 99 pc.printf("Type 'c' to enter control loop, or anything else to abort.\n\r");
ivo_david_michelle 14:64b06476d943 100 char b = pc.getc();
ivo_david_michelle 4:3040d0f9e8c6 101 if (b!='c') {
ivo_david_michelle 4:3040d0f9e8c6 102 pc.printf("Aborting");
ivo_david_michelle 4:3040d0f9e8c6 103 return 0;
ivo_david_michelle 4:3040d0f9e8c6 104 };
ivo_david_michelle 15:90e07946186f 105
ivo_david_michelle 22:92401a4fec13 106
ivo_david_michelle 22:92401a4fec13 107
ivo_david_michelle 22:92401a4fec13 108 /* pc.printf("Outputting duty cycle specified below now press all but b to abort.\n\r");
ivo_david_michelle 22:92401a4fec13 109 motor_1 = 0.07;
ivo_david_michelle 22:92401a4fec13 110
ivo_david_michelle 22:92401a4fec13 111
ivo_david_michelle 22:92401a4fec13 112 char c = pc.getc();
ivo_david_michelle 22:92401a4fec13 113 if (c!='c') {
ivo_david_michelle 22:92401a4fec13 114 pc.printf("Aborting");
ivo_david_michelle 22:92401a4fec13 115 return 0;
ivo_david_michelle 22:92401a4fec13 116 };
ivo_david_michelle 22:92401a4fec13 117 */
ivo_david_michelle 22:92401a4fec13 118
ivo_david_michelle 22:92401a4fec13 119
ivo_david_michelle 14:64b06476d943 120 Thread threadR(rc_thread);
ivo_david_michelle 22:92401a4fec13 121 Thread battery(battery_thread);
ivo_david_michelle 14:64b06476d943 122
ivo_david_michelle 14:64b06476d943 123 //check if remote controll is working with if statement
ivo_david_michelle 4:3040d0f9e8c6 124
ivo_david_michelle 22:92401a4fec13 125 Thread threadC(controller_thread);
ivo_david_michelle 4:3040d0f9e8c6 126 pc.printf("Entering control loop\n\r");
ivo_david_michelle 4:3040d0f9e8c6 127
ivo_david_michelle 4:3040d0f9e8c6 128 while (1) {
ivo_david_michelle 19:39c144ca2a2f 129 //myQuadcopter.readSensorValues();
ivo_david_michelle 18:a00d6b065c6b 130 //pc.printf("%lld: %f,%f,%f,%f\r\n", myQuadcopter.id, myQuadcopter.thrust, myQuadcopter.yaw, myQuadcopter.pitch, myQuadcopter.roll);
ivo_david_michelle 11:5c54826d23a7 131
ivo_david_michelle 15:90e07946186f 132 // myQuadcopter.controller();
ivo_david_michelle 13:291ba30c7806 133
ivo_david_michelle 17:96d0c72e413e 134 // wait(0.01);
ivo_david_michelle 1:b87e95907a18 135
ivo_david_michelle 4:3040d0f9e8c6 136 // Set duty cycle
ivo_david_michelle 22:92401a4fec13 137 // motors motorsPwm=myQuadcopter.getPwm();
ivo_david_michelle 5:f007542f1dab 138
ivo_david_michelle 18:a00d6b065c6b 139 //motor_2=motorsPwm.m2;
ivo_david_michelle 18:a00d6b065c6b 140 //motor_4=motorsPwm.m4;
ivo_david_michelle 15:90e07946186f 141
ivo_david_michelle 14:64b06476d943 142 //pc.printf("F: %f M_x: %f M_y: %f M_z: %f\n\r", F, M_x, M_y, M_z);
ivo_david_michelle 15:90e07946186f 143 // pc.printf("m1: %f m2: %f \n\r", motorsPwm.m2, motorsPwm.m4);
ivo_david_michelle 11:5c54826d23a7 144 //pc.printf("M_x: %f\tM_y: %f\tM_z: %f\tF: %f\n\r", result.M_x, result.M_y, result.M_z, result.F);
ivo_david_michelle 0:4c04e4fd1310 145 }
ivo_david_michelle 0:4c04e4fd1310 146 }