142 lines
4.6 KiB
Python
142 lines
4.6 KiB
Python
import socket
|
|
import json
|
|
from pprint import pprint
|
|
|
|
from func import *
|
|
|
|
s = socket.socket()
|
|
host = MODBUS_SERVER_HOST
|
|
port = 9760
|
|
|
|
s.connect((host, port))
|
|
# print(s)
|
|
def send_data(data):
|
|
response = None
|
|
if data["reqType"] == 'AddRCC':
|
|
# return
|
|
pass
|
|
s.send(str.encode(json.dumps(data)))
|
|
response_data = s.recv(1024)
|
|
response = json.loads(response_data)
|
|
|
|
if data["reqType"] == 'query':
|
|
# pprint({"request":data["queryAddr"], "response":response["queryData"]})
|
|
return response["queryData"]
|
|
elif data["reqType"] == 'command':
|
|
# pprint({"request":data["cmdData"], "response":response["cmdReply"]})
|
|
return response["cmdReply"]
|
|
elif data["reqType"] == 'AddRCC' and "cmdReply" in response.keys():
|
|
return response["cmdReply"]
|
|
else:
|
|
pprint(response)
|
|
|
|
def make_addrcc_data(q, emptyList=0):
|
|
# print(q)
|
|
return {"dsID": "www.hc-system.com.HCRemoteCommand","reqType":"AddRCC","emptyList":str(emptyList),"packID":"0","instructions":q}
|
|
def make_command_data(q):
|
|
return {"dsID": "www.hc-system.com.RemoteMonitor","reqType":"command","packID":"0","cmdData":q}
|
|
def make_query_data(q):
|
|
return {"dsID": "www.hc-system.com.RemoteMonitor","reqType":"query","packID":"0","queryAddr":q}
|
|
|
|
axis_coord_raw = send_data(make_query_data(['axis-0','axis-1','axis-2','axis-3','axis-4','axis-5']))
|
|
axis_coord = []
|
|
for i in axis_coord_raw:
|
|
axis_coord.append(float(i))
|
|
|
|
world_coord_raw = send_data(make_query_data(['world-0','world-1','world-2','world-3','world-4','world-5']))
|
|
world_coord = []
|
|
for i in world_coord_raw:
|
|
world_coord.append(float(i))
|
|
x,y,z,u,v,w = world_coord
|
|
|
|
print('axis', axis_coord)
|
|
print('world', world_coord)
|
|
print('remote command count', send_data(make_query_data(['RemoteCmdLen'])))
|
|
|
|
line_speed = 100.0
|
|
line_smooth = 9
|
|
line_tool = 1
|
|
|
|
global_speed = 100
|
|
physical_speed = 50
|
|
# laser_id = 15
|
|
laser_id = 14
|
|
# filename = 'half-sphere-no-angle'
|
|
filename = 'half-sphere'
|
|
|
|
pass_size = 4
|
|
|
|
def make_world_step(type, p):
|
|
step = {
|
|
"oneshot": "0",
|
|
"delay":"0.0",
|
|
"speed":str(line_speed),"smooth":str(line_smooth),
|
|
"coord":"0","tool":str(line_tool),
|
|
"ckStatus":"0x3F",
|
|
}
|
|
if type == 'line':
|
|
m0,m1,m2,m3,m4,m5=p
|
|
step.update({
|
|
"action": "10",
|
|
"m0": round(x + m0,3), "m1": round(y + m1,3), "m2": round(z + m2,3),
|
|
"m3": round(u + m3,3), "m4": round(v + m4,3), "m5": round(w + m5,3),
|
|
"m6": 0, "m7": 0,
|
|
})
|
|
elif type == 'curve':
|
|
m0,m1,m2,m3,m4,m5, m0_p, m1_p,m2_p,m3_p,m4_p,m5_p=p
|
|
step.update({
|
|
"action": "17",
|
|
"m0": round(x + m0,3), "m1": round(y + m1,3), "m2": round(z + m2,3),
|
|
"m3": round(u + m3,3), "m4": round(v + m4,3), "m5": round(w + m5,3),
|
|
"m6": 0, "m7": 0,
|
|
"m0_p": round(x + m0_p,3), "m1_p": round(y + m1_p,3), "m2_p": round(z + m2_p,3),
|
|
"m3_p": round(u + m3_p,3), "m4_p": round(v + m4_p,3), "m5_p": round(w + m3_p,3),
|
|
"m6_p": 0, "m7_p": 0,
|
|
})
|
|
|
|
for s in step:
|
|
step[s] = str(step[s])
|
|
return step
|
|
|
|
result = []
|
|
with open(f'data/{filename}.nc.result', 'r') as fp:
|
|
for line in fp:
|
|
data = line.strip().split(' ')
|
|
prep = {}
|
|
for item in data:
|
|
prep[item[:1]] = float(item[1:])
|
|
result.append(
|
|
make_world_step(
|
|
'line',
|
|
(
|
|
prep.get('X', 0), prep.get('Y', 0), prep.get('Z', 0),
|
|
prep.get('U', 0), prep.get('V', 0), prep.get('W', 0)
|
|
)
|
|
)
|
|
)
|
|
|
|
# Изменили глобальную скорость на global_speed%
|
|
send_data(make_command_data(['modifyGSPD', str(global_speed * 10)]))
|
|
send_data(make_addrcc_data([
|
|
# Включили физическую скорость
|
|
{"oneshot":"0", "action":"51","isUse":"1","speed":str(physical_speed*1000)},
|
|
# Поставили Y026 (laser id + 11) в TRUE -- ЛАЗЕР!!!!
|
|
{"oneshot":"0", "action":"200","type":"0","io_status":"1", "point":str(laser_id)},
|
|
], 1))
|
|
|
|
res_list = len(result) + 1
|
|
if res_list > 300:
|
|
res_list = 300
|
|
|
|
for i in range(pass_size, res_list, pass_size):
|
|
print(f"Отправили на устройство команды {i} ... {i + 1}")
|
|
send_data(make_addrcc_data(result[i:i+1]))
|
|
|
|
send_data(make_addrcc_data([
|
|
{"oneshot":"0", "action":"51","isUse":"0"},
|
|
{"oneshot":"0", "action":"200","type":"0","io_status":"0", "point":str(laser_id)},
|
|
]))
|
|
|
|
print('remote command count', send_data(make_query_data(['RemoteCmdLen'])))
|
|
send_data(make_command_data(['actionSingleCycle']))
|
|
# send_data(make_command_data(['actionStop'])) |