-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
70 lines (61 loc) · 2.3 KB
/
main.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
#!/usr/bin/env python3
"""A module having the Main entry point for the robot
Usage:
python3 main.py <device path = /dev/input/eventX> [-z|--zero]
"""
import argparse
import time
from signal import pause
from gpiozero import Robot
from gpiozero.pins.pigpio import PiGPIOFactory
from core.events import EventSource
from core.robot_control import RobotControl
from sensors.distance import ProximitySensor
from sensors.shutdown import ShutdownSensor
from utils.utilities import print_event
def event_printer(config):
path = config.device
es = EventSource(path)
while True:
try:
if es.is_connected:
e = next(es.events())
if e:
print_event(e)
else:
print('Not Connected')
time.sleep(2)
except KeyboardInterrupt:
return
except Exception as ex:
print(f"{type(ex)}: {ex}")
def main(config):
try:
pin_factory = PiGPIOFactory() if config.zero else None
proximity = ProximitySensor(safe_distance=10, pin_factory=pin_factory)
shutdown_control = ShutdownSensor(pin_factory=pin_factory)
robot = Robot(left=(10, 9), right=(8, 7), pin_factory=pin_factory)
rc = RobotControl(config.device, fwd_sensor=proximity)
shutdown_control.subscribe(robot.stop)
shutdown_control.subscribe(robot.close)
robot.source = rc()
pause()
except KeyboardInterrupt:
pass
except Exception as ex:
print(f"{type(ex)}: {ex}")
exit(1)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='A remote controlled robot rover')
parser.add_argument('device', help='device is the path to a file like device of the form /dev/input/eventX where '
'X is an integer. See the readme for help finding this value.')
parser.add_argument('-z', '--zero', action='store_true', default=False,
help='The code is running on a piZero use the PiGPIO pin factory for improved performance.')
args = parser.parse_args()
if args.zero:
import pigpio
pi = pigpio.pi()
if not pi.connected:
print('Requested PiGPIO pin factory but PiGPIO daemon not started exiting...')
exit(1)
main(args)