-
Notifications
You must be signed in to change notification settings - Fork 0
/
acsclientcontroller.py
184 lines (163 loc) · 6.8 KB
/
acsclientcontroller.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 socket
import struct
"""
DATA FORMAT
+----------------------------------------+-------------+-------------+------------------+
| msgeid (8 bytes) | double_1 (8B)| double_2 (8B)| double_3 (8B) |
+----------------------------------------+-------------+-------------+------------------+
| CONNECT_ACS_SIMULATOR (1) | 0 | 0 | 0 |
| CONNECT_ACS_REAL_HARDWARE (2) | 0 | 0 | 0 |
| CLEAR_FAULTS_ACS_STAGE (3) | 0 | 0 | 0 |
| ENABLE_ACS_STAGE (4) | 0 | 0 | 0 |
| COMMUTE_ACS_STAGE (5) | 0 | 0 | 0 |
| SET_ACCEL_ACS_STAGE (6) | 0 | 0 | acceleration |
| SET_VEL_ACS_STAGE (7) | 0 | velocity | 0 |
| MOVE_ACS_STAGE (8) | targetpos | targetpos | targetpos |
| HALT_ACS_STAGE (9) | 0 | 0 | 0 |
| SET_DECEL_ACS_STAGE (10) | 0 | 0 | deceleration |
| SEND_POS_ACS_STAGE (11) | posx | posy | posa |
+----------------------------------------+-------------+-------------+------------------+
"""
# Data structure
class ZStageData:
def __init__(self, dataframe_1, dataframe_2, dataframe_3, dataframe_4):
self.dataframe1 = dataframe_1
self.dataframe2 = dataframe_2
self.dataframe3 = dataframe_3
self.dataframe4 = dataframe_4
self.connected = False # Flag to track the connection status
def pack(self):
# Pack data into a binary format (using struct)
header = struct.pack("!d", self.dataframe1)
payload = struct.pack("!ddd", self.dataframe2, self.dataframe3, self.dataframe4)
return header + payload
class ACSClient:
"""Class to connect to ACS server module and control the linear actuator."""
def __init__(self, server_ip: int, server_port: str):
self.server_ip = server_ip
self.server_port = server_port
self.client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
def struct(self, dataframe_1, dataframe_2, dataframe_3, dataframe_4):
res = ZStageData(dataframe_1, dataframe_2, dataframe_3, dataframe_4)
structed_data = res.pack()
return structed_data
def connect_client(self):
try:
self.client_socket.connect((self.server_ip, self.server_port))
self.connected = True # Connection successful
return True
except ConnectionError as ce:
print(f"Got ConnectionError while connecting to ACS server: {ce}")
return False
except Exception as e:
print(f"Error in connect_client: {e}")
raise
def send_data(self, structed_data):
try:
self.client_socket.sendall(structed_data)
res = self.client_socket.recv(1024)
response_str = res.decode("utf-8")
return response_str
except ConnectionResetError as cre:
raise ConnectionError("ERROR - Connection reset by peer") from cre
except Exception as e:
print(f"Error in send_data: {e}")
raise
def close_connection(self):
self.client_socket.close()
print("Connection closed.")
def connect_acs_simulator(self):
dataframe1 = 1
dataframe2 = 0
dataframe3 = 0
dataframe4 = 0
res = self.struct(dataframe1, dataframe2, dataframe3, dataframe4)
res_server = self.send_data(res).strip("\x00")
return res_server
def connect_acs_real_hardware(self):
dataframe1 = 2
dataframe2 = 0
dataframe3 = 0
dataframe4 = 0
res = self.struct(dataframe1, dataframe2, dataframe3, dataframe4)
res_server = self.send_data(res).strip("\x00")
return res_server
def clear_faults_acs_stage(self):
dataframe1 = 3
dataframe2 = 0
dataframe3 = 0
dataframe4 = 0
res = self.struct(dataframe1, dataframe2, dataframe3, dataframe4)
res_server = self.send_data(res).strip("\x00")
return res_server
def enable_acs_stage(self):
dataframe1 = 4
dataframe2 = 0
dataframe3 = 0
dataframe4 = 0
res = self.struct(dataframe1, dataframe2, dataframe3, dataframe4)
res_server = self.send_data(res).strip("\x00")
return res_server
def commute_acs_stage(self):
dataframe1 = 5
dataframe2 = 0
dataframe3 = 0
dataframe4 = 0
res = self.struct(dataframe1, dataframe2, dataframe3, dataframe4)
res_server = self.send_data(res).strip("\x00")
return res_server
def halt_acs_stage(self):
dataframe1 = 9
dataframe2 = 0
dataframe3 = 0
dataframe4 = 0
res = self.struct(dataframe1, dataframe2, dataframe3, dataframe4)
res_server = self.send_data(res).strip("\x00")
return res_server
def set_accel_acs_stage(self, accel):
"""mm/s2"""
dataframe1 = 6
dataframe2 = 0
dataframe3 = 0
dataframe4 = accel
res = self.struct(dataframe1, dataframe2, dataframe3, dataframe4)
res_server = self.send_data(res).strip("\x00")
return res_server
def set_decel_acs_stage(self, decel):
"""mm/s2"""
dataframe1 = 10
dataframe2 = 0
dataframe3 = 0
dataframe4 = decel
res = self.struct(dataframe1, dataframe2, dataframe3, dataframe4)
res_server = self.send_data(res).strip("\x00")
return res_server
def set_vel_acs_stage(self, vel):
"""mm/s"""
dataframe1 = 7
dataframe2 = 0
dataframe3 = vel
dataframe4 = 0
res = self.struct(dataframe1, dataframe2, dataframe3, dataframe4)
res_server = self.send_data(res).strip("\x00")
return res_server
def move_acs_stage(self, targetpos_x, targetpos_y, targetpos_a):
"""mm"""
dataframe1 = 8
dataframe2 = targetpos_x
dataframe3 = targetpos_y
dataframe4 = targetpos_a
res = self.struct(dataframe1, dataframe2, dataframe3, dataframe4)
res_server = self.send_data(res).strip("\x00")
return res_server
def get_pos(self):
"""(xpos,ypos,apos) [mm,mm,mm]"""
dataframe1 = 11
dataframe2 = 0
dataframe3 = 0
dataframe4 = 0
res = self.struct(dataframe1, dataframe2, dataframe3, dataframe4)
res_server = self.send_data(res)
pos_string = res_server.strip("\x00")
pos_values = [float(value.split(":")[1]) for value in pos_string.split()]
return pos_values