MATLAB与Robotstudio联合仿真 stocket通信

tmd网上有关matlab与robotstudio stocket通信的内容太少了,索性自己写个文章,免得后来人浪费时间。

本文代码参考:http://t.csdn.cn/02UkD

感谢大佬的代码。stocket通信的前置条件也可以参考上面链接。

matlab与robotstudio通信分为两部分代码,matlab作为服务器端,发送位置数据给作为服务器端的robotstudio。首先介绍robotstudio端的代码:

这是一个根据matlab发送来的数据,以p10点为基准移动机器人的代码。matlab发送的是p10点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;

!定义一个p10点

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]];

!定义工具数据

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;

!关键部分:取接收字段的第1位赋值给‘pass’,第2位向后取8个数赋值给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);

!根据pass值是否为‘1’来判断是否执行运动

IF pass = 1 AND flag1 THEN

!移动到距离p10点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'变量用于判断发来的数据是否给轴偏移量赋值,因此发来的数据必须用'1'开头。

string2{2}:=StrPart(string1,2,8);表示取字段的2位之后的8个数,赋值给string2{2},该变量作为轴的偏移量不应太大,否则超出机器人移动范围,故2-9位取值应为‘00000300’表示偏移量为300mm。机器人移动到距离p10点x轴方向300mm,y轴300mm,z轴15mm的位置。

接下来是matlab代码:表示发送字符'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');

进行通信时,首先运行robotstudio端代码,

出现下面提示后启动matlab端代码:

启动matlab后,打开示教器出现以下提示,表示已接受到信息。

 机器人运动到如图所示的位置。

 代码不是最终版本,一次通信只能移动一次,接下来需要连续不断得发送信息给机器人,实现连续的运动。

  • 2
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值