-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
63 lines (58 loc) · 2.07 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
"""Main file to run the FrED device"""
import threading
import time
import RPi.GPIO as GPIO
from database import Database
from user_interface import UserInterface
from fan import Fan
from spooler import Spooler
from extruder import Extruder
def hardware_control(gui: UserInterface) -> None:
"""Thread to handle hardware control"""
time.sleep(1)
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
try:
fan = Fan(gui)
spooler = Spooler(gui)
extruder = Extruder(gui)
fan.start(1000, 45)
spooler.start(1000, 0)
except Exception as e:
print(f"Error in hardware control: {e}")
gui.show_message("Error while starting the device",
"Please restart the program.")
init_time = time.time()
while True:
try:
current_time = time.time() - init_time
Database.time_readings.append(current_time)
if gui.start_motor_calibration:
spooler.calibrate()
gui.start_motor_calibration = False
if gui.heater_open_loop_enabled:
extruder.temperature_open_loop_control(current_time)
elif gui.device_started:
extruder.temperature_control_loop(current_time)
extruder.stepper_control_loop()
if gui.spooling_control_state:
spooler.motor_control_loop(current_time)
fan.control_loop()
time.sleep(0.05)
except Exception as e:
print(f"Error in hardware control loop: {e}")
gui.show_message("Error in hardware control loop",
"Please restart the program.")
fan.stop()
spooler.stop()
extruder.stop()
if __name__ == "__main__":
print("Starting FrED Device...")
ui = UserInterface()
time.sleep(2)
hardware_thread = threading.Thread(target=hardware_control, args=(ui,))
hardware_thread.start()
threading.Lock()
ui.start_gui()
hardware_thread.join()
print("FrED Device Closed.")