work with basic ros 2 messages -凯发k8网页登录
this example examines various ways to create, inspect, and populate ros 2 messages in matlab, that are commonly encountered in robotics applications.
ros messages are the primary container for exchanging data in ros 2. publishers and subscribers exchange data using messages on specified topics to carry data between nodes. for more information on sending and receiving messages, see .
to identify its data structure, each message has a message type. for example, sensor data from a laser scanner is typically sent in a message of type sensor_msgs/laserscan
. each message type identifies the data elements that are contained in a message. every message type name is a combination of a package name, followed by a forward slash /, and a type name:
matlab® supports many ros 2 message types that are commonly encountered in robotics applications. this example examines some of the ways to create, inspect, and populate ros 2 messages in matlab.
prerequisites: ,
find message types
use examplehelperros2createsamplenetwork
to populate the ros 2 network with three nodes and setup sample publishers and subscribers on specific topics.
examplehelperros2createsamplenetwork
use ros2 topic list -t
to find the available topics and their associated message type.
ros2 topic list -t
topic messagetype _____________________ _________________________________ {'/parameter_events'} {'rcl_interfaces/parameterevent'} {'/pose' } {'geometry_msgs/twist' } {'/rosout' } {'rcl_interfaces/log' } {'/scan' } {'sensor_msgs/laserscan' }
to find out more about the topic message type, use ros2message
to create an empty message of the same type. ros2message
supports tab completion for the message type. to quickly complete message type names, type the first few characters of the name you want to complete, and then press the tab key.
scandata = ros2message("sensor_msgs/laserscan")
scandata = struct with fields:
messagetype: 'sensor_msgs/laserscan'
header: [1×1 struct]
angle_min: 0
angle_max: 0
angle_increment: 0
time_increment: 0
scan_time: 0
range_min: 0
range_max: 0
ranges: 0
intensities: 0
the created message, scandata
, has many fields associated with data that you typically received from a laser scanner. for example, the minimum sensing distance is stored in the range_min
property and the maximum sensing distance in range_max
property.
you can now delete the created message.
clear scandata
to see a complete list of all message types available for topics and services, use ros2 msg list
.
explore message structure and get message data
ros 2 messages are represented as structures and the message data is stored in fields. matlab provides convenient ways to find and explore the contents of messages.
use ros2 msg show
to view the definition of the message type.
ros2 msg show geometry_msgs/twist
# this expresses velocity in free space broken into its linear and angular parts. vector3 linear vector3 angular
if you subscribe to the /pose
topic, you can receive and examine the messages that are sent.
controlnode = ros2node("/base_station"); posesub = ros2subscriber(controlnode,"/pose","geometry_msgs/twist")
posesub = ros2subscriber with properties: topicname: '/pose' latestmessage: [] messagetype: 'geometry_msgs/twist' newmessagefcn: [] history: 'keeplast' depth: 10 reliability: 'reliable' durability: 'volatile'
use receive
to acquire data from the subscriber. once a new message is received, the function returns it and stores it in the posedata
variable. specify a timeout of 10 seconds for receiving messages.
posedata = receive(posesub,10)
posedata = struct with fields:
messagetype: 'geometry_msgs/twist'
linear: [1×1 struct]
angular: [1×1 struct]
the message has a type of geometry_msgs/twist
. there are two other fields in the message: linear
and angular
. you can see the values of these message fields by accessing them directly.
posedata.linear
ans = struct with fields:
messagetype: 'geometry_msgs/vector3'
x: 0.0206
y: -0.0468
z: -0.0223
posedata.angular
ans = struct with fields:
messagetype: 'geometry_msgs/vector3'
x: -0.0454
y: -0.0403
z: 0.0323
you can see that each of the values of these message fields is actually a message in itself. geometry_msgs/twist
is a composite message made up of two geometry_msgs/vector3
messages.
data access for these nested messages works exactly the same as accessing the data in other messages. access the x
component of the linear
message using this command:
xpose = posedata.linear.x
xpose = 0.0206
set message data
you can also set message property values. create a message with type geometry_msgs/twist
.
twist = ros2message("geometry_msgs/twist")
twist = struct with fields:
messagetype: 'geometry_msgs/twist'
linear: [1×1 struct]
angular: [1×1 struct]
the numeric properties of this message are initialized to 0
by default. you can modify any of the properties of this message. set the linear.y
field to 5.
twist.linear.y = 5;
you can view the message data to make sure that your change took effect.
twist.linear
ans = struct with fields:
messagetype: 'geometry_msgs/vector3'
x: 0
y: 5
z: 0
once a message is populated with your data, you can use it with publishers and subscribers.
copy messages
ros 2 messages are structures. they can be copied directly to make a new message. the copy and the original messages each have their own data.
make a new empty message to convey temperature data, then make a copy for modification.
tempmsgblank = ros2message("sensor_msgs/temperature");
tempmsgcopy = tempmsgblank
tempmsgcopy = struct with fields:
messagetype: 'sensor_msgs/temperature'
header: [1×1 struct]
temperature: 0
variance: 0
modify the temperature
property of tempmsg and notice that the contents of tempmsgblank
remain unchanged.
tempmsgcopy.temperature = 100
tempmsgcopy = struct with fields:
messagetype: 'sensor_msgs/temperature'
header: [1×1 struct]
temperature: 100
variance: 0
tempmsgblank
tempmsgblank = struct with fields:
messagetype: 'sensor_msgs/temperature'
header: [1×1 struct]
temperature: 0
variance: 0
it may be useful to keep a blank message structure around, and only set the specific fields when there is data for it before sending the message.
thermometernode = ros2node("/thermometer"); temppub = ros2publisher(thermometernode,"/temperature","sensor_msgs/temperature"); tempmsgs(10) = tempmsgblank; % pre-allocate message structure array for imeasure = 1:10 % copy blank message fields tempmsgs(imeasure) = tempmsgblank; % record sample temperature tempmsgs(imeasure).temperature = 20 randn*3; % only calculate the variation once sufficient data observed if imeasure >= 5 tempmsgs(imeasure).variance = var([tempmsgs(1:imeasure).temperature]); end % pass the data to subscribers send(temppub,tempmsgs(imeasure)) end errorbar([tempmsgs.temperature],[tempmsgs.variance])
save and load messages
you can save messages and store the contents for later use.
get a new message from the subscriber.
posedata = receive(posesub,10)
posedata = struct with fields:
messagetype: 'geometry_msgs/twist'
linear: [1×1 struct]
angular: [1×1 struct]
save the pose data to a mat file using the function.
save("posefile.mat","posedata")
before loading the file back into the workspace, clear the posedata
variable.
clear posedata
now you can load the message data by calling the function. this loads the
posedata
from above into the messagedata
structure. posedata
is a data field of the struct.
messagedata = load("posefile.mat")
messagedata = struct with fields:
posedata: [1×1 struct]
examine messagedata.posedata
to see the message contents.
messagedata.posedata
ans = struct with fields:
messagetype: 'geometry_msgs/twist'
linear: [1×1 struct]
angular: [1×1 struct]
you can now delete the mat file.
delete("posefile.mat")
disconnect from ros 2 network
remove the sample nodes, publishers, and subscribers from the ros 2 network.
examplehelperros2shutdownsamplenetwork