Main Content

MATLAB에서 ROS 사용자 지정 메시지 생성하기

사용자 지정 메시지를 사용하여 현재 ROS에서 지원되는 메시지 유형 세트를 확장할 수 있습니다. 사용자 지정 메시지는 사용자가 정의하는 메시지입니다. 현재 지원되는 메시지 유형을 보내고 받고 있다면 사용자 지정 메시지를 사용할 필요가 없습니다. 지원되는 메시지 유형의 목록을 보려면 MATLAB® 명령 창에 rosmsg list를 입력합니다. 지원되는 ROS 메시지에 대한 자세한 내용은 Work with Basic ROS Messages 항목을 참조하십시오.

ROS 사용자 지정 메시지를 처음 사용한다면 ROS Toolbox 시스템 요구 사항 항목을 참조하십시오.

ROS 사용자 지정 메시지는 이름이 msg인 폴더가 포함된 ROS 패키지 폴더에 지정됩니다. msg 폴더에는 모든 사용자 지정 메시지 유형 정의가 포함됩니다. 예를 들어 rosCustomMessages 폴더의 simple_msgs 패키지는 다음과 같은 폴더와 파일 구조를 갖습니다.

이 패키지는 사용자 지정 메시지 유형 Num.msg를 포함합니다. MATLAB은 이러한 파일을 사용하여 패키지에 포함된 사용자 지정 메시지를 사용하기 위해 필요한 파일을 생성합니다.

이 예제에서는 MATLAB에서 ROS 사용자 지정 메시지를 만들고 공유 가능한 ZIP 아카이브로 압축합니다. 필수적인 msg 파일이 포함되어 있는 ROS 패키지가 있어야 합니다.

사용자 지정 메시지 패키지 폴더를 준비한 다음에는 부모 폴더의 경로를 지정하고 지정된 경로로 rosgenmsg를 호출합니다.

새 MATLAB 세션을 열고 로컬 폴더에 사용자 지정 메시지 패키지 폴더를 만듭니다. Windows 시스템에서 사용자 지정 메시지를 생성하는 경우, 짧은 폴더 경로를 선택하면 폴더 경로의 문자 수 제한을 피할 수 있습니다. 예를 들면 다음과 같습니다.

genDir = fullfile('C:/test/rosCustomMessages')

genDir = fullfile(pwd,'rosCustomMessages');
packagePath = fullfile(genDir,'simple_msgs');
mkdir(packagePath)

사용자 지정 메시지 패키지 폴더 안에 이름이 msg인 폴더를 만듭니다.

mkdir(packagePath,'msg')

msg 폴더 안에 이름이 .msg인 파일을 만듭니다.

messageDefinition = {'int64 num'};

fileID = fopen(fullfile(packagePath,'msg', ...
               'Num.msg'),'w');
fprintf(fileID,'%s\n',messageDefinition{:});
fclose(fileID);

사용자 지정 메시지 패키지 폴더 안에 이름이 srv인 폴더를 만듭니다.

mkdir(packagePath,'srv')

srv 폴더 안에 이름이 .srv인 파일을 만듭니다.

serviceDefinition = {'int64 a'
                     'int64 b'
                     '---'
                     'int64 sum'};
 
fileID = fopen(fullfile(packagePath,'srv', ...
               'AddTwoInts.srv'),'w');
fprintf(fileID,'%s\n',serviceDefinition{:});
fclose(fileID);

사용자 지정 메시지 패키지 폴더 안에 이름이 action인 폴더를 만듭니다.

mkdir(packagePath,'action')

action 폴더 안에 이름이 .action인 파일을 만듭니다.

actionDefinition = {'int64 goal'
                    '---'
                    'int64 result'
                    '---'
                    'int64 feedback'};
 
fileID = fopen(fullfile(packagePath,'action', ...
               'Test.action'),'w');
fprintf(fileID,'%s\n',actionDefinition{:});
fclose(fileID);

.msg, .srv, .action 파일의 ROS 정의에서 사용자 지정 메시지를 생성합니다. 생성된 사용자 지정 메시지의 공유 가능한 ZIP 아카이브를 CreateShareableFile 이름-값 인수를 사용하여 만듭니다.

이 ZIP 아카이브를 사용해 다른 시스템에 사용자 지정 메시지를 등록하는 방법에 대한 자세한 내용은 rosRegisterMessages 항목을 참조하십시오.

rosgenmsg(genDir,CreateShareableFile=true)
Identifying message files in folder 'C:/test/rosCustomMessages'.Done.
Creating a Python virtual environment.Done.
Adding required Python packages to virtual environment.Done.
Copying include folders.Done.
Copying libraries.Done.
Validating message files in folder 'C:/test/rosCustomMessages'.Done.
[1/1] Generating MATLAB interfaces for custom message packages... Done.
Running catkin build in folder 'C:/test/rosCustomMessages/matlab_msg_gen_ros1/win64'.
Build in progress. This may take several minutes...
Build succeeded.build log
Generating zip file in the folder 'C:/test/rosCustomMessages'.Done.
 
To use the custom messages, follow these steps:
 
1. Add the custom message folder to the MATLAB path by executing:
 
addpath('C:\test\rosCustomMessages\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.
 

rosmsg list를 입력하여 새 사용자 지정 메시지가 생성되었는지 확인합니다.