10
0

add line direction memory

This commit is contained in:
Silver Kuusik 2018-11-13 21:36:35 +01:00
parent 283deea22c
commit 0f86d3c64d

11
hal.py
View File

@ -50,6 +50,7 @@ class Sumorobot(object):
Pin(23, Pin.IN, Pin.PULL_UP) Pin(23, Pin.IN, Pin.PULL_UP)
# The phototransistors # The phototransistors
self.last_line = LEFT
self.adc_line_left = ADC(Pin(34)) self.adc_line_left = ADC(Pin(34))
self.adc_line_right = ADC(Pin(33)) self.adc_line_right = ADC(Pin(33))
@ -148,7 +149,7 @@ class Sumorobot(object):
self.config["right_line_threshold"] = self.adc_line_right.read() self.config["right_line_threshold"] = self.adc_line_right.read()
# Update the config file # Update the config file
with open("config.part", "w") as config_file: with open("config.part", "w") as config_file:
config_file.write(ujson.dumps(config)) config_file.write(ujson.dumps(self.config))
os.rename("config.part", "config.json") os.rename("config.part", "config.json")
# Function to get light inensity from the phototransistors # Function to get light inensity from the phototransistors
@ -170,10 +171,12 @@ class Sumorobot(object):
if dir == LEFT: if dir == LEFT:
line = abs(self.get_line(LEFT) - self.config["left_line_threshold"]) > 1000 line = abs(self.get_line(LEFT) - self.config["left_line_threshold"]) > 1000
self.set_led(LEFT_LINE, line) self.set_led(LEFT_LINE, line)
last_line = LEFT
return line return line
elif dir == RIGHT: elif dir == RIGHT:
line = abs(self.get_line(RIGHT) - self.config["right_line_threshold"]) > 1000 line = abs(self.get_line(RIGHT) - self.config["right_line_threshold"]) > 1000
self.set_led(RIGHT_LINE, line) self.set_led(RIGHT_LINE, line)
last_line = RIGHT
return line return line
def set_servo(self, dir, speed): def set_servo(self, dir, speed):
@ -227,10 +230,14 @@ class Sumorobot(object):
# Go forward # Go forward
self.set_servo(LEFT, 100) self.set_servo(LEFT, 100)
self.set_servo(RIGHT, -100) self.set_servo(RIGHT, -100)
else: elif last_line == RIGHT:
# Go left # Go left
self.set_servo(LEFT, -100) self.set_servo(LEFT, -100)
self.set_servo(RIGHT, -100) self.set_servo(RIGHT, -100)
else:
# Go right
self.set_servo(LEFT, 100)
self.set_servo(RIGHT, 100)
# Increase search counter # Increase search counter
self.search_counter += 1 self.search_counter += 1
elif dir == FORWARD: elif dir == FORWARD: