About clients and services, Rosject Second Part

I’m in the second part of the rosject and i made two solutions and i like to ask wich one is the best or the right one.

First Option: No client(I don’t think this is the way because i think it’s a weird use of service, like a switch¿)

bool service_callback(std_srvs::Empty::Request &req,
                      std_srvs::Empty::Response &res) {
  ROS_INFO("Service Called!");

  service_called = true;
return true;
}

and then in the main it’s just:

while (ros::ok()) {

    if (service_called && ( position > 375 || position < 345)) {
      var.angular.z = 0.1;
    } else {
      service_called = false;
      var.angular.z = 0.0;
      ROS_INFO("Nearest Laser: %i", position);
    }
    pub.publish(var);
    ros::spinOnce();
    r.sleep();
  }

position it’s just a global int that the subscriber callback is filling with the closest laser to a wall in ranges

**Second option: With Client ** (i think this one is what it’s supose to)


bool service_callback(std_srvs::Empty::Request &req,
                      std_srvs::Empty::Response &res) {
  ROS_INFO("Service Called!");
  ROS_INFO("El minimo es: %i", position);
  if ((375 < position || 345 > position)) {
    var.angular.z = 0.1;
    return false;
  } else {
    var.angular.z = 0.0;
    return true;
  }
}

the loop in the main is just:

while (ros::ok()) {
    pub.publish(var);
    ros::spinOnce();
    r.sleep();
  }

Client:

 bool keepCallingService = true;

  while (keepCallingService) {
    if (move_robot_client.call(srv)) {
      keepCallingService = false;
      ROS_INFO("Service Ended.");
    }
  }

The second option where you add the client is more complete