new linefollower example
This commit is contained in:
parent
3ef0fff256
commit
1c0310b9c7
1 changed files with 58 additions and 66 deletions
|
@ -8,82 +8,74 @@ Simple Linefollower
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
|
from compLib.Motor import Motor
|
||||||
|
from compLib.Display import Display
|
||||||
|
from compLib.IRSensor import IRSensor
|
||||||
|
from compLib.Encoder import Encoder
|
||||||
|
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from compLib.IRWrapper import IRWrapper
|
IRSensor.set(1, True)
|
||||||
from compLib.Motor import Motor
|
IRSensor.set(2, True)
|
||||||
|
IRSensor.set(3, True)
|
||||||
|
IRSensor.set(4, True)
|
||||||
|
IRSensor.set(5, True)
|
||||||
|
|
||||||
STRAIGHT_SPEED = 40.0
|
DRIVE_SPEED = 85
|
||||||
TURN_SPEED = 40.0
|
TURN_SPEED = 50
|
||||||
|
COLOR_BREAK = 750
|
||||||
|
KP = 50.0
|
||||||
|
KD = 0.0
|
||||||
|
|
||||||
COLOR_BREAK = 500
|
def drive(leftSpeed, rightSpeed):
|
||||||
|
leftSpeed *= -1
|
||||||
|
rightSpeed *= 0.906
|
||||||
|
|
||||||
|
Motor.power(1, min(max(-100, leftSpeed), 100))
|
||||||
|
Motor.power(2, min(max(-100, rightSpeed), 100))
|
||||||
|
|
||||||
def drive(left, right, active_break=False, back_mult=0.0):
|
def follow(sleepTime = 0.1):
|
||||||
left = max(min(left, 100), -100)
|
lastError = 0
|
||||||
right = max(min(right, 100), -100)
|
sensorsBlack = 0
|
||||||
|
while sensorsBlack < 3:
|
||||||
|
sensorsBlack = 0
|
||||||
|
for i in range(1, 6):
|
||||||
|
if IRSensor.read(i) > COLOR_BREAK:
|
||||||
|
sensorsBlack += 1
|
||||||
|
|
||||||
print("left {} right {}".format(left, right))
|
error = lastError
|
||||||
|
if IRSensor.read(3) > COLOR_BREAK:
|
||||||
|
error = 0
|
||||||
|
elif IRSensor.read(1) > COLOR_BREAK:
|
||||||
|
error = -1.5
|
||||||
|
elif IRSensor.read(5) > COLOR_BREAK:
|
||||||
|
error = 1.5
|
||||||
|
elif IRSensor.read(2) > COLOR_BREAK:
|
||||||
|
error = -1
|
||||||
|
elif IRSensor.read(4) > COLOR_BREAK:
|
||||||
|
error = 1
|
||||||
|
elif error == 1.5:
|
||||||
|
error = 3
|
||||||
|
elif error == -1.5:
|
||||||
|
error = -3
|
||||||
|
|
||||||
Motor.power(0, left)
|
lastError = error
|
||||||
Motor.power(1, left * back_mult)
|
|
||||||
Motor.power(2, right * back_mult)
|
|
||||||
Motor.power(3, right)
|
|
||||||
|
|
||||||
if active_break:
|
adjustment = KP * error + KD * (error - lastError)
|
||||||
time.sleep(0.001)
|
leftSpeed = DRIVE_SPEED + adjustment
|
||||||
|
rightSpeed = DRIVE_SPEED - adjustment
|
||||||
|
|
||||||
Motor.power(0, 10)
|
print(f"{leftSpeed} {rightSpeed} {adjustment} {error}")
|
||||||
Motor.power(1, 10)
|
drive(leftSpeed, rightSpeed)
|
||||||
Motor.power(2, 10)
|
|
||||||
Motor.power(3, 10)
|
|
||||||
|
|
||||||
time.sleep(0.001)
|
|
||||||
|
|
||||||
|
|
||||||
def follow_till_line():
|
|
||||||
while True:
|
|
||||||
print("left: {:.2f} middle: {:.2f} right: {:.2f}".format(IRWrapper.bottom_left_calibrated(),
|
|
||||||
IRWrapper.bottom_middle_calibrated(),
|
|
||||||
IRWrapper.bottom_right_calibrated()))
|
|
||||||
|
|
||||||
left = IRWrapper.bottom_left_calibrated()
|
|
||||||
right = IRWrapper.bottom_right_calibrated()
|
|
||||||
|
|
||||||
if left > COLOR_BREAK:
|
|
||||||
drive(-TURN_SPEED, TURN_SPEED, back_mult=1.25)
|
|
||||||
while IRWrapper.bottom_middle_calibrated() < COLOR_BREAK and \
|
|
||||||
IRWrapper.bottom_right_calibrated() < COLOR_BREAK:
|
|
||||||
pass
|
|
||||||
drive(0, 0)
|
|
||||||
elif right > COLOR_BREAK:
|
|
||||||
drive(TURN_SPEED, -TURN_SPEED, back_mult=1.25)
|
|
||||||
while IRWrapper.bottom_middle_calibrated() < COLOR_BREAK and \
|
|
||||||
IRWrapper.bottom_left_calibrated() < COLOR_BREAK:
|
|
||||||
pass
|
|
||||||
drive(0, 0)
|
|
||||||
else:
|
|
||||||
drive(STRAIGHT_SPEED, STRAIGHT_SPEED, True)
|
|
||||||
|
|
||||||
if IRWrapper.bottom_left_calibrated() > 750 and IRWrapper.bottom_right_calibrated() > 750:
|
|
||||||
break
|
|
||||||
|
|
||||||
print("left: {} middle: {} right: {}".format(IRWrapper.bottom_left_calibrated(),
|
|
||||||
IRWrapper.bottom_middle_calibrated(),
|
|
||||||
IRWrapper.bottom_right_calibrated()))
|
|
||||||
|
|
||||||
drive(0, 0)
|
drive(0, 0)
|
||||||
|
time.sleep(sleepTime)
|
||||||
|
|
||||||
time.sleep(0.2)
|
def main():
|
||||||
|
follow()
|
||||||
|
follow()
|
||||||
|
follow()
|
||||||
|
follow()
|
||||||
|
follow(0.2)
|
||||||
|
|
||||||
|
main()
|
||||||
if __name__ == "__main__":
|
|
||||||
# Put bot with middle sensor onto black line
|
|
||||||
IRWrapper.calibrate()
|
|
||||||
|
|
||||||
# Initialize Motors, so the bot doesn't speed off instantly
|
|
||||||
drive(1, 1)
|
|
||||||
time.sleep(1)
|
|
||||||
|
|
||||||
# Follow line, till the left and right sensor are on black
|
|
||||||
follow_till_line()
|
|
Reference in a new issue