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

209 lines
5.3 KiB
Go
Raw Normal View History

2024-01-22 15:08:46 +08:00
package message
import (
"bytes"
"encoding/binary"
"fmt"
2024-04-19 16:55:51 +08:00
"math"
2024-01-22 15:08:46 +08:00
)
const (
radar_head1 byte = 0x0C
radar_head2 byte = 0xFC
)
2024-04-19 16:55:51 +08:00
var autoInc byte = 0
var (
fixed_speed = 0.009155
driftDefaultVal = 0.1
)
2024-01-24 17:12:34 +08:00
2024-01-22 16:35:50 +08:00
// 雷达与VOBC接口-雷达通讯协议
2024-01-24 17:12:34 +08:00
type RadarInfo struct {
AutoInc byte //自增计数器每发送一次自增1.范围0-256
RealSpeed uint16 //实际速度
DriftCounterS1 uint16 //位移计数器S1
DriftCounterS2 uint16 //位移计数器S2
InnerCheck1 byte //内部使用,我们只有在协议效验时用到该两个字节
InnerCheck2 byte //内部使用,我们只有在协议效验时用到该两个字节
2024-01-22 15:08:46 +08:00
State *RadarState
Tail byte
}
2024-01-24 17:12:34 +08:00
2024-04-19 16:55:51 +08:00
func NewRadarSender(speed float32, forward bool, displacement uint16) *RadarInfo {
state := &RadarState{SwModel1: 1, SwModel0: 1, calculateBit: 1, signalQualityBit: 1, BlackoutBit: 0, DirectionState: 1, Direction: IsTrue(forward)}
ri := &RadarInfo{State: state}
ri.RealSpeed = uint16(math.Abs(float64(speed*3.6) / fixed_speed))
2024-04-28 11:01:12 +08:00
disMeter := float64(displacement / 100)
disMeterKM := float64(disMeter / 100)
if disMeter > 9999 {
2024-04-19 16:55:51 +08:00
ri.DriftCounterS1 = 9999
} else {
2024-04-28 11:01:12 +08:00
ri.DriftCounterS1 = uint16(disMeter)
2024-04-19 16:55:51 +08:00
}
2024-04-28 11:01:12 +08:00
if disMeterKM > 9999 {
2024-04-19 16:55:51 +08:00
ri.DriftCounterS2 = 9999
} else {
2024-04-28 11:01:12 +08:00
ri.DriftCounterS2 = uint16(disMeterKM)
2024-04-19 16:55:51 +08:00
}
return ri
}
2024-01-22 15:08:46 +08:00
type RadarState struct {
2024-04-19 16:55:51 +08:00
SwModel1 byte
SwModel0 byte
calculateBit byte //计算状态位
signalQualityBit byte //信号质量标志位
BlackoutBit byte //Blackout标志位
innerBit byte //内部使用
DirectionState byte //方向状态 1行驶方向有效 0行驶方向无效
Direction byte //行驶方向 1前向 0反向
}
func (r *RadarState) Encode() byte {
var state byte
state = setBit(state, 7, r.SwModel1)
state = setBit(state, 6, r.SwModel0)
state = setBit(state, 5, r.calculateBit)
state = setBit(state, 4, r.signalQualityBit)
state = setBit(state, 3, r.BlackoutBit)
state = setBit(state, 1, r.DirectionState)
state = setBit(state, 0, r.Direction)
return state
2024-01-22 15:08:46 +08:00
}
2024-01-24 17:12:34 +08:00
func (r RadarInfo) Encode() []byte {
buf := make([]byte, 0)
buf = append(buf, radar_head1)
buf = append(buf, radar_head2)
2024-04-19 16:55:51 +08:00
autoInc = autoInc + 1
buf = append(buf, autoInc)
2024-04-11 08:55:12 +08:00
2024-01-24 17:12:34 +08:00
buf = binary.LittleEndian.AppendUint16(buf, r.RealSpeed)
buf = binary.LittleEndian.AppendUint16(buf, r.DriftCounterS1)
buf = binary.LittleEndian.AppendUint16(buf, r.DriftCounterS2)
2024-04-19 16:55:51 +08:00
2024-01-24 17:12:34 +08:00
buf = append(buf, 0)
buf = append(buf, 0)
2024-04-19 16:55:51 +08:00
buf = append(buf, r.State.Encode())
2024-04-11 08:55:12 +08:00
var sum = 0
2024-01-24 17:12:34 +08:00
for _, d := range buf {
sum += int(d)
}
buf = append(buf, byte(^sum+1))
return buf
}
func (r *RadarInfo) Decode(data []byte) error {
2024-01-22 15:08:46 +08:00
if len(data) < 13 {
return fmt.Errorf("雷达数据预读取失败需要读取13字节可读取:%v", len(data))
}
buf := bytes.NewBuffer(data)
_, _, err := readHeader(buf)
if err != nil {
return err
}
autoInc := readByteData(buf)
speed := readSpeedOrCounter(buf)
s1 := readSpeedOrCounter(buf)
s2 := readSpeedOrCounter(buf)
i1, i2 := readRadarInnerData(buf)
state := readRadarState(buf)
tail := readByteData(buf)
r.AutoInc = autoInc
r.RealSpeed = speed
r.DriftCounterS1 = s1
r.DriftCounterS2 = s2
r.InnerCheck1 = i1
r.InnerCheck2 = i2
r.State = state
r.Tail = tail
2024-01-24 17:12:34 +08:00
if !(r.Tail == r.createTail()) {
2024-01-22 15:08:46 +08:00
return fmt.Errorf("数据解析完成,但协议效验不通过")
}
2024-04-19 16:55:51 +08:00
2024-01-22 15:08:46 +08:00
return nil
}
2024-04-19 16:55:51 +08:00
func (s *RadarState) Decode(d byte) {
s.SwModel1 = GetBit(d, 7)
s.SwModel0 = GetBit(d, 6)
s.calculateBit = GetBit(d, 5)
s.signalQualityBit = GetBit(d, 4)
s.BlackoutBit = GetBit(d, 3)
s.DirectionState = GetBit(d, 1)
s.Direction = GetBit(d, 0)
2024-01-22 15:08:46 +08:00
}
2024-01-24 17:12:34 +08:00
func culDataSize(d uint16) int {
return int(d>>8) + int(d&0x00FF)
}
func (r *RadarInfo) createTail() byte {
var sum = int(radar_head1) + int(radar_head2)
sum += int(r.AutoInc)
sum += culDataSize(r.RealSpeed)
sum += culDataSize(r.DriftCounterS1)
sum += culDataSize(r.DriftCounterS2)
sum += int(r.InnerCheck1)
sum += int(r.InnerCheck2)
2024-04-19 16:55:51 +08:00
sum += int(r.State.Encode())
2024-01-24 17:12:34 +08:00
return byte(^sum + 1)
2024-01-22 15:08:46 +08:00
}
2024-01-24 17:12:34 +08:00
2024-01-22 15:08:46 +08:00
func readHeader(buf *bytes.Buffer) (byte, byte, error) {
/*if buf.Len() < 2 {
return 0, 0, fmt.Errorf("雷达协议解析头部没有可读充足的数据")
}*/
d1, _ := buf.ReadByte()
d2, _ := buf.ReadByte()
2024-01-22 16:35:50 +08:00
if d1 == radar_head1 && d2 == radar_head2 {
return d1, d2, nil
2024-01-22 15:08:46 +08:00
}
2024-01-22 16:35:50 +08:00
return 0, 0, fmt.Errorf("雷达协议解析头部未找到对应的头部帧")
2024-01-22 15:08:46 +08:00
}
func readByteData(buf *bytes.Buffer) byte {
/* if buf.Len() < 1 {
return 0, fmt.Errorf("")
}*/
d, _ := buf.ReadByte()
return d
}
2024-01-24 17:12:34 +08:00
func readSpeedOrCounter(buf *bytes.Buffer) uint16 {
2024-01-22 15:08:46 +08:00
ss, _ := buf.ReadByte()
limit, _ := buf.ReadByte()
2024-01-24 17:12:34 +08:00
data := binary.LittleEndian.Uint16([]byte{ss, limit})
return data
//return &RadarData{SourceData: ss, valRange: limit, data: data}
2024-01-22 15:08:46 +08:00
}
func readRadarInnerData(buf *bytes.Buffer) (byte, byte) {
/*if buf.Len() < 2 {
return 0, 0, fmt.Errorf("")
}*/
i1, _ := buf.ReadByte()
i2, _ := buf.ReadByte()
return i1, i2
}
func readRadarState(buf *bytes.Buffer) *RadarState {
/*if buf.Len() < 1 {
return nil, fmt.Errorf("")
}*/
state, _ := buf.ReadByte()
2024-04-19 16:55:51 +08:00
s := &RadarState{}
s.Decode(state)
return s
2024-01-22 15:08:46 +08:00
}
func driftCounter(buf *bytes.Buffer) (uint16, error) {
if buf.Len() < 2 {
return 0, fmt.Errorf("")
}
var driftCounter uint16
binary.Read(buf, binary.LittleEndian, &driftCounter)
return driftCounter, nil
}