0. 飞行效果
飞行效果
1.前期准备
实验环境:Ubuntu20.04,ROS noetic
1.1 完成PX4的安装和编译
1.2 完成mavros 的安装
1.3 完成QGC的安装
上述内容网上资料很多,这里就不赘述了。
2.代码思路
代码思路参考PX4官网的Offboard control 的思路,重点在于如何将键盘上的内容转化为无人机的飞行信息。
代码1:无人机节点控制
#include <ros/ros.h>
#include <iostream>
#include <eigen3/Eigen/Dense>
#include <geometry_msgs/PoseStamped.h>
#include <mavros_msgs/CommandBool.h>
#include <mavros_msgs/SetMode.h>
#include <mavros_msgs/State.h>
#include <std_msgs/String.h>
using namespace Eigen;
using namespace std;
//定义话题名称
ros::Publisher local_pos_pub;
ros::Subscriber key_sub;
// 计算无人机与目标位置的距离
Eigen::Vector3d target_position;
bool position_updated = false;
void callback(const std_msgs::String::ConstPtr &msg)
{
ROS_INFO("Received keyboard input: %s", msg->data.c_str());
position_updated = true;
// 根据接收到的字符执行不同操作
//w,a,s,d控制前后左右,p,l控制高度
if (msg->data == "w")
{