-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIO_module.py
472 lines (360 loc) · 17.7 KB
/
IO_module.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import time
import numpy as np
import can
import can.interfaces.kvaser
class IO(QObject):
"""
Class representing Input/Output operations for controlling motors and reading encoder values via a CAN Bus.
Attributes:
fourq (object): An object representing the class that handles the communication with the 4Q Sensor
A (int): Last known Azimuth position.
E (int): Last known elevation position.
data (dict): Dictionary for position data and data from the 4Q sensor
FQ_out (dict): Dictionary for storing raw output data from the 4Q sensor.
motor_ID_A (int): ID for Azimuth motor.
motor_ID_E (int): ID for Elevation motor.
motor_reply_ID_E (int): Reply ID for Elevation motor.
motor_reply_ID_A (int): Reply ID for Azimuth motor.
enc_ID_E (int): ID for Elevation encoder.
enc_ID_A (int): ID for Azimuth encoder.
enc_SID_E (int): Elevation encoder SID.
enc_SID_A (int): Azimuth encoder SID.
N_no_answer (dict): Dictionary to track no answer counts.
good_to_send (dict): Dictionary to track send statuses.
status_reply (dict): Dictionary to track reply statuses.
M_status (dict): Dictionary to track motor statuses.
M_rep_C (dict): Dictionary to track motor reply C.
M_rep_M (dict): Dictionary to track motor reply M.
Enc_offset_A (int): Offset for Azimuth encoder.
Enc_offset_E (int): Offset for Elevation encoder.
scaler_enc (float): Encoder scaler.
scaler_step (int): Step scaler.
drehmatrix (list): Rotation matrix.
bus (can.interface.Bus): CAN bus interface.
"""
def __init__(self,fourq): #fourq einfügen
"""
Initialize the IO class.
Args:
fourq (object): An object representing the class that handles the communication with the 4Q Sensor
"""
self.fourq = fourq
self.A= 180 # last known Azimuth position
self.E= 63 # last known elevation position
self.data = {}
self.FQ_out = {}
self.motor_ID_A = 1
self.motor_ID_E = 3
self.motor_reply_ID_E = 4
self.motor_reply_ID_A = 2
self.enc_ID_E = 0x185
self.enc_ID_A = 0x184
self.enc_SID_E = 1541
self.enc_SID_A = 1540
self.N_no_answer = {'E': 0, 'A': 0}
self.good_to_send = {'E': 1, 'A': 1}
self.status_reply = {'E': 0, 'A': 0}
self.M_status = {'E': 100, 'A': 100}
self.M_rep_C = {'E': 0, 'A': 0}
self.M_rep_M = {'E': 0, 'A': 0}
self.Enc_offset_A=141
self.Enc_offset_E=-217
self.soft_end_switch = 0
self.scaler_enc=2**18/-360
self.scaler_step=1
self.drehmatrix = [[0,-1],[1,0]]
self.bus = can.interface.Bus(bustype="kvaser", channel=0, bitrate=500000)
self.bus.set_filters([{"can_id": 0, "can_mask": 0x7FF, "extended": False},
{"can_id": 2, "can_mask": 0x7FF, "extended": False}])
self.bus.set_filters([{"can_id": self.enc_ID_A, "can_mask": 0x1FFFFFFF, "extended": True},
{"can_id": self.enc_ID_E, "can_mask": 0x1FFFFFFF, "extended": True},
{"can_id": self.motor_reply_ID_E, "can_mask": 0x1FFFFFFF, "extended": True},
{"can_id": self.motor_reply_ID_A, "can_mask": 0x1FFFFFFF, "extended": True}])
# Initialize Encoders and Motor End Switches
self.initialize_encoder(self.enc_SID_A)
self.initialize_encoder(self.enc_SID_E)
#Initialize motor end switch
self.initialize_motor_end_switches()
# Disable Power Off at Zero Velocity for Motor
self.disable_power_off_at_zero_velocity()
def send_data(self, M):
"""
Send data to motors based on input values.
Args:
M (dict): A dictionary containing speed values for the motor.
Returns:
None
"""
if M['E'] <= 0:
direction = 1 #rotate right
speed = int(abs(M['E']*self.scaler_step))
byte0 = direction.to_bytes(1, "big")
byte1 = speed.to_bytes(6, "big")
byte= byte0+byte1
self.msg_E = can.Message(is_extended_id=True,dlc=7,arbitration_id=self.motor_ID_E,is_fd=False,data=byte)
else:
direction = 2 #rotate left
speed = int(abs(M['E']*self.scaler_step))
byte0 = direction.to_bytes(1, "big")
byte1 = speed.to_bytes(6, "big")
byte= byte0+byte1
self.msg_E = can.Message(is_extended_id=True,dlc=7,arbitration_id=self.motor_ID_E,is_fd=False,data=byte)
if M['A'] <= 0:
direction = 1 #rotate right
speed = int(abs(M['A']*self.scaler_step))
byte0 = direction.to_bytes(1, "big")
byte1 = speed.to_bytes(6, "big")
byte= byte0+byte1
self.msg_A = can.Message(is_extended_id=True,dlc=7,arbitration_id=self.motor_ID_A,is_fd=False,data=byte)
else:
direction = 2 #rotate left
speed = int(abs(M['A']*self.scaler_step))
byte0 = direction.to_bytes(1, "big")
byte1 = speed.to_bytes(6, "big")
byte= byte0+byte1
self.msg_A = can.Message(is_extended_id=True,dlc=7,arbitration_id=self.motor_ID_A,is_fd=False,data=byte)
if self.good_to_send['E'] == 1:
#print(self.msg_E)
self.bus.send(self.msg_E)
self.good_to_send['E'] = 0
else:
print('no reply from motor E')
self.N_no_answer['E'] += 1
if self.N_no_answer['E'] > 4:
self.N_no_answer['E'] = 0
self.good_to_send['E'] = 1
if self.good_to_send['A'] == 1:
#print(self.msg_A)
self.bus.send(self.msg_A)
self.good_to_send['A'] = 0
else:
print('no reply from motor A')
self.N_no_answer['A'] += 1
if self.N_no_answer['A'] > 4:
self.N_no_answer['A'] = 0
self.good_to_send['A'] = 1
return self.soft_end_switch
def getdata(self):
"""
Get data from motors that are already processed.
Returns:
dict: A dictionary containing motor data and positional data from the 4Q Sensor.
"""
self.data['A'] = self.A
self.data['E'] = self.E
self.data['FQ'] = self.get_4Q_data()
return self.data
def get_4Q_data(self):
"""
Gets the data from the 4Q sensor via the Q4_com_module
Returns:
numpy.ndarray: Transformed data.
"""
self.fq = self.fourq.get_data()
self.FQ_out['X'] = self.fq['X']
self.FQ_out['Y'] = self.fq['Y']
self.t1 = np.dot(self.drehmatrix, np.array([self.fq['X'],self.fq['Y']]))
self.variable_drehmatrix = [[np.cos(-self.A),-np.sin(-self.A)],[-np.sin(-self.A),-np.cos(-self.A)]]
self.t2 = np.dot(self.variable_drehmatrix,self.t1)
return self.t2
def request_manual_input(self):
"""
Request manual input for motors to controll the Heliostat with the switches on the outside of the building.
Returns:
None
"""
data = b'\x0f\xff\x00\x00\x00\x00\x00'
self.msg_E = can.Message(is_extended_id=True,dlc=7,arbitration_id=self.motor_ID_E,is_fd=False,data=data)
self.bus.send(self.msg_E)
data = b'\x0f\xff\x00\x00\x00\x00\x00'
self.msg_A = can.Message(is_extended_id=True,dlc=7,arbitration_id=self.motor_ID_A,is_fd=False,data=data)
self.bus.send(self.msg_A)
def request_motor_data(self):
"""
Request motor data.
Returns:
None
"""
data = b'\x06\xce\x00\x00\x00\x00\x00'
self.msg_E = can.Message(is_extended_id=True,dlc=7,arbitration_id=self.motor_ID_E,is_fd=False,data=data)
self.bus.send(self.msg_E)
'''
data = b'\x06\xce\x00\x00\x00\x00\x00'
self.msg_A = can.Message(is_extended_id=True,dlc=7,arbitration_id=self.motor_ID_A,is_fd=False,data=data)
self.bus.send(self.msg_A)
'''
data = b'\x06\x0b\x00\x00\x00\x00\x00'
self.msg_A = can.Message(is_extended_id=True,dlc=7,arbitration_id=self.motor_ID_A,is_fd=False,data=data)
self.bus.send(self.msg_A)
def start_notifier(self):
"""
Start the CAN bus notifier to receive messages asynchronously.
Returns:
None
"""
bus = can.interface.Bus(bustype="kvaser", channel=0,bitrate=500000)
can.Notifier(bus,[self.receive_fcn()])
def receive_fcn(self):
"""
Function to receive CAN messages and process them.
This function continuously listens for CAN messages and updates motor data and statuses accordingly.
Returns:
None
Raises:
can.CanError: If an error frame is detected during message reception.
"""
bus = can.interface.Bus(bustype="kvaser", channel=0,bitrate=500000)
try:
for msg in bus:
if msg.arbitration_id == self.enc_ID_A:
relevant_bytes = msg.data[:4]
self.A = -(int.from_bytes(relevant_bytes, byteorder='little', signed=True))/self.scaler_enc + self.Enc_offset_A
if self.A > 360:
self.A = self.A - 360
elif msg.arbitration_id == self.enc_ID_E:
relevant_bytes = msg.data[:4]
self.E = -(int.from_bytes(relevant_bytes, byteorder='little', signed=True))/self.scaler_enc + self.Enc_offset_E
elif msg.arbitration_id == self.motor_reply_ID_E:
self.M_status['E'] = int.from_bytes(msg.data[1:2], byteorder='little', signed=True)
axis_parameter=int.from_bytes(msg.data[2:3], byteorder='little', signed=True)
if axis_parameter == 2 or axis_parameter == 15:
self.M_rep_C['E'] = axis_parameter
self.M_rep_M['E'] = int.from_bytes(msg.data[6:10], byteorder='little', signed=True)/self.scaler_step
elif axis_parameter == 6:
self.status_reply['E'] = int.from_bytes(msg.data[6:10], byteorder='little', signed=True)
if not (self.M_status['E'] & 100):
print("Data Transmission Error Motor E")
else:
self.good_to_send['E'] = 1
elif msg.arbitration_id == self.motor_reply_ID_A:
self.M_status['A'] = int.from_bytes(msg.data[1:2], byteorder='little', signed=True)
axis_parameter = int.from_bytes(msg.data[2:3], byteorder='little', signed=True)
if axis_parameter == 2 or axis_parameter == 15:
self.M_rep_C['A'] = axis_parameter
self.M_rep_M['A'] = int.from_bytes(msg.data[6:10], byteorder='little', signed=True)/self.scaler_step
if axis_parameter == 6:
self.status_reply['A'] = int.from_bytes(msg.data[6:10], byteorder='little', signed=True)
if self.status_reply['A'] == 0 or self.status_reply['A'] == 1:
self.status_soft_end_switch = int.from_bytes(msg.data[6:10], byteorder='little', signed=True)
if not (self.M_status['A'] & 100):
print("Data Transmission Error Motor E")
else:
self.good_to_send['A'] = 1
except can.CanError:
print("Error frame detected")
#Functions to initialize the motors and the encoder
def initialize_encoder(self, enc_SID):
"""
Initialize an encoder.
Args:
SID (int): Encoder SID.
"""
messageout1 = can.Message(arbitration_id=enc_SID, is_extended_id=False, dlc=8)
messageout1.data = b'\x22\x00\x18\x02\xfe\x00\x00\x00'
self.bus.send(messageout1)
messageout2 = can.Message(arbitration_id=enc_SID, is_extended_id=False, dlc=8)
messageout2.data = b'\x22\x00\x18\x05\xe8\x03\x00\x00'
self.bus.send(messageout2)
messageout3 = can.Message(arbitration_id=0, is_extended_id=False, dlc=8, data=[0] * 8)
node_id = [(enc_SID - 0x600) & 0xFF]
node_id = node_id[0]
data = bytes([1,node_id,0,0,0,0,0,0])
messageout3.data = data
self.bus.send(messageout3)
def initialize_motor_end_switches(self):
"""
Disable motor end switches.
Args:
motor_ID (int): Motor ID.
"""
# Left end switch
messageout = can.Message(arbitration_id=self.motor_ID_E, is_extended_id=True, dlc=7)
messageout.data = b'\x05\x0d\x00\x00\x00\x00\x01'
self.bus.send(messageout)
# Right end switch
messageout.data = b'\x05\x0c\x00\x00\x00\x00\x01'
self.bus.send(messageout)
# Left end switch
messageout = can.Message(arbitration_id=self.motor_ID_A, is_extended_id=True, dlc=7)
messageout.data = b'\x05\x0d\x00\x00\x00\x00\x00'
self.bus.send(messageout)
# Right end switch
messageout.data = b'\x05\x0c\x00\x00\x00\x00\x00'
self.bus.send(messageout)
def disable_power_off_at_zero_velocity(self):
"""
Disable power off at zero velocity for a motor. !!!Attention with this settings!!! Study the manual before changing the power for the motor.
Args:
motor_ID (int): Motor ID.
"""
messageout = can.Message(arbitration_id=self.motor_ID_E, is_extended_id=True, dlc=7)
messageout.data = b'\x05\x07\x00\x00\x00\x00\x10'
self.bus.send(messageout)
def stop_encoder(self):
"""
Sends stop commands to the encoder.
Returns:
None
"""
data = b'\x02\x04\x00\x00\x00\x00\x00\x00'
messageout1 = can.Message(is_extended_id=False,dlc=8,arbitration_id=0,is_fd=False,data=data)
self.bus.send(messageout1)
data = b'\x02\x05\x00\x00\x00\x00\x00\x00'
messageout2 = can.Message(is_extended_id=False,dlc=8,arbitration_id=0,is_fd=False,data=data)
self.bus.send(messageout2)
def rotate_right(self):
"""
This turns the motor to the right infinitely long
"""
while True:
vel = 5000
direction = 1 #rotate right
speed = int(abs(vel))
byte0 = direction.to_bytes(1, "big")
byte1 = speed.to_bytes(6, "big")
byte= byte0+byte1
print(byte)
self.msg_E = can.Message(is_extended_id=True,dlc=7,arbitration_id=self.motor_ID_E,is_fd=False,data=byte)
self.bus.send(self.msg_E)
time.sleep(1)
print("send")
def getting_params(self):
print("I am here")
data = b'\x06\x04\x00\x00\x00\x00\x00'
messageout1 = can.Message(is_extended_id=False,dlc=7,arbitration_id=self.motor_ID_E,is_fd=False,data=data)
self.bus.send(messageout1)
print("send 1")
time.sleep(5)
data = b'\x06\x05\x00\x00\x00\x00\x00'
messageout1 = can.Message(is_extended_id=False,dlc=7,arbitration_id=self.motor_ID_E,is_fd=False,data=data)
self.bus.send(messageout1)
print("send 2")
time.sleep(5)
data = b'\x06\x06\x00\x00\x00\x00\x00'
messageout1 = can.Message(is_extended_id=False,dlc=7,arbitration_id=self.motor_ID_E,is_fd=False,data=data)
self.bus.send(messageout1)
print("send 3")
time.sleep(5)
data = b'\x06\x07\x00\x00\x00\x00\x00'
messageout1 = can.Message(is_extended_id=False,dlc=7,arbitration_id=self.motor_ID_E,is_fd=False,data=data)
self.bus.send(messageout1)
print("send 4")
time.sleep(5)
data = b'\x06\x8c\x00\x00\x00\x00\x00'
messageout1 = can.Message(is_extended_id=False,dlc=7,arbitration_id=self.motor_ID_E,is_fd=False,data=data)
self.bus.send(messageout1)
print("send 5")
time.sleep(5)
def delete(self):
"""
Calls the stop function of the encoder and shuts down the CAN bus interface
Returns:
None
"""
M = {'A': 0, 'E': 0}
self.send_data(M)
self.stop_encoder()
self.bus.shutdown()
del self.bus