Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Wrapper function for data received through topics

Which approach should I use? I have a function foo(float data) that handles some data, computes stuff and returns data, both receiving and returning should go to a topic.

Is it better to write a wrapper function that receives a message from a topic, extracts the data and calls foo(float data). And again gets the return value and publishes it in a message.

ros::Subscriber in_sub = n.subscribe<std_msgs::Float32>("/dist_front", 1, wrapper_foo);
ros::spin();
return 0; 
}

void wrapper_foo(std_msgs::Float32 msg_dist){
    // Extract the information
    float ret = foo(float msg_dist.data());

    //publish
    std_msgs::Float32 msg_acc;
    msg_acc.data = ret;
    acc_pub.publish(msg_acc);
}

float foo(float dist) {
        ...
        return 1.0f;
 }

Or change foo(float data) to foo(std_msg::Float32 data) and extract the data in this function and also publishes it. This is less code, but I have the feeling I'm loosing modularity.

ros::Subscriber in_sub = n.subscribe<std_msgs::Float32>("/dist_front", 1, foo);
ros::spin();
return 0; 
}

void foo(std_msgs::Float32 msg_dist) {
        // Extract the information
        float dist = msg_dist.data();

        //publish
        std_msgs::Float32 msg_acc;
        msg_acc.data = 1.0f;
        acc_pub.publish(msg_acc);
        ...
 }