Homero Silva / Mbed 2 deprecated PRGP_Pi_Swarm_ground_search_algorithm

Dependencies:   mbed

Fork of Pi_Swarm_Blank by James Hilder

Committer:
homero
Date:
Sun Aug 23 17:22:54 2015 +0000
Revision:
16:a32ee9ed15c4
Parent:
15:f7a8989a3cd3
Child:
17:4fa7c9e1b512
Bracket error fixed.

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 *
re633 11:c5094a68283f 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
re633 11:c5094a68283f 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
re633 11:c5094a68283f 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
re633 11:c5094a68283f 21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
re633 11:c5094a68283f 22 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
re633 11:c5094a68283f 23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
re633 11:c5094a68283f 24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
re633 11:c5094a68283f 25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
re633 11:c5094a68283f 26 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
re633 11:c5094a68283f 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
re633 11:c5094a68283f 28 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
re633 11:c5094a68283f 29 * POSSIBILITY OF SUCH DAMAGE.
re633 11:c5094a68283f 30 */
re633 11:c5094a68283f 31 /**
re633 11:c5094a68283f 32 * @file main.cpp
re633 11:c5094a68283f 33 * @brief The Ticker fucntion for gaining sensor data and the main function for the PRGP Pi-Swarm Controllers.
re633 11:c5094a68283f 34 * @details In this file the main function for the Pi-Swarm Controller is defined.
re633 11:c5094a68283f 35 * @version 1.0
homero 13:c18d82f62d38 36 * @author Robert Evans
re633 11:c5094a68283f 37 * @date 24/07/15
homero 13:c18d82f62d38 38 * @version 2.0
homero 13:c18d82f62d38 39 * @author Homero Silva
homero 13:c18d82f62d38 40 * @date 16/08/15
homero 13:c18d82f62d38 41 * @ultrasonic sensor added
homero 15:f7a8989a3cd3 42 * @version 3.0
homero 13:c18d82f62d38 43 * @author Robert Evans
homero 13:c18d82f62d38 44 * @date 17/08/15
homero 13:c18d82f62d38 45 * @communication added
re633 11:c5094a68283f 46 */
jah128 3:1aa1de26966a 47 #include "main.h" // Certain parameters can be set by changing the defines in piswarm.h
homero 14:fc406dfff94f 48 #include "pi_swarm_functions.h"
homero 13:c18d82f62d38 49 #include "hcsr04.h"
jah128 0:46cd1498a39a 50
jah128 1:37502eb3b70f 51 PiSwarm piswarm;
jah128 1:37502eb3b70f 52 Serial pc (USBTX,USBRX);
homero 13:c18d82f62d38 53 HCSR04 usensor(p30,p14);
jah128 0:46cd1498a39a 54
re633 9:ef0907fda2f1 55 //Tickers
homero 14:fc406dfff94f 56 Ticker ticker_25ms; //Ticker used to periodically obtain new IR readings.
homero 14:fc406dfff94f 57 Ticker ticker_ultrasonic50ms; //Ticker used to periodically obtain a new ultrasonic reading.
homero 13:c18d82f62d38 58
homero 13:c18d82f62d38 59 //Timeouts
homero 14:fc406dfff94f 60 Timeout tickChangeTimeout; //Timeout used to adjust IR read ticker phase to enable to beacon syncronisation.
homero 14:fc406dfff94f 61 Timeout broadcastTimeout; //Timeout used for broadcasting messages multiple times with a set interval.
homero 13:c18d82f62d38 62
homero 13:c18d82f62d38 63 //Timers
homero 14:fc406dfff94f 64 Timer turn_timer; //Timer used to turn a user specified number of degrees.
homero 14:fc406dfff94f 65 Timer levy_timer; //Timer used to control time between levy walk steps.
re633 9:ef0907fda2f1 66
re633 9:ef0907fda2f1 67 //Global Variables
homero 14:fc406dfff94f 68
homero 14:fc406dfff94f 69 //Individucal Pi-Swarm variables: speed and motor trim etc
homero 14:fc406dfff94f 70 float BASE_SPEED = 0.6; //The input to the motor functions when the robot is moving forward and does not detect obstacles.
homero 14:fc406dfff94f 71 float r_mot_scaler = 1; //This variable is allows particular pi swarms to be adjusted to travel in a straight line.
homero 14:fc406dfff94f 72 float l_mot_scaler = 1; //This variable is allows particular pi swarms to be adjusted to travel in a straight line.
homero 14:fc406dfff94f 73 uint16_t at_beacon_threshold = 3800; //A raw IR reading greater than this value indicates the robot is at a beacon.
homero 14:fc406dfff94f 74 int16_t g_currentHeading = 0; //The current heading of the Pi-swarm relative to the IR beacon.
homero 14:fc406dfff94f 75 uint8_t robot_id; //The id unique to each Pi-Swarm robot
homero 14:fc406dfff94f 76
homero 14:fc406dfff94f 77 //Finite state machine information
homero 14:fc406dfff94f 78 uint8_t volatile gv_state = States::READY_TO_START; //This is the current state of the finite state machine
homero 14:fc406dfff94f 79 int8_t g_obstacle_type = 0; //The Pi-Swarm will perform different obstacle avoidance behaviours depending on which sensors detect obstacles. This variable is used to note which sensor detected the obstacle.
homero 14:fc406dfff94f 80
homero 14:fc406dfff94f 81 //IR Readings + beacon syncronisation variables
re633 9:ef0907fda2f1 82 uint8_t const IR_READ_PER_BEAC = 20; //The number of IR readings between beacon flashes
re633 9:ef0907fda2f1 83 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
homero 14:fc406dfff94f 84 int16_t volatile gv_IRValDiffs[IR_READ_PER_BEAC][8] = {0};//The difference between consective IR readings.
homero 14:fc406dfff94f 85 int16_t volatile gv_IRValDiffsTwo[IR_READ_PER_BEAC][8] = {0};//The difference between IR readings seperated by two ticks
homero 14:fc406dfff94f 86 uint8_t volatile beacon_detected[8] = {0}; //An IR sensor is judged to have a detected the beacon in a valid way the corresponding array variable will be set to 1, else 0.
re633 9:ef0907fda2f1 87 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)
homero 14:fc406dfff94f 88 int8_t g_beaconOn = 100; //The first tick at which the beacon is illuminated
homero 13:c18d82f62d38 89 uint8_t const BEACON_SUSPECTED = 20; //Value by which consecutive IR sensor readings need to jump by for in order to cause beacon to be suspected.
homero 14:fc406dfff94f 90 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 11:c5094a68283f 91 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 92 int8_t tick_beacon_period_check = 100; //Is used to temporarily store the value of tick_beacon_suspected
homero 14:fc406dfff94f 93 uint8_t beacon_syncronised_flag = 0; //Set to one when the robot is synchronised with the beacon
homero 14:fc406dfff94f 94 uint8_t volatile beacon_illuminated_flag = 0; //Should be 1 when beacon is illuminated otherwise 0
homero 14:fc406dfff94f 95
homero 14:fc406dfff94f 96 //Ultrasonic Readings
homero 14:fc406dfff94f 97 float volatile distance_ultrasonic_sensor = 1000; //Here is stored the latest reading from the ultrasonic sensor in mm from 1 -800. 1000 is an error value.
homero 14:fc406dfff94f 98
homero 14:fc406dfff94f 99 //Levy walk variables
homero 14:fc406dfff94f 100 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.
homero 14:fc406dfff94f 101 uint8_t g_back_count = 0; //After a set number of Levy walk turns the robot will move backward. This variable monitors how many Levy turns since the last backward move.
homero 14:fc406dfff94f 102 uint8_t set_new_levy_time_flag = 1; //Must start as 1. If fwd move state is entered following a levy turn the flag will be 1 indicating a new time should be set.
homero 14:fc406dfff94f 103
homero 14:fc406dfff94f 104
homero 15:f7a8989a3cd3 105 //Communication
homero 14:fc406dfff94f 106 uint8_t broadcasted_flag = 1; //Set to one when a message has been broadcast. 0 when about to broadcast a new message
homero 14:fc406dfff94f 107 uint8_t broadcasted_count = 0; //The number of messages that have been broadcasted in one batch
homero 14:fc406dfff94f 108 int8_t volatile start_flag = 1; //Set to 1 on receiving a command by RF. Indicates robot can start searching for target beacon.
homero 14:fc406dfff94f 109 int8_t volatile back_flag = 0; //Set to 1 on receiving a command by RF. Indicates robot can start searching for home beacon, unless it is at the target beacon.
homero 14:fc406dfff94f 110 int8_t volatile return_flag = 0; //Set to 1 on receiving a command by RF. Indicates all robots now start or continue searching for home beacon.
homero 13:c18d82f62d38 111
re633 9:ef0907fda2f1 112 //Ticker Function*************************************************************************************
re633 9:ef0907fda2f1 113 //This function is called by a ticker every 25ms
re633 9:ef0907fda2f1 114 //The function firstly stores the background IRS values.
re633 9:ef0907fda2f1 115 //Secondly if beacon is off it uses illuminated IR sensors to estimate distances
re633 9:ef0907fda2f1 116 //Each second it will update when it believes beacon illumination time to be
re633 9:ef0907fda2f1 117 //It will work out which sensors spotted then beacon and will illuminate the Leds accordingly
homero 15:f7a8989a3cd3 118 void readIRs()
homero 15:f7a8989a3cd3 119 {
homero 15:f7a8989a3cd3 120
re633 9:ef0907fda2f1 121 //Fistly update the beaconVisable flag if possible
homero 15:f7a8989a3cd3 122 if(gv_counter25ms == mod8((g_beaconOn - 1), IR_READ_PER_BEAC)) {
homero 14:fc406dfff94f 123 beacon_illuminated_flag = 1;
re633 9:ef0907fda2f1 124 }
homero 15:f7a8989a3cd3 125
homero 15:f7a8989a3cd3 126 if(gv_counter25ms == mod8((g_beaconOn + 2), IR_READ_PER_BEAC)) {
homero 14:fc406dfff94f 127 beacon_illuminated_flag = 0;
re633 9:ef0907fda2f1 128 }
re633 9:ef0907fda2f1 129 //Firstly store background values
re633 9:ef0907fda2f1 130 //Also make a note of which point in the second did the values change most.
re633 9:ef0907fda2f1 131 //For which sensor specifically did the values change the most
re633 9:ef0907fda2f1 132 //That sensor will be used to estimate the beacon start time if a threshold value is met
re633 9:ef0907fda2f1 133 piswarm.store_background_raw_ir_values();
re633 9:ef0907fda2f1 134 int16_t IRchange = 0;
homero 13:c18d82f62d38 135 int16_t IRchange2 = 0;
re633 9:ef0907fda2f1 136 uint8_t loopCounter = 0;
re633 9:ef0907fda2f1 137 //In this loop the raw IR values are read.
re633 9:ef0907fda2f1 138 //If the points where the IR values have increased by the greatest amount are noted as this indicates a beacon illumination
re633 9:ef0907fda2f1 139 for(loopCounter = 0; loopCounter < 8; loopCounter++) {
re633 9:ef0907fda2f1 140 gv_IRVals[gv_counter25ms][loopCounter] = piswarm.get_background_raw_ir_value(loopCounter);
homero 15:f7a8989a3cd3 141
homero 13:c18d82f62d38 142 IRchange = gv_IRVals[gv_counter25ms][loopCounter]-gv_IRVals[mod8((gv_counter25ms-1),IR_READ_PER_BEAC)][loopCounter];
homero 13:c18d82f62d38 143 IRchange2 = gv_IRVals[gv_counter25ms][loopCounter]-gv_IRVals[mod8((gv_counter25ms-2),IR_READ_PER_BEAC)][loopCounter];
re633 9:ef0907fda2f1 144 gv_IRValDiffs[gv_counter25ms][loopCounter] = IRchange;
homero 13:c18d82f62d38 145 gv_IRValDiffsTwo[gv_counter25ms][loopCounter] = IRchange2;
homero 15:f7a8989a3cd3 146
re633 9:ef0907fda2f1 147 //printf("change %d count %d\n",IRchange);
re633 9:ef0907fda2f1 148 //If difference is greater than a threshold value then the beacon is suspected. This will be confirmed depending on the robots state of movement.
homero 15:f7a8989a3cd3 149 if (IRchange > BEACON_SUSPECTED) {
re633 11:c5094a68283f 150 tick_beacon_suspected = gv_counter25ms;
re633 9:ef0907fda2f1 151 piswarm.cls();
re633 11:c5094a68283f 152 piswarm.printf("%d",tick_beacon_suspected);
homero 15:f7a8989a3cd3 153 }
re633 9:ef0907fda2f1 154 }
homero 15:f7a8989a3cd3 155
re633 9:ef0907fda2f1 156 //Now store the illuminated values if the beacon is not illuminated-
re633 9:ef0907fda2f1 157 piswarm.store_illuminated_raw_ir_values();
homero 15:f7a8989a3cd3 158
homero 15:f7a8989a3cd3 159 //In this loop convert each raw active IR reading into a distance estimate
re633 9:ef0907fda2f1 160 for(loopCounter = 0; loopCounter < 8; loopCounter++) {
homero 15:f7a8989a3cd3 161
re633 9:ef0907fda2f1 162 //Specific sensor readings converted to distances
re633 9:ef0907fda2f1 163 float temp = piswarm.get_illuminated_raw_ir_value(loopCounter);
homero 13:c18d82f62d38 164 //if(gv_counter25ms == 1) pc.printf("sen %d :%f\n", loopCounter,temp);
homero 15:f7a8989a3cd3 165 if(gv_counter25ms == 0) {
homero 13:c18d82f62d38 166 pc.printf("sen %d raw: %f",loopCounter,temp);
homero 13:c18d82f62d38 167 }
homero 15:f7a8989a3cd3 168 if(temp>3500) {
homero 15:f7a8989a3cd3 169 temp = 3500;
homero 15:f7a8989a3cd3 170 } else if (temp < 97) {
homero 15:f7a8989a3cd3 171 temp = 97;
re633 9:ef0907fda2f1 172 }
homero 15:f7a8989a3cd3 173 //#put this into a function
re633 9:ef0907fda2f1 174 //Switch case for robot 5
homero 15:f7a8989a3cd3 175 switch(robot_id) {
homero 13:c18d82f62d38 176 case 9:
homero 15:f7a8989a3cd3 177 switch(loopCounter) {
homero 13:c18d82f62d38 178 case 0:
homero 13:c18d82f62d38 179 temp = 1643 * sqrt(1/(temp-190))-35;
homero 15:f7a8989a3cd3 180 break;
homero 13:c18d82f62d38 181 case 1:
homero 13:c18d82f62d38 182 temp = 1097 * sqrt(1/(temp-190))-24;
homero 15:f7a8989a3cd3 183 break;
homero 13:c18d82f62d38 184 case 2:
homero 13:c18d82f62d38 185 temp = 838 * sqrt(1/(temp-120))-17;
homero 13:c18d82f62d38 186 break;
homero 13:c18d82f62d38 187 case 3:
homero 13:c18d82f62d38 188 temp = 1017 * sqrt(1/(temp-175))-22;
homero 13:c18d82f62d38 189 break;
homero 13:c18d82f62d38 190 case 4:
homero 13:c18d82f62d38 191 temp = 777 * sqrt(1/(temp-130))-16;
homero 13:c18d82f62d38 192 break;
homero 13:c18d82f62d38 193 case 5:
homero 13:c18d82f62d38 194 temp = 765 * sqrt(1/(temp-115))-11;
homero 13:c18d82f62d38 195 break;
homero 13:c18d82f62d38 196 case 6:
homero 13:c18d82f62d38 197 temp = 1086 * sqrt(1/(temp-150))-17;
homero 13:c18d82f62d38 198 break;
homero 13:c18d82f62d38 199 case 7:
homero 13:c18d82f62d38 200 temp = 1578 * sqrt(1/(temp-220))-24;
homero 13:c18d82f62d38 201 break;
homero 13:c18d82f62d38 202 }
re633 9:ef0907fda2f1 203 break;
homero 13:c18d82f62d38 204 case 12:
homero 15:f7a8989a3cd3 205 switch(loopCounter) {
homero 13:c18d82f62d38 206 case 0:
homero 13:c18d82f62d38 207 temp = 877 * sqrt(1/(temp-80))-13;
homero 15:f7a8989a3cd3 208 break;
homero 13:c18d82f62d38 209 case 1:
homero 13:c18d82f62d38 210 temp = 887 * sqrt(1/(temp-110))-13;
homero 15:f7a8989a3cd3 211 break;
homero 13:c18d82f62d38 212 case 2:
homero 13:c18d82f62d38 213 temp = 744 * sqrt(1/(temp-90))-8;
homero 13:c18d82f62d38 214 break;
homero 13:c18d82f62d38 215 case 3:
homero 13:c18d82f62d38 216 temp = 816 * sqrt(1/(temp-105))-17;
homero 13:c18d82f62d38 217 break;
homero 13:c18d82f62d38 218 case 4:
homero 13:c18d82f62d38 219 temp = 821 * sqrt(1/(temp-125))-7;
homero 13:c18d82f62d38 220 break;
homero 13:c18d82f62d38 221 case 5:
homero 13:c18d82f62d38 222 temp = 741 * sqrt(1/(temp-110))-7;
homero 13:c18d82f62d38 223 break;
homero 13:c18d82f62d38 224 case 6:
homero 13:c18d82f62d38 225 temp = 1000 * sqrt(1/(temp-95))-18;
homero 13:c18d82f62d38 226 break;
homero 13:c18d82f62d38 227 case 7:
homero 13:c18d82f62d38 228 temp = 924 * sqrt(1/(temp-115))-12;
homero 13:c18d82f62d38 229 break;
homero 13:c18d82f62d38 230 }
re633 9:ef0907fda2f1 231 break;
homero 13:c18d82f62d38 232 default:
homero 15:f7a8989a3cd3 233 temp = 662 * sqrt(1/(temp-152));
homero 13:c18d82f62d38 234
re633 9:ef0907fda2f1 235 }
homero 15:f7a8989a3cd3 236
homero 15:f7a8989a3cd3 237 if(gv_counter25ms == 0) {
homero 15:f7a8989a3cd3 238 pc.printf("sen %d dist:%f\n",loopCounter,temp);
homero 13:c18d82f62d38 239 }
homero 15:f7a8989a3cd3 240 if (temp > 130) {
re633 9:ef0907fda2f1 241 temp = 130;
re633 9:ef0907fda2f1 242 }
homero 15:f7a8989a3cd3 243 gv_IRDistances[loopCounter] = temp;
re633 9:ef0907fda2f1 244
homero 13:c18d82f62d38 245 }
homero 15:f7a8989a3cd3 246
re633 9:ef0907fda2f1 247 //reset counter after 1 second (beacon period)
re633 9:ef0907fda2f1 248 gv_counter25ms = mod8(gv_counter25ms + 1,IR_READ_PER_BEAC);
re633 9:ef0907fda2f1 249 }
re633 9:ef0907fda2f1 250
homero 15:f7a8989a3cd3 251 //Get the distance of the ultrsonic sensor in cm
homero 15:f7a8989a3cd3 252 void get_ultrasonic_readings()
homero 15:f7a8989a3cd3 253 {
homero 13:c18d82f62d38 254 float old_dist = distance_ultrasonic_sensor;
homero 15:f7a8989a3cd3 255 int static count = 0;
homero 13:c18d82f62d38 256 distance_ultrasonic_sensor = usensor.get_dist_mm();
homero 15:f7a8989a3cd3 257 if(count <100) {
homero 13:c18d82f62d38 258 pc.printf("US: %.1f, %d\n",distance_ultrasonic_sensor, count);
homero 13:c18d82f62d38 259 count ++;
homero 15:f7a8989a3cd3 260 } else {
homero 13:c18d82f62d38 261 count =0;
homero 13:c18d82f62d38 262 }
homero 15:f7a8989a3cd3 263
homero 15:f7a8989a3cd3 264 if(distance_ultrasonic_sensor <= 0 || distance_ultrasonic_sensor == 1000) {
homero 13:c18d82f62d38 265 distance_ultrasonic_sensor = old_dist;
homero 13:c18d82f62d38 266 }
homero 15:f7a8989a3cd3 267 //piswarm.cls();
homero 15:f7a8989a3cd3 268
homero 13:c18d82f62d38 269 usensor.start();
homero 13:c18d82f62d38 270 }
homero 13:c18d82f62d38 271
homero 13:c18d82f62d38 272 //When called the readIRs ticker will be reattached after the specified time.
homero 15:f7a8989a3cd3 273 void atTimeout()
homero 15:f7a8989a3cd3 274 {
homero 13:c18d82f62d38 275 ticker_25ms.attach_us(&readIRs,25000);
homero 13:c18d82f62d38 276 }
homero 13:c18d82f62d38 277
homero 15:f7a8989a3cd3 278 void atBroadcastTimeout()
homero 15:f7a8989a3cd3 279 {
homero 13:c18d82f62d38 280 char function;
homero 15:f7a8989a3cd3 281 if(robot_id == 1|| robot_id == 12) {
homero 13:c18d82f62d38 282 function = 2;
homero 15:f7a8989a3cd3 283 } else if(robot_id == 9) {
homero 13:c18d82f62d38 284 function = 3;
homero 13:c18d82f62d38 285 }
homero 15:f7a8989a3cd3 286
homero 13:c18d82f62d38 287 char message[2];
homero 13:c18d82f62d38 288 message[0] = piswarm.get_id()+48;
homero 13:c18d82f62d38 289 broadcast_user_rf_command(function,message,1);
homero 13:c18d82f62d38 290 broadcasted_count++;
homero 13:c18d82f62d38 291 broadcasted_flag = 1;
homero 13:c18d82f62d38 292 }
homero 13:c18d82f62d38 293 //This is a wait function for one
homero 13:c18d82f62d38 294
re633 9:ef0907fda2f1 295 //*******************************************************************************************************
homero 15:f7a8989a3cd3 296 //This is where the program code goes.
homero 15:f7a8989a3cd3 297 int main()
homero 15:f7a8989a3cd3 298 {
homero 15:f7a8989a3cd3 299 init();
homero 13:c18d82f62d38 300 robot_id = piswarm.get_id();
re633 9:ef0907fda2f1 301 //starting point in state 11
homero 15:f7a8989a3cd3 302 //usensor.start(); // get first reading of the ultrasonic sensor required before ticker
homero 13:c18d82f62d38 303 //wait_ms(50);
homero 15:f7a8989a3cd3 304
homero 15:f7a8989a3cd3 305
homero 13:c18d82f62d38 306 //wait(1); //Wait a second to allow IR array to be filled
homero 15:f7a8989a3cd3 307
re633 9:ef0907fda2f1 308 //Controller is a finite state machine
homero 15:f7a8989a3cd3 309 while(1) {
homero 15:f7a8989a3cd3 310
re633 9:ef0907fda2f1 311 //Waiting for signal to begin searching
homero 15:f7a8989a3cd3 312 if(gv_state == States::READY_TO_START) {
homero 13:c18d82f62d38 313 //pc.printf("%d\n",start_flag);
homero 15:f7a8989a3cd3 314 if(start_flag == 1) {
re633 12:118f2b0ed8eb 315 //Change state here after recieving a radio command
homero 13:c18d82f62d38 316 ticker_25ms.attach_us(&readIRs,25000);
homero 15:f7a8989a3cd3 317
homero 15:f7a8989a3cd3 318 if(robot_id == 1) {
homero 14:fc406dfff94f 319 at_beacon_threshold = 3850;
homero 13:c18d82f62d38 320 }
homero 15:f7a8989a3cd3 321
homero 15:f7a8989a3cd3 322 else if (robot_id == 9) {
homero 14:fc406dfff94f 323 at_beacon_threshold = 3950;
homero 13:c18d82f62d38 324 }
homero 15:f7a8989a3cd3 325
homero 15:f7a8989a3cd3 326 else if (robot_id == 12) {
homero 14:fc406dfff94f 327 at_beacon_threshold = 3980;
homero 13:c18d82f62d38 328 r_mot_scaler = 1.02;
homero 13:c18d82f62d38 329 l_mot_scaler = 0.98;
homero 13:c18d82f62d38 330 }
homero 15:f7a8989a3cd3 331
homero 15:f7a8989a3cd3 332 else {
homero 14:fc406dfff94f 333 at_beacon_threshold = 3900;
homero 13:c18d82f62d38 334 }
homero 15:f7a8989a3cd3 335
homero 15:f7a8989a3cd3 336 usensor.start(); // get first reading of the ultrasonic sensor required before ticker
homero 13:c18d82f62d38 337 wait_ms(50);
homero 13:c18d82f62d38 338 ticker_ultrasonic50ms.attach_us(&get_ultrasonic_readings,50000);
homero 15:f7a8989a3cd3 339
homero 14:fc406dfff94f 340 turn_timer.start();
homero 14:fc406dfff94f 341 levy_timer.start();
homero 13:c18d82f62d38 342 //pc.printf("why\n");
homero 13:c18d82f62d38 343 changeState(States::SEARCHING_FWD);
re633 10:da62735d6df9 344 }
homero 15:f7a8989a3cd3 345
homero 15:f7a8989a3cd3 346 //Searching state
homero 15:f7a8989a3cd3 347 } else if (gv_state == States::SEARCHING_FWD || gv_state == States::SEARCHING_TURNING) {
homero 15:f7a8989a3cd3 348
re633 12:118f2b0ed8eb 349 //Do something here on receipt of 'function 5' if necessary.
re633 12:118f2b0ed8eb 350 //As currently the home beacon will immediately switch on that is not necessary.
homero 15:f7a8989a3cd3 351
re633 12:118f2b0ed8eb 352 //Determine if suspected beacon is actually the beacon.
re633 9:ef0907fda2f1 353 //This is done by checking the period between flashes matches the beacon period
homero 15:f7a8989a3cd3 354 if(tick_beacon_suspected != 100) {
re633 9:ef0907fda2f1 355 //When the beacon flag is first raised store its value and reset it
homero 15:f7a8989a3cd3 356 if(tick_beacon_period_check == 100) {
re633 11:c5094a68283f 357 tick_beacon_period_check = tick_beacon_suspected;
re633 11:c5094a68283f 358 tick_beacon_suspected = 100;
homero 15:f7a8989a3cd3 359 //Check the timing of the latest jump with the last one to see if period matches the Beacon.
re633 9:ef0907fda2f1 360 } else {
re633 9:ef0907fda2f1 361 piswarm.locate(0,1);
re633 11:c5094a68283f 362 piswarm.printf("%d %d",tick_beacon_period_check,tick_beacon_suspected);
re633 11:c5094a68283f 363 //printf("%d %d *********************************",tick_beacon_period_check,tick_beacon_suspected);
re633 9:ef0907fda2f1 364 //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 365 int8_t test = (tick_beacon_period_check - tick_beacon_suspected);
homero 15:f7a8989a3cd3 366
re633 9:ef0907fda2f1 367 test = test * test;
homero 15:f7a8989a3cd3 368
re633 9:ef0907fda2f1 369 //if test is low then identify the beacon as the cause of the flags
homero 15:f7a8989a3cd3 370 if(test < 2) {
re633 9:ef0907fda2f1 371 //Beacon found change to state 2
re633 11:c5094a68283f 372 g_beaconOn = tick_beacon_period_check; //update the global variable that stores when beacon flashes occur
homero 15:f7a8989a3cd3 373
homero 13:c18d82f62d38 374 //wait(2);
homero 13:c18d82f62d38 375 changeState(States::MOVING_TO_BEACON);
re633 9:ef0907fda2f1 376 } else {
re633 9:ef0907fda2f1 377 //Reset the flag to try again
re633 11:c5094a68283f 378 tick_beacon_period_check = 100;
re633 9:ef0907fda2f1 379 }
re633 9:ef0907fda2f1 380 }
re633 9:ef0907fda2f1 381 }
homero 15:f7a8989a3cd3 382
homero 15:f7a8989a3cd3 383 if(gv_state == States::SEARCHING_FWD) {
homero 15:f7a8989a3cd3 384
homero 15:f7a8989a3cd3 385
homero 15:f7a8989a3cd3 386 if(gv_IRDistances[0] < 100 || gv_IRDistances[1] < 100) {
re633 9:ef0907fda2f1 387 piswarm.stop();
homero 15:f7a8989a3cd3 388 piswarm.cls();
re633 10:da62735d6df9 389 piswarm.printf("ob R");
re633 10:da62735d6df9 390 piswarm.play_tune("CC",1);
homero 14:fc406dfff94f 391 g_obstacle_type = 1;
homero 13:c18d82f62d38 392 changeState(States::SEARCHING_TURNING);
homero 15:f7a8989a3cd3 393
homero 15:f7a8989a3cd3 394 //Avoid obstacle to the left
homero 15:f7a8989a3cd3 395 } else if(gv_IRDistances[6] < 100 || gv_IRDistances[7] < 100) {
re633 9:ef0907fda2f1 396 piswarm.stop();
homero 15:f7a8989a3cd3 397 piswarm.cls();
re633 10:da62735d6df9 398 piswarm.printf("ob L");
re633 10:da62735d6df9 399 piswarm.play_tune("CC",1);
homero 14:fc406dfff94f 400 g_obstacle_type = 2;
homero 15:f7a8989a3cd3 401 changeState(States::SEARCHING_TURNING);
homero 15:f7a8989a3cd3 402
homero 15:f7a8989a3cd3 403 } else if(distance_ultrasonic_sensor < 70) {
homero 13:c18d82f62d38 404 piswarm.stop();
homero 15:f7a8989a3cd3 405 piswarm.cls();
homero 13:c18d82f62d38 406 piswarm.printf("ob F");
homero 13:c18d82f62d38 407 piswarm.play_tune("DD",1);
homero 13:c18d82f62d38 408 //wait(0.1);
homero 14:fc406dfff94f 409 g_obstacle_type = 3;
homero 13:c18d82f62d38 410 changeState(States::SEARCHING_TURNING);
homero 15:f7a8989a3cd3 411
homero 15:f7a8989a3cd3 412
homero 15:f7a8989a3cd3 413 } else if(distance_ultrasonic_sensor <= 80) {
homero 15:f7a8989a3cd3 414 g_obstacle_type = 3;
homero 15:f7a8989a3cd3 415 changeState(States::SEARCHING_TURNING);
homero 15:f7a8989a3cd3 416
homero 15:f7a8989a3cd3 417 } else if(levy_timer.read_us() > levy_target_time_us) {
homero 14:fc406dfff94f 418 g_back_count++;
homero 14:fc406dfff94f 419 set_new_levy_time_flag = 1;
re633 10:da62735d6df9 420 piswarm.play_tune("G",1);
homero 13:c18d82f62d38 421 piswarm.set_oled_colour(255,0,0);
homero 13:c18d82f62d38 422 changeState(States::SEARCHING_TURNING);
homero 15:f7a8989a3cd3 423 } else if(g_back_count >=8) {
homero 14:fc406dfff94f 424 g_back_count = 0;
homero 13:c18d82f62d38 425 piswarm.stop();
homero 13:c18d82f62d38 426 wait_ms(200);
homero 13:c18d82f62d38 427 piswarm.left_motor(-0.4*l_mot_scaler);
homero 15:f7a8989a3cd3 428 piswarm.right_motor(-0.4*r_mot_scaler);
homero 13:c18d82f62d38 429 wait(0.5);
homero 13:c18d82f62d38 430 changeState(States::SEARCHING_TURNING);
homero 15:f7a8989a3cd3 431
homero 15:f7a8989a3cd3 432 } else if (distance_ultrasonic_sensor <= 800) {
homero 15:f7a8989a3cd3 433 //decrease speed when PiSwarm is getting close to an obstacle
homero 15:f7a8989a3cd3 434 float velocity = 0.2;
homero 15:f7a8989a3cd3 435 if(distance_ultrasonic_sensor > 100) {
homero 15:f7a8989a3cd3 436 velocity = (distance_ultrasonic_sensor*0.00057) + 0.142;
homero 15:f7a8989a3cd3 437 }
homero 15:f7a8989a3cd3 438 piswarm.left_motor(velocity*l_mot_scaler);
homero 15:f7a8989a3cd3 439 piswarm.right_motor(velocity*r_mot_scaler);
homero 15:f7a8989a3cd3 440 //wait_ms(0.2);
homero 15:f7a8989a3cd3 441
homero 15:f7a8989a3cd3 442
homero 15:f7a8989a3cd3 443 //Otherwise continue moving forward until distance determined by levy algorithm is calculated.
homero 15:f7a8989a3cd3 444
homero 15:f7a8989a3cd3 445
homero 13:c18d82f62d38 446 } else {
homero 13:c18d82f62d38 447 piswarm.right_motor(BASE_SPEED*r_mot_scaler);
homero 15:f7a8989a3cd3 448 piswarm.left_motor(BASE_SPEED*l_mot_scaler);
homero 13:c18d82f62d38 449 }
homero 13:c18d82f62d38 450 //wait to get new ultrasound reading
homero 15:f7a8989a3cd3 451 //wait_ms(50);
homero 15:f7a8989a3cd3 452
homero 15:f7a8989a3cd3 453
homero 15:f7a8989a3cd3 454 } else if(gv_state == States::SEARCHING_TURNING) {
re633 9:ef0907fda2f1 455 piswarm.stop();//Stop the robot.
re633 9:ef0907fda2f1 456 int16_t randomAngle;
re633 9:ef0907fda2f1 457 //If sent here beacuse of obstacle find angle between -180 to -90 and 90 to 180
homero 15:f7a8989a3cd3 458 if(g_obstacle_type == 1) {
re633 10:da62735d6df9 459 randomAngle = rand()%90 - 135;
homero 15:f7a8989a3cd3 460
homero 15:f7a8989a3cd3 461 } else if(g_obstacle_type == 2) {
re633 10:da62735d6df9 462 randomAngle = rand()%90 + 45;
homero 15:f7a8989a3cd3 463
homero 15:f7a8989a3cd3 464 } else if(g_obstacle_type == 3) {
homero 14:fc406dfff94f 465 randomAngle = rand()%90 + 45;
homero 15:f7a8989a3cd3 466
homero 15:f7a8989a3cd3 467 //Otherwise if here due to levy walk: turn to any random angle
re633 9:ef0907fda2f1 468 } else {
homero 15:f7a8989a3cd3 469 randomAngle = rand()%360 - 180;
re633 9:ef0907fda2f1 470 }
re633 9:ef0907fda2f1 471 turnDegrees(randomAngle); //Make the turn
homero 14:fc406dfff94f 472 g_obstacle_type = 0;
homero 13:c18d82f62d38 473 changeState(States::SEARCHING_FWD);//Move back into state 11
homero 15:f7a8989a3cd3 474
homero 16:a32ee9ed15c4 475 }
homero 16:a32ee9ed15c4 476 //Beacon found state
homero 16:a32ee9ed15c4 477 } else if (gv_state == States::MOVING_TO_BEACON) {
homero 16:a32ee9ed15c4 478
homero 16:a32ee9ed15c4 479 //Do something here on receipt of 'function 5' if necessary.
homero 16:a32ee9ed15c4 480 //As currently the home beacon will immediately switch on that is not necessary.
homero 16:a32ee9ed15c4 481
homero 16:a32ee9ed15c4 482 static int16_t valid_distances[8] = {0};
homero 15:f7a8989a3cd3 483
homero 16:a32ee9ed15c4 484 int16_t maxValue[2] = {0,100}; //Value and sensor position
homero 16:a32ee9ed15c4 485 int8_t loopCounter = 0;
homero 16:a32ee9ed15c4 486
homero 16:a32ee9ed15c4 487 if(beacon_illuminated_flag == 0) {
homero 16:a32ee9ed15c4 488 for(loopCounter = 0; loopCounter < 8; loopCounter++) {
homero 16:a32ee9ed15c4 489 valid_distances[loopCounter] = gv_IRDistances[loopCounter];
homero 16:a32ee9ed15c4 490 }
homero 16:a32ee9ed15c4 491 }
re633 9:ef0907fda2f1 492
homero 16:a32ee9ed15c4 493 //If beacon visible
homero 16:a32ee9ed15c4 494 if(beacon_syncronised_flag == 1) {
homero 16:a32ee9ed15c4 495
homero 16:a32ee9ed15c4 496 //Firstly check beacon is still visible
homero 16:a32ee9ed15c4 497 beacon_syncronised_flag = 0;
homero 16:a32ee9ed15c4 498 //Update array concerning which IRs can see the beacon
homero 16:a32ee9ed15c4 499 for(loopCounter = 0; loopCounter<8; loopCounter++) {
homero 15:f7a8989a3cd3 500
homero 16:a32ee9ed15c4 501 //Find which sensor has the highest reading
homero 16:a32ee9ed15c4 502 if( gv_IRValDiffs[g_beaconOn][loopCounter] > BEACON_SUSPECTED) {
homero 16:a32ee9ed15c4 503 if( valid_distances[loopCounter] > 100 && valid_distances[mod8((loopCounter + 1),8)]>100 && valid_distances[mod8((loopCounter - 1),8)]>100) {
homero 16:a32ee9ed15c4 504 if(gv_IRVals[g_beaconOn][loopCounter] > maxValue[0]) {
homero 16:a32ee9ed15c4 505 maxValue[0] = gv_IRVals[g_beaconOn][loopCounter];
homero 16:a32ee9ed15c4 506 maxValue[1] = loopCounter;
homero 16:a32ee9ed15c4 507 beacon_syncronised_flag = 1; //This will remain as one so long as at least on sensor can see beacon
homero 16:a32ee9ed15c4 508 }
homero 16:a32ee9ed15c4 509 }
re633 9:ef0907fda2f1 510 }
re633 9:ef0907fda2f1 511 }
homero 15:f7a8989a3cd3 512
homero 16:a32ee9ed15c4 513 //Only do this if beacon still visible
homero 15:f7a8989a3cd3 514 if(beacon_syncronised_flag == 1) {
homero 15:f7a8989a3cd3 515
homero 16:a32ee9ed15c4 516 //If the adjacent two sensors are above the threshold too then they can also be marked as illuminated
homero 15:f7a8989a3cd3 517 for(loopCounter = 0; loopCounter<8; loopCounter++) {
re633 9:ef0907fda2f1 518
homero 16:a32ee9ed15c4 519 //reset all beacon detected values
homero 16:a32ee9ed15c4 520 beacon_detected[loopCounter] = 0;
homero 16:a32ee9ed15c4 521
homero 16:a32ee9ed15c4 522 if(abs(maxValue[1] - loopCounter)< 3 || abs(maxValue[1] + 8 - loopCounter)< 3 || abs(maxValue[1] - 8 - loopCounter)< 3) {
homero 16:a32ee9ed15c4 523 if(gv_IRValDiffs[g_beaconOn][loopCounter] > BEACON_SUSPECTED) {
homero 16:a32ee9ed15c4 524 beacon_detected[loopCounter] = 1;
re633 9:ef0907fda2f1 525 }
re633 9:ef0907fda2f1 526 }
re633 9:ef0907fda2f1 527 }
homero 15:f7a8989a3cd3 528
homero 14:fc406dfff94f 529
homero 16:a32ee9ed15c4 530 //Update the piswarm LEDS so the ones that can see the beacon are on.
homero 16:a32ee9ed15c4 531 piswarm.set_oleds(beacon_detected[0]||beacon_detected[1],
homero 16:a32ee9ed15c4 532 beacon_detected[1]||beacon_detected[2],
homero 16:a32ee9ed15c4 533 beacon_detected[2],
homero 16:a32ee9ed15c4 534 beacon_detected[3],
homero 16:a32ee9ed15c4 535 0,
homero 16:a32ee9ed15c4 536 beacon_detected[4],
homero 16:a32ee9ed15c4 537 beacon_detected[5],
homero 16:a32ee9ed15c4 538 beacon_detected[5]||beacon_detected[6],
homero 16:a32ee9ed15c4 539 beacon_detected[6]||beacon_detected[7],
homero 16:a32ee9ed15c4 540 beacon_detected[7]||beacon_detected[0]);
homero 15:f7a8989a3cd3 541
homero 16:a32ee9ed15c4 542 //If the max IR value is below a threshold then move toward beacon. Else change state
homero 16:a32ee9ed15c4 543 if(maxValue[0] < at_beacon_threshold) {
homero 16:a32ee9ed15c4 544
homero 16:a32ee9ed15c4 545 //Calculate the heading of Pi-Swarm Relative to beacon
homero 16:a32ee9ed15c4 546 calculateNewHeading();
homero 16:a32ee9ed15c4 547
homero 16:a32ee9ed15c4 548 if(g_currentHeading > 5 || g_currentHeading < -5) {
homero 16:a32ee9ed15c4 549 turnDegrees(-g_currentHeading);
re633 9:ef0907fda2f1 550 }
homero 15:f7a8989a3cd3 551
homero 16:a32ee9ed15c4 552 //If the beacon is not currently on but obstacle detected then do obstacle avoidance
homero 16:a32ee9ed15c4 553 int16_t randomAngle;
homero 16:a32ee9ed15c4 554 if(beacon_illuminated_flag == 0) {
homero 16:a32ee9ed15c4 555 if(gv_IRDistances[0] < 100 || gv_IRDistances[1] < 100) {
homero 16:a32ee9ed15c4 556 randomAngle = rand()%90 - 135;
homero 16:a32ee9ed15c4 557 piswarm.stop();
homero 16:a32ee9ed15c4 558 wait_ms(100);
homero 16:a32ee9ed15c4 559 piswarm.backward(0.3);
homero 16:a32ee9ed15c4 560 wait_ms(200);
homero 16:a32ee9ed15c4 561 turnDegrees(randomAngle);
homero 16:a32ee9ed15c4 562 } else if (gv_IRDistances[6] < 100 || gv_IRDistances[7] < 100) {
homero 16:a32ee9ed15c4 563 randomAngle = rand()%90 + 45;
homero 16:a32ee9ed15c4 564 piswarm.stop();
homero 16:a32ee9ed15c4 565 wait_ms(100);
homero 16:a32ee9ed15c4 566 piswarm.backward(0.3);
homero 16:a32ee9ed15c4 567 wait_ms(200);
homero 16:a32ee9ed15c4 568 turnDegrees(randomAngle);
homero 16:a32ee9ed15c4 569 } else if ( distance_ultrasonic_sensor < 100) {
homero 16:a32ee9ed15c4 570 randomAngle = rand()%60 - 30;
homero 16:a32ee9ed15c4 571 piswarm.stop();
homero 16:a32ee9ed15c4 572 wait_ms(100);
homero 16:a32ee9ed15c4 573 piswarm.backward(0.3);
homero 16:a32ee9ed15c4 574 wait_ms(200);
homero 16:a32ee9ed15c4 575 turnDegrees(randomAngle);
homero 15:f7a8989a3cd3 576 }
homero 13:c18d82f62d38 577 }
homero 16:a32ee9ed15c4 578 piswarm.right_motor(0.3*r_mot_scaler*r_mot_scaler);
homero 16:a32ee9ed15c4 579 piswarm.left_motor(0.3*l_mot_scaler*l_mot_scaler);
homero 16:a32ee9ed15c4 580 wait_ms(500);
homero 16:a32ee9ed15c4 581 //Should be at beacon
homero 16:a32ee9ed15c4 582 } else {
homero 16:a32ee9ed15c4 583 piswarm.stop();
homero 16:a32ee9ed15c4 584 calculateNewHeading();
homero 16:a32ee9ed15c4 585 if(g_currentHeading > 5 || g_currentHeading < -5) {
homero 16:a32ee9ed15c4 586 turnDegrees(-g_currentHeading);
homero 15:f7a8989a3cd3 587 }
homero 15:f7a8989a3cd3 588
homero 16:a32ee9ed15c4 589 //If either of these flags is one then the beacon should be the home beacon so change to state 4.
homero 16:a32ee9ed15c4 590 if(return_flag == 1 || back_flag == 1) {
homero 16:a32ee9ed15c4 591 changeState(States::AT_HOME_BEACON);
re633 12:118f2b0ed8eb 592 } else {
homero 16:a32ee9ed15c4 593 changeState(States::AT_TARGET_BEACON);
re633 12:118f2b0ed8eb 594 }
re633 9:ef0907fda2f1 595 }
re633 9:ef0907fda2f1 596 }
homero 16:a32ee9ed15c4 597 //Else need to syncronise with beacon
homero 16:a32ee9ed15c4 598 } else {
homero 15:f7a8989a3cd3 599
homero 16:a32ee9ed15c4 600 while(beacon_syncronised_flag == 0) {
homero 15:f7a8989a3cd3 601
homero 16:a32ee9ed15c4 602 //Sychronise the ticker with the beacon
homero 16:a32ee9ed15c4 603 uint8_t testBefore = 0;
homero 16:a32ee9ed15c4 604 uint8_t testDuring = 0;
homero 16:a32ee9ed15c4 605 uint8_t testAfter = 0;
homero 16:a32ee9ed15c4 606 for(loopCounter = 0; loopCounter < 8; loopCounter++) {
homero 16:a32ee9ed15c4 607 if (gv_IRValDiffs[mod8((g_beaconOn - 1),IR_READ_PER_BEAC)][loopCounter] > BEACON_SUSPECTED) {
homero 16:a32ee9ed15c4 608 testBefore = 1;
homero 16:a32ee9ed15c4 609 }
homero 16:a32ee9ed15c4 610 if (gv_IRValDiffs[g_beaconOn][loopCounter] > BEACON_SUSPECTED) {
homero 16:a32ee9ed15c4 611 testDuring = 1;
homero 16:a32ee9ed15c4 612 }
homero 16:a32ee9ed15c4 613 if (gv_IRValDiffsTwo[mod8((g_beaconOn + 2),IR_READ_PER_BEAC)][loopCounter] > BEACON_SUSPECTED) {
homero 16:a32ee9ed15c4 614 testAfter = 1;
homero 16:a32ee9ed15c4 615 }
homero 16:a32ee9ed15c4 616 if (gv_IRValDiffsTwo[mod8((g_beaconOn + 2),IR_READ_PER_BEAC)][loopCounter] < -BEACON_SUSPECTED) {
homero 16:a32ee9ed15c4 617 testAfter = 2;
homero 16:a32ee9ed15c4 618 }
homero 16:a32ee9ed15c4 619 }
homero 15:f7a8989a3cd3 620
homero 16:a32ee9ed15c4 621 //Firstly if the beacon is not detected by any of the sensors then change state back to search
homero 16:a32ee9ed15c4 622 if(testBefore == 0 && testDuring == 0 && testAfter == 0) {
homero 16:a32ee9ed15c4 623 changeState(States::SEARCHING_FWD);
homero 16:a32ee9ed15c4 624 beacon_syncronised_flag = 1;//to exit while loop
homero 16:a32ee9ed15c4 625
homero 16:a32ee9ed15c4 626 //If the tick before g_beaconOn is detecting the change caused by the flash change the value of g_beaconOn
homero 16:a32ee9ed15c4 627 } else if(testBefore == 1) {
homero 16:a32ee9ed15c4 628 g_beaconOn = g_beaconOn - 1;
homero 16:a32ee9ed15c4 629
homero 16:a32ee9ed15c4 630 //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 10ms
homero 16:a32ee9ed15c4 631 } else if(testBefore == 0 && testDuring == 1 && testAfter == 1) {
homero 16:a32ee9ed15c4 632 ticker_25ms.detach();
homero 16:a32ee9ed15c4 633 tickChangeTimeout.attach_us(&atTimeout,10000);
homero 16:a32ee9ed15c4 634 wait(1);//Do not delete this wait
homero 15:f7a8989a3cd3 635
homero 16:a32ee9ed15c4 636 //If successful the set flag
homero 16:a32ee9ed15c4 637 } else if (testBefore == 0 && testDuring == 1 && testAfter == 2) {
homero 16:a32ee9ed15c4 638 beacon_syncronised_flag = 1;
homero 15:f7a8989a3cd3 639
homero 16:a32ee9ed15c4 640 //Error handle. If this happens stop the piswarm
homero 16:a32ee9ed15c4 641 } else {
homero 16:a32ee9ed15c4 642 piswarm.set_oled_colour(255,255,255);
homero 16:a32ee9ed15c4 643 piswarm.set_oleds(1,1,1,1,1,1,1,1,1,1);
homero 16:a32ee9ed15c4 644 piswarm.cls();
homero 16:a32ee9ed15c4 645 piswarm.printf("%d %d %d",testBefore, testDuring,testAfter);
homero 16:a32ee9ed15c4 646 piswarm.stop();
homero 16:a32ee9ed15c4 647 ticker_25ms.detach();
homero 16:a32ee9ed15c4 648 tickChangeTimeout.attach_us(&atTimeout,10000);
homero 16:a32ee9ed15c4 649 wait_ms(500);
homero 16:a32ee9ed15c4 650 beacon_syncronised_flag = 1;
homero 16:a32ee9ed15c4 651 changeState(States::SEARCHING_FWD);
re633 9:ef0907fda2f1 652 }
re633 9:ef0907fda2f1 653 }
homero 16:a32ee9ed15c4 654 }
homero 15:f7a8989a3cd3 655
homero 16:a32ee9ed15c4 656 //At target Beacon.
homero 16:a32ee9ed15c4 657 //Broadcast target beacon found and wait.
homero 16:a32ee9ed15c4 658 } else if (gv_state == States::AT_TARGET_BEACON) {
homero 15:f7a8989a3cd3 659
homero 16:a32ee9ed15c4 660 piswarm.stop();
homero 16:a32ee9ed15c4 661 piswarm.set_oled_colour(150,255,0);
homero 16:a32ee9ed15c4 662 piswarm.set_oleds(1,1,1,1,1,1,1,1,1,1);
homero 16:a32ee9ed15c4 663
homero 16:a32ee9ed15c4 664 //Detach tickers before broadcasting and waiting for reply
homero 16:a32ee9ed15c4 665 ticker_25ms.detach();
homero 16:a32ee9ed15c4 666 ticker_ultrasonic50ms.detach();
homero 16:a32ee9ed15c4 667
homero 16:a32ee9ed15c4 668 uint8_t const num_to_broadcast = 10;
homero 15:f7a8989a3cd3 669
homero 16:a32ee9ed15c4 670 while(back_flag == 0) {
homero 16:a32ee9ed15c4 671 if(broadcasted_flag == 1 && broadcasted_count < num_to_broadcast) {
homero 16:a32ee9ed15c4 672 broadcastTimeout.attach_us(&atBroadcastTimeout,100000);
homero 16:a32ee9ed15c4 673 broadcasted_flag = 0;
homero 13:c18d82f62d38 674 }
re633 12:118f2b0ed8eb 675 }
homero 16:a32ee9ed15c4 676
homero 16:a32ee9ed15c4 677 ticker_25ms.attach_us(&readIRs,25000);
homero 16:a32ee9ed15c4 678 ticker_ultrasonic50ms.attach_us(&get_ultrasonic_readings,50000);
homero 16:a32ee9ed15c4 679
homero 16:a32ee9ed15c4 680 //Return to beacon search state but now robot is lookling for the home beacon.
homero 16:a32ee9ed15c4 681 changeState(States::SEARCHING_FWD);
homero 16:a32ee9ed15c4 682
homero 16:a32ee9ed15c4 683 //Back at home area. Stop and wait.
homero 16:a32ee9ed15c4 684 } else if (gv_state == States::AT_HOME_BEACON) {
homero 16:a32ee9ed15c4 685 piswarm.stop();
homero 16:a32ee9ed15c4 686 piswarm.set_oleds(1,1,1,1,1,1,1,1,1,1);
homero 16:a32ee9ed15c4 687 piswarm.play_tune( "T180L8O5EERERCL4EGR<GR",22 );
homero 16:a32ee9ed15c4 688 while(1) {
homero 16:a32ee9ed15c4 689 piswarm.set_oled_colour(0,150,0);
homero 16:a32ee9ed15c4 690 wait_ms(200);
homero 16:a32ee9ed15c4 691 piswarm.set_oled_colour(150,0,0);
homero 16:a32ee9ed15c4 692 wait_ms(200);
homero 16:a32ee9ed15c4 693 piswarm.set_oled_colour(0,0,150);
homero 16:a32ee9ed15c4 694 wait_ms(200);
homero 16:a32ee9ed15c4 695 }
re633 9:ef0907fda2f1 696 }
homero 15:f7a8989a3cd3 697 }
jah128 7:d03e54d9eb1c 698 }
homero 16:a32ee9ed15c4 699
jah128 2:e806b595f9ce 700 /***************************************************************************************************************************************
jah128 2:e806b595f9ce 701 *
jah128 4:823174be9a6b 702 * Beyond this point, empty code blocks for optional functions is given
jah128 2:e806b595f9ce 703 *
homero 15:f7a8989a3cd3 704 * These may be left blank if not used, but should not be deleted
jah128 2:e806b595f9ce 705 *
jah128 2:e806b595f9ce 706 **************************************************************************************************************************************/
homero 15:f7a8989a3cd3 707
jah128 1:37502eb3b70f 708 // Communications
jah128 1:37502eb3b70f 709
jah128 1:37502eb3b70f 710 // 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 711 // If the communication stack is not being used, all radio data is sent to processRawRFData() instead
jah128 1:37502eb3b70f 712
homero 15:f7a8989a3cd3 713 void handleUserRFCommand(char sender, char broadcast_message, char request_response, char id, char is_command, char function, char * data, char length)
homero 15:f7a8989a3cd3 714 {
jah128 1:37502eb3b70f 715 // A 'user' RF Command has been received: write the code here to process it
jah128 1:37502eb3b70f 716 // sender = ID of the sender, range 0 to 31
jah128 1:37502eb3b70f 717 // broadcast_message = 1 is message sent to all robots, 0 otherwise
jah128 1:37502eb3b70f 718 // request_response = 1 if a response is expected, 0 otherwise
jah128 1:37502eb3b70f 719 // id = Message ID, range 0 to 255
jah128 1:37502eb3b70f 720 // 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 721 // function = The function identifier. Range 0 to 15
jah128 1:37502eb3b70f 722 // * data = Array containing extra data bytes
jah128 1:37502eb3b70f 723 // length = Length of extra data bytes held (range 0 to 57)
homero 15:f7a8989a3cd3 724
homero 15:f7a8989a3cd3 725
jah128 4:823174be9a6b 726 //Do something...
re633 12:118f2b0ed8eb 727 piswarm.cls();
re633 12:118f2b0ed8eb 728 piswarm.locate(0,0);
re633 12:118f2b0ed8eb 729 piswarm.printf("URF:%d",function);
re633 12:118f2b0ed8eb 730 if(length > 0) {
re633 12:118f2b0ed8eb 731 piswarm.locate(0,1);
homero 15:f7a8989a3cd3 732 piswarm.printf("%s",data);
re633 12:118f2b0ed8eb 733 }
re633 12:118f2b0ed8eb 734
homero 15:f7a8989a3cd3 735 if(function == 1) {
re633 12:118f2b0ed8eb 736 start_flag = 1;
re633 12:118f2b0ed8eb 737 }
re633 12:118f2b0ed8eb 738
homero 15:f7a8989a3cd3 739 if(function == 5) {
re633 12:118f2b0ed8eb 740 return_flag = 1;
re633 12:118f2b0ed8eb 741 }
homero 15:f7a8989a3cd3 742 if(function == 6) {
re633 12:118f2b0ed8eb 743 back_flag = 1;
re633 10:da62735d6df9 744 }
homero 15:f7a8989a3cd3 745 }
jah128 1:37502eb3b70f 746
re633 12:118f2b0ed8eb 747 // This function is used to send the RF message:
re633 12:118f2b0ed8eb 748 void broadcast_user_rf_command(int function, char * message, int length)
re633 12:118f2b0ed8eb 749 {
re633 12:118f2b0ed8eb 750 //This function augments the communications stack
re633 12:118f2b0ed8eb 751 //It sends a 'user' RF command to all members (ie target_id = 0)
re633 12:118f2b0ed8eb 752 //It sends a 'request', not a 'command', meaning it will still be handled if commands are disabled (RF_ALLOW_COMMANDS set to 0, recommended)
re633 12:118f2b0ed8eb 753 //It takes three inputs:
re633 12:118f2b0ed8eb 754 // * function (an integer from 0 to 15)
re633 12:118f2b0ed8eb 755 // * message (a char array)
re633 12:118f2b0ed8eb 756 // * length (length of message in bytes)
re633 12:118f2b0ed8eb 757 send_rf_message(0,48+(function % 16),message,length);
re633 12:118f2b0ed8eb 758 }
re633 12:118f2b0ed8eb 759
homero 15:f7a8989a3cd3 760 void handleUserRFResponse(char sender, char broadcast_message, char success, char id, char is_command, char function, char * data, char length)
homero 15:f7a8989a3cd3 761 {
jah128 1:37502eb3b70f 762 // A 'user' RF Response has been received: write the code here to process it
jah128 1:37502eb3b70f 763 // sender = ID of the sender, range 0 to 31
jah128 1:37502eb3b70f 764 // broadcast_message = 1 is message sent to all robots, 0 otherwise
jah128 1:37502eb3b70f 765 // success = 1 if operation successful, 0 otherwise
jah128 1:37502eb3b70f 766 // id = Message ID, range 0 to 255
jah128 1:37502eb3b70f 767 // 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 768 // function = The function identifier. Range 0 to 15
jah128 1:37502eb3b70f 769 // * data = Array containing extra data bytes
jah128 1:37502eb3b70f 770 // length = Length of extra data bytes held (range 0 to 57)
jah128 4:823174be9a6b 771
homero 15:f7a8989a3cd3 772 //Do something...
homero 15:f7a8989a3cd3 773 }
jah128 1:37502eb3b70f 774
homero 15:f7a8989a3cd3 775 void processRawRFData(char * rstring, char cCount)
homero 15:f7a8989a3cd3 776 {
jah128 1:37502eb3b70f 777 // A raw RF packet has been received: write the code here to process it
jah128 1:37502eb3b70f 778 // rstring = The received packet
jah128 1:37502eb3b70f 779 // cCount = Packet length
homero 15:f7a8989a3cd3 780
jah128 4:823174be9a6b 781 //Do something...
jah128 0:46cd1498a39a 782 }
jah128 0:46cd1498a39a 783
homero 15:f7a8989a3cd3 784 void switch_pressed()
homero 15:f7a8989a3cd3 785 {
jah128 1:37502eb3b70f 786 //Switch(es) pressed {1 = Center 2 = Right 4 = Left 8 = Down 16 = Up}
jah128 1:37502eb3b70f 787 char switches = piswarm.get_switches();
homero 15:f7a8989a3cd3 788
jah128 1:37502eb3b70f 789 //Do something...
homero 13:c18d82f62d38 790 }