-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
110 lines (84 loc) · 3.09 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
"""
Purpose: Main Module for Robot Control System
Author: Jianjun Lang
Date: November 14, 2024
Version: v3.6
Additional Description:
Updates:
Version 3.6
Transitioned from using subprocess to multiprocessing.
Eliminated the use of package variables; parameters are now passed using function arguments.
"""
import multiprocessing
import random
import os
import signal
import sys
# import run functions
from .http_server.http_server import run_http_server
from .drivers.picamera_driver import run_picamera_driver
from .websockets_server import run_websockets_server
from .drivers.gpio_driver import run_gpio_driver
def main():
'''parameters'''
mainPath = os.path.dirname(os.path.abspath(__file__))
httpPath = mainPath + "/http_server"
# server ports
httpPort = 80
websocketPort = random.randint(49152, 65535)
# driver ports
cameraPort = random.randint(49152, 65535)
leftMotor_port = random.randint(49152, 65535)
rightMotor_port = random.randint(49152, 65535)
suctionMotor_port = random.randint(49152, 65535)
indicatorLED_port = random.randint(49152, 65535)
driverPort_tuple = (leftMotor_port, rightMotor_port, suctionMotor_port, indicatorLED_port)
# GPIO pins
GPIOpin_pwm_left = 23
GPIOpin_pwm_right = 18
GPIOpin_pwm_suction = 12
GPIOpin_LED_indicator = 5
GPIOpin_direction_right = 17
GPIOpin_direction_left = 22
pin_tuple = (GPIOpin_pwm_left, GPIOpin_pwm_right, GPIOpin_pwm_suction, GPIOpin_LED_indicator, GPIOpin_direction_right, GPIOpin_direction_left)
# PWM Frequency (Hz)
PWMfreq_motion = 10000
PWMfreq_suction = 18000
pwmFreq_tuple = (PWMfreq_motion, PWMfreq_suction)
# log print
http_logPrint = 1
websocket_logPrint = 1
gpioDriver_logPrint = 1
'''start and manage the child processes'''
# processes
processes = []
# SIGINT handler
def signalHandler(sig, frame):
# terminate child processes
for process in processes:
process.terminate()
# wait processes to end
for process in processes:
process.join()
print("\nAll Processes Ended")
sys.exit(0)
# Process Parameters
processParameters = [
(run_http_server, (httpPort, httpPath, http_logPrint, websocketPort, "localhost", cameraPort, b"<END_OF_FILE>",)),
(run_picamera_driver, (cameraPort,)),
(run_websockets_server, (websocketPort, leftMotor_port, rightMotor_port, suctionMotor_port, websocket_logPrint)),
(run_gpio_driver, (driverPort_tuple, pin_tuple, pwmFreq_tuple, gpioDriver_logPrint)),
]
# ignore SIGINT which inherit by child processes
signal.signal(signal.SIGINT, signal.SIG_IGN)
# start processes
for processParameter in processParameters:
process = multiprocessing.Process(target=processParameter[0], args=processParameter[1])
processes.append(process)
process.start()
# assign handler to SIGINT
signal.signal(signal.SIGINT, signalHandler)
print("All Processes Started")
for process in processes:
process.join()
main()