fishros / d2l-ros2

动手学ROS2课程配套教程,欢迎关注公众号《鱼香ROS》
https://fishros.com/d2lros2
476 stars 71 forks source link

6.服务之RCLPY实现 (案例代码有误) #33

Open ipc-robot opened 1 month ago

ipc-robot commented 1 month ago
ipc-robot@ipcrobot-Precision-Tower-3420:~/ros2_ws$ ros2 run example_service_rclpy service_client_02
[INFO] [1722589543.093786297] [service_client_02]: 节点已启动:service_client_02!
Traceback (most recent call last):
  File "/home/ipc-robot/ros2_ws/install/example_service_rclpy/lib/example_service_rclpy/service_client_02", line 33, in <module>
    sys.exit(load_entry_point('example-service-rclpy==0.0.0', 'console_scripts', 'service_client_02')())
  File "/home/ipc-robot/ros2_ws/install/example_service_rclpy/lib/python3.10/site-packages/example_service_rclpy/service_client_02.py", line 14, in main
    node.send_request(3,4)
AttributeError: 'ServiceClient02' object has no attribute 'send_request'
[ros2run]: Process exited with failure 1

以下是client_02的代码:

#!/usr/bin/env python3
import rclpy
from rclpy.node import Node
from example_interfaces.srv import AddTwoInts

class ServiceClient02(Node):
    def __init__(self,name):
        super().__init__(name)
        self.get_logger().info("节点已启动:%s!" % name)

def main(args=None):
    rclpy.init(args=args) # 初始化rclpy
    node = ServiceClient02("service_client_02")  # 新建一个节点
    node.send_request(3,4)
    rclpy.spin(node) # 保持节点运行,检测是否收到退出指令(Ctrl+C)
    rclpy.shutdown() # 关闭rclpy
fishros commented 1 month ago

需要继续往下看,这里代码不完整。另外最近新书和新课程上线了,建议学习新版教程,更加详细,且代码经过反复验证:https://github.com/fishros/ros2bookcode