-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart_simcar.py
321 lines (254 loc) · 10.1 KB
/
start_simcar.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
from multiprocessing import Process
import sys
from multiprocessing import Event, Pipe
import argparse
from multiprocessing.connection import Connection
from src import config as config_module
parser = argparse.ArgumentParser()
parser.add_argument(
"config_path", help="Path to the config file.", default="./config_pc.json"
)
args = parser.parse_args()
config_module.config_path = args.config_path
config = config_module.get_config()
from src.hardware.camera.CameraSpooferProcess import CameraSpooferProcess
from src.hardware.camera.cameraprocess import CameraProcess
from src.hardware.camera.SIMCameraProcess import SIMCameraProcess
from src.lib.perception.lanekeepz import LaneKeepingProcess as LaneKeeping
from src.lib.perception.signdetection import SignDetectionProcess
from src.data.localisationssystem.home_locProc import LocalisationProcess
from src.data.localisationssystem.locsysProc import LocalisationSystemProcess
from src.data.server_sim import ServerSIM as LocSysSIM
from src.data.server_sim import ServerSIM as IMUSIM
from src.lib.cortex.posfusproc import PositionFusionProcess
from src.data.environmentalserver.environmental import EnvironmentalHandler
from src.data.distance_sim import DistanceSIM
from src.hardware.ultrasonic.distanceProc import DistanceProcess
from src.data.trafficlights.trafficProc import TrafficProcess
from src.data.server_sim import ServerSIM as TrafficSIM
from src.lib.actuator.momentcontrol import MovementControl
from src.lib.actuator.sim_connect import SimulatorConnector
from src.hardware.serialhandler.SerialHandlerProcess import SerialHandlerProcess
from src.utils.camerastreamer.zmqStreamerProcess import CameraStreamerProcess
from src.utils.remotecontrol.RemoteControlReceiverProcess import (
RemoteControlReceiverProcess,
)
from src.lib.cortex.decisionproc import DecisionMakingProcess
import sys
from loguru import logger
from typing import List
isPI = True
try:
from src.utils.IMU.imuProc import IMUProcess
except Exception as e:
print(e)
isPI = False
# =================== CONFIG LOGGER ======================================
logger.level("PIPE", no=12, icon="==")
logger.level("SYNC", no=13, color="<yellow>")
logger.level("XY", no=14)
logger.level("TIME", no=15)
def filter(level: List[int]):
return lambda r: r["level"].no in level or r["level"].no > 19
TEST_PIPE = True
logger.remove()
if TEST_PIPE:
logger.add(
sys.stderr,
filter=filter([18]),
format="<green>{HH:mm:ss.SSS}</green> | <level>{level: <8}</level> | <cyan>{name}</cyan>:<cyan>{line}</cyan> - <level>{message}</level>",
)
logger.add(
"file1.log",
filter=lambda r: r["level"] == 14,
format="<green>{HH:mm:ss.SSS}</green> | <level>{level: <8}</level> | <cyan>{name}</cyan>:<cyan>{line}</cyan> - <level>{message}</level>",
)
# logger.level("LK", no=10, color="<blue>", icon='' )
# logger.level("INT", no=10, color="<blue>", icon='' )
# ========================================================================
# SCRIPT USED FOR WIRING ALL COMPONENTS
# ========================================================================
sys.path.append(".")
TRAFFIC_SIM_PORT = 7777
LOCSYS_SIM_PORT = 8888
LOCSYS_HOME_PORT = 8888
STREAM_PORT1 = 2244
STREAM_PORT2 = 4422
# ["cam", "lk", "sd"]
streams = ["lk"]
# =============================== INITIALIZING PROCESSES =================================
# Pipe collections
allProcesses: List[Process] = []
movementControlR = []
dataFusionInputName = []
posFusionInputName = []
camOutNames = []
# =============================== RC CONTROL =================================================
if config["enableRc"]:
# rc -> serial handler
rcShR, rcShS = Pipe(duplex=False)
rcProc = RemoteControlReceiverProcess([], [rcShS])
allProcesses.append(rcProc)
# ===================================== PERCEPTION ===================================
if config["enableLaneKeeping"]:
lkProc = LaneKeeping([], [], enable_stream=("lk" in streams))
allProcesses.append(lkProc)
dataFusionInputName.append("lk")
camOutNames.append("lk")
dataFusionOutPs: List[Connection] = []
if not config["enableSignDet"]:
if "sd" in streams:
streams.remove("sd")
if config["enableSignDet"]:
sDProc = SignDetectionProcess([], [], [], enable_stream=("sd" in streams))
allProcesses.append(sDProc)
dataFusionInputName.append("sd")
camOutNames.append("sd")
# =============================== DATA ===================================================
# -------LOCSYS----------
if config["enableSIM"]:
# LocSys -> Position Fusion
locsysProc = LocSysSIM([], [], "loc", LOCSYS_SIM_PORT)
allProcesses.append(locsysProc)
posFusionInputName.append("loc")
elif config["home_loc"]:
# LocSys -> Position Fusion
print(">>> Starting Home Localization process")
locsysProc = LocalisationProcess([], [])
allProcesses.append(locsysProc)
posFusionInputName.append("loc")
elif config["loc_server"]:
# LocSys -> Position Fusion
locsysProc = LocalisationSystemProcess([], [])
allProcesses.append(locsysProc)
posFusionInputName.append("loc")
# -------TrafficLightSemaphore----------
if config["enableSIM"]:
# Traffic Semaphore -> Decision Making (data fusion)
trafficProc = TrafficSIM([], [], "tl", TRAFFIC_SIM_PORT)
allProcesses.append(trafficProc)
dataFusionInputName.append("tl")
elif config["tl_server"]:
# Traffic Semaphore -> Decision Making (data fusion)
trafficProc = TrafficProcess([], [])
allProcesses.append(trafficProc)
dataFusionInputName.append("tl")
# ========================= IMU ===================================================
# IMU -> Position Fusino
if isPI and not config["enableSIM"]:
print("IMU process started")
imuProc = IMUProcess([], [])
allProcesses.append(imuProc)
posFusionInputName.append("imu")
else:
imuProc = IMUSIM([], [], "imu", 5555)
allProcesses.append(imuProc)
posFusionInputName.append("imu")
#
# # ===================== Position Fusion ==========================================
if len(posFusionInputName) > 0:
posfzzProc = PositionFusionProcess([], [], inPsnames=posFusionInputName)
allProcesses.append(posfzzProc)
dataFusionInputName.append("pos")
# ===================== Distance Sensor ==========================================
# Distance Sensor -> Decision Making (data fusion)
if config["enableSIM"]:
disProc = DistanceSIM([], [], 6666, log=False)
allProcesses.append(disProc)
dataFusionInputName.append("dis")
elif isPI:
disProc = DistanceProcess([], [])
allProcesses.append(disProc)
dataFusionInputName.append("dis")
# ==== Movement Control pipe
# Decision Process -> Movement control
FzzMcR, FzzMcS = Pipe(duplex=False)
dataFusionOutPs.append(FzzMcS)
# ======================= Environment Server ======================================
if config["env_server"]:
beacon = 23456
id = 120
serverpublickey = "publickey_server_test.pem"
clientprivatekey = "privatekey_client_test.pem"
gpsStR, gpsStS = Pipe(duplex=False)
envhandler = EnvironmentalHandler(
id, beacon, serverpublickey, gpsStR, clientprivatekey
)
allProcesses.append(envhandler)
dataFusionOutPs.append(gpsStS)
# ======================= Decision Making =========================================
# TODO enableTelemtry
if False:
dataFusionInputName.append("tel")
datafzzProc = DecisionMakingProcess([], dataFusionOutPs, inPsnames=dataFusionInputName)
allProcesses.append(datafzzProc)
movementControlR.append(FzzMcR)
#
# ======================= Actuator =================================================
# Movement control
if config["enableSIM"] and isPI:
# Movement control -> Serial handler
mcSHR, mcSHS = Pipe(duplex=False)
# Moment control -> SIM Serial Handler
mcSSHR, mcSSHS = Pipe(duplex=False)
cfProc = MovementControl(movementControlR, [mcSHS, mcSSHS])
allProcesses.append(cfProc)
else:
# Movement control -> Serial handler
mcSHR, mcSHS = Pipe(duplex=False)
cfProc = MovementControl(movementControlR, [mcSHS])
allProcesses.append(cfProc)
# Serial handler or Simulator Connector
if config["enableSIM"] and isPI:
# shProc = SimulatorConnector([mcSSHR], [])
# allProcesses.append(shProc)
shProc = SerialHandlerProcess([mcSHR], [])
allProcesses.append(shProc)
elif config["enableSIM"] and not isPI:
shProc = SimulatorConnector([mcSHR], [])
allProcesses.append(shProc)
else:
try:
shProc = SerialHandlerProcess([mcSHR], [])
allProcesses.append(shProc)
except Exception:
print("ERROR: Falied to start Serial Handler")
# ========================= Streamer =====================================================
if "cam" in streams:
camOutNames.append("stream")
if config["enableStream"]:
streamProc = CameraStreamerProcess([], [], streams[0], port=STREAM_PORT1)
allProcesses.append(streamProc)
if len(streams) > 1:
streamProc2 = CameraStreamerProcess([], [], streams[1], port=STREAM_PORT2)
allProcesses.append(streamProc2)
# ========================== Camera process ==============================================
if config["enableCameraSpoof"]:
camSpoofer = CameraSpooferProcess([], [], "vid")
allProcesses.append(camSpoofer)
else:
if config["enableSIM"]:
camProc = SIMCameraProcess([], [], camOutNames)
else:
camProc = CameraProcess([], [], camOutNames)
allProcesses.append(camProc)
# ===================================== START PROCESSES ==================================
print("Starting the processes!", allProcesses)
for proc in allProcesses:
proc.daemon = True
proc.start()
# ===================================== STAYING ALIVE ====================================
blocker = Event()
try:
blocker.wait()
except KeyboardInterrupt:
print("\nCatching a KeyboardInterruption exception! Shutdown all processes.\n")
for proc in allProcesses:
if hasattr(proc, "stop") and callable(getattr(proc, "stop")):
print("Process with stop", proc)
proc.stop()
proc.join()
else:
print("Process witouth stop", proc)
proc.terminate()
proc.join()