PX4在ROS-Kinetic外部控制下的仿真(1)

  • 1安装ROS-Kinetic
    • 1.1
    • 1.2 添加软件源
    • 1.3 安装密钥
    • 1.4 更新软件源
    • 1.5 安装ROS-Kinetic4
    • 1.6 初始化rosdep
    • 1.7 环境变量设置
    • 1.8 创建ROS工作空间
    • 1.9 检查是否安装成功
  • 2安装MAVROS包5
  • 3编写外部控制程序
    • 3.1 建立一个外部控制程序包
    • 3.2 外部控制程序
    • 3.3 修改Cmakelists
  • 4 编译测试
    • 4.1 编译
    • 4.2 开始仿真

官方文档是基于ROS-indigo作的介绍,ubuntu16.04下只能安装kinetic版本的ROS,亲身试坑后留下一些经验教训。
主要参考了PX4开发文档1,ROS官方文档2和相关博客步骤如下3:
步骤如下:

1安装ROS-Kinetic

1.1PX4在ROS-Kinetic外部控制下的仿真(1)_第1张图片

这几个全勾上

1.2 添加软件源

sudo sh -c 'echo "deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list'

1.3 安装密钥

sudo apt-key adv --keyserver hkp://ha.pool.sks-keyservers.net:80 --recv-key 421C365BD9FF1F717815A3895523BAEEB01FA116

1.4 更新软件源

sudo apt-get update

1.5 安装ROS-Kinetic4

sudo apt-get install ros-kinetic-desktop-full

1.6 初始化rosdep

sudo rosdep init
rosdep update

1.7 环境变量设置

echo "source /opt/ros/kinetic/setup.bash" >> ~/.bashrc
source ~/.bashrc

1.8 创建ROS工作空间

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

1.9 检查是否安装成功

source devel/setup.bash
echo $ROS_PACKAGE_PATH

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

2安装MAVROS包5

现在也可以直接安装不用从源码编译了
deb安装:

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

==========分割线=========
源码编译 :

cd ~/catkin_ws/src
git clone https://github.com/mavlink/mavros.git

编译

cd ~/catkin_ws
catkin_make 

提示缺什么就装什么 一般需要以下几个依赖包

sudo apt-get install ros-kinetic-mavlink
sudo apt-get install ros-kinetic-geometry
sudo apt-get install ros-kinetic-control-toolbox
sudo apt-get install libgeographic-dev
sudo apt-get install geographiclib-tools

还需要下载一些额外数据:

cd ~/catkin_ws/src/mavros/mavros/scripts/
sudo ./install_geographiclib_datasets.sh

接下来运行catkin_make应该就没有问题了。

3编写外部控制程序

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

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

pip install catkin_create_pkg #不一定需要
cd ~/catkin_ws/src/
catkin_create_pkg offb roscpp mavros geometry_msgs

3.2 外部控制程序

cd ~/catkin_ws/src/offb/src/
gedit offb_node.cpp

将官网的示例程序https://dev.px4.io/zh/ros/mavros_offboard.html复制粘贴进来,
注意:由于kinetic版本的ros下的mavros是master版本,与官网介绍的indigo版本不一样,有一些不同,直接编译会在这一句(65行)报错:
offb_set_mode.response.sucess
提示set_mode没有sucess的属性,因为master版本的mavros的属性名变了,此处应把sucess改为mode_sent,即为
offb_set_mode.response.mode_sent

此处参考MAVROS源码:
https://github.com/mavlink/mavros/blob/master/mavros_msgs/srv/SetMode.srv

保存后关闭。

3.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(${PROJECT_NAME} ${${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(${PROJECT_NAME}_node ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})

## Specify libraries to link a library or executable target against
target_link_libraries(offb_node
  ${catkin_LIBRARIES}
)

保存关闭

4 编译测试

4.1 编译

cd ~/catkin_ws
catkin_make

4.2 开始仿真

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

cd ~/src/Firmware
make posix_sitl_default gazebo

打开终端2,运行mavros

roslaunch mavros px4.launch fcu_url:="udp://:[email protected]:14557"

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

source ~/catkin_ws/devel/setup.bash
rosrun offb offb_node

  1. https://dev.px4.io/zh/ros/mavros_offboard.html ↩
  2. http://wiki.ros.org/kinetic/Installation/Ubuntu ↩
  3. http://blog.csdn.net/oqqENvY12/article/details/55263122 ↩
  4. 这里选择完整版,已经包含了Gazebo7 ↩
  5. Kinect对应的是默认的master分支,不需要另外切换 ↩

你可能感兴趣的:(四轴飞行器,PX4,ROS)