全自主巡航无人机项目思路:STM32/PX4 + ROS + AI 实现从传感融合到智能规 ...

诗林  金牌会员 | 2024-7-18 09:44:22 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 685|帖子 685|积分 2055

1. 项目概述

本项目旨在计划并实现一款高度自主的主动巡航无人机体系。该体系能够按照预设路径自主飞行,完成各种巡航任务,如电力巡线、丛林防火、边境巡逻和灾难监测等。
1.1 体系特点



  • 基于STM32F4和PX4的高性能嵌入式飞控体系
  • 多传感器融合技能实现准确定位和姿态估计
  • Wi-Fi/4G双模无线通信,支持远程控制和数据传输
  • 基于ROS的智能路径规划算法,实现复杂情况下的自主导航
  • 模块化计划,易于扩展和维护
1.2 技能栈



  • 嵌入式开发:STM32F4 MCU,PX4飞控体系,C/C++编程语言
  • 传感器集成:GPS、IMU(加快率计、陀螺仪、磁力计)、气压计
  • 无线通信:Wi-Fi模块(短隔断),4G模块(远隔断),MAVLink协议
  • 路径规划:ROS框架,Python编程语言
  • 开发工具:STM32CubeIDE,QGroundControl地面站软件
2. 体系计划

2.1 硬件架构


硬件体系主要由以下模块构成:

  • 飞控主板:采用STM32F4系列MCU,运行PX4飞控体系
  • 定位模块:集成GPS模块,提供准确的环球定位信息
  • 姿态测量:IMU(惯性测量单元)包罗加快率计、陀螺仪和磁力计
  • 高度测量:气压计用于测量相对高度和垂直速率
  • 通信模块:Wi-Fi模块用于短隔断高带宽通信,4G模块用于远隔断通信
  • 动力体系:电机驱动控制四个无刷电机
  • 视觉体系:高清摄像头用于情况感知和目标识别
  • 电源体系:锂电池供电,配备电源管理模块
2.2 软件架构


软件体系主要包括以下组件:

  • PX4飞控体系:

    • 传感器驱动:负责读取和处理各类传感器数据
    • 姿态估计:使用扩展卡尔曼滤波(EKF)融合IMU、GPS等数据
    • 位置控制:实现准确的位置保持和轨迹跟踪
    • 飞行模式:包括手动、半主动、全主动等多种飞行模式
    • 通信模块:基于MAVLink协议与地面站和ROS节点通信

  • 地面站(QGroundControl):

    • 飞行监控:及时显示飞行状态、位置和传感器数据
    • 任务规划:计划巡航路径,设置航点和任务参数
    • 参数设置:调整PID参数,设置飞行限制等
    • 固件更新:支持远程固件升级

  • ROS(机器人操作体系)节点:

    • 路径规划:使用A*或RRT算法进行全局路径规划
    • 停滞物检测:基于视觉或激光雷达数据进行及时停滞物检测
    • SLAM建图:同步定位与舆图构建,用于未知情况导航

  • 通信流程:

    • PX4飞控体系通过MAVLink协议与地面站和ROS节点进行双向通信
    • 地面站发送控制指令和任务信息给飞控体系
    • ROS节点将规划的路径、检测到的停滞物信息发送给飞控体系
    • 飞控体系及时反馈飞行状态和传感器数据给地面站和ROS节点

这种分层的软件架构计划具有以下优势:


  • 模块化:各个组件功能明确,便于开发和维护
  • 机动性:可以根据需求easily添加或更换功能模块
  • 可扩展性:支持添加新的传感器和算法以增强体系本领
  • 可靠性:核心飞控功能由成熟的PX4体系保障,提高体系稳定性
3. 核心代码实现

3.1 姿态估计

姿态估计是主动巡航无人机体系的关键模块之一。我们使用四元数表现姿态,并采用互补滤波算法融合加快率计和陀螺仪数据。以下是核心代码实现:
  1. #include <math.h>
  2. // 四元数结构体
  3. typedef struct {
  4.     float w, x, y, z;
  5. } Quaternion;
  6. // 姿态估计参数
  7. #define dt 0.01f  // 采样周期
  8. #define alpha 0.98f  // 互补滤波系数
  9. Quaternion attitude = {1.0f, 0.0f, 0.0f, 0.0f};  // 初始姿态
  10. void attitudeUpdate(float acc[3], float gyro[3]) {
  11.     // 归一化加速度
  12.     float accMag = sqrt(acc[0]*acc[0] + acc[1]*acc[1] + acc[2]*acc[2]);
  13.     float ax = acc[0] / accMag;
  14.     float ay = acc[1] / accMag;
  15.     float az = acc[2] / accMag;
  16.     // 基于加速度计算俯仰角和横滚角
  17.     float pitch = atan2(-ax, sqrt(ay*ay + az*az));
  18.     float roll = atan2(ay, az);
  19.     // 构造基于加速度的四元数
  20.     Quaternion qAcc;
  21.     qAcc.w = cos(roll/2) * cos(pitch/2);
  22.     qAcc.x = cos(roll/2) * sin(pitch/2);
  23.     qAcc.y = sin(roll/2) * cos(pitch/2);
  24.     qAcc.z = -sin(roll/2) * sin(pitch/2);
  25.     // 基于陀螺仪数据的四元数微分方程
  26.     float qDot[4];
  27.     qDot[0] = 0.5f * (-attitude.x*gyro[0] - attitude.y*gyro[1] - attitude.z*gyro[2]);
  28.     qDot[1] = 0.5f * (attitude.w*gyro[0] + attitude.y*gyro[2] - attitude.z*gyro[1]);
  29.     qDot[2] = 0.5f * (attitude.w*gyro[1] - attitude.x*gyro[2] + attitude.z*gyro[0]);
  30.     qDot[3] = 0.5f * (attitude.w*gyro[2] + attitude.x*gyro[1] - attitude.y*gyro[0]);
  31.     // 更新姿态四元数
  32.     attitude.w += qDot[0] * dt;
  33.     attitude.x += qDot[1] * dt;
  34.     attitude.y += qDot[2] * dt;
  35.     attitude.z += qDot[3] * dt;
  36.     // 互补滤波
  37.     attitude.w = alpha * attitude.w + (1-alpha) * qAcc.w;
  38.     attitude.x = alpha * attitude.x + (1-alpha) * qAcc.x;
  39.     attitude.y = alpha * attitude.y + (1-alpha) * qAcc.y;
  40.     attitude.z = alpha * attitude.z + (1-alpha) * qAcc.z;
  41.     // 归一化四元数
  42.     float mag = sqrt(attitude.w*attitude.w + attitude.x*attitude.x +
  43.                      attitude.y*attitude.y + attitude.z*attitude.z);
  44.     attitude.w /= mag;
  45.     attitude.x /= mag;
  46.     attitude.y /= mag;
  47.     attitude.z /= mag;
  48. }
复制代码
代码说明:

  • 我们定义了一个Quaternion结构体来表现姿态四元数。
  • attitudeUpdate函数接收加快率计和陀螺仪的原始数据作为输入。
  • 起首处理加快率计数据,盘算出俯仰角和横滚角,并构造对应的四元数。
  • 然后利用陀螺仪数据,通过四元数微分方程更新姿态。
  • 使用互补滤波算法融合加快率计和陀螺仪的结果,alpha参数决定了各自的权重。
  • 最后对结果四元数进行归一化,确保其表现有效的旋转。
这种方法团结了加快率计的长期稳定性和陀螺仪的短期正确性,能够得到更加准确的姿态估计。
3.2 位置控制

位置控制是实现主动巡航的关键。我们使用PID控制器来实现准确的位置保持和轨迹跟踪。以下是简化的PID控制器实现:
  1. #include <math.h>
  2. typedef struct {
  3.     float Kp, Ki, Kd;  // PID参数
  4.     float integral;    // 积分项
  5.     float prevError;   // 上一次的误差
  6. } PIDController;
  7. // 初始化PID控制器
  8. void initPIDController(PIDController* pid, float Kp, float Ki, float Kd) {
  9.     pid->Kp = Kp;
  10.     pid->Ki = Ki;
  11.     pid->Kd = Kd;
  12.     pid->integral = 0.0f;
  13.     pid->prevError = 0.0f;
  14. }
  15. // PID控制器更新函数
  16. float updatePID(PIDController* pid, float setpoint, float measurement, float dt) {
  17.     float error = setpoint - measurement;
  18.    
  19.     // 比例项
  20.     float P = pid->Kp * error;
  21.    
  22.     // 积分项
  23.     pid->integral += error * dt;
  24.     float I = pid->Ki * pid->integral;
  25.    
  26.     // 微分项
  27.     float derivative = (error - pid->prevError) / dt;
  28.     float D = pid->Kd * derivative;
  29.    
  30.     // 计算输出
  31.     float output = P + I + D;
  32.    
  33.     // 更新上一次误差
  34.     pid->prevError = error;
  35.    
  36.     return output;
  37. }
  38. // 位置控制主函数
  39. void positionControl(float targetPosition[3], float currentPosition[3], float* outputs) {
  40.     static PIDController pidX, pidY, pidZ;
  41.    
  42.     // 初始化PID控制器(仅在第一次调用时执行)
  43.     static int initialized = 0;
  44.     if (!initialized) {
  45.         initPIDController(&pidX, 1.0f, 0.1f, 0.05f);  // 示例PID参数
  46.         initPIDController(&pidY, 1.0f, 0.1f, 0.05f);
  47.         initPIDController(&pidZ, 1.5f, 0.15f, 0.1f);  // 垂直方向通常需要更强的控制
  48.         initialized = 1;
  49.     }
  50.    
  51.     // 更新每个轴的PID控制器
  52.     float dt = 0.01f;  // 假设控制周期为10ms
  53.     outputs[0] = updatePID(&pidX, targetPosition[0], currentPosition[0], dt);
  54.     outputs[1] = updatePID(&pidY, targetPosition[1], currentPosition[1], dt);
  55.     outputs[2] = updatePID(&pidZ, targetPosition[2], currentPosition[2], dt);
  56. }
复制代码
代码说明:

  • PIDController结构体包罗PID控制器的参数和状态。
  • initPIDController函数用于初始化PID控制器的参数。
  • updatePID函数实现了PID控制算法的核心逻辑,包括比例、积分和微分三个部分。
  • positionControl函数是位置控制的主函数,它为X、Y、Z三个轴分别创建和更新PID控制器。
  • 控制器的输出可以直接用作无人机的速率或加快率指令,具体取决于飞控体系的接口计划。
这个简化的PID控制器为每个轴独立控制,在实际应用中可能必要考虑轴间耦合和更复杂的动力学模子。
3.3 路径规划

路径规划模块使用ROS(机器人操作体系)和Python实现。我们采用A*算法进行全局路径规划。以下是简化的实现:
  1. import rospy
  2. from geometry_msgs.msg import PoseStamped
  3. from nav_msgs.msg import OccupancyGrid, Path
  4. import numpy as np
  5. class AStarPlanner:
  6.     def __init__(self):
  7.         self.map = None
  8.         self.start = None
  9.         self.goal = None
  10.         self.path = []
  11.         # ROS节点初始化
  12.         rospy.init_node('astar_planner')
  13.         self.map_sub = rospy.Subscriber('/map', OccupancyGrid, self.map_callback)
  14.         self.start_sub = rospy.Subscriber('/start_pose', PoseStamped, self.start_callback)
  15.         self.goal_sub = rospy.Subscriber('/goal_pose', PoseStamped, self.goal_callback)
  16.         self.path_pub = rospy.Publisher('/path', Path, queue_size=1)
  17.     def map_callback(self, msg):
  18.         self.map = np.array(msg.data).reshape((msg.info.height, msg.info.width))
  19.     def start_callback(self, msg):
  20.         self.start = (int(msg.pose.position.x), int(msg.pose.position.y))
  21.         self.plan()
  22.     def goal_callback(self, msg):
  23.         self.goal = (int(msg.pose.position.x), int(msg.pose.position.y))
  24.         self.plan()
  25.     def heuristic(self, a, b):
  26.         return np.sqrt((b[0] - a[0]) ** 2 + (b[1] - a[1]) ** 2)
  27.     def get_neighbors(self, node):
  28.         directions = [(0,1),(0,-1),(1,0),(-1,0),(1,1),(1,-1),(-1,1),(-1,-1)]
  29.         neighbors = []
  30.         for direction in directions:
  31.             neighbor = (node[0] + direction[0], node[1] + direction[1])
  32.             if 0 <= neighbor[0] < self.map.shape[0] and 0 <= neighbor[1] < self.map.shape[1]:
  33.                 if self.map[neighbor] == 0:  # 假设0表示自由空间
  34.                     neighbors.append(neighbor)
  35.         return neighbors
  36.     def astar(self):
  37.         open_set = set([self.start])
  38.         closed_set = set()
  39.         came_from = {}
  40.         g_score = {self.start: 0}
  41.         f_score = {self.start: self.heuristic(self.start, self.goal)}
  42.         while open_set:
  43.             current = min(open_set, key=lambda x: f_score[x])
  44.             if current == self.goal:
  45.                 path = []
  46.                 while current in came_from:
  47.                     path.append(current)
  48.                     current = came_from[current]
  49.                 path.append(self.start)
  50.                 return path[::-1]
  51.             open_set.remove(current)
  52.             closed_set.add(current)
  53.             for neighbor in self.get_neighbors(current):
  54.                 if neighbor in closed_set:
  55.                     continue
  56.                 tentative_g_score = g_score[current] + self.heuristic(current, neighbor)
  57.                 if neighbor not in open_set:
  58.                     open_set.add(neighbor)
  59.                 elif tentative_g_score >= g_score[neighbor]:
  60.                     continue
  61.                 came_from[neighbor] = current
  62.                 g_score[neighbor] = tentative_g_score
  63.                 f_score[neighbor] = g_score[neighbor] + self.heuristic(neighbor, self.goal)
  64.         return None  # 没有找到路径
  65.     def plan(self):
  66.         if self.map is not None and self.start is not None and self.goal is not None:
  67.             self.path = self.astar()
  68.             if self.path:
  69.                 # 发布路径消息
  70.                 path_msg = Path()
  71.                 path_msg.header.frame_id = "map"
  72.                 path_msg.header.stamp = rospy.Time.now()
  73.                 for point in self.path:
  74.                     pose = PoseStamped()
  75.                     pose.pose.position.x = point[0]
  76.                     pose.pose.position.y = point[1]
  77.                     path_msg.poses.append(pose)
  78.                 self.path_pub.publish(path_msg)
  79.             else:
  80.                 rospy.logwarn("No path found")
  81. if __name__ == '__main__':
  82.     planner = AStarPlanner()
  83.     rospy.spin()
复制代码
代码说明:

  • AStarPlanner类实现了A*路径规划算法。
  • 使用ROS的订阅者接收舆图、起点和尽头信息:

    • /map: 接收占用栅格舆图
    • /start_pose: 接收起点位置
    • /goal_pose: 接收尽头位置

  • map_callback, start_callback, goal_callback 函数处理接收到的数据。
  • heuristic函数盘算两点间的欧几里得隔断,作为A*算法的启发函数。
  • get_neighbors函数返回给定节点的有效邻居节点。
  • astar函数实现了A*算法的核心逻辑:

    • 使用open_set和closed_set来管理待探索和已探索的节点
    • f_score = g_score + heuristic,用于选择最优节点
    • 当找到目标节点时,通过came_from字典回溯构建路径
    • 如果无法找到路径,返回None

  • plan函数是路径规划的主函数:

    • 查抄是否已接收到必要的信息(舆图、起点、尽头)
    • 调用astar函数进行路径规划
    • 如果找到路径,将其转换为ROS的Path消息并发布

  • 在主函数中,我们创建AStarPlanner实例并使用rospy.spin()保持节点运行。
个路径规划模块的主要特点:


  • 使用ROS框架,便于与其他ROS节点(如定位、控制模块)集成
  • 实现了A*算法,能够在给定的栅格舆图上找到最优路径
  • 考虑了停滞物避免,只在自由空间中规划路径
  • 支持及时规划,当接收到新的起点或尽头时会重新规划
  • 将规划结果以标准的ROS Path消息格式发布,便于其他模块使用
在实际应用中,这个根本实现可以进一步优化:

  • 添加路径平滑处理,使路径更适合无人机飞行
  • 实现动态避障,考虑移动停滞物
  • 优化A*算法,如使用Jump Point Search等变体提高效率
  • 添加局部路径规划,以应对舆图变革或未知停滞物
4. 项目总结

本主动巡航无人机体系集成了多项关键技能:

  • 基于STM32F4和PX4的嵌入式飞控体系,实现了稳定的飞行控制
  • 多传感器融合的姿态估盘算法,提高了飞行姿态的准确度
  • PID控制器实现的位置控制,确保了准确的路径跟踪
  • 基于ROS的A*路径规划算法,实现了智能化的任务规划
通过这些模块的协同工作,体系能够完成复杂情况下的自主巡航任务。
 

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

诗林

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表