Wrapper function for data received through topics

asked 2016-11-14 11:20:35 -0500

Jonas25 gravatar image

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);
        ...
 }
edit retag flag offensive close merge delete