Tobis Programm forked to not destroy your golden files
Fork of Robocode by
source/Movement.cpp@111:cd7b7b551616, 2017-05-08 (annotated)
- Committer:
- aeschsim
- Date:
- Mon May 08 07:16:01 2017 +0000
- Revision:
- 111:cd7b7b551616
- Parent:
- 110:510927a51be8
- Child:
- 113:c7afe49752b9
add move for distance backwards in safety function (see last commit)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cittecla | 18:a82994e67297 | 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" |
cittecla | 105:d489c2e8de35 | 7 | #define OFFSET_GREIFER_TO_IRSENSOR 0.15 // 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; |
cittecla | 105:d489c2e8de35 | 15 | |
cittecla | 63:b27aa01c2cf6 | 16 | Timer t; |
cittecla | 105:d489c2e8de35 | 17 | |
cittecla | 86:df8c869a5a52 | 18 | int search_state = 0; |
cittecla | 77:ff87a10c4baf | 19 | |
cittecla | 94:0381e8b1beda | 20 | float left = 0; |
cittecla | 94:0381e8b1beda | 21 | float right = 0; |
cittecla | 94:0381e8b1beda | 22 | |
cittecla | 94:0381e8b1beda | 23 | bool devider = true; |
cittecla | 94:0381e8b1beda | 24 | |
cittecla | 61:628f8a4e857c | 25 | |
cittecla | 52:56399c2f13cd | 26 | int moving() |
cittecla | 52:56399c2f13cd | 27 | { |
cittecla | 52:56399c2f13cd | 28 | |
cittecla | 39:92723f7ea54f | 29 | return 0; |
cittecla | 52:56399c2f13cd | 30 | } |
cittecla | 52:56399c2f13cd | 31 | |
cittecla | 106:02d3327bf76a | 32 | /** |
cittecla | 106:02d3327bf76a | 33 | * Stops current movement immediately |
cittecla | 106:02d3327bf76a | 34 | **/ |
cittecla | 86:df8c869a5a52 | 35 | void stop_move() |
cittecla | 86:df8c869a5a52 | 36 | { |
cittecla | 86:df8c869a5a52 | 37 | set_speed(0,0); |
cittecla | 86:df8c869a5a52 | 38 | wanted_dist = 0; |
cittecla | 86:df8c869a5a52 | 39 | is_moving = false; |
cittecla | 86:df8c869a5a52 | 40 | } |
cittecla | 86:df8c869a5a52 | 41 | |
cittecla | 106:02d3327bf76a | 42 | /** |
cittecla | 106:02d3327bf76a | 43 | * Stops current turn immediately |
cittecla | 106:02d3327bf76a | 44 | **/ |
cittecla | 86:df8c869a5a52 | 45 | void stop_turn() |
cittecla | 86:df8c869a5a52 | 46 | { |
cittecla | 86:df8c869a5a52 | 47 | set_speed(0,0); |
cittecla | 86:df8c869a5a52 | 48 | wanted_deg = 0; |
cittecla | 86:df8c869a5a52 | 49 | is_turning = false; |
cittecla | 86:df8c869a5a52 | 50 | } |
cittecla | 86:df8c869a5a52 | 51 | |
cittecla | 106:02d3327bf76a | 52 | |
cittecla | 106:02d3327bf76a | 53 | /** |
cittecla | 106:02d3327bf76a | 54 | * move for wanted distance on circle with a given radius |
cittecla | 106:02d3327bf76a | 55 | * needs to be called until return < 0 |
cittecla | 106:02d3327bf76a | 56 | * if calling distance not 0: distance and radius initilisation. |
cittecla | 106:02d3327bf76a | 57 | * by Claudio Citterio |
cittecla | 106:02d3327bf76a | 58 | **/ |
cittecla | 105:d489c2e8de35 | 59 | float move_for_distance_with_radius(float distance, float r) |
cittecla | 105:d489c2e8de35 | 60 | { |
cittecla | 105:d489c2e8de35 | 61 | |
cittecla | 105:d489c2e8de35 | 62 | if(distance != 0) { |
cittecla | 105:d489c2e8de35 | 63 | |
cittecla | 105:d489c2e8de35 | 64 | is_moving = true; |
cittecla | 105:d489c2e8de35 | 65 | wanted_dist = fabsf(distance); |
cittecla | 105:d489c2e8de35 | 66 | |
cittecla | 105:d489c2e8de35 | 67 | float circumference = r*2*(float)M_PI; |
cittecla | 105:d489c2e8de35 | 68 | float circumference_inner = ((r-(float)OFFSET_WHEELS)*2*(float)M_PI); |
cittecla | 105:d489c2e8de35 | 69 | float circumference_outer = ((r+(float)OFFSET_WHEELS)*2*(float)M_PI); |
cittecla | 105:d489c2e8de35 | 70 | |
cittecla | 109:d18a2beb9b9b | 71 | float max_speed = 50; |
cittecla | 109:d18a2beb9b9b | 72 | float inner_speed = max_speed/circumference*circumference_inner; |
cittecla | 109:d18a2beb9b9b | 73 | float outer_speed = max_speed/circumference*circumference_outer; |
cittecla | 105:d489c2e8de35 | 74 | |
cittecla | 109:d18a2beb9b9b | 75 | //reduce outer speed to max speed |
cittecla | 109:d18a2beb9b9b | 76 | float multiplier = 1.0f/inner_speed*max_speed; |
cittecla | 109:d18a2beb9b9b | 77 | inner_speed *= multiplier; |
cittecla | 109:d18a2beb9b9b | 78 | outer_speed *= multiplier; |
cittecla | 105:d489c2e8de35 | 79 | |
cittecla | 109:d18a2beb9b9b | 80 | if(r < 0.21f) { |
cittecla | 109:d18a2beb9b9b | 81 | outer_speed *= 0.5f; |
cittecla | 109:d18a2beb9b9b | 82 | inner_speed *= 0.5f; |
cittecla | 109:d18a2beb9b9b | 83 | } |
cittecla | 105:d489c2e8de35 | 84 | |
cittecla | 108:02bc5b4e67b7 | 85 | if(r != 0) { |
cittecla | 108:02bc5b4e67b7 | 86 | //move with turn |
cittecla | 108:02bc5b4e67b7 | 87 | if(distance > 0) { //move forward |
cittecla | 105:d489c2e8de35 | 88 | direction = 1; |
cittecla | 105:d489c2e8de35 | 89 | left = outer_speed; |
cittecla | 105:d489c2e8de35 | 90 | right = inner_speed; |
cittecla | 105:d489c2e8de35 | 91 | } else { //move backward |
cittecla | 105:d489c2e8de35 | 92 | direction = 0; |
cittecla | 105:d489c2e8de35 | 93 | left = -outer_speed; |
cittecla | 105:d489c2e8de35 | 94 | right = -inner_speed; |
cittecla | 105:d489c2e8de35 | 95 | } |
cittecla | 105:d489c2e8de35 | 96 | } else { |
cittecla | 105:d489c2e8de35 | 97 | //normal straight movement |
cittecla | 108:02bc5b4e67b7 | 98 | printf("move straight\r\n"); |
cittecla | 105:d489c2e8de35 | 99 | if(distance > 0) { //move forward |
cittecla | 105:d489c2e8de35 | 100 | direction = 1; |
cittecla | 109:d18a2beb9b9b | 101 | left = max_speed; |
cittecla | 109:d18a2beb9b9b | 102 | right = max_speed; |
cittecla | 105:d489c2e8de35 | 103 | } else { //move backward |
cittecla | 105:d489c2e8de35 | 104 | direction = 0; |
cittecla | 109:d18a2beb9b9b | 105 | left = -max_speed; |
cittecla | 109:d18a2beb9b9b | 106 | right = -max_speed; |
cittecla | 105:d489c2e8de35 | 107 | } |
cittecla | 105:d489c2e8de35 | 108 | } |
cittecla | 105:d489c2e8de35 | 109 | |
cittecla | 105:d489c2e8de35 | 110 | set_speed(left, right); |
cittecla | 105:d489c2e8de35 | 111 | devider = true; |
cittecla | 105:d489c2e8de35 | 112 | t.reset(); |
cittecla | 105:d489c2e8de35 | 113 | t.start(); |
cittecla | 105:d489c2e8de35 | 114 | } else { |
cittecla | 105:d489c2e8de35 | 115 | float speed_multiplier = 0.6f; |
cittecla | 105:d489c2e8de35 | 116 | if(wanted_dist < 0.10f && devider == true) { |
cittecla | 105:d489c2e8de35 | 117 | //printf("devided\r\n"); |
cittecla | 105:d489c2e8de35 | 118 | devider = false; |
cittecla | 105:d489c2e8de35 | 119 | left = left * speed_multiplier; |
cittecla | 105:d489c2e8de35 | 120 | right = right * speed_multiplier; |
cittecla | 105:d489c2e8de35 | 121 | //printf("left: %f || right: %f\r\n", left, right); |
cittecla | 105:d489c2e8de35 | 122 | set_speed(left, right); |
cittecla | 105:d489c2e8de35 | 123 | } |
cittecla | 105:d489c2e8de35 | 124 | |
cittecla | 105:d489c2e8de35 | 125 | float speed_left = get_speed_left(); |
cittecla | 105:d489c2e8de35 | 126 | float speed_right = get_speed_right(); |
cittecla | 105:d489c2e8de35 | 127 | 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 | 128 | t.reset(); |
cittecla | 105:d489c2e8de35 | 129 | |
cittecla | 105:d489c2e8de35 | 130 | if(wanted_dist <= 0) { //distance covered, Stop function |
cittecla | 105:d489c2e8de35 | 131 | set_speed(0,0); |
cittecla | 105:d489c2e8de35 | 132 | is_moving = false; |
cittecla | 105:d489c2e8de35 | 133 | t.stop(); |
cittecla | 105:d489c2e8de35 | 134 | } |
cittecla | 105:d489c2e8de35 | 135 | } |
cittecla | 105:d489c2e8de35 | 136 | printf("remaining distance to cover: %f\r\n", wanted_dist); |
cittecla | 105:d489c2e8de35 | 137 | return wanted_dist; |
cittecla | 105:d489c2e8de35 | 138 | } |
cittecla | 105:d489c2e8de35 | 139 | |
cittecla | 106:02d3327bf76a | 140 | /** |
cittecla | 106:02d3327bf76a | 141 | * move for wanted distance |
cittecla | 106:02d3327bf76a | 142 | * needs to be called until return < 0 |
cittecla | 106:02d3327bf76a | 143 | * if calling distance not 0: distance initilisation. |
cittecla | 106:02d3327bf76a | 144 | * by Claudio Citterio |
cittecla | 106:02d3327bf76a | 145 | **/ |
cittecla | 85:d8ea8a99fa3a | 146 | float move_for_distance(float distance) |
cittecla | 52:56399c2f13cd | 147 | { |
cittecla | 88:b89cace9329b | 148 | printf("move for distance\r\n"); |
cittecla | 85:d8ea8a99fa3a | 149 | if(distance != 0) { |
cittecla | 52:56399c2f13cd | 150 | |
cittecla | 85:d8ea8a99fa3a | 151 | is_moving = true; |
cittecla | 85:d8ea8a99fa3a | 152 | |
cittecla | 105:d489c2e8de35 | 153 | wanted_dist = fabsf(distance); |
cittecla | 85:d8ea8a99fa3a | 154 | |
cittecla | 85:d8ea8a99fa3a | 155 | if(distance > 0) { //move forward |
cittecla | 85:d8ea8a99fa3a | 156 | direction = 1; |
cittecla | 85:d8ea8a99fa3a | 157 | left = 50.0f; |
cittecla | 85:d8ea8a99fa3a | 158 | right = 50.0f; |
cittecla | 85:d8ea8a99fa3a | 159 | } else { //move backward |
cittecla | 85:d8ea8a99fa3a | 160 | direction = 0; |
cittecla | 85:d8ea8a99fa3a | 161 | left = -50.0f; |
cittecla | 85:d8ea8a99fa3a | 162 | right = -50.0f; |
cittecla | 85:d8ea8a99fa3a | 163 | } |
cittecla | 88:b89cace9329b | 164 | printf("set speed %f\r\n", left); |
cittecla | 85:d8ea8a99fa3a | 165 | set_speed(left, right); |
cittecla | 94:0381e8b1beda | 166 | devider = true; |
cittecla | 85:d8ea8a99fa3a | 167 | t.reset(); |
cittecla | 85:d8ea8a99fa3a | 168 | t.start(); |
cittecla | 85:d8ea8a99fa3a | 169 | |
cittecla | 85:d8ea8a99fa3a | 170 | } else { |
cittecla | 94:0381e8b1beda | 171 | float speed_multiplier = 0.6f; |
cittecla | 94:0381e8b1beda | 172 | if(wanted_dist < 0.10f && devider == true) { |
cittecla | 94:0381e8b1beda | 173 | //printf("devided\r\n"); |
cittecla | 94:0381e8b1beda | 174 | devider = false; |
aeschsim | 99:78d87027c85b | 175 | left = left * speed_multiplier; |
aeschsim | 99:78d87027c85b | 176 | right = right * speed_multiplier; |
aeschsim | 99:78d87027c85b | 177 | //printf("left: %f || right: %f\r\n", left, right); |
cittecla | 94:0381e8b1beda | 178 | set_speed(left, right); |
cittecla | 94:0381e8b1beda | 179 | } |
cittecla | 85:d8ea8a99fa3a | 180 | |
cittecla | 85:d8ea8a99fa3a | 181 | float speed_left = get_speed_left(); |
cittecla | 94:0381e8b1beda | 182 | printf("speed left: %f\r\n", speed_left); |
cittecla | 89:7f9d6e641a01 | 183 | wanted_dist -= (2*(float)wheel_r*(float)M_PI)/(2*M_PI) * t.read() * fabsf(speed_left)*0.1f; |
cittecla | 85:d8ea8a99fa3a | 184 | t.reset(); |
cittecla | 85:d8ea8a99fa3a | 185 | |
cittecla | 85:d8ea8a99fa3a | 186 | if(wanted_dist <= 0) { //distance covered, Stop function |
cittecla | 85:d8ea8a99fa3a | 187 | set_speed(0,0); |
cittecla | 85:d8ea8a99fa3a | 188 | is_moving = false; |
cittecla | 85:d8ea8a99fa3a | 189 | t.stop(); |
cittecla | 85:d8ea8a99fa3a | 190 | } |
cittecla | 85:d8ea8a99fa3a | 191 | } |
cittecla | 88:b89cace9329b | 192 | printf("remaining distance to cover: %f\r\n", wanted_dist); |
cittecla | 85:d8ea8a99fa3a | 193 | return wanted_dist; |
cittecla | 52:56399c2f13cd | 194 | } |
cittecla | 52:56399c2f13cd | 195 | |
cittecla | 106:02d3327bf76a | 196 | /** |
cittecla | 106:02d3327bf76a | 197 | * turn for wanted degree |
cittecla | 106:02d3327bf76a | 198 | * needs to be called until return < 0 |
cittecla | 106:02d3327bf76a | 199 | * if deg not 0: turn initilisation. |
cittecla | 106:02d3327bf76a | 200 | * Claudio Citterio |
cittecla | 106:02d3327bf76a | 201 | **/ |
cittecla | 108:02bc5b4e67b7 | 202 | float turn_for_deg(float deg) |
cittecla | 52:56399c2f13cd | 203 | { |
cittecla | 52:56399c2f13cd | 204 | |
cittecla | 75:dba260cb5ae4 | 205 | if(deg != 0) { |
cittecla | 61:628f8a4e857c | 206 | |
cittecla | 61:628f8a4e857c | 207 | is_turning = true; |
cittecla | 94:0381e8b1beda | 208 | wanted_deg = fabsf(deg); |
cittecla | 61:628f8a4e857c | 209 | |
cittecla | 61:628f8a4e857c | 210 | if(deg < 0) { // turn left |
cittecla | 61:628f8a4e857c | 211 | direction = 1; |
cittecla | 94:0381e8b1beda | 212 | left = -30.0f; |
cittecla | 94:0381e8b1beda | 213 | right = 30.0f; |
cittecla | 61:628f8a4e857c | 214 | } else { // turn right |
cittecla | 61:628f8a4e857c | 215 | direction = 0; |
cittecla | 94:0381e8b1beda | 216 | left = 30.0f; |
cittecla | 94:0381e8b1beda | 217 | right = -30.0f; |
cittecla | 61:628f8a4e857c | 218 | } |
cittecla | 61:628f8a4e857c | 219 | set_speed(left, right); |
cittecla | 94:0381e8b1beda | 220 | devider = true; |
cittecla | 71:ddf4eb5c3081 | 221 | t.reset(); |
cittecla | 61:628f8a4e857c | 222 | t.start(); |
cittecla | 61:628f8a4e857c | 223 | |
cittecla | 61:628f8a4e857c | 224 | } else { |
cittecla | 94:0381e8b1beda | 225 | float speed_multiplier = 0.6f; |
cittecla | 94:0381e8b1beda | 226 | if(wanted_deg < 10.0f && devider == true) { |
cittecla | 94:0381e8b1beda | 227 | devider = false; |
aeschsim | 99:78d87027c85b | 228 | left = left * speed_multiplier; |
aeschsim | 99:78d87027c85b | 229 | right = right * speed_multiplier; |
cittecla | 94:0381e8b1beda | 230 | set_speed(left, right); |
cittecla | 94:0381e8b1beda | 231 | } |
cittecla | 101:6b10685aa34d | 232 | |
cittecla | 61:628f8a4e857c | 233 | float speed_left = get_speed_left(); |
cittecla | 94:0381e8b1beda | 234 | 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 | 235 | t.reset(); |
cittecla | 75:dba260cb5ae4 | 236 | if(wanted_deg <= 0) { |
cittecla | 61:628f8a4e857c | 237 | set_speed(0,0); |
cittecla | 61:628f8a4e857c | 238 | is_turning = false; |
cittecla | 61:628f8a4e857c | 239 | t.stop(); |
cittecla | 61:628f8a4e857c | 240 | } |
cittecla | 61:628f8a4e857c | 241 | } |
cittecla | 94:0381e8b1beda | 242 | printf("remaining deg %f\r\n", wanted_deg); |
cittecla | 75:dba260cb5ae4 | 243 | return (wanted_deg); |
cittecla | 52:56399c2f13cd | 244 | } |
cittecla | 52:56399c2f13cd | 245 | |
cittecla | 106:02d3327bf76a | 246 | /** has errors |
cittecla | 106:02d3327bf76a | 247 | * moves to next coordinate from coordinate list |
cittecla | 106:02d3327bf76a | 248 | * by Claudio Citterio |
cittecla | 106:02d3327bf76a | 249 | **/ |
cittecla | 52:56399c2f13cd | 250 | |
cittecla | 52:56399c2f13cd | 251 | int move_to_next_coord() |
cittecla | 52:56399c2f13cd | 252 | { |
cittecla | 52:56399c2f13cd | 253 | |
cittecla | 52:56399c2f13cd | 254 | float current_heading = get_current_heading(); |
cittecla | 53:453f24775644 | 255 | position current_pos = get_current_pos(); |
cittecla | 52:56399c2f13cd | 256 | position next_pos = get_next_pos(); |
cittecla | 61:628f8a4e857c | 257 | |
cittecla | 52:56399c2f13cd | 258 | float needed_heading = 0; |
cittecla | 52:56399c2f13cd | 259 | float distance = 0; |
cittecla | 61:628f8a4e857c | 260 | |
cittecla | 52:56399c2f13cd | 261 | // nord(-y) = 0 grad |
cittecla | 61:628f8a4e857c | 262 | if(current_pos.y > next_pos.y) { |
cittecla | 61:628f8a4e857c | 263 | if(current_pos.x > next_pos.x) needed_heading = 315; |
cittecla | 61:628f8a4e857c | 264 | distance = sqrt2; |
cittecla | 61:628f8a4e857c | 265 | if(current_pos.x == next_pos.x) needed_heading = 0; |
cittecla | 61:628f8a4e857c | 266 | distance = 1; |
cittecla | 61:628f8a4e857c | 267 | if(current_pos.x < next_pos.x) needed_heading = 45; |
cittecla | 61:628f8a4e857c | 268 | distance = sqrt2; |
cittecla | 61:628f8a4e857c | 269 | } |
cittecla | 61:628f8a4e857c | 270 | if(current_pos.y == next_pos.y) { |
cittecla | 61:628f8a4e857c | 271 | if(current_pos.x > next_pos.x) needed_heading = 270; |
cittecla | 61:628f8a4e857c | 272 | distance = 1; |
cittecla | 53:453f24775644 | 273 | if(current_pos.x == next_pos.x) //error same position; |
cittecla | 61:628f8a4e857c | 274 | if(current_pos.x < next_pos.x) needed_heading = 90; |
cittecla | 61:628f8a4e857c | 275 | distance = 1; |
cittecla | 61:628f8a4e857c | 276 | } |
cittecla | 61:628f8a4e857c | 277 | if(current_pos.y < next_pos.y) { |
cittecla | 61:628f8a4e857c | 278 | if(current_pos.x > next_pos.x) needed_heading = 225; |
cittecla | 61:628f8a4e857c | 279 | distance = sqrt2; |
cittecla | 61:628f8a4e857c | 280 | if(current_pos.x == next_pos.x) needed_heading = 180; |
cittecla | 61:628f8a4e857c | 281 | distance = 1; |
cittecla | 61:628f8a4e857c | 282 | if(current_pos.x < next_pos.x) needed_heading = 135; |
cittecla | 61:628f8a4e857c | 283 | distance = sqrt2; |
cittecla | 61:628f8a4e857c | 284 | } |
cittecla | 52:56399c2f13cd | 285 | |
cittecla | 52:56399c2f13cd | 286 | if(needed_heading != current_heading) { |
cittecla | 53:453f24775644 | 287 | turn_for_deg((needed_heading-current_heading)); |
cittecla | 61:628f8a4e857c | 288 | } else { |
cittecla | 85:d8ea8a99fa3a | 289 | move_for_distance(distance); |
cittecla | 39:92723f7ea54f | 290 | } |
cittecla | 39:92723f7ea54f | 291 | return 0; |
cittecla | 52:56399c2f13cd | 292 | } |
cittecla | 52:56399c2f13cd | 293 | |
cittecla | 108:02bc5b4e67b7 | 294 | /** |
cittecla | 106:02d3327bf76a | 295 | * this function searchs a nearby brick, moves towards it and grabbs it |
cittecla | 106:02d3327bf76a | 296 | * by Tobias Berger, state machine by Claudio Citterio |
cittecla | 106:02d3327bf76a | 297 | **/ |
cittecla | 52:56399c2f13cd | 298 | int move_in_search_for_brick() |
cittecla | 52:56399c2f13cd | 299 | { |
cittecla | 109:d18a2beb9b9b | 300 | float upper = getDistanceIR(2); // get distance from upper max Sensor |
cittecla | 109:d18a2beb9b9b | 301 | float lower = getDistanceIR(3); // get distance from Lower max Sensor |
aeschsim | 96:1c6867536350 | 302 | printf("Current Search State: >%d<\r\n",search_state); |
cittecla | 86:df8c869a5a52 | 303 | switch (search_state) { |
cittecla | 86:df8c869a5a52 | 304 | case 0: //first cycle right |
cittecla | 86:df8c869a5a52 | 305 | turn_for_deg(60.0f); // call function and start turning |
cittecla | 86:df8c869a5a52 | 306 | search_state = 1; |
cittecla | 86:df8c869a5a52 | 307 | break; |
cittecla | 85:d8ea8a99fa3a | 308 | |
cittecla | 86:df8c869a5a52 | 309 | case 1: // turn right 60 deg |
cittecla | 101:6b10685aa34d | 310 | if((lower<0.75f)&&(lower>0.05f)) { // if something is in the range of 10 to 80cm at the lower Sensor |
cittecla | 86:df8c869a5a52 | 311 | if(fabsf((upper-lower))>0.02f) { // and nothing is detected with the upper Sensor |
cittecla | 86:df8c869a5a52 | 312 | stop_turn(); |
cittecla | 101:6b10685aa34d | 313 | search_state = 5; //brick found |
aeschsim | 99:78d87027c85b | 314 | printf("Brick found lower: %f upper:%f",lower,upper); |
cittecla | 86:df8c869a5a52 | 315 | } |
cittecla | 86:df8c869a5a52 | 316 | } else { |
cittecla | 86:df8c869a5a52 | 317 | search_state = 1; // go to same state |
cittecla | 86:df8c869a5a52 | 318 | if(turn_for_deg(0) < 0) { |
cittecla | 86:df8c869a5a52 | 319 | stop_turn(); |
cittecla | 86:df8c869a5a52 | 320 | search_state = 2; |
cittecla | 86:df8c869a5a52 | 321 | } |
cittecla | 86:df8c869a5a52 | 322 | } |
cittecla | 86:df8c869a5a52 | 323 | break; |
cittecla | 85:d8ea8a99fa3a | 324 | |
cittecla | 86:df8c869a5a52 | 325 | case 2: // first cycle left |
cittecla | 86:df8c869a5a52 | 326 | turn_for_deg(-120.0f); |
cittecla | 86:df8c869a5a52 | 327 | search_state = 3; |
cittecla | 86:df8c869a5a52 | 328 | break; |
cittecla | 85:d8ea8a99fa3a | 329 | |
cittecla | 86:df8c869a5a52 | 330 | case 3: // turn left 120 deg |
cittecla | 101:6b10685aa34d | 331 | if((lower<0.75f)&&(lower>0.1f)) { // if something is in the range of 10 to 75cm at the lower Sensor |
cittecla | 86:df8c869a5a52 | 332 | if(fabsf((upper-lower))>0.02f) { // and nothing is detected with the upper Sensor |
cittecla | 86:df8c869a5a52 | 333 | stop_turn(); |
cittecla | 104:7bc5eb2b4199 | 334 | search_state = 10; //brick found |
cittecla | 101:6b10685aa34d | 335 | printf("Brick found lower: %f upper:%f",lower,upper); |
cittecla | 86:df8c869a5a52 | 336 | } |
cittecla | 86:df8c869a5a52 | 337 | } else { |
cittecla | 86:df8c869a5a52 | 338 | search_state = 3; // go to same state |
cittecla | 86:df8c869a5a52 | 339 | if(turn_for_deg(0) < 0) { |
cittecla | 86:df8c869a5a52 | 340 | stop_turn(); |
cittecla | 101:6b10685aa34d | 341 | search_state = 20; // error |
cittecla | 86:df8c869a5a52 | 342 | } |
cittecla | 86:df8c869a5a52 | 343 | } |
cittecla | 86:df8c869a5a52 | 344 | break; |
cittecla | 52:56399c2f13cd | 345 | |
cittecla | 101:6b10685aa34d | 346 | case 5: // turn back left |
cittecla | 101:6b10685aa34d | 347 | turn_for_deg(-10.0f); |
cittecla | 101:6b10685aa34d | 348 | search_state = 8; |
cittecla | 101:6b10685aa34d | 349 | break; |
cittecla | 105:d489c2e8de35 | 350 | |
cittecla | 101:6b10685aa34d | 351 | case 8: // turn back right continue |
cittecla | 101:6b10685aa34d | 352 | if((lower<0.75f)&&(lower>0.1f)) { // if something is in the range of 10 to 75cm at the lower Sensor |
cittecla | 101:6b10685aa34d | 353 | if(fabsf((upper-lower))>0.02f) { // and nothing is detected with the upper Sensor |
cittecla | 101:6b10685aa34d | 354 | stop_turn(); |
cittecla | 101:6b10685aa34d | 355 | search_state = 10; //brick found |
cittecla | 101:6b10685aa34d | 356 | } |
cittecla | 101:6b10685aa34d | 357 | } else { |
cittecla | 104:7bc5eb2b4199 | 358 | search_state = 8; // go to same state |
cittecla | 101:6b10685aa34d | 359 | if(turn_for_deg(0) < 0) { |
cittecla | 101:6b10685aa34d | 360 | stop_turn(); |
cittecla | 101:6b10685aa34d | 361 | search_state = 20; // error |
cittecla | 101:6b10685aa34d | 362 | } |
cittecla | 86:df8c869a5a52 | 363 | } |
cittecla | 86:df8c869a5a52 | 364 | break; |
cittecla | 86:df8c869a5a52 | 365 | |
cittecla | 101:6b10685aa34d | 366 | |
cittecla | 101:6b10685aa34d | 367 | case 10: // first cycle move |
cittecla | 101:6b10685aa34d | 368 | float distance_to_Brick = lower-(float)OFFSET_GREIFER_TO_IRSENSOR; // calculate |
cittecla | 101:6b10685aa34d | 369 | move_for_distance(distance_to_Brick); |
cittecla | 101:6b10685aa34d | 370 | search_state =11; |
cittecla | 101:6b10685aa34d | 371 | break; |
cittecla | 101:6b10685aa34d | 372 | |
cittecla | 101:6b10685aa34d | 373 | case 11: // move forward |
cittecla | 101:6b10685aa34d | 374 | if(move_for_distance(0) < 0) { |
aeschsim | 110:510927a51be8 | 375 | //Safety Function: |
aeschsim | 110:510927a51be8 | 376 | if (getDistanceIR(2)<0.05f) { |
aeschsim | 110:510927a51be8 | 377 | stop_move(); |
aeschsim | 111:cd7b7b551616 | 378 | //move_for_distance(-0.10f); |
aeschsim | 110:510927a51be8 | 379 | search_state = 0; |
aeschsim | 110:510927a51be8 | 380 | } |
cittecla | 101:6b10685aa34d | 381 | stop_move(); |
cittecla | 101:6b10685aa34d | 382 | search_state = 12; |
cittecla | 101:6b10685aa34d | 383 | } |
cittecla | 101:6b10685aa34d | 384 | break; |
cittecla | 101:6b10685aa34d | 385 | |
cittecla | 101:6b10685aa34d | 386 | case 12: // Grabbing |
cittecla | 94:0381e8b1beda | 387 | return 50; //main state machine set as Grabbing |
cittecla | 94:0381e8b1beda | 388 | |
cittecla | 86:df8c869a5a52 | 389 | default: |
aeschsim | 96:1c6867536350 | 390 | printf("default State - move in search for brick\r\n"); |
cittecla | 86:df8c869a5a52 | 391 | // error |
cittecla | 86:df8c869a5a52 | 392 | break; |
PESGruppe1 | 74:d9c387b83196 | 393 | } |
cittecla | 86:df8c869a5a52 | 394 | return 47; //called until function is done |
cittecla | 52:56399c2f13cd | 395 | } |
cittecla | 52:56399c2f13cd | 396 | |
cittecla | 39:92723f7ea54f | 397 | |
cittecla | 39:92723f7ea54f | 398 | |
cittecla | 39:92723f7ea54f | 399 | |
cittecla | 39:92723f7ea54f | 400 | |
cittecla | 39:92723f7ea54f | 401 | |
cittecla | 39:92723f7ea54f | 402 | |
cittecla | 39:92723f7ea54f | 403 |