Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
38 changes: 38 additions & 0 deletions src/mmap_drl_nav/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# 机器人导航系统 - 环境配置与运行手册
## 一、环境设置
运行本项目代码前,需确保开发环境满足以下依赖版本要求:
- Python 3.7 及以上版本
- PyTorch 1.7 及以上版本
- CARLA 模拟器 0.9.11 版本
- 其余 Python 依赖项详见项目根目录下的 `requirements.txt` 文件

## 二、依赖安装步骤
### 1. 安装Python依赖
```bash
pip install -r requirements.txt
```
## 三、代码运行方法
### 运行系统模拟
执行以下命令可启动CARLA模拟器,加载完整的感知、注意力与决策模块,生成机器人导航策略:
```bash
python run_simulation.py
```

## 四、模型训练与测试
### 1. 训练模型
执行以下命令加载模拟数据集,训练感知、跨域注意力和决策模块:
```bash
python main.py --mode train
```
> 说明:训练参数、超参数等可在 `main.py` 文件中按需配置。

### 2. 测试模型
模型训练完成后,执行以下命令加载测试数据集,评估模型在新数据上的表现并输出测试损失:
```bash
python main.py --mode test
```

### 总结
1. 环境核心要求:Python 3.7+、PyTorch 1.7+、CARLA 0.9.11,需确保CARLA环境变量配置正确;
2. 核心操作命令:模拟运行用 `run_simulation.py`,模型训练/测试用 `main.py --mode [train/test]`;
3. Jetson部署关键步骤:先训练导出ONNX模型,再通过 `deploy.py` 指定模型路径加载推理。
59 changes: 59 additions & 0 deletions src/mmap_drl_nav/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import torch
import torch.nn as nn
from torch.optim import Adam
from utils.dataloader import get_dataloader
from models.perception_module import PerceptionModule
from models.attention_module import CrossDomainAttention
from models.decision_module import DecisionModule
from models.sagm import SelfAssessmentGradientModel

class IntegratedSystem:
def __init__(self, device='cpu', state_dim=128, action_dim=2):
self.device = device
self.perception = PerceptionModule().to(self.device)
self.attention = CrossDomainAttention(num_blocks=6).to(self.device)
self.decision = DecisionModule().to(self.device)
self.sagm = SelfAssessmentGradientModel(input_dim=state_dim + action_dim).to(self.device)

def forward(self, image, lidar_data, imu_data, action):
scene_info, segmentation, odometry, obstacles, boundary = self.perception(imu_data, image, lidar_data)
fused_features = self.attention(scene_info, segmentation, odometry, obstacles, boundary)

policy, value = self.decision(fused_features)

sagm_q_value = self.sagm(fused_features, action)
return policy, value, sagm_q_value

def train_model(model, dataloader, optimizer, device, num_epochs=10):
model.train()
for epoch in range(num_epochs):
running_loss = 0.0
for i, (image, lidar_data, imu_data, target_action) in enumerate(dataloader):
image, lidar_data, imu_data, target_action = image.to(device), lidar_data.to(device), imu_data.to(device), target_action.to(device)
optimizer.zero_grad()
policy_output, value_output, sagm_q_value = model(image, lidar_data, imu_data, target_action)

loss = (nn.MSELoss()(policy_output, target_action) +
nn.MSELoss()(value_output, target_action.sum(dim=1, keepdim=True)) +
nn.MSELoss()(sagm_q_value, target_action.sum(dim=1, keepdim=True)))
loss.backward()
optimizer.step()
running_loss += loss.item()
if i % 10 == 9:
print(f'Epoch [{epoch+1}/{num_epochs}], Batch [{i+1}/{len(dataloader)}], Loss: {running_loss / 10:.4f}')
running_loss = 0.0
print('Training complete')

def test_model(model, dataloader, device):
model.eval()
total_loss = 0.0
with torch.no_grad():
for image, lidar_data, imu_data, target_action in dataloader:
image, lidar_data, imu_data, target_action = image.to(device), lidar_data.to(device), imu_data.to(device), target_action.to(device)
policy_output, value_output, sagm_q_value = model(image, lidar_data, imu_data, target_action)
loss = (nn.MSELoss()(policy_output, target_action) +
nn.MSELoss()(value_output, target_action.sum(dim=1, keepdim=True)) +
nn.MSELoss()(sagm_q_value, target_action.sum(dim=1, keepdim=True)))
total_loss += loss.item()
avg_loss = total_loss / len(dataloader)
print(f'Test Average Loss: {avg_loss:.4f}')
215 changes: 215 additions & 0 deletions src/mmap_drl_nav/run_simulation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
import torch
import time
import logging
import carla
from typing import Tuple, Optional
from models.perception_module import PerceptionModule
from models.attention_module import CrossDomainAttention
from models.decision_module import DecisionModule
from models.dqn_agent import DQNAgent
from envs.carla_environment import CarlaEnvironment

# 配置日志
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[
logging.StreamHandler()
]
)
logger = logging.getLogger(__name__)

# 系统配置
CONFIG = {
"simulation_steps": 100,
"step_delay": 0.1,
"carla_host": "localhost",
"carla_port": 2000,
"image_size": (3, 256, 256),
"lidar_size": (1, 256, 256),
"imu_size": (1, 6),
"max_throttle": 1.0,
"max_steer": 1.0
}


class IntegratedSystem:
"""
自动驾驶集成系统,整合感知、注意力和决策模块
"""

def __init__(self, device: str = 'cpu'):
self.device = torch.device(device)
self.logger = logging.getLogger(self.__class__.__name__)

# 初始化各个模块
try:
self.perception = PerceptionModule().to(self.device)
self.attention = CrossDomainAttention(num_blocks=6).to(self.device)
self.decision = DecisionModule().to(self.device)
self.logger.info(f"成功初始化所有模块,使用设备: {self.device}")
except Exception as e:
self.logger.error(f"模块初始化失败: {e}")
raise

def forward(self, image: torch.Tensor, lidar_data: torch.Tensor, imu_data: torch.Tensor) -> Tuple[
torch.Tensor, torch.Tensor]:
"""
前向传播,处理输入数据并输出策略和价值

Args:
image: 图像数据 [B, 3, H, W]
lidar_data: LiDAR数据 [B, 1, H, W]
imu_data: IMU数据 [B, 6]

Returns:
policy: 决策策略 [B, num_actions]
value: 状态价值 [B, 1]
"""
try:
# 感知模块处理
scene_info, segmentation, odometry, obstacles, boundary = self.perception(imu_data, image, lidar_data)

# 跨域注意力融合特征
fused_features = self.attention(scene_info, segmentation, odometry, obstacles, boundary)

# 决策模块输出
policy, value = self.decision(fused_features)

return policy, value
except Exception as e:
self.logger.error(f"前向传播过程出错: {e}")
raise


def init_carla_environment(host: str = "localhost", port: int = 2000) -> Optional[CarlaEnvironment]:
"""
初始化CARLA环境

Args:
host: CARLA服务器地址
port: CARLA服务器端口

Returns:
初始化后的CarlaEnvironment实例
"""
try:
env = CarlaEnvironment(host=host, port=port)
# 等待环境加载完成
time.sleep(2.0)
logger.info(f"成功连接到CARLA服务器 {host}:{port}")

# 检查车辆是否正确初始化
if hasattr(env, 'vehicle') and env.vehicle is not None:
logger.info("CARLA车辆已成功初始化")
else:
logger.error("CARLA车辆初始化失败")
return None

return env
except Exception as e:
logger.error(f"CARLA环境初始化失败: {e}")
return None


def clamp_control_values(throttle: float, steer: float) -> Tuple[float, float]:
"""
限制控制信号在合法范围内

Args:
throttle: 油门值
steer: 转向值

Returns:
限制后的油门和转向值
"""
# 限制油门在 0-1 之间
throttle_clamped = max(0.0, min(CONFIG["max_throttle"], throttle))
# 限制转向在 -1 到 1 之间
steer_clamped = max(-CONFIG["max_steer"], min(CONFIG["max_steer"], steer))

return throttle_clamped, steer_clamped


def run_simulation():
"""运行CARLA仿真主函数"""
# 1. 初始化CARLA环境
env = init_carla_environment(
host=CONFIG["carla_host"],
port=CONFIG["carla_port"]
)
if env is None:
logger.error("无法初始化CARLA环境,退出仿真")
return

# 2. 初始化集成系统
try:
device = 'cuda' if torch.cuda.is_available() else 'cpu'
system = IntegratedSystem(device=device)
logger.info(f"集成系统初始化完成,使用设备: {device}")
except Exception as e:
logger.error(f"集成系统初始化失败: {e}")
return

# 3. 运行仿真步骤
logger.info(f"开始运行{CONFIG['simulation_steps']}步仿真")
for step in range(CONFIG["simulation_steps"]):
try:
# 生成模拟输入数据(实际应用中应从CARLA环境获取真实数据)
image = torch.randn(*CONFIG["image_size"]).unsqueeze(0).to(system.device)
lidar_data = torch.randn(*CONFIG["lidar_size"]).unsqueeze(0).to(system.device)
imu_data = torch.randn(*CONFIG["imu_size"]).to(system.device)

# 前向传播获取决策
policy, value = system.forward(image, lidar_data, imu_data)

# 转换并限制控制信号
throttle, steer = clamp_control_values(
float(policy[0][0]),
float(policy[0][1])
)

# 应用控制信号到车辆
control = carla.VehicleControl(throttle=throttle, steer=steer)
env.vehicle.apply_control(control)

# 日志输出
if step % 10 == 0: # 每10步输出一次日志
logger.info(
f"仿真步骤 {step:3d} | 油门: {throttle:.3f} | "
f"转向: {steer:.3f} | 状态价值: {value[0][0]:.3f}"
)

# 模拟步骤间延迟
time.sleep(CONFIG["step_delay"])

except Exception as e:
logger.error(f"仿真步骤 {step} 出错: {e}")
continue

# 4. 清理资源
try:
# 停止车辆
env.vehicle.apply_control(carla.VehicleControl(throttle=0.0, brake=1.0))
time.sleep(1.0)

# 清理CARLA环境
if hasattr(env, 'cleanup'):
env.cleanup()

logger.info("仿真完成,已清理所有资源")
except Exception as e:
logger.error(f"清理资源时出错: {e}")


if __name__ == "__main__":
# 设置随机种子以保证可复现性
torch.manual_seed(42)

# 运行仿真
try:
run_simulation()
except KeyboardInterrupt:
logger.info("用户中断了仿真")
except Exception as e:
logger.error(f"仿真运行出错: {e}")
Loading