balise detect

This commit is contained in:
xzb 2023-11-29 17:28:33 +08:00
parent 7a75716196
commit 2ed1f5aaed
3 changed files with 45 additions and 20 deletions

View File

@ -53,15 +53,14 @@ type TrainBtm struct {
AboveBalise bool
//列车在运行方向顺序扫描到的应答器
//避免丢失
scannedBalises chan *TrainBaliseTelegram
//*TrainBaliseTelegram
scannedBalises *telegramQueue
//最近经过的应答器id
lastTelegram *TrainBaliseTelegram
}
const SB_LEN = 16
func NewTrainBtm() *TrainBtm {
return &TrainBtm{scannedBalises: make(chan *TrainBaliseTelegram, SB_LEN)}
return &TrainBtm{scannedBalises: &telegramQueue{}}
}
// 应答器计数器加1[0,255]
@ -80,12 +79,11 @@ func (t *TrainBtm) baliseMessageCounterAdd1() {
}
}
func (t *TrainBtm) FindScannedBalises() <-chan *TrainBaliseTelegram {
if len(t.scannedBalises) > 0 {
return t.scannedBalises
} else {
return nil
func (t *TrainBtm) FindScannedBalises() *TrainBaliseTelegram {
return t.scannedBalises.removeHead()
}
func (t *TrainBtm) ClearScannedBalises() {
t.scannedBalises.clear()
}
// Scanning BTM通过车载应答器天线接收到应答器报文
@ -94,12 +92,7 @@ func (t *TrainBtm) Scanning(aboveBalise bool, telegram *TrainBaliseTelegram) {
isNewTbt := telegram != nil && (t.lastTelegram == nil || t.lastTelegram.Up != telegram.Up || t.lastTelegram.BaliseId != telegram.BaliseId)
if isNewTbt {
t.lastTelegram = telegram
}
//
if isNewTbt {
if len(t.scannedBalises) < SB_LEN {
t.scannedBalises <- telegram
}
t.scannedBalises.addTail(telegram)
t.baliseMessageCounterAdd1()
}
//
@ -111,6 +104,36 @@ func (t *TrainBtm) Scanning(aboveBalise bool, telegram *TrainBaliseTelegram) {
}
}
type telegramQueue struct {
queue [3]*TrainBaliseTelegram
}
func (q *telegramQueue) moveHead() {
q.queue[0], q.queue[1], q.queue[2] = q.queue[1], q.queue[2], nil
}
func (q *telegramQueue) addTail(t *TrainBaliseTelegram) {
if q.queue[len(q.queue)-1] == nil {
q.queue[len(q.queue)-1] = t
} else {
q.moveHead()
q.queue[len(q.queue)-1] = t
}
}
func (q *telegramQueue) removeHead() *TrainBaliseTelegram {
for i, rt := range q.queue {
if rt != nil {
q.queue[i] = nil
return rt
}
}
return nil
}
func (q *telegramQueue) clear() {
for i, _ := range q.queue {
q.queue[i] = nil
}
}
var (
TrainPositionInfoType = ecs.NewComponentType[TrainPositionInfo]()
TrainBtmType = ecs.NewComponentType[TrainBtm]()

View File

@ -108,12 +108,12 @@ func TrainBalisePowerAmplifierSwitch(w ecs.World, turnOn bool) error {
}
// GetScannedBaliseTelegram 获取扫描到的应答器报文
func GetScannedBaliseTelegram(w ecs.World) (<-chan *component.TrainBaliseTelegram, error) {
result := <-ecs.Request[<-chan *component.TrainBaliseTelegram](w, func() ecs.Result[<-chan *component.TrainBaliseTelegram] {
func GetScannedBaliseTelegram(w ecs.World) (*component.TrainBaliseTelegram, error) {
result := <-ecs.Request[*component.TrainBaliseTelegram](w, func() ecs.Result[*component.TrainBaliseTelegram] {
//获取当前关联CANET设备的列车
canetTrain := findCanetTrain(w)
if canetTrain == nil {
return ecs.NewResult[<-chan *component.TrainBaliseTelegram](nil, fmt.Errorf("系统中当前不存在与CANET设备关联的列车实体"))
return ecs.NewResult[*component.TrainBaliseTelegram](nil, fmt.Errorf("系统中当前不存在与CANET设备关联的列车实体"))
}
//
train := component.TrainBtmType.Get(canetTrain)
@ -121,7 +121,7 @@ func GetScannedBaliseTelegram(w ecs.World) (<-chan *component.TrainBaliseTelegra
if tbt != nil {
return ecs.NewOkResult(tbt)
} else {
return ecs.NewResult[<-chan *component.TrainBaliseTelegram](nil, fmt.Errorf("此刻没有要发送的应答器报文"))
return ecs.NewResult[*component.TrainBaliseTelegram](nil, fmt.Errorf("此刻没有要发送的应答器报文"))
}
})
return result.Val, result.Err

View File

@ -51,6 +51,8 @@ func (s *BaliseDetectSystem) DetectBalise(w ecs.World, trainEntry *ecs.Entry, la
btm.Scanning(true, tbt)
}
}
} else {
btm.ClearScannedBalises()
}
}