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

How can I store subscribed message and use that from other function?

asked 2019-01-21 19:50:39 -0600

jajugoguma gravatar image

Hi.

I've written a simple subscriber to subscribe and use message, but it isn't work. it just prints zero, not stored values.

The subscriber is test program to use stored message from published by v-rep.

How should I change in this code?

Thank you for your interest and help.

#include <ros/ros.h>
#include <geometry_msgs/Pose.h>

geometry_msgs::Pose p;

void poseCB(const geometry_msgs::Pose::ConstPtr &msg) {
    p = *msg;
}

void print() {
    ROS_INFO("Crrent state : (%f, %f, %f)", p.position.x, p.position.y, p.position.z);
}

int main(int argc, char **argv) {
    ros::init(argc, argv, "sub_test");
    ros::NodeHandle nh;
    ros::Subscriber sub = nh.subscribe<geometry_msgs::Pose>("/vrep_ros_interface/bear_pose", 1, &poseCB);
    print();
    ros::spin();

    return 0;
}
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2019-01-21 22:47:27 -0600

Hamid Didari gravatar image

updated 2019-01-21 22:48:04 -0600

Hi in your code print just call for once you can change code like this:

ros::Rate loop_rate(100);
 while (ros::ok()){
    print();
    ros::spinOnce();
    loop_rate.sleep();   
    }
edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2019-01-21 19:50:39 -0600

Seen: 143 times

Last updated: Jan 21 '19