问题描述:
按照官方教程,把Ubuntu的ROS文件夹拷贝到window,在matlab运行命令rosgenmsg('path')后可实现matlab与Ubuntu中的ROS通讯。然而一些小伙伴此命令报错This may take several minutes...错误使用 ros.internal.ROSProjectBuilder/buildPackage Error building package: build log.
出错 rosgenmsg (第 592 行)
buildPackage(builder, [], 'install', catkinMakeArgs); %other messages might need to be present in the same directory
此时点开 “build log” 搜索 error字眼,发现出错的地方有UnicodeDecodeError: 'utf-8' codec can't decode byte 0xbd in position 4319。 或者一些'UnicodeDecodeError'> :‘gbk字眼(看第二段)
一、UnicodeDecodeError: 'utf-8'字眼
这是字符编码问题,在catkin_make命令中默认支持的字符仅是英文,而window10或11却支持很多种,例如我当时用qq邮箱注册的window账户,那么路径就有C:/Users/123456789@qq.com/AppData...,而catkin必须要英文开头,且@这类符号也不行,并且也不能用空格(改下划线),例如“name xyz”需要改成“name_xyz”。
注意:我试过Ubuntu20.04 + win10+matlab 2022b或win11+matlab2023a,其它环境不确定。
可惜的是window即使改用户名也不会改路径,因为早期安装或注册的程序已经默认了原来的路径,改用户名只是window重新映射了一个显示不同的名字,并不能解决。
于是只能新建一个管理员用户,从新建的用户打开matlab,记得刷新下路径(一般来说matlab2023会自动在新用户新建一个用户配置,如果没有就手动刷新)。
然后就可以成功编成matla格式。
>> rosgenmsg('robotaxi_msgs_test')
Identifying message files in folder 'D:/ros_getmesage/robotaxi_msgs_test'..Done.
Validating message files in folder 'D:/ros_getmesage/robotaxi_msgs_test'..Done.
[1/1] Generating MATLAB interfaces for custom message packages... Done.
Running catkin build in folder 'D:/ros_getmesage/robotaxi_msgs_test/matlab_msg_gen_ros1/win64'.
Build in progress. This may take several minutes...
Build succeeded.build log
To use the custom messages, follow these steps:
1. Add the custom message folder to the MATLAB path by executing:
addpath('D:\ros_getmesage\robotaxi_msgs_test\matlab_msg_gen_ros1\win64\install\m')
savepath
2. Refresh all message class definitions, which requires clearing the workspace, by executing:
clear classes
rehash toolboxcache
3. Verify that you can use the custom messages.
Enter "rosmsg list" and ensure that the output contains the generated
custom message types.
值得一提,做到这一步其实就可以放到原来含中文字符的用户名下,不过要手动修改路径。然后就可以删掉新建的用户。
二、'UnicodeDecodeError'> :‘gbk字眼
这是由于.msg文件中注释有中文或莫名的字符或换行符导致
例如map.msg
Header header
uint32 frame
bool is_location #这个变量表示xxx
你会发现把 注释#这些删掉就可以正常通过,而不删rosgenmsg第592行报错'gbk'。如果注释多的话,可以搜下怎么把代码里的注释批量去掉,这里不赘述。
另外,如果你熟悉python,那解决办法还有一个:
log里说的是matlab调用Python转化rosmessage文件,但是写Python的人没有考虑把中文字符解码写进去,自行修改py文件即可。
看报错的文件在哪,例如我的是C:\Program Files\MATLAB\R2023a\sys\ros1\win64\ros1\lib\site-packages\genmsg 里的 msg_loader.py文件285行
原来是
with open(file_path, 'r') as f:
text = f.read()
try:
增加 ‘utf-8’解码,即
with open(file_path, 'r', encoding = 'utf-8') as f:
text = f.read()
try:
保存后在matlab再次运行命令即可。但实践过程通常是一个py把“带中文的字符串”传递给另一个.py文件继续处理,结果然后也会报错。这家导致你需要改多个py文件,操作也不简便。
--------------------------------------------------------------
总得来说matlab的ros模块很多调用Python处理,但默认的py文件没有写支持中文或字符路径的内容。要么你把路径和文件去掉这些字符,要么你手动在py文件加上'utf-8'的解码。