ROS 启动文件问题

ROS Launch file Issue

我正在实施一个简单的 roslaunch 文件,但我得到了退出代码 2 状态我已将确切的日志粘贴在下面,不知道会导致此问题的原因以及如何纠正它。当通过 rosrun 用作节点时,它可以完美运行。

PC@PC :~/catkin_ws$ roslaunch apriltag_ros apriltag.launch 
... logging to /home/pc/.ros/log/c7672d6c-479d-11ec-bd02-c56b9aa24743/roslaunch-PC-20782.log
Checking log directory for disk usage. This may take a while.
Press Ctrl-C to interrupt 
Done checking log file disk usage. Usage is <1GB.

started roslaunch server http://PC:38583/

SUMMARY
========

PARAMETERS
* /rosdistro: noetic
* /rosversion: 1.15.13

NODES
/
apriltag_ros (apriltag_ros/tagdetector.py)

auto-starting new master
process[master]: started with pid [20790]
ROS_MASTER_URI=http://localhost:11311

setting /run_id to c7672d6c-479d-11ec-bd02-c56b9aa24743
process[rosout-1]: started with pid [20800]
started core service [/rosout] 
process[apriltag_ros-2]: started with pid [20803]
usage: tagdetector.py [-h] [-f FAMILIES] [-B N] [-t N] [-x SCALE] [-b SIGMA] [-0] [-1] [-2] [-c]
tagdetector.py: error: unrecognized arguments: __name:=apriltag_ros __log:=/home/pc/.ros/log/c7672d6c-479d-11ec-bd02-c56b9aa24743/apriltag_ros-2.log
[apriltag_ros-2] process has died [pid 20803, exit code 2, cmd /home/pc/catkin_ws/src/apriltag_ros/scripts/tagdetector.py __name:=apriltag_ros __log:=/home/pc/.ros/log/c7672d6c-479d-11ec-bd02-c56b9aa24743/apriltag_ros-2.log].
log file: /home/pc/.ros/log/c7672d6c-479d-11ec-bd02-c56b9aa24743/apriltag_ros-2*.log

启动文件代码如下

<?xml version = "1.0"?>

<launch>
    <node name = "apriltag_ros" pkg = "apriltag_ros" type = "tagdetector.py"  output="screen" />
</launch>

编辑 1:我添加了用于发布到 ROS 的功能,其中 ROS 节点已初始化和使用。

我用来发布数据的函数如下

# Import Libraries
from argparse import ArgumentParser
import sys
import cv2
import apriltagbase
import numpy as np
import math
import rospy
from apriltag_ros.msg import tag

def location_publisher():
    """
    ROS Publisher: Publishes X, Y and Yaw values
    """
    # rospy.myargv(argv=sys.argv)
    pub = rospy.Publisher('apriltag_pose', tag, queue_size=10)
    rospy.init_node('apriltag_ros')
    msg = tag()
    msg.location.x = z # z in camera frame of reference is the distance from the tag i.e. x in general frame of reference
    msg.location.y = y
    msg.location.theta = yaw
    msg.status.data = status_tag
    msg.tagid.data = tag_id
    rospy.loginfo(msg)
    pub.publish(msg)

# Detect AprilTag
parser = ArgumentParser(description='Detect AprilTags from video stream.')
apriltagbase.add_arguments(parser)
options = parser.parse_args()

detector = apriltagbase.Detector(options, searchpath=apriltagbase._get_dll_path())

while(video.isOpened()):

    check,frame = video.read()
    
    if not check:
        break

    # overlay box on AprilTag format of detect_tags can be viewd in apriltag.py line 590.
    result,overlay = apriltagbase.detect_tags(frame, 
                                        detector,
                                        camera_params=(565.348501, 565.653872, 326.910261, 226.544390),
                                        tag_size=0.1688,
                                        vizualization=3,
                                        verbose=3,
                                        annotation=True
                                        )
    cv2.imshow('April Tag', overlay)
    

编辑 2:删除了完整代码,现在只显示开源代码

Link 到 ROS 论坛上的同一个问题:https://answers.ros.org/question/391124/roslaunch-exit-code-2-error/

输出

usage: tagdetector.py [-h] [-f FAMILIES] [-B N] [-t N] [-x SCALE] [-b SIGMA] [-0] [-1] [-2] [-c]

表明,tagdetector.py是一个脚本,需要用一些特定的参数调用。 首先你应该确定,如果脚本是一个 rosnode 以确保像这样通过 roslaunch 启动它是正确的。 其次,查看 the launch documentation,其中显示了如何向节点添加参数。

因此您需要通过添加

将一些提到的参数添加到您的启动文件中
args="-h"

到节点启动入口。

更新

您添加的脚本使用 apriltagbase 中定义的解析参数。

apriltagbase.add_arguments(parser)
options = parser.parse_args()

argparse 对于此处未添加的所有参数均失败。所以你需要将其替换为

options, unknown = parser.parse_known_args()

防止由于 roslaunch 自动添加的参数而失败。