-
Notifications
You must be signed in to change notification settings - Fork 0
/
zaxis.py
184 lines (164 loc) · 6.14 KB
/
zaxis.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
import os
import sys
import time # Import the time module for sleep
file_dir = os.path.dirname(__file__)
sys.path.append(file_dir)
from acsclientcontroller import *
flag_axis = False
MAX_VEL = 2.0 # mm/s (Adjust as needed)
MAX_ACCEL = 2.0 # mm/s^2 (Adjust as needed)
MAX_DECEL = 2.0 # mm/s^2 (Adjust as needed)
class Axis:
"""Class to control a z ACS controller."""
def __init__(self, mechres=0):
if not flag_axis:
self.b = ACSClient("127.0.0.1", 4000)
res = self.b.connect_client()
if not res:
raise ValueError("ERROR: Could not connect to ACSClient")
res = self.b.connect_acs_simulator()
if res != "OK":
raise ValueError("ERROR: Could not connect to ACS simulator")
else:
self.b = ACSClient("100.0.0.10", 9999)
res = self.b.connect_client()
if res != "OK":
raise ValueError("ERROR: Could not connect to ACSClient")
res = self.b.connect_acs_real_hardware()
if res != "OK":
raise ValueError("ERROR: Could not connect to real hardware")
def init(self):
if not flag_axis:
res = self.b.connect_acs_simulator()
if res != "OK":
raise ValueError("ERROR: Could not connect to ACS simulator")
else:
res = self.b.connect_acs_real_hardware()
if res != "OK":
raise ValueError("ERROR: Could not connect to real hardware")
res = self.b.clear_faults_acs_stage()
if res != "OK":
raise ValueError("ERROR: Could not clear faults in ACS stage")
res = self.b.enable_acs_stage()
if res != "OK":
raise ValueError("ERROR: Could not enable ACS stage")
res = self.b.commute_acs_stage()
if res != "OK":
raise ValueError("ERROR: Could not commute ACS stage")
def recover(self):
if not flag_axis:
res = self.b.connect_acs_simulator()
if res != "OK":
raise ValueError("ERROR: Could not connect to ACS simulator")
else:
res = self.b.connect_acs_real_hardware()
if res != "OK":
raise ValueError("ERROR: Could not connect to real hardware")
res = self.b.clear_faults_acs_stage()
if res != "OK":
raise ValueError("ERROR: Could not clear faults in ACS stage")
res = self.b.enable_acs_stage()
if res != "OK":
raise ValueError("ERROR: Could not enable ACS stage")
def command_movr_mm(
self, speed: float, accel: float, decel: float, distance: float
):
"""Relative stage movement."""
try:
if speed > MAX_VEL:
raise ValueError(
f"Speed value must be less than or equal to {MAX_VEL} mm/s"
)
if accel > MAX_ACCEL:
raise ValueError(
f"Acceleration value must be less than or equal to {MAX_ACCEL} mm/s^2"
)
if decel > MAX_DECEL:
raise ValueError(
f"Deceleration value must be less than or equal to {MAX_DECEL} mm/s^2"
)
res = self.b.set_vel_acs_stage(speed)
if res != "OK":
return f"ERROR - {res}"
res = self.b.set_accel_acs_stage(accel)
if res != "OK":
return f"ERROR - {res}"
res = self.b.set_decel_acs_stage(decel)
if res != "OK":
return f"ERROR - {res}"
res = self.b.move_acs_stage(distance, distance, distance)
if res != "OK":
return f"ERROR - {res}"
return "OK"
except ConnectionError as ce:
return f"ERROR - ConnectionError: {ce}"
except ValueError as ve:
return f"ERROR - {ve}"
def command_movr_mm_(
self,
speed: float,
accel: float,
decel: float,
distance_x: float,
distance_y: float,
distance_a,
):
"""Relative stage movement."""
try:
if speed > MAX_VEL:
raise ValueError(
f"Speed value must be less than or equal to {MAX_VEL} mm/s"
)
if accel > MAX_ACCEL:
raise ValueError(
f"Acceleration value must be less than or equal to {MAX_ACCEL} mm/s^2"
)
if decel > MAX_DECEL:
raise ValueError(
f"Deceleration value must be less than or equal to {MAX_DECEL} mm/s^2"
)
res = self.b.set_vel_acs_stage(speed)
if res != "OK":
return f"ERROR - {res}"
res = self.b.set_accel_acs_stage(accel)
if res != "OK":
return f"ERROR - {res}"
res = self.b.set_decel_acs_stage(decel)
if res != "OK":
return f"ERROR - {res}"
res = self.b.move_acs_stage(distance_x, distance_y, distance_a)
if res != "OK":
return f"ERROR - {res}"
return "OK"
except ConnectionError as ce:
return f"ERROR - ConnectionError: {ce}"
except ValueError as ve:
return f"ERROR - {ve}"
def update(self):
try:
res = self.b.get_pos()
if (
(isinstance(res, tuple) or isinstance(res, list))
and len(res) == 3
and all(isinstance(val, float) for val in res)
):
return res
else:
return "ERROR - Unexpected position data"
except ConnectionError as ce:
return f"ERROR - ConnectionError: {ce}"
if __name__ == "__main__":
"""Test code for the API"""
z = Axis()
# Move forward
result = z.command_movr_mm(speed=1.5, accel=1.5, decel=1.5, distance=10.0)
if result == "OK":
print(z.update())
else:
print(result)
# Move backward
result = z.command_movr_mm(speed=1.5, accel=1.5, decel=1.5, distance=-10.0)
if result == "OK":
print(z.update())
else:
print(result)