Tobis Programm forked to not destroy your golden files
Fork of Robocode by
source/Movement.cpp@94:0381e8b1beda, 2017-05-02 (annotated)
- Committer:
- cittecla
- Date:
- Tue May 02 15:29:50 2017 +0000
- Revision:
- 94:0381e8b1beda
- Parent:
- 92:b2f4ed4cb1e5
- Child:
- 96:1c6867536350
changed something in move for brick
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 | 86:df8c869a5a52 | 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 | 39:92723f7ea54f | 8 | |
cittecla | 85:d8ea8a99fa3a | 9 | bool is_moving = false; |
cittecla | 85:d8ea8a99fa3a | 10 | float wanted_dist = 0; |
cittecla | 39:92723f7ea54f | 11 | |
cittecla | 61:628f8a4e857c | 12 | bool is_turning = false; |
cittecla | 61:628f8a4e857c | 13 | bool direction = false; |
cittecla | 61:628f8a4e857c | 14 | float wanted_deg = 0; |
cittecla | 63:b27aa01c2cf6 | 15 | Timer t; |
cittecla | 61:628f8a4e857c | 16 | float previous_t = 0; |
cittecla | 86:df8c869a5a52 | 17 | int search_state = 0; |
cittecla | 77:ff87a10c4baf | 18 | float restdeg = 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 | 86:df8c869a5a52 | 32 | void stop_move() |
cittecla | 86:df8c869a5a52 | 33 | { |
cittecla | 86:df8c869a5a52 | 34 | set_speed(0,0); |
cittecla | 86:df8c869a5a52 | 35 | wanted_dist = 0; |
cittecla | 86:df8c869a5a52 | 36 | is_moving = false; |
cittecla | 86:df8c869a5a52 | 37 | } |
cittecla | 86:df8c869a5a52 | 38 | |
cittecla | 86:df8c869a5a52 | 39 | void stop_turn() |
cittecla | 86:df8c869a5a52 | 40 | { |
cittecla | 86:df8c869a5a52 | 41 | set_speed(0,0); |
cittecla | 86:df8c869a5a52 | 42 | wanted_deg = 0; |
cittecla | 86:df8c869a5a52 | 43 | is_turning = false; |
cittecla | 86:df8c869a5a52 | 44 | } |
cittecla | 86:df8c869a5a52 | 45 | |
cittecla | 85:d8ea8a99fa3a | 46 | float move_for_distance(float distance) |
cittecla | 52:56399c2f13cd | 47 | { |
cittecla | 88:b89cace9329b | 48 | printf("move for distance\r\n"); |
cittecla | 85:d8ea8a99fa3a | 49 | if(distance != 0) { |
cittecla | 52:56399c2f13cd | 50 | |
cittecla | 85:d8ea8a99fa3a | 51 | is_moving = true; |
cittecla | 85:d8ea8a99fa3a | 52 | |
cittecla | 85:d8ea8a99fa3a | 53 | wanted_dist = sqrt(distance*distance); |
cittecla | 85:d8ea8a99fa3a | 54 | |
cittecla | 85:d8ea8a99fa3a | 55 | if(distance > 0) { //move forward |
cittecla | 85:d8ea8a99fa3a | 56 | direction = 1; |
cittecla | 85:d8ea8a99fa3a | 57 | left = 50.0f; |
cittecla | 85:d8ea8a99fa3a | 58 | right = 50.0f; |
cittecla | 85:d8ea8a99fa3a | 59 | } else { //move backward |
cittecla | 85:d8ea8a99fa3a | 60 | direction = 0; |
cittecla | 85:d8ea8a99fa3a | 61 | left = -50.0f; |
cittecla | 85:d8ea8a99fa3a | 62 | right = -50.0f; |
cittecla | 85:d8ea8a99fa3a | 63 | } |
cittecla | 88:b89cace9329b | 64 | printf("set speed %f\r\n", left); |
cittecla | 85:d8ea8a99fa3a | 65 | set_speed(left, right); |
cittecla | 94:0381e8b1beda | 66 | devider = true; |
cittecla | 85:d8ea8a99fa3a | 67 | t.reset(); |
cittecla | 85:d8ea8a99fa3a | 68 | t.start(); |
cittecla | 85:d8ea8a99fa3a | 69 | |
cittecla | 85:d8ea8a99fa3a | 70 | } else { |
cittecla | 94:0381e8b1beda | 71 | float speed_multiplier = 0.6f; |
cittecla | 94:0381e8b1beda | 72 | if(wanted_dist < 0.10f && devider == true) { |
cittecla | 94:0381e8b1beda | 73 | //printf("devided\r\n"); |
cittecla | 94:0381e8b1beda | 74 | devider = false; |
cittecla | 94:0381e8b1beda | 75 | if(direction) { //move forward |
cittecla | 94:0381e8b1beda | 76 | left = left * speed_multiplier; |
cittecla | 94:0381e8b1beda | 77 | right = right * speed_multiplier; |
cittecla | 94:0381e8b1beda | 78 | //printf("left: %f || right: %f\r\n", left, right); |
cittecla | 94:0381e8b1beda | 79 | } |
cittecla | 94:0381e8b1beda | 80 | set_speed(left, right); |
cittecla | 94:0381e8b1beda | 81 | } |
cittecla | 85:d8ea8a99fa3a | 82 | |
cittecla | 85:d8ea8a99fa3a | 83 | float speed_left = get_speed_left(); |
cittecla | 94:0381e8b1beda | 84 | printf("speed left: %f\r\n", speed_left); |
cittecla | 89:7f9d6e641a01 | 85 | wanted_dist -= (2*(float)wheel_r*(float)M_PI)/(2*M_PI) * t.read() * fabsf(speed_left)*0.1f; |
cittecla | 85:d8ea8a99fa3a | 86 | t.reset(); |
cittecla | 85:d8ea8a99fa3a | 87 | |
cittecla | 85:d8ea8a99fa3a | 88 | if(wanted_dist <= 0) { //distance covered, Stop function |
cittecla | 85:d8ea8a99fa3a | 89 | set_speed(0,0); |
cittecla | 85:d8ea8a99fa3a | 90 | is_moving = false; |
cittecla | 85:d8ea8a99fa3a | 91 | t.stop(); |
cittecla | 85:d8ea8a99fa3a | 92 | } |
cittecla | 85:d8ea8a99fa3a | 93 | } |
cittecla | 88:b89cace9329b | 94 | printf("remaining distance to cover: %f\r\n", wanted_dist); |
cittecla | 85:d8ea8a99fa3a | 95 | return wanted_dist; |
cittecla | 52:56399c2f13cd | 96 | } |
cittecla | 52:56399c2f13cd | 97 | |
cittecla | 85:d8ea8a99fa3a | 98 | float turn_for_deg(float deg) //if deg not 0 equals initilisation. |
cittecla | 52:56399c2f13cd | 99 | { |
cittecla | 52:56399c2f13cd | 100 | |
cittecla | 75:dba260cb5ae4 | 101 | if(deg != 0) { |
cittecla | 61:628f8a4e857c | 102 | |
cittecla | 61:628f8a4e857c | 103 | is_turning = true; |
cittecla | 94:0381e8b1beda | 104 | wanted_deg = fabsf(deg); |
cittecla | 61:628f8a4e857c | 105 | |
cittecla | 61:628f8a4e857c | 106 | if(deg < 0) { // turn left |
cittecla | 61:628f8a4e857c | 107 | direction = 1; |
cittecla | 94:0381e8b1beda | 108 | left = -30.0f; |
cittecla | 94:0381e8b1beda | 109 | right = 30.0f; |
cittecla | 61:628f8a4e857c | 110 | } else { // turn right |
cittecla | 61:628f8a4e857c | 111 | direction = 0; |
cittecla | 94:0381e8b1beda | 112 | left = 30.0f; |
cittecla | 94:0381e8b1beda | 113 | right = -30.0f; |
cittecla | 61:628f8a4e857c | 114 | } |
cittecla | 61:628f8a4e857c | 115 | set_speed(left, right); |
cittecla | 94:0381e8b1beda | 116 | devider = true; |
cittecla | 71:ddf4eb5c3081 | 117 | t.reset(); |
cittecla | 61:628f8a4e857c | 118 | t.start(); |
cittecla | 61:628f8a4e857c | 119 | |
cittecla | 61:628f8a4e857c | 120 | } else { |
cittecla | 94:0381e8b1beda | 121 | float speed_multiplier = 0.6f; |
cittecla | 94:0381e8b1beda | 122 | if(wanted_deg < 10.0f && devider == true) { |
cittecla | 94:0381e8b1beda | 123 | devider = false; |
cittecla | 94:0381e8b1beda | 124 | if(direction) { //move forward |
cittecla | 94:0381e8b1beda | 125 | left = left * speed_multiplier; |
cittecla | 94:0381e8b1beda | 126 | right = right * speed_multiplier; |
cittecla | 94:0381e8b1beda | 127 | } |
cittecla | 94:0381e8b1beda | 128 | set_speed(left, right); |
cittecla | 94:0381e8b1beda | 129 | } |
cittecla | 94:0381e8b1beda | 130 | |
cittecla | 61:628f8a4e857c | 131 | float speed_left = get_speed_left(); |
cittecla | 94:0381e8b1beda | 132 | 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 | 133 | t.reset(); |
cittecla | 75:dba260cb5ae4 | 134 | if(wanted_deg <= 0) { |
cittecla | 61:628f8a4e857c | 135 | set_speed(0,0); |
cittecla | 61:628f8a4e857c | 136 | is_turning = false; |
cittecla | 61:628f8a4e857c | 137 | t.stop(); |
cittecla | 61:628f8a4e857c | 138 | } |
cittecla | 61:628f8a4e857c | 139 | } |
cittecla | 94:0381e8b1beda | 140 | printf("remaining deg %f\r\n", wanted_deg); |
cittecla | 75:dba260cb5ae4 | 141 | return (wanted_deg); |
cittecla | 52:56399c2f13cd | 142 | } |
cittecla | 52:56399c2f13cd | 143 | |
cittecla | 52:56399c2f13cd | 144 | |
cittecla | 52:56399c2f13cd | 145 | int move_to_next_coord() |
cittecla | 52:56399c2f13cd | 146 | { |
cittecla | 52:56399c2f13cd | 147 | |
cittecla | 52:56399c2f13cd | 148 | float current_heading = get_current_heading(); |
cittecla | 53:453f24775644 | 149 | position current_pos = get_current_pos(); |
cittecla | 52:56399c2f13cd | 150 | position next_pos = get_next_pos(); |
cittecla | 61:628f8a4e857c | 151 | |
cittecla | 52:56399c2f13cd | 152 | float needed_heading = 0; |
cittecla | 52:56399c2f13cd | 153 | float distance = 0; |
cittecla | 61:628f8a4e857c | 154 | |
cittecla | 52:56399c2f13cd | 155 | // nord(-y) = 0 grad |
cittecla | 61:628f8a4e857c | 156 | if(current_pos.y > next_pos.y) { |
cittecla | 61:628f8a4e857c | 157 | if(current_pos.x > next_pos.x) needed_heading = 315; |
cittecla | 61:628f8a4e857c | 158 | distance = sqrt2; |
cittecla | 61:628f8a4e857c | 159 | if(current_pos.x == next_pos.x) needed_heading = 0; |
cittecla | 61:628f8a4e857c | 160 | distance = 1; |
cittecla | 61:628f8a4e857c | 161 | if(current_pos.x < next_pos.x) needed_heading = 45; |
cittecla | 61:628f8a4e857c | 162 | distance = sqrt2; |
cittecla | 61:628f8a4e857c | 163 | } |
cittecla | 61:628f8a4e857c | 164 | if(current_pos.y == next_pos.y) { |
cittecla | 61:628f8a4e857c | 165 | if(current_pos.x > next_pos.x) needed_heading = 270; |
cittecla | 61:628f8a4e857c | 166 | distance = 1; |
cittecla | 53:453f24775644 | 167 | if(current_pos.x == next_pos.x) //error same position; |
cittecla | 61:628f8a4e857c | 168 | if(current_pos.x < next_pos.x) needed_heading = 90; |
cittecla | 61:628f8a4e857c | 169 | distance = 1; |
cittecla | 61:628f8a4e857c | 170 | } |
cittecla | 61:628f8a4e857c | 171 | if(current_pos.y < next_pos.y) { |
cittecla | 61:628f8a4e857c | 172 | if(current_pos.x > next_pos.x) needed_heading = 225; |
cittecla | 61:628f8a4e857c | 173 | distance = sqrt2; |
cittecla | 61:628f8a4e857c | 174 | if(current_pos.x == next_pos.x) needed_heading = 180; |
cittecla | 61:628f8a4e857c | 175 | distance = 1; |
cittecla | 61:628f8a4e857c | 176 | if(current_pos.x < next_pos.x) needed_heading = 135; |
cittecla | 61:628f8a4e857c | 177 | distance = sqrt2; |
cittecla | 61:628f8a4e857c | 178 | } |
cittecla | 52:56399c2f13cd | 179 | |
cittecla | 52:56399c2f13cd | 180 | if(needed_heading != current_heading) { |
cittecla | 53:453f24775644 | 181 | turn_for_deg((needed_heading-current_heading)); |
cittecla | 61:628f8a4e857c | 182 | } else { |
cittecla | 85:d8ea8a99fa3a | 183 | move_for_distance(distance); |
cittecla | 39:92723f7ea54f | 184 | } |
cittecla | 39:92723f7ea54f | 185 | return 0; |
cittecla | 52:56399c2f13cd | 186 | } |
cittecla | 52:56399c2f13cd | 187 | |
PESGruppe1 | 74:d9c387b83196 | 188 | // Tobias Berger |
cittecla | 52:56399c2f13cd | 189 | int move_in_search_for_brick() |
cittecla | 52:56399c2f13cd | 190 | { |
PESGruppe1 | 79:50f8d58f79ab | 191 | float upper = getDistanceIR(2); // get distance from upper Center Sensor |
PESGruppe1 | 79:50f8d58f79ab | 192 | float lower = getDistanceIR(3); // get distance from Lower Center Sensor |
cittecla | 85:d8ea8a99fa3a | 193 | |
cittecla | 86:df8c869a5a52 | 194 | switch (search_state) { |
cittecla | 86:df8c869a5a52 | 195 | case 0: //first cycle right |
cittecla | 86:df8c869a5a52 | 196 | turn_for_deg(60.0f); // call function and start turning |
cittecla | 86:df8c869a5a52 | 197 | search_state = 1; |
cittecla | 86:df8c869a5a52 | 198 | break; |
cittecla | 85:d8ea8a99fa3a | 199 | |
cittecla | 86:df8c869a5a52 | 200 | case 1: // turn right 60 deg |
cittecla | 86:df8c869a5a52 | 201 | if((lower<0.75f)&&(lower>0.1f)) { // if something is in the range of 10 to 80cm at the lower Sensor |
cittecla | 86:df8c869a5a52 | 202 | if(fabsf((upper-lower))>0.02f) { // and nothing is detected with the upper Sensor |
cittecla | 86:df8c869a5a52 | 203 | stop_turn(); |
cittecla | 86:df8c869a5a52 | 204 | search_state = 4; //brick found |
cittecla | 86:df8c869a5a52 | 205 | } |
cittecla | 86:df8c869a5a52 | 206 | } else { |
cittecla | 86:df8c869a5a52 | 207 | search_state = 1; // go to same state |
cittecla | 86:df8c869a5a52 | 208 | if(turn_for_deg(0) < 0) { |
cittecla | 86:df8c869a5a52 | 209 | stop_turn(); |
cittecla | 86:df8c869a5a52 | 210 | search_state = 2; |
cittecla | 86:df8c869a5a52 | 211 | } |
cittecla | 86:df8c869a5a52 | 212 | } |
cittecla | 86:df8c869a5a52 | 213 | break; |
cittecla | 85:d8ea8a99fa3a | 214 | |
cittecla | 86:df8c869a5a52 | 215 | case 2: // first cycle left |
cittecla | 86:df8c869a5a52 | 216 | turn_for_deg(-120.0f); |
cittecla | 86:df8c869a5a52 | 217 | search_state = 3; |
cittecla | 86:df8c869a5a52 | 218 | break; |
cittecla | 85:d8ea8a99fa3a | 219 | |
cittecla | 86:df8c869a5a52 | 220 | case 3: // turn left 120 deg |
cittecla | 86:df8c869a5a52 | 221 | if((lower<0.75f)&&(lower>0.1f)) { // if something is in the range of 10 to 80cm at the lower Sensor |
cittecla | 86:df8c869a5a52 | 222 | if(fabsf((upper-lower))>0.02f) { // and nothing is detected with the upper Sensor |
cittecla | 86:df8c869a5a52 | 223 | stop_turn(); |
cittecla | 86:df8c869a5a52 | 224 | search_state = 4; //brick found |
cittecla | 86:df8c869a5a52 | 225 | } |
cittecla | 86:df8c869a5a52 | 226 | } else { |
cittecla | 86:df8c869a5a52 | 227 | search_state = 3; // go to same state |
cittecla | 86:df8c869a5a52 | 228 | if(turn_for_deg(0) < 0) { |
cittecla | 86:df8c869a5a52 | 229 | stop_turn(); |
cittecla | 86:df8c869a5a52 | 230 | search_state = 10; // error |
cittecla | 86:df8c869a5a52 | 231 | } |
cittecla | 86:df8c869a5a52 | 232 | } |
cittecla | 86:df8c869a5a52 | 233 | break; |
cittecla | 52:56399c2f13cd | 234 | |
cittecla | 86:df8c869a5a52 | 235 | case 4: // first cycle move |
cittecla | 86:df8c869a5a52 | 236 | float distance_to_Brick = lower-(float)OFFSET_GREIFER_TO_IRSENSOR; // calculate |
cittecla | 86:df8c869a5a52 | 237 | move_for_distance(distance_to_Brick); |
cittecla | 86:df8c869a5a52 | 238 | search_state = 5; |
cittecla | 86:df8c869a5a52 | 239 | break; |
cittecla | 86:df8c869a5a52 | 240 | |
cittecla | 86:df8c869a5a52 | 241 | case 5: // move forward |
cittecla | 86:df8c869a5a52 | 242 | if(move_for_distance(0) < 0) { |
cittecla | 86:df8c869a5a52 | 243 | stop_move(); |
cittecla | 86:df8c869a5a52 | 244 | search_state = 6; |
cittecla | 86:df8c869a5a52 | 245 | } |
cittecla | 86:df8c869a5a52 | 246 | break; |
cittecla | 86:df8c869a5a52 | 247 | |
cittecla | 86:df8c869a5a52 | 248 | case 6: // Grabbing |
cittecla | 94:0381e8b1beda | 249 | return 50; //main state machine set as Grabbing |
cittecla | 94:0381e8b1beda | 250 | |
cittecla | 86:df8c869a5a52 | 251 | default: |
cittecla | 86:df8c869a5a52 | 252 | // error |
cittecla | 86:df8c869a5a52 | 253 | break; |
PESGruppe1 | 74:d9c387b83196 | 254 | } |
cittecla | 86:df8c869a5a52 | 255 | return 47; //called until function is done |
cittecla | 52:56399c2f13cd | 256 | } |
cittecla | 52:56399c2f13cd | 257 | |
cittecla | 39:92723f7ea54f | 258 | |
cittecla | 39:92723f7ea54f | 259 | |
cittecla | 39:92723f7ea54f | 260 | |
cittecla | 39:92723f7ea54f | 261 | |
cittecla | 39:92723f7ea54f | 262 | |
cittecla | 39:92723f7ea54f | 263 | |
cittecla | 39:92723f7ea54f | 264 |