前言: 本次教程是官方提供的 MAVROS Offboard (板外) 控制示例,但加上了外部遙控器(RC)控制(如果想要在真機上實現,還要修改代碼)
注:搭建仿真環(huán)境可以看下面教程 ??
(最新)ubuntu搭建PX4無人機仿真環(huán)境(1) —— 概念介紹及環(huán)境建議
(最新)ubuntu搭建PX4無人機仿真環(huán)境(2) —— MAVROS安裝
(最新)ubuntu搭建PX4無人機仿真環(huán)境(3) —— ubuntu安裝QGC地面站
(最新)ubuntu搭建PX4無人機仿真環(huán)境(4) —— 仿真環(huán)境搭建
ubuntu安裝ROS melodic(最新、超詳細圖文教程)
前期回顧:
帶你玩轉PX4無人機仿真(1) —— 運行官方案例(C++)
帶你玩轉PX4無人機仿真(2) —— 定點飛行
一. 設置遙控器
(富斯i6s 航模遙控器)
仿真電腦和遙控器通過Micro USB數據線 (也就是老版安卓手機的充電線) 連接
(仿真電腦與遙控器連接圖)
當在仿真時,打開QGC,在設置中可以看到多了一個 游戲手柄 的界面,如下圖所示
點擊按鈕分配并撥動遙控器上的撥桿,就可以看到所對應的通道,然后為撥桿配置功能。(我的配置如下,僅供參考)
二. 仿真代碼
我們繼續(xù)使用 帶你玩轉PX4無人機仿真(1)
中的功能包,在 off_node 功能包下的 src 目錄下新建一個 offb_node_rc.cpp 文件
cd ~/catkin_ws/src/off_node/src/
touch offb_node_rc.cpp // 創(chuàng)建文件
gedit offb_node_rc.cpp // 打開文件
打開文件,將代碼粘貼上去
#include <ros/ros.h>
#include <geometry_msgs/PoseStamped.h>
#include <mavros_msgs/CommandBool.h>
#include <mavros_msgs/SetMode.h>
#include <mavros_msgs/State.h>
#define GREEN "\033[0;1;32m"
using namespace std;
mavros_msgs::State current_state;
void state_cb(const mavros_msgs::State::ConstPtr& msg){
current_state = *msg;
ROS_INFO("current mode: %s",current_state.mode.c_str());
ROS_INFO("system_status: %d",current_state.system_status);
}
geometry_msgs::PoseStamped aim_pos;
geometry_msgs::PoseStamped curr_pos;
void arrive_pos(const geometry_msgs::PoseStamped::ConstPtr& msg){
curr_pos = *msg;
cout <<GREEN <<"distance: "<<fabs((*msg).pose.position.z - aim_pos.pose.position.z) <<endl; // 打印與目標點的差距
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "off_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");
ros::Subscriber local_pos_sub = nh.subscribe<geometry_msgs::PoseStamped>
("mavros/local_position/pose",10,arrive_pos);
//the setpoint publishing rate MUST be faster than 2Hz
ros::Rate rate(10.0); // 10Hz 100ms
// wait for FCU connection
while(ros::ok() && !current_state.connected){
ros::spinOnce();
rate.sleep();
}
ROS_INFO("vehicle connected!");
//geometry_msgs::PoseStamped pose;
aim_pos.pose.position.x = 0;
aim_pos.pose.position.y = 0;
aim_pos.pose.position.z = 2;
//send a few setpoints before starting
for(int i = 100; ros::ok() && i > 0; --i){
local_pos_pub.publish(aim_pos);
ros::spinOnce();
rate.sleep();
}
mavros_msgs::CommandBool arm_cmd;
arm_cmd.request.value = true;
ros::Time last_request = ros::Time::now();
int count = 0; // 計時
while(ros::ok()){
if( current_state.mode == "OFFBOARD"){
if( !current_state.armed){
if( arming_client.call(arm_cmd) &&arm_cmd.response.success){
ROS_INFO("Vehicle armed");
}
}
}
if(current_state.mode == "OFFBOARD" && fabs(curr_pos.pose.position.z - aim_pos.pose.position.z) <= 0.3){
count++;
cout <<GREEN << "count: "<< count<< endl;
if(count > 150) // 15s
{
mavros_msgs::SetMode land_set_mode;
land_set_mode.request.custom_mode = "AUTO.LAND"; // 發(fā)送降落命令
if(set_mode_client.call(land_set_mode) && land_set_mode.response.mode_sent){
ROS_INFO("land enabled");
}
//任務結束,關閉該節(jié)點
ros::shutdown();
}
}
if(current_state.mode != "OFFBOARD"){
ROS_INFO("switch to Offboard");
}
local_pos_pub.publish(aim_pos);
ros::spinOnce();
rate.sleep();
}
return 0;
}
將下面內容添加到 off_node 功能包下的 CMakeLists.txt 文件里
add_executable(offb_node src/offb_node_rc.cpp)
target_link_libraries(offb_node_rc ${catkin_LIBRARIES})
三. 編譯運行
運行下面命令,編譯代碼
cd ~/catkin_ws
catkin_make # 使用catkin build話,則為 catkin build
創(chuàng)建一個啟動腳本 offb_rc.sh
#!/bin/bash
source ~/.bashrc
gnome-terminal --window -e 'bash -c "roscore; exec bash"' \
--tab -e 'bash -c "sleep 5; roslaunch px4 mavros_posix_sitl.launch; exec bash"' \
--tab -e 'bash -c "sleep 10; rosrun off_node offb_node_rc; exec bash"' \
運行
chmod +x offb_rc.sh
./offb_rc.sh
運行效果如下:??
PX4無人機仿真——官方案例 (RC版)
參考
MAVROS Offboard 控制示例 (C++) | PX4 自動駕駛用戶指南
Prometheus仿真入門 — 仿真中的遙控器使用說明文章來源:http://www.zghlxwxcb.cn/news/detail-826075.html
如有其他問題,或者發(fā)現文章有錯誤,請在評論區(qū)留言
Keep learning!文章來源地址http://www.zghlxwxcb.cn/news/detail-826075.html
到了這里,關于帶你玩轉PX4無人機仿真(3) —— 運行官方案例(RC版)的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!