越疆机器人TCP通讯之12点标定脚本程序
-- Version: Lua 5.4.4
-- 此线程为主线程,可调用任何指令
--必须先示教P1点位置,运行到P5点后手动暂停将当前位置保存到P2
--P2作为角度旋转标定基准位
--本程序用于越疆Robot和海康Vision master 12点标定
VelL(20) -- 设置线性速度
AccL(20) -- 设置线性加速度
SpeedFactor(20) -- 设置全局速度
MovL(P1)---初始化位置
x=50---X赋值
y=50---Y赋值
r=15---角度赋值
---------字符串分割----------
function string.split(str,delimiter)
if str==nil or str=='' or delimiter==nil then
return nil
end
local result = {}
for match in (str..delimiter):gmatch("(.-)"..delimiter) do
table.insert(result,match)
end
return result
end
------------------通讯连接---------------------
function Connect(isServer,ip,prot)--建立TCP通信
::Cab_PC:: --跳转标签
err, socket = TCPCreate(isServer,ip,prot)-- isServer:表示客户端或服务器(false: 表示创建客户端; true: 表示创建服务) i
print("err=",err)
Wait(100)
if err ~= 0 then
print("创建链接失败,重新创建链接...\r\n")
Wait(100)
goto Cab_PC --创建服务器失败跳转到create_serverCab标签继续创建
end
err = TCPStart(socket,0)--socket:socket对象 0:如果为0,则一直等待连接。如果不为0,则超过设定 的时间后退出连接。
if err ~= 0 then
print("握手失败,重新发起...\r\n")
TCPDestroy(socket)--关闭TCP功能
Wait(100)
goto Cab_PC
end
print("握手成功...\r\n")
end
-----------------数据交互-----------------
function ReceiveData() --数据接受
local
local DataArrayS={}
err,Recbuf=TCPRead(socket,0,"string")
if err==0 and Recbuf~="" then
DataArrayS=string.split(Recbuf,",")
else
Connect(false,"192.168.1.90",8604)
end
return DataArrayS
end
----------数据发送-----------
function SendData(socket,Str)
if Str~="" then
err=TCPWrite(socket, Str)
print("err=",err)
if err==0 then
print("Send Data OK")
else
Connect(false,"192.168.1.90",8604) --接受失败,再次调用重连机制
print("Send Data NG")
end
end
end
---------------------------main-----------------------------
---P1
Connect(false,"192.168.1.90",8604) --建立TCP通信
print("hello world")
MovL(P1,{user=0,tool=1})
Wait(500)
currentPose_1=GetPose(0,1)
Wait(300)
print("currentPose_1=",currentPose_1)
Test1="A"..","..tonumber(currentPose_1.pose[1])..","..tonumber(currentPose_1.pose[2])..","..tonumber(currentPose_1.pose[6])
SendData(socket,Test1)
Wait(1000)
---P2
RelMovLUser({0,y,0,0,0,0})
Wait(500)
currentPose_2=GetPose(0,1)
Wait(300)
print("currentPose_2=",currentPose_2)
Test2="A"..","..tonumber(currentPose_2.pose[1])..","..tonumber(currentPose_2.pose[2])..","..tonumber(currentPose_2.pose[6])
SendData(socket,Test2)
Wait(1000)
---P3
RelMovLUser({0,y,0,0,0,0})
Wait(500)
currentPose_3=GetPose(0,1)
Wait(300)
print("currentPose_3=",currentPose_3)
Test3="A"..","..tonumber(currentPose_3.pose[1])..","..tonumber(currentPose_3.pose[2])..","..tonumber(currentPose_3.pose[6])
SendData(socket,Test3)
Wait(1000)
-- Version: Lua 5.4.4
-- 此线程为主线程,可调用任何指令
--必须先示教P1点位置,运行到P5点后手动暂停将当前位置保存到P2
--P2作为角度旋转标定基准位
--本程序用于越疆Robot和海康Vision master 12点标定
VelL(20) -- 设置线性速度
AccL(20) -- 设置线性加速度
SpeedFactor(20) -- 设置全局速度
MovL(P1)---初始化位置
x=50---X赋值
y=50---Y赋值
r=15---角度赋值
---------字符串分割----------
function string.split(str,delimiter)
if str==nil or str=='' or delimiter==nil then
return nil
end
local result = {}
for match in (str..delimiter):gmatch("(.-)"..delimiter) do
table.insert(result,match)
end
return result
end
------------------通讯连接---------------------
function Connect(isServer,ip,prot)--建立TCP通信
::Cab_PC:: --跳转标签
err, socket = TCPCreate(isServer,ip,prot)-- isServer:表示客户端或服务器(false: 表示创建客户端; true: 表示创建服务) i
print("err=",err)
Wait(100)
if err ~= 0 then
print("创建链接失败,重新创建链接...\r\n")
Wait(100)
goto Cab_PC --创建服务器失败跳转到create_serverCab标签继续创建
end
err = TCPStart(socket,0)--socket:socket对象 0:如果为0,则一直等待连接。如果不为0,则超过设定 的时间后退出连接。
if err ~= 0 then
print("握手失败,重新发起...\r\n")
TCPDestroy(socket)--关闭TCP功能
Wait(100)
goto Cab_PC
end
print("握手成功...\r\n")
end
-----------------数据交互-----------------
function ReceiveData() --数据接受
local
local DataArrayS={}
err,Recbuf=TCPRead(socket,0,"string")
if err==0 and Recbuf~="" then
DataArrayS=string.split(Recbuf,",")
else
Connect(false,"192.168.1.90",8604)
end
return DataArrayS
end
----------数据发送-----------
function SendData(socket,Str)
if Str~="" then
err=TCPWrite(socket, Str)
print("err=",err)
if err==0 then
print("Send Data OK")
else
Connect(false,"192.168.1.90",8604) --接受失败,再次调用重连机制
print("Send Data NG")
end
end
end
---------------------------main-----------------------------
---P1
Connect(false,"192.168.1.90",8604) --建立TCP通信
print("hello world")
MovL(P1,{user=0,tool=1})
Wait(500)
currentPose_1=GetPose(0,1)
Wait(300)
print("currentPose_1=",currentPose_1)
Test1="A"..","..tonumber(currentPose_1.pose[1])..","..tonumber(currentPose_1.pose[2])..","..tonumber(currentPose_1.pose[6])
SendData(socket,Test1)
Wait(1000)
---P2
RelMovLUser({0,y,0,0,0,0})
Wait(500)
currentPose_2=GetPose(0,1)
Wait(300)
print("currentPose_2=",currentPose_2)
Test2="A"..","..tonumber(currentPose_2.pose[1])..","..tonumber(currentPose_2.pose[2])..","..tonumber(currentPose_2.pose[6])
SendData(socket,Test2)
Wait(1000)
---P3
RelMovLUser({0,y,0,0,0,0})
Wait(500)
currentPose_3=GetPose(0,1)
Wait(300)
print("currentPose_3=",currentPose_3)
Test3="A"..","..tonumber(currentPose_3.pose[1])..","..tonumber(currentPose_3.pose[2])..","..tonumber(currentPose_3.pose[6])
SendData(socket,Test3)
Wait(1000)