启动文件以启动 ROS 服务?

Launch file to start ROS Services?

我创建了带有客户端和服务器节点的 ROS 服务。我创建了一个 ROS 服务,将 IMU 传感器值从服务器传递到客户端。我能够调用服务器和客户端节点并获取值。但是当用启动文件调用它们时,我得到了零

这里是服务器节点

    #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;
}

客户

#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;
}

和启动文件

<?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>

我收到了 0 个回复。

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

但是当手动 运行 服务器和客户端

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

有什么帮助吗?

返回 0 的原因是客户端在启动时立即调用该服务。由于它也立即 returns 最后一个缓存值,并且它们几乎同时通过 roslaunch 启动,这意味着基本上没有办法通过调用服务接收有关该主题的消息。这适用于 rosrun,因为必须手动启动节点才能让它有足够的时间实际接收有关该主题的内容。您可以通过在客户端的启动时添加延迟来观察它,如下所示:

#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;
}