使用apriltag_ros检测相机姿态

0 环境

  • Nvidia Jetson Orin NX
  • Jetpack 5.1.1
  • 双目相机
  • ROS noetic

1 相机驱动

1.1 建立ROS工作空间

建立工作空间

# 在home下名为catkin_ws的新建文件夹
mkdir -p  ~/catkin_ws/src

# 进入catkin_ws文件夹
cd ~/catkin_ws/src

# 将当前文件夹属性转化为工作空间
catkin_init_workspace

编译工作空间

# 进入catkin_ws文件夹
cd ~/catkin_ws/

# 或使用返回上一级
cd ..

# 编译文件夹
catkin_make
catkin_make install

设置环境变量

gedit ~/.bashrc

在其中加入

# 每个人路径不同
source /opt/ros/noetic/setup.bash
source /home/work/catkin_ws/devel/setup.bash

1.2 安装usb_cam包

cd ~/catkin_ws/src
https://github.com/ros-drivers/usb_cam.git
cd ~/catkin_ws
catkin_make

1.3 配置usb_cam参数

cd ~/catkin_ws/src/usb_cam/config
gedit usb_cam.yml

需要修改的参数包括

video_device 相机的端口号
pixel_format 相机输出格式
image_width 长度
image_height 宽度
framerate 帧率

1.4 如何查看相机的分辨率和帧率

# 安装v4l工具
sudo apt install v4l-utils

# 列出所有设备
v4l2-ctl --list-devices

# 列出指定设备的预览支持格式
v4l2-ctl --list-formats-ext --device /dev/video0

其他用法请参考
https://blog.csdn.net/u012906122/article/details/126356698
https://www.mankier.com/1/v4l2-ctl

1.5 运行usb_cam

roslaunch usb_cam usb_cam-test.launch

打开rqt查看图像

rqt_image_view

2 相机标定

2.1 单目相机的标定

# 打开usb_cam
roslaunch usb_cam usb_cam-test.launch
# 开始标定,size是棋盘格数量,square是单个格子大小
rosrun camera_calibration cameracalibrator.py --size 6x7 --square 0.108 image:=/usb_cam/image_raw camera:=/usb_cam

打开标定界面后,请在视野中移动、倾斜标定板,直至左上角四个进度条都是绿色,此时CALIBRATE亮起,点击开始计算

之后点击SAVE保存,点击COMMIT退出

在保存路径中提取ost.yaml,放置在~/catkin_ws/src/usb_cam,修改usb_cam-test.launch

<launch>
  <arg name="image_view" default="false" />

  <node name="usb_cam" pkg="usb_cam" type="usb_cam_node" output="screen" >
      <rosparam command="load" file="$(find usb_cam)/config/usb_cam.yml"/>
      <param name="camera_info_url" type="string" value="file:///home/orin511/catkin_ws/src/usb_cam/ost.yaml" />
  </node>
  <node if="$(arg image_view)" name="image_view" pkg="image_view" type="image_view"
        respawn="false" output="screen">
    <remap from="image" to="/usb_cam/image_raw"/>
    <param name="autosize" value="true" />
  </node>
</launch>

2.2 双目相机的标定

暂无

3 使用april_tag包

3.1 安装apriltag包

sudo apt install apriltag

或者编译安装(不要放在ROS工作空间里

cd ~
git clone https://github.com/AprilRobotics/apriltag.git
cd apriltag
mkdir build
cd build
cmake ..  
make
sudo make install  #安装到ros中

3.2 安装apriltag_ros包

cd ~/catkin_ws/src
git clone https://github.com/AprilRobotics/apriltag_ros.git 
cd ~/catkin_ws
catkin_make

3.3 配置apriltag_ros包

配置launch文件

gedit continuous_detection.launch

修改camera_name/usb_cam
修改image_topicimage_raw

<launch>
  <!-- set to value="gdbserver localhost:10000" for remote debugging -->
  <arg name="launch_prefix" default="" />

  <!-- configure camera input -->
  <arg name="camera_name" default="/usb_cam" />
  <arg name="image_topic" default="image_raw" />
  <arg name="queue_size" default="1" />

  <!-- apriltag_ros continuous detection node -->
  <node pkg="apriltag_ros" type="apriltag_ros_continuous_node" name="apriltag_ros_continuous_node" clear_params="true" output="screen" launch-prefix="$(arg launch_prefix)">
    <!-- Remap topics from those used in code to those on the ROS network -->
    <remap from="image_rect" to="$(arg camera_name)/$(arg image_topic)" />
    <remap from="camera_info" to="$(arg camera_name)/camera_info" />

    <param name="publish_tag_detections_image" type="bool" value="true" /><!-- default: false -->
    <param name="queue_size" type="int" value="$(arg queue_size)" />

    <!-- load parameters (incl. tag family, tags, etc.) -->
    <rosparam command="load" file="$(find apriltag_ros)/config/settings.yaml"/>
    <rosparam command="load" file="$(find apriltag_ros)/config/tags.yaml"/>
  </node>
</launch>

配置settings.yaml

# AprilTag 3 code parameters
# Find descriptions in apriltag/include/apriltag.h:struct apriltag_detector
#                      apriltag/include/apriltag.h:struct apriltag_family
tag_family:        'tag36h11' # options: tagStandard52h13, tagStandard41h12, tag36h11, tag25h9, tag16h5, tagCustom48h12, tagCircle21h7, tagCircle49h12
tag_threads:       2          # default: 2
tag_decimate:      1.0        # default: 1.0
tag_blur:          0.0        # default: 0.0
tag_refine_edges:  1          # default: 1
tag_debug:         0          # default: 0
max_hamming_dist:  2          # default: 2 (Tunable parameter with 2 being a good choice - values >=3 consume large amounts of memory. Choose the largest value possible.)
# Other parameters
publish_tf:        true       # default: false
transport_hint:    "raw"      # default: raw, see http://wiki.ros.org/image_transport#Known_Transport_Packages for options

配置tags.yaml

# # Definitions of tags to detect
#
# ## General remarks
#
# - All length in meters
# - 'size' refers to the length of the shared border between solid black and solid white rectangle.
#   See README.md or https://github.com/AprilRobotics/apriltag/wiki/AprilTag-User-Guide#pose-estimation for details.
# - Ellipsis (...) signifies that the previous element can be repeated multiple times.
#
# ## Standalone tag definitions
# ### Remarks
#
# - name is optional
#
# ### Syntax
#
# standalone_tags:
#   [
#     {id: ID, size: SIZE, name: NAME},
#     ...
#   ]
standalone_tags:
  [
    {id: 0, size: 0.09, name: Lup},
    {id: 1, size: 0.09, name: Rup},
    {id: 2, size: 0.09, name: Ldown},
    {id: 3, size: 0.09, name: Rdown},
  ]
# ## Tag bundle definitions
# ### Remarks
#
# - name is optional
# - x, y, z have default values of 0 thus they are optional
# - qw has default value of 1 and qx, qy, qz have default values of 0 thus they are optional
#
# ### Syntax
#
# tag_bundles:
#   [
#     {
#       name: 'CUSTOM_BUNDLE_NAME',
#       layout:
#         [
#           {id: ID, size: SIZE, x: X_POS, y: Y_POS, z: Z_POS, qw: QUAT_W_VAL, qx: QUAT_X_VAL, qy: QUAT_Y_VAL, qz: QUAT_Z_VAL},
#           ...
#         ]
#     },
#     ...
#   ]
tag_bundles:
  [
  ]

3.4 使用apriltag_ros包

roslaunch usb_cam usb_cam-test.launch
roslaunch apriltag_ros continuous_detection.launch

使用RVIZ查看,需要视野内有tag,而且将坐标系选为相机

rviz

选择TF坐标系即可观察

3.5 Bug修复

在Rviz中有概率无法使用TF或image观察结果,一般表现为画面中出现tag时会卡死,这一问题目前无法解决,但可以进行缓解

https://github.com/AprilRobotics/apriltag_ros/issues/153

注释掉 continuous_detector.cpp 中的以下行可以缓解此问题,但不会发布 /tag_detections_image 主题。/tag_detections 和 /tf 主题工作正常。

if (draw_tag_detections_image_)
  {
    tag_detector_->drawDetections(cv_image_);
    tag_detections_image_publisher_.publish(cv_image_->toImageMsg());
  }
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Apriltag is a popular library for detecting and identifying visual fiducial markers called tags. The `apriltag_ros` package is a ROS wrapper for the Apriltag library, allowing you to use Apriltag functionalities within a ROS environment. It provides ROS nodes that can subscribe to image topics, detect Apriltags in the images, and publish the detected tag poses. To use `apriltag_ros` in Python, you can follow these steps: 1. Install the `apriltag_ros` package: ```bash sudo apt-get install ros-<distro>-apriltag-ros ``` Replace `<distro>` with your ROS distribution (e.g., melodic, noetic). 2. Create a ROS package (if you haven't already) where you'll write your Python code: ```bash cd ~/catkin_ws/src catkin_create_pkg my_apriltag_pkg rospy apriltag_ros ``` Replace `my_apriltag_pkg` with your desired package name. 3. In your Python script, import the necessary modules: ```python #!/usr/bin/env python import rospy from sensor_msgs.msg import Image from cv_bridge import CvBridge from apriltag_ros.msg import AprilTagDetectionArray ``` 4. Initialize the ROS node and create a subscriber to the image topic: ```python rospy.init_node('apriltag_detector') bridge = CvBridge() def image_callback(msg): cv_image = bridge.imgmsg_to_cv2(msg, desired_encoding='passthrough') # Perform Apriltag detection using cv_image image_sub = rospy.Subscriber('/camera/image_raw', Image, image_callback) ``` Replace `/camera/image_raw` with the appropriate image topic for your setup. 5. Process the received image in the callback function and publish the detected tag poses: ```python def image_callback(msg): cv_image = bridge.imgmsg_to_cv2(msg, desired_encoding='passthrough') # Perform Apriltag detection using cv_image # Publish the detected tag poses detection_array = AprilTagDetectionArray() # Populate detection_array with detected tag poses pub = rospy.Publisher('/apriltag/detections', AprilTagDetectionArray, queue_size=10) pub.publish(detection_array) ``` Replace `/apriltag/detections` with the desired topic to publish the detected tag poses. 6. Finally, run your ROS node: ```bash rosrun my_apriltag_pkg my_apriltag_node.py ``` Remember to replace `my_apriltag_pkg` and `my_apriltag_node.py` with your actual package and node names. This is a basic example to get you started with `apriltag_ros` in Python. You can find more information about the package and its functionalities in the official ROS documentation and the `apriltag_ros` GitHub repository.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值