/
burgerbot.py
158 lines (123 loc) · 4.26 KB
/
burgerbot.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# BurgerBot
# Kevin McAleer
# June 2022
from motor import Motor, pico_motor_shim
from pimoroni import REVERSED_DIR
from servo import Servo
import gc
from machine import Pin
from time import sleep, sleep_us, ticks_us
gc.threshold(50000) # setup garbage collection
class RangeFinder():
def __init__(self,trigger_pin:int = 0, echo_pin:int = 1):
self.trigger = Pin(trigger_pin, Pin.OUT)
self.echo = Pin(echo_pin, Pin.IN)
@property
def distance(self):
""" Returns the distance in cm """
# set the signal on & off times to zero
signalon = 0
signaloff = 0
# reset the trigger
self.trigger.low()
sleep_us(2)
self.trigger.high()
sleep_us(5)
self.trigger.low()
while self.echo.value() == 0:
signaloff = ticks_us()
while self.echo.value() == 1:
signalon = ticks_us()
elapsed_microseconds = signalon - signaloff
self.duration = elapsed_microseconds
self.distance_to_object = (elapsed_microseconds * 0.343) / 2
return round(self.distance_to_object / 10 ,1)
# MOTOR_1 = machine.Pin(6, machine.PIN_OUT)
# MOTOR_1_SPEED = machine.Pin(7, ,machine.PIN_OUT)
# MOTOR_2 = machine.Pin(27, machine.PIN_OUT)
# MOTOR_2_SPEED = machine.Pin(26, ,machine.PIN_OUT)
# @server.route("/penup", methods=["POST"])
# def penup():
# global command
# command = "penup"
# return render_template("index.html", command=command)
class Burgerbot:
# Create a list of motors
MOTOR_PINS = [pico_motor_shim.MOTOR_1, pico_motor_shim.MOTOR_2]
motors = [Motor(pins) for pins in MOTOR_PINS]
__speed = 0
pen_servo = Servo(16)
echo_pin = 0
trigger_pin = 1
line_sensor = Pin(17, Pin.IN)
range_finder = RangeFinder(echo_pin=echo_pin, trigger_pin=trigger_pin)
def __init__(self):
""" Initialize the Burgerbot """
# Use the settings below to configure the motors so they turn in the same direction
# self.motors[0].direction(REVERSED_DIR)
self.motors[1].direction(REVERSED_DIR)
# self.pen_servo.enable()
# self.pen_servo.to_mid()
for motor in self.motors:
motor.enable()
self.speed = 0.5
@property
def distance(self):
return self.range_finder.distance
@property
def line_detected(self)->bool:
if self.line_sensor.value() == 1:
return True
else:
return False
def pen_middle(self):
self.pen_servo.to_mid()
def pen_down(self):
self.pen_servo.value(30)
def pen_up(self):
self.pen_servo.value(-20)
def forward(self, duration=0.5):
""" Drive the motors forward """
for m in self.motors:
m.speed(self.speed)
sleep(duration)
def backward(self, duration=0.5):
""" Drive the motors backward """
for m in self.motors:
m.speed(-self.speed)
sleep(duration)
def turnleft(self, duration=0.5):
""" Turn the motors left """
# set the speed of the motors
self.motors[0].speed(self.speed)
self.motors[1].speed(-self.speed)
sleep(duration)
def turnright(self, duration=0.5):
""" Turn the motors right """
# set the speed of the motors
self.motors[0].speed(-self.speed)
self.motors[1].speed(self.speed)
sleep(duration)
def stop(self):
""" Stop the motors """
# Disable the motors
for m in self.motors:
m.disable()
def left_motor(self, speed):
self.motors[0].speed(speed)
def right_motor(self, speed):
self.motors[1].speed(speed)
@property
def speed(self):
""" Get the speed of the motors """
return self.__speed
@speed.setter
def speed(self, value):
""" Set the speed of the motors """
# Checl the speed value is within the range we expect (-1 to 1)
if -1 <= value <= 1:
self.__speed = value
# for m in self.motors:
# m.speed(self.__speed)
else:
print(f"Speed value should be between -1 and +1, however {value} was provided")