Worksheet 11 - Drag racing Basics

Run Time, Stop sensor and their use in drag racing

The thing that's different about drag racing is that the robot must cross the finish line
and stop before it gets to the buffer, a short distance after.
 It needs to detect the finish line and start braking.

Our robots have  a stop sensor installed that can detect the white finish line.
You should  use  the starter program DRAG1a.py as a starting point. See text version here
The latest file (with logging and Bluetooth 8/7/2022) is Drag1e.py  (txt)
 - it now outputs pre-race and sends "Button" when button pressed
You should use the latest (28/6/22)  UKMARS.py import file

Importantly, the robot needs not to stop on the start line, so it needs to keep track of time,
and only start looking for the stop line about 1 second after starting.

Tasks

You need to know how to keep track of the time after starting,
 - to avoid stopping on the start line when it's starting out on the race

You also need to know how to read the stop sensor,
 - it has a calibrate function (calibs) which gives 0 for black and 100 for white.
 - how to stop the robot when it sees white
 - how to start the race using paper under the stop sensor (optional)

Also the program has two speeds. A starting speed (earlyspeed) that you set in the code,
and a late speed that you can set with the switches. The cross-over time is set in the code too.
You need to know how to change these for the best stable run of the drag race .


 Step 1 Time

Using the drag racing program, check the following :


Note that there is a count of loops, that is cleared at the start (line 130):
loopcount = 0

and is increased each time round the loop:
    loopcount = loopcount +1
  

Note also that the sleep time in the main loop is reduced to 3 milliseconds, in order to
- shorten the interval between checks of the stop line
- and to improve steering performance.
    time.sleep(0.002)        # wait 2 milliseconds 
This gives a loop time of about 3 mS which is 330 loops per second.
There is therefore a count going, that you can use to make things happen at specified times in the race.

Example
To show how you can make things happen at specified times,
try the following anywhere inside the main loop of function race():
    if (loopcount == 900):     # 3 seconds
        led2.on()              # turn on side LED
        speed=0                # stop motors
        FOSpeed = 0            # - even if fallen off

This should make the robot stop after 3 seconds.

When your Robot is going fast enough, 3 seconds should get you down the Drag race track of 7.2Meters

 Step 2 - stop sensor

The program uses the "stopsense" sensor that is imported from UKMARS.py,
You use the following line to read its calibrated value -

    ssense = calibs(stopsense.read_u16()) # stop sensor

The pre-race loop now looks like this, to make the calibration happen in the pre-race loop:
def pre_race():
    # pre race set-up loop to calibrate sensors,
    #  print the workings, and check everything
    while Button.value()==0:    # wait for Start button button to be pressed
        lsense = calibl(lfront.read_u16()) # read left front sensor
        rsense = calibr(rfront.read_u16()) # read right front sensor
        ssense = calibs(stopsense.read_u16()) # read stop sensor
    
        setspeeds(lsense,rsense)  # set up error from readings
    
        print ("L ",lsense," R ",rsense,"Error",error,"Stop",ssense)  # print the sensor values and error
        time.sleep(0.1)     # wait 100 milliseconds

Run this under Thonny, and see the values that are printed :-
 - with the black board under the robot, and
 - with the stop line under the robot
Note that after calibration, white is close to 100 and black to zero.
The threshold you should use for going from black to white is 60,
whereas the threshold in the other direction is 40.

It will help to use the Thonny Plotter to show it as a graph

The numbers should settle down after calibration




Step 2a - speed profile

To get a good speed going in a drag race, its worth starting with a very high speed,
then dropping to a sensible (and stable) speed for the rest of the race.

The starting early speed is set up at line 12 in the code , together with mult and dfactor.
Edit these for a good getaway,

The latespeed to be used after 0.5 seconds (latespeed) is set from from the switches:


swval = readswitches()   # read value of 4-way switch
if swval != 150 :        # ignore if ON,ON,ON,ON
    latespeed = swval*0.6 + 10


And it takes over at line 134 inside function race()

    if (loopcount == 80):      # after 1/2 seconds
        speed=latespeed        # motors at a more stable speed
        mult = .2


Stopping (at line 150)
if (ssense > 50 and loopcount >240):    
                 if(speed>0):
                      logtext()
                      speed=0 # stop motors
                      
We stop the robot, not by switching the motors off, but by setting speed to zero (line 154).
This means that it keeps steering after it has "stopped"
You might need to set the speed to a small negative value (-10) to get it to stop in a shorter distnce.


Step 3 start sensor

You will have noticed in the video of the Drag race project , that the robot is started by sliding paper under the sensor,
and removing it when the race starts.

The flow-chart below describes the "awaitstop()" function (line 50)  that waits for the paper to be inserted,
then flashes the LED while waiting for the paper to be withdrawn, at which point it turns the LED off:

NOTE June 2022 The awaitstop routine should not calibrate the stop sensor, it should only read it,
otherwise if you hold the paper near the sensors, it will need to see an impossibly white stop marker to
stop sucessfully at the end of the race. It should use the new function reads() in the new version of UKMARS.py instead.

There's a call to this function before the main race loop, that is commented out.

  if you leave it commented , you just need to press the button when you start a race.


Operating instructions summary

Switch-on
The program is designed to be switched-off between races.
On switching on it re-starts calibrating the sensors,
 - so it should be switched on only when it is in position in contact with the track.

Bluetooth
The connection will be lost on switch-off. You will need to re-connect each race, if you want to see the log when it stops

Calibration
The sensors need to see both the brightest white and the darkest black before the race starts,
 - so you should slide the robot over the line and start markers, so that the range of brightness can be recorded
The two red LEDs on the line sensor board will show when each sensor is over the line,
  and the side LED does the same for the stop sensor

Race Start (button)
To start the Drag race, make sure that all parts of the robot are behind the starting line, and
    press the pushbutton

Race Start with paper flag
 If the awaitstop() method of starting is enabled , you need to press the button,
  and then put the white paper under the stop sensor, then remove it to start the race