ft. button press reset

Dependencies:   mbed

Fork of BeaconDemo_RobotCodeNew by James O'Keeffe

Committer:
jah128
Date:
Mon Oct 26 23:58:08 2015 +0000
Revision:
10:1b09d4bb847b
Child:
11:7b3ee540ba56
Reordered files; updated display

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jah128 10:1b09d4bb847b 1 /// PsiSwarm Beautiful Meme Project Source Code
jah128 10:1b09d4bb847b 2 /// Version 0.1
jah128 10:1b09d4bb847b 3 /// James Hilder, Alan Millard, Homero Elizondo, Jon Timmis
jah128 10:1b09d4bb847b 4 /// University of York
jah128 10:1b09d4bb847b 5
jah128 10:1b09d4bb847b 6 // Programs.cpp - Various PsiSwarm Programs for Beautiful Meme Project
jah128 10:1b09d4bb847b 7
jah128 10:1b09d4bb847b 8 #include "main.h"
jah128 10:1b09d4bb847b 9
jah128 10:1b09d4bb847b 10 float battery_low_threshold = 3.63; // Threshold at which to interrupt program and start recharging routine: suggest 3.55
jah128 10:1b09d4bb847b 11 float battery_high_threshold = 3.97; // Threshold at which to end battery recharging routine and resume normal program: suggest 4.0
jah128 10:1b09d4bb847b 12
jah128 10:1b09d4bb847b 13 char was_turning = 0;
jah128 10:1b09d4bb847b 14 enum random_walk_state {random_walk, turn_towards, interact, turn_away, avoid_obstacle};
jah128 10:1b09d4bb847b 15 enum random_walk_state internal_state = random_walk;
jah128 10:1b09d4bb847b 16 char action_timeout = 0;
jah128 10:1b09d4bb847b 17 char interaction_timeout = 0;
jah128 10:1b09d4bb847b 18 char random_walk_timeout = 0;
jah128 10:1b09d4bb847b 19 float previous_left_motor_speed = 0.5;
jah128 10:1b09d4bb847b 20 float previous_right_motor_speed = 0.5;
jah128 10:1b09d4bb847b 21 int obstacle_avoidance_threshold = 300;
jah128 10:1b09d4bb847b 22 char recharge_power_check_count = 0;
jah128 10:1b09d4bb847b 23 char battery_low_count = 0;
jah128 10:1b09d4bb847b 24
jah128 10:1b09d4bb847b 25 ///The head to bearing program moves towards a given bearing (eg 0 for the beacon or 180 for the opposite wall) and keeps going until an obstacle is detected in front of it
jah128 10:1b09d4bb847b 26 void head_to_bearing_program(int target_bearing)
jah128 10:1b09d4bb847b 27 {
jah128 10:1b09d4bb847b 28 if(step_cycle == 0 || was_turning == 0) {
jah128 10:1b09d4bb847b 29 // Check if we are heading in the right bearing (+- 25 degrees)
jah128 10:1b09d4bb847b 30 int current_bearing = (360 - beacon_heading) % 360;
jah128 10:1b09d4bb847b 31 // Current bearing should range from 0 to 359; target_bearing likewise; check the are within 25 degrees of each other
jah128 10:1b09d4bb847b 32 char bearing_ok = 0;
jah128 10:1b09d4bb847b 33 int lower_bound = target_bearing - 25;
jah128 10:1b09d4bb847b 34 int upper_bound = target_bearing + 25;
jah128 10:1b09d4bb847b 35 if(lower_bound < 0) {
jah128 10:1b09d4bb847b 36 if(current_bearing > (lower_bound + 360) || current_bearing < upper_bound) bearing_ok = 1;
jah128 10:1b09d4bb847b 37 } else if(upper_bound > 359) {
jah128 10:1b09d4bb847b 38 if(current_bearing > lower_bound || current_bearing < (upper_bound - 360)) bearing_ok = 1;
jah128 10:1b09d4bb847b 39 } else if(current_bearing > lower_bound && current_bearing < upper_bound) bearing_ok = 1;
jah128 10:1b09d4bb847b 40 // Check if there is an obstacle in front of us
jah128 10:1b09d4bb847b 41 if((reflected_sensor_data[7] > 1000 || reflected_sensor_data[0] > 1000) && bearing_ok == 1) target_reached = 1;
jah128 10:1b09d4bb847b 42 else {
jah128 10:1b09d4bb847b 43 // Now move forward if we are facing correct bearing, otherwise turn
jah128 10:1b09d4bb847b 44 if(bearing_ok == 1) {
jah128 10:1b09d4bb847b 45 //Check if anything is in front of us to determine speed - if it is, move slowly
jah128 10:1b09d4bb847b 46 int t_time = 6 * BEACON_PERIOD;
jah128 10:1b09d4bb847b 47 float t_speed = 1.0;
jah128 10:1b09d4bb847b 48 if(reflected_sensor_data[7] > 150 || reflected_sensor_data[0] > 150) {
jah128 10:1b09d4bb847b 49 t_time = 4 * BEACON_PERIOD;
jah128 10:1b09d4bb847b 50 t_speed = 0.6;
jah128 10:1b09d4bb847b 51 }
jah128 10:1b09d4bb847b 52 if(reflected_sensor_data[7] > 300 || reflected_sensor_data[0] > 300) {
jah128 10:1b09d4bb847b 53 t_time = 3 * BEACON_PERIOD;
jah128 10:1b09d4bb847b 54 t_speed = 0.4;
jah128 10:1b09d4bb847b 55 }
jah128 10:1b09d4bb847b 56 if(reflected_sensor_data[7] > 500 || reflected_sensor_data[0] > 500) {
jah128 10:1b09d4bb847b 57 t_time = 2 * BEACON_PERIOD;
jah128 10:1b09d4bb847b 58 t_speed = 0.2;
jah128 10:1b09d4bb847b 59 }
jah128 10:1b09d4bb847b 60 time_based_forward(t_speed,t_time,0);
jah128 10:1b09d4bb847b 61 was_turning = 0;
jah128 10:1b09d4bb847b 62 } else {
jah128 10:1b09d4bb847b 63 turn_to_bearing(target_bearing);
jah128 10:1b09d4bb847b 64 was_turning = 1;
jah128 10:1b09d4bb847b 65 }
jah128 10:1b09d4bb847b 66 }
jah128 10:1b09d4bb847b 67 }
jah128 10:1b09d4bb847b 68 }
jah128 10:1b09d4bb847b 69
jah128 10:1b09d4bb847b 70 /// Recharging program
jah128 10:1b09d4bb847b 71 void recharging_program()
jah128 10:1b09d4bb847b 72 {
jah128 10:1b09d4bb847b 73 switch(recharging_state) {
jah128 10:1b09d4bb847b 74 case 0:
jah128 10:1b09d4bb847b 75 // We are not currently recharging, check the battery state
jah128 10:1b09d4bb847b 76 if(get_battery_voltage() < battery_low_threshold) {
jah128 10:1b09d4bb847b 77 // Battery is below low threshold
jah128 10:1b09d4bb847b 78 battery_low_count ++;
jah128 10:1b09d4bb847b 79 // We don't immediately start recharging routine in case as battery level can fluctuate a little due to load; if we get a low value for 4 continuous timesteps we start recharging routine
jah128 10:1b09d4bb847b 80 if(battery_low_count > 3) {
jah128 10:1b09d4bb847b 81 // Set recharging state to 'looking for charger'
jah128 10:1b09d4bb847b 82 recharging_state = 1;
jah128 10:1b09d4bb847b 83 strcpy(prog_name,"CHARGING PROGRAM");
jah128 10:1b09d4bb847b 84 set_program_info("HEAD TO BEACON");
jah128 10:1b09d4bb847b 85 }
jah128 10:1b09d4bb847b 86 } else battery_low_count = 0;
jah128 10:1b09d4bb847b 87 break;
jah128 10:1b09d4bb847b 88 // State 1: Head to beacon [as this is where battery charger is]
jah128 10:1b09d4bb847b 89 case 1:
jah128 10:1b09d4bb847b 90 target_reached = 0;
jah128 10:1b09d4bb847b 91 head_to_bearing_program(0);
jah128 10:1b09d4bb847b 92 if(target_reached == 1) {
jah128 10:1b09d4bb847b 93 recharging_state = 2;
jah128 10:1b09d4bb847b 94 set_program_info("TURN 90 DEGREES");
jah128 10:1b09d4bb847b 95 }
jah128 10:1b09d4bb847b 96 break;
jah128 10:1b09d4bb847b 97 // Stage 2: Turn 90 degrees to align with charging pads
jah128 10:1b09d4bb847b 98 case 2:
jah128 10:1b09d4bb847b 99 disable_ir_emitters = 1;
jah128 10:1b09d4bb847b 100 time_based_turn_degrees(0.8, 70.0, 1);
jah128 10:1b09d4bb847b 101 recharge_power_check_count = 0;
jah128 10:1b09d4bb847b 102 recharging_state = 3;
jah128 10:1b09d4bb847b 103 break;
jah128 10:1b09d4bb847b 104 // Stage 3: Wait for turn to complete
jah128 10:1b09d4bb847b 105 case 3:
jah128 10:1b09d4bb847b 106 if (time_based_motor_action != 1) {
jah128 10:1b09d4bb847b 107 recharging_state = 4;
jah128 10:1b09d4bb847b 108 set_program_info("CHECK FOR POWER");
jah128 10:1b09d4bb847b 109 }
jah128 10:1b09d4bb847b 110 break;
jah128 10:1b09d4bb847b 111 // Stage 4: Check if charging
jah128 10:1b09d4bb847b 112 case 4:
jah128 10:1b09d4bb847b 113 recharge_power_check_count++;
jah128 10:1b09d4bb847b 114 if(get_dc_status() == 1) {
jah128 10:1b09d4bb847b 115 recharging_state = 5;
jah128 10:1b09d4bb847b 116 } else {
jah128 10:1b09d4bb847b 117 if(recharge_power_check_count < 10)recharging_state = 6;
jah128 10:1b09d4bb847b 118 else {
jah128 10:1b09d4bb847b 119 recharging_state = 7;
jah128 10:1b09d4bb847b 120 set_program_info("NO POWER - RETRY");
jah128 10:1b09d4bb847b 121 }
jah128 10:1b09d4bb847b 122 }
jah128 10:1b09d4bb847b 123 break;
jah128 10:1b09d4bb847b 124 // Stage 5: Charging. Wait until threshold voltage exceeded
jah128 10:1b09d4bb847b 125 case 5:
jah128 10:1b09d4bb847b 126 if(get_battery_voltage() > battery_high_threshold) {
jah128 10:1b09d4bb847b 127 set_program_info("LEAVE CHARGER");
jah128 10:1b09d4bb847b 128 recharging_state = 7;
jah128 10:1b09d4bb847b 129 } else {
jah128 10:1b09d4bb847b 130 char b_voltage[16];
jah128 10:1b09d4bb847b 131 sprintf(b_voltage,"CHARGE: %1.3fV",get_battery_voltage());
jah128 10:1b09d4bb847b 132 set_program_info(b_voltage);
jah128 10:1b09d4bb847b 133 }
jah128 10:1b09d4bb847b 134 break;
jah128 10:1b09d4bb847b 135 // Stage 6: We didn't find power, keep turning a couple of degrees at a time a recheck
jah128 10:1b09d4bb847b 136 case 6:
jah128 10:1b09d4bb847b 137 time_based_turn_degrees(0.5,4,1);
jah128 10:1b09d4bb847b 138 recharging_state = 4;
jah128 10:1b09d4bb847b 139 break;
jah128 10:1b09d4bb847b 140 // Stage 7: Charge may be finished. Turn 90 degrees then move away and resume previous program
jah128 10:1b09d4bb847b 141 case 7:
jah128 10:1b09d4bb847b 142 time_based_turn_degrees(0.8, 90.0, 1);
jah128 10:1b09d4bb847b 143 recharging_state = 8;
jah128 10:1b09d4bb847b 144 break;
jah128 10:1b09d4bb847b 145
jah128 10:1b09d4bb847b 146 // Stage 8: Wait for turn to complete
jah128 10:1b09d4bb847b 147 case 8:
jah128 10:1b09d4bb847b 148 if (time_based_motor_action != 1) recharging_state = 9;
jah128 10:1b09d4bb847b 149 break;
jah128 10:1b09d4bb847b 150 // Stage 9: Move away
jah128 10:1b09d4bb847b 151 case 9:
jah128 10:1b09d4bb847b 152 time_based_forward(0.5, 1000000, 1);
jah128 10:1b09d4bb847b 153 recharging_state = 10;
jah128 10:1b09d4bb847b 154 break;
jah128 10:1b09d4bb847b 155 // Stage 10: Wait for move to complete
jah128 10:1b09d4bb847b 156 case 10:
jah128 10:1b09d4bb847b 157 if (time_based_motor_action != 1) recharging_state = 11;
jah128 10:1b09d4bb847b 158 break;
jah128 10:1b09d4bb847b 159 // Stage 11: Check if battery is below low threshold; if it is, start over, else end charge cycle
jah128 10:1b09d4bb847b 160 case 11:
jah128 10:1b09d4bb847b 161 disable_ir_emitters = 0;
jah128 10:1b09d4bb847b 162 if (get_battery_voltage() < battery_low_threshold) {
jah128 10:1b09d4bb847b 163 recharging_state = 1;
jah128 10:1b09d4bb847b 164 } else {
jah128 10:1b09d4bb847b 165 recharging_state = 0;
jah128 10:1b09d4bb847b 166 //Restore name of old program on display
jah128 10:1b09d4bb847b 167 set_program(main_program_state);
jah128 10:1b09d4bb847b 168 }
jah128 10:1b09d4bb847b 169 break;
jah128 10:1b09d4bb847b 170 }
jah128 10:1b09d4bb847b 171 }
jah128 10:1b09d4bb847b 172
jah128 10:1b09d4bb847b 173 ///Alan's Random Walk\Obstacle Avoid and Robot Interaction Program
jah128 10:1b09d4bb847b 174 void curved_random_walk_with_interaction_program()
jah128 10:1b09d4bb847b 175 {
jah128 10:1b09d4bb847b 176 if(internal_state == random_walk) {
jah128 10:1b09d4bb847b 177 if(interaction_timeout < 4)
jah128 10:1b09d4bb847b 178 interaction_timeout++;
jah128 10:1b09d4bb847b 179
jah128 10:1b09d4bb847b 180 int closest_robot = -1;
jah128 10:1b09d4bb847b 181 unsigned short shortest_distance = 0;
jah128 10:1b09d4bb847b 182
jah128 10:1b09d4bb847b 183 // Check whether there are any other robots within range
jah128 10:1b09d4bb847b 184 for(int i = 0; i < 8; i++) {
jah128 10:1b09d4bb847b 185 if(robots_found[i]) {
jah128 10:1b09d4bb847b 186 if(robots_distance[i] > shortest_distance) {
jah128 10:1b09d4bb847b 187 shortest_distance = robots_distance[i];
jah128 10:1b09d4bb847b 188 closest_robot = i;
jah128 10:1b09d4bb847b 189 }
jah128 10:1b09d4bb847b 190 }
jah128 10:1b09d4bb847b 191 }
jah128 10:1b09d4bb847b 192
jah128 10:1b09d4bb847b 193 // Turn towards the closest robot
jah128 10:1b09d4bb847b 194 if(closest_robot >= 0 && shortest_distance > 300 && interaction_timeout >= 4) {
jah128 10:1b09d4bb847b 195 time_based_turn_degrees(1, robots_heading[closest_robot], 1);
jah128 10:1b09d4bb847b 196
jah128 10:1b09d4bb847b 197 action_timeout = 0;
jah128 10:1b09d4bb847b 198 internal_state = turn_towards;
jah128 10:1b09d4bb847b 199 char temp_message[17];
jah128 10:1b09d4bb847b 200 sprintf(temp_message,"FACE ROBOT %d",closest_robot);
jah128 10:1b09d4bb847b 201 set_program_info(temp_message);
jah128 10:1b09d4bb847b 202 } else { // Otherwise, do a random walk
jah128 10:1b09d4bb847b 203 // Check the front sensors for obstacles
jah128 10:1b09d4bb847b 204 if(reflected_sensor_data[0] > obstacle_avoidance_threshold ||
jah128 10:1b09d4bb847b 205 reflected_sensor_data[1] > obstacle_avoidance_threshold ||
jah128 10:1b09d4bb847b 206 reflected_sensor_data[6] > obstacle_avoidance_threshold ||
jah128 10:1b09d4bb847b 207 reflected_sensor_data[7] > obstacle_avoidance_threshold) {
jah128 10:1b09d4bb847b 208 // Ignore the rear sensors when calculating the heading
jah128 10:1b09d4bb847b 209 reflected_sensor_data[2] = 0;
jah128 10:1b09d4bb847b 210 reflected_sensor_data[3] = 0;
jah128 10:1b09d4bb847b 211 reflected_sensor_data[4] = 0;
jah128 10:1b09d4bb847b 212 reflected_sensor_data[5] = 0;
jah128 10:1b09d4bb847b 213
jah128 10:1b09d4bb847b 214 // Turn 180 degrees away from the sensed obstacle
jah128 10:1b09d4bb847b 215 int heading = get_bearing_from_ir_array (reflected_sensor_data) + 180;
jah128 10:1b09d4bb847b 216
jah128 10:1b09d4bb847b 217 // Normalise
jah128 10:1b09d4bb847b 218 heading %= 360;
jah128 10:1b09d4bb847b 219
jah128 10:1b09d4bb847b 220 if(heading < -180)
jah128 10:1b09d4bb847b 221 heading += 360;
jah128 10:1b09d4bb847b 222
jah128 10:1b09d4bb847b 223 if(heading > 180)
jah128 10:1b09d4bb847b 224 heading -= 360;
jah128 10:1b09d4bb847b 225 set_program_info("AVOID OBSTACLE");
jah128 10:1b09d4bb847b 226 time_based_turn_degrees(1, heading, 1);
jah128 10:1b09d4bb847b 227
jah128 10:1b09d4bb847b 228 action_timeout = 0;
jah128 10:1b09d4bb847b 229 internal_state = turn_away;
jah128 10:1b09d4bb847b 230 } else {
jah128 10:1b09d4bb847b 231 // Change motor speeds every 1s
jah128 10:1b09d4bb847b 232 if(random_walk_timeout >= 2) {
jah128 10:1b09d4bb847b 233 float random_offset = (((float) rand() / (float) RAND_MAX) - 0.5) * 0.5;
jah128 10:1b09d4bb847b 234
jah128 10:1b09d4bb847b 235 float left_motor_speed = previous_left_motor_speed - random_offset;
jah128 10:1b09d4bb847b 236 float right_motor_speed = previous_right_motor_speed + random_offset;
jah128 10:1b09d4bb847b 237
jah128 10:1b09d4bb847b 238 float threshold = 0.25;
jah128 10:1b09d4bb847b 239
jah128 10:1b09d4bb847b 240 if(left_motor_speed < threshold)
jah128 10:1b09d4bb847b 241 left_motor_speed = threshold;
jah128 10:1b09d4bb847b 242
jah128 10:1b09d4bb847b 243 if(right_motor_speed < threshold)
jah128 10:1b09d4bb847b 244 right_motor_speed = threshold;
jah128 10:1b09d4bb847b 245
jah128 10:1b09d4bb847b 246 set_left_motor_speed(left_motor_speed);
jah128 10:1b09d4bb847b 247 set_right_motor_speed(right_motor_speed);
jah128 10:1b09d4bb847b 248
jah128 10:1b09d4bb847b 249 random_walk_timeout = 0;
jah128 10:1b09d4bb847b 250 }
jah128 10:1b09d4bb847b 251
jah128 10:1b09d4bb847b 252 random_walk_timeout++;
jah128 10:1b09d4bb847b 253 }
jah128 10:1b09d4bb847b 254 }
jah128 10:1b09d4bb847b 255 } else if(internal_state == turn_towards) {
jah128 10:1b09d4bb847b 256 if(action_timeout < 4)
jah128 10:1b09d4bb847b 257 action_timeout++;
jah128 10:1b09d4bb847b 258 else {
jah128 10:1b09d4bb847b 259 set_program_info("SAY HELLO");
jah128 10:1b09d4bb847b 260 vibrate();
jah128 10:1b09d4bb847b 261
jah128 10:1b09d4bb847b 262 action_timeout = 0;
jah128 10:1b09d4bb847b 263 internal_state = interact;
jah128 10:1b09d4bb847b 264 }
jah128 10:1b09d4bb847b 265 } else if(internal_state == interact) {
jah128 10:1b09d4bb847b 266 if(action_timeout < 4)
jah128 10:1b09d4bb847b 267 action_timeout++;
jah128 10:1b09d4bb847b 268 else {
jah128 10:1b09d4bb847b 269 set_program_info("TURN AROUND");
jah128 10:1b09d4bb847b 270 time_based_turn_degrees(1, 180, 1);
jah128 10:1b09d4bb847b 271
jah128 10:1b09d4bb847b 272 action_timeout = 0;
jah128 10:1b09d4bb847b 273 internal_state = turn_away;
jah128 10:1b09d4bb847b 274 }
jah128 10:1b09d4bb847b 275
jah128 10:1b09d4bb847b 276 } else if(internal_state == turn_away) {
jah128 10:1b09d4bb847b 277 if(action_timeout < 4)
jah128 10:1b09d4bb847b 278 action_timeout++;
jah128 10:1b09d4bb847b 279 else {
jah128 10:1b09d4bb847b 280 set_program_info("RANDOM WALK");
jah128 10:1b09d4bb847b 281 interaction_timeout = 0;
jah128 10:1b09d4bb847b 282 internal_state = random_walk;
jah128 10:1b09d4bb847b 283 }
jah128 10:1b09d4bb847b 284 } else if(internal_state == avoid_obstacle) {
jah128 10:1b09d4bb847b 285 if(action_timeout < 4)
jah128 10:1b09d4bb847b 286 action_timeout++;
jah128 10:1b09d4bb847b 287 else
jah128 10:1b09d4bb847b 288 set_program_info("RANDOM WALK");
jah128 10:1b09d4bb847b 289 internal_state = random_walk;
jah128 10:1b09d4bb847b 290 }
jah128 10:1b09d4bb847b 291 }
jah128 10:1b09d4bb847b 292
jah128 10:1b09d4bb847b 293
jah128 10:1b09d4bb847b 294 void straight_random_walk_with_interaction_program()
jah128 10:1b09d4bb847b 295 {
jah128 10:1b09d4bb847b 296
jah128 10:1b09d4bb847b 297 }
jah128 10:1b09d4bb847b 298
jah128 10:1b09d4bb847b 299
jah128 10:1b09d4bb847b 300 void find_space_program()
jah128 10:1b09d4bb847b 301 {
jah128 10:1b09d4bb847b 302
jah128 10:1b09d4bb847b 303 }