tmd網(wǎng)上有關(guān)matlab與robotstudio stocket通信的內(nèi)容太少了,索性自己寫個(gè)文章,免得后來(lái)人浪費(fèi)時(shí)間。
本文代碼參考:http://t.csdn.cn/02UkD
感謝大佬的代碼。stocket通信的前置條件也可以參考上面鏈接。
matlab與robotstudio通信分為兩部分代碼,matlab作為服務(wù)器端,發(fā)送位置數(shù)據(jù)給作為服務(wù)器端的robotstudio。首先介紹robotstudio端的代碼:
這是一個(gè)根據(jù)matlab發(fā)送來(lái)的數(shù)據(jù),以p10點(diǎn)為基準(zhǔn)移動(dòng)機(jī)器人的代碼。matlab發(fā)送的是p10點(diǎn)xyz軸方向上的偏移量。
MODULE moudle1
!define variables定義變量
VAR socketdev server;
VAR socketdev client;
VAR string message;
VAR string string1:="";
VAR string string2{3}:=["","",""];
VAR num pass:=0;
VAR num X:=0;
VAR num Y:=0;
VAR num Rz:=0;
VAR bool flag1:=FALSE;
VAR rawbytes data;
!定義一個(gè)p10點(diǎn)
CONST robtarget p10:=[[0,0,23.72],[0.00796547,0.908649,0.417482,0.0017992],[0,-1,1,0],[9E+09,9E+09,9E+09,9E+09,9E+09,9E+09]];
!定義工具數(shù)據(jù)
TASK PERS tooldata toolxi:=[TRUE,[[0,0,160],[1,0,0,0]],[1,[1,0,1],[1,0,0,0],0,0,0]];
PROC main()
a:
!create communication建立通信
SocketCreate server;
SocketBind server,"127.0.0.1",55000;
SocketListen server;
SocketAccept server,client;
!send a meassage to the client
SocketSend client,\Str:="Hello client";
!receive a message from the client
SocketReceive client,\Str:=string1;
UnpackRawBytes data ,1,message,\ASCII:=16;
!close cmmunication
SocketClose server;
TPErase;
TPWrite string1;
WaitTime 4;
!關(guān)鍵部分:取接收字段的第1位賦值給‘pass’,第2位向后取8個(gè)數(shù)賦值給x,y
string2{1}:=StrPart(string1,1,1);
string2{2}:=StrPart(string1,2,8);
string2{3}:=StrPart(string1,2,8);
flag1:=StrToVal(string2{1},pass);
flag1:=StrToVal(string2{2},X);
flag1:=StrToVal(string2{3},Y);
!根據(jù)pass值是否為‘1’來(lái)判斷是否執(zhí)行運(yùn)動(dòng)
IF pass = 1 AND flag1 THEN
!移動(dòng)到距離p10點(diǎn)x軸方向xmm的位置,y軸方向ymm的位置,z軸方向15mm的位置
MoveL Offs(p10,X,Y,15), v1000, fine, toolxi;
MoveL Offs(p10,X,Y,0), v1000, fine, toolxi;
WaitTime 0.5;
MoveL Offs(p10,X,Y,15), v1000, z50, toolxi;
ELSE
TPWrite "failed to get the data";
waittime 2;
GOTO a;
ENDIF
SocketClose server;
SocketClose client;
ENDPROC
ENDMODULE
程序中'pass'變量用于判斷發(fā)來(lái)的數(shù)據(jù)是否給軸偏移量賦值,因此發(fā)來(lái)的數(shù)據(jù)必須用'1'開頭。
string2{2}:=StrPart(string1,2,8);表示取字段的2位之后的8個(gè)數(shù),賦值給string2{2},該變量作為軸的偏移量不應(yīng)太大,否則超出機(jī)器人移動(dòng)范圍,故2-9位取值應(yīng)為‘00000300’表示偏移量為300mm。機(jī)器人移動(dòng)到距離p10點(diǎn)x軸方向300mm,y軸300mm,z軸15mm的位置。
接下來(lái)是matlab代碼:表示發(fā)送字符'100000300'給robotstudio。
%%socket communication with robotstudio
tc=tcpip('127.0.0.1',55000);%若使用matlab2021a以上的版本,將tcpip改為tcpclient
%open the communication
fopen(tc);
%recive a message from the robotstudio
message=fread(tc);
%send a message to the robotstudio
fwrite(tc,'100000300');
進(jìn)行通信時(shí),首先運(yùn)行robotstudio端代碼,
出現(xiàn)下面提示后啟動(dòng)matlab端代碼:
啟動(dòng)matlab后,打開示教器出現(xiàn)以下提示,表示已接受到信息。
?機(jī)器人運(yùn)動(dòng)到如圖所示的位置。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-424323.html
?代碼不是最終版本,一次通信只能移動(dòng)一次,接下來(lái)需要連續(xù)不斷得發(fā)送信息給機(jī)器人,實(shí)現(xiàn)連續(xù)的運(yùn)動(dòng)。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-424323.html
到了這里,關(guān)于MATLAB與Robotstudio聯(lián)合仿真 stocket通信的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!