python 正在接收ImportError尝试将自定义服务文件导入到服务服务器节点

siotufzp  于 2023-05-21  发布在  Python
关注(0)|答案(1)|浏览(170)

我试图构建一个ROS 2网络,其中服务服务器节点从www.example.com脚本读取数据sensor.py,并将其提供给定制服务。当我试图从我所在的包文件夹导入我的.srv文件时,我收到一个错误

bin/python3 /home/name/ros2_ws/src/dof_filter_service_pkg/dof_filter_service_pkg/service_server.py
Traceback (most recent call last):
  File "/home/name/ros2_ws/src/dof_filter_service_pkg/dof_filter_service_pkg/service_server.py", line 7, in <module>
    from dof_filter_service_pkg import dof_filter
ImportError: cannot import name 'dof_filter' from 'dof_filter_service_pkg' (/home/name/ros2_ws/install/dof_filter_service_pkg/lib/python3.10/site-packages/dof_filter_service_pkg/__init__.py)

下面是我的service_server.py文件的设置:

import rclpy
import numpy as np
import socket
import time

from rclpy.node import Node
from dof_filter_service_pkg import dof_filter

class ServiceServer(Node):
    def __init__(self):
        super().__init__('service_server')
        self.service = self.create_service(dof_filter, 'service_server_topic',self.service_callback)

下面是我的dof_filter.srv文件,它位于一个srv文件夹中

# service_client sends an empty request
# number_of_samples is a fixed amount handled in service_server

---

# Response message:

float32 filtered_data

我的package.xml文件:

<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
  <name>dof_filter_service_pkg</name>
  <version>0.0.0</version>
  <description>3-DOF ROS2 Package</description>
  <maintainer email="name@todo.todo">name</maintainer>
  <license>TODO: License declaration</license>

  <build_depend>rclpy</depend>
  <exec_depend>rclpy</exec_depend>

  <buildtool_depend>rosidl_default_generators</buildtool_depend>
  <exec_depend>rosidl_default_generators</exec_depend>

  <test_depend>ament_copyright</test_depend>
  <test_depend>ament_flake8</test_depend>
  <test_depend>ament_pep257</test_depend>
  <test_depend>python3-pytest</test_depend>

  <export>
    <build_type>ament_python</build_type>
  </export>
</package>

我在执行colcon build时没有收到任何错误,只有在尝试运行service_server. py时才收到。所有这些文件都在我的src > dof_filter_service_pkg > dof_filter_service_pkg文件夹中。使用Ubuntu 22.04、ROS 2 Humble和Python 3.10。谢谢大家!

我尝试过colconbuild,将dof_filter.srv移出文件夹并放入包目录,然后将from dof_filter_service_pkg import dof_filter更改为from dof_filter_service_pkg.srv import dof_filter

tf7tbtn2

tf7tbtn21#

根据您的评论,似乎您在ament_python包中包含了srv文件。通常,建议您专门为消息和服务文件定义创建一个包。您可以通过以下方式创建一个新的ament_cmake包:ros2 pkg create --build-type ament_cmake tutorial_interfaces。接下来,您应该使用mkdir srv创建您的srv文件目录,并将您的服务定义放在那里。然后编辑您的CMakeLists.txt以包含服务的构建步骤:

find_package(rosidl_default_generators REQUIRED)

rosidl_generate_interfaces(${PROJECT_NAME}
  "srv/YourService.srv"
)

最后,确保package.xml中包含默认生成器

<build_depend>rosidl_default_generators</build_depend>

<exec_depend>rosidl_default_runtime</exec_depend>

<member_of_group>rosidl_interface_packages</member_of_group>

然后你可以像其他colcon软件包一样构建它。

相关问题