Tobis Programm forked to not destroy your golden files
Fork of Robocode by
source/Positioning.cpp@118:fbd6d41f6ce8, 2017-05-12 (annotated)
- Committer:
- cittecla
- Date:
- Fri May 12 08:29:53 2017 +0000
- Revision:
- 118:fbd6d41f6ce8
- Parent:
- 112:b3270806629f
- Child:
- 120:cdf7a6751f9e
change positioning stuff;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cittecla | 12:91c2e07d2b5b | 1 | /** |
cittecla | 12:91c2e07d2b5b | 2 | * Positioning function library |
cittecla | 12:91c2e07d2b5b | 3 | * Handels position of the Robot on the map |
cittecla | 12:91c2e07d2b5b | 4 | **/ |
cittecla | 33:8a98f8b9d859 | 5 | |
cittecla | 33:8a98f8b9d859 | 6 | |
cittecla | 38:3526c36e4c73 | 7 | #include "Positioning.h" |
cittecla | 112:b3270806629f | 8 | coordinates current_coord; // in degree // Updated |
cittecla | 112:b3270806629f | 9 | position current_pos; // in degree // Generated from current_coord |
cittecla | 46:8b52c7b34d34 | 10 | float current_heading; |
cittecla | 52:56399c2f13cd | 11 | position next_pos; |
cittecla | 12:91c2e07d2b5b | 12 | |
cittecla | 62:c2fcf3b349e9 | 13 | bool init = false; |
aeschsim | 68:29c52ea147d5 | 14 | Timer t2; |
cittecla | 62:c2fcf3b349e9 | 15 | |
cittecla | 62:c2fcf3b349e9 | 16 | int deg_r = 0; |
cittecla | 62:c2fcf3b349e9 | 17 | int deg_l = 0; |
cittecla | 62:c2fcf3b349e9 | 18 | float last_dist_r = 0; |
cittecla | 62:c2fcf3b349e9 | 19 | float last_dist_l = 0; |
cittecla | 70:922cbbfebf02 | 20 | float direction_r = 0; |
cittecla | 70:922cbbfebf02 | 21 | float direction_l = 0; |
cittecla | 62:c2fcf3b349e9 | 22 | |
cittecla | 62:c2fcf3b349e9 | 23 | |
cittecla | 62:c2fcf3b349e9 | 24 | coordinates get_current_coord() |
cittecla | 62:c2fcf3b349e9 | 25 | { |
cittecla | 62:c2fcf3b349e9 | 26 | return current_coord; |
cittecla | 62:c2fcf3b349e9 | 27 | } |
cittecla | 62:c2fcf3b349e9 | 28 | |
cittecla | 62:c2fcf3b349e9 | 29 | coordinates pos_to_coord(position pos) |
cittecla | 62:c2fcf3b349e9 | 30 | { |
cittecla | 62:c2fcf3b349e9 | 31 | coordinates coord = {0}; |
cittecla | 62:c2fcf3b349e9 | 32 | coord.x = pos.x + 0.5f; |
cittecla | 62:c2fcf3b349e9 | 33 | coord.y = pos.y + 0.5f; |
cittecla | 62:c2fcf3b349e9 | 34 | return coord; |
cittecla | 62:c2fcf3b349e9 | 35 | } |
cittecla | 62:c2fcf3b349e9 | 36 | |
cittecla | 62:c2fcf3b349e9 | 37 | position coord_to_pos(coordinates coord) |
cittecla | 62:c2fcf3b349e9 | 38 | { |
cittecla | 62:c2fcf3b349e9 | 39 | position pos = {0}; |
aeschsim | 69:1fdcef6a7577 | 40 | pos.x = rint(coord.x -0.5f); //round values |
cittecla | 62:c2fcf3b349e9 | 41 | pos.y = rint(coord.y -0.5f); |
cittecla | 62:c2fcf3b349e9 | 42 | return pos; |
cittecla | 62:c2fcf3b349e9 | 43 | } |
cittecla | 62:c2fcf3b349e9 | 44 | |
cittecla | 34:40d8d29b44b8 | 45 | position get_current_pos() |
cittecla | 33:8a98f8b9d859 | 46 | { |
cittecla | 62:c2fcf3b349e9 | 47 | current_pos = coord_to_pos(current_coord); |
cittecla | 33:8a98f8b9d859 | 48 | return current_pos; |
cittecla | 33:8a98f8b9d859 | 49 | } |
cittecla | 38:3526c36e4c73 | 50 | |
cittecla | 52:56399c2f13cd | 51 | position get_next_pos() |
cittecla | 52:56399c2f13cd | 52 | { |
cittecla | 52:56399c2f13cd | 53 | return next_pos; |
cittecla | 52:56399c2f13cd | 54 | } |
cittecla | 52:56399c2f13cd | 55 | |
cittecla | 46:8b52c7b34d34 | 56 | float get_current_heading() |
cittecla | 46:8b52c7b34d34 | 57 | { |
cittecla | 46:8b52c7b34d34 | 58 | return current_heading; |
cittecla | 46:8b52c7b34d34 | 59 | } |
cittecla | 46:8b52c7b34d34 | 60 | |
cittecla | 70:922cbbfebf02 | 61 | void clamp_heading() |
cittecla | 70:922cbbfebf02 | 62 | { |
cittecla | 70:922cbbfebf02 | 63 | |
cittecla | 112:b3270806629f | 64 | while (current_heading >= 360) current_heading -= 360; |
cittecla | 112:b3270806629f | 65 | while (current_heading < 0) current_heading += 360; |
cittecla | 70:922cbbfebf02 | 66 | |
cittecla | 70:922cbbfebf02 | 67 | } |
cittecla | 38:3526c36e4c73 | 68 | |
cittecla | 60:b57577b0072f | 69 | void positioning() |
cittecla | 39:92723f7ea54f | 70 | { |
cittecla | 112:b3270806629f | 71 | //printf("positioning...\r\n"); |
cittecla | 112:b3270806629f | 72 | |
cittecla | 112:b3270806629f | 73 | // get wheel_speed |
cittecla | 112:b3270806629f | 74 | float speed_left = get_speed_left(); |
cittecla | 112:b3270806629f | 75 | float speed_right = get_speed_right(); |
cittecla | 112:b3270806629f | 76 | |
cittecla | 112:b3270806629f | 77 | // calc movement and turning speed |
cittecla | 112:b3270806629f | 78 | float movement_speed = (speed_left + speed_right) /2.0f ; |
cittecla | 112:b3270806629f | 79 | float turning_speed = speed_left - speed_right; |
cittecla | 112:b3270806629f | 80 | |
cittecla | 112:b3270806629f | 81 | // calc heading and coverd distance |
cittecla | 112:b3270806629f | 82 | current_heading += 360/(2*circle_r*M_PI) * ((2*(float)wheel_r*(float)M_PI)/(2*M_PI) * t2.read() * turning_speed*0.1f); |
cittecla | 112:b3270806629f | 83 | float distance = (2*(float)wheel_r*(float)M_PI)/(2*M_PI) * t2.read() * movement_speed * 0.1f; |
cittecla | 112:b3270806629f | 84 | clamp_heading(); |
cittecla | 112:b3270806629f | 85 | t2.reset(); |
cittecla | 112:b3270806629f | 86 | t2.start(); |
cittecla | 112:b3270806629f | 87 | |
cittecla | 112:b3270806629f | 88 | // calc new position |
cittecla | 112:b3270806629f | 89 | current_coord.x += sin(current_heading/180.0f*(float)M_PI)* distance; |
cittecla | 112:b3270806629f | 90 | current_coord.y -= cos(current_heading/180.0f*(float)M_PI) * distance; |
cittecla | 71:ddf4eb5c3081 | 91 | |
cittecla | 118:fbd6d41f6ce8 | 92 | current_pos = coord_to_pos(current_coord); |
cittecla | 39:92723f7ea54f | 93 | } |
cittecla | 38:3526c36e4c73 | 94 | |
cittecla | 38:3526c36e4c73 | 95 | |
cittecla | 62:c2fcf3b349e9 | 96 | |
cittecla | 62:c2fcf3b349e9 | 97 | int initial_positioning() |
cittecla | 33:8a98f8b9d859 | 98 | { |
cittecla | 62:c2fcf3b349e9 | 99 | if(init == false) { |
cittecla | 71:ddf4eb5c3081 | 100 | printf("initial_positioning init\r\n"); |
cittecla | 62:c2fcf3b349e9 | 101 | last_dist_r = 100; |
cittecla | 62:c2fcf3b349e9 | 102 | last_dist_l = 100; |
cittecla | 33:8a98f8b9d859 | 103 | |
cittecla | 112:b3270806629f | 104 | // deg_r = -50; |
cittecla | 112:b3270806629f | 105 | // deg_l = 50; |
cittecla | 112:b3270806629f | 106 | |
cittecla | 72:4e8a151d804e | 107 | deg_r = 0; |
cittecla | 72:4e8a151d804e | 108 | deg_l = 0; |
cittecla | 62:c2fcf3b349e9 | 109 | |
cittecla | 72:4e8a151d804e | 110 | set_servo_position(0,-deg_l); //servo sensor left |
cittecla | 62:c2fcf3b349e9 | 111 | set_servo_position(2,-deg_r); //servo sensor right |
cittecla | 70:922cbbfebf02 | 112 | current_coord.x = 0; |
cittecla | 70:922cbbfebf02 | 113 | current_coord.y = 0; |
cittecla | 62:c2fcf3b349e9 | 114 | t2.start(); |
cittecla | 62:c2fcf3b349e9 | 115 | init = true; |
cittecla | 62:c2fcf3b349e9 | 116 | } else { |
cittecla | 33:8a98f8b9d859 | 117 | |
cittecla | 62:c2fcf3b349e9 | 118 | if(t2 > 0.2f) { |
cittecla | 112:b3270806629f | 119 | |
cittecla | 71:ddf4eb5c3081 | 120 | t2.reset(); |
cittecla | 72:4e8a151d804e | 121 | float dist_r = getDistanceIR(0); |
cittecla | 72:4e8a151d804e | 122 | float dist_l = getDistanceIR(4); |
cittecla | 72:4e8a151d804e | 123 | printf("distances %f || %f\r\n",dist_l, dist_r); |
cittecla | 12:91c2e07d2b5b | 124 | |
cittecla | 62:c2fcf3b349e9 | 125 | //right |
cittecla | 62:c2fcf3b349e9 | 126 | if(dist_r < last_dist_r) { |
cittecla | 62:c2fcf3b349e9 | 127 | last_dist_r = dist_r; |
cittecla | 112:b3270806629f | 128 | |
cittecla | 112:b3270806629f | 129 | |
cittecla | 62:c2fcf3b349e9 | 130 | deg_r += 5; |
cittecla | 62:c2fcf3b349e9 | 131 | set_servo_position(2,-deg_r); |
cittecla | 70:922cbbfebf02 | 132 | } else if(direction_r == 0) { |
cittecla | 70:922cbbfebf02 | 133 | |
cittecla | 70:922cbbfebf02 | 134 | dist_r *= 100; |
cittecla | 70:922cbbfebf02 | 135 | float offsetx = 12; |
cittecla | 70:922cbbfebf02 | 136 | float offsety = 12; |
cittecla | 62:c2fcf3b349e9 | 137 | |
cittecla | 70:922cbbfebf02 | 138 | float x = (offsetx + sin(deg_r/180*(float)M_PI)*dist_r)/4; |
cittecla | 70:922cbbfebf02 | 139 | float y = (-offsety - cos(deg_r/180*(float)M_PI)*dist_r)/4; |
cittecla | 70:922cbbfebf02 | 140 | float hyp = sqrt(x*x+y*y); // y pos |
cittecla | 70:922cbbfebf02 | 141 | direction_r = asin(x/hyp)/(float)M_PI*180; // winkel |
cittecla | 70:922cbbfebf02 | 142 | |
cittecla | 70:922cbbfebf02 | 143 | current_coord.y = hyp; |
cittecla | 70:922cbbfebf02 | 144 | |
cittecla | 70:922cbbfebf02 | 145 | while (direction_r >= 360) direction_r -= 360; |
cittecla | 70:922cbbfebf02 | 146 | while (direction_r < 0) direction_r += 360; |
cittecla | 62:c2fcf3b349e9 | 147 | } |
cittecla | 80:92b9d083322d | 148 | |
cittecla | 62:c2fcf3b349e9 | 149 | //left |
cittecla | 70:922cbbfebf02 | 150 | if(dist_l < last_dist_l) { |
cittecla | 70:922cbbfebf02 | 151 | last_dist_l = dist_l; |
cittecla | 70:922cbbfebf02 | 152 | deg_l -= 5; |
cittecla | 72:4e8a151d804e | 153 | set_servo_position(0,-deg_l); |
cittecla | 70:922cbbfebf02 | 154 | } else if(direction_l == 0) { |
cittecla | 70:922cbbfebf02 | 155 | |
cittecla | 70:922cbbfebf02 | 156 | dist_r *= 100; |
cittecla | 70:922cbbfebf02 | 157 | float offsetx = -12; |
cittecla | 70:922cbbfebf02 | 158 | float offsety = 12; |
cittecla | 70:922cbbfebf02 | 159 | |
cittecla | 70:922cbbfebf02 | 160 | float x = (offsetx + sin(deg_l/180*(float)M_PI)*dist_l)/4; |
cittecla | 70:922cbbfebf02 | 161 | float y = (-offsety - cos(deg_l/180*(float)M_PI)*dist_l)/4; |
cittecla | 70:922cbbfebf02 | 162 | float hyp = sqrt(x*x+y*y); // y pos |
cittecla | 70:922cbbfebf02 | 163 | direction_l = asin(x/hyp)/(float)M_PI*180; // winkel |
cittecla | 70:922cbbfebf02 | 164 | |
cittecla | 70:922cbbfebf02 | 165 | current_coord.x = hyp; |
cittecla | 70:922cbbfebf02 | 166 | |
cittecla | 70:922cbbfebf02 | 167 | while (direction_l >= 360) direction_l -= 360; |
cittecla | 70:922cbbfebf02 | 168 | while (direction_l < 0) direction_l += 360; |
cittecla | 70:922cbbfebf02 | 169 | } |
cittecla | 70:922cbbfebf02 | 170 | |
cittecla | 70:922cbbfebf02 | 171 | if(current_coord.x != 0 && current_coord.y != 0) { |
cittecla | 70:922cbbfebf02 | 172 | printf("directions: %f || %f \r\n", direction_l, direction_r); |
cittecla | 70:922cbbfebf02 | 173 | current_heading = (360-direction_r + 270-direction_l)/2; |
cittecla | 70:922cbbfebf02 | 174 | |
cittecla | 70:922cbbfebf02 | 175 | clamp_heading(); |
cittecla | 70:922cbbfebf02 | 176 | printf("heading: %f \r\n", current_heading); |
cittecla | 70:922cbbfebf02 | 177 | |
cittecla | 70:922cbbfebf02 | 178 | //finished |
cittecla | 71:ddf4eb5c3081 | 179 | return 17; |
cittecla | 70:922cbbfebf02 | 180 | |
cittecla | 80:92b9d083322d | 181 | } |
cittecla | 62:c2fcf3b349e9 | 182 | } |
cittecla | 33:8a98f8b9d859 | 183 | } |
cittecla | 70:922cbbfebf02 | 184 | // not finished |
cittecla | 80:92b9d083322d | 185 | return 11; |
cittecla | 62:c2fcf3b349e9 | 186 | } |
cittecla | 62:c2fcf3b349e9 | 187 | |
cittecla | 62:c2fcf3b349e9 | 188 |