-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTraffic_Lights_Competition.py
51 lines (41 loc) · 1.53 KB
/
Traffic_Lights_Competition.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
import time
import math
import struct
from quanser.communications import Stream
from qvl.qlabs import QuanserInteractiveLabs
from qvl.traffic_light import QLabsTrafficLight
# creates a server connection with Quanser Interactive Labs and manages the communications
qlabs = QuanserInteractiveLabs()
print("Connecting to QLabs...")
# trying to connect to QLabs and open the instance we have created - program will end if this fails
try:
qlabs.open("localhost")
except:
print("Unable to connect to QLabs")
# traffic light
x_offset = 0.13
y_offset = 1.67
TrafficLight0 = QLabsTrafficLight(qlabs)
TrafficLight0.spawn_degrees([2.3 + x_offset, y_offset, 0], [0, 0, 0], scale=[.1, .1, .1], configuration=0, waitForConfirmation=True)
TrafficLight0.set_state(QLabsTrafficLight.STATE_GREEN)
TrafficLight1 = QLabsTrafficLight(qlabs)
TrafficLight1.spawn_degrees([-2.3 + x_offset, -1 + y_offset, 0], [0, 0, 180], scale=[.1, .1, .1], configuration=0, waitForConfirmation=True)
TrafficLight1.set_state(QLabsTrafficLight.STATE_RED)
i = 0
try:
while (True):
i = i + 1
# print (i)
if i % 2 == 0:
TrafficLight0.set_state(QLabsTrafficLight.STATE_RED)
TrafficLight1.set_state(QLabsTrafficLight.STATE_RED)
time.sleep(5)
else:
TrafficLight1.set_state(QLabsTrafficLight.STATE_GREEN)
TrafficLight0.set_state(QLabsTrafficLight.STATE_GREEN)
time.sleep(2)
except KeyboardInterrupt:
TrafficLight0.destroy()
TrafficLight1.destroy()
qlabs.close()
print("Done!")