From 747c9a234729a8af3bee20d73ca004bd74d9911b Mon Sep 17 00:00:00 2001 From: xzb <223@qq.com> Date: Fri, 25 Aug 2023 10:50:39 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B1=8F=E8=94=BD=E9=97=A8=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/common.go | 8 +- components/cstate/status.pb.go | 251 +++++++++++++++----------------- creator/init_simulation.go | 10 +- examples/test1/main.go | 8 +- operate/percentage_operation.go | 52 +++---- operate/psd_operation.go | 24 ++- operate/switch_operation.go | 42 ++++-- proto/src/status.proto | 32 ++-- system/debug_system.go | 43 ++++-- system/movable_system.go | 31 ++++ system/percentage_system.go | 59 +++++--- system/psd_system.go | 12 +- system/switch_system.go | 21 +-- 13 files changed, 328 insertions(+), 265 deletions(-) create mode 100644 system/movable_system.go diff --git a/components/common.go b/components/common.go index d4c6957..8385ad7 100644 --- a/components/common.go +++ b/components/common.go @@ -17,9 +17,11 @@ var DeviceIdentityComponent = ecs.NewComponentType[cstate.DeviceIdentity]() // 持有实体标签的组件 var EntityTagHandlerComponent = ecs.NewComponentType[cstate.EntityTagHandler]() -// 百分比设备组件 -var PercentageDeviceComponent = ecs.NewComponentType[cstate.PercentageDeviceState]() -var PercentageDeviceOperatingComponent = ecs.NewComponentType[cstate.PercentageDeviceOperating]() +// 可移动设备组件 +var MovableDeviceStateComponent = ecs.NewComponentType[cstate.MovableDeviceState]() + +// 比率设备组件 +var PercentageDeviceStateComponent = ecs.NewComponentType[cstate.PercentageDeviceState]() // 道岔继电器状态组件 var SwitchRelayStateComponent = ecs.NewComponentType[cstate.SwitchRelayState]() diff --git a/components/cstate/status.pb.go b/components/cstate/status.pb.go index 827ec1b..4dbdb3a 100644 --- a/components/cstate/status.pb.go +++ b/components/cstate/status.pb.go @@ -535,14 +535,16 @@ func (x *PsdState) GetInterlockReleased() bool { return false } -// 百分比设备,具体如道岔、门等 +// 比率设备 type PercentageDeviceState struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - // 当前百分比值,值范围[0,100],L=0,H=100 - Rate int32 `protobuf:"varint,1,opt,name=rate,proto3" json:"rate,omitempty"` + // 当前比率,值范围[0,100000] + Rate int64 `protobuf:"varint,1,opt,name=rate,proto3" json:"rate,omitempty"` + // 最终目标比率 + Target int64 `protobuf:"varint,2,opt,name=target,proto3" json:"target,omitempty"` } func (x *PercentageDeviceState) Reset() { @@ -577,33 +579,37 @@ func (*PercentageDeviceState) Descriptor() ([]byte, []int) { return file_status_proto_rawDescGZIP(), []int{6} } -func (x *PercentageDeviceState) GetRate() int32 { +func (x *PercentageDeviceState) GetRate() int64 { if x != nil { return x.Rate } return 0 } -// 百分比设备操作 -type PercentageDeviceOperating struct { +func (x *PercentageDeviceState) GetTarget() int64 { + if x != nil { + return x.Target + } + return 0 +} + +// 按速度移动的设备,具体如道岔、门等 +type MovableDeviceState struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields // 变动方向 + // true-位置增大;false-位置变小 ToH bool `protobuf:"varint,1,opt,name=toH,proto3" json:"toH,omitempty"` - // 0-100总距离,单位ms - LhDistance int64 `protobuf:"varint,2,opt,name=lhDistance,proto3" json:"lhDistance,omitempty"` - // 本次操作开始时已经移动的距离,单位ms - InitDistance int64 `protobuf:"varint,3,opt,name=initDistance,proto3" json:"initDistance,omitempty"` - // 本次操作一共要移动的距离,单位ms - SumDistance int64 `protobuf:"varint,4,opt,name=sumDistance,proto3" json:"sumDistance,omitempty"` - // 剩余移动距离,单位ms - RemainingDistance int64 `protobuf:"varint,5,opt,name=remainingDistance,proto3" json:"remainingDistance,omitempty"` + // 变动速率,一个world tick的位置变动率 + Speed int32 `protobuf:"varint,2,opt,name=speed,proto3" json:"speed,omitempty"` + // 被移动的物体的位置 + Position int64 `protobuf:"varint,3,opt,name=position,proto3" json:"position,omitempty"` } -func (x *PercentageDeviceOperating) Reset() { - *x = PercentageDeviceOperating{} +func (x *MovableDeviceState) Reset() { + *x = MovableDeviceState{} if protoimpl.UnsafeEnabled { mi := &file_status_proto_msgTypes[7] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) @@ -611,13 +617,13 @@ func (x *PercentageDeviceOperating) Reset() { } } -func (x *PercentageDeviceOperating) String() string { +func (x *MovableDeviceState) String() string { return protoimpl.X.MessageStringOf(x) } -func (*PercentageDeviceOperating) ProtoMessage() {} +func (*MovableDeviceState) ProtoMessage() {} -func (x *PercentageDeviceOperating) ProtoReflect() protoreflect.Message { +func (x *MovableDeviceState) ProtoReflect() protoreflect.Message { mi := &file_status_proto_msgTypes[7] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) @@ -629,42 +635,28 @@ func (x *PercentageDeviceOperating) ProtoReflect() protoreflect.Message { return mi.MessageOf(x) } -// Deprecated: Use PercentageDeviceOperating.ProtoReflect.Descriptor instead. -func (*PercentageDeviceOperating) Descriptor() ([]byte, []int) { +// Deprecated: Use MovableDeviceState.ProtoReflect.Descriptor instead. +func (*MovableDeviceState) Descriptor() ([]byte, []int) { return file_status_proto_rawDescGZIP(), []int{7} } -func (x *PercentageDeviceOperating) GetToH() bool { +func (x *MovableDeviceState) GetToH() bool { if x != nil { return x.ToH } return false } -func (x *PercentageDeviceOperating) GetLhDistance() int64 { +func (x *MovableDeviceState) GetSpeed() int32 { if x != nil { - return x.LhDistance + return x.Speed } return 0 } -func (x *PercentageDeviceOperating) GetInitDistance() int64 { +func (x *MovableDeviceState) GetPosition() int64 { if x != nil { - return x.InitDistance - } - return 0 -} - -func (x *PercentageDeviceOperating) GetSumDistance() int64 { - if x != nil { - return x.SumDistance - } - return 0 -} - -func (x *PercentageDeviceOperating) GetRemainingDistance() int64 { - if x != nil { - return x.RemainingDistance + return x.Position } return 0 } @@ -1191,80 +1183,75 @@ var file_status_proto_rawDesc = []byte{ 0x09, 0x61, 0x6c, 0x6c, 0x4f, 0x70, 0x65, 0x6e, 0x65, 0x64, 0x12, 0x2c, 0x0a, 0x11, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x6c, 0x6f, 0x63, 0x6b, 0x52, 0x65, 0x6c, 0x65, 0x61, 0x73, 0x65, 0x64, 0x18, 0x03, 0x20, 0x01, 0x28, 0x08, 0x52, 0x11, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x6c, 0x6f, 0x63, 0x6b, - 0x52, 0x65, 0x6c, 0x65, 0x61, 0x73, 0x65, 0x64, 0x22, 0x2b, 0x0a, 0x15, 0x50, 0x65, 0x72, 0x63, + 0x52, 0x65, 0x6c, 0x65, 0x61, 0x73, 0x65, 0x64, 0x22, 0x43, 0x0a, 0x15, 0x50, 0x65, 0x72, 0x63, 0x65, 0x6e, 0x74, 0x61, 0x67, 0x65, 0x44, 0x65, 0x76, 0x69, 0x63, 0x65, 0x53, 0x74, 0x61, 0x74, - 0x65, 0x12, 0x12, 0x0a, 0x04, 0x72, 0x61, 0x74, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, - 0x04, 0x72, 0x61, 0x74, 0x65, 0x22, 0xc1, 0x01, 0x0a, 0x19, 0x50, 0x65, 0x72, 0x63, 0x65, 0x6e, - 0x74, 0x61, 0x67, 0x65, 0x44, 0x65, 0x76, 0x69, 0x63, 0x65, 0x4f, 0x70, 0x65, 0x72, 0x61, 0x74, - 0x69, 0x6e, 0x67, 0x12, 0x10, 0x0a, 0x03, 0x74, 0x6f, 0x48, 0x18, 0x01, 0x20, 0x01, 0x28, 0x08, - 0x52, 0x03, 0x74, 0x6f, 0x48, 0x12, 0x1e, 0x0a, 0x0a, 0x6c, 0x68, 0x44, 0x69, 0x73, 0x74, 0x61, - 0x6e, 0x63, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x52, 0x0a, 0x6c, 0x68, 0x44, 0x69, 0x73, - 0x74, 0x61, 0x6e, 0x63, 0x65, 0x12, 0x22, 0x0a, 0x0c, 0x69, 0x6e, 0x69, 0x74, 0x44, 0x69, 0x73, - 0x74, 0x61, 0x6e, 0x63, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x03, 0x52, 0x0c, 0x69, 0x6e, 0x69, - 0x74, 0x44, 0x69, 0x73, 0x74, 0x61, 0x6e, 0x63, 0x65, 0x12, 0x20, 0x0a, 0x0b, 0x73, 0x75, 0x6d, - 0x44, 0x69, 0x73, 0x74, 0x61, 0x6e, 0x63, 0x65, 0x18, 0x04, 0x20, 0x01, 0x28, 0x03, 0x52, 0x0b, - 0x73, 0x75, 0x6d, 0x44, 0x69, 0x73, 0x74, 0x61, 0x6e, 0x63, 0x65, 0x12, 0x2c, 0x0a, 0x11, 0x72, - 0x65, 0x6d, 0x61, 0x69, 0x6e, 0x69, 0x6e, 0x67, 0x44, 0x69, 0x73, 0x74, 0x61, 0x6e, 0x63, 0x65, - 0x18, 0x05, 0x20, 0x01, 0x28, 0x03, 0x52, 0x11, 0x72, 0x65, 0x6d, 0x61, 0x69, 0x6e, 0x69, 0x6e, - 0x67, 0x44, 0x69, 0x73, 0x74, 0x61, 0x6e, 0x63, 0x65, 0x22, 0x3e, 0x0a, 0x0b, 0x42, 0x61, 0x6c, - 0x69, 0x73, 0x65, 0x53, 0x74, 0x61, 0x74, 0x65, 0x12, 0x2f, 0x0a, 0x07, 0x63, 0x6f, 0x6e, 0x74, - 0x65, 0x6e, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x15, 0x2e, 0x63, 0x73, 0x74, 0x61, - 0x74, 0x65, 0x2e, 0x42, 0x61, 0x6c, 0x69, 0x73, 0x65, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, - 0x52, 0x07, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x22, 0x4d, 0x0a, 0x0d, 0x42, 0x61, 0x6c, - 0x69, 0x73, 0x65, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, - 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, 0x12, 0x12, 0x0a, 0x04, 0x74, 0x79, - 0x70, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x04, 0x74, 0x79, 0x70, 0x65, 0x12, 0x18, - 0x0a, 0x07, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0c, 0x52, - 0x07, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x22, 0x6e, 0x0a, 0x14, 0x4f, 0x63, 0x63, 0x75, - 0x70, 0x69, 0x65, 0x64, 0x4c, 0x69, 0x6e, 0x6b, 0x50, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, - 0x12, 0x16, 0x0a, 0x06, 0x6c, 0x69, 0x6e, 0x6b, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, - 0x52, 0x06, 0x6c, 0x69, 0x6e, 0x6b, 0x49, 0x64, 0x12, 0x20, 0x0a, 0x0b, 0x73, 0x74, 0x61, 0x72, - 0x74, 0x4f, 0x66, 0x66, 0x73, 0x65, 0x74, 0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x52, 0x0b, 0x73, - 0x74, 0x61, 0x72, 0x74, 0x4f, 0x66, 0x66, 0x73, 0x65, 0x74, 0x12, 0x1c, 0x0a, 0x09, 0x65, 0x6e, - 0x64, 0x4f, 0x66, 0x66, 0x73, 0x65, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x03, 0x52, 0x09, 0x65, - 0x6e, 0x64, 0x4f, 0x66, 0x66, 0x73, 0x65, 0x74, 0x22, 0x89, 0x01, 0x0a, 0x0a, 0x54, 0x72, 0x61, - 0x69, 0x6e, 0x53, 0x74, 0x61, 0x74, 0x65, 0x12, 0x37, 0x0a, 0x0a, 0x61, 0x63, 0x74, 0x69, 0x76, - 0x65, 0x48, 0x65, 0x61, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x17, 0x2e, 0x63, 0x73, - 0x74, 0x61, 0x74, 0x65, 0x2e, 0x54, 0x72, 0x61, 0x69, 0x6e, 0x41, 0x63, 0x74, 0x69, 0x76, 0x65, - 0x45, 0x6e, 0x75, 0x6d, 0x52, 0x0a, 0x61, 0x63, 0x74, 0x69, 0x76, 0x65, 0x48, 0x65, 0x61, 0x64, - 0x12, 0x42, 0x0a, 0x0d, 0x6f, 0x63, 0x63, 0x75, 0x70, 0x69, 0x65, 0x64, 0x4c, 0x69, 0x6e, 0x6b, - 0x73, 0x18, 0x07, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x1c, 0x2e, 0x63, 0x73, 0x74, 0x61, 0x74, 0x65, - 0x2e, 0x4f, 0x63, 0x63, 0x75, 0x70, 0x69, 0x65, 0x64, 0x4c, 0x69, 0x6e, 0x6b, 0x50, 0x6f, 0x73, - 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x0d, 0x6f, 0x63, 0x63, 0x75, 0x70, 0x69, 0x65, 0x64, 0x4c, - 0x69, 0x6e, 0x6b, 0x73, 0x22, 0x40, 0x0a, 0x16, 0x54, 0x6f, 0x77, 0x50, 0x6f, 0x73, 0x69, 0x74, - 0x69, 0x6f, 0x6e, 0x42, 0x75, 0x74, 0x74, 0x6f, 0x6e, 0x53, 0x74, 0x61, 0x74, 0x65, 0x12, 0x12, - 0x0a, 0x04, 0x70, 0x6f, 0x73, 0x31, 0x18, 0x01, 0x20, 0x01, 0x28, 0x08, 0x52, 0x04, 0x70, 0x6f, - 0x73, 0x31, 0x12, 0x12, 0x0a, 0x04, 0x70, 0x6f, 0x73, 0x32, 0x18, 0x02, 0x20, 0x01, 0x28, 0x08, - 0x52, 0x04, 0x70, 0x6f, 0x73, 0x32, 0x22, 0x2b, 0x0a, 0x0b, 0x42, 0x75, 0x74, 0x74, 0x6f, 0x6e, - 0x53, 0x74, 0x61, 0x74, 0x65, 0x12, 0x1c, 0x0a, 0x09, 0x70, 0x72, 0x65, 0x73, 0x73, 0x44, 0x6f, - 0x77, 0x6e, 0x18, 0x01, 0x20, 0x01, 0x28, 0x08, 0x52, 0x09, 0x70, 0x72, 0x65, 0x73, 0x73, 0x44, - 0x6f, 0x77, 0x6e, 0x22, 0x84, 0x01, 0x0a, 0x14, 0x42, 0x75, 0x74, 0x74, 0x6f, 0x6e, 0x50, 0x72, - 0x65, 0x73, 0x73, 0x4f, 0x70, 0x65, 0x72, 0x61, 0x74, 0x69, 0x6e, 0x67, 0x12, 0x14, 0x0a, 0x05, - 0x73, 0x74, 0x61, 0x72, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x08, 0x52, 0x05, 0x73, 0x74, 0x61, - 0x72, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x64, 0x6f, 0x77, 0x6e, 0x18, 0x02, 0x20, 0x01, 0x28, 0x08, - 0x52, 0x04, 0x64, 0x6f, 0x77, 0x6e, 0x12, 0x20, 0x0a, 0x0b, 0x6f, 0x70, 0x65, 0x72, 0x61, 0x74, - 0x65, 0x54, 0x69, 0x6d, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x03, 0x52, 0x0b, 0x6f, 0x70, 0x65, - 0x72, 0x61, 0x74, 0x65, 0x54, 0x69, 0x6d, 0x65, 0x12, 0x20, 0x0a, 0x0b, 0x6e, 0x65, 0x65, 0x64, - 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x72, 0x6d, 0x18, 0x04, 0x20, 0x01, 0x28, 0x08, 0x52, 0x0b, 0x6e, - 0x65, 0x65, 0x64, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x72, 0x6d, 0x22, 0x4a, 0x0a, 0x16, 0x42, 0x75, - 0x74, 0x74, 0x6f, 0x6e, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x72, 0x6d, 0x4f, 0x70, 0x65, 0x72, 0x61, - 0x74, 0x69, 0x6e, 0x67, 0x12, 0x18, 0x0a, 0x07, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x72, 0x6d, 0x18, - 0x01, 0x20, 0x01, 0x28, 0x08, 0x52, 0x07, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x72, 0x6d, 0x12, 0x16, - 0x0a, 0x06, 0x63, 0x61, 0x6e, 0x63, 0x65, 0x6c, 0x18, 0x02, 0x20, 0x01, 0x28, 0x08, 0x52, 0x06, - 0x63, 0x61, 0x6e, 0x63, 0x65, 0x6c, 0x2a, 0x81, 0x01, 0x0a, 0x0c, 0x53, 0x69, 0x67, 0x6e, 0x61, - 0x6c, 0x41, 0x73, 0x70, 0x65, 0x63, 0x74, 0x12, 0x06, 0x0a, 0x02, 0x4e, 0x6f, 0x10, 0x00, 0x12, - 0x05, 0x0a, 0x01, 0x52, 0x10, 0x01, 0x12, 0x05, 0x0a, 0x01, 0x47, 0x10, 0x02, 0x12, 0x05, 0x0a, - 0x01, 0x59, 0x10, 0x03, 0x12, 0x05, 0x0a, 0x01, 0x57, 0x10, 0x04, 0x12, 0x05, 0x0a, 0x01, 0x42, - 0x10, 0x05, 0x12, 0x06, 0x0a, 0x02, 0x52, 0x59, 0x10, 0x06, 0x12, 0x06, 0x0a, 0x02, 0x52, 0x57, - 0x10, 0x07, 0x12, 0x06, 0x0a, 0x02, 0x47, 0x47, 0x10, 0x08, 0x12, 0x06, 0x0a, 0x02, 0x47, 0x59, - 0x10, 0x09, 0x12, 0x06, 0x0a, 0x02, 0x59, 0x59, 0x10, 0x0a, 0x12, 0x06, 0x0a, 0x02, 0x52, 0x46, - 0x10, 0x0b, 0x12, 0x06, 0x0a, 0x02, 0x59, 0x46, 0x10, 0x0c, 0x12, 0x06, 0x0a, 0x02, 0x47, 0x46, - 0x10, 0x0d, 0x12, 0x06, 0x0a, 0x02, 0x57, 0x46, 0x10, 0x0e, 0x2a, 0x35, 0x0a, 0x0f, 0x54, 0x72, - 0x61, 0x69, 0x6e, 0x41, 0x63, 0x74, 0x69, 0x76, 0x65, 0x45, 0x6e, 0x75, 0x6d, 0x12, 0x0a, 0x0a, - 0x06, 0x48, 0x45, 0x41, 0x44, 0x5f, 0x4e, 0x10, 0x00, 0x12, 0x0a, 0x0a, 0x06, 0x48, 0x45, 0x41, - 0x44, 0x5f, 0x41, 0x10, 0x01, 0x12, 0x0a, 0x0a, 0x06, 0x48, 0x45, 0x41, 0x44, 0x5f, 0x42, 0x10, - 0x02, 0x42, 0x0a, 0x5a, 0x08, 0x2e, 0x2f, 0x63, 0x73, 0x74, 0x61, 0x74, 0x65, 0x62, 0x06, 0x70, - 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x65, 0x12, 0x12, 0x0a, 0x04, 0x72, 0x61, 0x74, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, + 0x04, 0x72, 0x61, 0x74, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x74, 0x61, 0x72, 0x67, 0x65, 0x74, 0x18, + 0x02, 0x20, 0x01, 0x28, 0x03, 0x52, 0x06, 0x74, 0x61, 0x72, 0x67, 0x65, 0x74, 0x22, 0x58, 0x0a, + 0x12, 0x4d, 0x6f, 0x76, 0x61, 0x62, 0x6c, 0x65, 0x44, 0x65, 0x76, 0x69, 0x63, 0x65, 0x53, 0x74, + 0x61, 0x74, 0x65, 0x12, 0x10, 0x0a, 0x03, 0x74, 0x6f, 0x48, 0x18, 0x01, 0x20, 0x01, 0x28, 0x08, + 0x52, 0x03, 0x74, 0x6f, 0x48, 0x12, 0x14, 0x0a, 0x05, 0x73, 0x70, 0x65, 0x65, 0x64, 0x18, 0x02, + 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x73, 0x70, 0x65, 0x65, 0x64, 0x12, 0x1a, 0x0a, 0x08, 0x70, + 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x18, 0x03, 0x20, 0x01, 0x28, 0x03, 0x52, 0x08, 0x70, + 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x22, 0x3e, 0x0a, 0x0b, 0x42, 0x61, 0x6c, 0x69, 0x73, + 0x65, 0x53, 0x74, 0x61, 0x74, 0x65, 0x12, 0x2f, 0x0a, 0x07, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, + 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x15, 0x2e, 0x63, 0x73, 0x74, 0x61, 0x74, 0x65, + 0x2e, 0x42, 0x61, 0x6c, 0x69, 0x73, 0x65, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x52, 0x07, + 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x22, 0x4d, 0x0a, 0x0d, 0x42, 0x61, 0x6c, 0x69, 0x73, + 0x65, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, + 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, 0x12, 0x12, 0x0a, 0x04, 0x74, 0x79, 0x70, 0x65, + 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x04, 0x74, 0x79, 0x70, 0x65, 0x12, 0x18, 0x0a, 0x07, + 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x07, 0x63, + 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x22, 0x6e, 0x0a, 0x14, 0x4f, 0x63, 0x63, 0x75, 0x70, 0x69, + 0x65, 0x64, 0x4c, 0x69, 0x6e, 0x6b, 0x50, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x16, + 0x0a, 0x06, 0x6c, 0x69, 0x6e, 0x6b, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, + 0x6c, 0x69, 0x6e, 0x6b, 0x49, 0x64, 0x12, 0x20, 0x0a, 0x0b, 0x73, 0x74, 0x61, 0x72, 0x74, 0x4f, + 0x66, 0x66, 0x73, 0x65, 0x74, 0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x52, 0x0b, 0x73, 0x74, 0x61, + 0x72, 0x74, 0x4f, 0x66, 0x66, 0x73, 0x65, 0x74, 0x12, 0x1c, 0x0a, 0x09, 0x65, 0x6e, 0x64, 0x4f, + 0x66, 0x66, 0x73, 0x65, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x03, 0x52, 0x09, 0x65, 0x6e, 0x64, + 0x4f, 0x66, 0x66, 0x73, 0x65, 0x74, 0x22, 0x89, 0x01, 0x0a, 0x0a, 0x54, 0x72, 0x61, 0x69, 0x6e, + 0x53, 0x74, 0x61, 0x74, 0x65, 0x12, 0x37, 0x0a, 0x0a, 0x61, 0x63, 0x74, 0x69, 0x76, 0x65, 0x48, + 0x65, 0x61, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x17, 0x2e, 0x63, 0x73, 0x74, 0x61, + 0x74, 0x65, 0x2e, 0x54, 0x72, 0x61, 0x69, 0x6e, 0x41, 0x63, 0x74, 0x69, 0x76, 0x65, 0x45, 0x6e, + 0x75, 0x6d, 0x52, 0x0a, 0x61, 0x63, 0x74, 0x69, 0x76, 0x65, 0x48, 0x65, 0x61, 0x64, 0x12, 0x42, + 0x0a, 0x0d, 0x6f, 0x63, 0x63, 0x75, 0x70, 0x69, 0x65, 0x64, 0x4c, 0x69, 0x6e, 0x6b, 0x73, 0x18, + 0x07, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x1c, 0x2e, 0x63, 0x73, 0x74, 0x61, 0x74, 0x65, 0x2e, 0x4f, + 0x63, 0x63, 0x75, 0x70, 0x69, 0x65, 0x64, 0x4c, 0x69, 0x6e, 0x6b, 0x50, 0x6f, 0x73, 0x69, 0x74, + 0x69, 0x6f, 0x6e, 0x52, 0x0d, 0x6f, 0x63, 0x63, 0x75, 0x70, 0x69, 0x65, 0x64, 0x4c, 0x69, 0x6e, + 0x6b, 0x73, 0x22, 0x40, 0x0a, 0x16, 0x54, 0x6f, 0x77, 0x50, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, + 0x6e, 0x42, 0x75, 0x74, 0x74, 0x6f, 0x6e, 0x53, 0x74, 0x61, 0x74, 0x65, 0x12, 0x12, 0x0a, 0x04, + 0x70, 0x6f, 0x73, 0x31, 0x18, 0x01, 0x20, 0x01, 0x28, 0x08, 0x52, 0x04, 0x70, 0x6f, 0x73, 0x31, + 0x12, 0x12, 0x0a, 0x04, 0x70, 0x6f, 0x73, 0x32, 0x18, 0x02, 0x20, 0x01, 0x28, 0x08, 0x52, 0x04, + 0x70, 0x6f, 0x73, 0x32, 0x22, 0x2b, 0x0a, 0x0b, 0x42, 0x75, 0x74, 0x74, 0x6f, 0x6e, 0x53, 0x74, + 0x61, 0x74, 0x65, 0x12, 0x1c, 0x0a, 0x09, 0x70, 0x72, 0x65, 0x73, 0x73, 0x44, 0x6f, 0x77, 0x6e, + 0x18, 0x01, 0x20, 0x01, 0x28, 0x08, 0x52, 0x09, 0x70, 0x72, 0x65, 0x73, 0x73, 0x44, 0x6f, 0x77, + 0x6e, 0x22, 0x84, 0x01, 0x0a, 0x14, 0x42, 0x75, 0x74, 0x74, 0x6f, 0x6e, 0x50, 0x72, 0x65, 0x73, + 0x73, 0x4f, 0x70, 0x65, 0x72, 0x61, 0x74, 0x69, 0x6e, 0x67, 0x12, 0x14, 0x0a, 0x05, 0x73, 0x74, + 0x61, 0x72, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x08, 0x52, 0x05, 0x73, 0x74, 0x61, 0x72, 0x74, + 0x12, 0x12, 0x0a, 0x04, 0x64, 0x6f, 0x77, 0x6e, 0x18, 0x02, 0x20, 0x01, 0x28, 0x08, 0x52, 0x04, + 0x64, 0x6f, 0x77, 0x6e, 0x12, 0x20, 0x0a, 0x0b, 0x6f, 0x70, 0x65, 0x72, 0x61, 0x74, 0x65, 0x54, + 0x69, 0x6d, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x03, 0x52, 0x0b, 0x6f, 0x70, 0x65, 0x72, 0x61, + 0x74, 0x65, 0x54, 0x69, 0x6d, 0x65, 0x12, 0x20, 0x0a, 0x0b, 0x6e, 0x65, 0x65, 0x64, 0x43, 0x6f, + 0x6e, 0x66, 0x69, 0x72, 0x6d, 0x18, 0x04, 0x20, 0x01, 0x28, 0x08, 0x52, 0x0b, 0x6e, 0x65, 0x65, + 0x64, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x72, 0x6d, 0x22, 0x4a, 0x0a, 0x16, 0x42, 0x75, 0x74, 0x74, + 0x6f, 0x6e, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x72, 0x6d, 0x4f, 0x70, 0x65, 0x72, 0x61, 0x74, 0x69, + 0x6e, 0x67, 0x12, 0x18, 0x0a, 0x07, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x72, 0x6d, 0x18, 0x01, 0x20, + 0x01, 0x28, 0x08, 0x52, 0x07, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x72, 0x6d, 0x12, 0x16, 0x0a, 0x06, + 0x63, 0x61, 0x6e, 0x63, 0x65, 0x6c, 0x18, 0x02, 0x20, 0x01, 0x28, 0x08, 0x52, 0x06, 0x63, 0x61, + 0x6e, 0x63, 0x65, 0x6c, 0x2a, 0x81, 0x01, 0x0a, 0x0c, 0x53, 0x69, 0x67, 0x6e, 0x61, 0x6c, 0x41, + 0x73, 0x70, 0x65, 0x63, 0x74, 0x12, 0x06, 0x0a, 0x02, 0x4e, 0x6f, 0x10, 0x00, 0x12, 0x05, 0x0a, + 0x01, 0x52, 0x10, 0x01, 0x12, 0x05, 0x0a, 0x01, 0x47, 0x10, 0x02, 0x12, 0x05, 0x0a, 0x01, 0x59, + 0x10, 0x03, 0x12, 0x05, 0x0a, 0x01, 0x57, 0x10, 0x04, 0x12, 0x05, 0x0a, 0x01, 0x42, 0x10, 0x05, + 0x12, 0x06, 0x0a, 0x02, 0x52, 0x59, 0x10, 0x06, 0x12, 0x06, 0x0a, 0x02, 0x52, 0x57, 0x10, 0x07, + 0x12, 0x06, 0x0a, 0x02, 0x47, 0x47, 0x10, 0x08, 0x12, 0x06, 0x0a, 0x02, 0x47, 0x59, 0x10, 0x09, + 0x12, 0x06, 0x0a, 0x02, 0x59, 0x59, 0x10, 0x0a, 0x12, 0x06, 0x0a, 0x02, 0x52, 0x46, 0x10, 0x0b, + 0x12, 0x06, 0x0a, 0x02, 0x59, 0x46, 0x10, 0x0c, 0x12, 0x06, 0x0a, 0x02, 0x47, 0x46, 0x10, 0x0d, + 0x12, 0x06, 0x0a, 0x02, 0x57, 0x46, 0x10, 0x0e, 0x2a, 0x35, 0x0a, 0x0f, 0x54, 0x72, 0x61, 0x69, + 0x6e, 0x41, 0x63, 0x74, 0x69, 0x76, 0x65, 0x45, 0x6e, 0x75, 0x6d, 0x12, 0x0a, 0x0a, 0x06, 0x48, + 0x45, 0x41, 0x44, 0x5f, 0x4e, 0x10, 0x00, 0x12, 0x0a, 0x0a, 0x06, 0x48, 0x45, 0x41, 0x44, 0x5f, + 0x41, 0x10, 0x01, 0x12, 0x0a, 0x0a, 0x06, 0x48, 0x45, 0x41, 0x44, 0x5f, 0x42, 0x10, 0x02, 0x42, + 0x0a, 0x5a, 0x08, 0x2e, 0x2f, 0x63, 0x73, 0x74, 0x61, 0x74, 0x65, 0x62, 0x06, 0x70, 0x72, 0x6f, + 0x74, 0x6f, 0x33, } var ( @@ -1282,24 +1269,24 @@ func file_status_proto_rawDescGZIP() []byte { var file_status_proto_enumTypes = make([]protoimpl.EnumInfo, 2) var file_status_proto_msgTypes = make([]protoimpl.MessageInfo, 16) var file_status_proto_goTypes = []interface{}{ - (SignalAspect)(0), // 0: cstate.SignalAspect - (TrainActiveEnum)(0), // 1: cstate.TrainActiveEnum - (*DeviceIdentity)(nil), // 2: cstate.DeviceIdentity - (*SwitchRelayState)(nil), // 3: cstate.SwitchRelayState - (*PhysicalSectionState)(nil), // 4: cstate.PhysicalSectionState - (*SignalState)(nil), // 5: cstate.SignalState - (*SignalDisplayOperating)(nil), // 6: cstate.SignalDisplayOperating - (*PsdState)(nil), // 7: cstate.PsdState - (*PercentageDeviceState)(nil), // 8: cstate.PercentageDeviceState - (*PercentageDeviceOperating)(nil), // 9: cstate.PercentageDeviceOperating - (*BaliseState)(nil), // 10: cstate.BaliseState - (*BaliseContent)(nil), // 11: cstate.BaliseContent - (*OccupiedLinkPosition)(nil), // 12: cstate.OccupiedLinkPosition - (*TrainState)(nil), // 13: cstate.TrainState - (*TowPositionButtonState)(nil), // 14: cstate.TowPositionButtonState - (*ButtonState)(nil), // 15: cstate.ButtonState - (*ButtonPressOperating)(nil), // 16: cstate.ButtonPressOperating - (*ButtonConfirmOperating)(nil), // 17: cstate.ButtonConfirmOperating + (SignalAspect)(0), // 0: cstate.SignalAspect + (TrainActiveEnum)(0), // 1: cstate.TrainActiveEnum + (*DeviceIdentity)(nil), // 2: cstate.DeviceIdentity + (*SwitchRelayState)(nil), // 3: cstate.SwitchRelayState + (*PhysicalSectionState)(nil), // 4: cstate.PhysicalSectionState + (*SignalState)(nil), // 5: cstate.SignalState + (*SignalDisplayOperating)(nil), // 6: cstate.SignalDisplayOperating + (*PsdState)(nil), // 7: cstate.PsdState + (*PercentageDeviceState)(nil), // 8: cstate.PercentageDeviceState + (*MovableDeviceState)(nil), // 9: cstate.MovableDeviceState + (*BaliseState)(nil), // 10: cstate.BaliseState + (*BaliseContent)(nil), // 11: cstate.BaliseContent + (*OccupiedLinkPosition)(nil), // 12: cstate.OccupiedLinkPosition + (*TrainState)(nil), // 13: cstate.TrainState + (*TowPositionButtonState)(nil), // 14: cstate.TowPositionButtonState + (*ButtonState)(nil), // 15: cstate.ButtonState + (*ButtonPressOperating)(nil), // 16: cstate.ButtonPressOperating + (*ButtonConfirmOperating)(nil), // 17: cstate.ButtonConfirmOperating } var file_status_proto_depIdxs = []int32{ 0, // 0: cstate.SignalState.display:type_name -> cstate.SignalAspect @@ -1405,7 +1392,7 @@ func file_status_proto_init() { } } file_status_proto_msgTypes[7].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*PercentageDeviceOperating); i { + switch v := v.(*MovableDeviceState); i { case 0: return &v.state case 1: diff --git a/creator/init_simulation.go b/creator/init_simulation.go index bb474e5..57e0bbb 100644 --- a/creator/init_simulation.go +++ b/creator/init_simulation.go @@ -44,7 +44,8 @@ func initEntites(config *WorldConfig, world ecs.World) { entry := CreateSwitchEntity(world) components.DeviceIdentityComponent.Set(entry, &cstate.DeviceIdentity{Id: md.GetId()}) components.SwitchRelayStateComponent.Set(entry, &cstate.SwitchRelayState{DcJ: false, FcJ: false, DbJ: true, FbJ: false}) - components.PercentageDeviceComponent.Set(entry, &cstate.PercentageDeviceState{Rate: system.SwitchNormalRate}) + components.PercentageDeviceStateComponent.Set(entry, &cstate.PercentageDeviceState{Rate: system.SwitchNormalVaule, Target: system.SwitchNormalVaule}) + components.MovableDeviceStateComponent.Set(entry, &cstate.MovableDeviceState{ToH: false, Speed: 0, Position: system.SwitchNormalVaule}) }) // foreachModels(config.ModelManager, umi.Signal, func(md umi.IDeviceModel) { @@ -62,7 +63,8 @@ func initEntites(config *WorldConfig, world ecs.World) { for _, psdCell := range psd.AllDeviceCells() { cellEntry := CreatePsdCellEntity(world, psdEntry) components.DeviceIdentityComponent.Set(cellEntry, &cstate.DeviceIdentity{Id: psdCell.GetId()}) - components.PercentageDeviceComponent.Set(cellEntry, &cstate.PercentageDeviceState{Rate: system.PsdCellWholeCloseRate}) + components.PercentageDeviceStateComponent.Set(cellEntry, &cstate.PercentageDeviceState{Rate: system.PsdCellWholeCloseValue, Target: system.PsdCellWholeCloseValue}) + components.MovableDeviceStateComponent.Set(cellEntry, &cstate.MovableDeviceState{ToH: true, Speed: 0, Position: system.PsdCellWholeCloseValue}) } }) // @@ -87,7 +89,7 @@ func CreateModelStorageRefEntity(w ecs.World) *ecs.Entry { // 创建道岔实体 func CreateSwitchEntity(w ecs.World) *ecs.Entry { - return w.Create(components.DeviceIdentityComponent, components.SwitchRelayStateComponent, components.PercentageDeviceComponent) + return w.Create(components.DeviceIdentityComponent, components.SwitchRelayStateComponent, components.PercentageDeviceStateComponent, components.MovableDeviceStateComponent) } // 创建信号机实体 @@ -109,7 +111,7 @@ func CreatePsdEntity(w ecs.World) *ecs.Entry { // 创建屏蔽门cell实体 func CreatePsdCellEntity(w ecs.World, psdEntry *ecs.Entry) *ecs.Entry { - return w.Create(components.DeviceIdentityComponent, components.PercentageDeviceComponent, components.EntityTagHandlerComponent.Get(psdEntry).Tag) + return w.Create(components.DeviceIdentityComponent, components.PercentageDeviceStateComponent, components.MovableDeviceStateComponent, components.EntityTagHandlerComponent.Get(psdEntry).Tag) } // 创建应答器实体 diff --git a/examples/test1/main.go b/examples/test1/main.go index 0c79d92..4e1aeec 100644 --- a/examples/test1/main.go +++ b/examples/test1/main.go @@ -26,12 +26,14 @@ func main() { ModelManager: woldModelStorage, Tick: 300, InitTime: time.Now(), - Systems: []ecs.ISystem{system.NewPercentageSystem(), system.NewSwitchSystem(), system.NewPsdSystem(), system.NewTimerSystem(), system.NewDebugSystem()}, + Systems: []ecs.ISystem{system.NewMovableDeviceSystem(), system.NewPercentageDeviceSystem(), system.NewSwitchSystem(), system.NewPsdSystem(), system.NewTimerSystem(), system.NewDebugSystem()}, } world := creator.InitializeWorld(config) // world.StartUp() // + time.Sleep(2 * time.Second) + // testPsdOpt(world) // time.Sleep(120 * time.Second) @@ -100,9 +102,9 @@ func testPsdCellOpt(w ecs.World) { operate.FirePsdCellOperation(w, "psd1", "psd1Cell2", 100) } func testSwitchTurn(w ecs.World) { - operate.FireSwitchTurn(w, "switch1", system.SwitchReverseRate) + operate.FireSwitchTurn(w, "switch1", operate.SwitchReverseRate) time.Sleep(8 * time.Second) - operate.FireSwitchTurn(w, "switch1", system.SwitchNormalRate) + operate.FireSwitchTurn(w, "switch1", operate.SwitchNormalRate) } func testSignalOpt(w ecs.World) { diff --git a/operate/percentage_operation.go b/operate/percentage_operation.go index afea98f..9277c3d 100644 --- a/operate/percentage_operation.go +++ b/operate/percentage_operation.go @@ -5,45 +5,33 @@ import ( "joylink.club/ecs" "joylink.club/rtsssimulation/components" - "joylink.club/rtsssimulation/components/cstate" + "joylink.club/rtsssimulation/system" ) -// 触发操作百分比设备 -// deviceEntry 百分比组件所在的实体 -// lhDistance 百分比设备从0-100的距离,单位ms -// toRate 本次操作最终目标百分比 -func FirePercentageDeviceOperation(w ecs.World, deviceEntry *ecs.Entry, lhDistance int64, toRate int32) error { - if !deviceEntry.HasComponent(components.PercentageDeviceComponent) { - entityId := components.DeviceIdentityComponent.Get(deviceEntry).Id - return fmt.Errorf("实体[%s]中不存在百分比组件", entityId) - } - curRate := components.PercentageDeviceComponent.Get(deviceEntry).Rate - if curRate == toRate { - return nil +// 触发比率设备变动到目标比率 +// lhDistance: 从L到H的总耗时,单位ms +// targetRateValue: 变动的最终目标比率值,[0,100000] +func FirePercentageDevice(world ecs.World, deviceEntry *ecs.Entry, lhDistance int64, targetRateValue int64) error { + if targetRateValue < system.PercentageRateValueMin || targetRateValue > system.PercentageRateValueMax { + return fmt.Errorf("targetRateValue须在[%d,%d]范围内", system.PercentageRateValueMin, system.PercentageRateValueMax) } // - if !deviceEntry.HasComponent(components.PercentageDeviceOperatingComponent) { - deviceEntry.AddComponent(components.PercentageDeviceOperatingComponent) + id := components.DeviceIdentityComponent.Get(deviceEntry).Id + if !deviceEntry.HasComponent(components.PercentageDeviceStateComponent) { + return fmt.Errorf("比率设备[%s]实体中没有百分比组件", id) } - operation := components.PercentageDeviceOperatingComponent.Get(deviceEntry) - // - toHigh := toRate > curRate - var initDistance, sumDistance int64 - if toHigh { - initDistance = int64(float64(lhDistance) * (float64(curRate) / float64(100))) - sumDistance = int64(float64(lhDistance) * (float64(toRate-curRate) / float64(100))) - } else { - initDistance = int64(float64(lhDistance) * (float64(100-curRate) / float64(100))) - sumDistance = int64(float64(lhDistance) * (float64(curRate-toRate) / float64(100))) + if !deviceEntry.HasComponent(components.MovableDeviceStateComponent) { + return fmt.Errorf("比率设备[%s]实体中没有按速度移动组件", id) } // - *operation = cstate.PercentageDeviceOperating{ - ToH: toHigh, - LhDistance: lhDistance, - InitDistance: initDistance, - SumDistance: sumDistance, - RemainingDistance: sumDistance, - } + speed := int32(float64(world.Tick()) * (float64(system.PercentageRateValueMax) / float64(lhDistance))) + // + percent := components.PercentageDeviceStateComponent.Get(deviceEntry) + movable := components.MovableDeviceStateComponent.Get(deviceEntry) + // + percent.Target = targetRateValue + movable.Speed = speed + movable.ToH = targetRateValue > percent.Rate // return nil } diff --git a/operate/psd_operation.go b/operate/psd_operation.go index 229e1f2..e6e4490 100644 --- a/operate/psd_operation.go +++ b/operate/psd_operation.go @@ -10,6 +10,13 @@ import ( "joylink.club/rtsssimulation/umi" ) +const ( + //屏蔽门完全关闭,百分比 + PsdCellWholeCloseRate int8 = system.PercentageRateMax + //屏蔽门完全打开,百分比 + PsdCellWholeOpenRate int8 = system.PercentageRateMin +) + // 车站单侧屏蔽门互锁解除操作 func FirePsdInterlockRelease(world ecs.World, psdId string, lockRelease bool) error { psdEntry, err := findPsdEntry(world, psdId) @@ -31,20 +38,23 @@ func FirePsdOperation(world ecs.World, psdId string, closeOpt bool) error { //屏蔽门标签 psdTag := components.EntityTagHandlerComponent.Get(psdEntry).Tag psdCellQuery := ecs.NewQuery(filter.Contains(psdTag)) + var errPercent error = nil psdCellQuery.Each(world, func(psdCellEntry *ecs.Entry) { - if closeOpt { - FirePercentageDeviceOperation(world, psdCellEntry, findPsdWholeMoveTime(world, psdId), system.PsdCellWholeCloseRate) - } else { - FirePercentageDeviceOperation(world, psdCellEntry, findPsdWholeMoveTime(world, psdId), system.PsdCellWholeOpenRate) + if errPercent == nil { + if closeOpt { + errPercent = FirePercentageDevice(world, psdCellEntry, findPsdWholeMoveTime(world, psdId), system.GetRateValue(PsdCellWholeCloseRate)) + } else { + errPercent = FirePercentageDevice(world, psdCellEntry, findPsdWholeMoveTime(world, psdId), system.GetRateValue(PsdCellWholeOpenRate)) + } } }) // - return nil + return errPercent } // 车站单侧屏蔽门单元cell操作 // closeRate cell最终关上的百分比,100-完全关上,0-完全未关即完全打开,(0,100)内即半关闭 -func FirePsdCellOperation(world ecs.World, psdId string, psdCellId string, closeRate int32) error { +func FirePsdCellOperation(world ecs.World, psdId string, psdCellId string, closeRate int8) error { if closeRate < 0 || closeRate > 100 { return fmt.Errorf("屏蔽门单元操作closeRate(%d)不在[0,100]内", closeRate) } @@ -61,7 +71,7 @@ func FirePsdCellOperation(world ecs.World, psdId string, psdCellId string, close return fmt.Errorf("屏蔽门[%s]的单元门[%s]的实体不存在", psdId, psdCellId) } // - return FirePercentageDeviceOperation(world, psdCellEntry, findPsdWholeMoveTime(world, psdId), closeRate) + return FirePercentageDevice(world, psdCellEntry, findPsdWholeMoveTime(world, psdId), system.GetRateValue(closeRate)) } // 获取屏蔽门实体 diff --git a/operate/switch_operation.go b/operate/switch_operation.go index 7a61a3d..0de214b 100644 --- a/operate/switch_operation.go +++ b/operate/switch_operation.go @@ -9,31 +9,43 @@ import ( "joylink.club/rtsssimulation/umi" ) +const ( + //道岔定位 + SwitchNormalRate int8 = system.PercentageRateMin + //道岔反位 + SwitchReverseRate int8 = system.PercentageRateMax +) + // 触发道岔正常转动 // w: 当前世界 // switchId: 道岔id -// terminalRate: 道岔转动到百分比,规定定位百分比为0,反位百分比为100 -func FireSwitchTurn(w ecs.World, switchId string, terminalRate int32) error { - if terminalRate < 0 || terminalRate > 100 { - return fmt.Errorf("道岔转动终点百分比[%d]不在范围[0,100]内", terminalRate) +// turnRate: 道岔转动到目标百分比[0,100] +func FireSwitchTurn(w ecs.World, switchId string, turnRate int8) error { + if turnRate < 0 || turnRate > 100 { + return fmt.Errorf("道岔操作turnRate(%d)不在[0,100]内", turnRate) } var switchEntry *ecs.Entry = system.QueryEntityById(w, system.SwitchQuery, switchId) if switchEntry == nil { return fmt.Errorf("道岔[%s]的实体不存在", switchId) } // - switchRelay := components.SwitchRelayStateComponent.Get(switchEntry) - switchRate := components.PercentageDeviceComponent.Get(switchEntry) - if terminalRate == switchRate.Rate { - return nil - } - switchRelay.DcJ = terminalRate < switchRate.Rate - switchRelay.FcJ = terminalRate > switchRate.Rate - switchRelay.DbJ = false - switchRelay.FbJ = false + switchPercent := components.PercentageDeviceStateComponent.Get(switchEntry) + turnRateValue := system.GetRateValue(turnRate) // - lhDistance := getSwitchTurnTime(w, switchId) - return FirePercentageDeviceOperation(w, switchEntry, lhDistance, terminalRate) + switchRelay := components.SwitchRelayStateComponent.Get(switchEntry) + if turnRateValue == switchPercent.Rate { + return nil + } else { + lhDistance := getSwitchTurnTime(w, switchId) + if turnRateValue > switchPercent.Rate { //反操 + switchRelay.DcJ = false + switchRelay.FcJ = true + } else { //定操 + switchRelay.DcJ = true + switchRelay.FcJ = false + } + return FirePercentageDevice(w, switchEntry, lhDistance, turnRateValue) + } } // 获取道岔转动耗时ms diff --git a/proto/src/status.proto b/proto/src/status.proto index 8b8ef31..a0e038e 100644 --- a/proto/src/status.proto +++ b/proto/src/status.proto @@ -102,24 +102,24 @@ message PsdState{ //////////////////////////////////////////////////////// -//百分比设备,具体如道岔、门等 +//比率设备 message PercentageDeviceState{ - //当前百分比值,值范围[0,100],L=0,H=100 - int32 rate =1; -} -//百分比设备操作 -message PercentageDeviceOperating{ - //变动方向 - bool toH = 1; - //0-100总距离,单位ms - int64 lhDistance = 2; - //本次操作开始时已经移动的距离,单位ms - int64 initDistance = 3; - //本次操作一共要移动的距离,单位ms - int64 sumDistance = 4; - //剩余移动距离,单位ms - int64 remainingDistance = 5; + //当前比率,值范围[0,100000] + int64 rate = 1; + //最终目标比率 + int64 target = 2; } + +//按速度移动的设备,具体如道岔、门等 +message MovableDeviceState{ + //变动方向 + //true-位置增大;false-位置变小 + bool toH = 1; + //变动速率,一个world tick的位置变动率 + int32 speed = 2; + //被移动的物体的位置 + int64 position = 3; +} //////////////////////////////////////////////////////// // 应答器状态 diff --git a/system/debug_system.go b/system/debug_system.go index 6d344e2..a59993d 100644 --- a/system/debug_system.go +++ b/system/debug_system.go @@ -18,12 +18,27 @@ func NewDebugSystem() *DebugSystem { // world 执行 func (me *DebugSystem) Update(w ecs.World) { + //debugMovableDevice(w) debugPsd(w) + //debugSwitch(w) //debugSignal(w) //debugTowPosButton(w) } +// 可按速度移动组件 +func debugMovableDevice(w ecs.World) { + query := ecs.NewQuery(filter.Contains(components.PercentageDeviceStateComponent, components.MovableDeviceStateComponent)) + query.Each(w, func(e *ecs.Entry) { + id := components.DeviceIdentityComponent.Get(e).Id + if "psd1Cell1" == id { + percent := components.PercentageDeviceStateComponent.Get(e) + movable := components.MovableDeviceStateComponent.Get(e) + fmt.Printf("==>>屏蔽门cell [%s],curRateValue = %d ,targetRateValue = %d , speed = %d position = %d\n", id, percent.Rate, percent.Target, movable.Speed, movable.Position) + } + }) +} + // 两档位按钮旋钮 func debugTowPosButton(w ecs.World) { towPosButtonsQuery := ecs.NewQuery(filter.Contains(components.DeviceIdentityComponent, components.TowPositionButtonStateComponent)) @@ -60,20 +75,21 @@ func debugPsd(w ecs.World) { psdTag := components.EntityTagHandlerComponent.Get(e).Tag fmt.Printf("屏蔽门[%s] ,全关=%t ,全开=%t ,互锁解除=%t > ", psdId, psdState.AllClosed, psdState.AllOpened, psdState.InterlockReleased) // - psdCellQuery := ecs.NewQuery(filter.Contains(psdTag, components.PercentageDeviceComponent)) + psdCellQuery := ecs.NewQuery(filter.Contains(psdTag)) psdCellQuery.Each(w, func(e *ecs.Entry) { psdCellId := components.DeviceIdentityComponent.Get(e).Id - psdCellState := components.PercentageDeviceComponent.Get(e) + psdCellPercent := components.PercentageDeviceStateComponent.Get(e) + psdCellMovable := components.MovableDeviceStateComponent.Get(e) // - fmt.Printf("|| cell[%s] ,closeRate=%d ", psdCellId, psdCellState.Rate) - if e.HasComponent(components.PercentageDeviceOperatingComponent) { - psdCellOpt := components.PercentageDeviceOperatingComponent.Get(e) - if psdCellOpt.ToH { - fmt.Printf("== 关门操作中,剩余移动距离=%d", psdCellOpt.RemainingDistance) + fmt.Printf("|| cell[%s] ,closeRate=%d ", psdCellId, GetRate(psdCellPercent.Rate)) + if psdCellMovable.Speed > 0 { + if psdCellMovable.ToH { + fmt.Printf("== 关门操作中,移动速率=%d", psdCellMovable.Speed) } else { - fmt.Printf("== 开门操作中,剩余移动距离=%d", psdCellOpt.RemainingDistance) + fmt.Printf("== 开门操作中,移动速率=%d", psdCellMovable.Speed) } } + }) // fmt.Println() @@ -92,15 +108,16 @@ func debugSignal(w ecs.World) { // 显示道岔状态 func debugSwitch(w ecs.World) { - switchesQuery := ecs.NewQuery(filter.Contains(components.DeviceIdentityComponent, components.SwitchRelayStateComponent, components.PercentageDeviceComponent)) + switchesQuery := ecs.NewQuery(filter.Contains(components.DeviceIdentityComponent, components.SwitchRelayStateComponent)) switchesQuery.Each(w, func(e *ecs.Entry) { + percent := components.PercentageDeviceStateComponent.Get(e) + movable := components.MovableDeviceStateComponent.Get(e) id := components.DeviceIdentityComponent.Get(e).Id - curRate := components.PercentageDeviceComponent.Get(e).Rate + curRate := GetRate(percent.Rate) j := components.SwitchRelayStateComponent.Get(e) fmt.Printf("道岔 [%s],当前位置百分比 [%d] ,Dcj = %t, Fcj = %t, Dbj = %t ,Fbj = %t ,", id, curRate, j.DcJ, j.FcJ, j.DbJ, j.FbJ) - if e.HasComponent(components.PercentageDeviceOperatingComponent) { - opt := components.PercentageDeviceOperatingComponent.Get(e) - fmt.Printf(" ==> 正在转动,剩余距离[%d]ms", opt.RemainingDistance) + if movable.Speed > 0 { + fmt.Printf(" ==> 正在转动,移动速率[%d]", movable.Speed) } fmt.Println() }) diff --git a/system/movable_system.go b/system/movable_system.go new file mode 100644 index 0000000..d9196f2 --- /dev/null +++ b/system/movable_system.go @@ -0,0 +1,31 @@ +package system + +import ( + "github.com/yohamta/donburi/filter" + "joylink.club/ecs" + "joylink.club/rtsssimulation/components" +) + +// 按速度移动的设备系统 +// 只负责按速度移动位置,由该组件的观察者负责解释位置 +type MovableDeviceSystem struct { + query *ecs.Query +} + +func NewMovableDeviceSystem() *MovableDeviceSystem { + return &MovableDeviceSystem{query: ecs.NewQuery(filter.Contains(components.MovableDeviceStateComponent))} +} + +// world 执行 +func (me *MovableDeviceSystem) Update(world ecs.World) { + me.query.Each(world, func(e *ecs.Entry) { + md := components.MovableDeviceStateComponent.Get(e) + if md.Speed > 0 { + if md.ToH { + md.Position += int64(md.Speed) + } else { + md.Position -= int64(md.Speed) + } + } + }) +} diff --git a/system/percentage_system.go b/system/percentage_system.go index 1a4c33d..1529c1a 100644 --- a/system/percentage_system.go +++ b/system/percentage_system.go @@ -6,34 +6,53 @@ import ( "joylink.club/rtsssimulation/components" ) -// 百分比设备系统 -type PercentageSystem struct { +type PercentageDeviceSystem struct { query *ecs.Query } -func NewPercentageSystem() *PercentageSystem { - return &PercentageSystem{query: ecs.NewQuery(filter.Contains(components.PercentageDeviceComponent, components.PercentageDeviceOperatingComponent))} +func NewPercentageDeviceSystem() *PercentageDeviceSystem { + return &PercentageDeviceSystem{query: ecs.NewQuery(filter.Contains(components.PercentageDeviceStateComponent, components.MovableDeviceStateComponent))} } +const ( + PercentageRateValueMax int64 = 100000 + PercentageRateValueMin int64 = 0 +) +const ( + PercentageRateMax int8 = 100 + PercentageRateMin int8 = 0 +) + // world 执行 -func (me *PercentageSystem) Update(world ecs.World) { +func (me *PercentageDeviceSystem) Update(world ecs.World) { me.query.Each(world, func(e *ecs.Entry) { - operation := components.PercentageDeviceOperatingComponent.Get(e) - if operation.RemainingDistance <= 0 { - operation.RemainingDistance = 0 - e.RemoveComponent(components.PercentageDeviceOperatingComponent) - } else { - operation.RemainingDistance -= int64(world.Tick()) - if operation.RemainingDistance < 0 { - operation.RemainingDistance = 0 + percentage := components.PercentageDeviceStateComponent.Get(e) + if percentage.Rate != percentage.Target { + movable := components.MovableDeviceStateComponent.Get(e) + if movable.ToH { + if movable.Position >= percentage.Target { + movable.Speed = 0 + movable.Position = percentage.Target + } + } else { + if movable.Position <= percentage.Target { + movable.Speed = 0 + movable.Position = percentage.Target + } } + // + percentage.Rate = movable.Position } - // - rateDevice := components.PercentageDeviceComponent.Get(e) - if operation.ToH { //百分比变大 - rateDevice.Rate = int32(float64(100) * (float64(operation.InitDistance+operation.SumDistance-operation.RemainingDistance) / float64(operation.LhDistance))) - } else { - rateDevice.Rate = 100 - int32(float64(100)*(float64(operation.InitDistance+operation.SumDistance-operation.RemainingDistance)/float64(operation.LhDistance))) - } + }) } + +// 映射百分比到大数值 +func GetRateValue(rate int8) int64 { + return int64(float64(float32(rate)/float32(100)) * float64(PercentageRateValueMax)) +} + +// 大数值映射到百分比 +func GetRate(rateValue int64) int8 { + return int8((float64(rateValue) / float64(PercentageRateValueMax)) * float64(100)) +} diff --git a/system/psd_system.go b/system/psd_system.go index 3dade75..0aae36d 100644 --- a/system/psd_system.go +++ b/system/psd_system.go @@ -11,9 +11,9 @@ var PsdQuery = ecs.NewQuery(filter.Contains(components.DeviceIdentityComponent, const ( //屏蔽门完全关闭 - PsdCellWholeCloseRate = 100 + PsdCellWholeCloseValue = PercentageRateValueMax //屏蔽门完全打开 - PsdCellWholeOpenRate = 0 + PsdCellWholeOpenValue = PercentageRateValueMin ) type PsdSystem struct { @@ -30,18 +30,18 @@ func (me *PsdSystem) Update(world ecs.World) { psdTag := components.EntityTagHandlerComponent.Get(psdEntry).Tag psdCellQuery, ok := me.cellsQuery[psdTag] if !ok { - psdCellQuery = ecs.NewQuery(filter.Contains(psdTag, components.PercentageDeviceComponent)) + psdCellQuery = ecs.NewQuery(filter.Contains(psdTag, components.PercentageDeviceStateComponent, components.MovableDeviceStateComponent)) me.cellsQuery[psdTag] = psdCellQuery } // var allCellClosed, allCellOpened bool = true, true psdCellQuery.Each(world, func(psdCellEntry *ecs.Entry) { - psdCellRate := components.PercentageDeviceComponent.Get(psdCellEntry).Rate + psdCellRate := components.PercentageDeviceStateComponent.Get(psdCellEntry).Rate if allCellClosed { - allCellClosed = psdCellRate == PsdCellWholeCloseRate + allCellClosed = psdCellRate == PsdCellWholeCloseValue } if allCellOpened { - allCellOpened = psdCellRate == PsdCellWholeOpenRate + allCellOpened = psdCellRate == PsdCellWholeOpenValue } }) // diff --git a/system/switch_system.go b/system/switch_system.go index 55f93b6..e80d809 100644 --- a/system/switch_system.go +++ b/system/switch_system.go @@ -6,13 +6,13 @@ import ( "joylink.club/rtsssimulation/components" ) -var SwitchQuery *ecs.Query = ecs.NewQuery(filter.Contains(components.DeviceIdentityComponent, components.SwitchRelayStateComponent, components.PercentageDeviceComponent)) +var SwitchQuery *ecs.Query = ecs.NewQuery(filter.Contains(components.DeviceIdentityComponent, components.SwitchRelayStateComponent, components.PercentageDeviceStateComponent)) const ( //道岔定位 - SwitchNormalRate int32 = 0 + SwitchNormalVaule int64 = PercentageRateValueMin //道岔反位 - SwitchReverseRate int32 = 100 + SwitchReverseValue int64 = PercentageRateValueMax ) // 道岔系统操作 @@ -26,20 +26,13 @@ func NewSwitchSystem() *SwitchSystem { // world 执行 func (me *SwitchSystem) Update(w ecs.World) { SwitchQuery.Each(w, func(e *ecs.Entry) { - switchRate := components.PercentageDeviceComponent.Get(e) + switchPercent := components.PercentageDeviceStateComponent.Get(e) switchRelay := components.SwitchRelayStateComponent.Get(e) - if switchRate.Rate <= 0 { - switchRelay.DcJ = false - switchRelay.DbJ = true - } - if switchRate.Rate >= 100 { - switchRelay.FcJ = false - switchRelay.FbJ = true - } - //过了操作时限,定操反操继电器自动复位 - if !e.HasComponent(components.PercentageDeviceOperatingComponent) { + if switchPercent.Rate == switchPercent.Target { switchRelay.DcJ = false switchRelay.FcJ = false + switchRelay.DbJ = switchPercent.Rate <= SwitchNormalVaule + switchRelay.FbJ = switchPercent.Rate >= SwitchReverseValue } else { switchRelay.DbJ = false switchRelay.FbJ = false