Controlling speed and distance.

The nose wheel optical reader produces 34 pulses per wheel revolution and this is doubled by sensing both leading and trailing edges.  A nose wheel diameter of approximately one inch produces 158 pulses per square (180 mm).  The counter lo-byte is returned to zero before each movement starts and the counter records the distance travelled (straight, turn or reverse).

While investigating the maze, the speed is kept low.  This is necessary to ensure that the mouse can be stopped in time when it encounters the end wall of a blocked square (worst case).  When the maze map is detailed enough to forecast the shortest route, this eventuality cannot occur (except at the start square or target squares) and the speed is increased.  Software determines speed dependent on the number of consecutive straights preceding a direction change (or the winning post!).  Braking is achieved by presenting a virtual short circuit across the motor windings (having first removed the supply).  Reversing the supply to the motors is also used.  To control speed, the increase in nose wheel count is measured over the 15 mS period and this figure is compared with the required speed variable.  Dependent on whether the comparison is above or below the required speed, the motor drive on/off duty cycle is reduced or allowed to continue during the next 15mS period.