对 U b u n t u 18.04 {\rm Ubuntu 18.04} Ubuntu18.04环境下的 R O S {\rm ROS} ROS的 m e l o d i c {\rm melodic} melodic版本安装 u s b − c a m {\rm usb-cam} usb−cam过程出现的两个常见问题提出解决方案。
1.问题1:usb-cam功能包编译时出现"未定义的引用"的问题
问题描述:把
u
s
b
−
c
a
m
{\rm usb-cam}
usb−cam功能包下载到工作空间进行编译时,出现很多的"未定义的引用"的错误,具体问题见下图所示:
2.解决方案
查看 u s b − c a m {\rm usb-cam} usb−cam功能包下的 C M a k e L i s t s . t x t {\rm CMakeLists.txt} CMakeLists.txt文件,其中 C M a k e L i s t s . t x t {\rm CMakeLists.txt} CMakeLists.txt文件具体内容如下代码块所示:
cmake_minimum_required(VERSION 2.8.12)
project(usb_cam)
## Find catkin macros and libraries
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
## is used, also find other catkin packages
find_package(catkin REQUIRED COMPONENTS cv_bridge image_transport roscpp std_msgs std_srvs sensor_msgs camera_info_manager)
# Find OpenCV
find_package(OpenCV REQUIRED)
## pkg-config libraries
find_package(PkgConfig REQUIRED)
pkg_check_modules(avcodec libavcodec REQUIRED)
pkg_check_modules(avutil libavutil REQUIRED)
pkg_check_modules(swscale libswscale REQUIRED)
###################################################
## Declare things to be passed to other projects ##
###################################################
## LIBRARIES: libraries you create in this project that dependent projects also need
## CATKIN_DEPENDS: catkin_packages dependent projects also need
## DEPENDS: system dependencies of this project that dependent projects also need
catkin_package(
INCLUDE_DIRS include
LIBRARIES ${PROJECT_NAME}
)
###########
## Build ##
###########
include_directories(include
${catkin_INCLUDE_DIRS}
${avcodec_INCLUDE_DIRS}
${swscale_INCLUDE_DIRS}
${avutil_INCLUDE_DIRS}
${OpenCV_INCLUDE_DIRS}
)
## Build the USB camera library
add_library(${PROJECT_NAME} src/usb_cam.cpp)
target_link_libraries(${PROJECT_NAME}
${avcodec_LINK_LIBRARIES}
${avutil_LINK_LIBRARIES}
${swscale_LINK_LIBRARIES}
${catkin_LIBRARIES}
${OpenCV_LIBS}
)
## Declare a cpp executable
add_executable(${PROJECT_NAME}_node nodes/usb_cam_node.cpp)
target_link_libraries(${PROJECT_NAME}_node
${PROJECT_NAME}
${avcodec_LINK_LIBRARIES}
${avutil_LINK_LIBRARIES}
${swscale_LINK_LIBRARIES}
${catkin_LIBRARIES}
${OpenCV_LIBS}
)
#############
## Install ##
#############
## Mark executables and/or libraries for installation
install(TARGETS ${PROJECT_NAME}_node ${PROJECT_NAME}
RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
)
## Copy launch files
install(DIRECTORY launch/
DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}/launch
FILES_MATCHING PATTERN "*.launch"
)
install(DIRECTORY include/${PROJECT_NAME}/
DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
FILES_MATCHING PATTERN "*.h" PATTERN "*.hpp"
)
分析 C M a k e L i s t s . t x t {\rm CMakeLists.txt} CMakeLists.txt文件可知在编译内容上,所依赖的库: a v c o d e c 、 s w s c a l e 、 a v u t i l {\rm avcodec、swscale、avutil} avcodec、swscale、avutil,使用 l o c a t e {\rm locate} locate命令找出路径:
###########
## Build ##
###########
include_directories(include
${catkin_INCLUDE_DIRS}
${avcodec_INCLUDE_DIRS}
${swscale_INCLUDE_DIRS}
${avutil_INCLUDE_DIRS}
${OpenCV_INCLUDE_DIRS}
)
## Build the USB camera library
add_library(${PROJECT_NAME} src/usb_cam.cpp)
target_link_libraries(${PROJECT_NAME}
${avcodec_LINK_LIBRARIES}
${avutil_LINK_LIBRARIES}
${swscale_LINK_LIBRARIES}
${catkin_LIBRARIES}
${OpenCV_LIBS}
)
## Declare a cpp executable
add_executable(${PROJECT_NAME}_node nodes/usb_cam_node.cpp)
target_link_libraries(${PROJECT_NAME}_node
${PROJECT_NAME}
${avcodec_LINK_LIBRARIES}
${avutil_LINK_LIBRARIES}
${swscale_LINK_LIBRARIES}
${catkin_LIBRARIES}
${OpenCV_LIBS}
)
# 在终端下分别locate出三个库的具体路径
locate libavcodec
locate libavutil
locate libswscale
得出具体路径后,在 C M a k e L i s t s . t x t {\rm CMakeLists.txt} CMakeLists.txt文件写入路径,见下面代码块:
## Declare a cpp executable
add_executable(${PROJECT_NAME}_node nodes/usb_cam_node.cpp)
# CMakeLists.txt文件新增路径部分
# ====================================================================
set(avcodec_LINK_LIBRARIES /usr/lib/x86_64-linux-gnu/libavcodec.so)
set(avutil_LINK_LIBRARIES /usr/lib/x86_64-linux-gnu/libavutil.so)
set(swscale_LINK_LIBRARIES /usr/lib/x86_64-linux-gnu/libswscale.so)
# ====================================================================
target_link_libraries(${PROJECT_NAME}_node
${PROJECT_NAME}
${avcodec_LINK_LIBRARIES}
${avutil_LINK_LIBRARIES}
${swscale_LINK_LIBRARIES}
${catkin_LIBRARIES}
${OpenCV_LIBS}
)
修改
C
M
a
k
e
L
i
s
t
s
.
t
x
t
{\rm CMakeLists.txt}
CMakeLists.txt文件后,返回工作空间进行编译,如下图所示:
3.问题2:usb-cam安装好后出现"找不到video0"的问题
问题描述:解决问题1后,成功进行编译,但在打开摄像头的时候出现找不到
V
i
d
e
o
0
{\rm Video0}
Video0的问题,具体如下图所示:
4.解决方案
# 1.关闭虚拟机,进入编辑虚拟机设置;
# 2.在"USB控制器选项"的"显示所有USB设备"前打上√;
# 3.启动虚拟机;
# 4.在虚拟机选项-->可移动设备-->USB 2.0 HD UVC Web Cam-->点击"连接";
# 5.查看是否显示video0;
cd /dev/
ls