Soccer Robot C - Basic lessons - Exercises - Steady direction
In Robocup Junior the most common strategy is to keep the robot pointing towards opponents half of the playfield. This exercise will help You to achieve that.
Task: resist rotation.
The robot stays in place but has to return to the initial direction if You try to rotate it by hand.
Solution
void RobotLine::loop() {
static int initialDirection;
if (setup())
initialDirection = heading();
int error = heading() - initialDirection;
go(0, 0, error);
}
In the first pass, "initialDirection" stores the initial direction. "error" is calculated as the difference between current heading and initial heading. This number defines rotational speed, which corrects robot's direction. If You want the robot to correct the angle faster, increase correction parameter, for example:
go(0, 0, error * 1.7);
You can vary the factor. However, bigger numbers can lead to instable oscillatory movement. In extreme cases, the robot will rotate continually. To get the best result, study PID controller algorithm.
How to order the robot to go ahead, while maintaining the same heading? It is very easy because rotational speed is independent. Just change the "go" line to this:
go(50, 0, error);