Drag race strategy:

To win, you must have the smallest time to get to the finish line, after the start signal is given
You will get a 5 second penalty if you don't stop and hit the end of the track.
You need to get up to the top speed as quickly as possible.
You need to keep the robot stable. This may involve reducing the speed.

Plan for Dragracer Code
There are 5 stages, or "States" in a drag race.
0: Calibrating, waiting for button.
1: Waiting for stop sensor paper.
2: Start the run veryfast
3: Run normal speed Wait for finish line
4: Braking after stop line. negative speed.
5: Stationary. awaing switch-off.

The main program can just call these 5 functions in sequence.
They don't need to be functions,
  - they could just be labelled sections of code, but it's maybe a good idea to put them into functions.
The speeds could be parameters (Sp,Kp,Kd) for these functions  2,3,4 and 5.
They could be named:
AwaitButton() -- calibration phase
Awaitstart() -- start of race
BOH() -- follows line fast
normal() -- follows line slower
Brake() -- follows line negative speed to stop earlier
Stop() -- follows line zero speed

Each function above would need to use the following  inputs:
  - Button, line and stop sensors.
Globals:  - smin,smax,lmin,lmax,rmin,rmax. Set during calibration.
 -  Loopcount: number of 0.05 second loops
 -  OldError.  Used for derivative calculation during line following.
 -  Override: (see below) set at the safety limit time to protect motors.
 and lower level functions:
CalibrateSensors() - sets up globals for use by steer().
ReadStop() - reads start/stop sensor. Uses the stop sensor min/max globals
Steer(Sp,Kp,Kd) - reads L and R, calculates error, and sets motor speeds according to Sp,Kp,K
   this is needed in all states apart from Calibration.
Interrupts are not needed unless encoders are used.
A second thread could be used to set the "override" global (1 or 0)that sets motor speeds to zero. ( all speeds simply multiplied by override before use)

Motor /driver safety
  10 ohm / 5 ohm resistor To limit current to 2 amps per motor.
Encoders to detect stalled motors
Limit race time to 2.5 seconds with second thread.
Notes:
Use stationary line following when awaiting paper withdrawal.

© Copyright St Albans Robots