这个任务老早就开始配环境了,但是gazebo的仿真环境一直没有配好,搁置了许久,今日清晨,茅塞顿开,顺利完成gazebo的配置并完成键盘控制,记录一下,方便后面学习。

效果图:通过键盘控制无人机运动及起飞、降落等基本操作。

在这里插入图片描述因为ardupilot仿真资料较少,官网提供了很大帮助,在gazebo仿真过程中,也是参考github官网的说明

ardupilot官网:
https://ardupilot.org/dev/docs/using-gazebo-simulator-with-sitl.html
ardupilot_gazebo网站:
https://github.com/khancyr/ardupilot_gazebo/blob/master/README.md

Ardupilot的配置可以参考我的这篇文章,重装几次系统都是按照这个配的。关于控制代码相关也可以翻阅我其他的文章。
Ardupilot飞控固件的仿真模拟
https://blog.csdn.net/weixin_44747240/article/details/104497893

Gazebo9安装

首先确保你的Ardupilot的SITL的2D仿真可以完成,并可以完成基本控制
首先装一下Gazebo,这里呢,我劝大家不用Ros自带的gazebo,因为我用自带gazebo配置总是不能通信,就卸载之后重新装了gazebo9,同样是参考官网代码:
添加源并更新

sudo sh -c 'echo "deb http://packages.osrfoundation.org/gazebo/ubuntu-stable `lsb_release -cs` main" > /etc/apt/sources.list.d/gazebo-stable.list'
wget http://packages.osrfoundation.org/gazebo.key -O - | sudo apt-key add -
sudo apt update

安装gezabo9和相关依赖

sudo apt install gazebo9 libgazebo9-dev

完成后打开出现一个空界面,即表示安装完成了。

gazebo --verbose

在这里插入图片描述

ardupilot_gazebo编译

接下来装ardupilot_gazebo并进行编译,依旧按照官方代码

git clone https://github.com/khancyr/ardupilot_gazebo
cd ardupilot_gazebo
mkdir build
cd build
cmake ..
make -j4
sudo make install

编译过程在make中出错
报错如下:

error: static assertion failed: boost::variant does not contain specified type U, call to boost::get<U>(boost::variant<T...>&) will always throw boost::bad_get exception
     BOOST_STATIC_ASSERT_MSG(
     ^
CMakeFiles/ArduPilotPlugin.dir/build.make:62: recipe for target 'CMakeFiles/ArduPilotPlugin.dir/src/ArduPilotPlugin.cc.o' failed
make[2]: *** [CMakeFiles/ArduPilotPlugin.dir/src/ArduPilotPlugin.cc.o] Error 1
CMakeFiles/Makefile2:104: recipe for target 'CMakeFiles/ArduPilotPlugin.dir/all' failed
make[1]: *** [CMakeFiles/ArduPilotPlugin.dir/all] Error 2
make[1]: *** 正在等待未完成的任务....
[ 75%] Linking CXX shared library libArduCopterIRLockPlugin.so
[ 75%] Built target ArduCopterIRLockPlugin
Makefile:127: recipe for target 'all' failed
make: *** [all] Error 2

这是ubuntu16.04的BOOST版本低导致的
解决

cd /usr/include/sdformat-6.2/sdf
sudo chmod a+w Param.hh
sudo gedit Param.hh

将里面的if语句修改如下

#if BOOST_VERSION < 105800
_value = boost::relaxed_get<T>(this->dataPtr->value);
#else
_value = boost::relaxed_get<T>(this->dataPtr->value);

然后解决,继续make,完成
在这里插入图片描述
继续安装,完成

sudo make install

在这里插入图片描述打开bashrc

gedit ~/.bashrc

添加内容

source /usr/share/gazebo/setup.sh
 #这里注意下和你实际的路径一致
export GAZEBO_MODEL_PATH=~/ardupilot_gazebo/models:${GAZEBO_MODEL_PATH}
export GAZEBO_MODEL_PATH=~/ardupilot_gazebo/models_gazebo:${GAZEBO_MODEL_PATH}
export GAZEBO_RESOURCE_PATH=~/ardupilot_gazebo/worlds:${GAZEBO_RESOURCE_PATH}

保存并source一下,完成

source .bashrc

运行仿真

四轴(3DR IRIS)

#先打开sitl,此时会提示link1 down,等待心跳
sim_vehicle.py -v ArduCopter -f gazebo-iris  -m --mav10 --map --console -I0
#再开gazebo
gazebo --verbose worlds/iris_arducopter_runway.world

此时完成了仿真搭建的过程。