Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Fork of Robocode by
source/Movement.cpp
- Committer:
- cittecla
- Date:
- 2017-05-01
- Revision:
- 87:df8c869a5a52
- Parent:
- 86:d8ea8a99fa3a
- Parent:
- 84:5d3bca1ece20
- Child:
- 88:b89cace9329b
File content as of revision 87:df8c869a5a52:
/**
* Movement function library
* Handels Movement of the Robot
**/
#include "Movement.h"
#define OFFSET_GREIFER_TO_IRSENSOR 0.15 // Constant for distance between front IR Sensor and the postion where the Greifer is in grabbing Position
bool is_moving = false;
float wanted_dist = 0;
bool is_turning = false;
bool direction = false;
float wanted_deg = 0;
Timer t;
float previous_t = 0;
int search_state = 0;
float restdeg = 0;
int moving()
{
return 0;
}
void stop_move()
{
set_speed(0,0);
wanted_dist = 0;
is_moving = false;
}
void stop_turn()
{
set_speed(0,0);
wanted_deg = 0;
is_turning = false;
}
float move_for_distance(float distance)
{
if(distance != 0) {
is_moving = true;
float left = 0;
float right = 0;
wanted_dist = sqrt(distance*distance);
if(distance > 0) { //move forward
direction = 1;
left = 50.0f;
right = 50.0f;
} else { //move backward
direction = 0;
left = -50.0f;
right = -50.0f;
}
set_speed(left, right);
t.reset();
t.start();
} else {
float speed_left = get_speed_left();
wanted_dist -= (2*(float)wheel_r*(float)M_PI) * t.read() * fabsf(speed_left);
t.reset();
if(wanted_dist <= 0) { //distance covered, Stop function
set_speed(0,0);
is_moving = false;
t.stop();
}
}
return wanted_dist;
}
int move_backward_for_distance()
{
return 0;
}
float turn_for_deg(float deg) //if deg not 0 equals initilisation.
{
if(deg != 0) {
is_turning = true;
float left = 0;
float right = 0;
wanted_deg = sqrt(deg*deg);
if(deg < 0) { // turn left
direction = 1;
left = -50.0f;
right = 50.0f;
} else { // turn right
direction = 0;
left = 50.0f;
right = -50.0f;
}
set_speed(left, right);
t.reset();
t.start();
} else {
float speed_left = get_speed_left();
wanted_deg -= 360.0f / (2*(float)wheel_r*(float)M_PI) * t.read() * fabsf(speed_left);
t.reset();
if(wanted_deg <= 0) {
set_speed(0,0);
is_turning = false;
t.stop();
}
}
return (wanted_deg);
}
int move_to_next_coord()
{
float current_heading = get_current_heading();
position current_pos = get_current_pos();
position next_pos = get_next_pos();
float needed_heading = 0;
float distance = 0;
// nord(-y) = 0 grad
if(current_pos.y > next_pos.y) {
if(current_pos.x > next_pos.x) needed_heading = 315;
distance = sqrt2;
if(current_pos.x == next_pos.x) needed_heading = 0;
distance = 1;
if(current_pos.x < next_pos.x) needed_heading = 45;
distance = sqrt2;
}
if(current_pos.y == next_pos.y) {
if(current_pos.x > next_pos.x) needed_heading = 270;
distance = 1;
if(current_pos.x == next_pos.x) //error same position;
if(current_pos.x < next_pos.x) needed_heading = 90;
distance = 1;
}
if(current_pos.y < next_pos.y) {
if(current_pos.x > next_pos.x) needed_heading = 225;
distance = sqrt2;
if(current_pos.x == next_pos.x) needed_heading = 180;
distance = 1;
if(current_pos.x < next_pos.x) needed_heading = 135;
distance = sqrt2;
}
if(needed_heading != current_heading) {
turn_for_deg((needed_heading-current_heading));
} else {
move_for_distance(distance);
}
return 0;
}
// Tobias Berger
int move_in_search_for_brick()
{
float upper = getDistanceIR(2); // get distance from upper Center Sensor
float lower = getDistanceIR(3); // get distance from Lower Center Sensor
switch (search_state) {
case 0: //first cycle right
turn_for_deg(60.0f); // call function and start turning
search_state = 1;
break;
case 1: // turn right 60 deg
if((lower<0.75f)&&(lower>0.1f)) { // if something is in the range of 10 to 80cm at the lower Sensor
if(fabsf((upper-lower))>0.02f) { // and nothing is detected with the upper Sensor
stop_turn();
search_state = 4; //brick found
}
} else {
search_state = 1; // go to same state
if(turn_for_deg(0) < 0) {
stop_turn();
search_state = 2;
}
}
break;
case 2: // first cycle left
turn_for_deg(-120.0f);
search_state = 3;
break;
case 3: // turn left 120 deg
if((lower<0.75f)&&(lower>0.1f)) { // if something is in the range of 10 to 80cm at the lower Sensor
if(fabsf((upper-lower))>0.02f) { // and nothing is detected with the upper Sensor
stop_turn();
search_state = 4; //brick found
}
} else {
search_state = 3; // go to same state
if(turn_for_deg(0) < 0) {
stop_turn();
search_state = 10; // error
}
}
break;
case 4: // first cycle move
float distance_to_Brick = lower-(float)OFFSET_GREIFER_TO_IRSENSOR; // calculate
move_for_distance(distance_to_Brick);
search_state = 5;
break;
case 5: // move forward
if(move_for_distance(0) < 0) {
stop_move();
search_state = 6;
}
break;
case 6: // Grabbing
return 52; //main state machine set as Grabbing
default:
// error
break;
}
return 47; //called until function is done
}
