服务之RCLPY实现遇到问题:waiting for service to become available...
-
@小鱼 抱歉,服务请求显示waiting for service to become available..
service_server_02.py的代码完全参照您的《动手学ros2》中代码(如下),本人刚入门,尚不清楚是否需要其他信息来协助找出问题
import rclpy
from rclpy.node import Node
from example_interfaces.srv import AddTwoIntsclass ServiceServer02(Node):
def init(self, name):
super().init(name)
self.get_logger().info("节点已启动:%s!" % name)
self.add_ints_server = self.create_service(
AddTwoInts, "add_two_ints_srv", self.handle_add_two_ints
)
self.get_logger().info("初始化已完成")def handle_add_two_ints(self, request, response): self.get_logger().info(f"收到请求,计算{request.a} + {request.b}") response.sum = request.a + request.b return response
def main(args=None):
rclpy.init(args=args) # 初始化rclpy
node = ServiceServer02("service_server_02") # 新建一个节点
rclpy.spin(node) # 保持节点运行,检测是否收到退出指令(Ctrl+C)
rclpy.shutdown() # 关闭rclpy -
@1785360876 请阅读提问前必看,并修改格式:1.不要截图 2.代码用代码块包裹 如果不知道怎么做,可以看编辑框右边的帮助:https://commonmark.org/help/
-
@小鱼
在终端中启节点~/d2lros2/chapt3/chapt3_ws$ ros2 run example_service_rclpy service_server_02 [INFO] [1721376210.440943060] [service_server_02]: 节点已启动:service_server_02
在另一终端请求服务始终显示“waiting for service to become available...”
og@GalaxyOg:~/d2lros2/chapt3/chapt3_ws$ ros2 service call /add_two_ints_srv example_interfaces/srv/AddTwoInts "{a: 5,b: 10}" waiting for service to become available...
已使用ros2 node list 和ros2 service list查看结果如下
og@GalaxyOg:~$ ros2 node list /service_server_02 og@GalaxyOg:~$ ros2 service list /add_two_ints_srv /service_server_02/describe_parameters /service_server_02/get_parameter_types /service_server_02/get_parameters /service_server_02/list_parameters /service_server_02/set_parameters /service_server_02/set_parameters_atomically
service_server_02.py的内容如下
import rclpy from rclpy.node import Node from example_interfaces.srv import AddTwoInts class ServiceServer02(Node): def __init__(self, name): super().__init__(name) self.get_logger().info("节点已启动:%s!" % name) self.add_ints_server = self.create_service( AddTwoInts, "add_two_ints_srv", self.handle_add_two_ints ) self.get_logger().info("初始化已完成") def handle_add_two_ints(self, request, response): self.get_logger().info(f"收到请求,计算{request.a} + {request.b}") response.sum = request.a + request.b return response def main(args=None): rclpy.init(args=args) # 初始化rclpy node = ServiceServer02("service_server_02") # 新建一个节点 rclpy.spin(node) # 保持节点运行,检测是否收到退出指令(Ctrl+C) rclpy.shutdown() # 关闭rclpy
-
@1785360876 看起来没问题:
尝试重启下daemon
ros2 daemon stop ros2 daemon start
然后再用:
ros2 run example_service_rclpy service_server_02 `` 试试
-
og@GalaxyOg:~/d2lros2/chapt3/chapt3_ws$ ros2 daemon stop The daemon is not running og@GalaxyOg:~/d2lros2/chapt3/chapt3_ws$ ros2 daemon start The daemon has been started
再次启动节点后命令行请求服务任然显示“waiting for service to become available...”
-
-
@小鱼
自带的服务在测试中正常运行,今日启动自己写的节点后发现服务中没有“/add_two_ints_srv”服务(前日测试中该服务在service list中),测试结果如下og@GalaxyOg:~$ ros2 service list /service_server_02/describe_parameters /service_server_02/get_parameter_types /service_server_02/get_parameters /service_server_02/list_parameters /service_server_02/set_parameters /service_server_02/set_parameters_atomically og@GalaxyOg:~$ ros2 node list /service_server_02
请问如何解决“/add_two_ints_srv”服务为正常启动的问题呢?
节点启动终端如下og@GalaxyOg:~/d2lros2/chapt3/chapt3_ws$ ros2 run example_service_rclpy service_server_02 [INFO] [1721546960.212194499] [service_server_02]: 节点已启动:service_server_02
-
@1785360876 打开install目录下对应的功能包,找找service_server_02对应的py文件,确认运行的文件和src中的一致。
-
@小鱼 问题已解决,非常感谢!
经检查是由于install下对应python文件与src下文件不一致导致(install文件夹下保存着我此前由于拼写错误而误创建的service_sever_02.py,随后我删除了该文件并创建了正确的service_server_02.py并在setupp.py文件中手动进行了更改)。
我在尝试解决问题的过程中曾试过只删除build文件夹后进行整改工作空间的build,但并未检查install文件夹,今后若遇到类似的问题是否能删除install与build文件夹,再重新build整个工作空间呢? -
@1785360876 最好可以删掉install和build重新构建,防止这个问题
-
-