Main Content

rosmessage

ROS 메시지 생성

R2019b 이후

설명

예제

msg = rosmessage(messagetype)은 메시지 유형을 가진 빈 ROS 메시지 객체를 생성합니다. messagetype string형 스칼라는 대소문자를 구분하며 부분 일치는 허용되지 않습니다. rosmsg("list")를 호출하여 주어진 목록의 메시지와 일치해야 합니다.

참고

향후 릴리스에서 ROS Toolbox는 ROS 메시지에 대해 객체 대신 메시지 구조체를 사용할 예정입니다.

지금 메시지 구조체를 사용하려면 "DataFormat" 이름-값 인수를 "struct"로 설정합니다. 자세한 내용은 ROS 메시지 구조체 항목을 참조하십시오.

예제

msg = rosmessage(pub)pub에서 퍼블리시한 토픽에 의해 결정되는 빈 메시지를 생성합니다.

msg = rosmessage(sub)sub에서 서브스크라이브한 토픽에 의해 결정되는 빈 메시지를 생성합니다.

msg = rosmessage(client)client의 서비스 유형 또는 액션 유형에 의해 결정되는 빈 메시지를 생성합니다.

msg = rosmessage(server)server의 서비스 유형 또는 액션 유형에 의해 결정되는 빈 메시지를 생성합니다.

msg = rosmessage(___,"DataFormat","struct")는 이전 구문의 인수를 사용하여 빈 메시지를 메시지 구조체로 생성합니다. 자세한 내용은 ROS 메시지 구조체 항목을 참조하십시오.

예제

모두 축소

std_msgs/String 메시지 유형으로 ROS 메시지를 구조체로 생성합니다.

strMsg = rosmessage("std_msgs/String","DataFormat","struct")
strMsg = struct with fields:
    MessageType: 'std_msgs/String'
           Data: ''

ROS master를 시작합니다.

rosinit
Launching ROS Core...
Done in 0.79122 seconds.
Initializing ROS master on http://172.30.241.81:56243.
Initializing global node /matlab_global_node_92672 with NodeURI http://dcc601040glnxa64:43053/ and MasterURI http://localhost:56243.

std_msgs/String 메시지 유형으로 /chatter 토픽에 대한 publisher를 생성합니다. "DataFormat" 이름-값 인수를 설정하여 ROS 메시지를 구조화합니다.

chatpub = rospublisher("/chatter","std_msgs/String","DataFormat","struct");

보낼 메시지를 생성합니다. 문자형 벡터를 사용하여 Data 속성을 지정합니다.

msg = rosmessage(chatpub);
msg.Data = 'test phrase';

publisher를 통해 메시지를 보냅니다.

send(chatpub,msg);

ROS 네트워크를 종료합니다.

rosshutdown
Shutting down global node /matlab_global_node_92672 with NodeURI http://dcc601040glnxa64:43053/ and MasterURI http://localhost:56243.
Shutting down ROS master on http://172.30.241.81:56243.

여러 메시지를 저장하기 위해 구조체형 배열을 생성할 수 있습니다. 이 배열은 다른 배열과 마찬가지로 인덱싱할 수 있습니다. 각 객체의 속성을 수정하거나 점 표기법을 사용하여 각 요소의 특정 속성에 액세스할 수 있습니다.

두 개의 메시지로 구성된 배열을 생성합니다. ROS 메시지에 대해 구조체를 사용하기 위해 DataFormat 이름-값 인수를 지정합니다.

blankMsg = rosmessage("std_msgs/String","DataFormat","struct")
blankMsg = struct with fields:
    MessageType: 'std_msgs/String'
           Data: ''

msgArray = [blankMsg blankMsg]
msgArray=1×2 struct array with fields:
    MessageType
    Data

배열의 개별 메시지 필드에 데이터를 할당합니다.

msgArray(1).Data = 'Some string'; 
msgArray(2).Data = 'Other string';

메시지의 모든 Data 필드를 셀형 배열로 읽습니다.

allData = {msgArray.Data}
allData = 1x2 cell
    {'Some string'}    {'Other string'}

ROS 메시지를 객체로 사용하여 배열을 사전에 할당하려면 repmat 함수 대신 arrayfun 함수 또는 cellfun 함수를 사용합니다. 이러한 함수는 핸들 클래스에 대한 객체 배열 또는 셀형 배열을 적절하게 생성합니다.

참고:향후 릴리스에서 ROS 메시지 객체는 제거될 예정입니다. ROS 메시지를 구조체로 사용하고 구조체형 배열을 활용하려면, rosmessage 함수를 호출할 때 DataFormat 이름-값 쌍을 지정하십시오.

ROS 메시지의 객체 배열을 사전할당합니다.

msgArray = arrayfun(@(~) rosmessage("std_msgs/String"),zeros(1,50));

ROS 메시지의 셀형 배열을 사전할당합니다.

msgCell = cellfun(@(~) rosmessage("std_msgs/String"),cell(1,50),"UniformOutput",false);

입력 인수

모두 축소

메시지 유형으로, string형 스칼라 또는 문자형 벡터로 지정됩니다. string형은 대소문자를 구분하며 부분 일치는 허용되지 않습니다. rosmsg("list")를 호출하여 주어진 목록의 메시지와 일치해야 합니다.

ROS publisher로, Publisher 객체 핸들로 지정됩니다. rospublisher를 사용하여 객체를 만들 수 있습니다.

ROS subscriber로, Subscriber 객체 핸들로 지정됩니다. rossubscriber를 사용하여 객체를 만들 수 있습니다.

ROS 서비스 또는 액션 클라이언트로, ServiceClient 또는 ActionClient 객체 핸들로 지정됩니다. rossvcclient 또는 rosactionclient를 각각 사용하여 서비스 클라이언트 또는 액션 클라이언트를 만들 수 있습니다.

ROS 서비스 또는 액션 서버로, ServiceServer 또는 ActionServer 객체 핸들로 지정됩니다. rossvcserver 또는 rosactionserver를 각각 사용하여 서비스 서버 또는 액션 서버를 만들 수 있습니다.

출력 인수

모두 축소

ROS 메시지로, Message 객체 핸들 또는 구조체로 반환됩니다.

확장 기능

버전 내역

R2019b에 개발됨

모두 확장