MATLAB 2019b中提供了较为完整的ROS功能支持,其中包括使用自定义的ROS消息类型。下面给出使用方法和一个例子。
测试环境:
- Ubuntu 18.04
- ROS Melodic
- MATLAB 2019b
使用步骤:
-
编写含有自定义ROS消息(服务)类型的程序包,并在catkin空间进行编译
$ cd ~/ROS/hai_ws/src $ git clone https://github.com/hai-zhu/multiagent_srv.git $ cd .. $ catkin build
-
打开MATLAB,指定该程序包的位置,并使用rosgenmsg命令生成自定义消息文件
>> folderpath = '/home/hai/ROS/hai_ws/src' >> rosgenmsg(folderpath)
注意:使用rosgenmsg命令安装工具箱Robotics System Toolbox Interface for ROS Custom Messages。
完成该命令后,将会在原ROS程序包所在的文件夹下(src)自动生成一个文件夹matlab_gen。同时在MATLAB的命令行窗口,会显示以下信息:
Checking subfolder "multiagent_srv" for custom messages. Building custom message files for the following packages: multiagent_srv Generating MATLAB classes for message packages in /home/hai/ROS/hai_ws/src/matlab_gen/jar. Loading file multiagent_srv-0.0.0.jar. Generating MATLAB code for multiagent_srv/CommMultiAgent message type. Generating MATLAB code for multiagent_srv/StepMultiAgentRequest message type. Generating MATLAB code for multiagent_srv/StepMultiAgentResponse message type. To use the custom messages, follow these steps: 1. Edit javaclasspath.txt, add the following file locations as new lines, and save the file: /home/hai/ROS/hai_ws/src/matlab_gen/jar/multiagent_srv-0.0.0.jar 2. Add the custom message folder to the MATLAB path by executing: addpath('/home/hai/ROS/hai_ws/src/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文件进行修改,并添加相关文件夹到MATLAB的路径中。重启MATLAB,使用命令rosmsg list就可以看到自定义的消息类型了。
参考资料: https://nl.mathworks.com/help/ros/ug/ros-custom-message-support.html
https://nl.mathworks.com/help/ros/ug/create-custom-messages-from-ros-package.html