Range Sensors range/fl, range/fr

I want to use all four range sensors of the Rosbot.
I did subscribe to all four range topics. Since each subsriber has its own callback function (one for fr, fl …) I would like to know how I can write a publisher to cmd_vel which uses all 4 range topics as its function input.
This may be a trivial problem, however I am stuck and help would be appreciated.

Thanks

Udo

You can not make a single subscriber for the four different topics, but you could store values from each sensor in global variables.

Define variables for all sensors

double sensor_fl;
double sensor_fr;
double sensor_rl;
double sensor_rr;

In callback for each sensor update value of appropriate variable:

void sensor_fl_callback(const sensor_msgs::Range::ConstPtr msg){
sensor_fl  = msg->range;
}

In main loop process the ranges and publish cmd_vel:

ros::Rate loop_rate(10);
while (ros::ok())
{
  // Process ranges here
  // place your output into cmd_vel_msg
  cmd_vel_pub.publish(cmd_vel_msg);
  ros::spinOnce();
  loop_rate.sleep();
}