jl-ecs/examples/rtss-cg/main.go

231 lines
9.3 KiB
Go
Raw Normal View History

2023-08-08 13:52:19 +08:00
package main
import (
"time"
"github.com/yohamta/donburi/filter"
"joylink.club/ecs"
"joylink.club/rtss/iwayside"
"joylink.club/rtss/mwayside"
"joylink.club/rtss/simulation"
"joylink.club/rtss/state"
"joylink.club/rtss/system"
)
2023-08-09 13:21:41 +08:00
//
//该测试例子线路结果图
//link为长轨道为两个相邻道岔间的轨道
//ax为计轴点
//dc为道岔岔尖为A,B为岔后定位轨B为侧向轨道
//轨道、区段左侧为A,右侧为B
//
/*
//K0 K3000 * 100 * 10 K6070 * 100 * 10
// link1 link2
//ax1==================ax3=====dc1======ax5==============================ax7
// =
// =
// ax9 link5
// =
// =
//ax2=============================ax4======dc2======ax6===================ax8
// link3 link4
//K0 K3070 * 100 * 10 K6070 * 100 * 10
*/
//
//
2023-08-08 13:52:19 +08:00
func main() {
//
2023-08-10 18:11:26 +08:00
simWorld := ecs.NewWorld(300)
2023-08-08 13:52:19 +08:00
//
initMemoryModel(simWorld)
//道岔
initSwitchState(simWorld)
//轨道
initLinkState(simWorld)
2023-08-10 18:11:26 +08:00
//物理区段
initPhsicalSectionState(simWorld)
2023-08-08 13:52:19 +08:00
//
simWorld.AddSystem(system.NewSwitchSystem())
simWorld.AddSystem(system.NewTrainSystem())
2023-08-10 18:11:26 +08:00
//simWorld.AddSystem(system.NewCiSystem())
simWorld.AddSystem(system.NewSectionSystem())
2023-08-08 13:52:19 +08:00
//
simWorld.StartUp()
//
operateSwitch(simWorld, "dc1")
operateSwitch(simWorld, "dc2")
2023-08-10 18:11:26 +08:00
time.Sleep(10 * time.Second)
//添加列车
initTrainState(simWorld, "train1", iwayside.LinkLocation{LinkId: "link4", LinkOffset: 3000 * 100 * 10}, false)
initTrainState(simWorld, "train2", iwayside.LinkLocation{LinkId: "link4", LinkOffset: 1000 * 100 * 10}, false)
2023-08-08 13:52:19 +08:00
time.Sleep(200 * time.Second)
}
// 根据道岔id来搬动道岔
func operateSwitch(world ecs.World, dcId string) {
query := ecs.NewQuery(filter.Contains(simulation.ComSwitchState, simulation.ComSwitchOperating))
query.Each(world, func(e *ecs.Entry) {
dcState := simulation.ComSwitchState.Get(e)
if dcState.Id == dcId {
simulation.ComSwitchOperating.SetValue(e, state.SwitchOperating{EnableStart: true, StartTime: time.Now()})
}
})
}
// 初始化memory
func initMemoryModel(world ecs.World) {
memoryEntry := world.Create(simulation.ComSimMemory)
2023-08-08 15:47:43 +08:00
//simulation.ComSimMemory.SetValue(memoryEntry, *simulation.NewSimMemory())
simMemory := simulation.NewSimMemory()
simulation.ComSimMemory.Set(memoryEntry, simMemory)
2023-08-09 13:21:41 +08:00
createSkeletonModelsRelation(simMemory)
createKilometerSignModelsRelation(simMemory)
createPhsicalSectionModelsRelation(simMemory)
2023-08-08 13:52:19 +08:00
}
// 初始化道岔状态
func initSwitchState(world ecs.World) {
simMemory := simulation.GetMemoryModel(world)
for _, switchModel := range simMemory.Switchs {
dc1 := &state.SwitchState{Id: switchModel.GetId(), Normal: true, Reverse: false}
dc1Entry := world.Create(simulation.ComSwitchState, simulation.ComSwitchOperating)
2023-08-08 15:47:43 +08:00
simulation.ComSwitchState.Set(dc1Entry, dc1)
2023-08-08 13:52:19 +08:00
}
}
// 初始化轨道状态
func initLinkState(world ecs.World) {
simMemory := simulation.GetMemoryModel(world)
for _, linkModel := range simMemory.Links {
linkState := &state.LinkState{Id: linkModel.GetId(), Occupied: false}
linkEntry := world.Create(simulation.ComLinkState)
2023-08-08 15:47:43 +08:00
simulation.ComLinkState.Set(linkEntry, linkState)
2023-08-08 13:52:19 +08:00
}
}
2023-08-10 18:11:26 +08:00
// 初始化物理区段状态
func initPhsicalSectionState(world ecs.World) {
simMemory := simulation.GetMemoryModel(world)
for _, phSecModel := range simMemory.PhysicalSections {
sectionState := &state.PhysicalSectionState{Id: phSecModel.GetId(), Occupied: false}
sectionEntry := world.Create(simulation.ComPhsicalSectionState)
simulation.ComPhsicalSectionState.Set(sectionEntry, sectionState)
}
}
2023-08-08 13:52:19 +08:00
// 添加列车并初始化
func initTrainState(world ecs.World, trainId string, linkLocation iwayside.LinkLocation, up bool) {
train := &state.TrainState{Id: trainId, LinkId: linkLocation.LinkId, LinkOffset: linkLocation.LinkOffset, Up: up}
trainEntry := world.Create(simulation.ComTrainState)
2023-08-08 15:47:43 +08:00
simulation.ComTrainState.Set(trainEntry, train)
2023-08-08 13:52:19 +08:00
}
2023-08-09 13:21:41 +08:00
// 固定关系构建骨架模型关系即长link与道岔的连接关系
func createSkeletonModelsRelation(memory *simulation.SimMemory) {
2023-08-08 13:52:19 +08:00
//
link1Model := &mwayside.LinkModel{DeviceModel: mwayside.DeviceModel{Id: "link1", Type: iwayside.Link}, Len: 3000 * 100 * 10}
link2Model := &mwayside.LinkModel{DeviceModel: mwayside.DeviceModel{Id: "link2", Type: iwayside.Link}, Len: 3070 * 100 * 10}
link3Model := &mwayside.LinkModel{DeviceModel: mwayside.DeviceModel{Id: "link3", Type: iwayside.Link}, Len: 3070 * 100 * 10}
link4Model := &mwayside.LinkModel{DeviceModel: mwayside.DeviceModel{Id: "link4", Type: iwayside.Link}, Len: 3000 * 100 * 10}
link5Model := &mwayside.LinkModel{DeviceModel: mwayside.DeviceModel{Id: "link5", Type: iwayside.Link}, Len: 120 * 100 * 10}
memory.Links[link1Model.Id] = link1Model
memory.Links[link2Model.Id] = link2Model
memory.Links[link3Model.Id] = link3Model
memory.Links[link4Model.Id] = link4Model
memory.Links[link5Model.Id] = link5Model
//
2023-08-09 13:21:41 +08:00
switch1Model := &mwayside.SwitchModel{DeviceModel: mwayside.DeviceModel{Id: "dc1", Type: iwayside.Switch}, KilometerSign: 3000 * 100 * 10}
switch2Model := &mwayside.SwitchModel{DeviceModel: mwayside.DeviceModel{Id: "dc2", Type: iwayside.Switch}, KilometerSign: 3070 * 100 * 10}
2023-08-08 13:52:19 +08:00
memory.Switchs[switch1Model.Id] = switch1Model
memory.Switchs[switch2Model.Id] = switch2Model
//
link1Model.PortB = &mwayside.SwitchRef{Port: iwayside.A, Switch: switch1Model}
link2Model.PortA = &mwayside.SwitchRef{Port: iwayside.B, Switch: switch1Model}
link3Model.PortB = &mwayside.SwitchRef{Port: iwayside.B, Switch: switch2Model}
link4Model.PortA = &mwayside.SwitchRef{Port: iwayside.A, Switch: switch2Model}
link5Model.PortA = &mwayside.SwitchRef{Port: iwayside.C, Switch: switch1Model}
link5Model.PortB = &mwayside.SwitchRef{Port: iwayside.C, Switch: switch2Model}
//
switch1Model.PortA = &mwayside.LinkRef{Port: iwayside.B, Link: link1Model}
switch1Model.PortB = &mwayside.LinkRef{Port: iwayside.A, Link: link2Model}
switch1Model.PortC = &mwayside.LinkRef{Port: iwayside.A, Link: link5Model}
//
switch2Model.PortA = &mwayside.LinkRef{Port: iwayside.A, Link: link4Model}
switch2Model.PortB = &mwayside.LinkRef{Port: iwayside.B, Link: link3Model}
switch2Model.PortC = &mwayside.LinkRef{Port: iwayside.B, Link: link5Model}
}
2023-08-09 13:21:41 +08:00
//
/*
//K0 K3000 * 100 * 10 K6070 * 100 * 10
// link1 link2
//ax1==================ax3=====dc1======ax5==============================ax7
// =
// =
// ax9 link5
// =
// =
//ax2=============================ax4======dc2======ax6===================ax8
// link3 link4
//K0 K3070 * 100 * 10 K6070 * 100 * 10
*/
//
//
//固定关系:构建物理区段关系
func createPhsicalSectionModelsRelation(memory *simulation.SimMemory) {
ax1 := memory.AxlePoints["ax1"]
ax2 := memory.AxlePoints["ax2"]
ax3 := memory.AxlePoints["ax3"]
ax4 := memory.AxlePoints["ax4"]
ax5 := memory.AxlePoints["ax5"]
ax6 := memory.AxlePoints["ax6"]
ax7 := memory.AxlePoints["ax7"]
ax8 := memory.AxlePoints["ax8"]
ax9 := memory.AxlePoints["ax9"]
//
ph13 := mwayside.NewLinePhysicalSectionModel("ph13", ax1, ax3)
ph359 := mwayside.NewAreaPhysicalSectionModel("ph359", ax3, ax5, ax9)
ph57 := mwayside.NewLinePhysicalSectionModel("ph57", ax5, ax7)
ph24 := mwayside.NewLinePhysicalSectionModel("ph24", ax2, ax4)
ph469 := mwayside.NewAreaPhysicalSectionModel("ph469", ax4, ax6, ax9)
ph68 := mwayside.NewLinePhysicalSectionModel("ph68", ax6, ax8)
//
memory.PhysicalSections[ph13.Id] = ph13
memory.PhysicalSections[ph359.Id] = ph359
memory.PhysicalSections[ph57.Id] = ph57
memory.PhysicalSections[ph24.Id] = ph24
memory.PhysicalSections[ph469.Id] = ph469
memory.PhysicalSections[ph68.Id] = ph68
}
// 固定关系:构建坐标设备与骨架模型的关系
func createKilometerSignModelsRelation(memory *simulation.SimMemory) {
link1 := memory.Links["link1"]
link2 := memory.Links["link2"]
link3 := memory.Links["link3"]
link4 := memory.Links["link4"]
link5 := memory.Links["link5"]
//
ax1 := mwayside.NewAxlePointModel("ax1", link1, 0)
ax2 := mwayside.NewAxlePointModel("ax2", link3, 0)
ax3 := mwayside.NewAxlePointModel("ax3", link1, 3000*100*10-20*100*10)
ax4 := mwayside.NewAxlePointModel("ax4", link3, 3070*100*10-20*100*10)
ax5 := mwayside.NewAxlePointModel("ax5", link2, 3000*100*10+20*100*10)
ax6 := mwayside.NewAxlePointModel("ax6", link4, 3070*100*10+20*100*10)
ax7 := mwayside.NewAxlePointModel("ax7", link2, 6070*100*10)
ax8 := mwayside.NewAxlePointModel("ax8", link4, 6070*100*10)
ax9 := mwayside.NewAxlePointModel("ax9", link5, 3000*100*10+35*100*10)
//
memory.AxlePoints[ax1.Id] = ax1
memory.AxlePoints[ax2.Id] = ax2
memory.AxlePoints[ax3.Id] = ax3
memory.AxlePoints[ax4.Id] = ax4
memory.AxlePoints[ax5.Id] = ax5
memory.AxlePoints[ax6.Id] = ax6
memory.AxlePoints[ax7.Id] = ax7
memory.AxlePoints[ax8.Id] = ax8
memory.AxlePoints[ax9.Id] = ax9
//
}