Tobis Programm forked to not destroy your golden files

Dependencies:   mbed

Fork of Robocode by PES 2 - Gruppe 1

Committer:
cittecla
Date:
Wed Apr 26 07:30:03 2017 +0000
Revision:
80:92b9d083322d
Parent:
72:4e8a151d804e
Child:
112:b3270806629f
Fixed IRSensor reading error.

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 62:c2fcf3b349e9 8 coordinates current_coord; // Updated
cittecla 62:c2fcf3b349e9 9 position current_pos; // 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 70:922cbbfebf02 64 while (direction_l >= 360) direction_l -= 360;
cittecla 70:922cbbfebf02 65 while (direction_l < 0) direction_l += 360;
cittecla 70:922cbbfebf02 66
cittecla 70:922cbbfebf02 67 }
cittecla 38:3526c36e4c73 68
cittecla 60:b57577b0072f 69 void positioning()
cittecla 39:92723f7ea54f 70 {
cittecla 60:b57577b0072f 71 printf("positioning...\r\n");
cittecla 71:ddf4eb5c3081 72
cittecla 71:ddf4eb5c3081 73
cittecla 71:ddf4eb5c3081 74
cittecla 70:922cbbfebf02 75 clamp_heading();
cittecla 39:92723f7ea54f 76 }
cittecla 38:3526c36e4c73 77
cittecla 38:3526c36e4c73 78
cittecla 62:c2fcf3b349e9 79
cittecla 62:c2fcf3b349e9 80 int initial_positioning()
cittecla 33:8a98f8b9d859 81 {
cittecla 62:c2fcf3b349e9 82 if(init == false) {
cittecla 71:ddf4eb5c3081 83 printf("initial_positioning init\r\n");
cittecla 62:c2fcf3b349e9 84 last_dist_r = 100;
cittecla 62:c2fcf3b349e9 85 last_dist_l = 100;
cittecla 33:8a98f8b9d859 86
cittecla 72:4e8a151d804e 87 // deg_r = -50;
cittecla 72:4e8a151d804e 88 // deg_l = 50;
cittecla 72:4e8a151d804e 89
cittecla 72:4e8a151d804e 90 deg_r = 0;
cittecla 72:4e8a151d804e 91 deg_l = 0;
cittecla 62:c2fcf3b349e9 92
cittecla 72:4e8a151d804e 93 set_servo_position(0,-deg_l); //servo sensor left
cittecla 62:c2fcf3b349e9 94 set_servo_position(2,-deg_r); //servo sensor right
cittecla 70:922cbbfebf02 95 current_coord.x = 0;
cittecla 70:922cbbfebf02 96 current_coord.y = 0;
cittecla 62:c2fcf3b349e9 97 t2.start();
cittecla 62:c2fcf3b349e9 98 init = true;
cittecla 62:c2fcf3b349e9 99 } else {
cittecla 33:8a98f8b9d859 100
cittecla 62:c2fcf3b349e9 101 if(t2 > 0.2f) {
cittecla 72:4e8a151d804e 102
cittecla 71:ddf4eb5c3081 103 t2.reset();
cittecla 72:4e8a151d804e 104 float dist_r = getDistanceIR(0);
cittecla 72:4e8a151d804e 105 float dist_l = getDistanceIR(4);
cittecla 72:4e8a151d804e 106 printf("distances %f || %f\r\n",dist_l, dist_r);
cittecla 12:91c2e07d2b5b 107
cittecla 62:c2fcf3b349e9 108 //right
cittecla 62:c2fcf3b349e9 109 if(dist_r < last_dist_r) {
cittecla 62:c2fcf3b349e9 110 last_dist_r = dist_r;
cittecla 72:4e8a151d804e 111
cittecla 72:4e8a151d804e 112
cittecla 62:c2fcf3b349e9 113 deg_r += 5;
cittecla 62:c2fcf3b349e9 114 set_servo_position(2,-deg_r);
cittecla 70:922cbbfebf02 115 } else if(direction_r == 0) {
cittecla 70:922cbbfebf02 116
cittecla 70:922cbbfebf02 117 dist_r *= 100;
cittecla 70:922cbbfebf02 118 float offsetx = 12;
cittecla 70:922cbbfebf02 119 float offsety = 12;
cittecla 62:c2fcf3b349e9 120
cittecla 70:922cbbfebf02 121 float x = (offsetx + sin(deg_r/180*(float)M_PI)*dist_r)/4;
cittecla 70:922cbbfebf02 122 float y = (-offsety - cos(deg_r/180*(float)M_PI)*dist_r)/4;
cittecla 70:922cbbfebf02 123 float hyp = sqrt(x*x+y*y); // y pos
cittecla 70:922cbbfebf02 124 direction_r = asin(x/hyp)/(float)M_PI*180; // winkel
cittecla 70:922cbbfebf02 125
cittecla 70:922cbbfebf02 126 current_coord.y = hyp;
cittecla 70:922cbbfebf02 127
cittecla 70:922cbbfebf02 128 while (direction_r >= 360) direction_r -= 360;
cittecla 70:922cbbfebf02 129 while (direction_r < 0) direction_r += 360;
cittecla 62:c2fcf3b349e9 130 }
cittecla 80:92b9d083322d 131
cittecla 62:c2fcf3b349e9 132 //left
cittecla 70:922cbbfebf02 133 if(dist_l < last_dist_l) {
cittecla 70:922cbbfebf02 134 last_dist_l = dist_l;
cittecla 70:922cbbfebf02 135 deg_l -= 5;
cittecla 72:4e8a151d804e 136 set_servo_position(0,-deg_l);
cittecla 70:922cbbfebf02 137 } else if(direction_l == 0) {
cittecla 70:922cbbfebf02 138
cittecla 70:922cbbfebf02 139 dist_r *= 100;
cittecla 70:922cbbfebf02 140 float offsetx = -12;
cittecla 70:922cbbfebf02 141 float offsety = 12;
cittecla 70:922cbbfebf02 142
cittecla 70:922cbbfebf02 143 float x = (offsetx + sin(deg_l/180*(float)M_PI)*dist_l)/4;
cittecla 70:922cbbfebf02 144 float y = (-offsety - cos(deg_l/180*(float)M_PI)*dist_l)/4;
cittecla 70:922cbbfebf02 145 float hyp = sqrt(x*x+y*y); // y pos
cittecla 70:922cbbfebf02 146 direction_l = asin(x/hyp)/(float)M_PI*180; // winkel
cittecla 70:922cbbfebf02 147
cittecla 70:922cbbfebf02 148 current_coord.x = hyp;
cittecla 70:922cbbfebf02 149
cittecla 70:922cbbfebf02 150 while (direction_l >= 360) direction_l -= 360;
cittecla 70:922cbbfebf02 151 while (direction_l < 0) direction_l += 360;
cittecla 70:922cbbfebf02 152 }
cittecla 70:922cbbfebf02 153
cittecla 70:922cbbfebf02 154 if(current_coord.x != 0 && current_coord.y != 0) {
cittecla 70:922cbbfebf02 155 printf("directions: %f || %f \r\n", direction_l, direction_r);
cittecla 70:922cbbfebf02 156 current_heading = (360-direction_r + 270-direction_l)/2;
cittecla 70:922cbbfebf02 157
cittecla 70:922cbbfebf02 158 clamp_heading();
cittecla 70:922cbbfebf02 159 printf("heading: %f \r\n", current_heading);
cittecla 70:922cbbfebf02 160
cittecla 70:922cbbfebf02 161 //finished
cittecla 71:ddf4eb5c3081 162 return 17;
cittecla 70:922cbbfebf02 163
cittecla 80:92b9d083322d 164 }
cittecla 62:c2fcf3b349e9 165 }
cittecla 33:8a98f8b9d859 166 }
cittecla 70:922cbbfebf02 167 // not finished
cittecla 80:92b9d083322d 168 return 11;
cittecla 62:c2fcf3b349e9 169 }
cittecla 62:c2fcf3b349e9 170
cittecla 62:c2fcf3b349e9 171