Homero Silva / Mbed 2 deprecated PRGP_Pi_Swarm_ground_search_algorithm

Dependencies:   mbed

Fork of Pi_Swarm_Blank by James Hilder

Committer:
re633
Date:
Wed Jul 29 13:58:57 2015 +0000
Revision:
11:c5094a68283f
Parent:
10:da62735d6df9
Child:
12:118f2b0ed8eb
Changed obstacle avoidance whilst moving toward beacon

Who changed what in which revision?

UserRevisionLine numberNew contents of line
re633 11:c5094a68283f 1 /*
re633 11:c5094a68283f 2 * Software License Agreement (BSD License)
re633 11:c5094a68283f 3 *
re633 11:c5094a68283f 4 * Copyright (c) 2015, University of York Robotics Laboratory (YRL).
re633 11:c5094a68283f 5 * All rights reserved.
re633 11:c5094a68283f 6 *
re633 11:c5094a68283f 7 * Redistribution and use in source and binary forms, with or without
re633 11:c5094a68283f 8 * modification, are permitted provided that the following conditions
re633 11:c5094a68283f 9 * are met:
re633 11:c5094a68283f 10 *
re633 11:c5094a68283f 11 * * Redistributions of source code must retain the above copyright
re633 11:c5094a68283f 12 * notice, this list of conditions and the following disclaimer.
re633 11:c5094a68283f 13 * * Redistributions in binary form must reproduce the above
re633 11:c5094a68283f 14 * copyright notice, this list of conditions and the following
re633 11:c5094a68283f 15 * disclaimer in the documentation and/or other materials provided
re633 11:c5094a68283f 16 * with the distribution.
re633 11:c5094a68283f 17 * * Neither the name of Willow Garage, Inc. nor the names of its
re633 11:c5094a68283f 18 * contributors may be used to endorse or promote products derived
re633 11:c5094a68283f 19 * from this software without specific prior written permission.
re633 11:c5094a68283f 20 *
re633 11:c5094a68283f 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
re633 11:c5094a68283f 22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
re633 11:c5094a68283f 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
re633 11:c5094a68283f 24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
re633 11:c5094a68283f 25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
re633 11:c5094a68283f 26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
re633 11:c5094a68283f 27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
re633 11:c5094a68283f 28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
re633 11:c5094a68283f 29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
re633 11:c5094a68283f 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
re633 11:c5094a68283f 31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
re633 11:c5094a68283f 32 * POSSIBILITY OF SUCH DAMAGE.
re633 11:c5094a68283f 33 */
re633 11:c5094a68283f 34 /**
re633 11:c5094a68283f 35 * @file main.cpp
re633 11:c5094a68283f 36 * @brief The Ticker fucntion for gaining sensor data and the main function for the PRGP Pi-Swarm Controllers.
re633 11:c5094a68283f 37 * @details In this file the main function for the Pi-Swarm Controller is defined.
re633 11:c5094a68283f 38 * @version 1.0
re633 11:c5094a68283f 39 * @author Robert
re633 11:c5094a68283f 40 * @author Evans
re633 11:c5094a68283f 41 * @date 24/07/15
re633 11:c5094a68283f 42 */
jah128 3:1aa1de26966a 43 #include "main.h" // Certain parameters can be set by changing the defines in piswarm.h
re633 9:ef0907fda2f1 44 #include "PiSwarmControllerFunctions.h"
re633 9:ef0907fda2f1 45
jah128 0:46cd1498a39a 46
jah128 1:37502eb3b70f 47 PiSwarm piswarm;
jah128 1:37502eb3b70f 48 Serial pc (USBTX,USBRX);
jah128 0:46cd1498a39a 49
re633 9:ef0907fda2f1 50 //Tickers
re633 9:ef0907fda2f1 51 Ticker ticker_25ms;
re633 9:ef0907fda2f1 52 Timer timer;
re633 10:da62735d6df9 53 Timer timerLevy;
re633 9:ef0907fda2f1 54
re633 9:ef0907fda2f1 55 //Global Variables
re633 9:ef0907fda2f1 56 uint8_t const IR_READ_PER_BEAC = 20; //The number of IR readings between beacon flashes
re633 9:ef0907fda2f1 57 uint16_t volatile gv_IRVals[IR_READ_PER_BEAC][8] = {0}; //The passive IR values each are stored in this array every 25ms for the last 0.5 seconds
re633 9:ef0907fda2f1 58 int16_t volatile gv_IRValDiffs[IR_READ_PER_BEAC][8] = {0};
re633 9:ef0907fda2f1 59 uint8_t volatile beacon_detected[8] = {0};
re633 9:ef0907fda2f1 60 int8_t volatile gv_counter25ms = 0; //This counter is increased every 25ms and resets to zero after one second. (It is signed because 1 is subtracted from it in ReadIRs)
re633 9:ef0907fda2f1 61 uint8_t const BEACON_SUSPECTED = 50; //Value by which consecutive IR sensor readings need to jump by for in order to cause beacon to be suspected.
re633 9:ef0907fda2f1 62 uint16_t const AT_BEACON_THRESHOLD = 3700;
re633 9:ef0907fda2f1 63 uint8_t volatile gv_state = 0; //This is the current state of the finite state machine
re633 11:c5094a68283f 64 uint32_t levy_target_time_us = 0; //The amount of time in micro seconds by which the robot needs to move in order to reach the distance required in next section of the levy walk.
re633 9:ef0907fda2f1 65 uint8_t volatile gv_IRDistances[8]; //Using the custom distance function the active ir readings are converted to distances and stored here every 25ms.
re633 9:ef0907fda2f1 66 int16_t g_currentHeading = 0;
re633 11:c5094a68283f 67 float BASE_SPEED = 0.4;
re633 9:ef0907fda2f1 68 int8_t g_beaconOn = 100;
re633 11:c5094a68283f 69 int8_t volatile tick_beacon_suspected = 100; //Is set to the tick value within the period between beacon flashes that the beacon flash is suspected to begin at
re633 11:c5094a68283f 70 int8_t tick_beacon_period_check = 100; //Is used to temporarily store the value of tick_beacon_suspected
re633 11:c5094a68283f 71 uint16_t const TURN_BACK_TIME = (800/BASE_SPEED); //Time between trying to continiue on path it wason before obstacle given by: (Distance in mm) / (robots speed)
re633 9:ef0907fda2f1 72 //Flags
re633 10:da62735d6df9 73 int8_t volatile flagSystemState = 0;
re633 9:ef0907fda2f1 74 int8_t flagObstacle = 0;
re633 9:ef0907fda2f1 75 int8_t flagStationary = 1; //Used to determine if robot is currentl stationary or moving.
re633 9:ef0907fda2f1 76 uint8_t flagBeaconSyncronised = 0; //Set to one when the robot is synchronised with the beacon
re633 9:ef0907fda2f1 77 uint8_t volatile flagBeaconIlluminated = 0; //Should be 1 when beacon is illuminated otherwise 0
re633 10:da62735d6df9 78 uint8_t flagSetNewLevyTime = 1; //Must start as 1
re633 9:ef0907fda2f1 79 //Ticker Function*************************************************************************************
re633 9:ef0907fda2f1 80 //This function is called by a ticker every 25ms
re633 9:ef0907fda2f1 81 //The function firstly stores the background IRS values.
re633 9:ef0907fda2f1 82 //Secondly if beacon is off it uses illuminated IR sensors to estimate distances
re633 9:ef0907fda2f1 83 //Each second it will update when it believes beacon illumination time to be
re633 9:ef0907fda2f1 84 //It will work out which sensors spotted then beacon and will illuminate the Leds accordingly
re633 9:ef0907fda2f1 85 void readIRs(){
re633 9:ef0907fda2f1 86
re633 9:ef0907fda2f1 87 //Fistly update the beaconVisable flag if possible
re633 9:ef0907fda2f1 88 if(gv_counter25ms == mod8((g_beaconOn - 1), IR_READ_PER_BEAC)){
re633 9:ef0907fda2f1 89 flagBeaconIlluminated = 1;
re633 9:ef0907fda2f1 90 }
re633 9:ef0907fda2f1 91
re633 9:ef0907fda2f1 92 if(gv_counter25ms == mod8((g_beaconOn + 2), IR_READ_PER_BEAC)){
re633 9:ef0907fda2f1 93 flagBeaconIlluminated = 0;
re633 9:ef0907fda2f1 94 }
re633 9:ef0907fda2f1 95 //Firstly store background values
re633 9:ef0907fda2f1 96 //Also make a note of which point in the second did the values change most.
re633 9:ef0907fda2f1 97 //For which sensor specifically did the values change the most
re633 9:ef0907fda2f1 98 //That sensor will be used to estimate the beacon start time if a threshold value is met
re633 9:ef0907fda2f1 99 piswarm.store_background_raw_ir_values();
re633 9:ef0907fda2f1 100 int16_t IRchange = 0;
re633 9:ef0907fda2f1 101 uint8_t loopCounter = 0;
re633 9:ef0907fda2f1 102 //In this loop the raw IR values are read.
re633 9:ef0907fda2f1 103 //If the points where the IR values have increased by the greatest amount are noted as this indicates a beacon illumination
re633 9:ef0907fda2f1 104 for(loopCounter = 0; loopCounter < 8; loopCounter++) {
re633 9:ef0907fda2f1 105 gv_IRVals[gv_counter25ms][loopCounter] = piswarm.get_background_raw_ir_value(loopCounter);
re633 9:ef0907fda2f1 106 IRchange = gv_IRVals[gv_counter25ms][loopCounter]-gv_IRVals[mod8((gv_counter25ms-1),IR_READ_PER_BEAC)][loopCounter];
re633 9:ef0907fda2f1 107
re633 9:ef0907fda2f1 108 gv_IRValDiffs[gv_counter25ms][loopCounter] = IRchange;
re633 9:ef0907fda2f1 109 //printf("change %d count %d\n",IRchange);
re633 9:ef0907fda2f1 110 //If difference is greater than a threshold value then the beacon is suspected. This will be confirmed depending on the robots state of movement.
re633 9:ef0907fda2f1 111 if (IRchange > BEACON_SUSPECTED){
re633 11:c5094a68283f 112 tick_beacon_suspected = gv_counter25ms;
re633 9:ef0907fda2f1 113 piswarm.cls();
re633 11:c5094a68283f 114 piswarm.printf("%d",tick_beacon_suspected);
re633 9:ef0907fda2f1 115 }
re633 9:ef0907fda2f1 116 }
re633 9:ef0907fda2f1 117
re633 9:ef0907fda2f1 118 //Now store the illuminated values if the beacon is not illuminated-
re633 9:ef0907fda2f1 119 piswarm.store_illuminated_raw_ir_values();
re633 9:ef0907fda2f1 120
re633 9:ef0907fda2f1 121 //In this loop convert each raw active IR reading into a distance estimate
re633 9:ef0907fda2f1 122 for(loopCounter = 0; loopCounter < 8; loopCounter++) {
re633 9:ef0907fda2f1 123
re633 9:ef0907fda2f1 124 //Specific sensor readings converted to distances
re633 9:ef0907fda2f1 125 float temp = piswarm.get_illuminated_raw_ir_value(loopCounter);
re633 9:ef0907fda2f1 126 if(temp>3500){
re633 9:ef0907fda2f1 127 temp = 3500;
re633 9:ef0907fda2f1 128 } else if (temp < 97){
re633 9:ef0907fda2f1 129 temp = 97;
re633 9:ef0907fda2f1 130 }
re633 9:ef0907fda2f1 131 //#put this into a function
re633 9:ef0907fda2f1 132 //Switch case for robot 5
re633 9:ef0907fda2f1 133 switch(loopCounter){
re633 9:ef0907fda2f1 134 case 0:
re633 9:ef0907fda2f1 135 temp = 662 * sqrt(1/(temp-148));
re633 9:ef0907fda2f1 136 break;
re633 9:ef0907fda2f1 137 case 1:
re633 9:ef0907fda2f1 138 temp = 662 * sqrt(1/(temp-144));
re633 9:ef0907fda2f1 139 break;
re633 9:ef0907fda2f1 140 case 2:
re633 9:ef0907fda2f1 141 temp = 662 * sqrt(1/(temp-120));
re633 9:ef0907fda2f1 142 break;
re633 9:ef0907fda2f1 143 case 3:
re633 9:ef0907fda2f1 144 temp = 662 * sqrt(1/(temp-148));
re633 9:ef0907fda2f1 145 break;
re633 9:ef0907fda2f1 146 case 4:
re633 9:ef0907fda2f1 147 temp = 662 * sqrt(1/(temp-120));
re633 9:ef0907fda2f1 148 break;
re633 9:ef0907fda2f1 149 case 5:
re633 9:ef0907fda2f1 150 temp = 662 * sqrt(1/(temp-132));
re633 9:ef0907fda2f1 151 break;
re633 9:ef0907fda2f1 152 case 6:
re633 9:ef0907fda2f1 153 temp = 662 * sqrt(1/(temp-152));
re633 9:ef0907fda2f1 154 break;
re633 9:ef0907fda2f1 155 case 7:
re633 9:ef0907fda2f1 156 temp = 662 * sqrt(1/(temp-212));
re633 9:ef0907fda2f1 157 break;
re633 9:ef0907fda2f1 158 }
re633 9:ef0907fda2f1 159
re633 9:ef0907fda2f1 160 if (temp > 130){
re633 9:ef0907fda2f1 161 temp = 130;
re633 9:ef0907fda2f1 162 }
re633 9:ef0907fda2f1 163 gv_IRDistances[loopCounter] = temp;
re633 9:ef0907fda2f1 164
re633 9:ef0907fda2f1 165 }
re633 9:ef0907fda2f1 166 //reset counter after 1 second (beacon period)
re633 9:ef0907fda2f1 167 gv_counter25ms = mod8(gv_counter25ms + 1,IR_READ_PER_BEAC);
re633 9:ef0907fda2f1 168 }
re633 9:ef0907fda2f1 169
re633 9:ef0907fda2f1 170 //*******************************************************************************************************
jah128 8:a789ef4fde52 171 //This is where the program code goes.
jah128 1:37502eb3b70f 172 int main() {
re633 9:ef0907fda2f1 173 init();
re633 9:ef0907fda2f1 174 ticker_25ms.attach_us(&readIRs,25000);
re633 9:ef0907fda2f1 175 //starting point in state 11
re633 9:ef0907fda2f1 176 timer.start();
re633 10:da62735d6df9 177 timerLevy.start();
re633 9:ef0907fda2f1 178 wait(1); //Wait a second to allow IR array to be filled
re633 9:ef0907fda2f1 179
re633 10:da62735d6df9 180 //changeState(11);
jah128 8:a789ef4fde52 181
re633 9:ef0907fda2f1 182 //Controller is a finite state machine
re633 9:ef0907fda2f1 183 while(1){
re633 9:ef0907fda2f1 184
re633 9:ef0907fda2f1 185 //Waiting for signal to begin searching
re633 9:ef0907fda2f1 186 if(gv_state == 0){
re633 10:da62735d6df9 187 if(flagSystemState == 1){
re633 10:da62735d6df9 188 changeState(11);
re633 10:da62735d6df9 189 }
re633 9:ef0907fda2f1 190 wait(1);
re633 9:ef0907fda2f1 191
re633 9:ef0907fda2f1 192 //Change state here after recieving a radio command
re633 9:ef0907fda2f1 193 //Searching state
re633 9:ef0907fda2f1 194 } else if (gv_state == 11 || gv_state == 12){
re633 9:ef0907fda2f1 195
re633 9:ef0907fda2f1 196 //Firstly determine if suspected beacon is actually the beacon.
re633 9:ef0907fda2f1 197 //This is done by checking the period between flashes matches the beacon period
re633 11:c5094a68283f 198 if(tick_beacon_suspected != 100){
re633 9:ef0907fda2f1 199 //piswarm.stop();
re633 9:ef0907fda2f1 200 //When the beacon flag is first raised store its value and reset it
re633 11:c5094a68283f 201 if(tick_beacon_period_check == 100){
re633 11:c5094a68283f 202 tick_beacon_period_check = tick_beacon_suspected;
re633 11:c5094a68283f 203 tick_beacon_suspected = 100;
re633 9:ef0907fda2f1 204 //Check the timing of the latest jump with the last one to see if period matches the Beacon.
re633 9:ef0907fda2f1 205 } else {
re633 9:ef0907fda2f1 206 piswarm.locate(0,1);
re633 11:c5094a68283f 207 piswarm.printf("%d %d",tick_beacon_period_check,tick_beacon_suspected);
re633 11:c5094a68283f 208 //printf("%d %d *********************************",tick_beacon_period_check,tick_beacon_suspected);
re633 9:ef0907fda2f1 209 //If the two numbers are similar then test will be low. For this to work the period of the ticker and beacon should be the same.
re633 11:c5094a68283f 210 int8_t test = (tick_beacon_period_check - tick_beacon_suspected);
re633 9:ef0907fda2f1 211
re633 9:ef0907fda2f1 212 test = test * test;
re633 9:ef0907fda2f1 213
re633 9:ef0907fda2f1 214 //if test is low then identify the beacon as the cause of the flags
re633 9:ef0907fda2f1 215 if(test < 2){
re633 9:ef0907fda2f1 216 //Beacon found change to state 2
re633 11:c5094a68283f 217 g_beaconOn = tick_beacon_period_check; //update the global variable that stores when beacon flashes occur
re633 9:ef0907fda2f1 218
re633 9:ef0907fda2f1 219 wait(2);
re633 9:ef0907fda2f1 220 changeState(2);
re633 9:ef0907fda2f1 221 } else {
re633 9:ef0907fda2f1 222 //Reset the flag to try again
re633 11:c5094a68283f 223 tick_beacon_period_check = 100;
re633 9:ef0907fda2f1 224 }
re633 9:ef0907fda2f1 225 }
re633 9:ef0907fda2f1 226 }
re633 9:ef0907fda2f1 227
re633 9:ef0907fda2f1 228 if(gv_state == 11){
re633 9:ef0907fda2f1 229 //Secondly if obstacles are detected ahead then execute a random turn.
re633 9:ef0907fda2f1 230 if(gv_IRDistances[0] < 100 || gv_IRDistances[1] < 100){
re633 9:ef0907fda2f1 231 piswarm.stop();
re633 10:da62735d6df9 232 piswarm.cls();
re633 10:da62735d6df9 233 piswarm.printf("ob R");
re633 10:da62735d6df9 234 piswarm.play_tune("CC",1);
re633 9:ef0907fda2f1 235 wait(0.1);
re633 9:ef0907fda2f1 236 flagObstacle = 1;
re633 9:ef0907fda2f1 237 changeState(12);
re633 9:ef0907fda2f1 238 } else if(gv_IRDistances[6] < 100 || gv_IRDistances[7] < 100){
re633 9:ef0907fda2f1 239 piswarm.stop();
re633 10:da62735d6df9 240 piswarm.cls();
re633 10:da62735d6df9 241 piswarm.printf("ob L");
re633 10:da62735d6df9 242 piswarm.play_tune("CC",1);
re633 9:ef0907fda2f1 243 wait(0.1);
re633 9:ef0907fda2f1 244 flagObstacle = 2;
re633 9:ef0907fda2f1 245 changeState(12);
re633 10:da62735d6df9 246
re633 10:da62735d6df9 247 //Otherwise continue moving forward until distance determined by levy algorithm is calculated.
re633 11:c5094a68283f 248 } else if(timerLevy.read_us() > levy_target_time_us){
re633 10:da62735d6df9 249 flagSetNewLevyTime = 1;
re633 10:da62735d6df9 250 piswarm.play_tune("G",1);
re633 10:da62735d6df9 251 changeState(12);
re633 9:ef0907fda2f1 252
re633 9:ef0907fda2f1 253 } else if (flagStationary == 1){
re633 9:ef0907fda2f1 254
re633 11:c5094a68283f 255 piswarm.forward(BASE_SPEED);
re633 9:ef0907fda2f1 256 flagStationary = 0;
re633 9:ef0907fda2f1 257 }
re633 9:ef0907fda2f1 258
re633 9:ef0907fda2f1 259 } else if(gv_state == 12){
re633 9:ef0907fda2f1 260 piswarm.stop();//Stop the robot.
re633 9:ef0907fda2f1 261 flagStationary = 1; //update this flag
re633 9:ef0907fda2f1 262 int16_t randomAngle;
re633 9:ef0907fda2f1 263 //If sent here beacuse of obstacle find angle between -180 to -90 and 90 to 180
re633 9:ef0907fda2f1 264 if(flagObstacle == 1){
re633 10:da62735d6df9 265 randomAngle = rand()%90 - 135;
re633 9:ef0907fda2f1 266
re633 9:ef0907fda2f1 267 } else if(flagObstacle == 2){
re633 10:da62735d6df9 268 randomAngle = rand()%90 + 45;
re633 10:da62735d6df9 269
re633 10:da62735d6df9 270 //Otherwise if here due to levy walk: turn to any random angle
re633 9:ef0907fda2f1 271 } else {
re633 10:da62735d6df9 272 randomAngle = rand()%360 - 180;
re633 9:ef0907fda2f1 273 }
re633 9:ef0907fda2f1 274 turnDegrees(randomAngle); //Make the turn
re633 10:da62735d6df9 275 wait(0.1);
re633 10:da62735d6df9 276 /* if(gv_IRDistances[0] < 70 || gv_IRDistances[1] < 70 || gv_IRDistances[6] < 70 || gv_IRDistances[7] < 70){
re633 10:da62735d6df9 277 //do nothing. Aim is that robot will keep turning the same way until clear of obstacle
re633 10:da62735d6df9 278 //could put a count in here to doemergency escape manouvre if necessary
re633 10:da62735d6df9 279 piswarm.play_tune("CC",1);
re633 10:da62735d6df9 280 } else { */
re633 10:da62735d6df9 281 flagObstacle = 0;
re633 10:da62735d6df9 282 changeState(11);//Move back into state 11
re633 10:da62735d6df9 283
re633 9:ef0907fda2f1 284 }
re633 9:ef0907fda2f1 285
re633 9:ef0907fda2f1 286
re633 9:ef0907fda2f1 287 //Beacon found state
re633 9:ef0907fda2f1 288 } else if (gv_state == 2){
re633 9:ef0907fda2f1 289 int16_t maxValue[2] = {0,100}; //Value and sensor position
re633 9:ef0907fda2f1 290 //wait(1);
re633 9:ef0907fda2f1 291 uint8_t loopCounter = 0;
re633 9:ef0907fda2f1 292
re633 9:ef0907fda2f1 293 //If beacon visible
re633 9:ef0907fda2f1 294 if(flagBeaconSyncronised == 1){
re633 9:ef0907fda2f1 295
re633 9:ef0907fda2f1 296 //Firstly check beacon is still visible
re633 9:ef0907fda2f1 297 flagBeaconSyncronised = 0;
re633 9:ef0907fda2f1 298 //Update array concerning which IRs can see the beacon
re633 9:ef0907fda2f1 299 for(loopCounter = 0; loopCounter<8; loopCounter++) {
re633 9:ef0907fda2f1 300
re633 9:ef0907fda2f1 301 //Find which sensor has the highest reading
re633 9:ef0907fda2f1 302 if( gv_IRValDiffs[g_beaconOn][loopCounter] > BEACON_SUSPECTED) {
re633 9:ef0907fda2f1 303 if(gv_IRVals[g_beaconOn][loopCounter] > maxValue[0]){
re633 9:ef0907fda2f1 304 maxValue[0] = gv_IRVals[g_beaconOn][loopCounter];
re633 9:ef0907fda2f1 305 maxValue[1] = loopCounter;
re633 9:ef0907fda2f1 306 }
re633 9:ef0907fda2f1 307 flagBeaconSyncronised = 1; //This will remain as one so long as at least on sensor can see beacon
re633 9:ef0907fda2f1 308 }
re633 9:ef0907fda2f1 309 }
re633 9:ef0907fda2f1 310
re633 9:ef0907fda2f1 311 //Only do this if beacon still visible
re633 9:ef0907fda2f1 312 if(flagBeaconSyncronised == 1){
re633 9:ef0907fda2f1 313
re633 9:ef0907fda2f1 314 //If the adjacent two sensors are above the threshold too then they can also be marked as illuminated
re633 9:ef0907fda2f1 315 for(loopCounter = 0; loopCounter<8; loopCounter++){
re633 9:ef0907fda2f1 316
re633 9:ef0907fda2f1 317 //reset all beacon detected values
re633 9:ef0907fda2f1 318 beacon_detected[loopCounter] = 0;
re633 9:ef0907fda2f1 319
re633 9:ef0907fda2f1 320 if(abs(maxValue[1] - loopCounter)< 3 || abs(maxValue[1] + 8 - loopCounter)< 3 || abs(maxValue[1] - 8 - loopCounter)< 3) {
re633 9:ef0907fda2f1 321 if(gv_IRValDiffs[g_beaconOn][loopCounter] > BEACON_SUSPECTED){
re633 9:ef0907fda2f1 322 beacon_detected[loopCounter] = 1;
re633 9:ef0907fda2f1 323 }
re633 9:ef0907fda2f1 324 }
re633 9:ef0907fda2f1 325 }
re633 9:ef0907fda2f1 326
re633 9:ef0907fda2f1 327
re633 9:ef0907fda2f1 328 //Update the piswarm LEDS so the ones that can see the beacon are on.
re633 9:ef0907fda2f1 329 piswarm.set_oleds(beacon_detected[0]||beacon_detected[1],
re633 9:ef0907fda2f1 330 beacon_detected[1]||beacon_detected[2],
re633 9:ef0907fda2f1 331 beacon_detected[2],
re633 9:ef0907fda2f1 332 beacon_detected[3],
re633 9:ef0907fda2f1 333 0,
re633 9:ef0907fda2f1 334 beacon_detected[4],
re633 9:ef0907fda2f1 335 beacon_detected[5],
re633 9:ef0907fda2f1 336 beacon_detected[5]||beacon_detected[6],
re633 9:ef0907fda2f1 337 beacon_detected[6]||beacon_detected[7],
re633 9:ef0907fda2f1 338 beacon_detected[7]||beacon_detected[0]);
re633 9:ef0907fda2f1 339
re633 9:ef0907fda2f1 340 //If the max IR value is below a threshold then move toward beacon. Else change state
re633 9:ef0907fda2f1 341 if(maxValue[0] < AT_BEACON_THRESHOLD){
re633 9:ef0907fda2f1 342
re633 9:ef0907fda2f1 343 //Calculate the heading of Pi-Swarm Relative to beacon
re633 9:ef0907fda2f1 344 //printf("%d ",g_currentHeading);
re633 9:ef0907fda2f1 345 calculateNewHeading();
re633 9:ef0907fda2f1 346 printf("%d ",g_currentHeading);
re633 9:ef0907fda2f1 347 if(g_currentHeading > 5 ||g_currentHeading < -5){
re633 9:ef0907fda2f1 348 turnDegrees(-g_currentHeading);
re633 9:ef0907fda2f1 349 }
re633 9:ef0907fda2f1 350 printf("%d\n",g_currentHeading);
re633 9:ef0907fda2f1 351 //}
re633 9:ef0907fda2f1 352
re633 9:ef0907fda2f1 353
re633 9:ef0907fda2f1 354
re633 9:ef0907fda2f1 355 //If the beacon is not currently on but obstacle detected then do obstacle avoidance
re633 9:ef0907fda2f1 356
re633 9:ef0907fda2f1 357 if(flagBeaconIlluminated == 0){
re633 9:ef0907fda2f1 358 if(gv_IRDistances[0] < 100 || gv_IRDistances[1] < 100){
re633 11:c5094a68283f 359 turnDegrees(-90);
re633 9:ef0907fda2f1 360 } else if (gv_IRDistances[6] < 100 || gv_IRDistances[7] < 100){
re633 11:c5094a68283f 361 turnDegrees(90);
re633 9:ef0907fda2f1 362 }
re633 9:ef0907fda2f1 363 }
re633 9:ef0907fda2f1 364
re633 9:ef0907fda2f1 365
re633 9:ef0907fda2f1 366 piswarm.forward(0.2);
re633 9:ef0907fda2f1 367 wait(1);
re633 9:ef0907fda2f1 368 //Should be at beacon
re633 9:ef0907fda2f1 369 } else {
re633 9:ef0907fda2f1 370 piswarm.stop();
re633 9:ef0907fda2f1 371 changeState(3);
re633 9:ef0907fda2f1 372
re633 9:ef0907fda2f1 373 }
re633 9:ef0907fda2f1 374 }
re633 9:ef0907fda2f1 375 //Else need to syncronise with beacon
re633 9:ef0907fda2f1 376 } else {
re633 9:ef0907fda2f1 377
re633 9:ef0907fda2f1 378 while(flagBeaconSyncronised == 0){
re633 9:ef0907fda2f1 379 //Sychronise the ticker with the beacon
re633 9:ef0907fda2f1 380
re633 9:ef0907fda2f1 381 uint8_t testBefore = 0;
re633 9:ef0907fda2f1 382 uint8_t testDuring = 0;
re633 9:ef0907fda2f1 383 uint8_t testAfter = 0;
re633 9:ef0907fda2f1 384 for(loopCounter = 0; loopCounter < 8; loopCounter++){
re633 9:ef0907fda2f1 385 if (gv_IRValDiffs[mod8((g_beaconOn - 1),IR_READ_PER_BEAC)][loopCounter] > BEACON_SUSPECTED){
re633 9:ef0907fda2f1 386 testBefore = 1;
re633 9:ef0907fda2f1 387 }
re633 9:ef0907fda2f1 388 if (gv_IRValDiffs[g_beaconOn][loopCounter] > BEACON_SUSPECTED){
re633 9:ef0907fda2f1 389 testDuring = 1;
re633 9:ef0907fda2f1 390 }
re633 9:ef0907fda2f1 391 if (gv_IRValDiffs[mod8((g_beaconOn + 2),IR_READ_PER_BEAC)][loopCounter] > BEACON_SUSPECTED){
re633 9:ef0907fda2f1 392 testAfter = 1;
re633 9:ef0907fda2f1 393 }
re633 9:ef0907fda2f1 394 if (gv_IRValDiffs[mod8((g_beaconOn + 2),IR_READ_PER_BEAC)][loopCounter] < -BEACON_SUSPECTED){
re633 9:ef0907fda2f1 395 testAfter = 2;
re633 9:ef0907fda2f1 396 }
re633 9:ef0907fda2f1 397 }
re633 9:ef0907fda2f1 398 //Firstly if the beacon is not detected by any of the sensors then change state back to search
re633 9:ef0907fda2f1 399 if(testBefore == 0 && testDuring == 0 && testAfter == 0){
re633 9:ef0907fda2f1 400 changeState(11);
re633 9:ef0907fda2f1 401 flagBeaconSyncronised = 1;//to exit while loop
re633 9:ef0907fda2f1 402
re633 9:ef0907fda2f1 403 //If the tick before g_beaconOn is detecting the change caused by the flash change the value of g_beaconOn
re633 9:ef0907fda2f1 404 } else if(testBefore == 1){
re633 9:ef0907fda2f1 405 g_beaconOn = g_beaconOn - 1;
re633 9:ef0907fda2f1 406
re633 9:ef0907fda2f1 407 //If the After Tick does not show a drop in value then it is also occuring within the beacon flash so delay the ticker by 15ms
re633 9:ef0907fda2f1 408 } else if(testBefore == 0 && testDuring == 1 && testAfter == 1){
re633 9:ef0907fda2f1 409 ticker_25ms.detach();
re633 9:ef0907fda2f1 410 timer.reset();
re633 9:ef0907fda2f1 411 while(timer.read_ms() < 100){};
re633 9:ef0907fda2f1 412 ticker_25ms.attach_us(&readIRs,25000);
re633 9:ef0907fda2f1 413 wait(1);
re633 9:ef0907fda2f1 414
re633 9:ef0907fda2f1 415 //If successful the set flag
re633 9:ef0907fda2f1 416 } else if (testBefore == 0 && testDuring == 1 && testAfter == 2){
re633 9:ef0907fda2f1 417 flagBeaconSyncronised = 1;
re633 9:ef0907fda2f1 418
re633 9:ef0907fda2f1 419 //Error handle. If this happens stop the piswarm
re633 9:ef0907fda2f1 420 } else {
re633 9:ef0907fda2f1 421 piswarm.set_oled_colour(255,255,255);
re633 9:ef0907fda2f1 422 piswarm.set_oleds(1,1,1,1,1,1,1,1,1,1);
re633 9:ef0907fda2f1 423 piswarm.cls();
re633 9:ef0907fda2f1 424 piswarm.printf("%d %d %d",testBefore, testDuring,testAfter);
re633 9:ef0907fda2f1 425 piswarm.stop();
re633 9:ef0907fda2f1 426 wait(5);
re633 9:ef0907fda2f1 427 changeState(11);
re633 9:ef0907fda2f1 428 }
re633 9:ef0907fda2f1 429 }
re633 9:ef0907fda2f1 430 }
re633 9:ef0907fda2f1 431 }
re633 9:ef0907fda2f1 432 }
jah128 7:d03e54d9eb1c 433 }
jah128 7:d03e54d9eb1c 434
jah128 2:e806b595f9ce 435 /***************************************************************************************************************************************
jah128 2:e806b595f9ce 436 *
jah128 4:823174be9a6b 437 * Beyond this point, empty code blocks for optional functions is given
jah128 2:e806b595f9ce 438 *
jah128 4:823174be9a6b 439 * These may be left blank if not used, but should not be deleted
jah128 2:e806b595f9ce 440 *
jah128 2:e806b595f9ce 441 **************************************************************************************************************************************/
jah128 2:e806b595f9ce 442
jah128 1:37502eb3b70f 443 // Communications
jah128 1:37502eb3b70f 444
jah128 1:37502eb3b70f 445 // If using the communication stack (USE_COMMUNICATION_STACK = 1), functionality for handling user RF responses should be added to the following functions
jah128 1:37502eb3b70f 446 // If the communication stack is not being used, all radio data is sent to processRawRFData() instead
jah128 1:37502eb3b70f 447
jah128 1:37502eb3b70f 448 void handleUserRFCommand(char sender, char broadcast_message, char request_response, char id, char is_command, char function, char * data, char length){
jah128 1:37502eb3b70f 449 // A 'user' RF Command has been received: write the code here to process it
jah128 1:37502eb3b70f 450 // sender = ID of the sender, range 0 to 31
jah128 1:37502eb3b70f 451 // broadcast_message = 1 is message sent to all robots, 0 otherwise
jah128 1:37502eb3b70f 452 // request_response = 1 if a response is expected, 0 otherwise
jah128 1:37502eb3b70f 453 // id = Message ID, range 0 to 255
jah128 1:37502eb3b70f 454 // is_command = 1 is message is a command, 0 if it is a request. If RF_ALLOW_COMMANDS is not selected, only requests will be sent to this block
jah128 1:37502eb3b70f 455 // function = The function identifier. Range 0 to 15
jah128 1:37502eb3b70f 456 // * data = Array containing extra data bytes
jah128 1:37502eb3b70f 457 // length = Length of extra data bytes held (range 0 to 57)
jah128 8:a789ef4fde52 458
jah128 7:d03e54d9eb1c 459
jah128 4:823174be9a6b 460 //Do something...
re633 10:da62735d6df9 461 if(function == 1 && gv_state == 0) {
re633 10:da62735d6df9 462 flagSystemState = 1;
re633 10:da62735d6df9 463 }
jah128 1:37502eb3b70f 464 }
jah128 1:37502eb3b70f 465
jah128 1:37502eb3b70f 466 void handleUserRFResponse(char sender, char broadcast_message, char success, char id, char is_command, char function, char * data, char length){
jah128 1:37502eb3b70f 467 // A 'user' RF Response has been received: write the code here to process it
jah128 1:37502eb3b70f 468 // sender = ID of the sender, range 0 to 31
jah128 1:37502eb3b70f 469 // broadcast_message = 1 is message sent to all robots, 0 otherwise
jah128 1:37502eb3b70f 470 // success = 1 if operation successful, 0 otherwise
jah128 1:37502eb3b70f 471 // id = Message ID, range 0 to 255
jah128 1:37502eb3b70f 472 // is_command = 1 is message is a command, 0 if it is a request. If RF_ALLOW_COMMANDS is not selected, only requests will be sent to this block
jah128 1:37502eb3b70f 473 // function = The function identifier. Range 0 to 15
jah128 1:37502eb3b70f 474 // * data = Array containing extra data bytes
jah128 1:37502eb3b70f 475 // length = Length of extra data bytes held (range 0 to 57)
jah128 4:823174be9a6b 476
jah128 4:823174be9a6b 477 //Do something...
jah128 1:37502eb3b70f 478 }
jah128 1:37502eb3b70f 479
jah128 1:37502eb3b70f 480 void processRawRFData(char * rstring, char cCount){
jah128 1:37502eb3b70f 481 // A raw RF packet has been received: write the code here to process it
jah128 1:37502eb3b70f 482 // rstring = The received packet
jah128 1:37502eb3b70f 483 // cCount = Packet length
jah128 4:823174be9a6b 484
jah128 4:823174be9a6b 485 //Do something...
jah128 0:46cd1498a39a 486 }
jah128 0:46cd1498a39a 487
jah128 1:37502eb3b70f 488 void switch_pressed() {
jah128 1:37502eb3b70f 489 //Switch(es) pressed {1 = Center 2 = Right 4 = Left 8 = Down 16 = Up}
jah128 1:37502eb3b70f 490 char switches = piswarm.get_switches();
jah128 1:37502eb3b70f 491
jah128 1:37502eb3b70f 492 //Do something...
jah128 0:46cd1498a39a 493 }