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.

main.cpp

Committer:
tylerjw
Date:
2012-10-15
Revision:
7:fcbf263a62b9
Parent:
6:2bd373ba18ae
Child:
8:513b31554e5f

File content as of revision 7:fcbf263a62b9:

/* Copyright (c) 2012 Tyler Weaver, MIT License
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
 * and associated documentation files (the "Software"), to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or
 * substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include "mbed.h"
#include "rtos.h"
#include "watchdog.h"
#include "Servo.h"

#define CENTER  'C'
#define LEFT    'L'
#define RIGHT   'R'

// Setup the watchdog timer
Watchdog wdt;

// status leds
BusOut status_led(LED4, LED3, LED2, LED1);

typedef struct {
    char msg;   // the direction to turn in
} messageXbeeT;

MemoryPool<messageXbeeT, 16> mpool_xbee;
Queue<messageXbeeT, 16> queue_xbee;

/**
* xbee_thread
* this thread reads characters from the xbee serial connection and posts messages containing them
*/
void xbee_thread(void const *argument)
{
    // xbee serial connection
    Serial xbee(p9,p10);
    xbee.baud(9600);
    while (true) {
        if (xbee.readable()) {
            messageXbeeT *message = mpool_xbee.alloc();
            message->msg = xbee.getc();

            queue_xbee.put(message);
        }
        Thread::wait(100);
    }
}

/**
parachute_thread
this thread recieves messages from the main thread and turns the servos for control of the parachute
*/
void parachute_thread(void const *argument)
{
    Serial pc(USBTX, USBRX);
    pc.baud(9600);
    
    pc.puts("\n\rCalibrating Servo Motors... ");
    // servos
    Servo left_s(p21);
    Servo right_s(p22);

    left_s.calibrate_max(0.0014);
    left_s.calibrate_min(-0.0008);
    right_s.calibrate_max(0.0016);
    right_s.calibrate_min(-0.0008);

    for(float i = 0.0; i <= 1.0; i+=0.1) {
        left_s = i;
        right_s = i;
        //release = i;
        Thread::wait(500);
    }
    right_s = 0;
    left_s = 0;
    pc.puts("OK!");

    char state = CENTER;
    pc.puts("\n\rEntering Main Loop... ");
    while (true) {
        osEvent evt_xbee = queue_xbee.get(1); // 20 millisecond wait
        if (evt_xbee.status == osEventMessage) {
            messageXbeeT *message = (messageXbeeT*)evt_xbee.value.p;
            state = message->msg;
            pc.puts("\n\rMessage recieved!");
        }
        switch (state) {
            case CENTER: // center
                status_led = 0x6;
                right_s = 0;
                left_s = 0;
                break;
            case LEFT: // left
                status_led = 0x3;
                right_s = 0;
                left_s = 1;
                break;
            case RIGHT: // right
                status_led = 0xC;
                right_s = 1;
                left_s = 0;
                break;
        }
    }

}

/**
main thread
this thread initializes everything then recieves messages from the xbee and sends messages to the parachute
*/
int main (void)
{
    AnalogIn battery(p19);
    DigitalOut battery_warning(p24);
    battery_warning = 1;
    
    const float BAT_MUL = 10.26;
    float battery_voltage;
    
    status_led = 0x9;
    // setup watchdog
    wdt.kick(2.0); // 2 second watchdog
    // setup xbee serial

    Thread thread1(xbee_thread);
    Thread thread2(parachute_thread);

    while (true) {
        battery_voltage = battery.read() * BAT_MUL;
        if(battery_voltage < 6.4)
            battery_warning = 0;
            
        Thread::wait(1000);
        wdt.kick();
    }
}