Tobis Programm forked to not destroy your golden files
Fork of Robocode by
source/Mapping.cpp@128:6bde4483ce7b, 2017-05-14 (annotated)
- Committer:
- cittecla
- Date:
- Sun May 14 14:40:12 2017 +0000
- Revision:
- 128:6bde4483ce7b
- Parent:
- 126:d0b2057272d0
- Child:
- 129:0f60bf9640bb
included path finding into state machine
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cittecla | 12:91c2e07d2b5b | 1 | /** |
cittecla | 12:91c2e07d2b5b | 2 | * Mapping function library |
cittecla | 12:91c2e07d2b5b | 3 | * Handels Mapping of the arena and LEGO-stones |
cittecla | 12:91c2e07d2b5b | 4 | **/ |
cittecla | 12:91c2e07d2b5b | 5 | |
cittecla | 38:3526c36e4c73 | 6 | #include "Mapping.h" |
cittecla | 38:3526c36e4c73 | 7 | |
cittecla | 51:4a18b47fd659 | 8 | #define list_time_value 60 |
cittecla | 51:4a18b47fd659 | 9 | #define list_target_value 1 |
cittecla | 51:4a18b47fd659 | 10 | #define list_boundry_value 255 |
cittecla | 51:4a18b47fd659 | 11 | #define servo_angle 50 |
cittecla | 51:4a18b47fd659 | 12 | |
cittecla | 51:4a18b47fd659 | 13 | |
cittecla | 47:728502df3cb6 | 14 | uint8_t obstacle_list[row][col] = { 0 }; |
cittecla | 49:652438112348 | 15 | uint8_t target_list[row][col] = {0}; |
cittecla | 51:4a18b47fd659 | 16 | |
cittecla | 47:728502df3cb6 | 17 | uint8_t a = list_time_value; //Substitution um Matrix übersichtlich zu halten |
cittecla | 49:652438112348 | 18 | uint8_t superpos[7][7] = {{0,0,a,a,a,0,0}, |
cittecla | 47:728502df3cb6 | 19 | {0,a,a,a,a,a,0}, |
cittecla | 47:728502df3cb6 | 20 | {a,a,a,a,a,a,a}, |
cittecla | 47:728502df3cb6 | 21 | {a,a,a,a,a,a,a}, |
cittecla | 47:728502df3cb6 | 22 | {a,a,a,a,a,a,a}, |
cittecla | 47:728502df3cb6 | 23 | {0,a,a,a,a,a,0}, |
cittecla | 47:728502df3cb6 | 24 | {0,0,a,a,a,0,0} |
cittecla | 47:728502df3cb6 | 25 | }; |
cittecla | 47:728502df3cb6 | 26 | |
cittecla | 46:8b52c7b34d34 | 27 | position old_pos = {0}; |
cittecla | 49:652438112348 | 28 | float old_heading = 0; |
cittecla | 46:8b52c7b34d34 | 29 | |
cittecla | 49:652438112348 | 30 | //****************************************************************************** |
cittecla | 13:57f5a7876d2f | 31 | void mapping() |
cittecla | 13:57f5a7876d2f | 32 | { |
cittecla | 60:b57577b0072f | 33 | printf("mapping...\r\n"); |
cittecla | 62:c2fcf3b349e9 | 34 | |
cittecla | 50:2e2bf0815fd9 | 35 | position current_pos = get_current_pos(); |
cittecla | 50:2e2bf0815fd9 | 36 | float current_heading = get_current_heading(); |
cittecla | 50:2e2bf0815fd9 | 37 | if(old_heading != current_heading || old_pos.x != current_pos.x || old_pos.y != current_pos.y) { |
cittecla | 47:728502df3cb6 | 38 | //check_sensor(left); |
cittecla | 49:652438112348 | 39 | float distance0 = getDistanceIR(0); |
cittecla | 49:652438112348 | 40 | if(distance0 < 0.75f && distance0 > 0.1f) { |
cittecla | 51:4a18b47fd659 | 41 | set_servo_position(0, servo_angle); |
cittecla | 49:652438112348 | 42 | float distance = getDistanceIR(1); |
cittecla | 49:652438112348 | 43 | int object = 0; |
cittecla | 57:1a395b6928ee | 44 | if(distance < 0.75f && distance > 0.1f && (distance -0.05f < distance0 || distance + 0.05f > distance0)) { |
cittecla | 49:652438112348 | 45 | object = 0; |
cittecla | 49:652438112348 | 46 | } else { |
cittecla | 49:652438112348 | 47 | object = 1; |
cittecla | 49:652438112348 | 48 | } |
cittecla | 52:56399c2f13cd | 49 | position mapping_pos = position_calculation(distance0, servo_angle, 0.12, -0.12, current_heading, current_pos); |
cittecla | 49:652438112348 | 50 | draw_to_map(mapping_pos, object); |
cittecla | 47:728502df3cb6 | 51 | } |
cittecla | 47:728502df3cb6 | 52 | //check_sensor(right); |
cittecla | 51:4a18b47fd659 | 53 | float distance4 = getDistanceIR(4); |
cittecla | 49:652438112348 | 54 | if(distance4 < 0.75f && distance4 > 0.1f) { |
cittecla | 51:4a18b47fd659 | 55 | set_servo_position(1, -servo_angle); |
cittecla | 51:4a18b47fd659 | 56 | float distance = getDistanceIR(5); |
cittecla | 49:652438112348 | 57 | int object = 0; |
cittecla | 57:1a395b6928ee | 58 | if(distance < 0.75f && distance > 0.1f && (distance -0.05f < distance4 || distance + 0.05f > distance4)) { |
cittecla | 49:652438112348 | 59 | object = 0; |
cittecla | 49:652438112348 | 60 | } else { |
cittecla | 49:652438112348 | 61 | object = 1; |
cittecla | 49:652438112348 | 62 | } |
cittecla | 52:56399c2f13cd | 63 | position mapping_pos = position_calculation(distance4, -servo_angle, -0.12, -0.12, current_heading, current_pos); |
cittecla | 49:652438112348 | 64 | draw_to_map(mapping_pos, object); |
cittecla | 49:652438112348 | 65 | } |
cittecla | 49:652438112348 | 66 | //check_sensor(center); |
cittecla | 51:4a18b47fd659 | 67 | float distance2 = getDistanceIR(2); |
cittecla | 49:652438112348 | 68 | if(distance2 < 0.75f && distance2 > 0.1f) { |
cittecla | 51:4a18b47fd659 | 69 | float distance = getDistanceIR(3); |
cittecla | 49:652438112348 | 70 | int object = 0; |
cittecla | 57:1a395b6928ee | 71 | if(distance < 0.75f && distance > 0.1f && (distance -0.05f < distance2 || distance + 0.05f > distance2)) { |
cittecla | 49:652438112348 | 72 | object = 0; |
cittecla | 49:652438112348 | 73 | } else { |
cittecla | 49:652438112348 | 74 | object = 1; |
cittecla | 49:652438112348 | 75 | } |
cittecla | 52:56399c2f13cd | 76 | position mapping_pos = position_calculation(distance2, 0, -0.12, -0.12, current_heading, current_pos); |
cittecla | 49:652438112348 | 77 | draw_to_map(mapping_pos, object); |
cittecla | 47:728502df3cb6 | 78 | } |
cittecla | 47:728502df3cb6 | 79 | |
cittecla | 50:2e2bf0815fd9 | 80 | old_pos = current_pos; |
cittecla | 50:2e2bf0815fd9 | 81 | old_heading = current_heading; |
cittecla | 62:c2fcf3b349e9 | 82 | } |
cittecla | 47:728502df3cb6 | 83 | } |
cittecla | 47:728502df3cb6 | 84 | |
cittecla | 49:652438112348 | 85 | //****************************************************************************** |
cittecla | 49:652438112348 | 86 | void draw_to_map(position pos, int object) |
cittecla | 47:728502df3cb6 | 87 | { |
cittecla | 47:728502df3cb6 | 88 | uint8_t x_negative_offset = 0; |
cittecla | 47:728502df3cb6 | 89 | uint8_t x_positive_offset = 0; |
cittecla | 47:728502df3cb6 | 90 | uint8_t y_negative_offset = 0; |
cittecla | 47:728502df3cb6 | 91 | uint8_t y_positive_offset = 0; |
cittecla | 47:728502df3cb6 | 92 | |
cittecla | 48:4078877669e4 | 93 | if (pos.x < 5) { |
cittecla | 48:4078877669e4 | 94 | x_negative_offset = 3 - pos.x; |
cittecla | 48:4078877669e4 | 95 | } |
cittecla | 48:4078877669e4 | 96 | if (pos.x > (col-5)) { |
cittecla | 48:4078877669e4 | 97 | x_positive_offset = pos.x - (col - 4); |
cittecla | 47:728502df3cb6 | 98 | } |
cittecla | 48:4078877669e4 | 99 | |
cittecla | 48:4078877669e4 | 100 | if (pos.y < 5) { |
cittecla | 49:652438112348 | 101 | y_negative_offset = 3 - pos.y; |
cittecla | 48:4078877669e4 | 102 | } |
cittecla | 48:4078877669e4 | 103 | if (pos.y > (row-5)) { |
cittecla | 48:4078877669e4 | 104 | y_positive_offset = pos.y - (row - 4); |
cittecla | 47:728502df3cb6 | 105 | } |
cittecla | 47:728502df3cb6 | 106 | |
cittecla | 48:4078877669e4 | 107 | for (int y = 0 + y_negative_offset; y < 7 - y_positive_offset; y++) { |
cittecla | 48:4078877669e4 | 108 | for (int x = 0 + x_negative_offset; x < 7 - x_positive_offset; x++) { |
cittecla | 49:652438112348 | 109 | switch(object) { |
cittecla | 49:652438112348 | 110 | case 0: |
cittecla | 49:652438112348 | 111 | a = list_time_value; |
cittecla | 49:652438112348 | 112 | obstacle_list[pos.y - 3 + y][pos.x - 3 + x] = superpos[y][x]; |
cittecla | 49:652438112348 | 113 | break; |
cittecla | 49:652438112348 | 114 | case 1: |
cittecla | 49:652438112348 | 115 | a = list_target_value; |
cittecla | 49:652438112348 | 116 | target_list[pos.y - 3 + y][pos.x - 3 + x] = superpos[y][x]; |
cittecla | 49:652438112348 | 117 | break; |
cittecla | 49:652438112348 | 118 | case 2: |
cittecla | 49:652438112348 | 119 | a = list_boundry_value; |
cittecla | 49:652438112348 | 120 | target_list[pos.y - 3 + y][pos.x - 3 + x] = superpos[y][x]; |
cittecla | 49:652438112348 | 121 | break; |
cittecla | 49:652438112348 | 122 | default: |
cittecla | 49:652438112348 | 123 | break; |
cittecla | 49:652438112348 | 124 | } |
cittecla | 47:728502df3cb6 | 125 | } |
cittecla | 47:728502df3cb6 | 126 | } |
cittecla | 28:274e1d4ecac5 | 127 | } |
cittecla | 28:274e1d4ecac5 | 128 | |
cittecla | 49:652438112348 | 129 | //****************************************************************************** |
cittecla | 49:652438112348 | 130 | void print_map() |
cittecla | 49:652438112348 | 131 | { |
cittecla | 52:56399c2f13cd | 132 | // Debug function for printing the obstacle matrix on putty. |
cittecla | 48:4078877669e4 | 133 | for (int y = 0; y < col; y++) { |
cittecla | 48:4078877669e4 | 134 | for (int x = 0; x < row; x++) { |
cittecla | 48:4078877669e4 | 135 | printf("%d ", obstacle_list[y][x]); |
cittecla | 48:4078877669e4 | 136 | } |
cittecla | 52:56399c2f13cd | 137 | printf("\r\n"); |
cittecla | 48:4078877669e4 | 138 | } |
cittecla | 52:56399c2f13cd | 139 | printf("\r\n"); |
cittecla | 48:4078877669e4 | 140 | } |
cittecla | 48:4078877669e4 | 141 | |
cittecla | 49:652438112348 | 142 | //****************************************************************************** |
cittecla | 50:2e2bf0815fd9 | 143 | position position_calculation(float distance, float degree, float offsetx, float offsety, float heading, position current_pos) |
cittecla | 47:728502df3cb6 | 144 | { |
cittecla | 50:2e2bf0815fd9 | 145 | distance *= 100; |
cittecla | 50:2e2bf0815fd9 | 146 | offsetx *= 100; |
cittecla | 50:2e2bf0815fd9 | 147 | offsety *= 100; |
cittecla | 50:2e2bf0815fd9 | 148 | |
cittecla | 50:2e2bf0815fd9 | 149 | position pos = { 0 }; |
cittecla | 50:2e2bf0815fd9 | 150 | float direction = 0; |
cittecla | 50:2e2bf0815fd9 | 151 | |
cittecla | 50:2e2bf0815fd9 | 152 | float x = (offsetx + sin(degree/180*(float)M_PI)*distance)/4; |
cittecla | 50:2e2bf0815fd9 | 153 | float y = (-offsety - cos(degree/180*(float)M_PI)*distance)/4; |
cittecla | 50:2e2bf0815fd9 | 154 | float hyp = sqrt(x*x+y*y); |
cittecla | 50:2e2bf0815fd9 | 155 | direction = asin(x/hyp)/(float)M_PI*180; |
cittecla | 50:2e2bf0815fd9 | 156 | direction += heading; |
cittecla | 47:728502df3cb6 | 157 | |
cittecla | 50:2e2bf0815fd9 | 158 | while (direction >= 360) direction -= 360; |
cittecla | 50:2e2bf0815fd9 | 159 | while (direction < 0) direction += 360; |
cittecla | 47:728502df3cb6 | 160 | |
cittecla | 71:ddf4eb5c3081 | 161 | printf("%f || %f || %f || %f\r\n", x, y, hyp, degree); |
cittecla | 50:2e2bf0815fd9 | 162 | |
cittecla | 50:2e2bf0815fd9 | 163 | if ((0 <= direction && direction < 90) || (180 <= direction && direction < 270)) { |
cittecla | 50:2e2bf0815fd9 | 164 | pos.x = current_pos.x + rint(sin(direction / 180 * (float)M_PI)*hyp); |
cittecla | 50:2e2bf0815fd9 | 165 | pos.y = current_pos.y - rint(cos(direction / 180 * (float)M_PI)*hyp); |
cittecla | 71:ddf4eb5c3081 | 166 | printf("%d || %d\r\n", pos.x,pos.y); |
cittecla | 50:2e2bf0815fd9 | 167 | } |
cittecla | 50:2e2bf0815fd9 | 168 | if ((90 <= direction && direction < 180) || (270 <= direction && direction < 360)) { |
cittecla | 50:2e2bf0815fd9 | 169 | pos.x = current_pos.x + rint(sin((180-direction) / 180 * (float)M_PI)*hyp); |
cittecla | 50:2e2bf0815fd9 | 170 | pos.y = current_pos.y + rint(cos((180-direction) / 180 * (float)M_PI)*hyp); |
cittecla | 71:ddf4eb5c3081 | 171 | printf("%d || %d\r\n", pos.x, pos.y); |
cittecla | 50:2e2bf0815fd9 | 172 | } |
cittecla | 50:2e2bf0815fd9 | 173 | |
cittecla | 46:8b52c7b34d34 | 174 | return pos; |
cittecla | 46:8b52c7b34d34 | 175 | } |
cittecla | 38:3526c36e4c73 | 176 | |
cittecla | 62:c2fcf3b349e9 | 177 | |
cittecla | 62:c2fcf3b349e9 | 178 | coordinates coordinates_calculation(float distance, float degree, float offsetx, float offsety, float heading, coordinates current_coord) |
cittecla | 62:c2fcf3b349e9 | 179 | { |
cittecla | 62:c2fcf3b349e9 | 180 | distance *= 100; |
cittecla | 62:c2fcf3b349e9 | 181 | offsetx *= 100; |
cittecla | 62:c2fcf3b349e9 | 182 | offsety *= 100; |
cittecla | 62:c2fcf3b349e9 | 183 | |
cittecla | 62:c2fcf3b349e9 | 184 | coordinates coord = { 0 }; |
cittecla | 62:c2fcf3b349e9 | 185 | float direction = 0; |
cittecla | 62:c2fcf3b349e9 | 186 | |
cittecla | 62:c2fcf3b349e9 | 187 | float x = (offsetx + sin(degree/180*(float)M_PI)*distance)/4; |
cittecla | 62:c2fcf3b349e9 | 188 | float y = (-offsety - cos(degree/180*(float)M_PI)*distance)/4; |
cittecla | 62:c2fcf3b349e9 | 189 | float hyp = sqrt(x*x+y*y); |
cittecla | 62:c2fcf3b349e9 | 190 | direction = asin(x/hyp)/(float)M_PI*180; |
cittecla | 62:c2fcf3b349e9 | 191 | direction += heading; |
cittecla | 62:c2fcf3b349e9 | 192 | |
cittecla | 62:c2fcf3b349e9 | 193 | while (direction >= 360) direction -= 360; |
cittecla | 62:c2fcf3b349e9 | 194 | while (direction < 0) direction += 360; |
cittecla | 62:c2fcf3b349e9 | 195 | |
cittecla | 62:c2fcf3b349e9 | 196 | printf("%f || %f || %f || %f\n", x, y, hyp, degree); |
cittecla | 62:c2fcf3b349e9 | 197 | |
cittecla | 62:c2fcf3b349e9 | 198 | if ((0 <= direction && direction < 90) || (180 <= direction && direction < 270)) { |
cittecla | 62:c2fcf3b349e9 | 199 | coord.x = current_coord.x + sin(direction / 180 * (float)M_PI)*hyp; |
cittecla | 62:c2fcf3b349e9 | 200 | coord.y = current_coord.y - cos(direction / 180 * (float)M_PI)*hyp; |
cittecla | 62:c2fcf3b349e9 | 201 | printf("%f || %f\n", coord.x,coord.y); |
cittecla | 62:c2fcf3b349e9 | 202 | } |
cittecla | 62:c2fcf3b349e9 | 203 | if ((90 <= direction && direction < 180) || (270 <= direction && direction < 360)) { |
cittecla | 62:c2fcf3b349e9 | 204 | coord.x = current_coord.x + sin((180-direction) / 180 * (float)M_PI)*hyp; |
cittecla | 62:c2fcf3b349e9 | 205 | coord.y = current_coord.y + cos((180-direction) / 180 * (float)M_PI)*hyp; |
cittecla | 62:c2fcf3b349e9 | 206 | printf("%f || %f\n", coord.x, coord.y); |
cittecla | 62:c2fcf3b349e9 | 207 | } |
cittecla | 62:c2fcf3b349e9 | 208 | |
cittecla | 62:c2fcf3b349e9 | 209 | return coord; |
cittecla | 62:c2fcf3b349e9 | 210 | } |
cittecla | 49:652438112348 | 211 | //****************************************************************************** |
cittecla | 41:462d379e85c4 | 212 | int select_target() |
cittecla | 41:462d379e85c4 | 213 | { |
cittecla | 41:462d379e85c4 | 214 | position myPos = get_current_pos(); |
cittecla | 41:462d379e85c4 | 215 | position diff = {0}; |
cittecla | 41:462d379e85c4 | 216 | target.x = 0; |
cittecla | 41:462d379e85c4 | 217 | int closest_dist = 10000; |
cittecla | 41:462d379e85c4 | 218 | int current_dist = 0; |
cittecla | 41:462d379e85c4 | 219 | |
cittecla | 41:462d379e85c4 | 220 | for(int i = 0; i < row; i++) { |
cittecla | 41:462d379e85c4 | 221 | for(int j = 0; j < col; j++) { |
cittecla | 41:462d379e85c4 | 222 | if(obstacle_list[i][j] == 2) { |
cittecla | 41:462d379e85c4 | 223 | diff.x = abs(myPos.x - j); |
cittecla | 41:462d379e85c4 | 224 | diff.y = abs(myPos.y - i); |
cittecla | 41:462d379e85c4 | 225 | current_dist = diff.x * diff.y; |
cittecla | 41:462d379e85c4 | 226 | if(current_dist < closest_dist) { |
cittecla | 41:462d379e85c4 | 227 | closest_dist = current_dist; |
cittecla | 41:462d379e85c4 | 228 | target.x = j; |
cittecla | 41:462d379e85c4 | 229 | target.y = i; |
cittecla | 41:462d379e85c4 | 230 | } |
cittecla | 41:462d379e85c4 | 231 | } |
cittecla | 41:462d379e85c4 | 232 | } |
cittecla | 41:462d379e85c4 | 233 | } |
cittecla | 41:462d379e85c4 | 234 | if(target.x == 0) { |
cittecla | 128:6bde4483ce7b | 235 | return 48; // No Target found |
cittecla | 41:462d379e85c4 | 236 | } else { |
cittecla | 128:6bde4483ce7b | 237 | start = get_current_pos(); |
cittecla | 128:6bde4483ce7b | 238 | printf("start: %d || %d\r\n", start.x, start.y); |
cittecla | 128:6bde4483ce7b | 239 | printf("Target: &d || %d\r\n", target.x, target.y); |
cittecla | 128:6bde4483ce7b | 240 | |
cittecla | 41:462d379e85c4 | 241 | return 36; // Target found |
cittecla | 41:462d379e85c4 | 242 | } |
cittecla | 41:462d379e85c4 | 243 | } |
cittecla | 41:462d379e85c4 | 244 | |
cittecla | 86:df8c869a5a52 | 245 | int generate_fake_target(){ |
cittecla | 126:d0b2057272d0 | 246 | obstacle_list[50][50] = 2; |
cittecla | 126:d0b2057272d0 | 247 | return 35; |
cittecla | 86:df8c869a5a52 | 248 | } |
cittecla | 86:df8c869a5a52 | 249 | |
cittecla | 49:652438112348 | 250 | //****************************************************************************** |
cittecla | 41:462d379e85c4 | 251 | int switch_target_red() |
cittecla | 41:462d379e85c4 | 252 | { |
cittecla | 41:462d379e85c4 | 253 | obstacle_list[target.y][target.x] = 0; |
cittecla | 47:728502df3cb6 | 254 | return 0; |
cittecla | 49:652438112348 | 255 | } |