ft. button press reset
Dependencies: mbed
Fork of BeaconDemo_RobotCodeNew by
main.cpp@21:efe191c96cbb, 2017-03-06 (annotated)
- Committer:
- jhok500
- Date:
- Mon Mar 06 13:52:55 2017 +0000
- Revision:
- 21:efe191c96cbb
- Parent:
- 20:fe7eb85cf3a1
- Child:
- 22:3643fc1c82b6
BeaconDemo_RobotCode
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jah128 | 6:ff3c66f7372b | 1 | /*********************************************************************** |
jah128 | 0:8a5497a2e366 | 2 | ** ██████╗ ███████╗██╗███████╗██╗ ██╗ █████╗ ██████╗ ███╗ ███╗ ** |
jah128 | 0:8a5497a2e366 | 3 | ** ██╔══██╗██╔════╝██║██╔════╝██║ ██║██╔══██╗██╔══██╗████╗ ████║ ** |
jah128 | 0:8a5497a2e366 | 4 | ** ██████╔╝███████╗██║███████╗██║ █╗ ██║███████║██████╔╝██╔████╔██║ ** |
jah128 | 0:8a5497a2e366 | 5 | ** ██╔═══╝ ╚════██║██║╚════██║██║███╗██║██╔══██║██╔══██╗██║╚██╔╝██║ ** |
jah128 | 0:8a5497a2e366 | 6 | ** ██║ ███████║██║███████║╚███╔███╔╝██║ ██║██║ ██║██║ ╚═╝ ██║ ** |
jah128 | 0:8a5497a2e366 | 7 | ** ╚═╝ ╚══════╝╚═╝╚══════╝ ╚══╝╚══╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═╝ ** |
jah128 | 0:8a5497a2e366 | 8 | ************************************************************************ |
jah128 | 0:8a5497a2e366 | 9 | **(C) Dr James Hilder - York Robotics Laboratory - University of York ** |
jah128 | 0:8a5497a2e366 | 10 | ***********************************************************************/ |
jah128 | 0:8a5497a2e366 | 11 | |
jah128 | 6:ff3c66f7372b | 12 | /// PsiSwarm Beautiful Meme Project Source Code |
jah128 | 16:976a1d0ea897 | 13 | /// Version 0.21 |
jah128 | 6:ff3c66f7372b | 14 | /// James Hilder, Alan Millard, Homero Elizondo, Jon Timmis |
jah128 | 6:ff3c66f7372b | 15 | /// University of York |
jah128 | 0:8a5497a2e366 | 16 | |
jah128 | 10:1b09d4bb847b | 17 | /// Include main.h - this includes psiswarm.h all the other necessary core files |
jah128 | 10:1b09d4bb847b | 18 | #include "main.h" |
jah128 | 9:085e090e1ec1 | 19 | |
jah128 | 10:1b09d4bb847b | 20 | char * program_name = "B-Meme"; |
jah128 | 10:1b09d4bb847b | 21 | char * author_name = "YRL"; |
jah128 | 16:976a1d0ea897 | 22 | char * version_name = "0.21"; |
jah128 | 0:8a5497a2e366 | 23 | |
jah128 | 6:ff3c66f7372b | 24 | // IMPORTANT!!! |
jah128 | 6:ff3c66f7372b | 25 | // Do not call the IR functions at all as they will interfere with the correct operation of this program |
jah128 | 6:ff3c66f7372b | 26 | // Instead, use the values held in the variables below; they are updated every 500ms |
jah128 | 6:ff3c66f7372b | 27 | |
jah128 | 6:ff3c66f7372b | 28 | char beacon_found = 0; // This will be a 1 when a beacon was detected during the previous 500ms window |
jah128 | 6:ff3c66f7372b | 29 | int beacon_heading = 0; // This is the heading from the last time a beacon was detected |
jah128 | 6:ff3c66f7372b | 30 | char robots_found[8]; // These will be a 1 when the respective robot [excluding self] was detected during the previous 500ms window |
jah128 | 7:ef9ab01b9e26 | 31 | int robots_heading[8]; // These are the headings from the last time the respective robots were detected |
jah128 | 6:ff3c66f7372b | 32 | unsigned short robots_distance[8]; // This is the maximum sensor value from the last time the respective robot was detected |
jah128 | 6:ff3c66f7372b | 33 | unsigned short reflected_sensor_data[8]; // The reflected IR values when this robots emitters are on |
jah128 | 6:ff3c66f7372b | 34 | unsigned short background_sensor_data[8];// The raw IR values when no robot (or beacon) should have its IR on |
jah128 | 6:ff3c66f7372b | 35 | |
jah128 | 17:f26a5805cde3 | 36 | |
jah128 | 17:f26a5805cde3 | 37 | char toggle_program_stage = 0; |
jah128 | 17:f26a5805cde3 | 38 | char default_normal_program = 5; // The program to run on turn on (after 'face beacon' program) |
jah128 | 17:f26a5805cde3 | 39 | char use_recharging_program = 0; // Set to 1 to force robot to run recharging program when battery voltage drops below a threshold |
jah128 | 6:ff3c66f7372b | 40 | char user_code_debug = 1; // Set to 1 to show terminal messages from "out" function [specific to this code] |
jah128 | 10:1b09d4bb847b | 41 | char display_debug_inf = 0; // Set to 1 to show debug info about beacon\robots on display [instead of running program info] |
jah128 | 10:1b09d4bb847b | 42 | char main_program_state; // Index of the currently running program |
jah128 | 13:f5994956b1ba | 43 | char previous_program; // Used to hold previous running program when it is paused for switch press etc |
jah128 | 10:1b09d4bb847b | 44 | char program_changed = 0; // Flag to update display when program is changed |
jah128 | 12:daa53285b6e4 | 45 | char program_run_init = 0; // Flag to tell program to run its initialisation on first loop, if neccessary |
jah128 | 10:1b09d4bb847b | 46 | char success_count = 0; // Flag to indicate the success of a program |
jah128 | 10:1b09d4bb847b | 47 | char step_cycle = 0; // Alternates between 0 and 1 in successive time-steps |
jah128 | 10:1b09d4bb847b | 48 | char target_reached = 0; // Flag to indicate if a program target has been reached |
jah128 | 10:1b09d4bb847b | 49 | char prog_name [17]; // Stores the name of the running program [line 0 on the display] |
jah128 | 10:1b09d4bb847b | 50 | char prog_info [17]; // Stores information about the current state of the program [line 1 on the display] |
jah128 | 10:1b09d4bb847b | 51 | char disable_ir_emitters = 0; // Used to disable IR emission during charging etc [use with caution!] |
jah128 | 10:1b09d4bb847b | 52 | char recharging_state = 0; // Stores the state of the recharging program (0 is not currently running) |
jah128 | 13:f5994956b1ba | 53 | char switch_held = 0; // Used for detected when the cursor switch is held to override program choice |
jah128 | 13:f5994956b1ba | 54 | char choose_program_mode = 0; |
jah128 | 16:976a1d0ea897 | 55 | char program_count = 8; |
jah128 | 13:f5994956b1ba | 56 | char program_selection; |
jah128 | 17:f26a5805cde3 | 57 | Ticker toggle_program_ticker; |
jah128 | 11:7b3ee540ba56 | 58 | |
jah128 | 14:f623db1e6184 | 59 | float battery_low_threshold = 3.60; // Threshold at which to interrupt program and start recharging routine: suggest 3.55 |
jah128 | 14:f623db1e6184 | 60 | float battery_high_threshold = 3.95; // Threshold at which to end battery recharging routine and resume normal program: suggest 4.0 |
jah128 | 11:7b3ee540ba56 | 61 | |
jah128 | 9:085e090e1ec1 | 62 | Ticker main_loop_ticker; |
jah128 | 9:085e090e1ec1 | 63 | |
JamesSW | 19:b5788427db67 | 64 | |
jah128 | 10:1b09d4bb847b | 65 | ///This is the main loop for the Beautiful Meme code. The code block is run once every 250mS* [with 4Hz beacon] once all the IR samples have been collected. |
jah128 | 9:085e090e1ec1 | 66 | void main_loop() |
jah128 | 9:085e090e1ec1 | 67 | { |
jah128 | 13:f5994956b1ba | 68 | if(switch_held == 1)switch_held=2; |
jah128 | 14:f623db1e6184 | 69 | if(switch_held == 3 && choose_program_mode == 0) { |
jah128 | 13:f5994956b1ba | 70 | //The switch has been held right and then released: stop the current program |
jah128 | 13:f5994956b1ba | 71 | previous_program = main_program_state; |
jah128 | 13:f5994956b1ba | 72 | program_selection = previous_program; |
jah128 | 13:f5994956b1ba | 73 | choose_program_mode = 1; |
jah128 | 13:f5994956b1ba | 74 | set_program(255); |
jah128 | 14:f623db1e6184 | 75 | set_program_info(get_program_name(program_selection)); |
jah128 | 13:f5994956b1ba | 76 | } |
jah128 | 10:1b09d4bb847b | 77 | if(use_recharging_program == 1)recharging_program(); |
jah128 | 9:085e090e1ec1 | 78 | update_display(); |
jah128 | 10:1b09d4bb847b | 79 | if(recharging_state == 0) { |
jah128 | 10:1b09d4bb847b | 80 | switch(main_program_state) { |
jah128 | 10:1b09d4bb847b | 81 | case 0: //Case 0 is the initial program: turn to face beacon |
jah128 | 10:1b09d4bb847b | 82 | if(step_cycle == 0) { |
jah128 | 10:1b09d4bb847b | 83 | char turn_status = turn_to_bearing(0); |
jah128 | 10:1b09d4bb847b | 84 | if(turn_status == 0) { |
jah128 | 10:1b09d4bb847b | 85 | success_count ++; |
jah128 | 10:1b09d4bb847b | 86 | if(success_count > 1) set_program(default_normal_program); |
jah128 | 10:1b09d4bb847b | 87 | } else success_count = 0; |
jah128 | 10:1b09d4bb847b | 88 | } |
jah128 | 10:1b09d4bb847b | 89 | break; |
jah128 | 10:1b09d4bb847b | 90 | case 1: |
jah128 | 10:1b09d4bb847b | 91 | target_reached = 0; |
jah128 | 10:1b09d4bb847b | 92 | head_to_bearing_program(0); |
jah128 | 10:1b09d4bb847b | 93 | if(target_reached == 1) set_program(2); |
jah128 | 10:1b09d4bb847b | 94 | break; |
jah128 | 10:1b09d4bb847b | 95 | case 2: |
jah128 | 10:1b09d4bb847b | 96 | target_reached = 0; |
jah128 | 10:1b09d4bb847b | 97 | head_to_bearing_program(180); |
jah128 | 10:1b09d4bb847b | 98 | if(target_reached == 1) set_program(1); |
jah128 | 10:1b09d4bb847b | 99 | break; |
jah128 | 10:1b09d4bb847b | 100 | case 3: |
jah128 | 10:1b09d4bb847b | 101 | curved_random_walk_with_interaction_program(); |
jah128 | 10:1b09d4bb847b | 102 | break; |
jah128 | 10:1b09d4bb847b | 103 | case 4: |
jah128 | 10:1b09d4bb847b | 104 | straight_random_walk_with_interaction_program(); |
jah128 | 10:1b09d4bb847b | 105 | break; |
jah128 | 10:1b09d4bb847b | 106 | case 5: |
jah128 | 13:f5994956b1ba | 107 | find_space_program(1); |
jah128 | 10:1b09d4bb847b | 108 | break; |
jah128 | 12:daa53285b6e4 | 109 | case 6: |
jah128 | 13:f5994956b1ba | 110 | clustering_program(0,1); |
jah128 | 12:daa53285b6e4 | 111 | break; |
jah128 | 16:976a1d0ea897 | 112 | case 7: |
jah128 | 16:976a1d0ea897 | 113 | tag_game_program(); |
jah128 | 16:976a1d0ea897 | 114 | break; |
JamesSW | 19:b5788427db67 | 115 | case 8: |
JamesSW | 19:b5788427db67 | 116 | mockFlocking(); |
JamesSW | 19:b5788427db67 | 117 | break; |
JamesSW | 19:b5788427db67 | 118 | case 254: |
JamesSW | 19:b5788427db67 | 119 | init(); |
JamesSW | 19:b5788427db67 | 120 | break; |
jah128 | 13:f5994956b1ba | 121 | case 255: |
jah128 | 13:f5994956b1ba | 122 | stop_program(); |
jah128 | 14:f623db1e6184 | 123 | break; |
jah128 | 10:1b09d4bb847b | 124 | } |
jah128 | 9:085e090e1ec1 | 125 | } |
jah128 | 9:085e090e1ec1 | 126 | step_cycle=1-step_cycle; |
jah128 | 9:085e090e1ec1 | 127 | } |
jah128 | 9:085e090e1ec1 | 128 | |
jah128 | 1:f6356cf1cefc | 129 | ///Place user code here that should be run after initialisation but before the main loop |
jah128 | 6:ff3c66f7372b | 130 | void user_code_setup() |
jah128 | 13:f5994956b1ba | 131 | { |
jah128 | 17:f26a5805cde3 | 132 | // Note for Alex: |
jah128 | 17:f26a5805cde3 | 133 | // |
jah128 | 17:f26a5805cde3 | 134 | // This is the code we have been working on for the beautiful meme project |
jah128 | 17:f26a5805cde3 | 135 | // |
jah128 | 17:f26a5805cde3 | 136 | // If you were to comment out (or delete...) all the code here, the robot will just |
jah128 | 17:f26a5805cde3 | 137 | // sit in an 'idle' loop when you turn it on - but should respond to any command codes |
jah128 | 17:f26a5805cde3 | 138 | // that are sent. |
jah128 | 17:f26a5805cde3 | 139 | |
jah128 | 9:085e090e1ec1 | 140 | wait(0.8); |
jah128 | 9:085e090e1ec1 | 141 | display.clear_display(); |
jah128 | 9:085e090e1ec1 | 142 | display.set_position(0,0); |
jhok500 | 21:efe191c96cbb | 143 | display.write_string("SCI MUSEUM"); |
jah128 | 9:085e090e1ec1 | 144 | display.set_position(1,0); |
jah128 | 9:085e090e1ec1 | 145 | display.write_string(" PROJECT"); |
jah128 | 9:085e090e1ec1 | 146 | wait(0.2); |
jah128 | 6:ff3c66f7372b | 147 | out("------------------------------------------------------\n"); |
jhok500 | 21:efe191c96cbb | 148 | out("Dank Meme Project Demo Code \n"); |
jah128 | 6:ff3c66f7372b | 149 | out("------------------------------------------------------\n"); |
jah128 | 6:ff3c66f7372b | 150 | locate_beacon(); |
jah128 | 6:ff3c66f7372b | 151 | while(beacon_found == 0) { |
jah128 | 9:085e090e1ec1 | 152 | wait(0.5); |
jah128 | 6:ff3c66f7372b | 153 | locate_beacon(); |
GusZ92 | 18:3fa5cd53ed45 | 154 | } |
GusZ92 | 18:3fa5cd53ed45 | 155 | //toggle_program_ticker.attach(toggle_program,20); |
jah128 | 6:ff3c66f7372b | 156 | start_infrared_timers(); |
jah128 | 9:085e090e1ec1 | 157 | main_loop_ticker.attach_us(&main_loop,BEACON_PERIOD * 10); |
GusZ92 | 18:3fa5cd53ed45 | 158 | set_program(4);//for debug ...was 0 ---------- |
jah128 | 9:085e090e1ec1 | 159 | set_leds(0x00,0x00); |
jah128 | 10:1b09d4bb847b | 160 | set_center_led(3,0.5); |
jah128 | 9:085e090e1ec1 | 161 | display.clear_display(); |
jah128 | 9:085e090e1ec1 | 162 | display.set_position(0,0); |
jah128 | 9:085e090e1ec1 | 163 | display.write_string("BEACON FOUND AT"); |
jah128 | 9:085e090e1ec1 | 164 | display.set_position(1,0); |
jah128 | 9:085e090e1ec1 | 165 | char degrees_string[16]; |
jah128 | 9:085e090e1ec1 | 166 | sprintf(degrees_string,"%d DEGREES",beacon_heading); |
jah128 | 9:085e090e1ec1 | 167 | display.write_string(degrees_string); |
jah128 | 0:8a5497a2e366 | 168 | } |
jah128 | 0:8a5497a2e366 | 169 | |
jah128 | 17:f26a5805cde3 | 170 | void toggle_program(){ |
jah128 | 17:f26a5805cde3 | 171 | toggle_program_stage ++; |
jah128 | 17:f26a5805cde3 | 172 | if(toggle_program_stage == 3) toggle_program_stage = 0; |
jah128 | 17:f26a5805cde3 | 173 | switch(toggle_program_stage){ |
jah128 | 17:f26a5805cde3 | 174 | case 0: set_program(5); break; |
jah128 | 17:f26a5805cde3 | 175 | case 1: set_program(6); break; |
jah128 | 17:f26a5805cde3 | 176 | case 2: set_program(7);break; |
jah128 | 17:f26a5805cde3 | 177 | case 3: set_program(7); break; |
jah128 | 17:f26a5805cde3 | 178 | } |
jah128 | 17:f26a5805cde3 | 179 | } |
jah128 | 17:f26a5805cde3 | 180 | |
jah128 | 1:f6356cf1cefc | 181 | /// Code goes here to handle what should happen when the user switch is pressed |
jah128 | 0:8a5497a2e366 | 182 | void handle_switch_event(char switch_state) |
jah128 | 0:8a5497a2e366 | 183 | { |
jah128 | 6:ff3c66f7372b | 184 | /// Switch_state = 1 if up is pressed, 2 if down is pressed, 4 if left is pressed, 8 if right is pressed and 16 if the center button is pressed |
jah128 | 1:f6356cf1cefc | 185 | /// NB For maximum compatability it is recommended to minimise reliance on center button press |
jah128 | 13:f5994956b1ba | 186 | if(choose_program_mode == 0) { |
jah128 | 13:f5994956b1ba | 187 | if(switch_state == 8) switch_held = 1; |
jah128 | 13:f5994956b1ba | 188 | else if(switch_state == 0 && switch_held == 2) switch_held = 3; |
jah128 | 13:f5994956b1ba | 189 | else switch_held = 0; |
jah128 | 13:f5994956b1ba | 190 | } else { |
jah128 | 13:f5994956b1ba | 191 | // We are in choose program mode |
jah128 | 13:f5994956b1ba | 192 | if(switch_state == 8) { |
jah128 | 13:f5994956b1ba | 193 | program_selection ++; |
jah128 | 13:f5994956b1ba | 194 | if(program_selection > program_count) program_selection = 0; |
jah128 | 13:f5994956b1ba | 195 | if(program_selection == program_count) set_program_info("RECHARGE"); |
jah128 | 13:f5994956b1ba | 196 | else set_program_info(get_program_name(program_selection)); |
jah128 | 13:f5994956b1ba | 197 | } |
jah128 | 13:f5994956b1ba | 198 | if(switch_state == 4) { |
jah128 | 14:f623db1e6184 | 199 | if(program_selection == 0) program_selection = program_count; |
jah128 | 14:f623db1e6184 | 200 | else program_selection --; |
jah128 | 13:f5994956b1ba | 201 | if(program_selection == program_count) set_program_info("RECHARGE"); |
jah128 | 13:f5994956b1ba | 202 | else set_program_info(get_program_name(program_selection)); |
jah128 | 13:f5994956b1ba | 203 | } |
jah128 | 13:f5994956b1ba | 204 | if(switch_state == 1 || switch_state == 2){ |
jah128 | 13:f5994956b1ba | 205 | if(program_selection == program_count){ |
jah128 | 13:f5994956b1ba | 206 | recharging_state = 1; |
jah128 | 13:f5994956b1ba | 207 | set_program(previous_program); |
jah128 | 13:f5994956b1ba | 208 | strcpy(prog_name,"CHARGING PROGRAM"); |
jah128 | 13:f5994956b1ba | 209 | set_program_info("HEAD TO BEACON"); |
jah128 | 13:f5994956b1ba | 210 | |
jah128 | 13:f5994956b1ba | 211 | } |
jah128 | 13:f5994956b1ba | 212 | else set_program(program_selection); |
jah128 | 13:f5994956b1ba | 213 | choose_program_mode = 0; |
jah128 | 13:f5994956b1ba | 214 | switch_held = 0; |
jah128 | 13:f5994956b1ba | 215 | } |
jah128 | 13:f5994956b1ba | 216 | } |
jah128 | 14:f623db1e6184 | 217 | //out("Switch:%d Switch_held:%d Program_Selection:%d Program_count:%d Prog_Info:%s\n",switch_state,switch_held,program_selection,program_count,prog_info); |
jah128 | 0:8a5497a2e366 | 218 | } |
GusZ92 | 18:3fa5cd53ed45 | 219 | void handle_user_serial_message(char * message, char length, char interface) |
GusZ92 | 18:3fa5cd53ed45 | 220 | { |
GusZ92 | 18:3fa5cd53ed45 | 221 | // This is where user code for handling a (non-system) serial message should go |
GusZ92 | 18:3fa5cd53ed45 | 222 | // |
GusZ92 | 18:3fa5cd53ed45 | 223 | // message = pointer to message char array |
GusZ92 | 18:3fa5cd53ed45 | 224 | // length = length of message |
GusZ92 | 18:3fa5cd53ed45 | 225 | // interface = 0 for PC serial connection, 1 for Bluetooth |
GusZ92 | 18:3fa5cd53ed45 | 226 | |
GusZ92 | 18:3fa5cd53ed45 | 227 | char buffer[255]; |
GusZ92 | 18:3fa5cd53ed45 | 228 | sprintf(buffer,message,length); |
GusZ92 | 18:3fa5cd53ed45 | 229 | for(int i=0; i<length; i++) { |
GusZ92 | 18:3fa5cd53ed45 | 230 | buffer[i]=message[i]; |
GusZ92 | 18:3fa5cd53ed45 | 231 | } |
GusZ92 | 18:3fa5cd53ed45 | 232 | buffer[length]=0; |
GusZ92 | 18:3fa5cd53ed45 | 233 | if(interface) debug("Received BT message:%s [%d chars]\n",buffer,length); |
GusZ92 | 18:3fa5cd53ed45 | 234 | else debug("Received USB message:%s [%d chars]\n",buffer,length); |
GusZ92 | 18:3fa5cd53ed45 | 235 | |
GusZ92 | 18:3fa5cd53ed45 | 236 | display.clear_display(); |
GusZ92 | 18:3fa5cd53ed45 | 237 | display.set_position(0,0); |
jhok500 | 21:efe191c96cbb | 238 | //display.write_string(buffer); |
GusZ92 | 18:3fa5cd53ed45 | 239 | |
GusZ92 | 18:3fa5cd53ed45 | 240 | //"A" = 0x41 |
jhok500 | 20:fe7eb85cf3a1 | 241 | if(buffer[0] == 'A'){ |
jhok500 | 21:efe191c96cbb | 242 | |
jhok500 | 20:fe7eb85cf3a1 | 243 | main_program_state = 255; |
jhok500 | 21:efe191c96cbb | 244 | display.write_string("MALLARD QUACK QUACK"); |
GusZ92 | 18:3fa5cd53ed45 | 245 | } |
GusZ92 | 18:3fa5cd53ed45 | 246 | //"B" = 0x42 |
jhok500 | 20:fe7eb85cf3a1 | 247 | else if(buffer[0] == 'B'){ |
jhok500 | 21:efe191c96cbb | 248 | //set_program(1); |
jhok500 | 20:fe7eb85cf3a1 | 249 | main_program_state = 1; |
jhok500 | 21:efe191c96cbb | 250 | display.write_string("LAPS (BEACON)"); |
GusZ92 | 18:3fa5cd53ed45 | 251 | } |
GusZ92 | 18:3fa5cd53ed45 | 252 | //"C" = 0x43 |
jhok500 | 20:fe7eb85cf3a1 | 253 | else if(buffer[0] == 'C'){ |
jhok500 | 20:fe7eb85cf3a1 | 254 | main_program_state = 2; |
jhok500 | 21:efe191c96cbb | 255 | display.write_string("LAPS (SOUTH)"); |
GusZ92 | 18:3fa5cd53ed45 | 256 | } |
GusZ92 | 18:3fa5cd53ed45 | 257 | //"D" = 0x44 |
jhok500 | 20:fe7eb85cf3a1 | 258 | else if(buffer[0] == 'D'){ |
jhok500 | 20:fe7eb85cf3a1 | 259 | main_program_state = 8; |
jhok500 | 21:efe191c96cbb | 260 | display.write_string("FLOCKING"); |
jhok500 | 20:fe7eb85cf3a1 | 261 | } |
jhok500 | 20:fe7eb85cf3a1 | 262 | else if(buffer[0] == 'E'){ |
jhok500 | 20:fe7eb85cf3a1 | 263 | main_program_state = 4; |
jhok500 | 21:efe191c96cbb | 264 | display.write_string("RANDOM WALK"); |
jhok500 | 20:fe7eb85cf3a1 | 265 | } |
jhok500 | 20:fe7eb85cf3a1 | 266 | else if(buffer[0] == 'F'){ |
jhok500 | 20:fe7eb85cf3a1 | 267 | main_program_state = 6; |
jhok500 | 21:efe191c96cbb | 268 | display.write_string("AGGREGATION"); |
jhok500 | 20:fe7eb85cf3a1 | 269 | } |
jhok500 | 20:fe7eb85cf3a1 | 270 | else if(buffer[0] == 'G'){ |
jhok500 | 20:fe7eb85cf3a1 | 271 | main_program_state = 5; |
jhok500 | 21:efe191c96cbb | 272 | display.write_string("FIND SPACE"); |
jhok500 | 20:fe7eb85cf3a1 | 273 | } |
jhok500 | 20:fe7eb85cf3a1 | 274 | else if(buffer[0] == 'H'){ |
jhok500 | 20:fe7eb85cf3a1 | 275 | main_program_state = 6; |
jhok500 | 21:efe191c96cbb | 276 | display.write_string("CLUSTERING"); |
jhok500 | 20:fe7eb85cf3a1 | 277 | } |
jhok500 | 20:fe7eb85cf3a1 | 278 | else if(buffer[0] == 'I'){ |
jhok500 | 20:fe7eb85cf3a1 | 279 | main_program_state = 7; |
jhok500 | 21:efe191c96cbb | 280 | display.write_string("TAG"); |
GusZ92 | 18:3fa5cd53ed45 | 281 | } |
GusZ92 | 18:3fa5cd53ed45 | 282 | else |
GusZ92 | 18:3fa5cd53ed45 | 283 | { |
JamesSW | 19:b5788427db67 | 284 | |
GusZ92 | 18:3fa5cd53ed45 | 285 | } |
GusZ92 | 18:3fa5cd53ed45 | 286 | } |
jah128 | 1:f6356cf1cefc | 287 | /// The main routine: it is recommended to leave this function alone and add user code to the above functions |
jah128 | 6:ff3c66f7372b | 288 | int main() |
jah128 | 6:ff3c66f7372b | 289 | { |
jah128 | 1:f6356cf1cefc | 290 | ///init() in psiswarm.cpp sets up the robot |
jah128 | 0:8a5497a2e366 | 291 | init(); |
jah128 | 0:8a5497a2e366 | 292 | user_code_setup(); |
jah128 | 0:8a5497a2e366 | 293 | user_code_running = 1; |
jah128 | 0:8a5497a2e366 | 294 | while(1) { |
jhok500 | 21:efe191c96cbb | 295 | |
jah128 | 9:085e090e1ec1 | 296 | wait(1); |
jah128 | 6:ff3c66f7372b | 297 | } |
jah128 | 6:ff3c66f7372b | 298 | } |
jah128 | 6:ff3c66f7372b | 299 | |
jah128 | 10:1b09d4bb847b | 300 | char * get_program_name(int index) |
jah128 | 13:f5994956b1ba | 301 | { |
jah128 | 10:1b09d4bb847b | 302 | char * ret_name = new char[17]; |
jah128 | 13:f5994956b1ba | 303 | switch(index) { |
jah128 | 10:1b09d4bb847b | 304 | case 0: |
jah128 | 10:1b09d4bb847b | 305 | strcpy(ret_name,"FACE BEACON"); |
jah128 | 10:1b09d4bb847b | 306 | break; |
jah128 | 10:1b09d4bb847b | 307 | case 1: |
jah128 | 10:1b09d4bb847b | 308 | strcpy(ret_name,"HEAD TO BEACON"); |
jah128 | 10:1b09d4bb847b | 309 | break; |
jah128 | 10:1b09d4bb847b | 310 | case 2: |
jah128 | 10:1b09d4bb847b | 311 | strcpy(ret_name,"HEAD TO SOUTH"); |
jah128 | 10:1b09d4bb847b | 312 | break; |
jah128 | 10:1b09d4bb847b | 313 | case 3: |
jah128 | 10:1b09d4bb847b | 314 | strcpy(ret_name,"RANDOM WALK 1"); |
jah128 | 10:1b09d4bb847b | 315 | break; |
jah128 | 10:1b09d4bb847b | 316 | case 4: |
GusZ92 | 18:3fa5cd53ed45 | 317 | strcpy(ret_name,"WAITING COMMAND");///gus, was random walk2 |
jah128 | 10:1b09d4bb847b | 318 | break; |
jah128 | 10:1b09d4bb847b | 319 | case 5: |
jah128 | 10:1b09d4bb847b | 320 | strcpy(ret_name,"FIND SPACE"); |
jah128 | 10:1b09d4bb847b | 321 | break; |
jah128 | 12:daa53285b6e4 | 322 | case 6: |
jah128 | 12:daa53285b6e4 | 323 | strcpy(ret_name,"CLUSTERING"); |
jah128 | 12:daa53285b6e4 | 324 | break; |
jah128 | 16:976a1d0ea897 | 325 | case 7: |
jah128 | 16:976a1d0ea897 | 326 | strcpy(ret_name,"TAG GAME"); |
jah128 | 16:976a1d0ea897 | 327 | break; |
jah128 | 13:f5994956b1ba | 328 | case 255: |
jah128 | 13:f5994956b1ba | 329 | strcpy(ret_name,"PROGRAM:"); |
jah128 | 14:f623db1e6184 | 330 | break; |
jah128 | 10:1b09d4bb847b | 331 | } |
jah128 | 10:1b09d4bb847b | 332 | return ret_name; |
jah128 | 10:1b09d4bb847b | 333 | } |
jah128 | 10:1b09d4bb847b | 334 | |
jah128 | 10:1b09d4bb847b | 335 | void set_program(int index) |
jah128 | 10:1b09d4bb847b | 336 | { |
jah128 | 10:1b09d4bb847b | 337 | main_program_state = index; |
jah128 | 10:1b09d4bb847b | 338 | program_changed = 1; |
jah128 | 12:daa53285b6e4 | 339 | program_run_init = 1; |
jah128 | 10:1b09d4bb847b | 340 | strcpy(prog_info,""); |
jah128 | 13:f5994956b1ba | 341 | strcpy(prog_name,get_program_name(index)); |
jah128 | 10:1b09d4bb847b | 342 | } |
jah128 | 10:1b09d4bb847b | 343 | |
jah128 | 10:1b09d4bb847b | 344 | void set_program_info(char * info) |
jah128 | 10:1b09d4bb847b | 345 | { |
jah128 | 10:1b09d4bb847b | 346 | strcpy(prog_info,info); |
jah128 | 10:1b09d4bb847b | 347 | program_changed = 1; |
jah128 | 10:1b09d4bb847b | 348 | } |
jah128 | 9:085e090e1ec1 | 349 | |
jah128 | 9:085e090e1ec1 | 350 | void update_display() |
jah128 | 9:085e090e1ec1 | 351 | { |
jah128 | 10:1b09d4bb847b | 352 | if(program_changed == 1) { |
jah128 | 10:1b09d4bb847b | 353 | program_changed = 0; |
jah128 | 9:085e090e1ec1 | 354 | display.clear_display(); |
jah128 | 13:f5994956b1ba | 355 | |
jah128 | 10:1b09d4bb847b | 356 | if(display_debug_inf==1) display_debug_info(); |
jah128 | 13:f5994956b1ba | 357 | else { |
jah128 | 10:1b09d4bb847b | 358 | display.set_position(0,0); |
jah128 | 10:1b09d4bb847b | 359 | display.write_string(prog_name); |
jah128 | 9:085e090e1ec1 | 360 | } |
jah128 | 10:1b09d4bb847b | 361 | display.set_position(1,0); |
jah128 | 10:1b09d4bb847b | 362 | display.write_string(prog_info); |
jah128 | 9:085e090e1ec1 | 363 | } |
jah128 | 9:085e090e1ec1 | 364 | } |
jah128 | 9:085e090e1ec1 | 365 | |
jah128 | 9:085e090e1ec1 | 366 | void display_debug_info() |
jah128 | 9:085e090e1ec1 | 367 | { |
jah128 | 9:085e090e1ec1 | 368 | char disp_line[16] = "- - - - - - - -"; |
jah128 | 9:085e090e1ec1 | 369 | if(beacon_found==1)disp_line[0]='B'; |
jah128 | 10:1b09d4bb847b | 370 | for(int i=1; i<8; i++) { |
jah128 | 10:1b09d4bb847b | 371 | if(robots_found[i])disp_line[((i)*2)]=48+i; |
jah128 | 9:085e090e1ec1 | 372 | } |
jah128 | 10:1b09d4bb847b | 373 | display.set_position(0,0); |
jah128 | 9:085e090e1ec1 | 374 | display.write_string(disp_line); |
jah128 | 9:085e090e1ec1 | 375 | } |
jah128 | 9:085e090e1ec1 | 376 | |
jah128 | 6:ff3c66f7372b | 377 | /// Verbose output |
jah128 | 6:ff3c66f7372b | 378 | void out(const char* format, ...) |
jah128 | 6:ff3c66f7372b | 379 | { |
jah128 | 6:ff3c66f7372b | 380 | char buffer[256]; |
jah128 | 6:ff3c66f7372b | 381 | if (debug_mode) { |
jah128 | 6:ff3c66f7372b | 382 | va_list vl; |
jah128 | 6:ff3c66f7372b | 383 | va_start(vl, format); |
jah128 | 6:ff3c66f7372b | 384 | vsprintf(buffer,format,vl); |
jah128 | 6:ff3c66f7372b | 385 | if(user_code_debug == 1) pc.printf("%s", buffer); |
jah128 | 6:ff3c66f7372b | 386 | va_end(vl); |
jah128 | 0:8a5497a2e366 | 387 | } |
jah128 | 0:8a5497a2e366 | 388 | } |