Tobis Programm forked to not destroy your golden files

Dependencies:   mbed

Fork of Robocode by PES 2 - Gruppe 1

Committer:
cittecla
Date:
Mon May 01 12:55:11 2017 +0000
Revision:
86:df8c869a5a52
Parent:
85:d8ea8a99fa3a
Parent:
83:5d3bca1ece20
Child:
88:b89cace9329b
Implemented move_in_search_for_brick with state machine + test function move forward

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cittecla 18:a82994e67297 1 /**
cittecla 18:a82994e67297 2 * Movement function library
cittecla 18:a82994e67297 3 * Handels Movement of the Robot
cittecla 18:a82994e67297 4 **/
cittecla 39:92723f7ea54f 5
cittecla 39:92723f7ea54f 6 #include "Movement.h"
cittecla 86:df8c869a5a52 7 #define OFFSET_GREIFER_TO_IRSENSOR 0.15 // Constant for distance between front IR Sensor and the postion where the Greifer is in grabbing Position
cittecla 39:92723f7ea54f 8
cittecla 85:d8ea8a99fa3a 9 bool is_moving = false;
cittecla 85:d8ea8a99fa3a 10 float wanted_dist = 0;
cittecla 39:92723f7ea54f 11
cittecla 61:628f8a4e857c 12 bool is_turning = false;
cittecla 61:628f8a4e857c 13 bool direction = false;
cittecla 61:628f8a4e857c 14 float wanted_deg = 0;
cittecla 63:b27aa01c2cf6 15 Timer t;
cittecla 61:628f8a4e857c 16 float previous_t = 0;
cittecla 86:df8c869a5a52 17 int search_state = 0;
cittecla 77:ff87a10c4baf 18 float restdeg = 0;
cittecla 77:ff87a10c4baf 19
cittecla 61:628f8a4e857c 20
cittecla 52:56399c2f13cd 21 int moving()
cittecla 52:56399c2f13cd 22 {
cittecla 52:56399c2f13cd 23
cittecla 39:92723f7ea54f 24 return 0;
cittecla 52:56399c2f13cd 25 }
cittecla 52:56399c2f13cd 26
cittecla 86:df8c869a5a52 27 void stop_move()
cittecla 86:df8c869a5a52 28 {
cittecla 86:df8c869a5a52 29 set_speed(0,0);
cittecla 86:df8c869a5a52 30 wanted_dist = 0;
cittecla 86:df8c869a5a52 31 is_moving = false;
cittecla 86:df8c869a5a52 32 }
cittecla 86:df8c869a5a52 33
cittecla 86:df8c869a5a52 34 void stop_turn()
cittecla 86:df8c869a5a52 35 {
cittecla 86:df8c869a5a52 36 set_speed(0,0);
cittecla 86:df8c869a5a52 37 wanted_deg = 0;
cittecla 86:df8c869a5a52 38 is_turning = false;
cittecla 86:df8c869a5a52 39 }
cittecla 86:df8c869a5a52 40
cittecla 85:d8ea8a99fa3a 41 float move_for_distance(float distance)
cittecla 52:56399c2f13cd 42 {
cittecla 85:d8ea8a99fa3a 43 if(distance != 0) {
cittecla 52:56399c2f13cd 44
cittecla 85:d8ea8a99fa3a 45 is_moving = true;
cittecla 85:d8ea8a99fa3a 46 float left = 0;
cittecla 85:d8ea8a99fa3a 47 float right = 0;
cittecla 85:d8ea8a99fa3a 48
cittecla 85:d8ea8a99fa3a 49 wanted_dist = sqrt(distance*distance);
cittecla 85:d8ea8a99fa3a 50
cittecla 85:d8ea8a99fa3a 51 if(distance > 0) { //move forward
cittecla 85:d8ea8a99fa3a 52 direction = 1;
cittecla 85:d8ea8a99fa3a 53 left = 50.0f;
cittecla 85:d8ea8a99fa3a 54 right = 50.0f;
cittecla 85:d8ea8a99fa3a 55 } else { //move backward
cittecla 85:d8ea8a99fa3a 56 direction = 0;
cittecla 85:d8ea8a99fa3a 57 left = -50.0f;
cittecla 85:d8ea8a99fa3a 58 right = -50.0f;
cittecla 85:d8ea8a99fa3a 59 }
cittecla 85:d8ea8a99fa3a 60 set_speed(left, right);
cittecla 85:d8ea8a99fa3a 61 t.reset();
cittecla 85:d8ea8a99fa3a 62 t.start();
cittecla 85:d8ea8a99fa3a 63
cittecla 85:d8ea8a99fa3a 64 } else {
cittecla 85:d8ea8a99fa3a 65
cittecla 85:d8ea8a99fa3a 66 float speed_left = get_speed_left();
cittecla 85:d8ea8a99fa3a 67 wanted_dist -= (2*(float)wheel_r*(float)M_PI) * t.read() * fabsf(speed_left);
cittecla 85:d8ea8a99fa3a 68 t.reset();
cittecla 85:d8ea8a99fa3a 69
cittecla 85:d8ea8a99fa3a 70 if(wanted_dist <= 0) { //distance covered, Stop function
cittecla 85:d8ea8a99fa3a 71 set_speed(0,0);
cittecla 85:d8ea8a99fa3a 72 is_moving = false;
cittecla 85:d8ea8a99fa3a 73 t.stop();
cittecla 85:d8ea8a99fa3a 74 }
cittecla 85:d8ea8a99fa3a 75 }
cittecla 85:d8ea8a99fa3a 76 return wanted_dist;
cittecla 52:56399c2f13cd 77 }
cittecla 52:56399c2f13cd 78
cittecla 52:56399c2f13cd 79 int move_backward_for_distance()
cittecla 52:56399c2f13cd 80 {
cittecla 52:56399c2f13cd 81
cittecla 39:92723f7ea54f 82 return 0;
cittecla 52:56399c2f13cd 83 }
cittecla 52:56399c2f13cd 84
cittecla 85:d8ea8a99fa3a 85 float turn_for_deg(float deg) //if deg not 0 equals initilisation.
cittecla 52:56399c2f13cd 86 {
cittecla 52:56399c2f13cd 87
cittecla 75:dba260cb5ae4 88 if(deg != 0) {
cittecla 61:628f8a4e857c 89
cittecla 61:628f8a4e857c 90 is_turning = true;
cittecla 61:628f8a4e857c 91 float left = 0;
cittecla 61:628f8a4e857c 92 float right = 0;
cittecla 61:628f8a4e857c 93
cittecla 75:dba260cb5ae4 94 wanted_deg = sqrt(deg*deg);
cittecla 61:628f8a4e857c 95
cittecla 61:628f8a4e857c 96 if(deg < 0) { // turn left
cittecla 61:628f8a4e857c 97 direction = 1;
cittecla 61:628f8a4e857c 98 left = -50.0f;
cittecla 61:628f8a4e857c 99 right = 50.0f;
cittecla 61:628f8a4e857c 100 } else { // turn right
cittecla 61:628f8a4e857c 101 direction = 0;
cittecla 61:628f8a4e857c 102 left = 50.0f;
cittecla 61:628f8a4e857c 103 right = -50.0f;
cittecla 61:628f8a4e857c 104 }
cittecla 61:628f8a4e857c 105 set_speed(left, right);
cittecla 71:ddf4eb5c3081 106 t.reset();
cittecla 61:628f8a4e857c 107 t.start();
cittecla 61:628f8a4e857c 108
cittecla 61:628f8a4e857c 109 } else {
cittecla 61:628f8a4e857c 110
cittecla 61:628f8a4e857c 111 float speed_left = get_speed_left();
cittecla 85:d8ea8a99fa3a 112 wanted_deg -= 360.0f / (2*(float)wheel_r*(float)M_PI) * t.read() * fabsf(speed_left);
cittecla 75:dba260cb5ae4 113 t.reset();
cittecla 75:dba260cb5ae4 114 if(wanted_deg <= 0) {
cittecla 61:628f8a4e857c 115 set_speed(0,0);
cittecla 61:628f8a4e857c 116 is_turning = false;
cittecla 61:628f8a4e857c 117 t.stop();
cittecla 61:628f8a4e857c 118 }
cittecla 61:628f8a4e857c 119 }
cittecla 75:dba260cb5ae4 120 return (wanted_deg);
cittecla 52:56399c2f13cd 121 }
cittecla 52:56399c2f13cd 122
cittecla 52:56399c2f13cd 123
cittecla 52:56399c2f13cd 124 int move_to_next_coord()
cittecla 52:56399c2f13cd 125 {
cittecla 52:56399c2f13cd 126
cittecla 52:56399c2f13cd 127 float current_heading = get_current_heading();
cittecla 53:453f24775644 128 position current_pos = get_current_pos();
cittecla 52:56399c2f13cd 129 position next_pos = get_next_pos();
cittecla 61:628f8a4e857c 130
cittecla 52:56399c2f13cd 131 float needed_heading = 0;
cittecla 52:56399c2f13cd 132 float distance = 0;
cittecla 61:628f8a4e857c 133
cittecla 52:56399c2f13cd 134 // nord(-y) = 0 grad
cittecla 61:628f8a4e857c 135 if(current_pos.y > next_pos.y) {
cittecla 61:628f8a4e857c 136 if(current_pos.x > next_pos.x) needed_heading = 315;
cittecla 61:628f8a4e857c 137 distance = sqrt2;
cittecla 61:628f8a4e857c 138 if(current_pos.x == next_pos.x) needed_heading = 0;
cittecla 61:628f8a4e857c 139 distance = 1;
cittecla 61:628f8a4e857c 140 if(current_pos.x < next_pos.x) needed_heading = 45;
cittecla 61:628f8a4e857c 141 distance = sqrt2;
cittecla 61:628f8a4e857c 142 }
cittecla 61:628f8a4e857c 143 if(current_pos.y == next_pos.y) {
cittecla 61:628f8a4e857c 144 if(current_pos.x > next_pos.x) needed_heading = 270;
cittecla 61:628f8a4e857c 145 distance = 1;
cittecla 53:453f24775644 146 if(current_pos.x == next_pos.x) //error same position;
cittecla 61:628f8a4e857c 147 if(current_pos.x < next_pos.x) needed_heading = 90;
cittecla 61:628f8a4e857c 148 distance = 1;
cittecla 61:628f8a4e857c 149 }
cittecla 61:628f8a4e857c 150 if(current_pos.y < next_pos.y) {
cittecla 61:628f8a4e857c 151 if(current_pos.x > next_pos.x) needed_heading = 225;
cittecla 61:628f8a4e857c 152 distance = sqrt2;
cittecla 61:628f8a4e857c 153 if(current_pos.x == next_pos.x) needed_heading = 180;
cittecla 61:628f8a4e857c 154 distance = 1;
cittecla 61:628f8a4e857c 155 if(current_pos.x < next_pos.x) needed_heading = 135;
cittecla 61:628f8a4e857c 156 distance = sqrt2;
cittecla 61:628f8a4e857c 157 }
cittecla 52:56399c2f13cd 158
cittecla 52:56399c2f13cd 159 if(needed_heading != current_heading) {
cittecla 53:453f24775644 160 turn_for_deg((needed_heading-current_heading));
cittecla 61:628f8a4e857c 161 } else {
cittecla 85:d8ea8a99fa3a 162 move_for_distance(distance);
cittecla 39:92723f7ea54f 163 }
cittecla 39:92723f7ea54f 164 return 0;
cittecla 52:56399c2f13cd 165 }
cittecla 52:56399c2f13cd 166
PESGruppe1 74:d9c387b83196 167 // Tobias Berger
cittecla 52:56399c2f13cd 168 int move_in_search_for_brick()
cittecla 52:56399c2f13cd 169 {
PESGruppe1 79:50f8d58f79ab 170 float upper = getDistanceIR(2); // get distance from upper Center Sensor
PESGruppe1 79:50f8d58f79ab 171 float lower = getDistanceIR(3); // get distance from Lower Center Sensor
cittecla 85:d8ea8a99fa3a 172
cittecla 86:df8c869a5a52 173 switch (search_state) {
cittecla 86:df8c869a5a52 174 case 0: //first cycle right
cittecla 86:df8c869a5a52 175 turn_for_deg(60.0f); // call function and start turning
cittecla 86:df8c869a5a52 176 search_state = 1;
cittecla 86:df8c869a5a52 177 break;
cittecla 85:d8ea8a99fa3a 178
cittecla 86:df8c869a5a52 179 case 1: // turn right 60 deg
cittecla 86:df8c869a5a52 180 if((lower<0.75f)&&(lower>0.1f)) { // if something is in the range of 10 to 80cm at the lower Sensor
cittecla 86:df8c869a5a52 181 if(fabsf((upper-lower))>0.02f) { // and nothing is detected with the upper Sensor
cittecla 86:df8c869a5a52 182 stop_turn();
cittecla 86:df8c869a5a52 183 search_state = 4; //brick found
cittecla 86:df8c869a5a52 184 }
cittecla 86:df8c869a5a52 185 } else {
cittecla 86:df8c869a5a52 186 search_state = 1; // go to same state
cittecla 86:df8c869a5a52 187 if(turn_for_deg(0) < 0) {
cittecla 86:df8c869a5a52 188 stop_turn();
cittecla 86:df8c869a5a52 189 search_state = 2;
cittecla 86:df8c869a5a52 190 }
cittecla 86:df8c869a5a52 191 }
cittecla 86:df8c869a5a52 192 break;
cittecla 85:d8ea8a99fa3a 193
cittecla 86:df8c869a5a52 194 case 2: // first cycle left
cittecla 86:df8c869a5a52 195 turn_for_deg(-120.0f);
cittecla 86:df8c869a5a52 196 search_state = 3;
cittecla 86:df8c869a5a52 197 break;
cittecla 85:d8ea8a99fa3a 198
cittecla 86:df8c869a5a52 199 case 3: // turn left 120 deg
cittecla 86:df8c869a5a52 200 if((lower<0.75f)&&(lower>0.1f)) { // if something is in the range of 10 to 80cm at the lower Sensor
cittecla 86:df8c869a5a52 201 if(fabsf((upper-lower))>0.02f) { // and nothing is detected with the upper Sensor
cittecla 86:df8c869a5a52 202 stop_turn();
cittecla 86:df8c869a5a52 203 search_state = 4; //brick found
cittecla 86:df8c869a5a52 204 }
cittecla 86:df8c869a5a52 205 } else {
cittecla 86:df8c869a5a52 206 search_state = 3; // go to same state
cittecla 86:df8c869a5a52 207 if(turn_for_deg(0) < 0) {
cittecla 86:df8c869a5a52 208 stop_turn();
cittecla 86:df8c869a5a52 209 search_state = 10; // error
cittecla 86:df8c869a5a52 210 }
cittecla 86:df8c869a5a52 211 }
cittecla 86:df8c869a5a52 212 break;
cittecla 52:56399c2f13cd 213
cittecla 86:df8c869a5a52 214 case 4: // first cycle move
cittecla 86:df8c869a5a52 215 float distance_to_Brick = lower-(float)OFFSET_GREIFER_TO_IRSENSOR; // calculate
cittecla 86:df8c869a5a52 216 move_for_distance(distance_to_Brick);
cittecla 86:df8c869a5a52 217 search_state = 5;
cittecla 86:df8c869a5a52 218 break;
cittecla 86:df8c869a5a52 219
cittecla 86:df8c869a5a52 220 case 5: // move forward
cittecla 86:df8c869a5a52 221 if(move_for_distance(0) < 0) {
cittecla 86:df8c869a5a52 222 stop_move();
cittecla 86:df8c869a5a52 223 search_state = 6;
cittecla 86:df8c869a5a52 224 }
cittecla 86:df8c869a5a52 225 break;
cittecla 86:df8c869a5a52 226
cittecla 86:df8c869a5a52 227 case 6: // Grabbing
cittecla 86:df8c869a5a52 228 return 52; //main state machine set as Grabbing
cittecla 86:df8c869a5a52 229
cittecla 86:df8c869a5a52 230 default:
cittecla 86:df8c869a5a52 231 // error
cittecla 86:df8c869a5a52 232 break;
PESGruppe1 74:d9c387b83196 233 }
cittecla 86:df8c869a5a52 234 return 47; //called until function is done
cittecla 52:56399c2f13cd 235 }
cittecla 52:56399c2f13cd 236
cittecla 39:92723f7ea54f 237
cittecla 39:92723f7ea54f 238
cittecla 39:92723f7ea54f 239
cittecla 39:92723f7ea54f 240
cittecla 39:92723f7ea54f 241
cittecla 39:92723f7ea54f 242
cittecla 39:92723f7ea54f 243