安装我的包时ROS2启动错误

问题描述 投票:0回答:1

我在 Ubuntu 20.04、22.04 上测试了 Foxy 和谦虚,并得到了相同的错误。

当我构建包并输入源命令时,

source /home/ws/ros2_ws/install/setup.bash
,我收到以下错误。

错误

$> ros2 launch
Failed to load entry point 'launch': No module named 'launch.launch_description_sources'
Traceback (most recent call last):
  File "/opt/ros/foxy/bin/ros2", line 11, in <module>
    load_entry_point('ros2cli==0.9.13', 'console_scripts', 'ros2')()
  File "/opt/ros/foxy/lib/python3.8/site-packages/ros2cli/cli.py", line 39, in main
    add_subparsers_on_demand(
  File "/opt/ros/foxy/lib/python3.8/site-packages/ros2cli/command/__init__.py", line 237, in add_subparsers_on_demand
    extension = command_extensions[name]
KeyError: 'launch'

到目前为止我找到的唯一解决方案是删除启动文件夹。可能是什么问题?

包树

├── config
│   └── params_app.yaml
├── face_pose_estimation
│   ├── app_for_1.py
│   ├── components
│   │   ├── main_component.py
│   │   ├── __init__.py
│   │   └── utils
│   │       ├── common.py
│   │       ├── __init__.py
│   │       └── play_voice.py
│   ├── __init__.py
│   └── save_service_server.py
├── launch
│   ├── __init__.py
│   └── app_1.launch.py
├── log
│   └── 20241011_1114.txt
├── package.xml
├── README.md
├── resource
│   ├── ros_app.service
│   └── weights
├── script
│   ├── all.sh
│   ├── micro_ros.sh
│   └── usbcam_open.sh
├── setup.cfg
├── setup.py

应用程序.launch.py

from launch import LaunchDescription
from launch_ros.actions import Node

from ament_index_python.packages import get_package_share_directory
import os


def generate_launch_description():
    package_share_dir = get_package_share_directory("my_package")
    cam_params_file = os.path.join(package_share_dir, "config", "params_usbcam.yaml")
    app_params_file = os.path.join(package_share_dir, "config", "params_app.yaml")

    return LaunchDescription(
        [
            # Node(
            #     package="micro_ros_agent",
            #     execuable="micro_ros_agent",
            #     name="micro_ros",
            #     arguments=[
            #         "serial",
            #         "--dev",
            #         "/dev/ttyUSB0",
            #         "-b",
            #         "115200",
            #     ],
            # ),
            Node(
                package="usb_cam",
                executable="usb_cam_node_exe",
                name="app_cam",
                # parameters=[cam_params_file],
            ),
            Node(
                package="my_package",
                executable="app_for_1",
                name="app",
                parameters=[app_params_file],
            ),
        ]

设置.py

import os
from setuptools import setup, find_packages

package_name = "my_package"

data_files = [
    ("share/ament_index/resource_index/packages", ["resource/" + package_name]),
    ("share/" + package_name, ["package.xml"]),
]


def package_files(data_files, directory_list):
    paths_dict = {}
    for directory in directory_list:
        for path, directories, filenames in os.walk(directory):
            for filename in filenames:
                if filename == "__init__.py":
                    continue
                file_path = os.path.join(path, filename)
                install_path = os.path.join("share", package_name, path)
                if install_path in paths_dict.keys():
                    paths_dict[install_path].append(file_path)
                else:
                    paths_dict[install_path] = [file_path]
    for key in paths_dict.keys():
        data_files.append((key, paths_dict[key]))
    return data_files




def copy_weights_to_home():
    home_dir = os.path.expanduser("~")
    dest_dir = os.path.join(home_dir, ".temp", "weights")
    os.makedirs(dest_dir, exist_ok=True)

    src_dir = os.path.abspath(
        os.path.join(os.path.dirname(__file__), "resource", "weights")
    )
    for path, directories, filenames in os.walk(src_dir):
        for filename in filenames:
            src_file = os.path.join(path, filename)
            dest_file = os.path.join(dest_dir, filename)
            if not os.path.exists(dest_file):
                os.symlink(src_file, dest_file)
            elif os.path.islink(dest_file) and os.readlink(dest_file) != src_file:
                os.remove(dest_file)
                os.symlink(src_file, dest_file)


setup(
    name=package_name,
    version="0.0.0",
    packages=find_packages(exclude=["test"]),
    data_files=package_files(data_files, ["launch", "config", "resource"]),
    install_requires=["setuptools"],
    zip_safe=True,
    maintainer="foo",
    maintainer_email="[email protected]",
    description="TODO: Package description",
    license="TODO: License declaration",
    tests_require=["pytest"],
    entry_points={
        "console_scripts": [
            "app_for_1 = my_package.app_for_1:main",
        ],
    },
)

copy_weights_to_home()

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>my_package</name>
  <version>0.0.0</version>
  <description>ROS package</description>
  <maintainer email="[email protected]">foo</maintainer>
  <license>TODO: License declaration</license>

  <buildtool_depend>ament_python</buildtool_depend>

  <exec_depend>rclpy</exec_depend>
  <exec_depend>launch</exec_depend>
  <exec_depend>launch_ros</exec_depend>

  <depend>std_msgs</depend>
  <depend>sensor_msgs</depend>
  <depend>cv_bridge</depend>
  <depend>usb_cam</depend>
  <depend>v4l-utils</depend>
  <depend>ffmpeg</depend>
  <depend>ament_index_python</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>
python ros ros2
1个回答
0
投票

您可能想查看:RoboticsBackend - 创建 Python 包

启动文件

在包的根目录下创建一个

launch/
文件夹。你会把所有 您的启动文件位于此文件夹中。
$ cd ~/ros2_ws/src/my_python_pkg/

$ mkdir launch

现在,要安装这些启动文件,您需要修改 setup.py。

import os 
from glob import glob 
from setuptools import setup 
... 
data_files=[
   ('share/ament_index/resource_index/packages',
       ['resource/' + package_name]),
   ('share/' + package_name, ['package.xml']),
   (os.path.join('share', package_name, 'launch'), glob('launch/*.launch.py')), 
], 
...

对于我们的示例,包名称为“my_python_pkg”,这将安装 将所有启动文件从 launch/ 文件夹放入 〜/ros2_ws/install/my_python_pkg/share/my_python_pkg/launch/.

注意:只需修改setup.py一次。此后,每次 添加一个启动文件,您只需要编译您的包,以便 文件已安装,就是这样。

然后,启动启动文件:

ros2 launch package_name launch_file_name

© www.soinside.com 2019 - 2024. All rights reserved.