一、Linux/ROS Nodes单机仿真

1.安装ROS-Kinetic(参考​​http://wiki.ros.org/kinetic/Installation/Ubuntu​​​)

1.1 添加软件源

sudo sh -c ‘echo “deb http://packages.ros.org/ros/ubuntu (lsbrelease−sc)main”>/etc/apt/sources.list.d/ros−latest.list′1.2安装密钥sudoapt−keyadv–keyserverhkp://ha.pool.sks−keyservers.net:80–recv−key421C365BD9FF1F717815A3895523BAEEB01FA1161.3更新软件源sudoapt−getupdate1.4安装ROS−Kineticsudoapt−getinstallros−kinetic−desktop−full1.5初始化rosdepsudorosdepinitrosdepupdate1.6环境变量设置echo“source/opt/ros/kinetic/setup.bash”>> /.bashrcsource /.bashrc1.7创建ROS工作空间mkdir−p /catkinws/srccd /catkinws/catkinmake1.8检查是否安装成功sourcedevel/setup.bashecho


若结果是:/home/catkin_ws/src:/opt/ros/kinetic/share:/opt/ros/kinetic/stacks 则表明一切正常,可以继续

2.安装MAVROS(参考​​https://dev.px4.io/en/ros/mavros_installation.html​​​)

2.1安装mavros

sudo apt-get install ros-kinetic-mavros ros-kinetic-mavros-extras

wget ​​https://raw.githubusercontent.com/mavlink/mavros/master/mavros/scripts/install_geographiclib_datasets.sh​​​

./install_geographiclib_datasets.sh

2.2下载mavros源码

cd ~/catkin_ws/src

git clone ​​https://github.com/mavlink/mavros.git​​​

2.3编译mavros

cd ~/catkin_ws

catkin_make

3.安装Gazebo(参考​​https://dev.px4.io/en/setup/dev_env_linux.html#jmavsimgazebo-simulation​​​)

3.1安装Gazebo和jMAVSim仿真器

下载ubuntu_sim.sh(​​https://raw.githubusercontent.com/PX4/Devguide/master/build_scripts/ubuntu_sim.sh​​​)

source ubuntu_sim.sh

3.2安装Gazebo在ROS上的开发工具链

下载ubuntu_sim_ros_gazebo.sh(​​https://raw.githubusercontent.com/PX4/Devguide/master/build_scripts/ubuntu_sim_ros_gazebo.sh​​​)

source ubuntu_sim_ros_gazebo.sh

4.编写外部控制程序

4.1 建立一个外部控制程序包

这个程序包依赖于roscpp 、mavros以及geometry_msgs。可以理解为一个ROS的一个节点

pip install catkin_create_pkg #不一定需要

cd ~/catkin_ws/src/

catkin_create_pkg offb roscpp mavros geometry_msgs

4.2 外部控制程序

cd ~/catkin_ws/src/offb/src/

gedit offb_node.cpp

将官网的示例程序​​https://dev.px4.io/en/ros/mavros_offboard.html​​​复制粘贴进来或者编写自己的控制程序

4.3 修改Cmakelists

cd ~/catkin_ws/src/offb/

gedit CMakeLists.txt

修改后##build##部分如下所示#####

Build ##

#####

Specify additional locations of header files

Your package locations should be listed before other locations include_directories(

include ${catkin_INCLUDE_DIRS}

)


Declare a C++ library

add_library(${PROJECT_NAME}

src/${PROJECT_NAME}/offb.cpp

)

Add cmake target dependencies of the library

as an example, code may need to be generated before libraries

either from message generation or dynamic reconfigure

add_dependencies(PROJECTNAMEPROJECTNAME{undefined{PROJECT_NAME}_EXPORTED_TARGETS}{PROJECT_NAME}_EXPORTED_TARGETS}{catkin_EXPORTED_TARGETS})

Declare a C++ executable

With catkin_make all packages are built within a single CMake context

The recommended prefix ensures that target names across packages don’t collide

add_executable(offb_node src/offb_node.cpp)


Rename C++ executable without prefix

The above recommended prefix causes long target names, the following renames the

target back to the shorter version for ease of user use

e.g. “rosrun someones_pkg node” instead of “rosrun someones_pkg someones_pkg_node”

set_target_properties(${PROJECT_NAME}_node PROPERTIES OUTPUT_NAME node PREFIX “”)

Add cmake target dependencies of the executable

same as for the library above

add_dependencies(PROJECTNAMEnodePROJECTNAMEnode{undefined{PROJECT_NAME}_EXPORTED_TARGETS}{PROJECT_NAME}_EXPORTED_TARGETS}{catkin_EXPORTED_TARGETS})

Specify libraries to link a library or executable target against target_link_libraries(offb_node


${catkin_LIBRARIES}

5.仿真测试

5.1 编译

cd ~/catkin_ws

catkin_make

5.2 开始仿真

打开终端1,启动gazebo仿真:

cd ~/src/Firmware

make posix_sitl_default gazebo

打开终端2,运行mavros

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

打开终端3,启用外部控制程序

source ~/catkin_ws/devel/setup.bash

rosrun offb offb_node

二、多机仿真(参考​​http://wiki.ros.org/hector_quadrotor​​​)

1.安装

mkdir -p hector_quadrotor_tutorial/src cd hector_quadrotor_tutorial wstool init src ​​https://raw.github.com/tu-darmstadt-ros-pkg/hector_quadrotor/kinetic-devel/tutorials.rosinstall​​​

2.编译

cd hector_quadrotor_tutorial

catkin_make

3.编写多机模型(四架)

cd ~/hector_quadrotor_tutorial/src/hector_quadrotor/hector_quadrotor_gazebo/launch/

gedit spawn_four_quadrotor.launch

编写模型代码