Our code was probably not that sophisticated or well written as others... especially since we didn't include any threading or header files, but oh well! :)
We first defined the struct Point, which included an x coordinate and a y coordinate. Then we defined two global variables, Point position and Point goal. The position point would constantly be updated using the feedback from the VPS overhead, while the goal point would change according to where we wanted Neville to go next.
Neville's primary function for moving around was called GoToPoint
, which had a turn()
function and then a straight()
The turn function took the position and goal points, used arctan to find the angle between the two, and compared it to Neville's current heading according to the gyroscope reading; then Neville would turn either clockwise or counterclockwise until it was within the given error margin of the desired angle heading. Then the straight function would come into play, and Neville would go forward until it was less than a certain distance away from the goal point. Its speed depended on how far away it was from the point; the farther away it was, the greater its speed, and it would gradually slow down until it reached the point.
motor_set_vel(LEFT, LEFT_VEL + get_dist()*GAIN);
motor_set_vel(RIGHT, RIGHT_VEL + get_dist()*GAIN);
We used a series of functions such as set_goal_gearbox(int territory)
, set_goal_lever(int territory)
, and set_goal_center(int territory)
to set Neville's goal point to either the coordinates of the gearbox, the lever, or the center of a territory.
We had a function to claim a territory by spinning the gearbox: Neville would first drive to the gearbox in a territory, then a third motor would turn on and spin some gears and wheels on Neville's back, which would in turn spin the gearbox. Then we had a similar function to pull the lever of that territory using the servo attached to its front and pull it five times to get five ping pong balls.
void claim_territory(int territory)
void mine_resources(int territory)
Both functions had a time out coded in so that if Neville hadn't aligned itself properly with either the gearbox or the lever, it would back up to the center of the territory before trying again. After it tried to realign itself five times, it would give up and move on to the next territory.
After Neville had successfully mined two territories, it would dump its ping pong balls in the center of the board. It was also told to attempt to dump if there were only twenty seconds left during the match, although this was not implemented that well and only worked occasionally.
Since Neville was easily pushed over by its opponents, we wanted it to avoid the opponent robot as much as possible. Using the VPS to read the opponent's coordinates, Neville would check the distance between itself and its opponent, and if the opponent was within a certain distance from Neville, Neville would attempt to go in the other direction.
Neville would also check to see which territory its opponent was in, and if they happened to be in the same territory, Neville would give up that territory and move on to another one.