rts-sim-testing-service/third_party/message/train_control.go
2023-10-26 17:16:07 +08:00

112 lines
3.4 KiB
Go

package message
import (
"encoding/binary"
"math"
"time"
"joylink.club/bj-rtsts-server/ts/protos/state"
)
// 接收到的列车控制信息
type TrainControlMsg struct {
ControlInfo *state.TrainVobcState
}
// 解析VOBC列车信息
func (r *TrainControlMsg) Decode(buf []byte) error {
t := &state.TrainVobcState{}
t.LifeSignal = int32(binary.BigEndian.Uint16(buf[0:2]))
b2 := buf[2]
t.Tc1Active = (b2 & 1) != 0
t.Tc2Active = (b2 & (1 << 1)) != 0
t.DirectionForward = (b2 & (1 << 2)) != 0
t.DirectionBackward = (b2 & (1 << 3)) != 0
t.TractionStatus = (b2 & (1 << 4)) != 0
t.BrakingStatus = (b2 & (1 << 5)) != 0
t.EmergencyBrakingStatus = (b2 & (1 << 6)) != 0
t.TurnbackStatus = (b2 & (1 << 7)) != 0
b3 := buf[3]
t.JumpStatus = (b3 & 1) != 0
t.Ato = (b3 & (1 << 1)) != 0
t.Fam = (b3 & (1 << 2)) != 0
t.Cam = (b3 & (1 << 3)) != 0
t.TractionSafetyCircuit = (b3 & (1 << 4)) != 0
t.ParkingBrakeStatus = (b3 & (1 << 5)) != 0
t.MaintainBrakeStatus = (b3 & (1 << 6)) != 0
t.TractionForce = int64(binary.BigEndian.Uint16(buf[4:6]))
t.BrakeForce = int64(binary.BigEndian.Uint16(buf[6:8]))
t.TrainLoad = int64(binary.BigEndian.Uint16(buf[8:10]))
b4 := buf[15]
t.LeftDoorOpenCommand = (b4 & 1) != 0
t.RightDoorOpenCommand = (b4 & (1 << 1)) != 0
t.LeftDoorCloseCommand = (b4 & (1 << 2)) != 0
t.RightDoorCloseCommand = (b4 & (1 << 3)) != 0
t.AllDoorClose = (b4 & (1 << 4)) != 0
t.UpdateTime = time.Now().UnixMilli()
r.ControlInfo = t
return nil
}
// 发送列车信息
type TrainSpeedMsg struct {
// 生命信号 每个周期+1
lifeSignal uint16
// 列车速度 10=1km/h
speed uint16
// 上坡
upslope bool
// 坡度值 1= 1‰
slope uint16
// 加速度 100 = 1 m/s*s
acceleration uint8
// 减速度 100 = 1 m/s*s
deceleration uint8
// 实际运行阻力 100 = 1KN
totalResistance uint32
// 空气阻力 100 = 1KN
airResistance uint32
// 坡道阻力 100 = 1KN
slopeResistance uint32
// 曲线阻力 100 = 1KN
curveResistance uint32
}
func (t *TrainSpeedMsg) DynamicsDecode(info *DynamicsTrainInfo) {
t.lifeSignal = info.LifeSignal
t.speed = uint16(math.Abs(float64(info.Speed * 36)))
t.upslope = info.UpSlope
t.slope = uint16(info.Slope)
t.totalResistance = uint32(math.Abs(float64(info.TotalResistance / 10)))
t.airResistance = uint32(info.AirResistance / 10)
t.slopeResistance = uint32(math.Abs(float64(info.SlopeResistance / 10)))
t.curveResistance = uint32(info.CurveResistance / 10)
d := math.Abs(float64(info.Acceleration * 100))
if info.Acceleration > 0 {
t.acceleration = uint8(d)
} else {
t.deceleration = uint8(d)
}
}
func (t *TrainSpeedMsg) Encode() []byte {
var data []byte
data = binary.BigEndian.AppendUint16(data, t.lifeSignal)
data = binary.BigEndian.AppendUint16(data, t.speed)
if t.upslope {
data = append(data, 1)
} else {
data = append(data, 0)
}
// 中间预留一位
data = append(data, 0)
data = append(data, t.acceleration) // 加速度 100 = 1 m/s*s
data = append(data, t.deceleration) // 减速度 100 = 1 m/s*s
data = binary.BigEndian.AppendUint32(data, t.totalResistance) // 实际运行阻力 100 = 1KN
data = binary.BigEndian.AppendUint32(data, t.airResistance) // 空气阻力 100 = 1KN
data = binary.BigEndian.AppendUint32(data, t.slopeResistance) // 坡道阻力 100 = 1KN
data = binary.BigEndian.AppendUint32(data, t.curveResistance) // 曲线阻力 100 = 1KN
data = binary.BigEndian.AppendUint16(data, t.slope) // 坡度值 1= 1‰
return data
}