rts-sim-testing-service/third_party/radar/radar_vobc.go

150 lines
4.9 KiB
Go
Raw Normal View History

2024-01-24 17:12:34 +08:00
package radar
import (
"context"
"fmt"
"joylink.club/bj-rtsts-server/config"
2024-04-28 11:01:12 +08:00
"joylink.club/bj-rtsts-server/dto/common_proto"
"joylink.club/bj-rtsts-server/dto/state_proto"
2024-01-24 17:12:34 +08:00
"joylink.club/bj-rtsts-server/third_party/message"
"joylink.club/bj-rtsts-server/third_party/udp"
"log/slog"
2024-04-28 11:01:12 +08:00
"math"
2024-01-24 17:12:34 +08:00
"sync"
2024-04-28 11:01:12 +08:00
"time"
2024-01-24 17:12:34 +08:00
)
type RadarVobc interface {
Start(radar RadarVobcManager)
Stop()
2024-04-19 16:55:51 +08:00
SendMsg(ri *message.RadarInfo)
2024-04-28 11:01:12 +08:00
TrainSpeedSender(info *message.DynamicsTrainInfo, trainState *state_proto.TrainState)
2024-01-24 17:12:34 +08:00
}
type RadarVobcManager interface {
GetRunRadarConfig() config.RadarConfig
2024-04-19 16:55:51 +08:00
//FindRadarTrain() *state_proto.TrainState
2024-01-24 17:12:34 +08:00
}
const radar_interval = 15
var (
2024-01-26 17:57:35 +08:00
initLock = sync.Mutex{}
_radar *radarVobc
fixed_speed = 0.009155
driftDefaultVal = 0.1
2024-01-24 17:12:34 +08:00
)
func Default() RadarVobc {
2024-01-25 10:57:34 +08:00
defer initLock.Unlock()
2024-01-24 17:12:34 +08:00
initLock.Lock()
if _radar == nil {
_radar = &radarVobc{}
}
return _radar
}
type radarVobc struct {
radarVobcTaskContext context.CancelFunc
vobcClient udp.UdpClient
radarVobcManager RadarVobcManager
}
func (rv *radarVobc) Start(radar RadarVobcManager) {
config := radar.GetRunRadarConfig()
if config.RemoteIp == "" || config.RemotePort == 0 || !config.Open {
slog.Info("雷达未开启", "远端ip:", config.RemoteIp, "远端端口:", config.RemotePort, "是否开启:", config.Open)
return
}
rv.vobcClient = udp.NewClient(fmt.Sprintf("%v:%v", config.RemoteIp, config.RemotePort))
2024-04-19 16:55:51 +08:00
//ctx, cancleFunc := context.WithCancel(context.Background())
//rv.radarVobcTaskContext = cancleFunc
2024-01-24 17:12:34 +08:00
rv.radarVobcManager = radar
2024-04-19 16:55:51 +08:00
//go rv.sendRadarInfo(ctx)
2024-01-24 17:12:34 +08:00
}
2024-01-25 09:57:17 +08:00
// sendRadarInfo 发送速度,位移计数 给vobc
2024-04-19 16:55:51 +08:00
func (rv *radarVobc) SendMsg(ri *message.RadarInfo) {
rv.vobcClient.Send(ri.Encode())
2024-04-28 11:01:12 +08:00
}
func (rv *radarVobc) TrainSpeedSender(info *message.DynamicsTrainInfo, trainState *state_proto.TrainState) {
forward := trainState.VobcState.DirectionForward
if trainState.VobcState.Tc1Active && trainState.TrainEndsA.RadarEnable {
s := parseRadarSpeedData(info.Speed, trainState.TrainEndsA)
rv.SendMsg(message.NewRadarSender(s, forward, info.Displacement))
} else if trainState.VobcState.Tc2Active && trainState.TrainEndsB.RadarEnable {
s := parseRadarSpeedData(info.Speed, trainState.TrainEndsB)
rv.SendMsg(message.NewRadarSender(s, forward, info.Displacement))
} else {
rv.SendMsg(message.NewRadarSender(0, forward, 0))
//slog.Error("列车行驶方向不确定或两端的雷达均未启用,发送雷达数据0", "列车前进=", trainState.VobcState.DirectionForward, "雷达启用A=", trainState.TrainEndsA.RadarEnable, "列车后退=", trainState.VobcState.DirectionBackward, "雷达启用B=", trainState.TrainEndsB.RadarEnable)
}
}
func parseRadarSpeedData(radarSpeed float32, trainEndState *common_proto.TrainEndsState) float32 {
//如果差值速度和速度输出都填写,那么就以速度输出为优先
//如果动力学速度-差值速度小于0呢么输出的就是0不能小于0
trainEndState.RadarCheckTime = int32(trainEndState.RadarCheckTimeOverAt - time.Now().Unix())
if trainEndState.RadarCheckTime <= 0 {
trainEndState.RadarCheckTime = 0
if trainEndState.RadarCheckTimeOverAt != 0 {
trainEndState.RadarCheckSpeedDiff = 0
trainEndState.RadarOutSpeed = 0
return radarSpeed
}
}
if trainEndState.RadarCheckSpeedDiff > 0 && trainEndState.RadarOutSpeed > 0 {
//如果雷达检测速度差值和速度输出都填写,那么就以速度输出为优先
return float32(trainEndState.RadarOutSpeed)
} else if trainEndState.RadarCheckSpeedDiff > 0 {
//如果雷达检测速度差值填写,那么就以速度差值为主
return float32(math.Abs(float64(radarSpeed - (trainEndState.RadarCheckSpeedDiff / 3.6))))
} else if trainEndState.RadarOutSpeed > 0 {
//如果雷达速度输出填写,那么就以速度输出为主
return float32(trainEndState.RadarOutSpeed) / 3.6
} else {
return radarSpeed
}
}
// sendRadarInfo 发送速度,位移计数 给vobc
2024-04-19 16:55:51 +08:00
/*
2024-01-25 09:57:17 +08:00
func (rv *radarVobc) sendRadarInfo(ctx context.Context) {
2024-01-26 17:57:35 +08:00
2024-04-19 16:55:51 +08:00
for {
select {
case <-ctx.Done():
return
default:
}
trainStatus := rv.radarVobcManager.FindRadarTrain()
if trainStatus != nil {
hourSpeed := float64(trainStatus.DynamicState.Speed / 100)
trainDift := trainStatus.DynamicState.Displacement
td := float64(trainDift / 1000)
s1 := uint16(math.Round(td / driftDefaultVal))
s2 := uint16(math.Round(td / 1000 / driftDefaultVal))
ri := message.RadarInfo{RealSpeed: uint16(math.Round(hourSpeed / fixed_speed)), DriftCounterS1: s1, DriftCounterS2: s2}
ri.State = &message.RadarState{Dir: message.IsTrue(trainStatus.RunDirection)}
rv.vobcClient.SendMsg(ri)
}
time.Sleep(time.Millisecond * radar_interval)
2024-01-24 17:12:34 +08:00
}
}
2024-04-19 16:55:51 +08:00
*/
2024-01-24 17:12:34 +08:00
func (rv *radarVobc) Stop() {
2024-01-25 09:57:17 +08:00
2024-04-19 16:55:51 +08:00
/* if rv.radarVobcTaskContext != nil {
2024-01-24 17:12:34 +08:00
rv.radarVobcTaskContext()
rv.radarVobcTaskContext = nil
2024-04-19 16:55:51 +08:00
}*/
2024-01-25 09:57:17 +08:00
if rv.vobcClient != nil {
rv.vobcClient.Close()
rv.vobcClient = nil
}
2024-01-24 17:12:34 +08:00
}