Alternative setspeeds() function
Worksheet 12
This page shows how you can make the UKMARS robot read a wider range of positions,
as being over the line rather than fallen off.
This might make drag racers more stable, as the derivative cannot work when fallen off
Below is the graph you get if you slide the drag-racing robot across the line (from left to right)
after switch-on, but before pressing the button. pre-race() function. After calibration.
This is the current (old) version.
=======================================================================
def setspeeds(ls,rs): # ls and rs are the sensor readings
global lspeed,rspeed # Python to let this function change speeds
global error,OldError,mult # to allow changing error
if (ls >FOlevel and rs >FOlevel): # detect not fallen off
led.off()
error = ls-rs # only if on-line,
myspeed = speed
else:
led.on() # Fall-off detected
# Off-line, so boost error
if error >0:
error = FOError
else:
error = -FOError
myspeed = speed * 0.8 # run at fall-off speed
val = error *mult # adjust steering amount
# Derivative calculations, using last-time's value
change = (error - OldError)
# add in the derivative to val
val = val + (change * dfactor)/10
lspeed = myspeed - val # speed of left motor
rspeed = myspeed + val # speed of right motor
OldError = error # keep track of this error for later
return (error)
========================================================================
Here is the graph for the new version (setspeeds2())
You can see that a wider range of positions are used to generate the error. It's a bit wiggly but has an increasing slope
for about 17mm as opposed to the 11mm for the old version.
Here is the code
Const = 2 * FOError # about 180
def setspeeds2(ls,rs): # ls and rs are the sensor readings
global lspeed,rspeed # Python to let this function change speeds
global error,OldError,mult # to allow changing error
if (ls >FOlevel): # left >15
if (rs >FOlevel): # right >15
error = ls-rs # central region
else:
error = Const - ls # right of central region
led.off()
myspeed = speed
else: #(ls <= FOlevel)
if (rs >FOlevel): # left of central region
error = rs - Const
led.off()
myspeed = speed
else: # fallen off
if error >0:
error = Const - FOlevel # to the right
else:
error = FOlevel - Const # to the left
led.on()
myspeed = speed * 0.8 # run at fall-off speed
error = error/2 # to give same error when fallen off
val = error *mult # adjust steering amount
# Derivative calculations, using last-time's value
change = (error - OldError)
# add in the derivative to val
val = val + (change * dfactor)/10
lspeed = myspeed - val # speed of left motor
rspeed = myspeed + val # speed of right motor
OldError = error # keep track of this error for later
return (error)