void RobotSoccer::lineAvoid(){
static TriState lineLeft;
static TriState lineFront;
static float escapeDirection;
const uint16_t WALL_DISTANCE = 550;
if (setup()){
print("Line avoid enter\n\r");
lineLeft = TriState::Unknown;
lineFront = TriState::Unknown;
}
// Line front?
if (mrm_ref_can->any(false, 0) && lineFront == TriState::Unknown && front() < WALL_DISTANCE)
lineFront = TriState::Yes, print("Front");
// Line right?
if (mrm_ref_can->any(false, 1) && lineLeft == TriState::Unknown && right() < WALL_DISTANCE)
lineLeft = TriState::Opposite, print("Right");
// Line back?
if (mrm_ref_can->any(false, 2) && lineFront == TriState::Unknown && back() < WALL_DISTANCE)
lineFront = TriState::Opposite, print("Back");
// Line left?
if (mrm_ref_can->any(false, 3) && lineLeft == TriState::Unknown && left() < WALL_DISTANCE)
lineLeft = TriState::Yes, print("Left");
if (lineLeft != TriState::Unknown || lineFront != TriState::Unknown){ // A line, not a false alarm
int8_t x = 0;
int8_t y = 0;
if (lineFront == TriState::Yes)
y--;
else if (lineFront == TriState::Opposite)
y++;
if (lineLeft == TriState::Yes)
x++;
else if (lineLeft == TriState::Opposite)
x--;
escapeDirection = atan2(x, y) / PI * 180;
print("Line avoid, F:%i R:%i B:%i L:%i Esc dir: %i L:%i F:%i\n\r", mrm_ref_can->any(false, 0), mrm_ref_can->any(false, 1),
mrm_ref_can->any(false, 2), mrm_ref_can->any(false, 3), (int)escapeDirection, lineLeft, lineFront);
go(70, escapeDirection, pidRotation->calculate(heading() - headingToMaintain), 100);
}
else
actionSet(actionIdle), print("False line");
}
Ispišemo na ekran što se dogodilo ("print()"), pokrenemo motore ("go()".
Ako je detektirana linija senzorom površine, ali je udaljenost bila prevelika, to ispišemo.