YRL Maze lab made more script-y

Dependencies:   PsiSwarmLab-ScriptingBased mbed

Fork of UKESF_Lab by UKESF Headstart Summer School

Committer:
jah128
Date:
Wed Oct 28 11:42:25 2015 +0000
Revision:
15:d18667eb57f5
Parent:
14:f623db1e6184
Child:
16:976a1d0ea897
Removed some debug lines

Who changed what in which revision?

UserRevisionLine numberNew 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 10:1b09d4bb847b 13 /// Version 0.2
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 10:1b09d4bb847b 22 char * version_name = "0.2";
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 12:daa53285b6e4 36 char default_normal_program = 6; // The program to run on turn on (after 'face beacon' program)
jah128 10:1b09d4bb847b 37 char use_recharging_program = 1; // Set to 1 to force robot to run recharging program when battery voltage drops below a threshold
jah128 6:ff3c66f7372b 38 char user_code_debug = 1; // Set to 1 to show terminal messages from "out" function [specific to this code]
jah128 10:1b09d4bb847b 39 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 40 char main_program_state; // Index of the currently running program
jah128 13:f5994956b1ba 41 char previous_program; // Used to hold previous running program when it is paused for switch press etc
jah128 10:1b09d4bb847b 42 char program_changed = 0; // Flag to update display when program is changed
jah128 12:daa53285b6e4 43 char program_run_init = 0; // Flag to tell program to run its initialisation on first loop, if neccessary
jah128 10:1b09d4bb847b 44 char success_count = 0; // Flag to indicate the success of a program
jah128 10:1b09d4bb847b 45 char step_cycle = 0; // Alternates between 0 and 1 in successive time-steps
jah128 10:1b09d4bb847b 46 char target_reached = 0; // Flag to indicate if a program target has been reached
jah128 10:1b09d4bb847b 47 char prog_name [17]; // Stores the name of the running program [line 0 on the display]
jah128 10:1b09d4bb847b 48 char prog_info [17]; // Stores information about the current state of the program [line 1 on the display]
jah128 10:1b09d4bb847b 49 char disable_ir_emitters = 0; // Used to disable IR emission during charging etc [use with caution!]
jah128 10:1b09d4bb847b 50 char recharging_state = 0; // Stores the state of the recharging program (0 is not currently running)
jah128 13:f5994956b1ba 51 char switch_held = 0; // Used for detected when the cursor switch is held to override program choice
jah128 13:f5994956b1ba 52 char choose_program_mode = 0;
jah128 13:f5994956b1ba 53 char program_count = 7;
jah128 13:f5994956b1ba 54 char program_selection;
jah128 11:7b3ee540ba56 55
jah128 14:f623db1e6184 56 float battery_low_threshold = 3.60; // Threshold at which to interrupt program and start recharging routine: suggest 3.55
jah128 14:f623db1e6184 57 float battery_high_threshold = 3.95; // Threshold at which to end battery recharging routine and resume normal program: suggest 4.0
jah128 11:7b3ee540ba56 58
jah128 9:085e090e1ec1 59 Ticker main_loop_ticker;
jah128 9:085e090e1ec1 60
jah128 10:1b09d4bb847b 61 ///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 62 void main_loop()
jah128 9:085e090e1ec1 63 {
jah128 13:f5994956b1ba 64 if(switch_held == 1)switch_held=2;
jah128 14:f623db1e6184 65 if(switch_held == 3 && choose_program_mode == 0) {
jah128 13:f5994956b1ba 66 //The switch has been held right and then released: stop the current program
jah128 13:f5994956b1ba 67 previous_program = main_program_state;
jah128 13:f5994956b1ba 68 program_selection = previous_program;
jah128 13:f5994956b1ba 69 choose_program_mode = 1;
jah128 13:f5994956b1ba 70 set_program(255);
jah128 14:f623db1e6184 71 set_program_info(get_program_name(program_selection));
jah128 13:f5994956b1ba 72 }
jah128 10:1b09d4bb847b 73 if(use_recharging_program == 1)recharging_program();
jah128 9:085e090e1ec1 74 update_display();
jah128 10:1b09d4bb847b 75 if(recharging_state == 0) {
jah128 10:1b09d4bb847b 76 switch(main_program_state) {
jah128 10:1b09d4bb847b 77 case 0: //Case 0 is the initial program: turn to face beacon
jah128 10:1b09d4bb847b 78 if(step_cycle == 0) {
jah128 10:1b09d4bb847b 79 char turn_status = turn_to_bearing(0);
jah128 10:1b09d4bb847b 80 if(turn_status == 0) {
jah128 10:1b09d4bb847b 81 success_count ++;
jah128 10:1b09d4bb847b 82 if(success_count > 1) set_program(default_normal_program);
jah128 10:1b09d4bb847b 83 } else success_count = 0;
jah128 10:1b09d4bb847b 84 }
jah128 10:1b09d4bb847b 85 break;
jah128 10:1b09d4bb847b 86 case 1:
jah128 10:1b09d4bb847b 87 target_reached = 0;
jah128 10:1b09d4bb847b 88 head_to_bearing_program(0);
jah128 10:1b09d4bb847b 89 if(target_reached == 1) set_program(2);
jah128 10:1b09d4bb847b 90 break;
jah128 10:1b09d4bb847b 91 case 2:
jah128 10:1b09d4bb847b 92 target_reached = 0;
jah128 10:1b09d4bb847b 93 head_to_bearing_program(180);
jah128 10:1b09d4bb847b 94 if(target_reached == 1) set_program(1);
jah128 10:1b09d4bb847b 95 break;
jah128 10:1b09d4bb847b 96 case 3:
jah128 10:1b09d4bb847b 97 curved_random_walk_with_interaction_program();
jah128 10:1b09d4bb847b 98 break;
jah128 10:1b09d4bb847b 99 case 4:
jah128 10:1b09d4bb847b 100 straight_random_walk_with_interaction_program();
jah128 10:1b09d4bb847b 101 break;
jah128 10:1b09d4bb847b 102 case 5:
jah128 13:f5994956b1ba 103 find_space_program(1);
jah128 10:1b09d4bb847b 104 break;
jah128 12:daa53285b6e4 105 case 6:
jah128 13:f5994956b1ba 106 clustering_program(0,1);
jah128 12:daa53285b6e4 107 break;
jah128 13:f5994956b1ba 108 case 255:
jah128 13:f5994956b1ba 109 stop_program();
jah128 14:f623db1e6184 110 break;
jah128 10:1b09d4bb847b 111 }
jah128 9:085e090e1ec1 112 }
jah128 9:085e090e1ec1 113 step_cycle=1-step_cycle;
jah128 9:085e090e1ec1 114 }
jah128 9:085e090e1ec1 115
jah128 1:f6356cf1cefc 116 ///Place user code here that should be run after initialisation but before the main loop
jah128 6:ff3c66f7372b 117 void user_code_setup()
jah128 13:f5994956b1ba 118 {
jah128 9:085e090e1ec1 119 wait(0.8);
jah128 9:085e090e1ec1 120 display.clear_display();
jah128 9:085e090e1ec1 121 display.set_position(0,0);
jah128 9:085e090e1ec1 122 display.write_string("BEAUTIFUL MEME");
jah128 9:085e090e1ec1 123 display.set_position(1,0);
jah128 9:085e090e1ec1 124 display.write_string(" PROJECT");
jah128 9:085e090e1ec1 125 wait(0.2);
jah128 6:ff3c66f7372b 126 out("------------------------------------------------------\n");
jah128 6:ff3c66f7372b 127 out("Beautiful Meme Project Demo Code \n");
jah128 6:ff3c66f7372b 128 out("------------------------------------------------------\n");
jah128 6:ff3c66f7372b 129 locate_beacon();
jah128 6:ff3c66f7372b 130 while(beacon_found == 0) {
jah128 9:085e090e1ec1 131 wait(0.5);
jah128 6:ff3c66f7372b 132 locate_beacon();
jah128 6:ff3c66f7372b 133 }
jah128 6:ff3c66f7372b 134 start_infrared_timers();
jah128 9:085e090e1ec1 135 main_loop_ticker.attach_us(&main_loop,BEACON_PERIOD * 10);
jah128 13:f5994956b1ba 136 set_program(5);
jah128 9:085e090e1ec1 137 set_leds(0x00,0x00);
jah128 10:1b09d4bb847b 138 set_center_led(3,0.5);
jah128 9:085e090e1ec1 139 display.clear_display();
jah128 9:085e090e1ec1 140 display.set_position(0,0);
jah128 9:085e090e1ec1 141 display.write_string("BEACON FOUND AT");
jah128 9:085e090e1ec1 142 display.set_position(1,0);
jah128 9:085e090e1ec1 143 char degrees_string[16];
jah128 9:085e090e1ec1 144 sprintf(degrees_string,"%d DEGREES",beacon_heading);
jah128 9:085e090e1ec1 145 display.write_string(degrees_string);
jah128 0:8a5497a2e366 146 }
jah128 0:8a5497a2e366 147
jah128 1:f6356cf1cefc 148 /// Code goes here to handle what should happen when the user switch is pressed
jah128 0:8a5497a2e366 149 void handle_switch_event(char switch_state)
jah128 0:8a5497a2e366 150 {
jah128 6:ff3c66f7372b 151 /// 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 152 /// NB For maximum compatability it is recommended to minimise reliance on center button press
jah128 13:f5994956b1ba 153 if(choose_program_mode == 0) {
jah128 13:f5994956b1ba 154 if(switch_state == 8) switch_held = 1;
jah128 13:f5994956b1ba 155 else if(switch_state == 0 && switch_held == 2) switch_held = 3;
jah128 13:f5994956b1ba 156 else switch_held = 0;
jah128 13:f5994956b1ba 157 } else {
jah128 13:f5994956b1ba 158 // We are in choose program mode
jah128 13:f5994956b1ba 159 if(switch_state == 8) {
jah128 13:f5994956b1ba 160 program_selection ++;
jah128 13:f5994956b1ba 161 if(program_selection > program_count) program_selection = 0;
jah128 13:f5994956b1ba 162 if(program_selection == program_count) set_program_info("RECHARGE");
jah128 13:f5994956b1ba 163 else set_program_info(get_program_name(program_selection));
jah128 13:f5994956b1ba 164 }
jah128 13:f5994956b1ba 165 if(switch_state == 4) {
jah128 14:f623db1e6184 166 if(program_selection == 0) program_selection = program_count;
jah128 14:f623db1e6184 167 else program_selection --;
jah128 13:f5994956b1ba 168 if(program_selection == program_count) set_program_info("RECHARGE");
jah128 13:f5994956b1ba 169 else set_program_info(get_program_name(program_selection));
jah128 13:f5994956b1ba 170 }
jah128 13:f5994956b1ba 171 if(switch_state == 1 || switch_state == 2){
jah128 13:f5994956b1ba 172 if(program_selection == program_count){
jah128 13:f5994956b1ba 173 recharging_state = 1;
jah128 13:f5994956b1ba 174 set_program(previous_program);
jah128 13:f5994956b1ba 175 strcpy(prog_name,"CHARGING PROGRAM");
jah128 13:f5994956b1ba 176 set_program_info("HEAD TO BEACON");
jah128 13:f5994956b1ba 177
jah128 13:f5994956b1ba 178 }
jah128 13:f5994956b1ba 179 else set_program(program_selection);
jah128 13:f5994956b1ba 180 choose_program_mode = 0;
jah128 13:f5994956b1ba 181 switch_held = 0;
jah128 13:f5994956b1ba 182 }
jah128 13:f5994956b1ba 183 }
jah128 14:f623db1e6184 184 //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 185 }
jah128 0:8a5497a2e366 186
jah128 1:f6356cf1cefc 187 /// The main routine: it is recommended to leave this function alone and add user code to the above functions
jah128 6:ff3c66f7372b 188 int main()
jah128 6:ff3c66f7372b 189 {
jah128 1:f6356cf1cefc 190 ///init() in psiswarm.cpp sets up the robot
jah128 0:8a5497a2e366 191 init();
jah128 0:8a5497a2e366 192 user_code_setup();
jah128 0:8a5497a2e366 193 user_code_running = 1;
jah128 0:8a5497a2e366 194 while(1) {
jah128 9:085e090e1ec1 195 wait(1);
jah128 6:ff3c66f7372b 196 }
jah128 6:ff3c66f7372b 197 }
jah128 6:ff3c66f7372b 198
jah128 10:1b09d4bb847b 199 char * get_program_name(int index)
jah128 13:f5994956b1ba 200 {
jah128 10:1b09d4bb847b 201 char * ret_name = new char[17];
jah128 13:f5994956b1ba 202 switch(index) {
jah128 10:1b09d4bb847b 203 case 0:
jah128 10:1b09d4bb847b 204 strcpy(ret_name,"FACE BEACON");
jah128 10:1b09d4bb847b 205 break;
jah128 10:1b09d4bb847b 206 case 1:
jah128 10:1b09d4bb847b 207 strcpy(ret_name,"HEAD TO BEACON");
jah128 10:1b09d4bb847b 208 break;
jah128 10:1b09d4bb847b 209 case 2:
jah128 10:1b09d4bb847b 210 strcpy(ret_name,"HEAD TO SOUTH");
jah128 10:1b09d4bb847b 211 break;
jah128 10:1b09d4bb847b 212 case 3:
jah128 10:1b09d4bb847b 213 strcpy(ret_name,"RANDOM WALK 1");
jah128 10:1b09d4bb847b 214 break;
jah128 10:1b09d4bb847b 215 case 4:
jah128 10:1b09d4bb847b 216 strcpy(ret_name,"RANDOM WALK 2");
jah128 10:1b09d4bb847b 217 break;
jah128 10:1b09d4bb847b 218 case 5:
jah128 10:1b09d4bb847b 219 strcpy(ret_name,"FIND SPACE");
jah128 10:1b09d4bb847b 220 break;
jah128 12:daa53285b6e4 221 case 6:
jah128 12:daa53285b6e4 222 strcpy(ret_name,"CLUSTERING");
jah128 12:daa53285b6e4 223 break;
jah128 13:f5994956b1ba 224 case 255:
jah128 13:f5994956b1ba 225 strcpy(ret_name,"PROGRAM:");
jah128 14:f623db1e6184 226 break;
jah128 10:1b09d4bb847b 227 }
jah128 10:1b09d4bb847b 228 return ret_name;
jah128 10:1b09d4bb847b 229 }
jah128 10:1b09d4bb847b 230
jah128 10:1b09d4bb847b 231 void set_program(int index)
jah128 10:1b09d4bb847b 232 {
jah128 10:1b09d4bb847b 233 main_program_state = index;
jah128 10:1b09d4bb847b 234 program_changed = 1;
jah128 12:daa53285b6e4 235 program_run_init = 1;
jah128 10:1b09d4bb847b 236 strcpy(prog_info,"");
jah128 13:f5994956b1ba 237 strcpy(prog_name,get_program_name(index));
jah128 10:1b09d4bb847b 238 }
jah128 10:1b09d4bb847b 239
jah128 10:1b09d4bb847b 240 void set_program_info(char * info)
jah128 10:1b09d4bb847b 241 {
jah128 10:1b09d4bb847b 242 strcpy(prog_info,info);
jah128 10:1b09d4bb847b 243 program_changed = 1;
jah128 10:1b09d4bb847b 244 }
jah128 9:085e090e1ec1 245
jah128 9:085e090e1ec1 246 void update_display()
jah128 9:085e090e1ec1 247 {
jah128 10:1b09d4bb847b 248 if(program_changed == 1) {
jah128 10:1b09d4bb847b 249 program_changed = 0;
jah128 9:085e090e1ec1 250 display.clear_display();
jah128 13:f5994956b1ba 251
jah128 10:1b09d4bb847b 252 if(display_debug_inf==1) display_debug_info();
jah128 13:f5994956b1ba 253 else {
jah128 10:1b09d4bb847b 254 display.set_position(0,0);
jah128 10:1b09d4bb847b 255 display.write_string(prog_name);
jah128 9:085e090e1ec1 256 }
jah128 10:1b09d4bb847b 257 display.set_position(1,0);
jah128 10:1b09d4bb847b 258 display.write_string(prog_info);
jah128 9:085e090e1ec1 259 }
jah128 9:085e090e1ec1 260 }
jah128 9:085e090e1ec1 261
jah128 9:085e090e1ec1 262 void display_debug_info()
jah128 9:085e090e1ec1 263 {
jah128 9:085e090e1ec1 264 char disp_line[16] = "- - - - - - - -";
jah128 9:085e090e1ec1 265 if(beacon_found==1)disp_line[0]='B';
jah128 10:1b09d4bb847b 266 for(int i=1; i<8; i++) {
jah128 10:1b09d4bb847b 267 if(robots_found[i])disp_line[((i)*2)]=48+i;
jah128 9:085e090e1ec1 268 }
jah128 10:1b09d4bb847b 269 display.set_position(0,0);
jah128 9:085e090e1ec1 270 display.write_string(disp_line);
jah128 9:085e090e1ec1 271 }
jah128 9:085e090e1ec1 272
jah128 6:ff3c66f7372b 273 /// Verbose output
jah128 6:ff3c66f7372b 274 void out(const char* format, ...)
jah128 6:ff3c66f7372b 275 {
jah128 6:ff3c66f7372b 276 char buffer[256];
jah128 6:ff3c66f7372b 277 if (debug_mode) {
jah128 6:ff3c66f7372b 278 va_list vl;
jah128 6:ff3c66f7372b 279 va_start(vl, format);
jah128 6:ff3c66f7372b 280 vsprintf(buffer,format,vl);
jah128 6:ff3c66f7372b 281 if(user_code_debug == 1) pc.printf("%s", buffer);
jah128 6:ff3c66f7372b 282 va_end(vl);
jah128 0:8a5497a2e366 283 }
jah128 0:8a5497a2e366 284 }