Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
4d76f89
feat(uav_flight_base): 鏃犱汉鏈哄熀纭€椋炶妯″潡锛堥敭鐩橀仴鎺?+ 杩愬姩瀛︿豢鐪?+ 鐘舵€佺洃鎺э級
sqrt-crypto Nov 12, 2026
a2bdf18
feat(uav_flight_base): 鏃犱汉鏈哄熀纭€椋炶妯″潡锛堥敭鐩橀仴鎺?+ 杩愬姩瀛︿豢鐪?+ 鐘舵€佺洃鎺э級
sqrt-crypto Nov 12, 2026
3efc12a
feat(uav_flight_base): 鏃犱汉鏈哄熀纭€椋炶妯″潡锛堥敭鐩橀仴鎺?+ 杩愬姩瀛︿豢鐪?+ 鐘舵€佺洃鎺э級
sqrt-crypto Nov 12, 2026
27904a7
feat(uav_flight_base): 鏃犱汉鏈哄熀纭€椋炶妯″潡锛堥敭鐩橀仴鎺?+ 杩愬姩瀛︿豢鐪?+ 鐘舵€佺洃鎺э級
sqrt-crypto Nov 12, 2026
18d2ca0
feat(uav_flight_base): 鏃犱汉鏈哄熀纭€椋炶妯″潡锛堥敭鐩橀仴鎺?+ 杩愬姩瀛︿豢鐪?+ 鐘舵€佺洃鎺э級
sqrt-crypto Nov 12, 2026
f8448cc
feat(uav_flight_base): 鏃犱汉鏈哄熀纭€椋炶妯″潡锛堥敭鐩橀仴鎺?+ 杩愬姩瀛︿豢鐪?+ 鐘舵€佺洃鎺э級
sqrt-crypto Nov 12, 2026
d10b80f
feat(uav_flight_base): 鏃犱汉鏈哄熀纭€椋炶妯″潡锛堥敭鐩橀仴鎺?+ 杩愬姩瀛︿豢鐪?+ 鐘舵€佺洃鎺э級
sqrt-crypto Nov 12, 2026
114d853
feat(uav_flight_base): 鏃犱汉鏈哄熀纭€椋炶妯″潡锛堥敭鐩橀仴鎺?+ 杩愬姩瀛︿豢鐪?+ 鐘舵€佺洃鎺э級
sqrt-crypto Nov 12, 2026
a77385f
feat(uav_flight_base): 鏃犱汉鏈哄熀纭€椋炶妯″潡锛堥敭鐩橀仴鎺?+ 杩愬姩瀛︿豢鐪?+ 鐘舵€佺洃鎺э級
sqrt-crypto Nov 12, 2026
c92981c
feat(uav_flight_base): 加入 Gazebo 无人机模型、激光避障与视觉伺服
sqrt-crypto Nov 13, 2026
2532e7a
feat(uav_flight_base): lidar grid mapping + A-star navigation (task 3)
sqrt-crypto Nov 13, 2026
9111f37
feat(uav_flight_base): lidar grid mapping + A-star navigation (task 3)
sqrt-crypto Nov 13, 2026
9a20fdf
feat(uav_flight_base): lidar grid mapping + A-star navigation (task 3)
sqrt-crypto Nov 13, 2026
8543036
feat(uav_flight_base): tune visual servo, move target in front, add c…
sqrt-crypto Nov 13, 2026
bd7215a
feat(uav_flight_base): tune visual servo, move target in front, add c…
sqrt-crypto Nov 13, 2026
5d4865a
feat(uav_flight_base): tune visual servo, move target in front, add c…
sqrt-crypto Nov 13, 2026
8d10f79
feat(uav_flight_base): tune visual servo, move target in front, add c…
sqrt-crypto Nov 13, 2026
7d37355
feat(uav_flight_base): end-to-end behavior cloning (image to cmd_vel)…
sqrt-crypto Nov 13, 2026
fab8931
feat(uav_flight_base): end-to-end behavior cloning (image to cmd_vel)…
sqrt-crypto Nov 13, 2026
f403bf0
feat(uav_flight_base): end-to-end behavior cloning (image to cmd_vel)…
sqrt-crypto Nov 13, 2026
d8c7f8c
feat(uav_flight_base): end-to-end behavior cloning (image to cmd_vel)…
sqrt-crypto Nov 13, 2026
c886614
feat(uav_flight_base): end-to-end behavior cloning (image to cmd_vel)…
sqrt-crypto Nov 13, 2026
0b52526
feat(uav_flight_base): unified main.py entry for tasks 1-4, update RE…
sqrt-crypto Nov 13, 2026
34ade7b
feat(uav_flight_base): unified main.py entry for tasks 1-4, update RE…
sqrt-crypto Nov 13, 2026
2f6384a
feat(uav_flight_base): neural scan-to-cmd policy + multi-scene evalua…
sqrt-crypto Nov 13, 2026
8996718
feat(uav_flight_base): neural scan-to-cmd policy + multi-scene evalua…
sqrt-crypto Nov 13, 2026
7821060
feat(uav_flight_base): neural scan-to-cmd policy + multi-scene evalua…
sqrt-crypto Nov 13, 2026
607be25
feat(uav_flight_base): neural scan-to-cmd policy + multi-scene evalua…
sqrt-crypto Nov 13, 2026
a85e31d
feat(uav_flight_base): neural scan-to-cmd policy + multi-scene evalua…
sqrt-crypto Nov 13, 2026
7481789
feat(uav_flight_base): neural scan-to-cmd policy + multi-scene evalua…
sqrt-crypto Nov 13, 2026
4e262ab
docs(uav_flight_base): multi-scene evaluation figures
sqrt-crypto Nov 13, 2026
a96dc7b
docs(uav_flight_base): multi-scene evaluation figures
sqrt-crypto Nov 13, 2026
e488cb8
docs(uav_flight_base): multi-scene evaluation figures
sqrt-crypto Nov 13, 2026
d7667da
feat(uav_flight_base): policy width/optimizer sweep (task 4 comparison)
sqrt-crypto Nov 13, 2026
79a8670
feat(uav_flight_base): policy width/optimizer sweep (task 4 comparison)
sqrt-crypto Nov 13, 2026
a504073
feat(uav_flight_base): policy width/optimizer sweep (task 4 comparison)
sqrt-crypto Nov 13, 2026
597c0d3
feat(uav_flight_base): policy width/optimizer sweep (task 4 comparison)
sqrt-crypto Nov 13, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 150 additions & 0 deletions docs/uav_flight_base/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
# uav_flight_base

> 无人机基础飞行模块:键盘遥控 + 最小运动学仿真 + 状态监控
> 作者:唐健斌 | 环境:Ubuntu 20.04 + ROS Noetic

## 模块定位

课程作业要求里,"人体/车辆/无人机/水下机器人"任选其一。本模块选**无人机**,
并先解决最基础的一件事:**让一架无人机在仿真里被控制起来,并且状态可观测**。

不接 Gazebo、不接 AirSim 也能跑——因为用的是自己写的一阶惯性运动学模型,
这样在没有图形界面的服务器上也能验证"遥控 → 控制 → 状态"这条链路。

## 数据流

```
键盘 (uav_teleop_key)
│ /uav/cmd_vel (geometry_msgs/Twist)
▼
运动学仿真 (uav_mock_sim)
│ /uav/odom (nav_msgs/Odometry)
│ TF: map -> base_link
▼
状态监控 (uav_state_monitor)
│ 终端打印 / CSV
▼
RViz / rqt_plot(可选)
```

## 话题

| 话题 | 类型 | 方向 | 说明 |
|---|---|---|---|
| `/uav/cmd_vel` | `geometry_msgs/Twist` | 订阅 | 期望速度(机体系) |
| `/uav/odom` | `nav_msgs/Odometry` | 发布 | 位置、姿态、速度 |
| `/tf` | `tf2_msgs/TFMessage` | 发布 | `map` → `base_link` |

## 运动学模型

一阶惯性环节,避免"按键即瞬移"的不真实感:

```
dv/dt = (v_cmd - v) / τ
```

- `τ`(`~time_constant`,默认 0.35s)越大,响应越"肉"
- 速度分别裁剪到 `±max_speed`
- 高度限制在 `[z_min, z_max]`

## 启动

```bash
rosrun uav_flight_base main.py --mode mock
```

或

```bash
roslaunch uav_flight_base main.launch mode:=mock teleop:=false
```

## 验证清单

- [x] `rostopic hz /uav/odom` 稳定在 50Hz
- [x] 键盘 `w` 后 `/uav/odom` 的 `x` 单调增大
- [x] 松开按键后速度在约 1s 内衰减到 0(一阶惯性)
- [x] `csv:=/tmp/flight.csv` 能导出轨迹
- [ ] 接 Gazebo 模型(下一次提交)

## 设计取舍

1. **先不接 Gazebo**:Gazebo 的无人机模型(rotors / PX4)依赖重、装一次半小时且容易崩。
先用轻量运动学模型把控制链路和文档跑通,再把后端换掉,代码接口不变。
2. **`main.py` 只做调度**:真正的节点逻辑都在 `scripts/` 下,每个脚本可单独 `rosrun` 调试。
3. **参数全部走 rosparam**:不写死在代码里,方便后面接不同机型。

---

## 任务 2(神经网络版):扫描 → 速度的端到端策略

### 数据流

```
专家策略 uav_scan_avoider ──/cmd_vel──┐
├─→ uav_scan_dataset.py ─→ scan_dataset.csv
激光雷达 /uav/scan ───────────────────┘ (90 维扫描 + 动作标签)
│
▼
uav_train_scan_policy.py ─→ scan_policy.npz
│
▼
/uav/scan ─→ uav_scan_policy.py ─→ /cmd_vel
```

### 网络结构

```
输入 90 维激光扫描(-π..π 降采样,距离截断 3.5m,标准化)
隐层 96 单元 ReLU
输出 2 维(linear.x, angular.z),标准化后反变换
损失 MSE + L2(1e-5),SGD + 动量 0.9,lr=2e-3,400 epoch
```

样本 **1757** 条,按时间顺序切分(前 85% 训练、后 15% 验证,避免相邻帧泄漏),
最佳验证损失 **0.0728**。

### 多场景评估结果

| 指标 | 场景A 开阔场地 | 场景B 密集障碍 |
|---|---|---|
| 运行时长 | 116 s | 137 s |
| 平均速度 | 0.61 m/s | **0.03 m/s** |
| 平均角速度 | 0.21 rad/s | 0.12 rad/s |
| 最近障碍距离 | 0.66 m | 0.59 m |
| 贴障时间占比 (<0.6m) | 0.0 % | **90.5 %** |

**结论**:训练数据只来自开阔场地,策略在**同类场景表现良好**(全程未贴近障碍),
但在**未见过的密集场景中趋于保守停滞**(平均速度 0.03 m/s)——典型的行为克隆分布漂移。
后续可通过在密集场景补采数据、或加入 DAgger 式在线纠正来改善。

![场景A](images/policy_sceneA.png)

![场景B](images/policy_sceneB.png)

### 网络结构与优化器对比

课程要求“同一模型比较不同算法、同一算法比较不同网络结构”。在固定数据(1757 组)、
固定轮数(300)、固定随机种子的条件下,只改变隐层宽度与优化器,共 10 组实验。

| 隐层宽度 | 参数量 | 验证损失(SGD+动量) | 验证损失(Adam) |
|---|---|---|---|
| 32 | 2 978 | 0.06767 | **0.06549** |
| 64 | 5 954 | 0.06723 | 0.06803 |
| 96 | 8 930 | 0.07226 | 0.06734 |
| 128 | 11 906 | 0.08125 | 0.07058 |
| 256 | 23 810 | 0.07399 | 0.06931 |

复现方式:

```bash
rosrun uav_flight_base uav_policy_sweep.py # 输出 sweep_results.csv
```

**结论**:参数量从 2 978 增至 23 810(约 8 倍),验证损失并未下降;最优为宽度 32 配 Adam。
说明在本数据集规模下网络容量已不是瓶颈,此后再加宽只会增加优化难度。
这一结果与场景 B 的停滞现象互为印证——瓶颈在数据分布覆盖,而非模型容量。

![结构对比](images/policy_width_sweep.png)


11 changes: 11 additions & 0 deletions docs/uav_flight_base/data/sweep_results.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
hidden,optimizer,params,val_loss,seconds
32,sgd,2978,0.06767,1.2
32,adam,2978,0.06549,2.0
64,sgd,5954,0.06723,1.5
64,adam,5954,0.06803,2.3
96,sgd,8930,0.07226,1.7
96,adam,8930,0.06734,2.7
128,sgd,11906,0.08125,2.0
128,adam,11906,0.07058,3.0
256,sgd,23810,0.07399,5.0
256,adam,23810,0.06931,6.5
Binary file added docs/uav_flight_base/images/avoid_demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/uav_flight_base/images/camera_frame.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/uav_flight_base/images/nav_result.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/uav_flight_base/images/policy_compare.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/uav_flight_base/images/policy_sceneA.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/uav_flight_base/images/policy_sceneB.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/uav_flight_base/images/servo_demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 24 additions & 0 deletions src/uav_flight_base/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
cmake_minimum_required(VERSION 3.0.2)
project(uav_flight_base)

find_package(catkin REQUIRED COMPONENTS
rospy
geometry_msgs
nav_msgs
tf2_ros
)

catkin_package(
CATKIN_DEPENDS rospy geometry_msgs nav_msgs tf2_ros
)

catkin_install_python(PROGRAMS
scripts/uav_teleop_key.py
scripts/uav_mock_sim.py
scripts/uav_state_monitor.py
DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)

install(DIRECTORY launch
DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}
)
143 changes: 143 additions & 0 deletions src/uav_flight_base/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
# uav_flight_base

无人机仿真模块(ROS1 Noetic + Gazebo 11),对应课程要求的四个任务。

## 统一入口

所有任务都由 `main.py` 分发,一条命令即可运行:

```bash
python3 main.py --list # 查看全部任务
python3 main.py --task 1 # 键盘遥控 + 运动学仿真
python3 main.py --task 2 --mode lidar # 激光雷达感知 + 反应式避障
python3 main.py --task 2 --mode vision # 相机颜色检测 + 视觉伺服
python3 main.py --task 3 # 栅格建图 + A* 导航
python3 main.py --task 4 --stage collect # 端到端网络:采集
python3 main.py --task 4 --stage train # 端到端网络:训练
python3 main.py --task 4 --stage drive # 端到端网络:飞行
```

| 任务 | 感知 | 控制 | 对应 launch |
|---|---|---|---|
| 1 键盘遥控 | — | 键盘 → Twist | `main.launch` |
| 2 传感器感知 | 360 线激光 | 反应式避障 | `avoid.launch` |
| 2 传感器感知 | 相机(HSV) | PID 视觉伺服 | `servo.launch` |
| 3 建图导航 | 激光 → 占据栅格 | A* + 纯追踪 | `nav.launch` |
| 4 端到端 | 相机图像 | 神经网络 | `main.py --task 4` |

> 无图形环境(纯 SSH)时,用
> `xvfb-run -a -s "-screen 0 1024x768x24" python3 main.py --task 2 --mode vision`
> 给 Gazebo 相机提供虚拟显示器。

## 模块说明(第一部分:基础链路)

提供**键盘遥控**、**最小运动学仿真**、**状态监控**三部分,
不依赖 Gazebo / AirSim 即可完整跑通,用于验证"遥控 → 控制 → 状态反馈"这条链路。

> 这是 10 次提交中的第 1 次:只做骨架与可运行的最小闭环。后续按任务 2/3/4 逐步加感知、建图、端到端网络。

## 目录结构

```
uav_flight_base/
├── main.py # 入口(仓库约定:入口以 main. 开头)
├── CMakeLists.txt
├── package.xml
├── launch/
│ └── main.launch # ROS 启动配置(仓库约定:main.launch)
└── scripts/
├── uav_teleop_key.py # 键盘 → /uav/cmd_vel
├── uav_mock_sim.py # 运动学仿真 → /uav/odom + TF
└── uav_state_monitor.py # 状态打印 / CSV 记录
```

## 环境

| 项目 | 版本 |
|---|---|
| 系统 | Ubuntu 20.04 |
| ROS | Noetic |
| Python | 3.8 |
| 依赖 | rospy / geometry_msgs / nav_msgs / tf2_ros(ROS 自带) |

## 安装

```bash
# 1. 放进 catkin 工作空间
cp -r uav_flight_base ~/catkin_ws/src/

# 2. 编译
cd ~/catkin_ws
catkin_make
source devel/setup.bash
```

## 运行

**方式一:看效果(需要图形界面)**

```bash
rosrun uav_flight_base main.py --mode mock
```

会启动仿真节点 + 状态监控 + 键盘遥控窗口(xterm)。

**方式二:只跑链路(不需要键盘)**

```bash
roslaunch uav_flight_base main.launch mode:=mock teleop:=false
```

另开一个终端手动发速度指令:

```bash
rostopic pub -r 10 /uav/cmd_vel geometry_msgs/Twist \
'{linear: {x: 1.0, y: 0.0, z: 0.5}, angular: {z: 0.3}}'
```

**方式三:记录飞行数据**

```bash
roslaunch uav_flight_base main.launch mode:=mock teleop:=false csv:=/tmp/flight.csv
```

## 键盘操作

| 按键 | 动作 |
|---|---|
| `w` / `s` | 前进 / 后退 |
| `a` / `d` | 左移 / 右移 |
| `r` / `f` | 上升 / 下降 |
| `q` / `e` | 左转 / 右转 |
| `空格` | 悬停(速度清零) |
| `Ctrl+C` | 退出 |

## 预期输出

```
[uav_flight_base] mode = mock
[INFO] [uav_state_monitor]: [ 0.5s] pos=( 0.42, 0.00, 1.50) yaw= 0.0° v=0.61 m/s
[INFO] [uav_state_monitor]: [ 1.0s] pos=( 0.88, 0.12, 1.62) yaw= 6.9° v=0.95 m/s
```

## 参数

| 参数 | 默认 | 说明 |
|---|---|---|
| `~rate` | 50.0 | 仿真频率 Hz |
| `~time_constant` | 0.35 | 一阶惯性时间常数,越大越"肉" |
| `~max_speed` | 5.0 | 最大速度 m/s |
| `~z_min` / `~z_max` | 0.2 / 30.0 | 高度限制 m |
| `~cmd_topic` | `/uav/cmd_vel` | 速度指令话题 |
| `~odom_topic` | `/uav/odom` | 里程计话题 |

## 后续计划

- [ ] 接 Gazebo 无人机模型(`mode:=gazebo`)
- [ ] 深度图避障
- [ ] 栅格建图 + A* 导航
- [ ] 端到端 CNN(图像 → 控制)

## 许可

MIT
18 changes: 18 additions & 0 deletions src/uav_flight_base/launch/avoid.launch
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0"?>
<!-- 任务2(无图形环境版):激光雷达感知 + 反应式避障 -->
<launch>
<arg name="gui" default="false"/>
<arg name="csv" default="/tmp/avoid.csv"/>

<include file="$(find uav_flight_base)/launch/gazebo.launch">
<arg name="gui" value="$(arg gui)"/>
</include>

<node name="uav_scan_avoider" pkg="uav_flight_base" type="uav_scan_avoider.py"
output="screen">
<param name="scan_topic" value="/uav/scan"/>
<param name="odom_topic" value="/odom"/>
<param name="cmd_topic" value="/cmd_vel"/>
<param name="csv" value="$(arg csv)"/>
</node>
</launch>
29 changes: 29 additions & 0 deletions src/uav_flight_base/launch/gazebo.launch
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0"?>
<!-- 在 Gazebo 中加载简化四旋翼(带前置相机与激光雷达) -->
<launch>
<arg name="gui" default="false"/>
<arg name="x" default="0.0"/>
<arg name="y" default="0.0"/>
<arg name="z" default="1.0"/>
<arg name="world" default="$(find uav_flight_base)/worlds/uav_world.world"/>

<include file="$(find gazebo_ros)/launch/empty_world.launch">
<arg name="world_name" value="$(arg world)"/>
<arg name="gui" value="$(arg gui)"/>
<arg name="paused" value="false"/>
<arg name="use_sim_time" value="true"/>
</include>

<param name="robot_description"
command="cat $(find uav_flight_base)/urdf/uav_simple.urdf"/>

<node name="spawn_uav" pkg="gazebo_ros" type="spawn_model"
args="-urdf -param robot_description -model uav -x $(arg x) -y $(arg y) -z $(arg z)"
output="screen" respawn="false"/>

<node name="uav_state_monitor" pkg="uav_flight_base" type="uav_state_monitor.py"
output="screen">
<param name="odom_topic" value="/odom"/>
<param name="period" value="1.0"/>
</node>
</launch>
Loading