-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.py
56 lines (50 loc) · 1.79 KB
/
controller.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
from motors import Motors
import RPi.GPIO as GPIO
import time
# class MotorState:
# OFF = 0
# FORWARD = 1
# BACKWARD = 2
class BaseController:
def __init__(self):
self._interface = Motors()
# GPIO setup
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(8, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(10, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(12, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
def step(self, sleep=True):
# TODO: switch debouncing
p1, p2 = GPIO.input(7), GPIO.input(8)
p3, p4 = GPIO.input(10), GPIO.input(12)
if p1 == GPIO.HIGH:
print('Going forwards')
self._interface.move_motor(0, 100)
self._interface.move_motor(1, 100)
self._interface.move_motor(2, -100)
self._interface.move_motor(3, -100)
elif p2 == GPIO.HIGH:
print('Going backwards')
self._interface.move_motor(0, -100)
self._interface.move_motor(1, -100)
self._interface.move_motor(2, 100)
self._interface.move_motor(3, 100)
elif p3 == GPIO.HIGH:
print('Going left')
self._interface.move_motor(0, -100)
self._interface.move_motor(1, 100)
self._interface.move_motor(2, -100)
self._interface.move_motor(3, 100)
elif p4 == GPIO.HIGH:
print('Going right')
self._interface.move_motor(0, 100)
self._interface.move_motor(1, -100)
self._interface.move_motor(2, 100)
self._interface.move_motor(3, -100)
else:
self._interface.stop_motors()
time.sleep(0.1)
controller = BaseController()
while True:
controller.step()