Worksheet 33

Wall following improvements

Purpose

This is to get the wall-follower robot to perform better.
- to run a straighter line in the corridor
- to make more accurate left turns
- to make more accurate 90 and 180 degree right spins

This Task

You should use the program from worksheet 32

And set up the tuning values that work best.


Step1: Use derivative in the corridor steering
variable olderror needs to be set up at the top of corridor:
 
def corridor():     # go down the corridor
    (left,front,right)= ReadWALLS()  # read all 3 walls
    olderror = left - target      # set up olderror before the loop
        
    while True:                   # this version doesn't stop
       #.... rest of loop continues here
This requires using the old error from the last time round the loop,
and adding an extra element of steering using olderror - error:
You will also need another tuning data item:
dfactor
        (left,front,right) = ReadWALLS()
        error = left - target      # subtract target
        # calculate steering due to error
        val = (error  * mult) /1000
		
        # add in steering due to derivative 
        val =  val + (error-olderror)*dfactor * mult /1000
        
        lspeed = speed + val
        rspeed = speed - val
        RightMotor.speed(rspeed)
        LeftMotor.speed(lspeed)
		
        olderror = error           # keep error value for next time

The constant dfactor controls how much derivative you use.
Using derivative will let you use a higher "mult" value, but keep stability.
These will need to be re-adjusted if you change the speed

Step2: Use a sensor to stop the left turn
This requires using a counting loop to stop the turn after a given time
You will also need to check the left sensor to finish the loop when the left wall appears
Modify the function "TurnLeft()", to check the left sensor as well as time.
################
#  TurnLeft()  #
################
def TurnLeft():   # gently turn left
    RightMotor.speed(30)     # Right motor faster
    LeftMotor.speed(10)      # than left motor
    loops = 0                # start count of loops
    while (loops < 60):
        (left,front,right)= ReadWALLS()  # (takes .005 seconds)
        if (left >target): #  if left wall reappears
            return
        loops = loops + 1
        time.sleep(0.005)     # total loop time = .01




Step3: Use a sensor to stop the right spin
This also requires using a counting loop to stop the spin after a given time
You will also need to check the front sensor to finish the loop when the front wall dissappears
Modify the function "Uturn()", to check the front sensor as well as time.
#################
#  SpinRight()  #
#################

# function to spin right 90 or 180 degrees
def SpinRight():   
    lspeed = 30      # adjust to suit
    rspeed = -30     # adjust to suit
    RightMotor.speed(rspeed)
    LeftMotor.speed(lspeed)
    loops = 0                # start count of loops
    while (loops < 45):
        (left,front,right)= ReadWALLS()  # (takes .005 seconds)
        if (front <1500): #  if front wall disappears
            break            # exit loop
        loops = loops + 1
        time.sleep(0.005)    # total loop time = .01
    RightMotor.stop()
    LeftMotor.stop()
    time.sleep(0.1)       # stop the spinning
	


(2/3/2022)