ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Subscribe Message file

asked 2017-11-23 10:01:56 -0500

Dornier gravatar image

updated 2017-11-23 10:03:04 -0500

Hello! I want to subscribe data from .msg file. However the file has four other msgs inside. The format of the .msg file is:

Header header
geometry_msgs/Vector3 direction
geometry_msgs/Vector3 normal
geometry_msgs/Point palmpos
geometry_msgs/Vector3 ypr

Could you tell me how to read, for example, x,y,z coordinates inside geometry_msgs/Vector3?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2017-11-26 16:24:43 -0500

updated 2017-11-26 17:40:03 -0500

I've created a custom msg (msg/mycustom.msg) in my package called mypkg, created a publisher and 2 subscribers, one in python, another in cpp. You can see below:

my_subscriber.py

#!/usr/bin/env python
import rospy
from mypkg.msg import mycustom

def callback(data):
    rospy.loginfo("direction x: [" + str(data.direction.x) + "]")
    rospy.loginfo("direction y: [" + str(data.direction.y) + "]")
    rospy.loginfo("direction z: [" + str(data.direction.z) + "]")

def listener():

    rospy.init_node('listener', anonymous=True)

    rospy.Subscriber("my_topic", mycustom, callback)

    rospy.spin()

if __name__ == '__main__':
    listener()

cpp_subscriber.cpp

#include "ros/ros.h"
#include "mypkg/mycustom.h"

void chatterCallback(const mypkg::mycustom::ConstPtr& msg)
{
  ROS_INFO("direction x: [%.2f]", msg->direction.x);
  ROS_INFO("direction y: [%.2f]", msg->direction.y);
  ROS_INFO("direction z: [%.2f]", msg->direction.z);
}

int main(int argc, char **argv)
{
  ros::init(argc, argv, "cpp_subscriber");

  ros::NodeHandle n;

  ros::Subscriber sub = n.subscribe("my_topic", 1000, chatterCallback);

  ros::spin();

  return 0;
}

There's a video step-by-step showing how I did it: https://www.youtube.com/watch?v=aLfJZf9py0g

edit flag offensive delete link more

Comments

how about if the data is a array?

Astronaut gravatar image Astronaut  ( 2020-03-04 23:04:27 -0500 )edit
0

answered 2017-11-23 19:39:57 -0500

jayess gravatar image

updated 2017-11-26 17:03:42 -0500

If your message is called data then you'd access the x, y, and z components of the direction attribute as:

x = data.direction.x
y = data.direction.y
z = data.direction.z

This is assuming Python. You can access the attributes of messages by using the . operator. If you're using C++, replace the first . with a ->. So it becomes

x = data->direction.x
y = data->direction.y
z = data->direction.z
edit flag offensive delete link more

Comments

Actually, "replace the . with a ->" is too generic and not true. It's necessary to do that only to get the pointer of data parameter, then we have to use . for the next attributes (e.g: data->direction.x)

marcoarruda gravatar image marcoarruda  ( 2017-11-26 16:28:32 -0500 )edit

Good point @marcoarruda. Thanks.

jayess gravatar image jayess  ( 2017-11-26 17:02:22 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2017-11-23 10:01:56 -0500

Seen: 1,242 times

Last updated: Mar 04 '20