Tobis Programm forked to not destroy your golden files

Dependencies:   mbed

Fork of Robocode by PES 2 - Gruppe 1

Committer:
cittecla
Date:
Sun May 14 09:34:37 2017 +0000
Revision:
123:de12131ff0b9
Parent:
122:8bfb39434fb7
Child:
124:6ef4d9ed2e28
Child:
132:8ae08f41bb43
move to brick by coordlist is now non static implemented

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cittecla 116:e03a3692cdf0 1 /**
cittecla 18:a82994e67297 2 * Movement function library
cittecla 18:a82994e67297 3 * Handels Movement of the Robot
cittecla 18:a82994e67297 4 **/
cittecla 39:92723f7ea54f 5
cittecla 39:92723f7ea54f 6 #include "Movement.h"
PESGruppe1 114:720dc5df42a5 7 #define OFFSET_GREIFER_TO_IRSENSOR 0.2 // Constant for distance between front IR Sensor and the postion where the Greifer is in grabbing Position
cittecla 109:d18a2beb9b9b 8 #define OFFSET_WHEELS 0.09 // Offset of the wheels from the max pos
cittecla 39:92723f7ea54f 9
cittecla 85:d8ea8a99fa3a 10 bool is_moving = false;
cittecla 85:d8ea8a99fa3a 11 float wanted_dist = 0;
cittecla 61:628f8a4e857c 12 bool is_turning = false;
cittecla 61:628f8a4e857c 13 float wanted_deg = 0;
cittecla 105:d489c2e8de35 14 bool direction = false;
PESGruppe1 114:720dc5df42a5 15 float restdegAfterstop = 0; // Variable for Rest degree we still have to cover after e.g. 1 brick found but its not really a brick so we turn further until e.g 60 degrees covered.
cittecla 122:8bfb39434fb7 16 float needed_heading = 0;
cittecla 122:8bfb39434fb7 17 float distance_to_next_coord = 0;
cittecla 122:8bfb39434fb7 18 float current_head = 0;
cittecla 123:de12131ff0b9 19 coordinates current_coordinates = {0};
cittecla 123:de12131ff0b9 20 coordinates next_coord = {0};
PESGruppe1 114:720dc5df42a5 21
cittecla 116:e03a3692cdf0 22 float TOLERANCE_BRICK_OR_OBSTACLE = 0.08f; // Variable for Brick detection it sets how much upper sensor and lower can differ that its still detected as an obstacle and not a brick
cittecla 105:d489c2e8de35 23
cittecla 63:b27aa01c2cf6 24 Timer t;
PESGruppe1 114:720dc5df42a5 25 Timer t8; // timer used for waiting enough distance measurements
cittecla 105:d489c2e8de35 26
cittecla 86:df8c869a5a52 27 int search_state = 0;
cittecla 120:cdf7a6751f9e 28 int coord_move_state = 0;
cittecla 120:cdf7a6751f9e 29 int list_step = 0;
cittecla 77:ff87a10c4baf 30
cittecla 94:0381e8b1beda 31 float left = 0;
cittecla 94:0381e8b1beda 32 float right = 0;
cittecla 94:0381e8b1beda 33
cittecla 94:0381e8b1beda 34 bool devider = true;
cittecla 94:0381e8b1beda 35
cittecla 61:628f8a4e857c 36
cittecla 52:56399c2f13cd 37 int moving()
cittecla 52:56399c2f13cd 38 {
cittecla 52:56399c2f13cd 39
cittecla 39:92723f7ea54f 40 return 0;
cittecla 52:56399c2f13cd 41 }
cittecla 52:56399c2f13cd 42
cittecla 106:02d3327bf76a 43 /**
cittecla 106:02d3327bf76a 44 * Stops current movement immediately
cittecla 106:02d3327bf76a 45 **/
cittecla 86:df8c869a5a52 46 void stop_move()
cittecla 86:df8c869a5a52 47 {
cittecla 86:df8c869a5a52 48 set_speed(0,0);
cittecla 86:df8c869a5a52 49 wanted_dist = 0;
cittecla 86:df8c869a5a52 50 is_moving = false;
cittecla 86:df8c869a5a52 51 }
cittecla 86:df8c869a5a52 52
cittecla 106:02d3327bf76a 53 /**
cittecla 106:02d3327bf76a 54 * Stops current turn immediately
cittecla 106:02d3327bf76a 55 **/
cittecla 86:df8c869a5a52 56 void stop_turn()
cittecla 86:df8c869a5a52 57 {
cittecla 86:df8c869a5a52 58 set_speed(0,0);
cittecla 86:df8c869a5a52 59 wanted_deg = 0;
cittecla 86:df8c869a5a52 60 is_turning = false;
cittecla 86:df8c869a5a52 61 }
cittecla 86:df8c869a5a52 62
cittecla 106:02d3327bf76a 63
cittecla 106:02d3327bf76a 64 /**
cittecla 106:02d3327bf76a 65 * move for wanted distance on circle with a given radius
cittecla 106:02d3327bf76a 66 * needs to be called until return < 0
cittecla 106:02d3327bf76a 67 * if calling distance not 0: distance and radius initilisation.
cittecla 106:02d3327bf76a 68 * by Claudio Citterio
cittecla 106:02d3327bf76a 69 **/
cittecla 105:d489c2e8de35 70 float move_for_distance_with_radius(float distance, float r)
cittecla 105:d489c2e8de35 71 {
cittecla 105:d489c2e8de35 72
cittecla 105:d489c2e8de35 73 if(distance != 0) {
cittecla 105:d489c2e8de35 74
cittecla 105:d489c2e8de35 75 is_moving = true;
cittecla 105:d489c2e8de35 76 wanted_dist = fabsf(distance);
cittecla 105:d489c2e8de35 77
cittecla 105:d489c2e8de35 78 float circumference = r*2*(float)M_PI;
cittecla 105:d489c2e8de35 79 float circumference_inner = ((r-(float)OFFSET_WHEELS)*2*(float)M_PI);
cittecla 105:d489c2e8de35 80 float circumference_outer = ((r+(float)OFFSET_WHEELS)*2*(float)M_PI);
cittecla 105:d489c2e8de35 81
cittecla 109:d18a2beb9b9b 82 float max_speed = 50;
cittecla 109:d18a2beb9b9b 83 float inner_speed = max_speed/circumference*circumference_inner;
cittecla 109:d18a2beb9b9b 84 float outer_speed = max_speed/circumference*circumference_outer;
cittecla 105:d489c2e8de35 85
cittecla 109:d18a2beb9b9b 86 //reduce outer speed to max speed
cittecla 109:d18a2beb9b9b 87 float multiplier = 1.0f/inner_speed*max_speed;
cittecla 109:d18a2beb9b9b 88 inner_speed *= multiplier;
cittecla 109:d18a2beb9b9b 89 outer_speed *= multiplier;
cittecla 105:d489c2e8de35 90
cittecla 109:d18a2beb9b9b 91 if(r < 0.21f) {
cittecla 113:c7afe49752b9 92 outer_speed *= 0.8f;
cittecla 113:c7afe49752b9 93 inner_speed *= 0.8f;
cittecla 109:d18a2beb9b9b 94 }
cittecla 105:d489c2e8de35 95
cittecla 108:02bc5b4e67b7 96 if(r != 0) {
cittecla 108:02bc5b4e67b7 97 //move with turn
cittecla 108:02bc5b4e67b7 98 if(distance > 0) { //move forward
cittecla 105:d489c2e8de35 99 direction = 1;
cittecla 105:d489c2e8de35 100 left = outer_speed;
cittecla 105:d489c2e8de35 101 right = inner_speed;
cittecla 105:d489c2e8de35 102 } else { //move backward
cittecla 105:d489c2e8de35 103 direction = 0;
cittecla 105:d489c2e8de35 104 left = -outer_speed;
cittecla 105:d489c2e8de35 105 right = -inner_speed;
cittecla 105:d489c2e8de35 106 }
cittecla 105:d489c2e8de35 107 } else {
cittecla 105:d489c2e8de35 108 //normal straight movement
cittecla 108:02bc5b4e67b7 109 printf("move straight\r\n");
cittecla 105:d489c2e8de35 110 if(distance > 0) { //move forward
cittecla 105:d489c2e8de35 111 direction = 1;
cittecla 109:d18a2beb9b9b 112 left = max_speed;
cittecla 109:d18a2beb9b9b 113 right = max_speed;
cittecla 105:d489c2e8de35 114 } else { //move backward
cittecla 105:d489c2e8de35 115 direction = 0;
cittecla 109:d18a2beb9b9b 116 left = -max_speed;
cittecla 109:d18a2beb9b9b 117 right = -max_speed;
cittecla 105:d489c2e8de35 118 }
cittecla 105:d489c2e8de35 119 }
cittecla 105:d489c2e8de35 120
cittecla 105:d489c2e8de35 121 set_speed(left, right);
cittecla 105:d489c2e8de35 122 devider = true;
cittecla 105:d489c2e8de35 123 t.reset();
cittecla 105:d489c2e8de35 124 t.start();
cittecla 105:d489c2e8de35 125 } else {
cittecla 105:d489c2e8de35 126 float speed_multiplier = 0.6f;
cittecla 105:d489c2e8de35 127 if(wanted_dist < 0.10f && devider == true) {
cittecla 105:d489c2e8de35 128 //printf("devided\r\n");
cittecla 105:d489c2e8de35 129 devider = false;
cittecla 105:d489c2e8de35 130 left = left * speed_multiplier;
cittecla 105:d489c2e8de35 131 right = right * speed_multiplier;
cittecla 105:d489c2e8de35 132 //printf("left: %f || right: %f\r\n", left, right);
cittecla 105:d489c2e8de35 133 set_speed(left, right);
cittecla 105:d489c2e8de35 134 }
cittecla 105:d489c2e8de35 135
cittecla 105:d489c2e8de35 136 float speed_left = get_speed_left();
cittecla 105:d489c2e8de35 137 float speed_right = get_speed_right();
cittecla 105:d489c2e8de35 138 wanted_dist -= (2*(float)wheel_r*(float)M_PI)/(2*M_PI) * t.read() * ((fabsf(speed_left)+fabsf(speed_right))/2) * 0.1f;
cittecla 105:d489c2e8de35 139 t.reset();
cittecla 105:d489c2e8de35 140
cittecla 105:d489c2e8de35 141 if(wanted_dist <= 0) { //distance covered, Stop function
cittecla 105:d489c2e8de35 142 set_speed(0,0);
cittecla 105:d489c2e8de35 143 is_moving = false;
cittecla 105:d489c2e8de35 144 t.stop();
cittecla 105:d489c2e8de35 145 }
cittecla 105:d489c2e8de35 146 }
cittecla 105:d489c2e8de35 147 printf("remaining distance to cover: %f\r\n", wanted_dist);
cittecla 105:d489c2e8de35 148 return wanted_dist;
cittecla 105:d489c2e8de35 149 }
cittecla 105:d489c2e8de35 150
cittecla 106:02d3327bf76a 151 /**
cittecla 106:02d3327bf76a 152 * move for wanted distance
cittecla 106:02d3327bf76a 153 * needs to be called until return < 0
cittecla 106:02d3327bf76a 154 * if calling distance not 0: distance initilisation.
cittecla 106:02d3327bf76a 155 * by Claudio Citterio
cittecla 106:02d3327bf76a 156 **/
cittecla 85:d8ea8a99fa3a 157 float move_for_distance(float distance)
cittecla 52:56399c2f13cd 158 {
cittecla 88:b89cace9329b 159 printf("move for distance\r\n");
cittecla 85:d8ea8a99fa3a 160 if(distance != 0) {
cittecla 52:56399c2f13cd 161
cittecla 85:d8ea8a99fa3a 162 is_moving = true;
cittecla 85:d8ea8a99fa3a 163
cittecla 105:d489c2e8de35 164 wanted_dist = fabsf(distance);
cittecla 85:d8ea8a99fa3a 165
cittecla 85:d8ea8a99fa3a 166 if(distance > 0) { //move forward
cittecla 85:d8ea8a99fa3a 167 direction = 1;
cittecla 85:d8ea8a99fa3a 168 left = 50.0f;
cittecla 85:d8ea8a99fa3a 169 right = 50.0f;
cittecla 85:d8ea8a99fa3a 170 } else { //move backward
cittecla 85:d8ea8a99fa3a 171 direction = 0;
cittecla 85:d8ea8a99fa3a 172 left = -50.0f;
cittecla 85:d8ea8a99fa3a 173 right = -50.0f;
cittecla 85:d8ea8a99fa3a 174 }
cittecla 88:b89cace9329b 175 printf("set speed %f\r\n", left);
cittecla 85:d8ea8a99fa3a 176 set_speed(left, right);
cittecla 94:0381e8b1beda 177 devider = true;
cittecla 85:d8ea8a99fa3a 178 t.reset();
cittecla 85:d8ea8a99fa3a 179 t.start();
cittecla 85:d8ea8a99fa3a 180
cittecla 85:d8ea8a99fa3a 181 } else {
cittecla 94:0381e8b1beda 182 float speed_multiplier = 0.6f;
cittecla 94:0381e8b1beda 183 if(wanted_dist < 0.10f && devider == true) {
cittecla 94:0381e8b1beda 184 //printf("devided\r\n");
cittecla 94:0381e8b1beda 185 devider = false;
aeschsim 99:78d87027c85b 186 left = left * speed_multiplier;
aeschsim 99:78d87027c85b 187 right = right * speed_multiplier;
aeschsim 99:78d87027c85b 188 //printf("left: %f || right: %f\r\n", left, right);
cittecla 94:0381e8b1beda 189 set_speed(left, right);
cittecla 94:0381e8b1beda 190 }
cittecla 85:d8ea8a99fa3a 191
cittecla 85:d8ea8a99fa3a 192 float speed_left = get_speed_left();
cittecla 94:0381e8b1beda 193 printf("speed left: %f\r\n", speed_left);
cittecla 89:7f9d6e641a01 194 wanted_dist -= (2*(float)wheel_r*(float)M_PI)/(2*M_PI) * t.read() * fabsf(speed_left)*0.1f;
cittecla 85:d8ea8a99fa3a 195 t.reset();
cittecla 85:d8ea8a99fa3a 196
cittecla 85:d8ea8a99fa3a 197 if(wanted_dist <= 0) { //distance covered, Stop function
cittecla 85:d8ea8a99fa3a 198 set_speed(0,0);
cittecla 85:d8ea8a99fa3a 199 is_moving = false;
cittecla 85:d8ea8a99fa3a 200 t.stop();
cittecla 85:d8ea8a99fa3a 201 }
cittecla 85:d8ea8a99fa3a 202 }
cittecla 88:b89cace9329b 203 printf("remaining distance to cover: %f\r\n", wanted_dist);
cittecla 85:d8ea8a99fa3a 204 return wanted_dist;
cittecla 52:56399c2f13cd 205 }
cittecla 52:56399c2f13cd 206
cittecla 106:02d3327bf76a 207 /**
cittecla 106:02d3327bf76a 208 * turn for wanted degree
cittecla 106:02d3327bf76a 209 * needs to be called until return < 0
cittecla 106:02d3327bf76a 210 * if deg not 0: turn initilisation.
cittecla 106:02d3327bf76a 211 * Claudio Citterio
cittecla 106:02d3327bf76a 212 **/
PESGruppe1 114:720dc5df42a5 213 float turn_for_deg(float deg, float multiplier)
cittecla 52:56399c2f13cd 214 {
cittecla 52:56399c2f13cd 215
cittecla 75:dba260cb5ae4 216 if(deg != 0) {
cittecla 61:628f8a4e857c 217
cittecla 61:628f8a4e857c 218 is_turning = true;
cittecla 94:0381e8b1beda 219 wanted_deg = fabsf(deg);
cittecla 61:628f8a4e857c 220
cittecla 61:628f8a4e857c 221 if(deg < 0) { // turn left
cittecla 61:628f8a4e857c 222 direction = 1;
PESGruppe1 114:720dc5df42a5 223 left = -20.0f*multiplier;
PESGruppe1 114:720dc5df42a5 224 right = 20.0f*multiplier;
cittecla 61:628f8a4e857c 225 } else { // turn right
cittecla 61:628f8a4e857c 226 direction = 0;
PESGruppe1 114:720dc5df42a5 227 left = 20.0f*multiplier;
PESGruppe1 114:720dc5df42a5 228 right = -20.0f*multiplier;
cittecla 61:628f8a4e857c 229 }
cittecla 61:628f8a4e857c 230 set_speed(left, right);
cittecla 94:0381e8b1beda 231 devider = true;
cittecla 71:ddf4eb5c3081 232 t.reset();
cittecla 61:628f8a4e857c 233 t.start();
cittecla 61:628f8a4e857c 234
cittecla 61:628f8a4e857c 235 } else {
cittecla 94:0381e8b1beda 236 float speed_multiplier = 0.6f;
cittecla 94:0381e8b1beda 237 if(wanted_deg < 10.0f && devider == true) {
cittecla 94:0381e8b1beda 238 devider = false;
aeschsim 99:78d87027c85b 239 left = left * speed_multiplier;
aeschsim 99:78d87027c85b 240 right = right * speed_multiplier;
cittecla 94:0381e8b1beda 241 set_speed(left, right);
cittecla 94:0381e8b1beda 242 }
cittecla 101:6b10685aa34d 243
cittecla 61:628f8a4e857c 244 float speed_left = get_speed_left();
cittecla 94:0381e8b1beda 245 wanted_deg -= 360/(2*circle_r*M_PI) * ((2*(float)wheel_r*(float)M_PI)/(2*M_PI) * t.read() * fabsf(speed_left)*0.1f);
cittecla 75:dba260cb5ae4 246 t.reset();
cittecla 75:dba260cb5ae4 247 if(wanted_deg <= 0) {
cittecla 61:628f8a4e857c 248 set_speed(0,0);
cittecla 61:628f8a4e857c 249 is_turning = false;
cittecla 61:628f8a4e857c 250 t.stop();
cittecla 61:628f8a4e857c 251 }
cittecla 61:628f8a4e857c 252 }
cittecla 94:0381e8b1beda 253 printf("remaining deg %f\r\n", wanted_deg);
cittecla 75:dba260cb5ae4 254 return (wanted_deg);
cittecla 52:56399c2f13cd 255 }
cittecla 52:56399c2f13cd 256
cittecla 106:02d3327bf76a 257 /** has errors
cittecla 106:02d3327bf76a 258 * moves to next coordinate from coordinate list
cittecla 106:02d3327bf76a 259 * by Claudio Citterio
cittecla 106:02d3327bf76a 260 **/
cittecla 122:8bfb39434fb7 261
cittecla 122:8bfb39434fb7 262 int move_to_brick_by_coordlist()
cittecla 120:cdf7a6751f9e 263 {
cittecla 120:cdf7a6751f9e 264 switch(coord_move_state) {
cittecla 120:cdf7a6751f9e 265 case 0:
cittecla 120:cdf7a6751f9e 266 // init move to brick by list
cittecla 120:cdf7a6751f9e 267 list_step = 1;
cittecla 52:56399c2f13cd 268
cittecla 120:cdf7a6751f9e 269 coord_move_state = 1;
cittecla 120:cdf7a6751f9e 270 break;
cittecla 120:cdf7a6751f9e 271 case 1:
cittecla 120:cdf7a6751f9e 272 // get positions, coords, heading and distances
cittecla 122:8bfb39434fb7 273 current_head = get_current_heading();
cittecla 123:de12131ff0b9 274 current_coordinates = get_current_coord();
cittecla 123:de12131ff0b9 275 position next_position = walkpath[list_step];
cittecla 123:de12131ff0b9 276 next_coord = pos_to_coord(next_position);
cittecla 120:cdf7a6751f9e 277
cittecla 120:cdf7a6751f9e 278 coord_move_state = 2;
cittecla 120:cdf7a6751f9e 279 break;
cittecla 120:cdf7a6751f9e 280 case 2:
cittecla 120:cdf7a6751f9e 281 // check if path is still possible with updated map or target reached
cittecla 120:cdf7a6751f9e 282
cittecla 123:de12131ff0b9 283 if(next_position.x == 0 && next_position.y == 0) {
cittecla 120:cdf7a6751f9e 284 // target reached
cittecla 120:cdf7a6751f9e 285 coord_move_state = 0;
cittecla 120:cdf7a6751f9e 286 return 47;
cittecla 120:cdf7a6751f9e 287 }
cittecla 123:de12131ff0b9 288 if(obstacle_list[next_position.x][next_position.y] != 0) {
cittecla 120:cdf7a6751f9e 289 // path obstructed
cittecla 120:cdf7a6751f9e 290 coord_move_state = 0;
cittecla 120:cdf7a6751f9e 291 return 35;
cittecla 120:cdf7a6751f9e 292 }
cittecla 123:de12131ff0b9 293 list_step += 1;
cittecla 120:cdf7a6751f9e 294 coord_move_state = 3;
cittecla 120:cdf7a6751f9e 295 break;
cittecla 120:cdf7a6751f9e 296 case 3:
cittecla 120:cdf7a6751f9e 297 // calc new headings
cittecla 123:de12131ff0b9 298 float x = next_coord.x - current_coordinates.x;
cittecla 123:de12131ff0b9 299 float y = next_coord.y - current_coordinates.y;
cittecla 123:de12131ff0b9 300 distance_to_next_coord = sqrt(x*x + y*y);
cittecla 123:de12131ff0b9 301
cittecla 123:de12131ff0b9 302 needed_heading = 90 + (atan(-y / x)/(float)M_PI * 180.0f)*-1.0f;
cittecla 123:de12131ff0b9 303 if (x < 0) needed_heading += 180;
cittecla 120:cdf7a6751f9e 304
cittecla 122:8bfb39434fb7 305 if(needed_heading != current_head) {
cittecla 120:cdf7a6751f9e 306 coord_move_state = 5;
cittecla 120:cdf7a6751f9e 307 } else {
cittecla 120:cdf7a6751f9e 308 coord_move_state = 8;
cittecla 120:cdf7a6751f9e 309 }
cittecla 123:de12131ff0b9 310 break;
cittecla 120:cdf7a6751f9e 311
cittecla 120:cdf7a6751f9e 312 case 5:
cittecla 120:cdf7a6751f9e 313 // turn init with new heading
cittecla 122:8bfb39434fb7 314 turn_for_deg(needed_heading-current_head,1);
cittecla 122:8bfb39434fb7 315 coord_move_state = 6;
cittecla 120:cdf7a6751f9e 316 break;
cittecla 120:cdf7a6751f9e 317 case 6:
cittecla 120:cdf7a6751f9e 318 //turn until new heading == heading
cittecla 122:8bfb39434fb7 319 if(turn_for_deg(0,1)<0) {
cittecla 122:8bfb39434fb7 320 stop_turn();
cittecla 122:8bfb39434fb7 321 coord_move_state = 8;
cittecla 122:8bfb39434fb7 322 }
cittecla 120:cdf7a6751f9e 323 break;
cittecla 120:cdf7a6751f9e 324
cittecla 120:cdf7a6751f9e 325 case 8:
cittecla 120:cdf7a6751f9e 326 // move init with distance
cittecla 122:8bfb39434fb7 327 move_for_distance(distance_to_next_coord);
cittecla 122:8bfb39434fb7 328 coord_move_state = 9;
cittecla 120:cdf7a6751f9e 329 break;
cittecla 120:cdf7a6751f9e 330 case 9:
cittecla 123:de12131ff0b9 331 // move until distance is covered
cittecla 122:8bfb39434fb7 332 if(move_for_distance(0)<0) {
cittecla 122:8bfb39434fb7 333 stop_move();
cittecla 123:de12131ff0b9 334 coord_move_state = 1;
cittecla 122:8bfb39434fb7 335 }
cittecla 122:8bfb39434fb7 336 break;
cittecla 61:628f8a4e857c 337 }
cittecla 122:8bfb39434fb7 338 return 45;
cittecla 122:8bfb39434fb7 339 }
cittecla 52:56399c2f13cd 340
cittecla 52:56399c2f13cd 341
cittecla 108:02bc5b4e67b7 342 /**
cittecla 106:02d3327bf76a 343 * this function searchs a nearby brick, moves towards it and grabbs it
cittecla 106:02d3327bf76a 344 * by Tobias Berger, state machine by Claudio Citterio
cittecla 106:02d3327bf76a 345 **/
cittecla 52:56399c2f13cd 346 int move_in_search_for_brick()
cittecla 52:56399c2f13cd 347 {
cittecla 109:d18a2beb9b9b 348 float upper = getDistanceIR(2); // get distance from upper max Sensor
cittecla 109:d18a2beb9b9b 349 float lower = getDistanceIR(3); // get distance from Lower max Sensor
cittecla 113:c7afe49752b9 350 //printf("Current Search State: >%d<\r\n",search_state);
cittecla 86:df8c869a5a52 351 switch (search_state) {
cittecla 86:df8c869a5a52 352 case 0: //first cycle right
cittecla 117:66d64dbd1b36 353 turn_for_deg(60.0f,0.8f); // call function and start turning
cittecla 86:df8c869a5a52 354 search_state = 1;
cittecla 86:df8c869a5a52 355 break;
cittecla 85:d8ea8a99fa3a 356
PESGruppe1 114:720dc5df42a5 357 case 1: // turn right and check for obstacles
cittecla 116:e03a3692cdf0 358 if((lower<0.45f)&&(lower>0.08f)) { // if something is in the range of 10 to 80cm at the lower Sensor
PESGruppe1 114:720dc5df42a5 359 if(fabsf((upper-lower))>TOLERANCE_BRICK_OR_OBSTACLE) { // and nothing is detected with the upper Sensor
cittecla 86:df8c869a5a52 360 stop_turn();
cittecla 116:e03a3692cdf0 361 t8.reset();
PESGruppe1 114:720dc5df42a5 362 t8.start(); // start timer for enough measurements
PESGruppe1 114:720dc5df42a5 363 restdegAfterstop = turn_for_deg(0,1); // get restdegrees from turn function. if a brick is falsly detected we turn restdegAfterstop to finisch search turn
PESGruppe1 114:720dc5df42a5 364 search_state = 2; // brick found
PESGruppe1 114:720dc5df42a5 365 printf("Brick first detetection lower: %f upper:%f",lower,upper);
cittecla 86:df8c869a5a52 366 }
cittecla 86:df8c869a5a52 367 } else {
cittecla 86:df8c869a5a52 368 search_state = 1; // go to same state
PESGruppe1 114:720dc5df42a5 369 if(turn_for_deg(0, 1) < 0) { // when first 60degree rotation finished
cittecla 86:df8c869a5a52 370 stop_turn();
PESGruppe1 114:720dc5df42a5 371 search_state = 4; // go to init turn other direction
PESGruppe1 114:720dc5df42a5 372 }
PESGruppe1 114:720dc5df42a5 373 }
PESGruppe1 114:720dc5df42a5 374 break;
PESGruppe1 114:720dc5df42a5 375
PESGruppe1 114:720dc5df42a5 376
PESGruppe1 114:720dc5df42a5 377
PESGruppe1 114:720dc5df42a5 378 case 2: // Check if Sensor after waiting still the same value
cittecla 116:e03a3692cdf0 379 if(t8.read() > 0.1f) {
cittecla 116:e03a3692cdf0 380 if((lower<0.45f)&&(lower>0.08f)) { // if something is in the range of 10 to 80cm at the lower Sensor
cittecla 116:e03a3692cdf0 381 if(fabsf((upper-lower))>TOLERANCE_BRICK_OR_OBSTACLE) { // and nothing is detected with the upper Sensor
cittecla 116:e03a3692cdf0 382 search_state = 10; // When still the same go to move forward
cittecla 116:e03a3692cdf0 383 } else {
cittecla 116:e03a3692cdf0 384 search_state=3; // When afterwait not the same go to continue turning
cittecla 116:e03a3692cdf0 385 }
PESGruppe1 114:720dc5df42a5 386 }
PESGruppe1 114:720dc5df42a5 387 }
PESGruppe1 114:720dc5df42a5 388 break;
PESGruppe1 114:720dc5df42a5 389
PESGruppe1 114:720dc5df42a5 390
PESGruppe1 114:720dc5df42a5 391 case 3: // init continue turning for restdeg
cittecla 117:66d64dbd1b36 392 turn_for_deg(restdegAfterstop,0.8f); // call function and start turning for restdegrees after stop
PESGruppe1 114:720dc5df42a5 393 search_state = 1; // go back to turn and search
PESGruppe1 114:720dc5df42a5 394 break;
PESGruppe1 114:720dc5df42a5 395
PESGruppe1 114:720dc5df42a5 396 case 4: // init turn left 120 deg
cittecla 117:66d64dbd1b36 397 turn_for_deg(-120.0f,0.8f);
PESGruppe1 114:720dc5df42a5 398 search_state = 5;
PESGruppe1 114:720dc5df42a5 399 break;
PESGruppe1 114:720dc5df42a5 400
PESGruppe1 114:720dc5df42a5 401 case 5: // turn and search opposite direction
PESGruppe1 114:720dc5df42a5 402 if((lower<0.45f)&&(lower>0.05f)) { // if something is in the range of 10 to 80cm at the lower Sensor
PESGruppe1 114:720dc5df42a5 403 if(fabsf((upper-lower))>TOLERANCE_BRICK_OR_OBSTACLE) { // and nothing is detected with the upper Sensor
PESGruppe1 114:720dc5df42a5 404 stop_turn();
cittecla 116:e03a3692cdf0 405 t8.reset();
PESGruppe1 114:720dc5df42a5 406 t8.start(); // start timer for enough measurements
PESGruppe1 114:720dc5df42a5 407 restdegAfterstop = turn_for_deg(0,1); // get restdegrees from turn function. if a brick is falsly detected we turn restdegAfterstop to finisch search turn
PESGruppe1 114:720dc5df42a5 408 search_state = 6; // brick found
PESGruppe1 114:720dc5df42a5 409 printf("Brick first detetection lower: %f upper:%f",lower,upper);
PESGruppe1 114:720dc5df42a5 410 }
PESGruppe1 114:720dc5df42a5 411 } else {
PESGruppe1 114:720dc5df42a5 412 search_state = 5; // go to same state
PESGruppe1 114:720dc5df42a5 413 if(turn_for_deg(0, 1) < 0) { // when 60degree rotation finished
PESGruppe1 114:720dc5df42a5 414 stop_turn();
PESGruppe1 114:720dc5df42a5 415 search_state = 20; // error go to default state, bc nothing found
cittecla 86:df8c869a5a52 416 }
cittecla 86:df8c869a5a52 417 }
cittecla 86:df8c869a5a52 418 break;
cittecla 85:d8ea8a99fa3a 419
cittecla 116:e03a3692cdf0 420 case 6: // Check if Sensor after waiting still detect brick
cittecla 116:e03a3692cdf0 421 if(t8.read() > 0.1f) {
cittecla 116:e03a3692cdf0 422 if((lower<0.45f)&&(lower>0.08f)) { // if something is in the range of 10 to 80cm at the lower Sensor
cittecla 116:e03a3692cdf0 423 if(fabsf((upper-lower))>TOLERANCE_BRICK_OR_OBSTACLE) { // and nothing is detected with the upper Sensor
cittecla 116:e03a3692cdf0 424 search_state = 10; // When still the same go to move forward
cittecla 116:e03a3692cdf0 425 } else {
cittecla 116:e03a3692cdf0 426 search_state=7; // When afterwait not the same go to continue turning
cittecla 116:e03a3692cdf0 427 }
cittecla 86:df8c869a5a52 428 }
cittecla 86:df8c869a5a52 429 }
cittecla 86:df8c869a5a52 430 break;
cittecla 52:56399c2f13cd 431
PESGruppe1 114:720dc5df42a5 432 case 7:// init continue turning for restdeg
cittecla 117:66d64dbd1b36 433 turn_for_deg(restdegAfterstop,0.8f); // call function and start turning for restdegrees after stop
PESGruppe1 114:720dc5df42a5 434 search_state = 5; // go back to turn and search
cittecla 101:6b10685aa34d 435 break;
cittecla 105:d489c2e8de35 436
PESGruppe1 114:720dc5df42a5 437 case 10: // first cycle move forward
cittecla 101:6b10685aa34d 438 float distance_to_Brick = lower-(float)OFFSET_GREIFER_TO_IRSENSOR; // calculate
cittecla 101:6b10685aa34d 439 move_for_distance(distance_to_Brick);
cittecla 101:6b10685aa34d 440 search_state =11;
cittecla 101:6b10685aa34d 441 break;
cittecla 101:6b10685aa34d 442
cittecla 101:6b10685aa34d 443 case 11: // move forward
cittecla 101:6b10685aa34d 444 if(move_for_distance(0) < 0) {
aeschsim 110:510927a51be8 445 //Safety Function:
cittecla 113:c7afe49752b9 446 if (getDistanceIR(2)<0.08f) {
aeschsim 110:510927a51be8 447 stop_move();
aeschsim 111:cd7b7b551616 448 //move_for_distance(-0.10f);
aeschsim 110:510927a51be8 449 search_state = 0;
aeschsim 110:510927a51be8 450 }
cittecla 101:6b10685aa34d 451 stop_move();
cittecla 101:6b10685aa34d 452 search_state = 12;
cittecla 101:6b10685aa34d 453 }
cittecla 101:6b10685aa34d 454 break;
cittecla 101:6b10685aa34d 455
cittecla 101:6b10685aa34d 456 case 12: // Grabbing
cittecla 94:0381e8b1beda 457 return 50; //main state machine set as Grabbing
cittecla 94:0381e8b1beda 458
cittecla 86:df8c869a5a52 459 default:
aeschsim 96:1c6867536350 460 printf("default State - move in search for brick\r\n");
cittecla 86:df8c869a5a52 461 // error
cittecla 86:df8c869a5a52 462 break;
PESGruppe1 74:d9c387b83196 463 }
cittecla 86:df8c869a5a52 464 return 47; //called until function is done
cittecla 52:56399c2f13cd 465 }
cittecla 52:56399c2f13cd 466
cittecla 39:92723f7ea54f 467
cittecla 39:92723f7ea54f 468
cittecla 39:92723f7ea54f 469
cittecla 39:92723f7ea54f 470
cittecla 39:92723f7ea54f 471
cittecla 39:92723f7ea54f 472
cittecla 39:92723f7ea54f 473