-
Notifications
You must be signed in to change notification settings - Fork 8
/
maqueen.py
76 lines (68 loc) · 1.78 KB
/
maqueen.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
import microbit
import machine
import utime
import neopixel
class Maqueen:
"""
Python class for DFRobot Micro:maqueen platform
https://www.dfrobot.com/product-1783.html
Author: Krzysztof Sawicki <[email protected]>
License: GNU
"""
def __init__(self):
self.rgbleds = neopixel.NeoPixel(microbit.pin15, 4)
print("MAQUEEN initialized")
def set_led(self, lednumber, value):
"""
Enable or disable the front LEDS
0 - left LED (P8)
1 - right LED (P12)
"""
if lednumber == 0:
microbit.pin8.write_digital(value)
elif lednumber == 1:
microbit.pin12.write_digital(value)
def read_distance(self):
"""
Reads distance from HC SR04 sensor
The result is in centimeters
Divider is taken from Makecode library for micro:maqueen
"""
divider = 42
maxtime = 250 * divider
microbit.pin2.read_digital() # just for setting PULL_DOWN on pin2
microbit.pin1.write_digital(0)
utime.sleep_us(2)
microbit.pin1.write_digital(1)
utime.sleep_us(10)
microbit.pin1.write_digital(0)
duration = machine.time_pulse_us(microbit.pin2, 1, maxtime)
distance = duration/divider
return distance
def read_patrol(self, which):
"""
Reads patrol sensor
"""
if which == 0: # left
return microbit.pin13.read_digital()
elif which == 1: # right
return microbit.pin14.read_digital()
def set_motor(self, motor, value):
"""
Controls motor
motor: 0 - left motor, 1 - right motor
value: -255 to +255, the sign means direction
"""
data = bytearray(3)
if motor == 0: # left motor
data[0] = 0
else:
data[0] = 2 # right motor is 2
if value < 0: # ccw direction
data[1] = 1
value = -1*value
data[2] = value
microbit.i2c.write(0x10, data, False) # 0x10 is i2c address of motor driver
def motor_stop_all(self):
self.set_motor(0, 0)
self.set_motor(1, 0)