《ROS机器人开发实践》功能包编译报错问题解决&&摄像头数据opencv

(一)创建工作空间

mkdir -p ~/catkin_ws/src
cd ~/catkin_ws/src
catkin_init_workspace

  工作空间创建完成,在根目录下,编译整个工作空间

cd ~/catkin_ws/
catkin_make

  此时,工作空间中自动生成两个文件夹:devel,build
  devel文件夹中产生几个setup.*sh形成的环境变量设置脚本,使用source命令运行这些脚本文件,则工作空间中的环境变量得以生效。

source devel/setup.sh

  使用命令查看环境是否设置成功:

echo $ROS_PACKAGE_PATH

  环境变量成功设置如下显示:

$ echo $ROS_PACKAGE_PATH
/home/wmj/catkin_ws/src:/opt/ros/kinetic/share

  如果希望环境变量再所有终端中有效,需要在终端配置文件中加入环境变量的设置:

echo "source ~/catkin_ws/devel/setup.bash">>~/.bashrc

  上面的意思就是将环境变量设置到/.bashrc文件中,我们使用以下方式打开更为清晰:

gedit ~/.bashrc

  在打开的文件最下面粘贴以下代码即可设置环境变量:

source ~/catkin_ws/devel/setup.bash

功能包

  一个功能包的目录下包含如下几个文件:cmakelists,package等文件,功能包不能嵌套放置,只能平行放置在代码空间中。
  package:描述功能包属性信息;
  cmakelists:记录功能包编译规则。
  创建功能包:

cd ~/catkin_ws/src
catkin_create_pkg learning std_msgs rospy roscpp

  创建完功能包,回到根目录,编译并设置环境变量:

cd ~/catkin_ws
catkin_make
source ~/catkin_ws/devel/setup.sh

注意:每次新增加了功能包,都需要重新编译才可以,否则无法使用新功能包。

(二)下载实例代码

git clone https://github.com/huchunxu/ros_exploring.git

  将实例代码中的功能包,都复制到工作空间中的src文件夹下,并编译:

(1)报错:

-- Could not find the required component 'ecto'. The following CMake error indicates that you either need to install the package with the same name or change your environment so that it can be found.
CMake Error at /opt/ros/kinetic/share/catkin/cmake/catkinConfig.cmake:83 (find_package):
  Could not find a package configuration file provided by "ecto" with any of
  the following names:

    ectoConfig.cmake
    ecto-config.cmake

  Add the installation prefix of "ecto" to CMAKE_PREFIX_PATH or set
  "ecto_DIR" to a directory containing one of the above files.  If "ecto"
  provides a separate development package or SDK, be sure it has been
  installed.
Call Stack (most recent call first):
  ork_tutorials/CMakeLists.txt:4 (find_package)


-- Configuring incomplete, errors occurred!
See also "/home/wmj/catkin_ws/build/CMakeFiles/CMakeOutput.log".
See also "/home/wmj/catkin_ws/build/CMakeFiles/CMakeError.log".
Invoking "cmake" failed

  以上错误代码表示某些功能包中缺少某些组件而形成的错误。
  这里是缺少数据包文件:‘ecto’,根据报错信息中表示缺相关依赖的组件,下面来安装相应的包:

sudo apt-get install ros-kinetic-ecto 
正在读取软件包列表... 完成
正在分析软件包的依赖关系树       
正在读取状态信息... 完成       
N: 忽略‘ros-latest.list’’(于目录‘/etc/apt/sources.list.d/’),鉴于它的文件扩展名无效
E: 无法定位软件包 ros-kinetic-ecto

  解决方法:

在etc/apt   的sources.list 添加镜像源   
deb http://archive.ubuntu.com/ubuntu/ trusty main universe restricted multiverse
deb http://packages.ros.org/ros/ubuntu xenial main
deb http://packages.ros.org/ros-shadow-fixed/ubuntu xenial main
注意:这里需要进入终端获取root身份,然后使用gedit打开更改才可有效保存(下面两个源是成功的)

然后 sudo apt-get update  接着安装就可以了

  成功代码:

wmj@wmj-Inspiron-5580:~$ sudo apt-get install ros-kinetic-ecto
正在读取软件包列表... 完成
正在分析软件包的依赖关系树       
正在读取状态信息... 完成       
将会同时安装下列软件:
  xdot
下列【新】软件包将被安装:
  ros-kinetic-ecto xdot
升级了 0 个软件包,新安装了 2 个软件包,要卸载 0 个软件包,有 7 个软件包未被升级。
需要下载 1,329 kB 的归档。
解压缩后会消耗 8,478 kB 的额外空间。
您希望继续执行吗? [Y/n] y

(2)我们再次编译工作空间,报错:

catkin_make
-- Could not find the required component 'manipulation_msgs'. The following CMake error indicates that you either need to install the package with the same name or change your environment so that it can be found.
CMake Error at /opt/ros/kinetic/share/catkin/cmake/catkinConfig.cmake:83 (find_package):
  Could not find a package configuration file provided by "manipulation_msgs"
  with any of the following names:

    manipulation_msgsConfig.cmake
    manipulation_msgs-config.cmake

  Add the installation prefix of "manipulation_msgs" to CMAKE_PREFIX_PATH or
  set "manipulation_msgs_DIR" to a directory containing one of the above
  files.  If "manipulation_msgs" provides a separate development package or
  SDK, be sure it has been installed.
Call Stack (most recent call first):
  marm_planning/CMakeLists.txt:10 (find_package)


-- Configuring incomplete, errors occurred!
See also "/home/wmj/catkin_ws/build/CMakeFiles/CMakeOutput.log".
See also "/home/wmj/catkin_ws/build/CMakeFiles/CMakeError.log".
Makefile:668: recipe for target 'cmake_check_build_system' failed
make: *** [cmake_check_build_system] Error 1
Invoking "make cmake_check_build_system" failed

和上边的问题类似,安装manipulation_msgs包即可:

sudo apt-get install ros-kinetic-manipulation-msgs 

有了上述源,这里不再出现无法定位软件包的错误,直接按装好了。

(3)我们再次编译工作空间,报错:

catkin_make
CMake Error at /opt/ros/kinetic/share/catkin/cmake/catkinConfig.cmake:83 (find_package):
  Could not find a package configuration file provided by
  "moveit_ros_perception" with any of the following names:

    moveit_ros_perceptionConfig.cmake
    moveit_ros_perception-config.cmake

  有了之前两次经验,我们这里很容易看出来,这里的报错原因是缺少组件moveit_ros_perception,改组件在包ros-kinetic-moveit-ros-planning-interface里:

sudo apt-get install ros-kinetic-moveit-ros-planning-interface

(4)我们再次编译工作空间,报错:

catkin_make
CMake Error at /opt/ros/kinetic/share/catkin/cmake/catkinConfig.cmake:83 (find_package):
  Could not find a package configuration file provided by "move_base_msgs"
  with any of the following names:

    move_base_msgsConfig.cmake
    move_base_msgs-config.cmake

  缺少move_base_msgs,在导航包navigation里面:

sudo apt-get install ros-kinetic-navigation

(5)我们再次编译工作空间,报错:

catkin_make
CMake Error at /opt/ros/kinetic/share/catkin/cmake/catkinConfig.cmake:83 (find_package):
  Could not find a package configuration file provided by
  "gazebo_ros_control" with any of the following names:

    gazebo_ros_controlConfig.cmake
    gazebo_ros_control-config.cmake

  安装ros-kinetic-gazebo-ros-control:

sudo apt-get install ros-kinetic-gazebo-ros-control

(6)我们再次编译工作空间,报错:

/usr/bin/ld: 找不到 -lmsc
collect2: error: ld returned 1 exit status
robot_voice/CMakeFiles/iat_publish.dir/build.make:165: recipe for target '/home/wmj/catkin_ws/devel/lib/robot_voice/iat_publish' failed
make[2]: *** [/home/wmj/catkin_ws/devel/lib/robot_voice/iat_publish] Error 1
CMakeFiles/Makefile2:1965: recipe for target 'robot_voice/CMakeFiles/iat_publish.dir/all' failed

[ 18%] Built target mrobot_bringup
Makefile:138: recipe for target 'all' failed
make: *** [all] Error 2
Invoking "make -j8 -l8" failed

  缺少链接库,根据《ROS机器人开发实践》书中介绍(科大讯飞那一块207页),将科大讯飞库文件拷贝到系统目录下,进入SDK根目录下的libs文件夹,选择相应的架构平台x64/x86,使用命令拷贝。

sudo cp libmsc.so /usr/lib/libmsc.so

(7)我们再次编译工作空间,报错:

Makefile:138: recipe for target 'all' failed
make: *** [all] Error 2
Invoking "make -j8 -l8" failed

这是权限问题,根据问题找原因。
进入 ros_exploring/ros_advanced/dynamic_tutorials/cfg 文件夹,打开终端。

chmod +x Tutorials.cfg

(8)再次使用catkin_make编译,成功:

[100%] Linking CXX executable /home/wmj/catkin_ws/devel/lib/action_tutorials/DoDishes_server
[100%] Built target DoDishes_client
[100%] Built target DoDishes_server
参考地址:
https://blog.csdn.net/sunstarss/article/details/88886426

(三)语音助手–科大讯飞SDK

(1)下载科大讯飞SDK

登录网址注册账户:

https://www.xfyun.cn/

登录后我们按照树上的教程一步步创建应用(204页)

(四)安装opencv库(计算机视觉库)

sudo apt-get install ros-kinetic-vision-opencv libopencv-dev python-opencv
N: 忽略‘ros-latest.list’’(于目录‘/etc/apt/sources.list.d/’),鉴于它的文件扩展名无效

(五)在ROS中使用opencv

  ROS提供了opencv的接口功能包——cv_bridge,使我们方便的调用opencv的各种功能,这就像是连接ROS与opencv的桥梁一样,开发者可做如下处理:
  (1)通过该功能包将ROS中的图像数据转换为opencv格式的图像,并调用各种opencv库进行图像处理。
  (2)通过该功能包将opencv处理过得数据,转化为ROS格式,通过话题发布,实现各节点之间的数据传输。
  以下例程,作用:一个ROS节点订阅摄像头驱动发布的图像消息,然后将其转换为opencv的图像格式进行显示,最后将opencv格式的图像转化为ROS图像消息进行发并显示。

roslaunch robot_vision usb_cam.launch
rosrun robot_vision cv_bridge_test.py
rqt_image_view

(1)roslaunch出现错误:

[usb_cam.launch] is neither a launch file in package [robot_vision] nor is [robot_vision] a launch file name
The traceback for the exception was written to the log file

roslaunch指令不识别robot_vision这个功能包,我们将下载好的配套代码功能包放入我们的ROS工作环境中即可,路径:~/ros_exploring/robot_perception/robot_vision,将功能包robot_vision整个复制到工作环境中的src目录中,再编译即可。

(2)roslaunch再次出现错误:

ERROR: cannot launch node of type [usb_cam/usb_cam_node]: usb_cam
ROS path [0]=/opt/ros/kinetic/share/ros
ROS path [1]=/home/wmj/catkin_ws/src
ROS path [2]=/opt/ros/kinetic/share

猜想可能是因为usb_cam功能包未正确安装,我们安装试试:

安装usb_cam

sudo apt-get install ros-kinetic-usb-cam

测试usb_cam

roslaunch usb_cam usb_cam-test.launch

摄像头实时捕获数据回传成功,代表usb_cam功能包安装成功。

(3)rosrun robot_vision cv_bridge_test.py出现错误

~$ rosrun robot_vision cv_bridge_test.py
[rosrun] Couldn't find executable named cv_bridge_test.py below /home/wmj/catkin_ws/src/robot_vision
[rosrun] Found the following, but they're either not files,
[rosrun] or not executable:
[rosrun]   /home/wmj/catkin_ws/src/robot_vision/scripts/cv_bridge_test.py

这个问题我们之前遇到过,这是由于权限问题:

需要在属性-权限中勾选 允许作为程序执行文件:
《ROS机器人开发实践》功能包编译报错问题解决&&摄像头数据opencv_第1张图片或者使用命令赋予可执行权限(这个上面(7)提到了):

chmod +x cv_bridge_test.py

赋予可执行文件的权限,上述命令则都可以正常运行。

以下几步也可以调用摄像头查看回传图像信息

(1)安装uvc camera功能包

sudo apt-get install ros-kinetic-uvc-camera

(2)安装image相关功能包

sudo apt-get install ros-kinetic-rqt-image-view

(3)运行uvc_camera节点

rosrun uvc_camera uvc_camera_node

(4)查看图像信息

1)使用image_view节点查看图像
rosrun image_view image_view image:=/image_raw
说明:最后面的附加选项“image:=/image_raw”是把话题列表中的话题以图像形式查看的选项。
(2)用rqt_image_view节点检查
rqt_image_view image:=/image_raw
(3)使用rviz查看
rviz
增加image,然后将[Image][Image Topic]的值更改为“/image_raw”。
使用apt-get安装的软件包好像只有执行程序,没有launch文件和节点源文件等等,所以采用了自建uvc-camera软件包更该参数。

(六)人脸识别应用

  opencv已经集成了人脸识别算法,我们无需重新开发该算法,只需调用opencv相应的接口即可实现人脸识别功能。
  打开摄像头:

roslaunch robot_vision usb_cam.launch

  启动人脸检测功能包:

roslaunch robot_vision face_detector.launch

  打开摄像头显示画面:

rqt_image_view

  第一句命令没问题,前面就已经成功尝试了,第二句命令又出现了问题:

NODES
  /
    face_detector (robot_vision/face_detector.py)

ROS_MASTER_URI=http://localhost:11311

ERROR: cannot launch node of type [robot_vision/face_detector.py]: can't locate node [face_detector.py] in package [robot_vision]
No processes to monitor
shutting down processing monitor...
... shutting down processing monitor complete

  这里还是权限问题,将目标文件的权限更改即可:

chmod +x face_detector.py

  运行后成功代码提示如下:

NODES
  /
    face_detector (robot_vision/face_detector.py)

ROS_MASTER_URI=http://localhost:11311

process[face_detector-1]: started with pid [1467]
[INFO] [1644047040.490020]: Face detector is started..
[INFO] [1644047040.490217]: Please subscribe the ROS image.

  在image-view中选择"/cv_bridge_image"即可获得该接口处理过的图像,我们可以看到,在获取到的图像上,我们的脸上会有一个矩形框,代表已经检测到了人脸。

人脸检测逻辑

  基于haar特征的级联分类器对象检测算法,为快速,可靠的人脸检测应用提供了一种有效的算法,算法大概流程:

haar特征及连分类器:有着大量的样本特征,比对人脸的源
cascade瀑布级联分类器:进行模式匹配,如正脸和侧脸
(1)获取图像并进行灰度转换,进行边缘处理与噪声过滤
(2)将图像缩小,分类器直方图均衡化,同时将匹配分类器放大相同倍数,直到匹配分类器大小大于检测图像
(3)根据cascade分类器中的不同类型分别进行匹配

你可能感兴趣的:(ROS学习过程,opencv,自动驾驶,linux,ubuntu)