1.matlab读取ros下标准话题消息

1)准备工作 录制一个rosbag包
我的rosbag包格式如下

很简单 只有一个topic
类型为:nav_msgs/Odometry
名称为: /uwb_position
2)matlab代码读取 我直接给出matlb代码 ,详细解读看注释

bag_uwb1 = rosbag("C:\Users\Administrator.DESKTOP-0SCKJAS\Desktop\2022-10-15\coop_node3\ucar_uwb.bag");%bag_uwb是读出来的包名
%bag_uwb1 = rosbag("ucar_uwb.bag"); %放在同一路径下也可以这么写
topic_uwb1 = select(bag_uwb1,'Topic','/uwb_position');%topic_uwb是包内你要的话题的格式
data_uwb1 = readMessages(topic_uwb1);%data_uwb1就是这个话题的数据了,在matlab里可以直接打开,形式如下图所示
len_uwb1 = 11597;
ucar_uwb = zeros(len_uwb1,2);%把data_uwb1拆成数组形式,便于后续处理
for i =1:len_uwb1
    ucar_uwb(i,1) = data_uwb1{i, 1}.Pose.Pose.Position.X;  
    ucar_uwb(i,2) = data_uwb1{i, 1}.Pose.Pose.Position.Y;
end

2.matlab读取ros下自定义话题消息

自定义的话题消息,需要配置后matlab才可以读取话题消息
Mathworks给出的官方教程
1)安装ROS Toolbox interface for ROS Custom Messages

直接在APP->获取更多APP搜索即可 需要账号登陆正版

2)生成自定义message的功能包,一般自定义的msg都在功能包内有一个单独的msg文件夹 ,在Ubuntu系统的工作空间内进行catkin_make 或者 catkin build (反正就是你平时编译这个功能包的方法)确定没有错误

这个是我在ros下自定义好的msg

3)把整个功能包复制到windows下 (我用的是win10的matlab+Ubuntu的ros自定义消息)
直接在命令行窗口运行以下两行

folderpath = 'C:\Users\Administrator.DESKTOP-0SCKJAS\Desktop\2022-10-15\2022-11-01'
rosgenmsg(folderpath)

其中路径要换成你的功能包所在的路径
运行结束,窗口会提示三个要求

  1. Edit javaclasspath.txt, add the following file locations as new lines, and save the file:
    C:\Users\Administrator.DESKTOP-0SCKJAS\Desktop\2022-10-15\2022-11-01/matlab_gen/jar/multiagent_srv-0.0.0.jar
    Add the custom message folder to the MATLAB path by executing:
  2. addpath(‘C:\Users\Administrator.DESKTOP-0SCKJAS\Desktop\2022-10-15\2022-11-01/matlab_gen/msggen’)
    savepath
  3. Restart MATLAB and verify that you can use the custom messages. Type “rosmsg list” and ensure that the output contains the generated
    custom message types.

其中的路径每个人会不一样
①直接点击蓝字javaclasspath.txt,如果第一次的话会提醒你新建,确认新建即可,然后把提示的文件行加入即可
②添加路径,这样在其他路径下也可以用这个topic ,记得savepath
③重启matlab
4)test
可以直接在命令行终端输入 rosmsg show nlink_parser/LinktrackNodeframe3(这是我自己自定义的topic)

后续的读取和标准消息格式是一样的
和之前的ros消息是对应的

bag_uwb1 = rosbag("bag\2022-11-01-16-58-23.bag");%读包
topic_uwb1 = select(bag_uwb1,'Topic','/uwb_A1');%读话题
data_uwb1 = readMessages(topic_uwb1);%读数据
dis1 = zeros(length(data_uwb1),1);%数据处理称为简单数据形式
for i = 1:length(data_uwb1)
    if (~isempty(data_uwb1{i, 1}.Nodes) )
        dis1(i) = data_uwb1{i, 1}.Nodes.Dis;  
    end
end

参考文章

https://blog.csdn.net/zhu_hai_csdn/article/details/104381547