Tyler Weaver
/
HARP_simple
simple (non RTOS) version of the harp parogram
main.cpp@0:d4c44f8b93ea, 2012-09-20 (annotated)
- Committer:
- tylerjw
- Date:
- Thu Sep 20 23:51:21 2012 +0000
- Revision:
- 0:d4c44f8b93ea
0.1
;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tylerjw | 0:d4c44f8b93ea | 1 | /** |
tylerjw | 0:d4c44f8b93ea | 2 | * @author Tyler Weaver |
tylerjw | 0:d4c44f8b93ea | 3 | * |
tylerjw | 0:d4c44f8b93ea | 4 | * @section LICENSE |
tylerjw | 0:d4c44f8b93ea | 5 | * |
tylerjw | 0:d4c44f8b93ea | 6 | * Copyright (c) 2012 Tyler Weaver, MIT License |
tylerjw | 0:d4c44f8b93ea | 7 | * |
tylerjw | 0:d4c44f8b93ea | 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
tylerjw | 0:d4c44f8b93ea | 9 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
tylerjw | 0:d4c44f8b93ea | 10 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
tylerjw | 0:d4c44f8b93ea | 11 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
tylerjw | 0:d4c44f8b93ea | 12 | * furnished to do so, subject to the following conditions: |
tylerjw | 0:d4c44f8b93ea | 13 | * |
tylerjw | 0:d4c44f8b93ea | 14 | * The above copyright notice and this permission notice shall be included in all copies or |
tylerjw | 0:d4c44f8b93ea | 15 | * substantial portions of the Software. |
tylerjw | 0:d4c44f8b93ea | 16 | * |
tylerjw | 0:d4c44f8b93ea | 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
tylerjw | 0:d4c44f8b93ea | 18 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
tylerjw | 0:d4c44f8b93ea | 19 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
tylerjw | 0:d4c44f8b93ea | 20 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
tylerjw | 0:d4c44f8b93ea | 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
tylerjw | 0:d4c44f8b93ea | 22 | * |
tylerjw | 0:d4c44f8b93ea | 23 | * @section DESCRIPTION |
tylerjw | 0:d4c44f8b93ea | 24 | * |
tylerjw | 0:d4c44f8b93ea | 25 | * This is a simplified version of the Generation 1 HARP project. |
tylerjw | 0:d4c44f8b93ea | 26 | */ |
tylerjw | 0:d4c44f8b93ea | 27 | |
tylerjw | 0:d4c44f8b93ea | 28 | #include "mbed.h" |
tylerjw | 0:d4c44f8b93ea | 29 | #include "Servo.h" |
tylerjw | 0:d4c44f8b93ea | 30 | |
tylerjw | 0:d4c44f8b93ea | 31 | #define CENTER 100 |
tylerjw | 0:d4c44f8b93ea | 32 | #define LEFT 101 |
tylerjw | 0:d4c44f8b93ea | 33 | #define RIGHT 102 |
tylerjw | 0:d4c44f8b93ea | 34 | |
tylerjw | 0:d4c44f8b93ea | 35 | // servos |
tylerjw | 0:d4c44f8b93ea | 36 | Servo left_s(p21); |
tylerjw | 0:d4c44f8b93ea | 37 | Servo right_s(p22); |
tylerjw | 0:d4c44f8b93ea | 38 | Servo release(p23); |
tylerjw | 0:d4c44f8b93ea | 39 | |
tylerjw | 0:d4c44f8b93ea | 40 | // pc serial |
tylerjw | 0:d4c44f8b93ea | 41 | Serial pc(USBTX, USBRX); |
tylerjw | 0:d4c44f8b93ea | 42 | |
tylerjw | 0:d4c44f8b93ea | 43 | // xbee serial connection |
tylerjw | 0:d4c44f8b93ea | 44 | Serial xbee(p9,p10); |
tylerjw | 0:d4c44f8b93ea | 45 | |
tylerjw | 0:d4c44f8b93ea | 46 | // status leds |
tylerjw | 0:d4c44f8b93ea | 47 | BusOut status_led(LED4, LED3, LED2, LED1); |
tylerjw | 0:d4c44f8b93ea | 48 | |
tylerjw | 0:d4c44f8b93ea | 49 | void init(); // initalize all components |
tylerjw | 0:d4c44f8b93ea | 50 | void parachute(int); |
tylerjw | 0:d4c44f8b93ea | 51 | |
tylerjw | 0:d4c44f8b93ea | 52 | int main() |
tylerjw | 0:d4c44f8b93ea | 53 | { |
tylerjw | 0:d4c44f8b93ea | 54 | init(); |
tylerjw | 0:d4c44f8b93ea | 55 | |
tylerjw | 0:d4c44f8b93ea | 56 | char xbee_input_character; |
tylerjw | 0:d4c44f8b93ea | 57 | int control_state = CENTER; |
tylerjw | 0:d4c44f8b93ea | 58 | bool is_released = false; |
tylerjw | 0:d4c44f8b93ea | 59 | |
tylerjw | 0:d4c44f8b93ea | 60 | while(1) { |
tylerjw | 0:d4c44f8b93ea | 61 | if(xbee.readable()) { |
tylerjw | 0:d4c44f8b93ea | 62 | xbee_input_character = xbee.getc(); |
tylerjw | 0:d4c44f8b93ea | 63 | |
tylerjw | 0:d4c44f8b93ea | 64 | // switch input |
tylerjw | 0:d4c44f8b93ea | 65 | switch(xbee_input_character) { |
tylerjw | 0:d4c44f8b93ea | 66 | case 'L': // turn left |
tylerjw | 0:d4c44f8b93ea | 67 | status_led = 0x3; |
tylerjw | 0:d4c44f8b93ea | 68 | control_state = LEFT; |
tylerjw | 0:d4c44f8b93ea | 69 | break; |
tylerjw | 0:d4c44f8b93ea | 70 | case 'C': // center |
tylerjw | 0:d4c44f8b93ea | 71 | status_led = 0x6; |
tylerjw | 0:d4c44f8b93ea | 72 | control_state = CENTER; |
tylerjw | 0:d4c44f8b93ea | 73 | break; |
tylerjw | 0:d4c44f8b93ea | 74 | case 'R': // turn right |
tylerjw | 0:d4c44f8b93ea | 75 | status_led = 0xC; |
tylerjw | 0:d4c44f8b93ea | 76 | control_state = RIGHT; |
tylerjw | 0:d4c44f8b93ea | 77 | break; |
tylerjw | 0:d4c44f8b93ea | 78 | case 'X': // release |
tylerjw | 0:d4c44f8b93ea | 79 | status_led = 0xF; |
tylerjw | 0:d4c44f8b93ea | 80 | release = 1; |
tylerjw | 0:d4c44f8b93ea | 81 | control_state = CENTER; // just in case something stupid happened |
tylerjw | 0:d4c44f8b93ea | 82 | is_released = true; |
tylerjw | 0:d4c44f8b93ea | 83 | break; |
tylerjw | 0:d4c44f8b93ea | 84 | } |
tylerjw | 0:d4c44f8b93ea | 85 | } |
tylerjw | 0:d4c44f8b93ea | 86 | if(is_released) // dont move the parachute controlls unless released |
tylerjw | 0:d4c44f8b93ea | 87 | parachute(control_state); // run parachute controls |
tylerjw | 0:d4c44f8b93ea | 88 | } |
tylerjw | 0:d4c44f8b93ea | 89 | } |
tylerjw | 0:d4c44f8b93ea | 90 | |
tylerjw | 0:d4c44f8b93ea | 91 | /**************************************/ |
tylerjw | 0:d4c44f8b93ea | 92 | void init() |
tylerjw | 0:d4c44f8b93ea | 93 | { |
tylerjw | 0:d4c44f8b93ea | 94 | status_led = 0; |
tylerjw | 0:d4c44f8b93ea | 95 | // pc serial baud |
tylerjw | 0:d4c44f8b93ea | 96 | pc.baud(9600); |
tylerjw | 0:d4c44f8b93ea | 97 | status_led = 0x1; |
tylerjw | 0:d4c44f8b93ea | 98 | |
tylerjw | 0:d4c44f8b93ea | 99 | // xbee serial baud |
tylerjw | 0:d4c44f8b93ea | 100 | xbee.baud(9600); |
tylerjw | 0:d4c44f8b93ea | 101 | status_led = 0x2; |
tylerjw | 0:d4c44f8b93ea | 102 | |
tylerjw | 0:d4c44f8b93ea | 103 | // calibrate servos |
tylerjw | 0:d4c44f8b93ea | 104 | pc.puts("Calibrating servos...\t"); |
tylerjw | 0:d4c44f8b93ea | 105 | left_s.calibrate_max(0.0014); |
tylerjw | 0:d4c44f8b93ea | 106 | left_s.calibrate_min(-0.0008); |
tylerjw | 0:d4c44f8b93ea | 107 | right_s.calibrate_max(0.0016); |
tylerjw | 0:d4c44f8b93ea | 108 | right_s.calibrate_min(-0.0008); |
tylerjw | 0:d4c44f8b93ea | 109 | release.calibrate(0.0004); |
tylerjw | 0:d4c44f8b93ea | 110 | pc.puts("OK!\n"); |
tylerjw | 0:d4c44f8b93ea | 111 | status_led = 0x3; |
tylerjw | 0:d4c44f8b93ea | 112 | |
tylerjw | 0:d4c44f8b93ea | 113 | // test servos |
tylerjw | 0:d4c44f8b93ea | 114 | pc.puts("Testing servos...\t"); |
tylerjw | 0:d4c44f8b93ea | 115 | for(float i = 0.0; i <= 1.0; i+=0.1) { |
tylerjw | 0:d4c44f8b93ea | 116 | left_s = i; |
tylerjw | 0:d4c44f8b93ea | 117 | right_s = i; |
tylerjw | 0:d4c44f8b93ea | 118 | release = i; |
tylerjw | 0:d4c44f8b93ea | 119 | wait(0.5); |
tylerjw | 0:d4c44f8b93ea | 120 | } |
tylerjw | 0:d4c44f8b93ea | 121 | right_s = release = 0; |
tylerjw | 0:d4c44f8b93ea | 122 | left_s = 0; |
tylerjw | 0:d4c44f8b93ea | 123 | pc.puts("OK!\n"); |
tylerjw | 0:d4c44f8b93ea | 124 | status_led = 0x4; |
tylerjw | 0:d4c44f8b93ea | 125 | } |
tylerjw | 0:d4c44f8b93ea | 126 | |
tylerjw | 0:d4c44f8b93ea | 127 | /**************************************/ |
tylerjw | 0:d4c44f8b93ea | 128 | void parachute(int state) |
tylerjw | 0:d4c44f8b93ea | 129 | { |
tylerjw | 0:d4c44f8b93ea | 130 | switch(state) { |
tylerjw | 0:d4c44f8b93ea | 131 | case CENTER: |
tylerjw | 0:d4c44f8b93ea | 132 | right_s = 0; |
tylerjw | 0:d4c44f8b93ea | 133 | left_s = 0; |
tylerjw | 0:d4c44f8b93ea | 134 | break; |
tylerjw | 0:d4c44f8b93ea | 135 | case RIGHT: |
tylerjw | 0:d4c44f8b93ea | 136 | right_s = 1; |
tylerjw | 0:d4c44f8b93ea | 137 | left_s = 0; |
tylerjw | 0:d4c44f8b93ea | 138 | break; |
tylerjw | 0:d4c44f8b93ea | 139 | case LEFT: |
tylerjw | 0:d4c44f8b93ea | 140 | right_s = 0; |
tylerjw | 0:d4c44f8b93ea | 141 | left_s = 1; |
tylerjw | 0:d4c44f8b93ea | 142 | break; |
tylerjw | 0:d4c44f8b93ea | 143 | } |
tylerjw | 0:d4c44f8b93ea | 144 | } |