forked from zietzm/Helmholtz_Test_Bench
49 lines
1.6 KiB
Python
49 lines
1.6 KiB
Python
import serial
|
|
import time
|
|
from datetime import datetime
|
|
import socket
|
|
|
|
# Lookup table for cage axis to magnetometer transformation
|
|
# First entry: Magnetometer z axis to cage x (reversed) [1,1]
|
|
# axis_mapping = [[corresponding axis (0=x,1=y,2=z), direction (+-1) ...]
|
|
# axis_mapping = [[2, -1], [0, 1], [1, -1]] # Z_MGM = -X_HH, X_MGM = +Y_HH, Y_MGM = -Z_HH,
|
|
axis_mapping = [[0, -1], # X_MGM = -X_HH
|
|
[2, -1], # Y_MGM = -Z_HH
|
|
[1, -1]] # Z_MGM = -Y_HH,
|
|
|
|
# Helmholtz control software tcp port
|
|
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
s.connect(("localhost", 6677))
|
|
s.sendall("declare_api_version 2\n".encode())
|
|
|
|
# FGM3D software virtual serial port
|
|
ser = serial.Serial("COM11")
|
|
|
|
# check whether day time saving is current or not
|
|
if time.localtime().tm_isdst:
|
|
dst = 2
|
|
else:
|
|
dst = 1
|
|
|
|
line = b""
|
|
ready = False
|
|
while True:
|
|
line += ser.read()
|
|
if line[-2:] == b"\r\n":
|
|
new_line = line[:-2].decode('ascii')
|
|
|
|
delta = datetime.now().timestamp()*1000 - int(new_line.split(';')[0])+(dst*60*60*1000)
|
|
if delta < 500 and not ready:
|
|
ready = True
|
|
print("FGM3D adapter script ready!")
|
|
elif not ready:
|
|
print("FGM3D adapter script not ready!")
|
|
if ready:
|
|
# Data is not valid otherwise
|
|
# Only use the x y and z values
|
|
tokens = [float(v.replace(',', '.')) for v in new_line.split(";")[1:-1]]
|
|
axis_fields = [tokens[m[0]]*m[1] for m in axis_mapping]
|
|
axis_fields = ["{:.6e}".format(v) for v in axis_fields]
|
|
s.sendall(("magnetometer_field {} {} {}\n".format(*axis_fields)).encode())
|
|
line = b""
|