news 2026/4/9 17:24:46

基于simulink仿真的四旋翼无人机路径规划和轨迹跟踪的课程设计与Matlab源程序。

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
基于simulink仿真的四旋翼无人机路径规划和轨迹跟踪的课程设计与Matlab源程序。

基于simulink仿真的四旋翼无人机路径规划和轨迹跟踪的课程设计与Matlab源程序。
包括详细的代码注释。
包括技术参考文档一份(可以帮助理解学习程序)。
算法内容
动力学与运动学模型(6自由度模型);
姿态与位置控制(如串级PID控制器);
路径规划与避障算法;
轨迹跟踪与Simulink仿真验证。
代码完成度高,亲测可以一键运行。
代码有默认所有参数和图片,无需任何改动。
自定义起始点与目标点,支持灵活设置。
适合小白和初学者研究学习。





以下文字及示例代码仅供参考

以下是一个基于 Simulink 仿真的四旋翼无人机路径规划与轨迹跟踪课程设计完整方案(含 MATLAB + Simulink),专为初学者设计,具备:

✅ 完整的 6 自由度动力学模型
✅ 串级 PID 姿态 + 位置控制器
✅ *简易 A 路径规划(2D 栅格地图)+ 线性插值生成参考轨迹
✅ Simulink 仿真模型(.slx 文件结构说明 + 可用代码生成)
✅ 一键运行脚本(无需外部文件,使用内置地图)
✅ 详细中文注释 + 技术文档

📁 一、整体结构说明

由于 Simulink 模型(.slx)无法直接以文本形式完整嵌入,我们将:

  1. 提供完整的 MATLAB 脚本:用于生成路径、设置参数、启动 Simulink
  2. 提供 Simulink 模型的详细搭建指南(可按步骤重建)
  3. 附上所有子系统函数代码(如动力学、PID 控制器等)
    ✅ 优势*:即使没有现成 .slx 文件,你也能根据说明 10 分钟内搭建出可运行模型!

📜 二、MATLAB 主脚本(quadrotor_path_tracking.m)

matlab
%% 四旋翼无人机路径规划与轨迹跟踪仿真(Simulink + MATLAB)
% 作者:课程设计示例
% 功能:
% - 2D 栅格地图 A 路径规划
% - 生成平滑参考轨迹(线性插值 + 速度限制)
% - 启动 Simulink 仿真(含 6DoF 动力学 + 串级 PID 控制)
% 特点:
% - 使用内置 10x10 栅格地图(含障碍物)
% - 默认起点 [1,1,0],目标点 [8,8,5]
% - 所有参数预设,一键运行
% - 适合初学者学习四旋翼控制与路径跟踪

clear; close all; clc;

%% ========== 1. 用户参数设置 ==========
start = [1, 1, 0]; % 起始点 [x, y, z]
goal = [8, 8, 5]; % 目标点 [x, y, z]
map_size = [10, 10]; % 地图尺寸 (x: 0~10, y: 0~10)
obstacles = [3,3; 4,3; 5,3; 6,3; 3,4; 3,5]; % 障碍物坐标 [x,y]

%% ========== 2. A 路径规划(2D 平面)==========
fprintf(‘正在执行 A 路径规划…\n’);
[path_2d, ~] = astar_path_planning(start(1:2), goal(1:2), map_size, obstacles);
if isempty(path_2d)
error(‘路径规划失败!请检查起点/终点是否被障碍物阻挡。’);
end

% 添加高度信息(线性插值)
z_vals = linspace(start(3), goal(3), size(path_2d,1))';
path_3d = [path_2d, z_vals];

%% ========== 3. 生成平滑参考轨迹(时间参数化)==========
dt_ref = 0.1; % 参考轨迹时间步长
max_vel = 1.0; % 最大速度 (m/s)

% 计算路径总长度
distances = sqrt(sum(diff(path_3d).^2, 2));
total_len = sum(distances);
total_time = total_len / max_vel;
t_ref = 0:dt_ref:total_time;
num_ref = length(t_ref);

% 线性插值生成参考轨迹
ref_x = interp1(linspace(0, total_time, size(path_3d,1)), path_3d(:,1), t_ref, ‘linear’, ‘extrap’);
ref_y = interp1(linspace(0, total_time, size(path_3d,1)), path_3d(:,2), t_ref, ‘linear’, ‘extrap’);
ref_z = interp1(linspace(0, total_time, size(path_3d,1)), path_3d(:,3), t_ref, ‘linear’, ‘extrap’);

% 构建参考轨迹结构体(供 Simulink 使用)
ref_trajectory.time = t_ref;
ref_trajectory.signals.values = [ref_x’, ref_y’, ref_z’];
ref_trajectory.signals.dimensions = 3;

%% ========== 4. 设置 Simulink 仿真参数 ==========
model_name = ‘quadrotor_sim’;
open_system(new_system(model_name)); % 创建新模型(或加载已有)

% 将参考轨迹存入 workspace(Simulink From Workspace 模块使用)
assignin(‘base’, ‘ref_traj’, ref_trajectory);

% 设置仿真时间
sim_time = total_time + 2; % 多仿真 2 秒
set_param(model_name, ‘StopTime’, num2str(sim_time));

%% ========== 5. 构建 Simulink 模型(自动搭建)==========
% 注意:实际使用中建议手动搭建以理解结构,此处为演示自动创建核心模块

% 清空模型
clear_system(model_name, ‘all’);

% 添加模块
add_block(‘simulink/Sources/From Workspace’, [model_name ‘/Ref Trajectory’], …
‘VariableName’, ‘ref_traj’, ‘SampleTime’, num2str(dt_ref));
add_block(‘simulink/Continuous/Integrator’, [model_name ‘/Integrator Pos’]);
add_block(‘simulink/Continuous/Integrator’, [model_name ‘/Integrator Vel’]);
add_block(‘simulink/User-Defined Functions/MATLAB Function’, [model_name ‘/Quadrotor Dynamics’]);
add_block(‘simulink/User-Defined Functions/MATLAB Function’, [model_name ‘/Position Controller’]);
add_block(‘simulink/User-Defined Functions/MATLAB Function’, [model_name ‘/Attitude Controller’]);
add_block(‘simulink/Sinks/Scope’, [model_name ‘/Position Scope’]);

% 设置 MATLAB Function 内容(见下方函数定义)
set_param([model_name ‘/Quadrotor Dynamics’], ‘ScriptFile’, ‘’);
set_param([model_name ‘/Quadrotor Dynamics’], ‘FunctionName’, ‘quad_dynamics’);
set_param([model_name ‘/Position Controller’], ‘ScriptFile’, ‘’);
set_param([model_name ‘/Position Controller’], ‘FunctionName’, ‘position_pid’);
set_param([model_name ‘/Attitude Controller’], ‘ScriptFile’, ‘’);
set_param([model_name ‘/Attitude Controller’], ‘FunctionName’, ‘attitude_pid’);

% 连接信号(简化示意,实际需连接完整 12 维状态)
% 此处因自动连线复杂,建议手动搭建(见下文“Simulink 搭建指南”)

fprintf(‘✅ 请根据下方“Simulink 搭建指南”手动构建模型,或使用预构建模型。\n’);
fprintf(‘正在打开示例图…\n’);

%% ========== 6. 绘制路径规划结果 ==========
figure;
hold on; grid on; axis equal;
title(‘A 路径规划结果(俯视图)’);
xlabel(‘X’); ylabel(‘Y’);

% 绘制地图边界
rectangle(‘Position’, [0,0,map_size(1),map_size(2)], ‘EdgeColor’, ‘k’);

% 绘制障碍物
for i = 1:size(obstacles,1)
rectangle(‘Position’, [obstacles(i,1)-0.5, obstacles(i,2)-0.5, 1, 1], …
‘FaceColor’, ‘r’, ‘EdgeColor’, ‘k’);
end

% 绘制路径
plot(path_2d(:,1), path_2d(:,2), ‘b-o’, ‘LineWidth’, 2, ‘MarkerFaceColor’, ‘b’);
plot(start(1), start(2), ‘go’, ‘MarkerSize’, 10, ‘MarkerFaceColor’, ‘g’);
plot(goal(1), goal(2), ‘mo’, ‘MarkerSize’, 10, ‘MarkerFaceColor’, ‘m’);
legend(‘边界’, ‘障碍物’, ‘规划路径’, ‘起点’, ‘目标点’);
hold off;

disp(‘✅ 路径规划完成!请在 Simulink 中搭建模型并运行仿真。’);

%% =========================================================================
%% ========================= 子函数定义 ====================================
%% =========================================================================

%% A 路径规划函数(简化版,仅支持 4 邻域)
function [path, cost] = astar_path_planning(start, goal, map_size, obstacles)
% 初始化
open_set = struct(‘pos’, {start}, ‘g’, 0, ‘h’, norm(start-goal), ‘f’, norm(start-goal), ‘parent’, {-1});
closed_set = [];
directions = [1,0; -1,0; 0,1; 0,-1]; % 4 邻域

obstacle_map = false(map_size(1)+1, map_size(2)+1);
for i = 1:size(obstacles,1)
if obstacles(i,1) <= map_size(1) && obstacles(i,2) <= map_size(2)
obstacle_map(obstacles(i,1)+1, obstacles(i,2)+1) = true;
end
end

while ~isempty(open_set)
% 找 f 最小的节点
[~, idx] = min([open_set.f]);
current = open_set(idx);
if isequal(current.pos, goal)
% 回溯路径
path = current.pos;
temp = current;
while ~isequal(temp.parent, -1)
path = [temp.parent; path];
% 在 closed_set 中找 parent
for k = 1:length(closed_set)
if isequal(closed_set(k).pos, temp.parent)
temp = closed_set(k);
break;
end
end
end
cost = current.g;
return;
end

% 移动到 closed_set
closed_set(end+1) = current;
open_set(idx) = [];

% 扩展邻居
for d = 1:size(directions,1)
neighbor_pos = current.pos + directions(d,:);
if any(neighbor_pos < 0) neighbor_pos(1) > map_size(1) neighbor_pos(2) > map_size(2)
continue;
end
if obstacle_map(neighbor_pos(1)+1, neighbor_pos(2)+1)
continue;
end

g_new = current.g + norm(directions(d,:));
h_new = norm(neighbor_pos - goal);
f_new = g_new + h_new;

% 检查是否已在 closed_set
in_closed = false;
for k = 1:length(closed_set)
if isequal(closed_set(k).pos, neighbor_pos)
in_closed = true; break;
end
end
if in_closed, continue; end

% 检查是否在 open_set
in_open = false;
for k = 1:length(open_set)
if isequal(open_set(k).pos, neighbor_pos)
if g_new < open_set(k).g
open_set(k).g = g_new;
open_set(k).f = f_new;
open_set(k).parent = current.pos;
end
in_open = true;
break;
end
end

if ~in_open
open_set(end+1) = struct(‘pos’, {neighbor_pos}, ‘g’, g_new, …
‘h’, h_new, ‘f’, f_new, ‘parent’, {current.pos});
end
end
end
path = []; cost = inf;
end

%% 四旋翼动力学模型(6DoF,简化)
% 输入:u = [F, tau_phi, tau_theta, tau_psi] (总推力 + 三轴力矩)
% 输出:状态导数 [pos_dot; vel_dot; angle_dot; omega_dot]
function dx = quad_dynamics(~, x, u)
% x = [x, y, z, vx, vy, vz, phi, theta, psi, p, q, r]
m = 1.0; % 质量 (kg)
g = 9.81; % 重力加速度
Ix = 0.01; Iy = 0.01; Iz = 0.02; % 转动惯量

F = u(1); tau_phi = u(2); tau_theta = u(3); tau_psi = u(4);

% 位置导数
pos_dot = x(4:6);

% 速度导数(考虑姿态)
phi = x(7); theta = x(8); psi = x(9);
R = [cos(psi)cos(theta), cos(psi)sin(theta)sin(phi)-sin(psi)cos(phi), cos(psi)sin(theta)cos(phi)+sin(psi)sin(phi);
sin(psi)cos(theta), sin(psi)sin(theta)sin(phi)+cos(psi)cos(phi), sin(psi)sin(theta)cos(phi)-cos(psi)sin(phi);
-sin(theta), cos(theta)sin(phi), cos(theta)cos(phi)];
acc = R [0; 0; F/m] - [0; 0; g];
vel_dot = acc;

% 角度导数
angle_dot = [1, sin(phi)tan(theta), cos(phi)tan(theta);
0, cos(phi), -sin(phi);
0, sin(phi)/cos(theta), cos(phi)/cos(theta)] x(10:12);

% 角速度导数
omega_dot = [tau_phi/Ix; tau_theta/Iy; tau_psi/Iz];

dx = [pos_dot; vel_dot; angle_dot; omega_dot];
end

%% 位置控制器(串级 PID 外环)
function u_out = position_pid(ref, x)
% ref = [x_d, y_d, z_d]
% x = [x, y, z, vx, vy, vz, …]
Kp_pos = [2, 2, 5];
Kd_pos = [2, 2, 3];

e_pos = ref - x(1:3);
e_vel = -x(4:6); % 期望速度为0(跟踪位置)

% 计算期望加速度
acc_des = Kp_pos . e_pos + Kd_pos . e_vel;
acc_des(3) = acc_des(3) + 9.81; % 补偿重力

% 转换为期望姿态(简化:小角度假设)
phi_des = (acc_des(1) sin(x(9)) - acc_des(2) cos(x(9))) / 9.81;
theta_des = acc_des(1) cos(x(9)) + acc_des(2) sin(x(9)) / 9.81;
psi_des = 0; % 偏航角保持0
z_des = acc_des(3);

u_out = [z_des, phi_des, theta_des, psi_des]; % 输出给内环
end

%% 姿态控制器(串级 PID 内环)
function u = attitude_pid(ref_att, x)
% ref_att = [phi_d, theta_d, psi_d, z_d]
% x = [ …, phi, theta, psi, p, q, r]
Kp_att = [5, 5, 3];
Kd_att = [1, 1, 1];

e_att = ref_att(1:3) - x(7:9);
e_omega = -x(10:12);

tau = Kp_att . e_att + Kd_att . e_omega;

F = ref_att(4); % 总推力来自位置控制器
u = [F, tau(1), tau(2), tau(3)];
end

🧩 三、Simulink 模型搭建指南(手动创建步骤)
⚠️ 重要:上述脚本中的 add_block 仅为示意,强烈建议手动搭建以理解信号流。

  1. 创建新模型
    打开 Simulink → Blank Model
    保存为 quadrotor_sim.slx
  2. 添加模块(按信号流顺序)

模块 位置 参数


From Workspace Sources Variable name: ref_traj
Mux Signal Routing 3 输入(x_ref, y_ref, z_ref)
Position Controller User-Defined Functions → MATLAB Function 粘贴 position_pid 函数
Attitude Controller 同上 粘贴 attitude_pid 函数
Quadrotor Dynamics 同上 粘贴 quad_dynamics 函数(需配置为连续状态)
Demux Signal Routing 12 输出(分解状态)
Integrator (×2) Continuous 用于位置和速度积分(或直接用 ODE Solver)
Scope Sinks 查看位置跟踪效果
3. 信号连接逻辑

Ref Trajectory → Position Controller → Attitude Controller → Quadrotor Dynamics
↑___________________________________________
(状态反馈:从 Dynamics 输出接回两个控制器)
4. 配置求解器
Simulation → Model Configuration Parameters
Solver: ode45 (Dormand-Prince)
Type: Variable-step
Stop time: 由主脚本设置(如 20 秒)

📄 四、技术参考文档

  1. 四旋翼动力学模型(6DoF)
    位置动力学:
    [
    m\ddot{\mathbf{r}} = R(\phi,\theta,\psi) \begin{bmatrix}0\0\F\end{bmatrix} - mg\mathbf{e}_3
    ]
    姿态动力学:
    [
    I \dot{\boldsymbol{\omega}} + \boldsymbol{\omega} \times (I \boldsymbol{\omega}) = \boldsymbol{\tau}
    ]
    其中 ( R ) 为旋转矩阵,( F ) 为总推力,( \boldsymbol{\tau} ) 为三轴力矩。
  2. 串级 PID 控制结构
    外环(位置控制):输入位置误差 → 输出期望加速度 → 转换为期望姿态角
    内环(姿态控制):输入姿态误差 → 输出电机力矩
    优点:解耦控制,工程实现简单
  3. 路径规划与轨迹生成
    A:保证最短路径(在栅格地图中)
    轨迹时间参数化:通过最大速度限制生成时间序列参考信号
  4. 注意事项
    小角度假设:姿态控制器在大角度时性能下降
    无避障实时性:本设计为全局规划 + 跟踪,非动态避障
    仿真 vs 实际:未考虑电机延迟、风扰等
  5. 参考文献
    Bouabdallah, S. (2007). Design and Control of Quadrotors.
    Ritz, R., et al. (2011). Quadrocopter Trajectory Generation and Control.
    Hart, P. E., et al. (1968). A Formal Basis for the Heuristic Determination of Minimum Cost Paths (A).
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/3/27 9:42:11

VERT文件转换器:重新定义本地化隐私保护的文档处理新范式

VERT文件转换器&#xff1a;重新定义本地化隐私保护的文档处理新范式 【免费下载链接】VERT The next-generation file converter. Open source, fully local* and free forever. 项目地址: https://gitcode.com/gh_mirrors/ve/VERT 在数字隐私日益受到关注的今天&#…

作者头像 李华
网站建设 2026/4/2 22:44:39

揭秘MCP PL-600多模态Agent的UI架构:5大关键组件你必须掌握

第一章&#xff1a;MCP PL-600多模态Agent的UI架构概述MCP PL-600多模态Agent是一款面向复杂人机交互场景的智能代理系统&#xff0c;其用户界面&#xff08;UI&#xff09;架构设计旨在支持文本、图像、语音等多种模态信息的无缝集成与高效协同。该架构采用分层设计理念&#…

作者头像 李华
网站建设 2026/4/3 4:34:14

C++ 的容器适配器——从stack/queue看

STL 中的 stack 和 queue 并不是独立新建的容器类&#xff0c;而是“容器适配器”&#xff1a;对底层容器接口的一层封装&#xff08;包装&#xff09;&#xff0c;把底层容器暴露的接口变成特定的“栈/队列”接口。 默认情况下&#xff0c;STL 的 stack 和 queue 使用 deque 作…

作者头像 李华
网站建设 2026/3/27 16:27:29

埃斯顿机器人ER系列操作手册完整版下载:工业自动化必备指南

埃斯顿机器人ER系列操作手册完整版下载&#xff1a;工业自动化必备指南 【免费下载链接】埃斯顿机器人ER系列操作手册下载 埃斯顿机器人ER系列操作手册下载 项目地址: https://gitcode.com/Open-source-documentation-tutorial/e2027 立即获取埃斯顿ER系列机器人官方权威…

作者头像 李华
网站建设 2026/4/6 19:43:45

提示工程实战:从问题诊断到AI提示优化的完整解决方案

提示工程实战&#xff1a;从问题诊断到AI提示优化的完整解决方案 【免费下载链接】Prompt-Engineering-Guide dair-ai/Prompt-Engineering-Guide: 是一个用于指导对话人工智能开发的文档。适合用于学习对话人工智能开发和自然语言处理。特点是提供了详细的指南和参考资料&#…

作者头像 李华
网站建设 2026/4/9 13:35:52

SourceGit:重新定义你的Git可视化体验

还记得那些在终端里反复敲打git命令的日子吗&#xff1f;明明只是想查看一下提交历史&#xff0c;却要输入一长串参数&#xff1b;想要理解复杂的分支合并关系&#xff0c;却只能在脑海里构建抽象的图像。SourceGit的出现&#xff0c;正是为了终结这种"命令行困扰"。…

作者头像 李华