添加洗车机模型和ecs组件、实体、系统

This commit is contained in:
thesai 2024-03-08 17:11:53 +08:00
parent 669fa31625
commit 39be7b4bfa
7 changed files with 193 additions and 79 deletions

27
component/xcj.go Normal file
View File

@ -0,0 +1,27 @@
package component
import (
"joylink.club/ecs"
)
var (
XcjTag = ecs.NewTag()
XcjCircuitType = ecs.NewComponentType[XcjCircuit]()
)
type XcjCircuit struct {
//联锁驱动的继电器
XQJ *ecs.Entry //洗车请求
TWJ1 *ecs.Entry //头部停稳
TWJ2 *ecs.Entry //尾部停稳
TWJ3 *ecs.Entry //中部停稳
TGQJ *ecs.Entry //通过请求
XCJXJ *ecs.Entry //洗车就绪
XCYXJ *ecs.Entry //洗车允许
CFJ1 *ecs.Entry //头部移动允许
CFJ2 *ecs.Entry //尾部移动允许
CFJ3 *ecs.Entry //中部移动允许
JTJ *ecs.Entry //紧急停车
TGYXJ *ecs.Entry //通过允许
}

View File

@ -7,13 +7,13 @@ import (
"unsafe"
)
var MkxBaseComponentTypes = []ecs.IComponentType{component.UidType, component.CkmTag, component.CkmStateType, component.FixedPositionTransformType}
var CkmBaseComponentTypes = []ecs.IComponentType{component.UidType, component.CkmTag, component.CkmStateType, component.FixedPositionTransformType}
func LoadCkm(w ecs.World) error {
data := GetWorldData(w)
for _, ckm := range data.Repo.CkmList() {
//创建基础entry
entry := w.Entry(w.Create(MkxBaseComponentTypes...))
entry := w.Entry(w.Create(CkmBaseComponentTypes...))
component.UidType.SetValue(entry, component.Uid{Id: ckm.Id()})
data.EntityMap[ckm.Id()] = entry
//加载电路

View File

@ -51,5 +51,10 @@ func Load(w ecs.World, repo *repository.Repository) error {
if err != nil {
return err
}
//加载洗车机
err = LoadXcj(w)
if err != nil {
return err
}
return err
}

57
entity/xcj.go Normal file
View File

@ -0,0 +1,57 @@
package entity
import (
"joylink.club/ecs"
"joylink.club/rtsssimulation/component"
"joylink.club/rtsssimulation/repository"
"unsafe"
)
var XcjBaseComponentTypes = []ecs.IComponentType{component.UidType, component.XcjTag}
func LoadXcj(w ecs.World) error {
wd := GetWorldData(w)
for _, xcj := range wd.Repo.XcjList() {
//创建基础entry
entry := w.Entry(w.Create(XcjBaseComponentTypes...))
component.UidType.SetValue(entry, component.Uid{Id: xcj.Id()})
wd.EntityMap[xcj.Id()] = entry
//加载电路
if len(xcj.ComponentGroups()) != 0 {
circuit := &component.XcjCircuit{}
entry.AddComponent(component.XcjCircuitType, unsafe.Pointer(circuit))
for _, group := range xcj.ComponentGroups() {
for _, ec := range group.Components() {
relay := ec.(*repository.Relay)
switch ec.Code() {
case "XQJ":
circuit.XQJ = NewRelayEntity(w, relay, wd.EntityMap)
case "TWJ1":
circuit.TWJ1 = NewRelayEntity(w, relay, wd.EntityMap)
case "TWJ2":
circuit.TWJ2 = NewRelayEntity(w, relay, wd.EntityMap)
case "TWJ3":
circuit.TWJ3 = NewRelayEntity(w, relay, wd.EntityMap)
case "TGQJ":
circuit.TGQJ = NewRelayEntity(w, relay, wd.EntityMap)
case "XCJXJ":
circuit.XCJXJ = NewRelayEntity(w, relay, wd.EntityMap)
case "XCYXJ":
circuit.XCYXJ = NewRelayEntity(w, relay, wd.EntityMap)
case "CFJ1":
circuit.CFJ1 = NewRelayEntity(w, relay, wd.EntityMap)
case "CFJ2":
circuit.CFJ2 = NewRelayEntity(w, relay, wd.EntityMap)
case "CFJ3":
circuit.CFJ3 = NewRelayEntity(w, relay, wd.EntityMap)
case "JTJ":
circuit.JTJ = NewRelayEntity(w, relay, wd.EntityMap)
case "TGYXJ":
circuit.TGYXJ = NewRelayEntity(w, relay, wd.EntityMap)
}
}
}
}
}
return nil
}

View File

@ -1,77 +0,0 @@
package main
import (
"log/slog"
"os"
"time"
"joylink.club/ecs"
rtss_simulation "joylink.club/rtsssimulation"
"joylink.club/rtsssimulation/consts"
"joylink.club/rtsssimulation/entity"
"joylink.club/rtsssimulation/examples/signal_2xh1/sigSys"
"joylink.club/rtsssimulation/fi"
"joylink.club/rtsssimulation/repository"
"joylink.club/rtsssimulation/repository/model/proto"
)
const (
IdSignal2XH1 = "signal_2xh1-1"
)
// 信号机测试
func main() {
//
logConfig := &slog.HandlerOptions{AddSource: false, Level: slog.LevelDebug}
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stderr, logConfig)))
//
proto := &proto.Repository{}
proto.Id = "test-for-signal"
proto.Version = "v1.0"
addProtoSignal2XH1(proto)
repo := repository.BuildRepositoryForSignalTest(proto)
sim, _ := rtss_simulation.NewSimulation(repo)
loadEntities(sim, repo)
sim.SetSpeed(0.1)
sim.AddSystem(sigSys.NewSignalDebugSystem())
sim.StartUp()
//
time.Sleep(1 * time.Second)
slog.Debug("灭灯 .....")
fi.DriveSignal2XH1Dd(sim, IdSignal2XH1, false) //灭灯
time.Sleep(2 * time.Second)
slog.Debug("亮灯 .....")
fi.DriveSignal2XH1Dd(sim, IdSignal2XH1, true) //亮灯
time.Sleep(2 * time.Second)
slog.Debug("开通列车信号 .....")
fi.DriveSignal2XH1Lx(sim, IdSignal2XH1) //开通列车信号
time.Sleep(3 * time.Second)
slog.Debug("开通禁止信号 .....")
fi.DriveSignal2XH1Non(sim, IdSignal2XH1) //开通禁止信号
//
time.Sleep(3 * time.Second)
sim.Close()
}
func addProtoSignal2XH1(r *proto.Repository) {
//相关继电器
r.Relays = append(r.Relays, &proto.Relay{Id: "2xh1-ddj", Code: consts.SIGNAL_DDJ, Model: proto.Relay_JWXC_1700})
r.Relays = append(r.Relays, &proto.Relay{Id: "2xh1-dj", Code: consts.SIGNAL_DJ, Model: proto.Relay_JZXC_H18})
r.Relays = append(r.Relays, &proto.Relay{Id: "2xh1-lxj", Code: consts.SIGNAL_LXJ, Model: proto.Relay_JWXC_1700})
//
signal := &proto.Signal{}
signal.Id = IdSignal2XH1
signal.Km = &proto.Kilometer{}
//
group := &proto.ElectronicComponentGroup{Code: consts.SIGNAL_2XH1, ComponentIds: []string{"2xh1-ddj", "2xh1-dj", "2xh1-lxj"}}
//
signal.ElectronicComponentGroups = append(signal.ElectronicComponentGroups, group)
r.Signals = append(r.Signals, signal)
}
func loadEntities(w ecs.World, repo *repository.Repository) {
// 初始化世界数据单例组件
entity.LoadWorldData(w, repo)
//
if le := entity.LoadSignals(w); le != nil {
slog.Error(le.Error())
}
}

View File

@ -28,6 +28,8 @@ func BindSystem(w ecs.World) {
circuit_sys.NewSignalDCXHSystem(),
circuit_sys.NewSignalJCKXHSystem(),
circuit_sys.NewSignalJDXHSystem(),
circuit_sys.NewCkmSys(),
circuit_sys.NewXcjSys(),
device_sys.NewLightSys(),
//屏蔽门
circuit_sys.NewPsdSys(),

100
sys/circuit_sys/xcj.go Normal file
View File

@ -0,0 +1,100 @@
package circuit_sys
import (
"joylink.club/ecs"
"joylink.club/ecs/filter"
"joylink.club/rtsssimulation/component"
"joylink.club/rtsssimulation/entity"
"log/slog"
)
type XcjSys struct {
query *ecs.Query
}
func NewXcjSys() *XcjSys {
return &XcjSys{query: ecs.NewQuery(filter.Contains(entity.XcjBaseComponentTypes...))}
}
func (x *XcjSys) Update(w ecs.World) {
wd := entity.GetWorldData(w)
x.query.Each(w, func(entry *ecs.Entry) {
if entry.HasComponent(component.XcjCircuitType) {
circuit := component.XcjCircuitType.Get(entry)
//处理联锁对继电器的驱动
handleDrive(wd, circuit)
//驱动继电器
x.drive(circuit)
}
})
}
func (x *XcjSys) drive(circuit *component.XcjCircuit) {
if component.BitStateType.Get(circuit.XCJXJ).Val { //洗车就绪
if component.BitStateType.Get(circuit.XQJ).Val { //洗车请求
component.RelayDriveType.Get(circuit.XCYXJ).Td = true
component.RelayDriveType.Get(circuit.XCJXJ).Td = false
}
} else if component.BitStateType.Get(circuit.XCYXJ).Val { //洗车允许
if component.BitStateType.Get(circuit.TWJ1).Val {
component.RelayDriveType.Get(circuit.CFJ1).Td = true
}
if component.BitStateType.Get(circuit.TWJ2).Val {
component.RelayDriveType.Get(circuit.CFJ2).Td = true
}
if component.BitStateType.Get(circuit.TWJ3).Val {
component.RelayDriveType.Get(circuit.CFJ3).Td = true
}
if component.BitStateType.Get(circuit.TGQJ).Val {
component.RelayDriveType.Get(circuit.TGYXJ).Td = true
}
} else if component.BitStateType.Get(circuit.TGYXJ).Val { //通过允许
if !component.BitStateType.Get(circuit.XQJ).Val && !component.BitStateType.Get(circuit.TGQJ).Val {
component.RelayDriveType.Get(circuit.XCJXJ).Td = true
component.RelayDriveType.Get(circuit.XCYXJ).Td = false
component.RelayDriveType.Get(circuit.CFJ1).Td = false
component.RelayDriveType.Get(circuit.CFJ2).Td = false
component.RelayDriveType.Get(circuit.CFJ3).Td = false
component.RelayDriveType.Get(circuit.TGYXJ).Td = false
}
}
}
// 处理联锁对继电器的驱动
func handleDrive(wd *component.WorldData, circuit *component.XcjCircuit) {
XQJId := component.UidType.Get(circuit.XQJ).Id
XQJBit, err := wd.QueryQdBit(XQJId)
if err != nil {
slog.Error(err.Error())
} else {
component.RelayDriveType.Get(circuit.XQJ).Td = XQJBit
}
TWJ1Id := component.UidType.Get(circuit.TWJ1).Id
TWJ1Bit, err := wd.QueryQdBit(TWJ1Id)
if err != nil {
slog.Error(err.Error())
} else {
component.RelayDriveType.Get(circuit.TWJ1).Td = TWJ1Bit
}
TWJ2Id := component.UidType.Get(circuit.TWJ2).Id
TWJ2Bit, err := wd.QueryQdBit(TWJ2Id)
if err != nil {
slog.Error(err.Error())
} else {
component.RelayDriveType.Get(circuit.TWJ2).Td = TWJ2Bit
}
TWJ3Id := component.UidType.Get(circuit.TWJ3).Id
TWJ3Bit, err := wd.QueryQdBit(TWJ3Id)
if err != nil {
slog.Error(err.Error())
} else {
component.RelayDriveType.Get(circuit.TWJ3).Td = TWJ3Bit
}
TGQJId := component.UidType.Get(circuit.TGQJ).Id
TGQJBit, err := wd.QueryQdBit(TGQJId)
if err != nil {
slog.Error(err.Error())
} else {
component.RelayDriveType.Get(circuit.TGQJ).Td = TGQJBit
}
}