High Altitude Recovery Payload

HARP: High Altitude Recovery Payload

Version 0.1: RC design

/media/uploads/tylerjw/_scaled_2012-07-23_mbed_xbee_breadboard.jpg

By connecting the second xbee to a computer using a terminal command and supplying the characters L, R, C, F the light patterns change on the mbed.

Committer:
tylerjw
Date:
Mon Oct 15 22:07:07 2012 +0000
Revision:
7:fcbf263a62b9
Parent:
6:2bd373ba18ae
Child:
8:513b31554e5f
Simpler working left and right servo and battery voltage monitoring with warning light.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tylerjw 1:21a6da67311c 1 /* Copyright (c) 2012 Tyler Weaver, MIT License
tylerjw 1:21a6da67311c 2 *
tylerjw 6:2bd373ba18ae 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
tylerjw 6:2bd373ba18ae 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
tylerjw 6:2bd373ba18ae 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
tylerjw 6:2bd373ba18ae 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
tylerjw 1:21a6da67311c 7 * furnished to do so, subject to the following conditions:
tylerjw 1:21a6da67311c 8 *
tylerjw 6:2bd373ba18ae 9 * The above copyright notice and this permission notice shall be included in all copies or
tylerjw 1:21a6da67311c 10 * substantial portions of the Software.
tylerjw 1:21a6da67311c 11 *
tylerjw 6:2bd373ba18ae 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
tylerjw 6:2bd373ba18ae 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
tylerjw 6:2bd373ba18ae 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
tylerjw 6:2bd373ba18ae 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
tylerjw 1:21a6da67311c 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
tylerjw 1:21a6da67311c 17 */
tylerjw 6:2bd373ba18ae 18
tylerjw 0:043ed5bb10f2 19 #include "mbed.h"
tylerjw 0:043ed5bb10f2 20 #include "rtos.h"
tylerjw 0:043ed5bb10f2 21 #include "watchdog.h"
tylerjw 7:fcbf263a62b9 22 #include "Servo.h"
tylerjw 7:fcbf263a62b9 23
tylerjw 7:fcbf263a62b9 24 #define CENTER 'C'
tylerjw 7:fcbf263a62b9 25 #define LEFT 'L'
tylerjw 7:fcbf263a62b9 26 #define RIGHT 'R'
tylerjw 0:043ed5bb10f2 27
tylerjw 0:043ed5bb10f2 28 // Setup the watchdog timer
tylerjw 0:043ed5bb10f2 29 Watchdog wdt;
tylerjw 0:043ed5bb10f2 30
tylerjw 0:043ed5bb10f2 31 // status leds
tylerjw 0:043ed5bb10f2 32 BusOut status_led(LED4, LED3, LED2, LED1);
tylerjw 0:043ed5bb10f2 33
tylerjw 0:043ed5bb10f2 34 typedef struct {
tylerjw 0:043ed5bb10f2 35 char msg; // the direction to turn in
tylerjw 7:fcbf263a62b9 36 } messageXbeeT;
tylerjw 6:2bd373ba18ae 37
tylerjw 7:fcbf263a62b9 38 MemoryPool<messageXbeeT, 16> mpool_xbee;
tylerjw 7:fcbf263a62b9 39 Queue<messageXbeeT, 16> queue_xbee;
tylerjw 6:2bd373ba18ae 40
tylerjw 6:2bd373ba18ae 41 /**
tylerjw 6:2bd373ba18ae 42 * xbee_thread
tylerjw 6:2bd373ba18ae 43 * this thread reads characters from the xbee serial connection and posts messages containing them
tylerjw 6:2bd373ba18ae 44 */
tylerjw 7:fcbf263a62b9 45 void xbee_thread(void const *argument)
tylerjw 7:fcbf263a62b9 46 {
tylerjw 7:fcbf263a62b9 47 // xbee serial connection
tylerjw 7:fcbf263a62b9 48 Serial xbee(p9,p10);
tylerjw 7:fcbf263a62b9 49 xbee.baud(9600);
tylerjw 0:043ed5bb10f2 50 while (true) {
tylerjw 0:043ed5bb10f2 51 if (xbee.readable()) {
tylerjw 7:fcbf263a62b9 52 messageXbeeT *message = mpool_xbee.alloc();
tylerjw 0:043ed5bb10f2 53 message->msg = xbee.getc();
tylerjw 0:043ed5bb10f2 54
tylerjw 6:2bd373ba18ae 55 queue_xbee.put(message);
tylerjw 0:043ed5bb10f2 56 }
tylerjw 0:043ed5bb10f2 57 Thread::wait(100);
tylerjw 0:043ed5bb10f2 58 }
tylerjw 0:043ed5bb10f2 59 }
tylerjw 0:043ed5bb10f2 60
tylerjw 6:2bd373ba18ae 61 /**
tylerjw 6:2bd373ba18ae 62 parachute_thread
tylerjw 6:2bd373ba18ae 63 this thread recieves messages from the main thread and turns the servos for control of the parachute
tylerjw 6:2bd373ba18ae 64 */
tylerjw 7:fcbf263a62b9 65 void parachute_thread(void const *argument)
tylerjw 7:fcbf263a62b9 66 {
tylerjw 7:fcbf263a62b9 67 Serial pc(USBTX, USBRX);
tylerjw 7:fcbf263a62b9 68 pc.baud(9600);
tylerjw 7:fcbf263a62b9 69
tylerjw 7:fcbf263a62b9 70 pc.puts("\n\rCalibrating Servo Motors... ");
tylerjw 7:fcbf263a62b9 71 // servos
tylerjw 7:fcbf263a62b9 72 Servo left_s(p21);
tylerjw 7:fcbf263a62b9 73 Servo right_s(p22);
tylerjw 6:2bd373ba18ae 74
tylerjw 7:fcbf263a62b9 75 left_s.calibrate_max(0.0014);
tylerjw 7:fcbf263a62b9 76 left_s.calibrate_min(-0.0008);
tylerjw 7:fcbf263a62b9 77 right_s.calibrate_max(0.0016);
tylerjw 7:fcbf263a62b9 78 right_s.calibrate_min(-0.0008);
tylerjw 6:2bd373ba18ae 79
tylerjw 7:fcbf263a62b9 80 for(float i = 0.0; i <= 1.0; i+=0.1) {
tylerjw 7:fcbf263a62b9 81 left_s = i;
tylerjw 7:fcbf263a62b9 82 right_s = i;
tylerjw 7:fcbf263a62b9 83 //release = i;
tylerjw 7:fcbf263a62b9 84 Thread::wait(500);
tylerjw 7:fcbf263a62b9 85 }
tylerjw 7:fcbf263a62b9 86 right_s = 0;
tylerjw 7:fcbf263a62b9 87 left_s = 0;
tylerjw 7:fcbf263a62b9 88 pc.puts("OK!");
tylerjw 6:2bd373ba18ae 89
tylerjw 7:fcbf263a62b9 90 char state = CENTER;
tylerjw 7:fcbf263a62b9 91 pc.puts("\n\rEntering Main Loop... ");
tylerjw 6:2bd373ba18ae 92 while (true) {
tylerjw 7:fcbf263a62b9 93 osEvent evt_xbee = queue_xbee.get(1); // 20 millisecond wait
tylerjw 7:fcbf263a62b9 94 if (evt_xbee.status == osEventMessage) {
tylerjw 7:fcbf263a62b9 95 messageXbeeT *message = (messageXbeeT*)evt_xbee.value.p;
tylerjw 7:fcbf263a62b9 96 state = message->msg;
tylerjw 7:fcbf263a62b9 97 pc.puts("\n\rMessage recieved!");
tylerjw 6:2bd373ba18ae 98 }
tylerjw 6:2bd373ba18ae 99 switch (state) {
tylerjw 7:fcbf263a62b9 100 case CENTER: // center
tylerjw 7:fcbf263a62b9 101 status_led = 0x6;
tylerjw 7:fcbf263a62b9 102 right_s = 0;
tylerjw 7:fcbf263a62b9 103 left_s = 0;
tylerjw 6:2bd373ba18ae 104 break;
tylerjw 6:2bd373ba18ae 105 case LEFT: // left
tylerjw 7:fcbf263a62b9 106 status_led = 0x3;
tylerjw 7:fcbf263a62b9 107 right_s = 0;
tylerjw 7:fcbf263a62b9 108 left_s = 1;
tylerjw 6:2bd373ba18ae 109 break;
tylerjw 6:2bd373ba18ae 110 case RIGHT: // right
tylerjw 7:fcbf263a62b9 111 status_led = 0xC;
tylerjw 7:fcbf263a62b9 112 right_s = 1;
tylerjw 7:fcbf263a62b9 113 left_s = 0;
tylerjw 6:2bd373ba18ae 114 break;
tylerjw 6:2bd373ba18ae 115 }
tylerjw 6:2bd373ba18ae 116 }
tylerjw 6:2bd373ba18ae 117
tylerjw 6:2bd373ba18ae 118 }
tylerjw 6:2bd373ba18ae 119
tylerjw 6:2bd373ba18ae 120 /**
tylerjw 6:2bd373ba18ae 121 main thread
tylerjw 6:2bd373ba18ae 122 this thread initializes everything then recieves messages from the xbee and sends messages to the parachute
tylerjw 6:2bd373ba18ae 123 */
tylerjw 7:fcbf263a62b9 124 int main (void)
tylerjw 7:fcbf263a62b9 125 {
tylerjw 7:fcbf263a62b9 126 AnalogIn battery(p19);
tylerjw 7:fcbf263a62b9 127 DigitalOut battery_warning(p24);
tylerjw 7:fcbf263a62b9 128 battery_warning = 1;
tylerjw 7:fcbf263a62b9 129
tylerjw 7:fcbf263a62b9 130 const float BAT_MUL = 10.26;
tylerjw 7:fcbf263a62b9 131 float battery_voltage;
tylerjw 7:fcbf263a62b9 132
tylerjw 0:043ed5bb10f2 133 status_led = 0x9;
tylerjw 0:043ed5bb10f2 134 // setup watchdog
tylerjw 0:043ed5bb10f2 135 wdt.kick(2.0); // 2 second watchdog
tylerjw 0:043ed5bb10f2 136 // setup xbee serial
tylerjw 0:043ed5bb10f2 137
tylerjw 0:043ed5bb10f2 138 Thread thread1(xbee_thread);
tylerjw 6:2bd373ba18ae 139 Thread thread2(parachute_thread);
tylerjw 0:043ed5bb10f2 140
tylerjw 0:043ed5bb10f2 141 while (true) {
tylerjw 7:fcbf263a62b9 142 battery_voltage = battery.read() * BAT_MUL;
tylerjw 7:fcbf263a62b9 143 if(battery_voltage < 6.4)
tylerjw 7:fcbf263a62b9 144 battery_warning = 0;
tylerjw 7:fcbf263a62b9 145
tylerjw 7:fcbf263a62b9 146 Thread::wait(1000);
tylerjw 0:043ed5bb10f2 147 wdt.kick();
tylerjw 0:043ed5bb10f2 148 }
tylerjw 0:043ed5bb10f2 149 }