0

I created ROS Service with a client and server node. I created a ROS Service that pass the IMU sensors values from the Server to Client. And Im able to call the server and client node and get the values. But when call them with the launch file I got zero

Here the server node

    #include "ros/ros.h"
    #include <sensor_msgs/Imu.h>
    #include "imu_service/ImuValue.h"
    
    ros::ServiceServer service;
    double current_x_orientation_s;
    double get_imu_orientation_x;
    
    bool get_val(imu_service::ImuValue::Request  &req, imu_service::ImuValue::Response &res)
    {
        
        ROS_INFO("sending back response");    
        res.current_x_orientation_s = get_imu_orientation_x;
        //.. same for the other IMU values
            
    }
    
    void imuCallback(const  sensor_msgs::ImuConstPtr& msg)
    {
      
         current_x_orientation_s= msg->orientation.x;
         get_imu_orientation_x=current_x_orientation_s;
         // ..same for other IMU values
               
    }
    
    int main(int argc, char **argv)
    {
      ros::init(argc, argv, "imu_status_server");
      ros::NodeHandle n;
      ros::Subscriber sub = n.subscribe("/thrbot/imu", 10, imuCallback);
      service = n.advertiseService("imu_status_server", get_val);
      ROS_INFO("Starting server...");
      ros::spin();
      return 0;
}

Client

#include "ros/ros.h"
#include "ros_services/ImuValue.h"
#include <cstdlib>


ros::ServiceClient client;


int main(int argc, char **argv)
{
        ros::init(argc,argv,"imu_client_node");
        ros::NodeHandle n;
        ros::NodeHandle nh_;
        //ros::Subscriber joy_sub_ = nh_.subscribe<sensor_msgs::Joy>("/thrbot/joy", 10, joystick_callback);
    client = n.serviceClient<ros_services::ImuValue>("imu_status_server");
    ros_services::ImuValue srv;
    client.call(srv);       
    std::cout << "Got accel x: " << srv.response.current_x_orientation_s << std::endl;

        

  return 0;
}

And the launch file

<?xml version="1.0"?>
<launch>
  
     
     <node name="ImuServerService" pkg="ros_services" type="ImuServerService"     output="screen">
       </node>
    
    <node name="ImuClientService" pkg="ros_services" type="ImuClientService"     output="screen"/>
 
</launch>

I got 0 as response.

[ INFO] [1631800449.207350420]: Starting server...
[ INFO] [1631800449.212336478]: sending back response
Got accel x: 0

But when manually run the Server and the Client with

rosrun ros_services ImuServerService and rosrun ros_services ImuClientService the value is correct one

Any help?

Bob9710
  • 205
  • 3
  • 15

1 Answers1

1

The reason you're getting 0 back is because the client immediately calls the service on startup. Since it also immediately returns the last cached value and they're both started at almost the same time via roslaunch this means there's essentially no way a message will be received on the topic by the service is called. This works with rosrun because having to manually launch the nodes gives it enough time to actually receive something on the topic. You can observe it by adding a delay to the start of your client like so:

#include "ros/ros.h"
#include "ros_services/ImuValue.h"
#include <cstdlib>


ros::ServiceClient client;


int main(int argc, char **argv)
{
        ros::init(argc,argv,"imu_client_node");
        ros::NodeHandle n;
        ros::NodeHandle nh_;
        //ros::Subscriber joy_sub_ = nh_.subscribe<sensor_msgs::Joy>("/thrbot/joy", 10, joystick_callback);
    client = n.serviceClient<ros_services::ImuValue>("imu_status_server");
    ros_services::ImuValue srv;

    ros::Duration(5).sleep(); //Sleep for 5 seconds    

    client.call(srv);       
    std::cout << "Got accel x: " << srv.response.current_x_orientation_s << std::endl;

        

  return 0;
}
BTables
  • 4,413
  • 2
  • 11
  • 30
  • ok great. thanks. One comment. How will be the launch file if I want start multiple services with one launch file? Such as IMUService, SonarService, CameraService and so on, in one launch file at once – Bob9710 Sep 17 '21 at 09:04
  • The launch file can look basically the same as the one you have above. The only different is you'll need to add a `` for every service you want to add; i.e. SonarSerivce, CameraService. – BTables Sep 17 '21 at 16:08
  • Ok. great thanks. Last thing. I post a question to start the same services(also can be multiple) with a normal joystick click. So , launch file same services just with a joystick click to call the service , but there even didnt get a zero value.Simple nothing happen . The thread is this one [link] (https://stackoverflow.com/questions/69179083/how-to-call-a-ros-service-with-a-launch-file-every-time-press-the-joystick-to-co/69186667?noredirect=1#comment122305682_69186667). Please can you help on that? – Bob9710 Sep 17 '21 at 18:02