package device_sys import ( "joylink.club/ecs" "joylink.club/ecs/filter" "joylink.club/rtsssimulation/component" "joylink.club/rtsssimulation/entity" ) // MotorSystem 电机马达 type MotorSystem struct { query *ecs.Query } func NewMotorSystem() *MotorSystem { return &MotorSystem{ query: ecs.NewQuery(filter.Contains(component.UidType, component.MotorType)), } } func (s *MotorSystem) Update(w ecs.World) { wd := entity.GetWorldData(w) s.query.Each(w, func(entry *ecs.Entry) { motorId := component.UidType.Get(entry).Id motor := component.MotorType.Get(entry) if motor.Ms.OnForward() { //正转电源 if !motor.Forward && motor.Speed == 0 { motor.Forward = true } } else if motor.Ms.OnReverse() { //反转电源 if motor.Forward && motor.Speed == 0 { motor.Forward = false } } // model, ok := wd.Repo.FindById(motorId).(motorModel) if !ok { model = motorImpl //默认工频 } // speed := motor.Speed + s.calculateAc(motor, model)*float32(w.Tick()) if speed <= 0 { speed = 0 } //变频器 if entry.HasComponent(component.MotorFcType) { fc := component.MotorFcType.Get(entry) SPEED := 60 * float32(fc.F) //当前频率目标转速 if speed > SPEED { speed = SPEED } } else { if speed > model.MotorMaxSpeed() { speed = model.MotorMaxSpeed() } } // motor.Speed = speed }) } func (s *MotorSystem) calculateAc(motor *component.Motor, model motorModel) float32 { //大于0加速,小于0减速 ac := model.MotorMaxSpeed() / float32(model.MotorAcTime()) if motor.Ms.On() { //电源---正转或反转启动 if motor.Ms.OnForward() && motor.RunningReverse() || motor.Ms.OnReverse() && motor.RunningForward() { ac = -ac } } else { //电源关闭 ac = -ac * 2 // r/ms } return ac } var motorImpl = &motorModelImpl{maxSpeed: 3000, acTime: 2000} //默认工频 type motorModel interface { //MotorMaxSpeed 电机最大转速,r/min MotorMaxSpeed() float32 //MotorAcTime 电机由静止加速到最大转速的时间,ms MotorAcTime() uint16 } type motorModelImpl struct { maxSpeed float32 acTime uint16 } func (m *motorModelImpl) MotorMaxSpeed() float32 { return m.maxSpeed } func (m *motorModelImpl) MotorAcTime() uint16 { return m.acTime }