-
Notifications
You must be signed in to change notification settings - Fork 1
/
challenge_3.py
71 lines (56 loc) · 1.63 KB
/
challenge_3.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
from machine import SoftI2C, Pin, PWM
from time import sleep
from drv8833 import DRV8833 # motor control
from vl53l4cd import VL53L4CD # distance Sensor
motor_ctl = None
distance_sensor = None
distance_led = Pin(16) # Change this to whatever PIN you put it on
def flash_led():
print("Flashing Lights! - Challenge 1")
led = Pin("LED")
led.on()
sleep(0.5)
led.off()
def create_motor_ctl():
print("Confgiure Motor")
frequency = 40000
ain1 = PWM(Pin(15, Pin.OUT))
ain2 = PWM(Pin(14, Pin.OUT))
bin1 = PWM(Pin(13, Pin.OUT))
bin2 = PWM(Pin(12, Pin.OUT))
ain1.freq(frequency)
ain2.freq(frequency)
bin1.freq(frequency)
bin2.freq(frequency)
return DRV8833(ain1, ain2, bin1, bin2)
def create_distance_sensor():
print("Confgiure Sensor")
distance_sensor_i2c = SoftI2C(sda=Pin(0), scl=Pin(1))
return VL53L4CD(distance_sensor_i2c)
# run some code!
flash_led()
motor_ctl = create_motor_ctl()
distance_sensor = create_distance_sensor()
throttle_a_val = 1
throttle_b_val = 1
distance_sensor.start_ranging()
last_distance = distance_sensor.get_distance()
while True:
dist = distance_sensor.get_distance()
print(f"Distance: {dist} cm")
if last_distance <= ___:
___
else:
___
last_distance = ___
sleep(0.1)
# keep everything from challenge 2
# if distance_sensor.distance < _____:
# throttle_a_val = _____
# throttle_b_val = _____
# elif distance_sensor.distance < _____:
# throttle_a_val = _____
# throttle_b_val = _____
# ...
motor_ctl.throttle_a(throttle_a_val)
motor_ctl.throttle_b(throttle_b_val)