官方文档是基于ROS-indigo作的介绍,ubuntu16.04下只能安装kinetic版本的ROS,亲身试坑后留下一些经验教训。
主要参考了PX4开发文档1,ROS官方文档2和相关博客步骤如下3:
步骤如下:
这几个全勾上
sudo sh -c 'echo "deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list'
sudo apt-key adv --keyserver hkp://ha.pool.sks-keyservers.net:80 --recv-key 421C365BD9FF1F717815A3895523BAEEB01FA116
sudo apt-get update
sudo apt-get install ros-kinetic-desktop-full
sudo rosdep init
rosdep update
echo "source /opt/ros/kinetic/setup.bash" >> ~/.bashrc
source ~/.bashrc
mkdir -p ~/catkin_ws/src
cd ~/catkin_ws/
catkin_make
source devel/setup.bash
echo $ROS_PACKAGE_PATH
若结果是:/home//catkin_ws/src:/opt/ros/indigo/share:/opt/ros/indigo/stacks 则表明一切正常,可以继续
现在也可以直接安装不用从源码编译了
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应该就没有问题了。
这个程序包依赖于roscpp 、mavros以及geometry_msgs。可以理解为一个ROS的一个节点
pip install catkin_create_pkg #不一定需要
cd ~/catkin_ws/src/
catkin_create_pkg offb roscpp mavros geometry_msgs
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
保存后关闭。
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}
)
保存关闭
cd ~/catkin_ws
catkin_make
打开终端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