Generation 3 of the Harp project

Dependencies:   Servo TMP36 GZ buffered-serial1 chan_fatfs_sd nmea_parser watchdog mbed-rtos mbed

Fork of HARP2 by Tyler Weaver

Committer:
tylerjw
Date:
Thu Jan 10 19:03:34 2013 +0000
Revision:
26:85cdb1031eb1
Parent:
25:81c3696ba2c9
Child:
27:24fd8e32511c
Tested file append

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tylerjw 24:7477105103e5 1 /**
tylerjw 24:7477105103e5 2 * HARP Version 2
tylerjw 26:85cdb1031eb1 3 *
tylerjw 26:85cdb1031eb1 4 * TODO: Test Servo Code
tylerjw 26:85cdb1031eb1 5 * Test Watchdog Timer
tylerjw 26:85cdb1031eb1 6 * Test Append file and f_size() macro
tylerjw 24:7477105103e5 7 */
tylerjw 24:7477105103e5 8
tylerjw 0:ce5f06c3895f 9 #include "mbed.h"
tylerjw 7:d8ecabe16c9e 10 #include "rtos.h"
tylerjw 10:b13416bbb4cd 11 #include "buffered_serial.h"
tylerjw 12:0d943d69d3ec 12 #include "ff.h"
tylerjw 26:85cdb1031eb1 13 #include "TMP36GZ.h"
tylerjw 24:7477105103e5 14 #include "nmea_parser.h"
tylerjw 25:81c3696ba2c9 15 #include "watchdog.h"
tylerjw 25:81c3696ba2c9 16 #include "Servo.h"
tylerjw 22:becb67846755 17 #include "config.h"
tylerjw 19:1cfe22ef30e2 18
tylerjw 0:ce5f06c3895f 19 Serial pc(USBTX, USBRX);
tylerjw 24:7477105103e5 20 BufferedSerial gps;
tylerjw 10:b13416bbb4cd 21 AnalogIn gps_battery(p20);
tylerjw 26:85cdb1031eb1 22 AnalogIn mbed_battery(p16);
tylerjw 26:85cdb1031eb1 23 TMP36GZ temperature(p17);
tylerjw 7:d8ecabe16c9e 24
tylerjw 24:7477105103e5 25 NmeaParser nmea;
tylerjw 17:4927053e120f 26
tylerjw 18:29b19a25a963 27 Semaphore parachute_sem(0);
tylerjw 26:85cdb1031eb1 28 Semaphore sd_sem(0);
tylerjw 18:29b19a25a963 29
tylerjw 13:db6af0620264 30 typedef struct {
tylerjw 13:db6af0620264 31 char line[80];
tylerjw 13:db6af0620264 32 } gps_line;
tylerjw 13:db6af0620264 33 MemoryPool<gps_line, 16> mpool_gps_line;
tylerjw 13:db6af0620264 34 Queue<gps_line, 16> queue_gps_line;
tylerjw 13:db6af0620264 35
tylerjw 13:db6af0620264 36 typedef struct {
tylerjw 14:dce4d8c29b17 37 char line[80];
tylerjw 13:db6af0620264 38 } sensor_line;
tylerjw 13:db6af0620264 39 MemoryPool<sensor_line, 16> mpool_sensor_line;
tylerjw 13:db6af0620264 40 Queue<sensor_line, 16> queue_sensor_line;
tylerjw 12:0d943d69d3ec 41
tylerjw 7:d8ecabe16c9e 42 void gps_thread(void const *args)
tylerjw 7:d8ecabe16c9e 43 {
tylerjw 10:b13416bbb4cd 44 char buffer[80];
tylerjw 18:29b19a25a963 45 float alt, alt_prev;
tylerjw 18:29b19a25a963 46 alt = alt_prev = 0;
tylerjw 9:4debfbc1fb3e 47
tylerjw 21:8799ee63c2cd 48 //DigitalOut gps_led(LED4);
tylerjw 7:d8ecabe16c9e 49
tylerjw 7:d8ecabe16c9e 50 gps.baud(4800);
tylerjw 7:d8ecabe16c9e 51
tylerjw 7:d8ecabe16c9e 52 while(true) {
tylerjw 21:8799ee63c2cd 53 //gps_led = !gps_led;
tylerjw 24:7477105103e5 54 gps.get_line(buffer);
tylerjw 19:1cfe22ef30e2 55 int line_type = nmea.parse(buffer);
tylerjw 14:dce4d8c29b17 56 //pc.puts(buffer);
tylerjw 13:db6af0620264 57 // send to log...
tylerjw 13:db6af0620264 58 gps_line *message = mpool_gps_line.alloc();
tylerjw 13:db6af0620264 59 strcpy(message->line, buffer);
tylerjw 13:db6af0620264 60 queue_gps_line.put(message);
tylerjw 19:1cfe22ef30e2 61
tylerjw 19:1cfe22ef30e2 62 // test altitude direction - release parachute thread to run
tylerjw 24:7477105103e5 63 if(line_type == RMC && nmea.quality()) {
tylerjw 19:1cfe22ef30e2 64 if(UNLOCK_ON_FALL) {
tylerjw 19:1cfe22ef30e2 65 if(alt != 0) { // first time
tylerjw 24:7477105103e5 66 alt = nmea.msl_altitude();
tylerjw 19:1cfe22ef30e2 67 } else {
tylerjw 19:1cfe22ef30e2 68 alt_prev = alt;
tylerjw 24:7477105103e5 69 alt = nmea.msl_altitude();
tylerjw 19:1cfe22ef30e2 70 if(alt < alt_prev) // going down
tylerjw 19:1cfe22ef30e2 71 parachute_sem.release(); // let the parachute code execute
tylerjw 19:1cfe22ef30e2 72 }
tylerjw 18:29b19a25a963 73 } else {
tylerjw 19:1cfe22ef30e2 74 parachute_sem.release();
tylerjw 18:29b19a25a963 75 }
tylerjw 18:29b19a25a963 76 }
tylerjw 13:db6af0620264 77 }
tylerjw 13:db6af0620264 78 }
tylerjw 13:db6af0620264 79
tylerjw 13:db6af0620264 80 void log_thread(const void *args)
tylerjw 13:db6af0620264 81 {
tylerjw 13:db6af0620264 82 FATFS fs;
tylerjw 13:db6af0620264 83 FIL fp_gps, fp_sensor;
tylerjw 13:db6af0620264 84
tylerjw 13:db6af0620264 85 DigitalOut log_led(LED3);
tylerjw 13:db6af0620264 86
tylerjw 13:db6af0620264 87 f_mount(0, &fs);
tylerjw 25:81c3696ba2c9 88 f_open(&fp_gps, "0:gps.txt", FA_OPEN_EXISTING | FA_WRITE);
tylerjw 26:85cdb1031eb1 89 f_lseek(&fp_gps, f_size(&fp_gps));
tylerjw 25:81c3696ba2c9 90 f_open(&fp_sensor, "0:sensors.csv", FA_OPEN_EXISTING | FA_WRITE);
tylerjw 26:85cdb1031eb1 91 f_lseek(&fp_sensor, f_size(&fp_sensor));
tylerjw 13:db6af0620264 92
tylerjw 26:85cdb1031eb1 93 sd_sem.release(); // sd card initialized... start sensor thread
tylerjw 26:85cdb1031eb1 94
tylerjw 13:db6af0620264 95 while(1) {
tylerjw 18:29b19a25a963 96 log_led = !log_led;
tylerjw 14:dce4d8c29b17 97 osEvent evt1 = queue_gps_line.get(1);
tylerjw 14:dce4d8c29b17 98 if (evt1.status == osEventMessage) {
tylerjw 14:dce4d8c29b17 99 gps_line *message = (gps_line*)evt1.value.p;
tylerjw 13:db6af0620264 100
tylerjw 13:db6af0620264 101 f_puts(message->line, &fp_gps);
tylerjw 13:db6af0620264 102 f_sync(&fp_gps);
tylerjw 19:1cfe22ef30e2 103
tylerjw 13:db6af0620264 104 mpool_gps_line.free(message);
tylerjw 13:db6af0620264 105 }
tylerjw 19:1cfe22ef30e2 106
tylerjw 14:dce4d8c29b17 107 osEvent evt2 = queue_sensor_line.get(1);
tylerjw 14:dce4d8c29b17 108 if(evt2.status == osEventMessage) {
tylerjw 14:dce4d8c29b17 109 sensor_line *message = (sensor_line*)evt2.value.p;
tylerjw 19:1cfe22ef30e2 110
tylerjw 14:dce4d8c29b17 111 f_puts(message->line, &fp_sensor);
tylerjw 14:dce4d8c29b17 112 f_sync(&fp_sensor);
tylerjw 19:1cfe22ef30e2 113
tylerjw 13:db6af0620264 114 mpool_sensor_line.free(message);
tylerjw 13:db6af0620264 115 }
tylerjw 13:db6af0620264 116 }
tylerjw 13:db6af0620264 117 }
tylerjw 13:db6af0620264 118
tylerjw 13:db6af0620264 119 void sensor_thread(const void* args)
tylerjw 13:db6af0620264 120 {
tylerjw 14:dce4d8c29b17 121 DigitalOut sensor_led(LED2);
tylerjw 16:653df0cfe6ee 122 Timer t;
tylerjw 18:29b19a25a963 123 float time;
tylerjw 18:29b19a25a963 124 float gps_battery_voltage, mbed_battery_voltage;
tylerjw 26:85cdb1031eb1 125 float temp;
tylerjw 26:85cdb1031eb1 126
tylerjw 26:85cdb1031eb1 127 sd_sem.wait(); // wait for the sd card to initialize and open files
tylerjw 19:1cfe22ef30e2 128
tylerjw 19:1cfe22ef30e2 129 if(WAIT_FOR_LOCK) {
tylerjw 24:7477105103e5 130 while(!nmea.date()) Thread::wait(100); // wait for lock
tylerjw 19:1cfe22ef30e2 131 }
tylerjw 19:1cfe22ef30e2 132
tylerjw 17:4927053e120f 133 t.start(); // start timer after lock
tylerjw 19:1cfe22ef30e2 134
tylerjw 14:dce4d8c29b17 135 sensor_line *message = mpool_sensor_line.alloc();
tylerjw 26:85cdb1031eb1 136 sprintf(message->line, "Date: %d, Time: %f\r\nGPS Time (UTC),GPS Battery(V),mbed Battery(V)", nmea.date(), nmea.utc_time());
tylerjw 26:85cdb1031eb1 137 queue_sensor_line.put(message);
tylerjw 26:85cdb1031eb1 138
tylerjw 26:85cdb1031eb1 139 message = mpool_sensor_line.alloc();
tylerjw 26:85cdb1031eb1 140 sprintf(message->line, ",Temperature,GPS Altitude,GPS Course \r\n");
tylerjw 14:dce4d8c29b17 141 queue_sensor_line.put(message);
tylerjw 19:1cfe22ef30e2 142
tylerjw 19:1cfe22ef30e2 143 while(true) {
tylerjw 13:db6af0620264 144 //get sensor line memory
tylerjw 18:29b19a25a963 145 sensor_led = !sensor_led;
tylerjw 13:db6af0620264 146 sensor_line *message = mpool_sensor_line.alloc();
tylerjw 19:1cfe22ef30e2 147
tylerjw 16:653df0cfe6ee 148 //timestamp
tylerjw 24:7477105103e5 149 time = nmea.utc_time();
tylerjw 19:1cfe22ef30e2 150
tylerjw 13:db6af0620264 151 //gps battery
tylerjw 18:29b19a25a963 152 gps_battery_voltage = gps_battery.read()*BAT_GPS_MUL;
tylerjw 19:1cfe22ef30e2 153
tylerjw 16:653df0cfe6ee 154 //mbed battery
tylerjw 18:29b19a25a963 155 mbed_battery_voltage = mbed_battery.read()*BAT_MBED_MUL;
tylerjw 19:1cfe22ef30e2 156
tylerjw 26:85cdb1031eb1 157 //temperature
tylerjw 26:85cdb1031eb1 158 temp = temperature.sample_f();
tylerjw 19:1cfe22ef30e2 159
tylerjw 26:85cdb1031eb1 160 sprintf(message->line, "%f,%f,%f,%f,%f,%f\r\n", time,gps_battery_voltage,mbed_battery_voltage,temp,nmea.calc_altitude_ft(),nmea.track());
tylerjw 14:dce4d8c29b17 161 queue_sensor_line.put(message);
tylerjw 26:85cdb1031eb1 162
tylerjw 26:85cdb1031eb1 163 Thread::wait(100);
tylerjw 0:ce5f06c3895f 164 }
tylerjw 7:d8ecabe16c9e 165 }
tylerjw 7:d8ecabe16c9e 166
tylerjw 17:4927053e120f 167 void parachute_thread(const void *args)
tylerjw 17:4927053e120f 168 {
tylerjw 21:8799ee63c2cd 169 DigitalOut left_turn(LED4);
tylerjw 21:8799ee63c2cd 170 DigitalOut right_turn(LED1);
tylerjw 25:81c3696ba2c9 171
tylerjw 25:81c3696ba2c9 172 // servos ////////////////////////// NOT TESTED!!! ///////////////////////////
tylerjw 25:81c3696ba2c9 173 Servo left_s(p21);
tylerjw 25:81c3696ba2c9 174 Servo right_s(p22);
tylerjw 25:81c3696ba2c9 175
tylerjw 25:81c3696ba2c9 176 left_s.calibrate_max(0.0007);
tylerjw 25:81c3696ba2c9 177 left_s.calibrate_min(-0.0014);
tylerjw 25:81c3696ba2c9 178 right_s.calibrate(0.0009);
tylerjw 25:81c3696ba2c9 179
tylerjw 25:81c3696ba2c9 180 left_s = 0.0;
tylerjw 25:81c3696ba2c9 181 right_s = 0.0;
tylerjw 25:81c3696ba2c9 182 ////////////////////////////////////////////////////////////////////////////////
tylerjw 19:1cfe22ef30e2 183
tylerjw 21:8799ee63c2cd 184 right_turn = 1;
tylerjw 21:8799ee63c2cd 185 Thread::wait(400);
tylerjw 21:8799ee63c2cd 186 left_turn = 1;
tylerjw 21:8799ee63c2cd 187 Thread::wait(400);
tylerjw 19:1cfe22ef30e2 188 while(true) {
tylerjw 21:8799ee63c2cd 189 right_turn = left_turn = 0;
tylerjw 18:29b19a25a963 190 parachute_sem.wait();
tylerjw 22:becb67846755 191
tylerjw 21:8799ee63c2cd 192 float distance = nmea.calc_dist_to_km(target_lat, target_lon);
tylerjw 19:1cfe22ef30e2 193
tylerjw 21:8799ee63c2cd 194 if(distance < distance_fudge_km)
tylerjw 19:1cfe22ef30e2 195 continue; // dont do anything
tylerjw 19:1cfe22ef30e2 196
tylerjw 24:7477105103e5 197 float course = nmea.track();
tylerjw 24:7477105103e5 198 float course_to = nmea.calc_initial_bearing(target_lat, target_lon);
tylerjw 19:1cfe22ef30e2 199 float course_diff = course_to - course;
tylerjw 22:becb67846755 200
tylerjw 22:becb67846755 201 if(course == 0.0) // not moving fast enough
tylerjw 22:becb67846755 202 continue; // do nothing
tylerjw 19:1cfe22ef30e2 203
tylerjw 21:8799ee63c2cd 204 if(course_diff < course_fudge && course_diff > neg_course_fudge) {
tylerjw 21:8799ee63c2cd 205 right_turn = left_turn = 1;
tylerjw 21:8799ee63c2cd 206 Thread::wait(400);
tylerjw 19:1cfe22ef30e2 207 continue; // don't do anything
tylerjw 21:8799ee63c2cd 208 } else if(course_diff > 180.0 || course_diff < 0.0) {
tylerjw 21:8799ee63c2cd 209 left_turn = 1;
tylerjw 25:81c3696ba2c9 210 right_s = 0.0; // NOT TESTED!!!
tylerjw 25:81c3696ba2c9 211 left_s = 1.0;
tylerjw 21:8799ee63c2cd 212 Thread::wait(400); // turn left
tylerjw 25:81c3696ba2c9 213 left_s = 0.0;
tylerjw 22:becb67846755 214 } else {
tylerjw 22:becb67846755 215 right_turn = 1;
tylerjw 25:81c3696ba2c9 216 left_s = 0.0;
tylerjw 25:81c3696ba2c9 217 right_s = 1.0;
tylerjw 22:becb67846755 218 Thread::wait(400); // turn righ
tylerjw 25:81c3696ba2c9 219 right_s = 0.0;
tylerjw 21:8799ee63c2cd 220 }
tylerjw 18:29b19a25a963 221 }
tylerjw 17:4927053e120f 222 }
tylerjw 17:4927053e120f 223
tylerjw 7:d8ecabe16c9e 224 int main()
tylerjw 7:d8ecabe16c9e 225 {
tylerjw 10:b13416bbb4cd 226 pc.baud(9600);
tylerjw 9:4debfbc1fb3e 227 Thread thread(gps_thread, NULL, osPriorityHigh);
tylerjw 13:db6af0620264 228 Thread thread2(log_thread, NULL, osPriorityNormal);
tylerjw 14:dce4d8c29b17 229 Thread thread3(sensor_thread, NULL, osPriorityNormal);
tylerjw 17:4927053e120f 230 Thread thread4(parachute_thread, NULL, osPriorityRealtime);
tylerjw 25:81c3696ba2c9 231
tylerjw 25:81c3696ba2c9 232 Watchdog wdt; // NOT TESTED!!!
tylerjw 25:81c3696ba2c9 233
tylerjw 25:81c3696ba2c9 234 wdt.kick(2.0);
tylerjw 13:db6af0620264 235
tylerjw 25:81c3696ba2c9 236 while(true) {
tylerjw 25:81c3696ba2c9 237 wdt.kick();
tylerjw 25:81c3696ba2c9 238 Thread::wait(500);
tylerjw 25:81c3696ba2c9 239 }
tylerjw 0:ce5f06c3895f 240 }