我正在尝试使用 roscpp 客户端来调用 rospy 服务器。不幸的是,即使我的服务器似乎运行正常且没有问题,我来自客户的呼叫也总是失败。我在下面包含了客户端和服务器的代码以及我收到的输出(我可以根据要求包含 CMakeList.txt 和 package.xml,但我很确定问题出在以下文件中)。
服务.py:
#!/usr/bin/env python
from std_srvs.srv import Empty, EmptyResponse
import rospy
def serviceCall(call):
print "service called"
return EmptyResponse()
def serviceCall_server():
rospy.init_node('service_server')
s = rospy.Service('a_new_service', Empty, serviceCall)
print "Ready to receive service calls."
rospy.spin()
if __name__ == "__main__":
serviceCall_server()
客户端.cpp:
#include <ros/ros.h>
#include <std_srvs/Empty.h>
int main(int argc, char** argv){
ros::init(argc, argv, "service_client");
ros::NodeHandle n;
ros::Rate r(30);
ros::ServiceClient service_call = n.serviceClient<std_srvs::Empty>("/a_new_service", 100);
std_srvs::Empty srv;
service_call.waitForExistence();
if (service_call.call(srv))
{
ROS_ERROR("Successfully called service a_new_service");
}
else
{
ROS_ERROR("Failed to call service a_new_service");
}
}
启动文件:
<launch>
<node name="server" pkg="test" type="server.py" output="screen"/>
<node name="client" pkg="test" type="client" output="screen" />
</launch>
启动文件的输出:
core service [/rosout] found
process[server-1]: started with pid [25659]
process[client-2]: started with pid [25660]
[ INFO] [1485448779.402439557]: waitForService: Service [/a_new_service] has not been advertised, waiting...
Ready to receive service calls.
[ INFO] [1485448779.630636002]: waitForService: Service [/a_new_service] is now available.
[ERROR] [1485448779.630685730]: Failed to call service a_new_service
运行启动文件会导致服务调用失败(请参阅输出的最后一行)。我可以从终端成功调用rosservice call /a_new_service {},这让我相信我的客户做错了什么,但我不确定哪里出了问题。
--
编辑:因此,经过进一步调查,这似乎是启动文件的错误,因为我可以使用 rosrun 调用服务器和客户端并让它们成功通信。有谁知道为什么启动文件会导致调用错误?特别是考虑到 waitForService 调用表明服务器可用这一事实。