[PX4]mavros安裝+offboard控制過程記錄

摘自:https://zhuanlan.zhihu.com/p/86116918

[PX4]mavros安裝+offboard控制過程記錄

UShark

UShark

不知名的SharkU

在上篇文章中,我們在ubuntu上搭建了PX4的開發環境。本篇文章將繼續開發環境的搭建,完成ROS+gazebo+Mavros的仿真環境搭建,其中mavros可以用來執行offboard控制。

!!!注意:

我的安裝過程可能稍有不同,由於在這之前我就已經安裝了ROS,而Gazebo則是在上一篇文章中已經安裝,因此我在安裝的時候沒有直接使用安裝腳本,而是將腳本逐行執行以避免問題。

如果你之前沒有在home下創建過ros工作空間,那你應該直接運行腳本,而不用按照本篇文章的記錄,以減少麻煩。

本篇文章分爲以下幾個部分:

  1. mavros的安裝
  2. offboard控制演示
  3. 視頻記錄

1.mavros的安裝

這部分的安裝過程是按照官方連接(ROS/Gazebo)來進行的,下面的命令實際上是腳本中的命令逐行運行。

1. 檢查系統版本

lsb_release -sc
bionic

查看自己的輸出是否爲bionic,確認自己的版本是否是18.04,如果不是,請尋找針對特定版本的教程。

2. 安裝仿真依賴項

wget https://raw.githubusercontent.com/PX4/Devguide/master/build_scripts/ubuntu_sim_common_deps.sh
​
./ubuntu_sim_common_deps.sh

如果下載下來的腳本不是可執行文件,運行

chmod +x ubuntu_sim_common_deps.sh

安裝過程中查看是否輸出了:

./ubuntu_sim_common_deps.sh: 行 47: * :語法錯誤:需要操作數

 

 

那麼你需要自己運行下面的命令來安裝fastrtps,雖然這個暫時也沒看到有什麼用。

進入home目錄,運行

(cd eProsima_FastCDR-1.0.8-Linux && ./configure --libdir=/usr/lib && make -j4 && sudo make install)
(cd eProsima_FastRTPS-1.7.1-Linux && ./configure --libdir=/usr/lib && make -j4 && sudo make install)

來進行安裝。

3. 安裝ROS+Gazebo

sudo apt-get install protobuf-compiler libeigen3-dev libopencv-dev -y
​
# 安裝的ROS+Gazebo
## ROS Gazebo: http://wiki.ros.org/melodic/Installation/Ubuntu
## Setup keys
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://keyserver.ubuntu.com:80' --recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654
## For keyserver connection problems substitute hkp://pgp.mit.edu:80 or hkp://keyserver.ubuntu.com:80 above.
sudo apt-get update
## Get ROS/Gazebo
sudo apt install ros-melodic-desktop-full -y
## Initialize rosdep
sudo rosdep init
rosdep update
## Setup environment variables
echo "source /opt/ros/melodic/setup.bash" >> ~/.bashrc;
source /opt/ros/melodic/setup.bash
​
## Install rosinstall and other dependencies
sudo apt install python-rosinstall python-rosinstall-generator python-wstool build-essential -y

4. 編譯MAVROS

# MAVROS: https://dev.px4.io/en/ros/mavros_installation.html
## Install dependencies
sudo apt-get install python-catkin-tools python-rosinstall-generator -y
​
## Create catkin workspace
mkdir -p ~/catkin_ws/src
cd ~/catkin_ws
catkin init
wstool init src
​
​
## Install MAVLink
###we use the Kinetic reference for all ROS distros as it's not distro-specific and up to date
rosinstall_generator --rosdistro kinetic mavlink | tee /tmp/mavros.rosinstall
​
## Build MAVROS
### Get source (upstream - released)
rosinstall_generator --upstream mavros | tee -a /tmp/mavros.rosinstall
​
### Setup workspace & install deps
wstool merge -t src /tmp/mavros.rosinstall
wstool update -t src
​
rosdep install --from-paths src --ignore-src -y
​
wget https://raw.githubusercontent.com/mavlink/mavros/master/mavros/scripts/install_geographiclib_datasets.sh
​
./install_geographiclib_datasets.sh
​
catkin build
​
# source 空間
echo "source ~/catkin_ws/devel/setup.bash >> ~/.bashrc
source ~/catkin_ws/devel/setup.bash

2. offboard控制

打開官方="https://dev.px4.io/master/en/ros/mavros_offboard.html">MAVROS Offboard control example鏈接,然後在catkin_ws目錄中,運行命令

catkin_create_pkg offboard_pkg roscpp std_msgs geometry_msgs mavros_msgs

然後定位到目錄~/catkin_ws/src/offboard_pkg/src/,新建一個文件offboard_node.cpp

將代碼複製進去(官方示例):

/**
 * @file offb_node.cpp
 * @brief Offboard control example node, written with MAVROS version 0.19.x, PX4 Pro Flight
 * Stack and tested in Gazebo SITL
 */
​
#include <ros/ros.h>
#include <geometry_msgs/PoseStamped.h>
#include <mavros_msgs/CommandBool.h>
#include <mavros_msgs/SetMode.h>
#include <mavros_msgs/State.h>
​
mavros_msgs::State current_state;
void state_cb(const mavros_msgs::State::ConstPtr& msg){
    current_state = *msg;
}
​
int main(int argc, char **argv)
{
    ros::init(argc, argv, "offb_node");
    ros::NodeHandle nh;
​
    ros::Subscriber state_sub = nh.subscribe<mavros_msgs::State>
            ("mavros/state", 10, state_cb);
    ros::Publisher local_pos_pub = nh.advertise<geometry_msgs::PoseStamped>
            ("mavros/setpoint_position/local", 10);
    ros::ServiceClient arming_client = nh.serviceClient<mavros_msgs::CommandBool>
            ("mavros/cmd/arming");
    ros::ServiceClient set_mode_client = nh.serviceClient<mavros_msgs::SetMode>
            ("mavros/set_mode");
​
    //the setpoint publishing rate MUST be faster than 2Hz
    ros::Rate rate(20.0);
​
    // wait for FCU connection
    while(ros::ok() && !current_state.connected){
        ros::spinOnce();
        rate.sleep();
    }
​
    geometry_msgs::PoseStamped pose;
    pose.pose.position.x = 0;
    pose.pose.position.y = 0;
    pose.pose.position.z = 2;
​
    //send a few setpoints before starting
    for(int i = 100; ros::ok() && i > 0; --i){
        local_pos_pub.publish(pose);
        ros::spinOnce();
        rate.sleep();
    }
​
    mavros_msgs::SetMode offb_set_mode;
    offb_set_mode.request.custom_mode = "OFFBOARD";
​
    mavros_msgs::CommandBool arm_cmd;
    arm_cmd.request.value = true;
​
    ros::Time last_request = ros::Time::now();
​
    while(ros::ok()){
        if( current_state.mode != "OFFBOARD" &&
            (ros::Time::now() - last_request > ros::Duration(5.0))){
            if( set_mode_client.call(offb_set_mode) &&
                offb_set_mode.response.mode_sent){
                ROS_INFO("Offboard enabled");
            }
            last_request = ros::Time::now();
        } else {
            if( !current_state.armed &&
                (ros::Time::now() - last_request > ros::Duration(5.0))){
                if( arming_client.call(arm_cmd) &&
                    arm_cmd.response.success){
                    ROS_INFO("Vehicle armed");
                }
                last_request = ros::Time::now();
            }
        }
​
        local_pos_pub.publish(pose);
​
        ros::spinOnce();
        rate.sleep();
    }
​
    return 0;
}

然後打開目錄~/catkin_ws/src/offboard_pkg/下的CMakeLists.txt添加下面的兩行:

add_executable(offboard_node src/offboard_node.cpp)
target_link_libraries(offboard_node ${catkin_LIBRARIES})

然後到目錄~/catkin_ws下,運行命令

catkin build

等待編譯完成後,如果你要在gazebo中仿真,運行命令

make px4_sitl gazebo_iris

打開QGroundControl

然後在終端下運行命令:

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

啓動PX4Mavros之間的連接,然後運行命令

rosrun offboard_pkg offboard_node

然後進入gazebo中進行觀察。

3. 視頻記錄

同時,我還將我的安裝過程錄製成了視頻,請點擊這裏查看。**如果想看offboard控制的效果,可以直接看視頻13:00處。

發佈於 2019-10-11

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章