Soccer Robot C - Basic lessons - Exercises - Playfield center
An exercise useful for goalkeeper in Robocup Junior Soccer.
Task: go to center of the playfield.
Move the robot just left - right and position it in the center between 2 walls 182 cm apart. However, do so only if at least one side has a free space of 81 cm (half of 182 minus robot's diameter of 10 cm) and assume that it is a wall in that case. Otherwise assume there are other obstacles and do not move. Use code for maintaining a steady direction:
void RobotSoccer::loop() {
static int initialDirection;
if (setup())
initialDirection = heading();
int error = heading() - initialDirection;
go(0, 0, error);
}
Solution
void RobotSoccer::loop() {
static int initialDirection;
if (setup())
initialDirection = heading();
int rotationalError = heading() - initialDirection;
int positionError = 0;
if (right() > 81)
positionError = 81 - right();
else if (left() > 81)
positionError = left() - 81;
go(abs(positionError), positionError < 0 ? 90 : -90, rotationalError);
}
- "rotationalError" is the same as before.
- "positionError" will be negative if the robot is left of the center and positive if right.
- First argument of "go()" is "abs(positionError)", absolute value of the error, and defines speed. That is natural as we will need a bigger speed if the error is bigger.
- Second argument is direction in which the robot will move. If error is negative, the robot is too much left, and need to go right (90°). Otherwise left (-90°).
- If there is no free space on either side, positionError will remain 0, speed will be 0, and the robot will not move.