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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ WindowsNoEditor/
├── CarlaUE4.exe
└── hutb.bat
```
3. 检查 `llm/.env` 文件中的Github和Deeopseek API密钥已配置
3. 检查 `llm/.env` 文件中的Github和Deepseek API密钥已配置
4. 双击 `hutb.bat` 启动模拟器


Expand Down
70 changes: 67 additions & 3 deletions llm/main_ai.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,29 @@ async def get_traffic_lights(self):
lights = [light for light in self.world.get_actors() if 'traffic_light' in light.type_id]
return lights[:5] # 只返回前5个

async def spawn_pedestrian(self, pedestrian_type='walker'):
"""生成行人"""
try:
# 查找行人蓝图
blueprint_library = self.world.get_blueprint_library()
walker_blueprints = blueprint_library.filter('walker.pedestrian.*')
if not walker_blueprints:
app_logger.error("❌ 未找到行人蓝图")
return None

blueprint = walker_blueprints[0] # 使用第一个找到的行人蓝图
spawn_point = self.world.get_map().get_spawn_points()[0]
# 设置随机位置偏移,避免与车辆重叠
spawn_point.location.x += 5.0

pedestrian = self.world.spawn_actor(blueprint, spawn_point)
self.actors.append(pedestrian)
app_logger.info(f"🚶 生成行人: {pedestrian_type}")
return pedestrian
except Exception as e:
app_logger.error(f"❌ 生成行人失败: {str(e)}")
return None

async def cleanup(self):
"""清理环境"""
for actor in self.actors:
Expand Down Expand Up @@ -425,6 +448,14 @@ async def cleanup_scene_impl(**kwargs) -> str:
return "✅ 已清理所有车辆和物体"


async def spawn_pedestrian_impl(query: str, **kwargs) -> str:
"""(实际功能:生成行人)"""
pedestrian = await carla_client.spawn_pedestrian(query)
if pedestrian:
return f"✅ 已生成行人: {query} (ID: {pedestrian.id})"
return "❌ 行人生成失败"


# ============ FastMCP 工具装饰器版本 ============

@mcp.tool()
Expand Down Expand Up @@ -458,6 +489,12 @@ async def cleanup_scene(language: Optional[str] = None, period: str = "daily") -
return await cleanup_scene_impl()


@mcp.tool()
async def spawn_pedestrian(query: str, user_type: Optional[str] = None) -> str:
"""(实际功能:生成行人)"""
return await spawn_pedestrian_impl(query)


@mcp.tool()
async def search_github_repositories(query: str, language: Optional[str] = None,
sort: str = "stars", limit: int = 8) -> str:
Expand Down Expand Up @@ -560,6 +597,20 @@ def __init__(self):
}
}
},
{
"type": "function",
"function": {
"name": "spawn_pedestrian",
"description": "生成行人",
"parameters": {
"type": "object",
"properties": {
"query": {"type": "string", "description": "行人类型,默认为walker"}
},
"required": ["query"]
}
}
},
{
"type": "function",
"function": {
Expand Down Expand Up @@ -766,6 +817,16 @@ async def execute_fastmcp_tool_call(self, tool_call):
"success": True,
"data": result
}

elif function_name == "spawn_pedestrian":
result = await spawn_pedestrian_impl(
query=arguments["query"],
user_type=arguments.get("user_type")
)
return {
"success": True,
"data": result
}
elif function_name == "search_github_repositories":
result = await search_github_repositories_impl(
query=arguments["query"],
Expand Down Expand Up @@ -837,9 +898,10 @@ async def chat(self, user_message):
CARLA仿真功能:
5. connect_carla - 连接CARLA服务器(默认localhost:2000)
6. spawn_vehicle - 生成车辆(model3/a2/mustang)
7. set_weather - 设置天气(clear/rain/fog)
8. get_traffic_lights - 查看交通灯状态
9. cleanup_scene - 清理仿真场景
7. spawn_pedestrian - 生成行人
8. set_weather - 设置天气(clear/rain/fog)
9. get_traffic_lights - 查看交通灯状态
10. cleanup_scene - 清理仿真场景

处理用户查询的策略:
GitHub相关:
Expand All @@ -850,6 +912,7 @@ async def chat(self, user_message):

CARLA相关:
- 如果用户提到"车辆"、"生成"、"创建汽车"等,使用spawn_vehicle
- 如果用户提到"行人"、"生成行人"、"创建行人"等,使用spawn_pedestrian
- 如果用户提到"天气"、"下雨"、"晴天"、"雾天"等,使用set_weather
- 如果用户提到"交通灯"、"信号灯"、"红绿灯"等,使用get_traffic_lights
- 如果用户提到"清理"、"重置"、"清除场景"等,使用cleanup_scene
Expand All @@ -867,6 +930,7 @@ async def chat(self, user_message):
用户指令示例:
- "连接carla服务器" -> connect_carla(host="localhost", port=2000)
- "生成一辆model3" -> spawn_vehicle(vehicle_type="model3")
- "生成行人" -> spawn_pedestrian(pedestrian_type="walker")
- "设置雨天" -> set_weather(weather_type="rain")
- "查看交通灯" -> get_traffic_lights()
- "清理场景" -> cleanup_scene()
Expand Down