jl-ecs/examples/rtss-cg/main.go
2023-08-11 13:46:57 +08:00

230 lines
9.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package main
import (
"time"
"github.com/yohamta/donburi/filter"
"joylink.club/ecs"
"joylink.club/ecs/examples/rtss-cg/iwayside"
"joylink.club/ecs/examples/rtss-cg/mwayside"
"joylink.club/ecs/examples/rtss-cg/simulation"
"joylink.club/ecs/examples/rtss-cg/state"
"joylink.club/ecs/examples/rtss-cg/system"
)
//
//该测试例子线路结果图
//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
*/
//
//
func main() {
// 构建固定模型
initMemoryModel()
// 创建一个仿真实例
simWorld := ecs.NewWorld(300)
//道岔
initSwitchState(simWorld)
//轨道
initLinkState(simWorld)
//物理区段
initPhsicalSectionState(simWorld)
//
simWorld.AddSystem(system.NewSwitchSystem())
simWorld.AddSystem(system.NewTrainSystem())
simWorld.AddSystem(system.NewSectionSystem())
//
simWorld.StartUp()
//
operateSwitch(simWorld, "dc1")
operateSwitch(simWorld, "dc2")
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)
time.Sleep(200 * time.Second)
}
// 根据道岔id来搬动道岔
func operateSwitch(world ecs.World, dcId string) {
query := ecs.NewQuery(filter.Contains(simulation.ComId, simulation.ComSwitchState, simulation.ComSwitchOperating))
query.Each(world, func(e *ecs.Entry) {
idData := simulation.ComId.Get(e)
if idData.Id == dcId {
simulation.ComSwitchOperating.SetValue(e, state.SwitchOperating{EnableStart: true, StartTime: time.Now()})
}
})
}
// 初始化memory
func initMemoryModel() {
simMemory := simulation.ModelsMemory
createSkeletonModelsRelation(simMemory)
createKilometerSignModelsRelation(simMemory)
createPhsicalSectionModelsRelation(simMemory)
}
// 初始化道岔状态
func initSwitchState(world ecs.World) {
simMemory := simulation.ModelsMemory
for _, switchModel := range simMemory.Switchs {
dc1 := &state.SwitchState{Normal: true, Reverse: false}
dc1Entry := world.Create(simulation.ComId, simulation.ComSwitchState, simulation.ComSwitchOperating)
simulation.ComSwitchState.Set(dc1Entry, dc1)
simulation.ComId.Set(dc1Entry, &iwayside.IdData{Id: switchModel.GetId()})
}
}
// 初始化轨道状态
func initLinkState(world ecs.World) {
simMemory := simulation.ModelsMemory
for _, linkModel := range simMemory.Links {
linkState := &state.LinkState{Id: linkModel.GetId(), Occupied: false}
linkEntry := world.Create(simulation.ComLinkState)
simulation.ComLinkState.Set(linkEntry, linkState)
}
}
// 初始化物理区段状态
func initPhsicalSectionState(world ecs.World) {
simMemory := simulation.ModelsMemory
for _, phSecModel := range simMemory.PhysicalSections {
sectionState := &state.PhysicalSectionState{Occupied: false}
sectionEntry := world.Create(simulation.ComId, simulation.ComPhsicalSectionState)
simulation.ComPhsicalSectionState.Set(sectionEntry, sectionState)
simulation.ComId.Set(sectionEntry, &iwayside.IdData{Id: phSecModel.GetId()})
}
}
// 添加列车并初始化
func initTrainState(world ecs.World, trainId string, linkLocation iwayside.LinkLocation, up bool) {
train := &state.TrainState{LinkId: linkLocation.LinkId, LinkOffset: linkLocation.LinkOffset, Up: up}
trainEntry := world.Create(simulation.ComId, simulation.ComTrainState)
simulation.ComTrainState.Set(trainEntry, train)
simulation.ComId.Set(trainEntry, &iwayside.IdData{Id: trainId})
}
// 固定关系构建骨架模型关系即长link与道岔的连接关系
func createSkeletonModelsRelation(memory *simulation.SimMemory) {
//
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
//
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}
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}
}
//
/*
//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
//
}