rts-sim-module/fi/motor.go
2023-12-27 11:28:34 +08:00

63 lines
1.7 KiB
Go
Raw Permalink 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 fi
import (
"fmt"
"joylink.club/ecs"
"joylink.club/rtsssimulation/component"
"joylink.club/rtsssimulation/entity"
)
// FiMotorSwitch 电机电源开关定义
type FiMotorSwitch uint8
const (
FiMsOff FiMotorSwitch = iota //关闭电源
FiMsOnForward //正转电源
FiMsOnReverse //反转电源
)
// MotorOperate 电机操作
//
// deviceId 电机id
// fiMs 电机电源;
// fc 电机如果有变频器,设置频率;
func MotorOperate(w ecs.World, deviceId string, fiMs FiMotorSwitch, fc uint16) error {
if fc < 0 || fc > 100 {
return fmt.Errorf("设置变频器频率[%d]不在[0,100]范围内", fc)
}
ms, e := func(fiMs FiMotorSwitch) (component.MotorSwitch, error) {
switch fiMs {
case FiMsOff:
return component.MsOff, nil
case FiMsOnForward:
return component.MsOnForward, nil
case FiMsOnReverse:
return component.MsOnReverse, nil
default:
return 0, fmt.Errorf("参数fiMs[%d]无法映射到component.MotorSwitch", fiMs)
}
}(fiMs)
if e != nil {
return e
}
r := <-ecs.Request[ecs.EmptyType](w, func() ecs.Result[ecs.EmptyType] {
wd := entity.GetWorldData(w)
deviceEntry, ok := wd.EntityMap[deviceId]
if !ok {
return ecs.NewErrResult(fmt.Errorf("设备[%s]实体不存在", deviceId))
}
//
if !deviceEntry.HasComponent(component.MotorType) {
return ecs.NewErrResult(fmt.Errorf("设备[%s]不是电机", deviceId))
}
motor := component.MotorType.Get(deviceEntry)
motor.Ms = ms
if deviceEntry.HasComponent(component.MotorFcType) { //变频电机
component.MotorFcType.Get(deviceEntry).F = fc
}
//
return ecs.NewOkEmptyResult()
})
return r.Err
}