当前位置:   article > 正文

Simulink使用ROS2工具箱相关知识_simulink ros

simulink ros

本文总结了在MBD阶段,使用simulink自带ROS2工具箱需注意的地方。

使用自定义ROS2消息类型

由于日常开发工作需要使用大量的结构体定义DDS消息类型,Matlab / Simulink的ros2 toolbox只携带原生标准数据类型,如std_msgs, sensor_msgs。这种情况下需要开发者在Matlab中,编译生成自定义的消息类型。由于windows对ros2的支持不够友好,所以我们使用Linux版本的Matlab / Simulink。软件包括如下:

  • Ubuntu 22.04
  • Matlab / Simulink R2022b
  • ros2 humble
  • Python3.9,由于R2022b不支持高版本的python进行ros idlc编译,所以需手动安装Python3.9(且不能使用默认的高版本,Python3.9.12被证明有效),参考:在 Ubuntu 22.04 上安装 Python 3.9(多版本适用)

这里可以先参考官方指导文档: 生成自定义消息类型

需要注意的是,ros2genmsg()函数只支持’.msg’格式的文件,‘.idl’文件与’.msg’文件的对应定义关系可参考:idl & msg对应表

如果需要删除已经生成的消息类型,可以在消息包定义的地方手动删除matlab genmsg相关的文件夹,然后在matlab命令行窗口执行:

reg = ros.internal.CustomMessageRegistry.getInstance('ros2',true); reg.refresh(true); 
  • 1

配置ROS2 DDS middleware & DOMAIN_ID

该工具箱自带fastdds & cyclonedds的中间件,也可根据需求编译自定义中间件。可以在matlab界面中的Preference里更改所需ros toolbox的配置项。

Domain id是在simulink里的suber / puber里更改

设置发布频率

环境设置里的步长实际含义代表为程序仿真时的步长,所以在环境里设置solver无法按照用户真实所需频率发布。可以引入simulation pace模块,设置与真实物理时间的比例调整程序运行周期。

Troubleshooting

自定义消息在使用时,容易遇到std::Runtime error的运行时报错。
在这里插入图片描述
官方论坛里有mathworks的工作人员定位是由一个链接库版本不匹配导致,根据其建议:
Please try the following workaround:

  1. Please rename libstdc++.so.6 file located in “matlabroot/sys/os/glnxa64” so that MATLAB cannot find it and is forced to use the system’s version of the library. Renaming it to libstdc++.so.6.old should suffice. (where “matlabroot” is root installation directory of MATLAB).
  2. Please delete the previously generated folders during ros2genmsg() execution to remove the existing ros2 custom messages.
  3. Now please regenerate the custom messages using ros2genmsg() in R2022b MATLAB and check if you still face the issues.

但是根据我的实际经验,sub block依然存在该问题,pub block可以正常运行。比较简单粗暴的办法就是sub端使用ROS2工具箱自带的标准数据类型,手写一个ros2节点将自定义消息数据打散重组为标准数据类型发给simulink。如果大家有更好的解决办法,欢迎讨论

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/一键难忘520/article/detail/884765
推荐阅读
相关标签
  

闽ICP备14008679号