-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
110 lines (94 loc) · 3.47 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
import sys
import os
import time
from PySide6.QtGui import QGuiApplication, QIcon
from PySide6.QtQml import QQmlApplicationEngine
from PySide6.QtCore import QObject, Slot, Signal
from acspy import acsc
class MainWindow(QObject):
def __init__(self):
QObject.__init__(self)
# Connect to the driver via COM port
try:
self.hc: object = acsc.OpenCommSerial()
# Activating axis 0
acsc.enable(self.hc, 0)
# Maximum torque applied to the motor that triggers the stopping mechanism
acsc.setJerk(self.hc, 0, 1000)
acsc.setAcceleration(self.hc, 0, 500)
acsc.setDeceleration(self.hc, 0, 100)
self.flags: int = acsc.AMF_RELATIVE
except acsc.AcscError as e:
print(f"Connection error to the servo driver: {e}")
# Error Handling
def my_excepthook(exctype, value, traceback):
sys.__excepthook__(exctype, value, traceback)
print("exctype = ", exctype)
print("value = ", value)
if str(value) == "name 'self.hc' is not defined":
print("The servo is not connected")
sys.excepthook = my_excepthook
@Slot()
def initializing_drive(self):
# Run the initialization script from 1 buffer of the flash controller
acsc.runBuffer(self.hc, 1)
print("Starting drive initialization")
@Slot()
def zero_position(self):
# Set the drive to the zero position
acsc.runBuffer(self.hc, 20)
print("Setting the drive to the zero position")
@Slot(float, int)
def rotation(self, speed: float, angle: int):
"""Turning the engine clockwise
Args:
speed (float): Turning speed
angle (int): Turning angle
"""
# If the axis is disabled, then turn it on
if not acsc.getMotorEnabled(self.hc, 0):
# Activating axis 0
acsc.enable(self.hc, 0)
time.sleep(0.3)
acsc.setVelocity(self.hc, 0, speed)
acsc.toPoint(self.hc, self.flags, 0, angle * -1)
print(
f"Turning the motor counterclockwise by an angle {angle} with speed {speed}"
)
@Slot(float, int)
def reverse_rotation(self, speed: float, angle: int):
"""Turning the engine counterclockwise
Args:
speed (float): Turning speed
angle (int): Turning angle
"""
# If the axis is disabled, then turn it on
if not acsc.getMotorEnabled(self.hc, 0):
# Activating axis 0
acsc.enable(self.hc, 0)
time.sleep(0.3)
acsc.setVelocity(self.hc, 0, speed)
acsc.toPoint(self.hc, self.flags, 0, angle)
print(
f"Turning the motor counterclockwise by an angle {angle} with speed {speed}"
)
@Slot()
def stop_rotation(self):
# Disabling the axis
acsc.disable(self.hc, 0)
print("The engine is stopped")
if __name__ == "__main__":
# Creating an application instance
app = QGuiApplication(sys.argv)
# Creating a QML engine
engine = QQmlApplicationEngine()
# Create a window class
main = MainWindow()
engine.rootContext().setContextProperty("backend", main)
# Installing the icon
app.setWindowIcon(QIcon("icon.ico"))
# Load the QML file into the engine
engine.load(os.path.join(os.path.dirname(__file__), "qml/main.qml"))
if not engine.rootObjects():
sys.exit(-1)
sys.exit(app.exec_())