-
Notifications
You must be signed in to change notification settings - Fork 0
/
spike_CarControl.py
68 lines (53 loc) · 1.95 KB
/
spike_CarControl.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
##############################################################################
# Imports #
##############################################################################
import hub,utime
##############################################################################
# Initialize Ports #
##############################################################################
# Raspberry Pi4
serial = hub.port.C
serial.mode(hub.port.MODE_FULL_DUPLEX)
serial.baud(115200)
# Motors
left = hub.port.B.motor
right = hub.port.A.motor
# Initial Velocities
left_vel = -40
right_vel = 40
turn_rate = 20
while True :
try :
# Read communcition from Raspberry Pi and Decode
line = serial.read(1000)
line = str(line.decode("UTF-8"))
if (line.startswith('d')) :
# We are reading from the GUI Wheel
try:
# Increase speed of one motor to turn car
turn_rate = int(line[1:])
new_right_vel = right_vel - (turn_rate//5)
new_left_vel = left_vel - (turn_rate //5)
left.pwm(new_left_vel)
right.pwm(new_right_vel)
utime.sleep(1)
except:
pass
elif (line.startswith('p')) :
# We are reading from the power bar
try :
# Update motor velocities
new_speed = int(line[1:])
left_vel = -new_speed
right_vel = new_speed
left.pwm(left_vel)
right.pwm(right_vel)
except:
pass
# Error Reading Number
except:
pass
utime.sleep_ms(100)
# Stop Car
left.pwm(0)
right.pwm(0)