309 lines
8.2 KiB
Markdown
309 lines
8.2 KiB
Markdown
# 导弹仿真示例
|
||
|
||
本目录包含了使用ThreatSource仿真系统进行导弹仿真的示例代码。这些示例展示了如何配置和运行不同类型的导弹仿真。
|
||
|
||
## 关于本库
|
||
|
||
ThreatSource 是一个基于 .NET 8.0 的类库,提供了完整的导弹仿真功能,包括:
|
||
|
||
- 多种制导系统(激光、红外、毫米波等)
|
||
- 运动学仿真和物理建模
|
||
- 事件驱动的仿真架构
|
||
- 智能数据管理系统
|
||
- 外部系统集成支持
|
||
|
||
## 系统要求
|
||
|
||
### C#/.NET 用户
|
||
|
||
- .NET 8.0 或更高版本
|
||
- 通过 NuGet 包管理器安装或直接引用 ThreatSource.dll
|
||
- Visual Studio 2019+ 或 Visual Studio Code
|
||
|
||
### C++用户
|
||
|
||
本库是一个 .NET 类库,C++用户需要通过 C++/CLI 包装层来使用:
|
||
|
||
- Windows 操作系统
|
||
- Visual Studio 2019 或更高版本(支持C++/CLI)
|
||
- .NET 8.0 运行时
|
||
- 创建 C++/CLI 项目并引用 ThreatSource.dll
|
||
|
||
## 示例文件
|
||
|
||
### C#示例 ([IRMissileSimulation.cs](IRMissileSimulation.cs))
|
||
|
||
红外成像制导导弹仿真示例,展示了:
|
||
|
||
- 如何使用SimulationManager管理仿真
|
||
- 如何从配置文件创建导弹实体
|
||
- 如何设置目标和仿真环境
|
||
- 如何订阅和处理仿真事件
|
||
- 如何运行仿真循环并获取结果
|
||
|
||
#### 核心代码片段
|
||
|
||
```csharp
|
||
using ThreatSource.Simulation;
|
||
using ThreatSource.Data;
|
||
using ThreatSource.Missile;
|
||
|
||
public class IRMissileSimulationExample
|
||
{
|
||
private ISimulationManager _simulationManager;
|
||
private ThreatSourceDataManager _dataManager;
|
||
|
||
public async Task RunSimulationExample()
|
||
{
|
||
// 1. 初始化仿真管理器
|
||
_simulationManager = new SimulationManager();
|
||
_dataManager = new ThreatSourceDataManager();
|
||
_simulationManager.StartSimulation(0.02); // 20ms时间步长
|
||
|
||
// 2. 创建目标实体
|
||
var target = new BaseEquipment("target_001")
|
||
{
|
||
KState = new KinematicState
|
||
{
|
||
Position = new Vector3(5000, 0, 1000), // 5km距离
|
||
Velocity = new Vector3(-50, 0, 0), // 50m/s移动
|
||
Orientation = new Orientation(0, 0, 0)
|
||
}
|
||
};
|
||
|
||
// 3. 从配置创建导弹
|
||
var missileData = _dataManager.GetMissile("IR_Missile_Example");
|
||
var missile = new InfraredImagingTerminalGuidanceMissile("missile_001", missileData)
|
||
{
|
||
KState = new KinematicState
|
||
{
|
||
Position = new Vector3(0, 0, 100),
|
||
Velocity = new Vector3(200, 0, 0),
|
||
Orientation = new Orientation(0, 0, 0)
|
||
}
|
||
};
|
||
|
||
// 4. 注册实体和事件
|
||
_simulationManager.RegisterEntity(target);
|
||
_simulationManager.RegisterEntity(missile);
|
||
_simulationManager.Subscribe<MissileExplodeEvent>(OnMissileExplode);
|
||
|
||
// 5. 运行仿真循环
|
||
while (simulationTime < maxTime && missile.IsActive)
|
||
{
|
||
_simulationManager.UpdateSimulation();
|
||
// 处理仿真状态...
|
||
}
|
||
}
|
||
}
|
||
```
|
||
|
||
### C++示例 ([IRMissileSimulation.cpp](IRMissileSimulation.cpp))
|
||
|
||
这是一个使用C++/CLI的示例代码,展示了如何在C++项目中使用本库:
|
||
|
||
- 如何创建C++/CLI包装层
|
||
- 如何配置导弹实体和目标
|
||
- 如何设置仿真参数
|
||
- 如何运行仿真并获取结果
|
||
|
||
#### 核心代码片段
|
||
|
||
```cpp
|
||
using namespace ThreatSource::Simulation;
|
||
using namespace ThreatSource::Data;
|
||
using namespace ThreatSource::Missile;
|
||
|
||
public ref class IRMissileSimulationWrapper
|
||
{
|
||
private:
|
||
ISimulationManager^ simulationManager;
|
||
ThreatSourceDataManager^ dataManager;
|
||
|
||
public:
|
||
void RunSimulationExample()
|
||
{
|
||
// 1. 初始化仿真系统
|
||
simulationManager = gcnew SimulationManager();
|
||
dataManager = gcnew ThreatSourceDataManager();
|
||
simulationManager->StartSimulation(0.02);
|
||
|
||
// 2. 创建目标
|
||
auto target = gcnew BaseEquipment("target_001");
|
||
target->KState = gcnew KinematicState();
|
||
target->KState->Position = Vector3(5000, 0, 1000);
|
||
|
||
// 3. 创建导弹
|
||
auto missileData = dataManager->GetMissile("IR_Missile_Example");
|
||
auto missile = gcnew InfraredImagingTerminalGuidanceMissile("missile_001", missileData);
|
||
|
||
// 4. 注册实体
|
||
simulationManager->RegisterEntity(target);
|
||
simulationManager->RegisterEntity(missile);
|
||
|
||
// 5. 运行仿真
|
||
while (simulationTime < maxTime && missile->IsActive)
|
||
{
|
||
simulationManager->UpdateSimulation();
|
||
// 处理仿真状态...
|
||
}
|
||
}
|
||
};
|
||
```
|
||
|
||
## 使用说明
|
||
|
||
### 基本仿真流程
|
||
|
||
1. **初始化仿真管理器**
|
||
```csharp
|
||
var simulationManager = new SimulationManager();
|
||
var dataManager = new ThreatSourceDataManager();
|
||
simulationManager.StartSimulation(timeStep);
|
||
```
|
||
|
||
2. **创建仿真实体**
|
||
```csharp
|
||
// 从配置创建导弹
|
||
var missileData = dataManager.GetMissile("missile_type");
|
||
var missile = new InfraredImagingTerminalGuidanceMissile("id", missileData);
|
||
|
||
// 创建目标
|
||
var target = new BaseEquipment("target_id");
|
||
```
|
||
|
||
3. **配置实体状态**
|
||
```csharp
|
||
missile.KState = new KinematicState
|
||
{
|
||
Position = new Vector3(x, y, z),
|
||
Velocity = new Vector3(vx, vy, vz),
|
||
Orientation = new Orientation(yaw, pitch, roll)
|
||
};
|
||
```
|
||
|
||
4. **注册实体和事件**
|
||
```csharp
|
||
simulationManager.RegisterEntity(missile);
|
||
simulationManager.RegisterEntity(target);
|
||
simulationManager.Subscribe<MissileExplodeEvent>(OnMissileExplode);
|
||
```
|
||
|
||
5. **运行仿真循环**
|
||
```csharp
|
||
while (condition)
|
||
{
|
||
simulationManager.UpdateSimulation();
|
||
// 处理仿真状态和结果
|
||
}
|
||
```
|
||
|
||
### 配置文件要求
|
||
|
||
示例代码需要相应的TOML配置文件:
|
||
|
||
```toml
|
||
# data/missiles/IR_Missile_Example.toml
|
||
Type = "missile"
|
||
|
||
[Name]
|
||
zh = "红外成像制导导弹示例"
|
||
en = "IR Imaging Guided Missile Example"
|
||
|
||
[Properties]
|
||
MaxSpeed = 800.0
|
||
MaxAcceleration = 50.0
|
||
MaxFlightTime = 120.0
|
||
MaxFlightDistance = 8000.0
|
||
|
||
[InfraredImagingGuidanceConfig]
|
||
MaxDetectionRange = 5000.0
|
||
FieldOfView = 30.0
|
||
TargetRecognitionProbability = 0.9
|
||
```
|
||
|
||
### C++/CLI项目配置
|
||
|
||
1. **创建C++/CLI项目**
|
||
- 在Visual Studio中选择"CLR空项目"
|
||
- 设置目标框架为.NET 8.0
|
||
|
||
2. **添加引用**
|
||
```xml
|
||
<Reference Include="ThreatSource">
|
||
<HintPath>path\to\ThreatSource.dll</HintPath>
|
||
</Reference>
|
||
```
|
||
|
||
3. **编译设置**
|
||
- 启用公共语言运行时支持 (/clr)
|
||
- 设置正确的.NET目标框架版本
|
||
|
||
## 注意事项
|
||
|
||
### 通用注意事项
|
||
|
||
1. **单位系统**:确保所有参数使用正确的单位(米、秒、弧度等)
|
||
2. **时间步长**:合理设置仿真时间步长,平衡精度和性能
|
||
3. **资源管理**:正确处理仿真资源的创建和释放
|
||
4. **异常处理**:添加适当的异常处理机制
|
||
|
||
### C#特定注意事项
|
||
|
||
1. 使用async/await处理异步操作
|
||
2. 正确订阅和取消订阅事件
|
||
3. 注意内存管理和垃圾回收
|
||
|
||
### C++/CLI特定注意事项
|
||
|
||
1. **平台限制**:仅支持Windows平台
|
||
2. **内存管理**:注意托管和非托管资源的正确释放
|
||
3. **类型转换**:正确处理托管和原生类型之间的转换
|
||
4. **异常处理**:使用托管异常处理机制
|
||
|
||
## 扩展示例
|
||
|
||
### 添加自定义事件处理
|
||
|
||
```csharp
|
||
// 订阅多种事件
|
||
simulationManager.Subscribe<FlightPhaseChangeEvent>(OnFlightPhaseChange);
|
||
simulationManager.Subscribe<TargetHitEvent>(OnTargetHit);
|
||
simulationManager.Subscribe<GuidanceActivationEvent>(OnGuidanceActivation);
|
||
|
||
private void OnFlightPhaseChange(FlightPhaseChangeEvent evt)
|
||
{
|
||
Console.WriteLine($"导弹 {evt.SenderId} 进入 {evt.NewPhase} 阶段");
|
||
}
|
||
```
|
||
|
||
### 多导弹仿真
|
||
|
||
```csharp
|
||
// 创建多个导弹
|
||
for (int i = 0; i < missileCount; i++)
|
||
{
|
||
var missile = new InfraredImagingTerminalGuidanceMissile($"missile_{i:D3}", missileData);
|
||
// 设置不同的初始位置和参数
|
||
simulationManager.RegisterEntity(missile);
|
||
}
|
||
```
|
||
|
||
### 实时状态监控
|
||
|
||
```csharp
|
||
// 在仿真循环中监控状态
|
||
while (simulationManager.IsRunning)
|
||
{
|
||
simulationManager.UpdateSimulation();
|
||
|
||
// 获取所有导弹状态
|
||
var missiles = simulationManager.GetEntitiesByType<BaseMissile>();
|
||
foreach (var missile in missiles)
|
||
{
|
||
var status = missile.GetStatusInfo();
|
||
// 处理状态信息
|
||
}
|
||
}
|
||
```
|