Ubuntu ROS 和Windows Matlab联合通信

Ubuntu ROS和Windows Matlab联合通信

实验室想用Matlab控制UR16e机器人,于是使用MATLAB与ROS进行联合通信。

连接
  1. ubuntu查找本机ip地址
ifconfig
  1. windows查找本机ip地址
ipconfig
  1. ubuntu配置文件修改 .bashrc
export ROS_IP=172............(Ubuntu ip地址)
export ROS_MASTER_URI=http://172........(Ubuntu ip地址):11311

然后运行roscore

  1. windows matlab初始化配置
setenv('ROS_MASTER_URI','http://172.......(Ubuntu ip地址):11311')
setenv('ROS_IP','172........(Windows ip地址)')
rosinit
测试

ubuntu打开小乌龟的测试示例

roscore #新开终端启动
rosrun turtlesim turtlesim_node #打开一个实例

在matlab上查看,用rostopic list 命令查看话题名称:

rostopic list

matlab控制ROS的测试代码

cmdpub = rospublisher('/turtle1/cmd_vel',rostype.geometry_msgs_Twist)
pause(3) % Wait to ensure publisher is setup
cmdmsg = rosmessage(cmdpub);
cmdmsg.Linear.X = 10;
cmdmsg.Angular.Z = 15; 
send(cmdpub,cmdmsg)
chatterpub = rospublisher('/chatter',rostype.std_msgs_String)
pause(3) % Wait to ensure publisher is setup
chattermsg = rosmessage(chatterpub);
chattermsg.Data = 'hello world'
send(chatterpub,chattermsg) 
pause(5)

Ubuntu单系统matlab与ROS通信

首先需要下载matlab得到linux版本

启动系统上的ROS

ifconfig #enp3s0 --看ip
sudo ~/.bashrc #修改文件
export ROS_IP=192******** #添加本机ip
roscore #新开终端启动
rosrun turtlesim turtlesim_node #打开一个实例

在MATLAB上启动

setenv('ROS_MASTER_URI','http://192.168.1.50:11311')  %这是本机的ip地址
setenv('ROS_IP','192.168.1.50')
rosinit
rostopic list
rostopic echo /turtle1/pose

在MATLAB上进行测试

cmdpub = rospublisher('/turtle1/cmd_vel',rostype.geometry_msgs_Twist)
pause(3) % Wait to ensure publisher is setup
cmdmsg = rosmessage(cmdpub);
cmdmsg.Linear.X = 10;
cmdmsg.Angular.Z = 15; 
send(cmdpub,cmdmsg)
chatterpub = rospublisher('/chatter',rostype.std_msgs_String)
pause(3) % Wait to ensure publisher is setup
chattermsg = rosmessage(chatterpub);
chattermsg.Data = 'hello world'
send(chatterpub,chattermsg) 


本机的地址 192.168.1.105

Ubuntu地址 http://192.168.1.50:11311 ip:192.168.1.50

这是在使用UR16e进行关节角度复现的代码,主要是执行各时间序列的关节位移

%node = ros.Node('/jointControll');
jntTrajPub=rospublisher("/scaled_pos_joint_traj_controller/follow_joint_trajectory/goal","control_msgs/FollowJointTrajectoryActionGoal");
pause(3);
controlCmdMsg= rosmessage(jntTrajPub);
controlCmdMsg.Goal.Trajectory.JointNames={'shoulder_pan_joint'; 'shoulder_lift_joint'; 'elbow_joint'
                                                 'wrist_1_joint' ;'wrist_2_joint'; 'wrist_3_joint'};
file_t=fopen('afterTrain2.txt','r');
%舍去第一行的信息
jntMatrix=textscan(file_t,'%f,%f,%f,%f,%f,%f,%f','HeaderLines',1);
%将元胞数组转化为矩阵
jntMatrix=cell2mat(jntMatrix);
[rows,cols]=size(jntMatrix)
point=rosmessage("trajectory_msgs/JointTrajectoryPoint");
point.Positions=jntMatrix(1,2:cols);      %将第一组关节存入关节数组
duration = rosduration(8);   %得到运行时间,转化为ROS格式
point.TimeFromStart=duration;  
%controlCmdMsg.Goal.Trajectory.Header.Stamp=rostime('now');
controlCmdMsg.Goal.Trajectory.Points(1)=point;
send(jntTrajPub,controlCmdMsg);
pause(8);
for row=2:rows
    jntMatrix(row,2:cols);
    point.Positions=jntMatrix(row,2:cols);      %存入关节数组
    duration = rosduration(jntMatrix(row,1)-jntMatrix(row-1,1));   %得到运行时间,转化为ROS格式
    point.TimeFromStart=duration;   
%    controlCmdMsg.Goal.Trajectory.Header.Stamp=rostime('now');
    controlCmdMsg.Goal.Trajectory.Points(1)=point;
    send(jntTrajPub,controlCmdMsg);
    pause(jntMatrix(row,1)-jntMatrix(row-1,1));
end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值