-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmulti_thread_servo_control_test.py
120 lines (87 loc) · 3.06 KB
/
multi_thread_servo_control_test.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
import time
from control import (
XboxController,
OperatingMode,
handle_operating_mode,
Mast,
SoundEffects,
handle_check_mode,
play_check_mode_sound,
)
import threading
from threading import Event
def main():
mast = Mast()
joy = XboxController()
sounds_effects = SoundEffects()
operating_mode = OperatingMode.EMERGENCY_STOP
# Set up background threads for stationary mode
end_event = Event()
reset_event = Event()
# Pause background threads on start
end_event.set()
reset_event.clear()
thread_one = threading.Thread(
target=thread_one_control, args=(mast, end_event, reset_event)
)
thread_two = threading.Thread(
target=thread_two_control, args=(mast, end_event, reset_event)
)
thread_one.daemon = True
thread_two.daemon = True
thread_one.start()
thread_two.start()
while True:
control_inputs = joy.read()
new_operating_mode = handle_operating_mode(control_inputs["operating_mode"])
# Alert the user if the operating mode has changed
if new_operating_mode and operating_mode != new_operating_mode:
operating_mode = new_operating_mode
sounds_effects.play_change_mode()
# If user wants to check mode, play the sound effect for the current mode
if handle_check_mode(control_inputs["check_mode"]):
play_check_mode_sound(operating_mode, sounds_effects)
# Pause the stationary background threads if the operating mode is not stationary
if operating_mode != OperatingMode.STATIONARY:
end_event.set()
reset_event.clear()
if operating_mode == OperatingMode.DRIVE:
# Use Controller to drive the rover and control the mast
pass
# wheels.handle_inputs(control_inputs["wheels"])
elif operating_mode == OperatingMode.ROBOTIC_ARM:
# Use Controller to control the robotic arm
pass
elif operating_mode == OperatingMode.STATIONARY:
# Stationary mode
# Random robotic arm movement
# Camera track face, alien thing
# Reset the flags for the stationary mode threads
if end_event.is_set():
end_event.clear()
reset_event.set()
elif operating_mode == OperatingMode.EMERGENCY_STOP:
# Send stop commands to all components
pass
# wheels.stop()
time.sleep(0.01)
def thread_one_control(mast: Mast, end_event: Event, reset_event: Event):
i = 0
while True:
if end_event.is_set():
reset_event.wait()
time.sleep(1)
print(f"Thread one speaking {i}")
mast.rotate_counterclockwise(50)
i += 1
def thread_two_control(mast: Mast, end_event: Event, reset_event: Event):
i = 0
while True:
if end_event.is_set():
reset_event.wait()
mast.rotate_clockwise(50)
print(f"Thread two speaking {i}")
time.sleep(1)
i += 1
if __name__ == "__main__":
main()