rts-sim-testing-service/third_party/acc/acc_vobc.go

133 lines
3.6 KiB
Go
Raw Normal View History

2024-01-26 17:57:35 +08:00
package acc
import (
"context"
2024-05-29 11:48:04 +08:00
"encoding/hex"
2024-01-26 17:57:35 +08:00
"fmt"
"joylink.club/bj-rtsts-server/config"
2024-04-28 11:01:12 +08:00
"joylink.club/bj-rtsts-server/dto/common_proto"
2024-01-26 17:57:35 +08:00
"joylink.club/bj-rtsts-server/dto/state_proto"
"joylink.club/bj-rtsts-server/third_party/message"
"joylink.club/bj-rtsts-server/third_party/udp"
2024-05-29 11:48:04 +08:00
"log/slog"
2024-04-28 11:01:12 +08:00
"math"
2024-01-26 17:57:35 +08:00
"sync"
2024-04-28 11:01:12 +08:00
"time"
2024-01-26 17:57:35 +08:00
)
type AccVobc interface {
Start(accManager AccVobcManager)
Stop()
2024-04-19 16:55:51 +08:00
SendAcc(acc *message.Accelerometer)
2024-04-28 11:01:12 +08:00
TrainAccSender(info *message.DynamicsTrainInfo, trainState *state_proto.TrainState)
2024-01-26 17:57:35 +08:00
}
type AccVobcManager interface {
GetRunAccConfig() config.AccConfig
FindAccTrain() *state_proto.TrainState
}
var (
initLock = &sync.Mutex{}
singleObj *accVobcService
)
func Default() AccVobc {
defer initLock.Unlock()
initLock.Lock()
2024-01-26 17:57:35 +08:00
if singleObj == nil {
singleObj = &accVobcService{}
}
return singleObj
}
type accVobcService struct {
controlContext context.CancelFunc
vobcClient udp.UdpClient
radarVobcManager AccVobcManager
}
func (acc *accVobcService) Start(accManager AccVobcManager) {
config := accManager.GetRunAccConfig()
if config.RemoteIp == "" || config.RemotePort <= 0 || !config.Open {
2024-06-12 17:45:47 +08:00
return
2024-01-26 17:57:35 +08:00
}
acc.vobcClient = udp.NewClient(fmt.Sprintf("%v:%v", config.RemoteIp, config.RemotePort))
acc.radarVobcManager = accManager
2024-04-19 16:55:51 +08:00
}
func (avs *accVobcService) SendAcc(acc *message.Accelerometer) {
2024-05-29 11:48:04 +08:00
if avs.vobcClient != nil {
data := acc.Encode()
hexStr := hex.EncodeToString(data)
slog.Info("发送 加速度消息数据:", hexStr)
err := avs.vobcClient.Send(data)
if err != nil {
slog.Info("发送 加速度消息数据失败:", hexStr)
return
}
}
//avs.vobcClient.Send(acc.BuildCanData())
2024-01-26 17:57:35 +08:00
}
2024-04-28 11:01:12 +08:00
func (avs *accVobcService) TrainAccSender(info *message.DynamicsTrainInfo, trainState *state_proto.TrainState) {
if trainState.VobcState.Tc1Active && trainState.TrainEndsA.AccEnable {
s := parseAccSpeedData(info.Acceleration, trainState.TrainEndsA)
avs.SendAcc(&message.Accelerometer{Acc: s})
} else if trainState.VobcState.Tc2Active && trainState.TrainEndsB.AccEnable {
s := parseAccSpeedData(info.Acceleration, trainState.TrainEndsB)
avs.SendAcc(&message.Accelerometer{Acc: s})
} else {
avs.SendAcc(&message.Accelerometer{Acc: 0})
}
}
func parseAccSpeedData(accSpeed float32, trainEndState *common_proto.TrainEndsState) float32 {
//如果差值速度和速度输出都填写,那么就以速度输出为优先
//如果动力学速度-差值速度小于0呢么输出的就是0不能小于0
trainEndState.AccCheckTime = int32(trainEndState.AccCheckTimeOverAt - time.Now().Unix())
if trainEndState.AccCheckTime <= 0 {
//判断雷达检测时间是否到期
trainEndState.AccCheckTime = 0
if trainEndState.AccCheckTimeOverAt != 0 {
trainEndState.AccCheckSpeedDiff = 0
return accSpeed
}
}
if trainEndState.AccCheckSpeedDiff > 0 {
//如果雷达检测速度差值填写,那么就以速度差值为主
return float32(math.Abs(float64(accSpeed - trainEndState.AccCheckSpeedDiff)))
} else {
return accSpeed
}
}
2024-01-26 17:57:35 +08:00
2024-04-19 16:55:51 +08:00
/*func (acc *accVobcService) sendTask(ctx context.Context) {
2024-01-26 17:57:35 +08:00
for {
select {
case <-ctx.Done():
return
default:
}
trainStatus := acc.radarVobcManager.FindAccTrain()
if trainStatus != nil {
2024-04-09 08:55:33 +08:00
// 发送加速度信息
2024-01-26 17:57:35 +08:00
speedAcc := trainStatus.DynamicState.Acceleration
t := speedAcc / accSpeedUnit
acc.vobcClient.SendMsg(&message.Accelerometer{Acc: math.Float32bits(t)})
}
time.Sleep(time.Millisecond * accInterval)
}
2024-04-19 16:55:51 +08:00
}*/
2024-01-26 17:57:35 +08:00
func (acc *accVobcService) Stop() {
if acc.controlContext != nil {
acc.controlContext()
acc.controlContext = nil
}
if acc.vobcClient != nil {
acc.vobcClient.Close()
acc.vobcClient = nil
}
}