Ubunutu18.04 ROS melodic 无人机 XTDrone PX4 仿真平台配置

一、依赖安装

sudo apt install ninja-build exiftool ninja-build protobuf-compiler libeigen3-dev genromfs xmlstarlet libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev python-pip python3-pip gawk
pip2 install pandas jinja2 pyserial cerberus pyulog==0.7.0 numpy toml pyquaternion empy pyyaml 
pip3 install packaging numpy empy toml pyyaml jinja2 pyargparse

安装catkin-tools:(之后可以单独编译工作空间中的功能包,便于排查错误)

sudo apt install python3-catkin-tools

新建一个工作空间:

mkdir -p ~/catkin_ws/src
mkdir -p ~/catkin_ws/scripts
cd catkin_ws && catkin init 
catkin build

 二、Gazebo重新安装

2.1卸载之前的gazebo

sudo apt-get remove gazebo*
sudo apt-get remove libgazebo*
sudo apt-get remove ros-melodic-gazebo*

2.2安装gazebo 9.1

(1)设置计算机以接受来自package.osrfoundation.org的软件
sudo sh -c 'echo "deb http://packages.osrfoundation.org/gazebo/ubuntu-stable `lsb_release -cs` main" > /etc/apt/sources.list.d/gazebo-stable.list'
cat /etc/apt/sources.list.d/gazebo-stable.list

 #出现deb http://packages.osrfoundation.org/gazebo/ubuntu-stable xenial main表示没问题

(2)设置密钥
wget https://packages.osrfoundation.org/gazebo.key -O - | sudo apt-key add -
sudo apt-get update
 (3)安装gazebo9.1
sudo apt-get install gazebo9=9.1*
sudo apt install ros-melodic-gazebo9-*
sudo apt install ros-melodic-gazebo-*
(4)XTDrone中对Gazebo的ROS插件做了修改,因此需要源码编译 :

安装依赖:

sudo apt-get install ros-melodic-moveit-msgs ros-melodic-object-recognition-msgs ros-melodic-octomap-msgs ros-melodic-camera-info-manager  ros-melodic-control-toolbox ros-melodic-polled-camera ros-melodic-controller-manager ros-melodic-transmission-interface ros-melodic-joint-limits-interface

 XTDrone源码下载

git clone https://gitee.com/robin_shaun/XTDrone.git
cd XTDrone
git submodule update --init --recursive

安装编译插件

cd ~/catkin_ws
cp -r ~/XTDrone/sitl_config/gazebo_ros_pkgs src/
catkin build 

之后验证是否安装成功:

roscore
source ~/catkin_ws/devel/setup.bash
rosrun gazebo_ros gazebo

三、MAVROS安装

在安装之前,请先更新软件库:

sudo apt-get update
sudo apt-get upgrade

开始安装:

sudo apt install ros-melodic-mavros ros-melodic-mavros-extras 		
wget https://raw.githubusercontent.com/mavlink/mavros/master/mavros/scripts/install_geographiclib_datasets.sh
sudo chmod a+x ./install_geographiclib_datasets.sh
sudo ./install_geographiclib_datasets.sh #这步需要装一段时间

四、PX4配置

git clone https://github.com/PX4/PX4-Autopilot.git
mv PX4-Autopilot PX4_Firmware
cd PX4_Firmware
git checkout -b xtdrone/dev v1.11.0-beta1
git submodule update --init --recursive
sudo make px4_sitl_default gazebo

编译完成后,会弹出Gazebo界面,将其关闭即可。

gedit ~/.bashrc

之后打开~/.bashrc,加入以下代码,注意路径匹配,前两个source顺序不能颠倒。 

source ~/catkin_ws/devel/setup.bash
source ~/PX4_Firmware/Tools/setup_gazebo.bash ~/PX4_Firmware/ ~/PX4_Firmware/build/px4_sitl_default
export ROS_PACKAGE_PATH=$ROS_PACKAGE_PATH:~/PX4_Firmware
export ROS_PACKAGE_PATH=$ROS_PACKAGE_PATH:~/PX4_Firmware/Tools/sitl_gazebo

之后再source一下:

source ~/.bashrc

然后运行如下命令,此时会启动Gazebo,如下图所示。

cd ~/PX4_Firmware
roslaunch px4 mavros_posix_sitl.launch

 

 但是大概率会报这个错:

ERROR [px4] is_server_running: failed to create lock file: /tmp/px4_lock-0, reason=Permission denied
ERROR [px4_daemon] error binding socket /tmp/px4-sock-0, error = Address already in use
ERROR [px4] is_server_running: failed to create lock file: /tmp/px4_lock-0, reason=Permission denied
ERROR [px4] Failed to communicate with daemon: Permission denied

原因是锁的权限不够。注意下面的usename 是自己的用户名

cd /tmp
sudo chmod 777 px4_lock-0
sudo chmod 777 px4-sock-0
sudo chown username px4_lock-0
sudo chown username px4-sock-0

 成功打开gazebo后,我们检查与mavros连接是否正常

rostopic echo /mavros/state

 若connected: True,则说明MAVROS与SITL通信成功。如果是false,一般是因为.bashrc里的路径写的不对,仔细检查。

五、安装地面站QGroundControl

sudo usermod -a -G dialout $USER
sudo apt-get remove modemmanager -y
sudo apt install gstreamer1.0-plugins-bad gstreamer1.0-libav gstreamer1.0-gl -y

下载QGroundControl.AppImage
下载完成后,进入到QGroundControl.AppImage相应的目录,修改QGroundControl.AppImage权限: 

chmod +x ./QGroundControl.AppImage

然后双击QGroundControl.AppImage或者在终端切换到QGroundControl.AppImage目录后,输入以下命令即可打开地面站:

./QGroundControl.AppImage

 六、XTDrone替换模型

cp sensing/gimbal/gazebo_gimbal_controller_plugin.cpp ~/PX4_Firmware/Tools/sitl_gazebo/src/
cp sitl_config/init.d-posix/rcS ~/PX4_Firmware/ROMFS/px4fmu_common/init.d-posix/
cp sitl_config/worlds/* ~/PX4_Firmware/Tools/sitl_gazebo/worlds/
cp -r sitl_config/models/* ~/PX4_Firmware/Tools/sitl_gazebo/models/ 
cp -r sitl_config/launch/* ~/PX4_Firmware/launch/
cd ~/.gazebo/models/
rm -r stereo_camera/ 3d_lidar/ 3d_gpu_lidar/ hokuyo_lidar/

由于修改了PX4 sitl_gazebo中的gazebo_gimbal_controller_plugin.cpp(源代码不能控制多无人机的云台),要再编译一次。

cd ~/PX4_Firmware
sudo make px4_sitl_default gazebo

每次编译PX4后都要重新添加锁的权限:(username是自己的用户名)

cd /tmp
sudo chmod 777 px4_lock-0
sudo chmod 777 px4-sock-0
sudo chown username px4_lock-0
sudo chown username px4-sock-0

七、键盘控制飞行

source ~/.bashrc
cd ~/PX4_Firmware
roslaunch px4 indoor1.launch

Gazebo启动后,在另一个终端运行(注意要等Gazebo完全启动完成,或者可能脚本会报错)

cd ~/XTDrone/communication/
python multirotor_communication.py iris 0

终端出现: iris_0: communication initialized ,则表示与0号iris成功建立通信,之后在另一个终端运行 

cd ~/XTDrone/control/keyboard
python multirotor_keyboard_control.py iris 1 vel

 此时上面的终端出现:iris_0:HOVER,则表示准备成功

起飞方式:

  • t  解锁
  • i  不断加速z方向的速度直到 > 0.3
  • b 起飞至适合的高度
  • s 悬停

你可能感兴趣的:(无人机,PX4,无人机,SLAM,ubuntu,c++)