My service client script is not working

My service server is as shown below and it works just fine whenever I simply use the command:

rosservice call /move_bb8_in_circle "{}"

However, when I try to create the service client, it is not working as expected, but the thing is that, my service client is giving response!

My service server script:

#include <geometry_msgs/Twist.h>
#include <ros/ros.h>
#include <std_srvs/Empty.h>

class Circle {
private:
  // ros::Publisher pub;
  ros::NodeHandle nh;
  ros::Publisher pub;
  ros::ServiceServer move_in_circle_service;
  geometry_msgs::Twist twist;

public:
  Circle();
  bool circle_callback(std_srvs::Empty::Request &request,
                       std_srvs::Empty::Response &response);
};

Circle::Circle()
    : move_in_circle_service{nh.advertiseService(
          "/move_bb8_in_circle", &Circle::circle_callback, this)},
      pub{nh.advertise<geometry_msgs::Twist>("cmd_vel", 10)} {}

bool Circle::circle_callback(std_srvs::Empty::Request &request,
                             std_srvs::Empty::Response &response) {
  ROS_INFO("My circle callback has been called");
  twist.linear.x = 0.5;
  twist.angular.z = 0.5;
  pub.publish(twist);
  ROS_INFO("I am moving");
  return true;
}

int main(int argc, char **argv) {

  ros::init(argc, argv, "move_bb8_in_circle_node");

  Circle bb8_move;

  ros::spin();

  return 0;
}

My service client script:

#include <ros/ros.h>
#include <std_srvs/Empty.h>

int main(int argc, char **argv) {

  ros::init(argc, argv, "move_bb8_in_circle_service_client");
  ros::NodeHandle nh;

  ros::ServiceClient move_bb8_in_circle_service;

  move_bb8_in_circle_service =
      nh.serviceClient<std_srvs::Empty>("/move_bb8_in_circle");

  std_srvs::Empty srv;

  if (move_bb8_in_circle_service.call(srv)) {
    ROS_INFO("Service client called");
  } else {
    ROS_ERROR("Failed to call /move_bb8_in_circle");
    return 1;
  };

  return 0;
}

When I launch call_bb8_move_in_circle_service_server.launch, my output is as follows but my robot is not circling as expected

process[move_bb8_in_circle_service_client-2]: started with pid [15942]
[ INFO] [1629478845.671246718]: My circle callback has been called
[ INFO] [1629478845.672613487]: I am moving
[ INFO] [1629478845.672996728]: Service client called
[move_bb8_in_circle_service_client-2] process has finished cleanly

As shown in the last output (Service client called), my service client is working properly but not the BB8 :confused:

Nevermind, I found the issue. Turns out that my service client somehow is too fast so I had to put some sleep rate in between to create a delay once the server is created. Somewhere along this line :

#include <ros/ros.h>
#include <std_srvs/Empty.h>

int main(int argc, char **argv) {

  ros::init(argc, argv, "move_bb8_in_circle_service_client");
  ros::NodeHandle nh;

  ros::ServiceClient move_bb8_in_circle_service;

  move_bb8_in_circle_service =
      nh.serviceClient<std_srvs::Empty>("/move_bb8_in_circle");

  std_srvs::Empty srv;

 ros::Rate rate(0.5);

    rate.sleep();

  if (move_bb8_in_circle_service.call(srv)) {
    ROS_INFO("Service client called");
  } else {
    ROS_ERROR("Failed to call /move_bb8_in_circle");
    return 1;
  };

  return 0;
}
1 Like