终于还是走上了这一步,对飞控下手,可以说是一张白纸了。记录一下学习的过程方便以后的查阅。
目录
一、ubuntu18.04配置px4编译环境及mavros环境
二、PX4的OffBoard控制
1.搭建功能包
2.编写控制程序
3.编译运行
4.运行结果
总结
一、ubuntu18.04配置px4编译环境及mavros环境
首先是环境的配置,我的系统是 ubuntu18.04,在配置环境的过程中会出现各种问题。但是都是可以解决的,配置环境的过程是比较繁琐的。我大概配置了有半天左右。参考的是下面这篇博文,建议大家显阅读一下再进行环境的配置,因为这篇博客中介绍了可能会出现的各种问题,所以当你在配置环境出现问题时,不要慌张去看一下是不是有被提到!
PX4从放弃到精通(二):ubuntu18.04配置px4编译环境及mavros环境_老王机器人的博客-CSDN博客_px4编译环境
二、PX4的OffBoard控制
在配置好环境之后,就是要实现一个简单的OffBoard控制。也可以在官网中找到相关的例程,我主要是理解一下程序的书写!
1.搭建功能包
Ctrl+T 打开终端,在终端中输入如下命令创建工作空间和功能包
mkdir -p px4_test/src
cd px4_test/src
catkin_create_pkg offboard_pkg roscpp std_msgs geometry_msgs mavros_msgs
cd ~/px4_test
catkin_make
cd ~/px4_test/src/offboard_pkg/src
touch test.cpp
打开功能包中的 CmakeLists.txt 文件,添加两行内容如下:
add_executable(test src/test.cpp)
target_link_libraries(test ${catkin_LIBRARIES})
如上,已经搭建好了一个大的框架,接下来就是在test.cpp中添加内容。
2.编写控制程序
代码如下:
/**
* @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;
}
3.编译运行
#先进行编译
cd ~/px4_test
catkin_make
#打开一个新的终端,进入你的 px4 安装的工作空间使用 gazebo 仿真
make px4_sitl gazebo_iris
#启动 PX4 与 Mavros 的连接
roslaunch mavros px4.launch fcu_url:="udp://:14540@127.0.0.1:14557"
#进入 px4_test 工作空间,执行
rosrun offboard_pkg test
4.运行结果
总结
简单的总结了一下Offboard控制的应用例程,哎呀呀一步一步走,一定要走踏实!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)