写在前面的话:

一定要确保mavros安装成功。我在安装mavros的时候采用的是官网默认的安装方式,期间一定有Error出现,没有成功,导致后面需要补充安装一些东西。验证是否安装成功,可以在本地运行roslaunch mavros px4.launch

注:

  1. 默认的mavros源码是安装在catkin_ws文件夹下,可以 首先采用 catkin clean 清除源码工程,然后采用catkin build(不是catkin make)进行编译。如果源码正常编译且环境变量设置正确的话,在其他地方创建ROS工程添加mavros依赖是不会报错的,否则会出现没有找到mavros功能包的错误。(如果显示catkin build没有这个指令,则运行sudo apt-get install python-catkin-tools进行安装即可)
  2. ROS工程中生成的 可执行文件 放在 devel/lib下面。
  3. ROS工程下cpp文件要在 Cmakelist中添加依赖,如下图所示。其中第一句话表示,查找src/offnode.cpp文件进行编译,生成的可执行文件名字为{PROJECT_NAME}. 在本次实验中,如下图2所示,project name 为 px4_simu。所以,生成的可执行文件为 px4_simu_node,参见下图3

在这里插入图片描述
在这里插入图片描述在这里插入图片描述

4.sudo apt-get install ros-kinetic-geographic-msgs
在这里插入图片描述

Could not find the required component 'geographic_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 "geographic_msgs"
  with any of the following names:
 
    geographic_msgsConfig.cmake
    geographic_msgs-config.cmake
 
  Add the installation prefix of "geographic_msgs" to CMAKE_PREFIX_PATH or
  set "geographic_msgs_DIR" to a directory containing one of the above files.
  If "geographic_msgs" provides a separate development package or SDK, be
  sure it has been installed.

紧接着编译仍然报错,

Could NOT find GeographicLib (missing: GeographicLib_LIBRARIES GeographicLib_INCLUDE_DIRS)

然后安装了

sudo apt-get install libgeographic-dev

当Ubuntu install某个东西时,给出下面提示

E: Could not get lock /var/lib/dpkg/lock-frontend - open (11: Resource temporarly unavailable)
E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), is an other process using it?

删除锁定文件,行了

sudo rm /var/lib/dpkg/lock-frontend       
sudo rm /var/lib/dpkg/lock

在重新编译mavros的时候,遇到下面的这个错误,当时就运行catkin_make_isolated了,当然这是不对的。改成catkin build

CMake Error at /opt/ros/kinetic/share/catkin/cmake/catkin_workspace.cmake:95 (message):
This workspace contains non-catkin packages in it, and catkin cannot build a non-homogeneous workspace without isolation. Try the 'catkin_make_isolated' command instead.
Call Stack (most recent call first):
CMakeLists.txt:68 (catkin_workspace)

此时运行 roslaunch mavros px4_2.launch 时,可能会出现如下错误 (下图所示,转载的图片)

UAS:GeographicLib exception: File not readable 
/usr/share/GeographicLib/geoids/egm96-5.pgm | 
Run install_geographiclib_dataset.sh script in 
order to install Geoid Model dataset!

在这里插入图片描述
解决方法:
mavros 工程下找到 install_geographiclib_datasets.sh,并采用sudo 运行。

  1. 参照添加链接描述 执行
sudo apt-get install geographiclib-* ros-kinetic-geographic-*

下面展示如何从零开发mavros! 终于进入正题了

  1. 首先创建Ros工作空间。
    在期望的路径上创建文件夹,比如这里我们采用如下指令创建一个空的文件夹
mkdir Ros_simulation

然后,进入该文件夹后创建src文件夹,指令同上

cd Ros_simulation
mkdir src
catkin_init_workspace 

此时,就可以返回Ros_simulation文件夹下完成后续工作空间的创建,指令为:

cd ..
catkin_make

完成之后,再次使用

 catkin_make install

完成整个工作空间的创建,创建之后如下图所示:

在这里插入图片描述

  1. 创建Ros功能包
    在src文件夹下,执行指令
 catkin_create_pkg px4_simu rospy roscpp std_msgs mavros

创建Ros功能包,其中px4_simu为功能包名称,后面三个参数为依赖。分别表示python支持,cpp支持,ROS消息支持以及mavros。
3. 然后返回工程文件夹进行编译

catkin_make

最后通过如下指令声明一下路径:

source devel/setup.bash  #该语句定位到工程文件夹下devel文件夹
echo $ROS_PACKAGE_PATH
  1. 在ROS功能包(本文中为px4_simu)下创建源文件,路径为px4_simu/src,使用指令

touch offnode.cpp

  1. 我这里将px4官网CPP代码直接复制进来,链接为单击这里,代码具体解释官网以及CSDN上有很多,在此不再赘述。ctrl+c ctrl+v之后,保存,在Cmakelist中添加依赖,具体如上图1所示。然后编译即可。
  2. 最后启动需要先启动gazebo:
make px4_sitl gazebo 

然后启动mavros:

roslaunch mavros px4.launch fcu_url:="udp://:14540@127.0.0.1:14557"

最后在启动Ros节点:

rosrun px4_simu px4_simu_node

就可以看到gazebo下四轴在offboard模式下起飞了。

后续

这次实验中,打开的终端为3个,下一步将考虑采用roslaunch的方式进行整合。