ROS系统读取USB相机图像数据
- 前言
- usb_cam 功能包下载与编译
- 摄像头选择
- 连接摄像头
- 可配置参数
前言
usb_cam功能包简介
为了丰富机器人与外界的交互方式,已经增加了与机器人的语音交互方式,不仅使机器人能够说话发声,还能听懂我们说的话,但是如果只有语音交互的话机器人就是一个盲人,无法看到这个色彩斑斓的大千世界,因此我们就需要为机器人增加视觉识别功能。现在市面上最常见的还是USB摄像头,物美价廉,要想使USB摄像头在ROS下正常工作,我们就需要一个软件包来支持,现在ROS下最常用的usb摄像头软件包就是usb_cam了,简单理解该软件包就是V4L(Video for Linux)USB摄像头驱动在ROS在的一个移植版本。截止到目前为止该软件包在indigo和jade版本上还处于维护状态,对于kinetic版本及其以上还未有维护,当然虽然没有维护但是在kinetic版本上也可以工作。
usb_cam 功能包下载与编译
系统环境:ubuntu20.04
ROS版本:noetic
usb_cam功能包可以通过github下载
git clone https://github.com/bosch-ros-pkg/usb_cam.git usb_cam
下载完成后提示:
正克隆到 ‘usb_cam’…
remote: Enumerating objects: 2232, done.
remote: Counting objects: 100% (2232/2232), done.
remote: Compressing objects: 100% (1015/1015), done.
remote: Total 2232 (delta 1033), reused 2082 (delta 988), pack-reused 0
接收对象中: 100% (2232/2232), 843.71 KiB | 306.00 KiB/s, 完成.
处理 delta 中: 100% (1033/1033), 完成.
拷贝到自己的ROS工作空间进行编译
catkin_make
编译报错:
– Checking for module ‘libv4l2’
– No package ‘libv4l2’ found
CMake Error at /usr/share/cmake-3.16/Modules/FindPkgConfig.cmake:463 (message):
A required package was not found
Call Stack (most recent call first):
/usr/share/cmake-3.16/Modules/FindPkgConfig.cmake:643 (_pkg_check_modules_internal)
usb_cam/CMakeLists.txt:10 (pkg_check_modules)
原因就是在usb_cam功能包的CMakeLists.txt的第10行是:
pkg_check_modules(video4linux libv4l2 REQUIRED)
系统中没有找到这个包,所以报错了。
sudo apt-get install libv4l2-dev
出现无法定位软件包,则需要更换镜像源
我换了源也不行,可能换源没有成功
sudo apt-get install libv4l-dev
成功了
获取:1 http://mirrors.aliyun.com/ubuntu focal/main amd64 libv4l2rds0 amd64 1.18.0-2build1 [15.8 kB]
获取:2 http://mirrors.aliyun.com/ubuntu focal/main amd64 libv4l-dev amd64 1.18.0-2build1 [108 kB]
已下载 124 kB,耗时 1秒 (221 kB/s)
正在选中未选择的软件包 libv4l2rds0:amd64。
(正在读取数据库 … 系统当前共安装有 361571 个文件和目录。)
准备解压 …/libv4l2rds0_1.18.0-2build1_amd64.deb …
正在解压 libv4l2rds0:amd64 (1.18.0-2build1) …
正在选中未选择的软件包 libv4l-dev:amd64。
准备解压 …/libv4l-dev_1.18.0-2build1_amd64.deb …
正在解压 libv4l-dev:amd64 (1.18.0-2build1) …
正在设置 libv4l2rds0:amd64 (1.18.0-2build1) …
正在设置 libv4l-dev:amd64 (1.18.0-2build1) …
正在处理用于 libc-bin (2.31-0ubuntu9.9) 的触发器 …
再次编译
catkin_make
顺利成功
摄像头选择
摄像头要选择:
- usb 接口
- 支持免驱协议:USB Video Class (UVC)
- 支持的操作系统:Linux with UVC (above linux-2.6)
连接摄像头
首先先通过 下面指令看下本地是否有摄像头,有几个
ls /dev/video*
这代表有两个摄像头(一个摄像头出两个/dev/video*)
所以再连接摄像头后,会挂载到 /dev/video4下面
修改usb_cam功能包下面的config文件夹下的 usb_cam.yml文件中的
video_device: /dev/video0
改为
video_device: /dev/video4
启动功能包
roslaunch usb_cam test_img_view.launch
实际场景:
采集到的摄像头图像:
查看下当前topic
rostopic list
/image_view/output
/image_view/parameter_descriptions
/image_view/parameter_updates
/rosout
/rosout_agg
/usb_cam/camera_info
/usb_cam/image_raw
/usb_cam/image_raw/compressed
/usb_cam/image_raw/compressed/parameter_descriptions
/usb_cam/image_raw/compressed/parameter_updates
/usb_cam/image_raw/compressedDepth
/usb_cam/image_raw/compressedDepth/parameter_descriptions
/usb_cam/image_raw/compressedDepth/parameter_updates
/usb_cam/image_raw/theora
/usb_cam/image_raw/theora/parameter_descriptions
/usb_cam/image_raw/theora/parameter_updates
输出频率为30hz
rostopic hz /usb_cam/image_raw/theora
subscribed to [/usb_cam/image_raw/theora]
average rate: 32.730
min: 0.000s max: 0.041s std dev: 0.01037s window: 33
average rate: 31.137
min: 0.000s max: 0.047s std dev: 0.00844s window: 62
average rate: 30.616
min: 0.000s max: 0.047s std dev: 0.00748s window: 92
可配置参数
start_service_name: "start_capture" # Defines name suffix for std_srvs::Empty service which restarts suspended streaming
stop_service_name: "stop_capture" # Defines name suffix for std_srvs::Empty service which suspends camera polling timer# 改成自己设备的挂载位置
video_device: /dev/video4 # Device driver's entrypoint
# 根据自己摄像头选择
io_method: mmap # I/O method# - read - for devices supporting virtual filesystem or block I/O# - mmap - for devices with direct libusb memory mapping# - userptr - for userspace devices supporting userspace pointer exchange
# 图像编码格式
pixel_format: yuyv # Pixel format for Video4linux device (also selects decoder mode)# https://wiki.videolan.org/YUV#YUV_4:2:0_.28I420.2FJ420.2FYV12.29# - yuyv - YUV420# - yuv - synonym for yuyv# - uyvy - UVY240# - yuvmono10 - Monochrome 10-bit pseudo-YUV# - rgb24 - Linear 8-bit RGB# - bgr24 - OpenCV-compatible 8-bit BGR# - grey - Grayscale 8-bit monochrome# - yu12 - YU-reversed YUV420# - mjpeg - FFMPEG decoder, MotionJPEG, for compatible hardware# - h264 - FFMPEG decoder, H.264, for compatible hardware
color_format: yuv422p # On-chip color representation mode for the input frame encoded by hardware# - yuv422p - YUV422 - default, compatible with most MJPEG hardware encoders# - yuv420p - YUV420 - mandatory for H.264 and H.265 hardware encoders
create_suspended: false # Instructs the node whether to start streaming immediately after launch# or to wait until the start service will be triggered
full_ffmpeg_log: false # Allows to suppress warning messages generated by libavcodec, cleans log
camera_name: head_camera # ROS internal name for the camera, used to generate camera_info message
# 发布topic中的frame_id
camera_frame_id: head_camera # Frame ID used to generate coordinate transformations
# 发布的话题名称
camera_transport_suffix: image_raw # Suffix used by image_transport to generate topic names
camera_info_url: "" # URI for camera calibration data (likely a YML file obtained from camera_calibration)
# 根据相机的参数设置图像的宽、高
image_width: 640 # Frame dimensions, should be supported by camera hardware
image_height: 480
# 发布频率
framerate: 30 # Camera polling frequency, Hz (integer)# Auxiliary camera parameters provided by libv4l2.# Names for these parameters are generated automatically according to the intrinsic control names exported by the# camera driver. The node queries camera's kernel controller module to determine the parameters that can be set up# via ROS. For these parameters the corresponding ROS parameters with identical names are generated under this# namespace.# See also the comprehensive node output describing parameter names and feasible values for them to be set up here.# It is also possible to have a list of the available control names using v4l2-ctl application from v4l2-util package:# v4l2-ctl --device=/dev/video<ID> -L
intrinsic_controls: focus_auto: trueexposure_auto_priority: trueexposure_auto: 3white_balance_temperature_auto: truepower_line_frequency: 1ignore: [brightness,contrast,saturation,gain,sharpness,backlight_compensation,white_balance_temperature,exposure_absolute,pan_absolute,tilt_absolute,focus_absolute,zoom_absolute] # Use this list to enumerate the control names that should be delisted from the camera setup# NOTE: the ROS parameters for the V4L controls supported but listed here would be STILL# generated, but the values WILL NOT BE USED to set up the camera. To affect these controls# once you want to do that, their names should me REMOVED from this list!