PES 2 - Gruppe 1 / Mbed 2 deprecated Robocode_Random

Dependencies:   mbed

Fork of Robocode by PES 2 - Gruppe 1

Committer:
cittecla
Date:
Thu Apr 20 11:37:24 2017 +0000
Revision:
71:ddf4eb5c3081
Parent:
70:922cbbfebf02
Child:
72:4e8a151d804e
Init runs successfully, not tested with robot, parameters for init not final

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 64:c2fcf3b349e9 8 coordinates current_coord; // Updated
cittecla 64: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 64:c2fcf3b349e9 13 bool init = false;
aeschsim 68:29c52ea147d5 14 Timer t2;
cittecla 64:c2fcf3b349e9 15
cittecla 64:c2fcf3b349e9 16 int deg_r = 0;
cittecla 64:c2fcf3b349e9 17 int deg_l = 0;
cittecla 64:c2fcf3b349e9 18 float last_dist_r = 0;
cittecla 64:c2fcf3b349e9 19 float last_dist_l = 0;
cittecla 70:922cbbfebf02 20 float direction_r = 0;
cittecla 70:922cbbfebf02 21 float direction_l = 0;
cittecla 64:c2fcf3b349e9 22
cittecla 64:c2fcf3b349e9 23
cittecla 64:c2fcf3b349e9 24 coordinates get_current_coord()
cittecla 64:c2fcf3b349e9 25 {
cittecla 64:c2fcf3b349e9 26 return current_coord;
cittecla 64:c2fcf3b349e9 27 }
cittecla 64:c2fcf3b349e9 28
cittecla 64:c2fcf3b349e9 29 coordinates pos_to_coord(position pos)
cittecla 64:c2fcf3b349e9 30 {
cittecla 64:c2fcf3b349e9 31 coordinates coord = {0};
cittecla 64:c2fcf3b349e9 32 coord.x = pos.x + 0.5f;
cittecla 64:c2fcf3b349e9 33 coord.y = pos.y + 0.5f;
cittecla 64:c2fcf3b349e9 34 return coord;
cittecla 64:c2fcf3b349e9 35 }
cittecla 64:c2fcf3b349e9 36
cittecla 64:c2fcf3b349e9 37 position coord_to_pos(coordinates coord)
cittecla 64:c2fcf3b349e9 38 {
cittecla 64:c2fcf3b349e9 39 position pos = {0};
aeschsim 69:1fdcef6a7577 40 pos.x = rint(coord.x -0.5f); //round values
cittecla 64:c2fcf3b349e9 41 pos.y = rint(coord.y -0.5f);
cittecla 64:c2fcf3b349e9 42 return pos;
cittecla 64:c2fcf3b349e9 43 }
cittecla 64:c2fcf3b349e9 44
cittecla 34:40d8d29b44b8 45 position get_current_pos()
cittecla 33:8a98f8b9d859 46 {
cittecla 64: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 61:b57577b0072f 69 void positioning()
cittecla 39:92723f7ea54f 70 {
cittecla 61: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 64:c2fcf3b349e9 79
cittecla 64:c2fcf3b349e9 80 int initial_positioning()
cittecla 33:8a98f8b9d859 81 {
cittecla 64:c2fcf3b349e9 82 if(init == false) {
cittecla 71:ddf4eb5c3081 83 printf("initial_positioning init\r\n");
cittecla 64:c2fcf3b349e9 84 last_dist_r = 100;
cittecla 64:c2fcf3b349e9 85 last_dist_l = 100;
cittecla 33:8a98f8b9d859 86
cittecla 64:c2fcf3b349e9 87 deg_r = -50;
cittecla 64:c2fcf3b349e9 88 deg_l = 50;
cittecla 64:c2fcf3b349e9 89
cittecla 64:c2fcf3b349e9 90 set_servo_position(0,deg_l); //servo sensor left
cittecla 64:c2fcf3b349e9 91 set_servo_position(2,-deg_r); //servo sensor right
cittecla 70:922cbbfebf02 92 current_coord.x = 0;
cittecla 70:922cbbfebf02 93 current_coord.y = 0;
cittecla 64:c2fcf3b349e9 94 t2.start();
cittecla 64:c2fcf3b349e9 95 init = true;
cittecla 64:c2fcf3b349e9 96 } else {
cittecla 33:8a98f8b9d859 97
cittecla 64:c2fcf3b349e9 98 if(t2 > 0.2f) {
cittecla 71:ddf4eb5c3081 99 t2.reset();
cittecla 64:c2fcf3b349e9 100 float dist_r = getDistanceIR(4);
cittecla 64:c2fcf3b349e9 101 float dist_l = getDistanceIR(0);
cittecla 12:91c2e07d2b5b 102
cittecla 64:c2fcf3b349e9 103 //right
cittecla 64:c2fcf3b349e9 104 if(dist_r < last_dist_r) {
cittecla 64:c2fcf3b349e9 105 last_dist_r = dist_r;
cittecla 64:c2fcf3b349e9 106 deg_r += 5;
cittecla 64:c2fcf3b349e9 107 set_servo_position(2,-deg_r);
cittecla 70:922cbbfebf02 108 } else if(direction_r == 0) {
cittecla 70:922cbbfebf02 109
cittecla 70:922cbbfebf02 110 dist_r *= 100;
cittecla 70:922cbbfebf02 111 float offsetx = 12;
cittecla 70:922cbbfebf02 112 float offsety = 12;
cittecla 64:c2fcf3b349e9 113
cittecla 70:922cbbfebf02 114 float x = (offsetx + sin(deg_r/180*(float)M_PI)*dist_r)/4;
cittecla 70:922cbbfebf02 115 float y = (-offsety - cos(deg_r/180*(float)M_PI)*dist_r)/4;
cittecla 70:922cbbfebf02 116 float hyp = sqrt(x*x+y*y); // y pos
cittecla 70:922cbbfebf02 117 direction_r = asin(x/hyp)/(float)M_PI*180; // winkel
cittecla 70:922cbbfebf02 118
cittecla 70:922cbbfebf02 119 current_coord.y = hyp;
cittecla 70:922cbbfebf02 120
cittecla 70:922cbbfebf02 121 while (direction_r >= 360) direction_r -= 360;
cittecla 70:922cbbfebf02 122 while (direction_r < 0) direction_r += 360;
cittecla 64:c2fcf3b349e9 123 }
cittecla 64:c2fcf3b349e9 124
cittecla 64:c2fcf3b349e9 125
cittecla 64:c2fcf3b349e9 126 //left
cittecla 70:922cbbfebf02 127 if(dist_l < last_dist_l) {
cittecla 70:922cbbfebf02 128 last_dist_l = dist_l;
cittecla 70:922cbbfebf02 129 deg_l -= 5;
cittecla 70:922cbbfebf02 130 set_servo_position(0,deg_l);
cittecla 70:922cbbfebf02 131 } else if(direction_l == 0) {
cittecla 70:922cbbfebf02 132
cittecla 70:922cbbfebf02 133 dist_r *= 100;
cittecla 70:922cbbfebf02 134 float offsetx = -12;
cittecla 70:922cbbfebf02 135 float offsety = 12;
cittecla 70:922cbbfebf02 136
cittecla 70:922cbbfebf02 137 float x = (offsetx + sin(deg_l/180*(float)M_PI)*dist_l)/4;
cittecla 70:922cbbfebf02 138 float y = (-offsety - cos(deg_l/180*(float)M_PI)*dist_l)/4;
cittecla 70:922cbbfebf02 139 float hyp = sqrt(x*x+y*y); // y pos
cittecla 70:922cbbfebf02 140 direction_l = asin(x/hyp)/(float)M_PI*180; // winkel
cittecla 70:922cbbfebf02 141
cittecla 70:922cbbfebf02 142 current_coord.x = hyp;
cittecla 70:922cbbfebf02 143
cittecla 70:922cbbfebf02 144 while (direction_l >= 360) direction_l -= 360;
cittecla 70:922cbbfebf02 145 while (direction_l < 0) direction_l += 360;
cittecla 70:922cbbfebf02 146 }
cittecla 70:922cbbfebf02 147
cittecla 70:922cbbfebf02 148 if(current_coord.x != 0 && current_coord.y != 0) {
cittecla 70:922cbbfebf02 149 printf("directions: %f || %f \r\n", direction_l, direction_r);
cittecla 70:922cbbfebf02 150 current_heading = (360-direction_r + 270-direction_l)/2;
cittecla 70:922cbbfebf02 151
cittecla 70:922cbbfebf02 152 clamp_heading();
cittecla 70:922cbbfebf02 153 printf("heading: %f \r\n", current_heading);
cittecla 70:922cbbfebf02 154
cittecla 70:922cbbfebf02 155 //finished
cittecla 71:ddf4eb5c3081 156 return 17;
cittecla 70:922cbbfebf02 157
cittecla 70:922cbbfebf02 158 }
cittecla 64:c2fcf3b349e9 159 }
cittecla 33:8a98f8b9d859 160 }
cittecla 70:922cbbfebf02 161 // not finished
cittecla 64:c2fcf3b349e9 162 return 16;
cittecla 64:c2fcf3b349e9 163 }
cittecla 64:c2fcf3b349e9 164
cittecla 64:c2fcf3b349e9 165