1. 程式人生 > >PX4在ROS-Kinetic外部控制下的模擬(1)

PX4在ROS-Kinetic外部控制下的模擬(1)

官方文件是基於ROS-indigo作的介紹,ubuntu16.04下只能安裝kinetic版本的ROS,親身試坑後留下一些經驗教訓。
主要參考了PX4開發文件1,ROS官方文件2和相關部落格步驟如下3
步驟如下:

1安裝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

儲存後關閉。

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