Tobis Programm forked to not destroy your golden files

Dependencies:   mbed

Fork of Robocode by PES 2 - Gruppe 1

Committer:
aeschsim
Date:
Fri May 12 15:38:01 2017 +0000
Revision:
121:af16ffc05593
Parent:
120:cdf7a6751f9e
Child:
125:573a18c2155f
changed offset calculation, mistake in atan(a/b)

Who changed what in which revision?

UserRevisionLine numberNew 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 120:cdf7a6751f9e 110 set_servo_position(0,deg_l); //servo sensor left
cittecla 120:cdf7a6751f9e 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 120:cdf7a6751f9e 130 deg_r += 3;
cittecla 120:cdf7a6751f9e 131 set_servo_position(2,deg_r);
cittecla 120:cdf7a6751f9e 132
cittecla 120:cdf7a6751f9e 133 } else if(current_coord.y == 0) {
cittecla 120:cdf7a6751f9e 134 deg_r -= 5; //compensate overturning
cittecla 120:cdf7a6751f9e 135 //deg_r -= 3; // compensate not straight looking
cittecla 70:922cbbfebf02 136 dist_r *= 100;
cittecla 120:cdf7a6751f9e 137 float offsetx = 11.5;
cittecla 120:cdf7a6751f9e 138 float offsety = 11;
cittecla 62:c2fcf3b349e9 139
aeschsim 121:af16ffc05593 140 current_coord.y = dist_r + sqrt(offsetx*offsetx+offsety*offsety)*cos((90-deg_r-atan(offsety/offsetx))/180.0f*(float)M_PI);
cittecla 70:922cbbfebf02 141
cittecla 70:922cbbfebf02 142
cittecla 62:c2fcf3b349e9 143 }
cittecla 80:92b9d083322d 144
cittecla 62:c2fcf3b349e9 145 //left
cittecla 70:922cbbfebf02 146 if(dist_l < last_dist_l) {
cittecla 70:922cbbfebf02 147 last_dist_l = dist_l;
cittecla 70:922cbbfebf02 148 deg_l -= 5;
cittecla 120:cdf7a6751f9e 149 set_servo_position(0,deg_l);
cittecla 120:cdf7a6751f9e 150
cittecla 120:cdf7a6751f9e 151 } else if(current_coord.x == 0) {
cittecla 120:cdf7a6751f9e 152 deg_l += 3; //compensate overturning
cittecla 120:cdf7a6751f9e 153 deg_l -= 3; //compensate not straight looking
cittecla 70:922cbbfebf02 154 dist_r *= 100;
cittecla 120:cdf7a6751f9e 155 float offsetx = 11.5;
cittecla 120:cdf7a6751f9e 156 float offsety = 11;
cittecla 70:922cbbfebf02 157
aeschsim 121:af16ffc05593 158 current_coord.x = dist_r + sqrt(offsetx*offsetx+offsety*offsety)*cos((90-fabsf(deg_l)-atan(offsetx/offsety))/180.0f*(float)M_PI);
cittecla 70:922cbbfebf02 159
cittecla 70:922cbbfebf02 160
cittecla 70:922cbbfebf02 161 }
cittecla 70:922cbbfebf02 162
cittecla 70:922cbbfebf02 163 if(current_coord.x != 0 && current_coord.y != 0) {
cittecla 120:cdf7a6751f9e 164
cittecla 120:cdf7a6751f9e 165 printf("degs: %d || %d \r\n", deg_l , deg_r);
cittecla 120:cdf7a6751f9e 166 printf("coords : %f || %f \r\n", current_coord.x, current_coord.y);
cittecla 120:cdf7a6751f9e 167 current_heading = (360-deg_r + 270-deg_l)/2;
cittecla 70:922cbbfebf02 168
cittecla 70:922cbbfebf02 169 clamp_heading();
cittecla 70:922cbbfebf02 170 printf("heading: %f \r\n", current_heading);
cittecla 120:cdf7a6751f9e 171
cittecla 120:cdf7a6751f9e 172 current_coord.x = 20;
cittecla 120:cdf7a6751f9e 173 current_coord.y = 20;
cittecla 70:922cbbfebf02 174
cittecla 70:922cbbfebf02 175 //finished
cittecla 120:cdf7a6751f9e 176 return 11;
cittecla 70:922cbbfebf02 177
cittecla 80:92b9d083322d 178 }
cittecla 62:c2fcf3b349e9 179 }
cittecla 33:8a98f8b9d859 180 }
cittecla 70:922cbbfebf02 181 // not finished
cittecla 120:cdf7a6751f9e 182 return 17;
cittecla 62:c2fcf3b349e9 183 }
cittecla 62:c2fcf3b349e9 184
cittecla 62:c2fcf3b349e9 185