在上篇文章中,我們在ubuntu
上搭建了PX4的開發環境。本篇文章將繼續開發環境的搭建,完成ROS
+gazebo
+Mavros
的仿真環境搭建,其中mavros
可以用來執行offboard
控制。
!!!注意:
我的安裝過程可能稍有不同,由於在這之前我就已經安裝了ROS
,而Gazebo
則是在上一篇文章中已經安裝,因此我在安裝的時候沒有直接使用安裝腳本,而是將腳本逐行執行以避免問題。
如果你之前沒有在home
下創建過ros
工作空間,那你應該直接運行腳本,而不用按照本篇文章的記錄,以減少麻煩。
本篇文章分為以下幾個部分:
mavros
的安裝offboard
控制演示- 視頻記錄
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控制
打開官方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://:14540@127.0.0.1:14557"
啟動PX4
與Mavros
之間的連接,然后運行命令
rosrun offboard_pkg offboard_node
然后進入gazebo
中進行觀察。
3. 視頻記錄
同時,我還將我的安裝過程錄制成了視頻,請點擊這里查看。**如果想看offboard
控制的效果,可以直接看視頻13:00
處。