Tobis Programm forked to not destroy your golden files
Fork of Robocode by
source/Mapping.cpp@46:8b52c7b34d34, 2017-04-11 (annotated)
- Committer:
- cittecla
- Date:
- Tue Apr 11 19:43:39 2017 +0000
- Revision:
- 46:8b52c7b34d34
- Parent:
- 41:462d379e85c4
- Child:
- 47:728502df3cb6
new mapping, draw to map and position_calculation functions
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 | 46:8b52c7b34d34 | 7 | //#include "Pathfinding.h" |
cittecla | 38:3526c36e4c73 | 8 | |
cittecla | 46:8b52c7b34d34 | 9 | uint8_t obstacle_list_superpos[7][7] = {{0,0,2,2,2,0,0}, |
cittecla | 46:8b52c7b34d34 | 10 | {0,2,2,2,2,2,0}, |
cittecla | 46:8b52c7b34d34 | 11 | {2,2,2,2,2,2,2}, |
cittecla | 46:8b52c7b34d34 | 12 | {2,2,2,1,2,2,2}, |
cittecla | 46:8b52c7b34d34 | 13 | {2,2,2,2,2,2,2}, |
cittecla | 46:8b52c7b34d34 | 14 | {0,2,2,2,2,2,0}, |
cittecla | 46:8b52c7b34d34 | 15 | {0,0,2,2,2,0,0}}; |
cittecla | 46:8b52c7b34d34 | 16 | |
cittecla | 46:8b52c7b34d34 | 17 | position old_pos = {0}; |
cittecla | 46:8b52c7b34d34 | 18 | position mapping_pos_right = {0}; |
cittecla | 46:8b52c7b34d34 | 19 | position mapping_pos_left = {0}; |
cittecla | 46:8b52c7b34d34 | 20 | |
cittecla | 46:8b52c7b34d34 | 21 | float old_heading = {0}; |
cittecla | 46:8b52c7b34d34 | 22 | float distance_left = 0; |
cittecla | 46:8b52c7b34d34 | 23 | float distance_right = 0; |
cittecla | 12:91c2e07d2b5b | 24 | |
cittecla | 13:57f5a7876d2f | 25 | void mapping() |
cittecla | 13:57f5a7876d2f | 26 | { |
cittecla | 46:8b52c7b34d34 | 27 | position new_pos = get_current_pos(); |
cittecla | 46:8b52c7b34d34 | 28 | float new_heading = get_current_heading(); |
cittecla | 46:8b52c7b34d34 | 29 | if(old_heading != new_heading || old_pos.x != new_pos.x || old_pos.y != new_pos.y){ |
cittecla | 28:274e1d4ecac5 | 30 | //pseudo code |
cittecla | 46:8b52c7b34d34 | 31 | //check_sensor(left); |
cittecla | 46:8b52c7b34d34 | 32 | distance_left = getDistanceIR(0); |
cittecla | 46:8b52c7b34d34 | 33 | if(distance_left < 0.75f && distance_left > 0.1f){ |
cittecla | 46:8b52c7b34d34 | 34 | mapping_pos_left = position_calculation(distance_left, 50, 0.12, 0.12, new_heading); |
cittecla | 46:8b52c7b34d34 | 35 | draw_to_map(mapping_pos_left); |
cittecla | 46:8b52c7b34d34 | 36 | } |
cittecla | 41:462d379e85c4 | 37 | //check_sensor(right); |
cittecla | 46:8b52c7b34d34 | 38 | distance_right = getDistanceIR(4); |
cittecla | 46:8b52c7b34d34 | 39 | if(distance_right < 0.75f && distance_right > 0.1f){ |
cittecla | 46:8b52c7b34d34 | 40 | mapping_pos_right = position_calculation(distance_right, -50, -0.12, 0.12, new_heading); |
cittecla | 46:8b52c7b34d34 | 41 | draw_to_map(mapping_pos_right); |
cittecla | 46:8b52c7b34d34 | 42 | } |
cittecla | 46:8b52c7b34d34 | 43 | |
cittecla | 46:8b52c7b34d34 | 44 | old_pos = new_pos; |
cittecla | 46:8b52c7b34d34 | 45 | old_heading = new_heading; |
cittecla | 46:8b52c7b34d34 | 46 | } |
cittecla | 28:274e1d4ecac5 | 47 | } |
cittecla | 28:274e1d4ecac5 | 48 | |
cittecla | 46:8b52c7b34d34 | 49 | void draw_to_map(position pos){ |
cittecla | 46:8b52c7b34d34 | 50 | |
cittecla | 46:8b52c7b34d34 | 51 | } |
cittecla | 46:8b52c7b34d34 | 52 | |
cittecla | 46:8b52c7b34d34 | 53 | position position_calculation(float distance,float degree,float ofsettx,float ofsetty, float heading){ |
cittecla | 46:8b52c7b34d34 | 54 | |
cittecla | 46:8b52c7b34d34 | 55 | |
cittecla | 46:8b52c7b34d34 | 56 | position pos = {0}; |
cittecla | 46:8b52c7b34d34 | 57 | return pos; |
cittecla | 46:8b52c7b34d34 | 58 | } |
cittecla | 38:3526c36e4c73 | 59 | |
cittecla | 41:462d379e85c4 | 60 | int select_target() |
cittecla | 41:462d379e85c4 | 61 | { |
cittecla | 41:462d379e85c4 | 62 | position myPos = get_current_pos(); |
cittecla | 41:462d379e85c4 | 63 | position diff = {0}; |
cittecla | 41:462d379e85c4 | 64 | target.x = 0; |
cittecla | 41:462d379e85c4 | 65 | int closest_dist = 10000; |
cittecla | 41:462d379e85c4 | 66 | int current_dist = 0; |
cittecla | 41:462d379e85c4 | 67 | |
cittecla | 41:462d379e85c4 | 68 | for(int i = 0; i < row; i++) { |
cittecla | 41:462d379e85c4 | 69 | for(int j = 0; j < col; j++) { |
cittecla | 41:462d379e85c4 | 70 | if(obstacle_list[i][j] == 2) { |
cittecla | 41:462d379e85c4 | 71 | diff.x = abs(myPos.x - j); |
cittecla | 41:462d379e85c4 | 72 | diff.y = abs(myPos.y - i); |
cittecla | 41:462d379e85c4 | 73 | current_dist = diff.x * diff.y; |
cittecla | 41:462d379e85c4 | 74 | if(current_dist < closest_dist) { |
cittecla | 41:462d379e85c4 | 75 | closest_dist = current_dist; |
cittecla | 41:462d379e85c4 | 76 | target.x = j; |
cittecla | 41:462d379e85c4 | 77 | target.y = i; |
cittecla | 41:462d379e85c4 | 78 | } |
cittecla | 41:462d379e85c4 | 79 | } |
cittecla | 41:462d379e85c4 | 80 | } |
cittecla | 41:462d379e85c4 | 81 | } |
cittecla | 41:462d379e85c4 | 82 | if(target.x == 0) { |
cittecla | 41:462d379e85c4 | 83 | return 47; // No Target found |
cittecla | 41:462d379e85c4 | 84 | } else { |
cittecla | 41:462d379e85c4 | 85 | return 36; // Target found |
cittecla | 41:462d379e85c4 | 86 | } |
cittecla | 41:462d379e85c4 | 87 | } |
cittecla | 41:462d379e85c4 | 88 | |
cittecla | 41:462d379e85c4 | 89 | int remove_target() |
cittecla | 41:462d379e85c4 | 90 | { |
cittecla | 41:462d379e85c4 | 91 | obstacle_list[target.y][target.x] = 0; |
cittecla | 40:a46667b62671 | 92 | return 0; |
cittecla | 40:a46667b62671 | 93 | } |
cittecla | 40:a46667b62671 | 94 | |
cittecla | 41:462d379e85c4 | 95 | int switch_target_red() |
cittecla | 41:462d379e85c4 | 96 | { |
cittecla | 41:462d379e85c4 | 97 | obstacle_list[target.y][target.x] = 0; |
cittecla | 41:462d379e85c4 | 98 | return 0; |
cittecla | 40:a46667b62671 | 99 | } |
cittecla | 40:a46667b62671 | 100 | |
cittecla | 40:a46667b62671 | 101 | |
cittecla | 40:a46667b62671 | 102 | |
cittecla | 40:a46667b62671 | 103 | |
cittecla | 40:a46667b62671 | 104 | |
cittecla | 38:3526c36e4c73 | 105 | /* |
cittecla | 28:274e1d4ecac5 | 106 | void chack_sensor(sensor) |
cittecla | 28:274e1d4ecac5 | 107 | { |
cittecla | 28:274e1d4ecac5 | 108 | get_distance(); |
cittecla | 28:274e1d4ecac5 | 109 | if(sensor < 80cm || sensor > 10cm) { |
cittecla | 28:274e1d4ecac5 | 110 | calculate_position(sensor, distance); |
cittecla | 28:274e1d4ecac5 | 111 | check_map(position.this, obstacle); |
cittecla | 28:274e1d4ecac5 | 112 | if(map.no-entry){ |
cittecla | 28:274e1d4ecac5 | 113 | check_if_obstacle(distance); |
cittecla | 28:274e1d4ecac5 | 114 | update_map(); |
cittecla | 28:274e1d4ecac5 | 115 | } |
cittecla | 28:274e1d4ecac5 | 116 | } |
cittecla | 14:9e2ce5880fb0 | 117 | } |
cittecla | 28:274e1d4ecac5 | 118 | |
cittecla | 28:274e1d4ecac5 | 119 | void get_distance(sensor) |
cittecla | 28:274e1d4ecac5 | 120 | { |
cittecla | 28:274e1d4ecac5 | 121 | sensor.value; |
cittecla | 28:274e1d4ecac5 | 122 | return value; |
cittecla | 28:274e1d4ecac5 | 123 | } |
cittecla | 28:274e1d4ecac5 | 124 | |
cittecla | 28:274e1d4ecac5 | 125 | void calcualte_position(sensor, distance) |
cittecla | 28:274e1d4ecac5 | 126 | { |
cittecla | 28:274e1d4ecac5 | 127 | do math; |
cittecla | 28:274e1d4ecac5 | 128 | return position; |
cittecla | 28:274e1d4ecac5 | 129 | } |
cittecla | 28:274e1d4ecac5 | 130 | |
cittecla | 28:274e1d4ecac5 | 131 | void check_if_obstacle(distance) |
cittecla | 28:274e1d4ecac5 | 132 | { |
cittecla | 28:274e1d4ecac5 | 133 | move_scanner(); |
cittecla | 28:274e1d4ecac5 | 134 | distance2 = get_distance(sensor); |
cittecla | 28:274e1d4ecac5 | 135 | reset_scanner(); |
cittecla | 28:274e1d4ecac5 | 136 | if(distance -5cm < distance2 && distance +5cm > distance2) { |
cittecla | 28:274e1d4ecac5 | 137 | return 1; |
cittecla | 28:274e1d4ecac5 | 138 | } else { |
cittecla | 28:274e1d4ecac5 | 139 | return 0; |
cittecla | 28:274e1d4ecac5 | 140 | } |
cittecla | 29:e7d0208bf2af | 141 | }*/ |