See the latest updated program (LFMk2d.py) with all the updates here  . Download here

Speed-adjusting Line Follower

So far we have adjusted only the following parameters to get our line followers working:
 - Speed 
 - Mult  - (Steering Strength)  how much the error between the sensors affects the speed difference between the motors.
 - Dfactor - (Derivative)  how much the robot response to the rate of change of the error

This version uses the error to control the speed. 
 - If the error is small (it's on the line and not coping with a corner) the speed increases slowly up to a maximum,
 - If the error is large (it's coping with a corner) the speed is set back to the minimum.

There are now 3 parameters to set:
 - minspeed (slower speed on corners)
 - maxspeed (fast speed on the straight) - Currently 90 in version 2d, so it might need to be reduced
 - accelleration ( rate at which it speeds up.) - Currently 2

The old program is called LFMk2a.py   Text view    and requires the UKMARS include file. (It's tuned for 660 RPM motors.) 

The program sets the maxspeed at line 25 , (you might need to reduce this for fast motors) 

 and the minspeed is set by the switches (in steps of 10 - unlike the drag race speeds).
the switch step can be reduced if it's all going too fast. try dividing "swval" by 2 or 3 to get finer control of minspeed:
                            minspeed = swval/3            on line 111

 The acceleration is set at line 27.  Try setting this to 0.5 instead of 2 and you will see it slowly speeding up on the straight.

Of course if you set accelleration =0, then it will travel only at minspeed. 
  This could be useful while tuning to find the best corner speed.

Using these you can set the minspeed to the highest value that goes round corners OK (also adjust mult).
and the maxspeed can be used to speed up on the straight.

Further work  (done in version 2d)

The original program did not  stop on the finish marker, and does not log the time to file "log.txt"
This will require the stop sensor to be calibrated in the pre-race loop, and read in the main loop. 

The time delay (loopcount value) is no longer used to enable the stop to be acted on.
Instead (version 2d) it counts  the stop markers (and crossovers ) to be identif the finish line.
The operator needs to manually count the markers and program that in (as "StopAt" ) before the race.

It needs a copy of the "logtext()" function.
see below:


1) Add Log file function.
# record that file not yet written
written=0

############
#  logtxt  #
############
# record stuff to log file
def logtext():
    pfile = open("log.txt","a")
    pfile.write("\r\n\r\nspeed="+str(speed))
    pfile.write("\r\nmult="+str(mult))
    pfile.write("\r\ndfactor="+str(dfactor))
    pfile.write("\r\nloops="+str(loopcount)+" "+str(loopcount/160))
    pfile.close()
    print("log.txt written")

2) Calibtrate the Stop sensor in the pre-race loop
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

3a)Use the CountStop() function in the main loop to keep a count (markerCount) of stop markers passed

- the variable StopAt needs to be set at the start of the code:
StopAt = 3                 # marker count to stop at

    CountStop()                 # check stop marker

3b) Check the number of stop markers passed and write logfile in the main loop
    # stop on stop marker if StopAt markers reached
    if (markerCount >= StopAt):
        speed = 0             # stop motors
        if(written == 0):
            logtext()
            written=1
        

4) It's probably a good idea to brake the motors momentarily when a curve is first detected:
Make the main race loop adjust the speed as follows:
    if (error >35 or error <-35):
        if (maxspeed - speed < 20):
            LeftMotor.speed(-20)  # set left motor speed
            RightMotor.speed(-20) # set right motor speed
            #time.sleep(0.01)     # wait extra 10 milliseconds

        speed = minspeed
    else:
        speed = min(speed+accell,maxspeed)

Of course if the speed has been set to zero, then the speed-adjustment above should not happen
    if (speed != 0):