本文档详细介绍了2D无人机正方形编队控制算法的实现、参数配置和使用方法。
- V1版本:基础实现,使用深色背景
- V2版本:增强了避碰能力,优化了视觉效果
- V3版本:使用白色背景,适合论文和报告使用,增强了图像质量设置
仿真过程主要包括以下步骤:
- 初始化:随机生成无人机初始位置,确保初始位置不会太近
- 目标位置计算:计算正方形编队的目标位置
- 迭代更新:
- 计算每架无人机受到的吸引力和斥力
- 更新无人机位置和速度
- 记录轨迹
- 检查收敛情况
- 可视化:实时显示无人机位置、轨迹和目标正方形
本项目实现了基于人工势场法的二维平面无人机正方形编队控制算法。主要特点包括:
- 领导者-跟随者结构:第一架无人机作为领导者,其余无人机作为跟随者
- 人工势场法:结合吸引力和斥力实现编队控制和碰撞避免
- 自适应速度控制:根据距离目标位置的远近动态调整速度
-
吸引力:无人机受到目标位置的吸引力
- 领导者吸引力:
LEADER_ATTRACT * 方向向量
- 跟随者吸引力:
FOLLOWER_ATTRACT * 方向向量
- 领导者吸引力:
-
斥力:无人机之间的避碰斥力
- 当两架无人机距离小于安全距离时产生斥力
- 斥力大小:
K_REPULSE * (1/距离 - 1/SAFE_DISTANCE) * (1/距离²)
-
碰撞避免优先级:通过
COLLISION_AVOIDANCE_PRIORITY
参数调整避碰优先级
quadrangular_formation_control_2D.m
:基础版本实现quadrangular_formation_control_2D_V2.m
:改进版本,增强了避碰能力quadrangular_formation_control_2D_V3_white_bg.m
:白色背景版本,适合论文和报告使用create_subfigure_2D.py
:Python脚本,用于生成多子图面板展示
主要参数及其含义:
NUM_UAV = 20 % 无人机数量
MAP_SIZE = 100 % 地图大小(正方形区域边长)
CENTER_POSITION = [50, 50] % 正方形编队的中心坐标
SQUARE_SIZE = 45 % 正方形编队的边长
% 势场参数
SAFE_DISTANCE = 6 % 无人机之间的安全距离
LEADER_ATTRACT = 4 % 领导者吸引力增益
FOLLOWER_ATTRACT = 2.5 % 跟随者吸引力增益
K_REPULSE = 3 % 避碰斥力增益
MAX_VELOCITY_LEADER = 3 % 领导者最大速度
MAX_VELOCITY_FOLLOWER = 2 % 跟随者最大速度
为了提供更好的可视化效果,代码中包含了丰富的视觉参数设置:
% 视觉效果参数
BACKGROUND_COLOR = [0.05, 0.05, 0.15] % 深蓝色背景
GRID_COLOR = [0.2, 0.2, 0.3] % 网格颜色
SQUARE_COLOR = [0.9, 0.7, 0.2] % 目标正方形颜色(金色)
LEADER_COLOR = [0.95, 0.2, 0.2] % 领导者颜色(红色)
FOLLOWER_COLOR = [0.2, 0.6, 0.9] % 跟随者颜色(蓝色)
% 轨迹样式参数
LEADER_TRAIL_WIDTH = 3.0 % 领导者轨迹线宽
FOLLOWER_TRAIL_WIDTH = 2.2 % 跟随者轨迹线宽
TRAIL_ALPHA = 0.8 % 轨迹透明度
TRAIL_GRADIENT = true % 是否启用渐变效果
- 在MATLAB中打开
quadrangular_formation_control_2D_V3_white_bg.m
文件 - 根据需要调整参数
- 运行脚本开始仿真
- 仿真结果将保存在
uav_square_formation2D/frames_时间戳/
目录下
运行create_subfigure_2D.py
脚本可以将仿真生成的图像合成为多子图面板:
python create_subfigure_2D.py
该脚本会自动查找最新的仿真结果,并生成4×2的子图面板,适合用于论文或报告展示。
-
如何调整编队形状?
- 修改
SQUARE_SIZE
参数可以改变正方形的大小 - 修改
CENTER_POSITION
可以改变编队中心位置
- 修改
-
如何提高避碰效果?
- 增大
K_REPULSE
和COLLISION_AVOIDANCE_PRIORITY
参数 - 增大
SAFE_DISTANCE
参数
- 增大
-
如何加快收敛速度?
- 增大
LEADER_ATTRACT
和FOLLOWER_ATTRACT
参数 - 增大
MAX_VELOCITY_LEADER
和MAX_VELOCITY_FOLLOWER
参数
- 增大
-
如何优化视觉效果?
- 调整
TRAIL_GRADIENT
和TRAIL_GRADIENT_COLORS
参数 - 修改颜色参数如
LEADER_COLOR
和FOLLOWER_COLOR
- 调整
代码中包含多项性能优化措施:
% 性能优化参数
ENABLE_DRAWNOW = true % 使用drawnow而不是pause来更新图形
SAVE_FRAME_INTERVAL = 5 % 每隔多少帧保存一次图像(减少I/O操作)
TRAIL_SIMPLIFY = true % 是否简化轨迹绘制
OPTIMIZE_SCATTER = true % 优化散点图绘制
这些优化可以显著提高仿真速度,特别是在处理大量无人机时。
This document provides detailed information about the implementation, parameter configuration, and usage of the 2D UAV square formation control algorithm.
- V1 Version: Basic implementation with dark background
- V2 Version: Enhanced collision avoidance and improved visual effects
- V3 Version: White background version suitable for papers and reports with enhanced image quality settings
The simulation process consists of the following steps:
- Initialization: Randomly generate initial UAV positions, ensuring they are not too close
- Target Position Calculation: Calculate the target positions for the square formation
- Iterative Updates:
- Calculate attractive and repulsive forces for each UAV
- Update UAV positions and velocities
- Record trajectories
- Check convergence status
- Visualization: Real-time display of UAV positions, trajectories, and target square
This project implements a 2D UAV square formation control algorithm based on artificial potential field method. Key features include:
- Leader-Follower Structure: The first UAV acts as the leader, while the rest are followers
- Artificial Potential Field Method: Combines attractive and repulsive forces for formation control and collision avoidance
- Adaptive Velocity Control: Dynamically adjusts velocity based on distance to target position
-
Attractive Force: UAVs are attracted to target positions
- Leader attraction:
LEADER_ATTRACT * direction vector
- Follower attraction:
FOLLOWER_ATTRACT * direction vector
- Leader attraction:
-
Repulsive Force: Collision avoidance force between UAVs
- Repulsive force is generated when the distance between two UAVs is less than the safe distance
- Force magnitude:
K_REPULSE * (1/distance - 1/SAFE_DISTANCE) * (1/distance²)
-
Collision Avoidance Priority: Adjusted through the
COLLISION_AVOIDANCE_PRIORITY
parameter
quadrangular_formation_control_2D.m
: Basic implementationquadrangular_formation_control_2D_V2.m
: Improved version with enhanced collision avoidancequadrangular_formation_control_2D_V3_white_bg.m
: White background version suitable for papers and reportscreate_subfigure_2D.py
: Python script for generating multi-panel displays
Main parameters and their meanings:
NUM_UAV = 20 % Number of UAVs
MAP_SIZE = 100 % Map size (square area side length)
CENTER_POSITION = [50, 50] % Center coordinates of the square formation
SQUARE_SIZE = 45 % Side length of the square formation
% Potential field parameters
SAFE_DISTANCE = 6 % Safe distance between UAVs
LEADER_ATTRACT = 4 % Leader attraction gain
FOLLOWER_ATTRACT = 2.5 % Follower attraction gain
K_REPULSE = 3 % Repulsive force gain for collision avoidance
MAX_VELOCITY_LEADER = 3 % Maximum velocity of the leader
MAX_VELOCITY_FOLLOWER = 2 % Maximum velocity of followers
To provide better visualization, the code includes rich visual parameter settings:
% Visual effect parameters
BACKGROUND_COLOR = [0.05, 0.05, 0.15] % Deep blue background
GRID_COLOR = [0.2, 0.2, 0.3] % Grid color
SQUARE_COLOR = [0.9, 0.7, 0.2] % Target square color (gold)
LEADER_COLOR = [0.95, 0.2, 0.2] % Leader color (red)
FOLLOWER_COLOR = [0.2, 0.6, 0.9] % Follower color (blue)
% Trail style parameters
LEADER_TRAIL_WIDTH = 3.0 % Leader trail line width
FOLLOWER_TRAIL_WIDTH = 2.2 % Follower trail line width
TRAIL_ALPHA = 0.8 % Trail transparency
TRAIL_GRADIENT = true % Whether to enable gradient effect
- Open the
quadrangular_formation_control_2D_V3_white_bg.m
file in MATLAB - Adjust parameters as needed
- Run the script to start the simulation
- Simulation results will be saved in the
uav_square_formation2D/frames_timestamp/
directory
Run the create_subfigure_2D.py
script to combine simulation-generated images into a multi-panel display:
python create_subfigure_2D.py
This script will automatically find the latest simulation results and generate a 4×2 panel suitable for papers or reports.
-
How to adjust the formation shape?
- Modify the
SQUARE_SIZE
parameter to change the size of the square - Modify
CENTER_POSITION
to change the center position of the formation
- Modify the
-
How to improve collision avoidance?
- Increase the
K_REPULSE
andCOLLISION_AVOIDANCE_PRIORITY
parameters - Increase the
SAFE_DISTANCE
parameter
- Increase the
-
How to speed up convergence?
- Increase the
LEADER_ATTRACT
andFOLLOWER_ATTRACT
parameters - Increase the
MAX_VELOCITY_LEADER
andMAX_VELOCITY_FOLLOWER
parameters
- Increase the
-
How to optimize visual effects?
- Adjust the
TRAIL_GRADIENT
andTRAIL_GRADIENT_COLORS
parameters - Modify color parameters such as
LEADER_COLOR
andFOLLOWER_COLOR
- Adjust the
The code includes multiple performance optimization measures:
% Performance optimization parameters
ENABLE_DRAWNOW = true % Use drawnow instead of pause to update graphics
SAVE_FRAME_INTERVAL = 5 % Save images every N frames (reduces I/O operations)
TRAIL_SIMPLIFY = true % Simplify trajectory drawing
OPTIMIZE_SCATTER = true % Optimize scatter plot drawing
These optimizations can significantly improve simulation speed, especially when handling a large number of UAVs.