Skip to content
Merged
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
23 changes: 22 additions & 1 deletion src/minicarsim/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
3. **传感器模拟**:LiDAR传感器点云数据生成
4. **物体检测**:检测环境中的障碍物
5. **数据保存**:保存点云数据和检测结果
6. **温度监控**:模拟车内温度变化并可视化
7. **智能空调系统**:根据温度自动开启/关闭空调调节温度
8. **温度调节可视化**:生成多种温度调节相关图表

## 环境要求

Expand All @@ -29,6 +32,11 @@ minicarsim/
└── output/ # 输出数据目录
├── lidar/ # LiDAR点云数据
└── annotations/ # 物体检测标注
└── visualization/ # 可视化图表
├── temperature_trend.png # 温度变化趋势图
├── temperature_summary.png # 温度调节汇总图
├── ac_control_XXXX.png # 空调控制状态图
└── thermal_map_XXXX.png # 环境温度分布图
```

## 快速开始
Expand All @@ -53,13 +61,19 @@ python main.py
- `MojocoDataSim`: 主要的仿真类
- `generate_realistic_lidar_data()`: 生成真实的LiDAR点云数据
- `detect_objects()`: 检测环境中的物体
- `simulate_temperature_data()`: 模拟车内温度变化
- `check_and_control_ac()`: 检查并控制空调开关
- `run_simulation()`: 运行仿真主循环
- `visualize_ac_control()`: 生成空调控制状态图
- `generate_temperature_trend()`: 生成温度变化趋势图
- `generate_temperature_summary_chart()`: 生成温度调节汇总图

### 配置参数

在`main.py`中可以调整的主要参数:

- `LIDAR_PARAMS`: LiDAR传感器参数
- `TEMPERATURE_PARAMS`: 温度监控参数
- `SIMULATION_FRAMES`: 仿真总帧数

## 模型说明
Expand All @@ -74,10 +88,15 @@ python main.py

## 数据输出

仿真运行后会产生两类数据:
仿真运行后会产生三类数据:

1. **LiDAR点云数据**:保存为`.npy`格式的NumPy数组
2. **物体检测标注**:保存为`.json`格式的标注文件
3. **温度调节可视化图表**:
- 温度变化趋势图 (`temperature_trend.png`)
- 温度调节汇总图 (`temperature_summary.png`)
- 空调控制状态图 (`ac_control_XXXX.png`)
- 环境温度分布图 (`thermal_map_XXXX.png`)

## 扩展建议

Expand All @@ -86,6 +105,7 @@ python main.py
3. 添加不同的环境地图
4. 实现自主导航功能
5. 添加更多的车辆控制方式
6. 增强空调控制算法(PID控制等)

## 故障排除

Expand All @@ -94,6 +114,7 @@ python main.py
1. **找不到模型文件**:检查`XML_PATH`是否正确
2. **无法显示可视化窗口**:确保已正确安装MuJoCo
3. **没有检测到物体**:检查小车与物体之间的距离
4. **温度调节效果不明显**:检查空调功率系数和舒适温度范围设置

### 支持

Expand Down
266 changes: 263 additions & 3 deletions src/minicarsim/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
TEMPERATURE_PARAMS = {
"ambient_temp": 25.0, # 环境基础温度 (摄氏度)
"temp_variation": 5.0, # 温度变化幅度
"heat_sources": ["obstacle1", "obstacle2", "obstacle3", "obstacle4", "obstacle5"] # 热源物体
"heat_sources": ["obstacle1", "obstacle2", "obstacle3", "obstacle4", "obstacle5"], # 热源物体
"comfort_range": [18.0, 28.0], # 舒适温度范围 (摄氏度)
"ac_power": 1.0 # 空调功率系数
}


Expand All @@ -59,6 +61,12 @@ def __init__(self, xml_path, output_dir):
print("仿真将在3秒后开始...")
time.sleep(3)

# 初始化空调系统状态
self.ac_status = False # 空调开关状态
self.ac_target_temp = 23.0 # 空调目标温度
self.comfort_min_temp = TEMPERATURE_PARAMS["comfort_range"][0]
self.comfort_max_temp = TEMPERATURE_PARAMS["comfort_range"][1]

def get_world_pose(self, body_name):
"""
获取指定物体的世界位姿
Expand Down Expand Up @@ -439,6 +447,13 @@ def simulate_temperature_data(self):
noise = np.random.normal(0, 0.5) # 均值为0,标准差为0.5的高斯噪声
temperature += noise

# 如果空调开启,调整温度
if self.ac_status:
# 空调效果:逐渐向目标温度靠近
temp_diff = self.ac_target_temp - temperature
ac_effect = temp_diff * TEMPERATURE_PARAMS["ac_power"] * 0.05 # 空调效果系数
temperature += ac_effect

return temperature

def visualize_temperature_data(self, temperature, detected_objects):
Expand Down Expand Up @@ -590,6 +605,237 @@ def generate_thermal_map(self, temperature, detected_objects):

print(f"已生成温度分布热力图: thermal_map_{self.frame_count:04d}.png")

def check_and_control_ac(self, temperature):
"""
检查温度并控制空调开关
:param temperature: 当前温度
:return: 是否开启了空调
"""
# 检查温度是否超出舒适范围
if temperature < self.comfort_min_temp or temperature > self.comfort_max_temp:
# 如果温度不在舒适范围内,开启空调
if not self.ac_status:
self.ac_status = True
print(f"🌡️ 空调已开启,当前温度: {temperature:.1f}°C,目标温度: {self.ac_target_temp:.1f}°C")
else:
# 如果温度在舒适范围内,关闭空调
if self.ac_status:
self.ac_status = False
print(f"❄️ 空调已关闭,当前温度: {temperature:.1f}°C,处于舒适范围内")

return self.ac_status

def visualize_ac_control(self, temperature, ac_status):
"""
生成空调控制状态可视化图
:param temperature: 当前温度
:param ac_status: 空调状态
"""
fig, ax = plt.subplots(figsize=(10, 6))

# 绘制温度范围
comfort_min = self.comfort_min_temp
comfort_max = self.comfort_max_temp
ambient_temp = TEMPERATURE_PARAMS["ambient_temp"]

# 绘制舒适温度区域
ax.axvspan(comfort_min, comfort_max, alpha=0.3, color='green', label='舒适温度区间')

# 绘制环境温度线
ax.axvline(ambient_temp, color='blue', linestyle='--', linewidth=1, label=f'环境温度 ({ambient_temp}°C)')

# 绘制当前温度
color = 'red' if ac_status else 'orange'
status_label = '空调运行中' if ac_status else '空调关闭'
ax.scatter(temperature, 1, s=100, color=color, label=f'当前温度 {temperature:.1f}°C ({status_label})')

# 绘制目标温度(如果空调开启)
if ac_status:
ax.axvline(self.ac_target_temp, color='purple', linestyle='-.', linewidth=1,
label=f'目标温度 ({self.ac_target_temp}°C)')

ax.set_xlim(ambient_temp - 10, ambient_temp + 10)
ax.set_ylim(0, 2)
ax.set_xlabel('温度 (°C)')
ax.set_title(f'空调控制系统状态 - 帧 {self.frame_count:04d}')
ax.legend()
ax.grid(True, alpha=0.3)

# 移除y轴刻度
ax.set_yticks([])

plt.tight_layout()
plt.savefig(f"{self.output_dir}/visualization/ac_control_{self.frame_count:04d}.png",
dpi=300, bbox_inches='tight')
plt.close()

print(f"已生成空调控制状态图: ac_control_{self.frame_count:04d}.png")

def generate_temperature_trend(self):
"""
生成温度变化趋势图
"""
# 读取之前保存的温度数据
temp_files = []
for file in os.listdir(f"{self.output_dir}/annotations"):
if file.startswith("temp_frame_") and file.endswith(".json"):
temp_files.append(file)

if not temp_files:
return

# 按帧排序
temp_files.sort()

frames = []
temperatures = []
ac_statuses = []

# 读取温度数据
for file in temp_files:
with open(f"{self.output_dir}/annotations/{file}", "r") as f:
temp_data = json.load(f)
frames.append(temp_data["frame"])
temperatures.append(temp_data["temperature"])
ac_statuses.append(temp_data["ac_status"])

# 绘制温度变化趋势图
fig, ax = plt.subplots(figsize=(12, 6))

# 绘制温度曲线
ax.plot(frames, temperatures, 'o-', color='red', linewidth=2, markersize=4, label='实测温度')

# 绘制舒适温度区间
ax.axhspan(self.comfort_min_temp, self.comfort_max_temp, alpha=0.2, color='green',
label='舒适温度区间')

# 绘制环境温度线
ax.axhline(TEMPERATURE_PARAMS["ambient_temp"], color='blue', linestyle='--',
label=f'环境温度 ({TEMPERATURE_PARAMS["ambient_temp"]}°C)')

# 标记空调开启的时间点
ac_on_frames = [frames[i] for i in range(len(frames)) if ac_statuses[i]]
ac_on_temps = [temperatures[i] for i in range(len(temperatures)) if ac_statuses[i]]
if ac_on_frames:
ax.scatter(ac_on_frames, ac_on_temps, color='purple', s=50, marker='^',
label='空调运行中', zorder=5)

ax.set_xlabel('帧序号')
ax.set_ylabel('温度 (°C)')
ax.set_title('温度变化趋势与空调控制状态')
ax.legend()
ax.grid(True, alpha=0.3)

plt.tight_layout()
plt.savefig(f"{self.output_dir}/visualization/temperature_trend.png", dpi=300, bbox_inches='tight')
plt.close()

print("已生成温度变化趋势图: temperature_trend.png")

def generate_temperature_summary_chart(self):
"""
生成温度调节汇总图,综合显示温度变化、空调状态和调节效果
"""
# 读取之前保存的温度数据
temp_files = []
for file in os.listdir(f"{self.output_dir}/annotations"):
if file.startswith("temp_frame_") and file.endswith(".json"):
temp_files.append(file)

if not temp_files:
return

# 按帧排序
temp_files.sort()

frames = []
temperatures = []
ac_statuses = []
target_temps = []

# 读取温度数据
for file in temp_files:
with open(f"{self.output_dir}/annotations/{file}", "r") as f:
temp_data = json.load(f)
frames.append(temp_data["frame"])
temperatures.append(temp_data["temperature"])
ac_statuses.append(temp_data["ac_status"])
target_temps.append(temp_data.get("ac_target_temp", None))

# 创建汇总图
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(15, 10))

# 第一个子图:温度变化和空调状态
ax1.plot(frames, temperatures, 'o-', color='red', linewidth=2, markersize=4, label='实测温度')
ax1.axhspan(self.comfort_min_temp, self.comfort_max_temp, alpha=0.2, color='green',
label='舒适温度区间')
ax1.axhline(TEMPERATURE_PARAMS["ambient_temp"], color='blue', linestyle='--',
label=f'环境温度 ({TEMPERATURE_PARAMS["ambient_temp"]}°C)')

# 标记空调开启的时间点
ac_on_frames = [frames[i] for i in range(len(frames)) if ac_statuses[i]]
ac_on_temps = [temperatures[i] for i in range(len(temperatures)) if ac_statuses[i]]
if ac_on_frames:
ax1.scatter(ac_on_frames, ac_on_temps, color='purple', s=50, marker='^',
label='空调运行中', zorder=5)

ax1.set_ylabel('温度 (°C)')
ax1.set_title('温度变化与空调控制状态')
ax1.legend()
ax1.grid(True, alpha=0.3)

# 第二个子图:空调工作时的目标温度和调节效果
# 只显示空调开启时的数据点
ac_frames = []
ac_temps = []
ac_targets = []
temp_differences = []

for i in range(len(frames)):
if ac_statuses[i] and target_temps[i] is not None:
ac_frames.append(frames[i])
ac_temps.append(temperatures[i])
ac_targets.append(target_temps[i])
temp_differences.append(abs(temperatures[i] - target_temps[i]))

if ac_frames:
ax2.plot(ac_frames, ac_temps, 'o-', color='red', linewidth=2, markersize=4, label='实测温度')
ax2.plot(ac_frames, ac_targets, 's-', color='purple', linewidth=2, markersize=4, label='目标温度')

# 添加温度差值的柱状图
ax3 = ax2.twinx()
bars = ax3.bar(ac_frames, temp_differences, alpha=0.3, color='orange', width=1.0, label='温度差值')
ax3.set_ylabel('温度差值 (°C)', color='orange')
ax3.tick_params(axis='y', labelcolor='orange')

# 添加数值标签
for bar, diff in zip(bars, temp_differences):
height = bar.get_height()
ax3.annotate(f'{diff:.1f}',
xy=(bar.get_x() + bar.get_width()/2, height),
xytext=(0, 3), # 3 points vertical offset
textcoords="offset points",
ha='center', va='bottom', fontsize=8, color='orange')

ax2.set_xlabel('帧序号')
ax2.set_ylabel('温度 (°C)')
ax2.set_title('空调调节效果分析')
ax2.grid(True, alpha=0.3)

# 合并图例
if ac_frames:
lines2, labels2 = ax2.get_legend_handles_labels()
lines3, labels3 = ax3.get_legend_handles_labels()
ax2.legend(lines2 + lines3, labels2 + labels3, loc='upper left')
else:
ax2.legend()

plt.tight_layout()
plt.savefig(f"{self.output_dir}/visualization/temperature_summary.png", dpi=300, bbox_inches='tight')
plt.close()

print("已生成温度调节汇总图: temperature_summary.png")

def run_simulation(self):
"""运行MuJoCo仿真并生成数据"""
print("开始仿真...")
Expand All @@ -616,6 +862,9 @@ def run_simulation(self):

# 新增:模拟温度数据
temperature = self.simulate_temperature_data()

# 检查并控制空调
ac_status = self.check_and_control_ac(temperature)

# 基于传感器数据计算控制指令
left_speed, right_speed, steering_angle = self.calculate_avoidance_control(
Expand Down Expand Up @@ -646,12 +895,17 @@ def run_simulation(self):

# 新增:生成温度分布热力图
self.generate_thermal_map(temperature, annotations["objects"])


# 新增:生成空调控制状态图
self.visualize_ac_control(temperature, ac_status)

# 在保存数据时也保存温度信息
temp_data = {
"frame": self.frame_count,
"temperature": temperature,
"unit": "celsius"
"unit": "celsius",
"ac_status": ac_status,
"ac_target_temp": self.ac_target_temp if ac_status else None
}
with open(f"{self.output_dir}/annotations/temp_frame_{self.frame_count:04d}.json", "w") as f:
json.dump(temp_data, f, indent=4)
Expand Down Expand Up @@ -685,6 +939,12 @@ def run_simulation(self):
# 控制仿真速度以便观察
time.sleep(0.01)

# 生成最终的温度趋势图
self.generate_temperature_trend()

# 生成温度调节汇总图
self.generate_temperature_summary_chart()

print(f"仿真完成!数据已保存到:{self.output_dir}")


Expand Down
Loading