-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathline_following_behavior.py
39 lines (30 loc) · 1.23 KB
/
line_following_behavior.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
from robot import Robot
from time import sleep
class LineFollowingBehavior:
# Note - this is the robot ON the line.
def __init__(self, the_robot, forward_speed=30, cornering=-30):
self.robot = the_robot
self.forward_speed = forward_speed
self.cornering = cornering
def when_left_crosses_line(self):
self.robot.set_left(self.cornering)
def when_right_crosses_line(self):
self.robot.set_right(self.cornering)
def when_left_off_line(self):
self.robot.set_left(self.forward_speed)
def when_right_off_line(self):
self.robot.set_right(self.forward_speed)
def run(self):
# Setup conditions
self.robot.left_line_sensor.when_line = self.when_left_crosses_line
self.robot.left_line_sensor.when_no_line = self.when_left_off_line
self.robot.right_line_sensor.when_line = self.when_right_crosses_line
self.robot.right_line_sensor.when_no_line = self.when_right_off_line
# Start driving
self.robot.set_left(self.forward_speed)
self.robot.set_right(self.forward_speed)
while True:
sleep(0.02)
bot = Robot()
behavior = LineFollowingBehavior(bot)
behavior.run()