rts-sim-testing-service/bin/acc_conn-example.go

153 lines
4.2 KiB
Go
Raw Normal View History

2024-05-20 15:22:58 +08:00
package main
import (
"encoding/hex"
2024-05-20 15:22:58 +08:00
"fmt"
"github.com/spf13/viper"
"joylink.club/bj-rtsts-server/bin/config"
"joylink.club/bj-rtsts-server/third_party/message"
"joylink.club/bj-rtsts-server/third_party/udp"
"log/slog"
"sort"
2024-05-20 15:22:58 +08:00
"time"
)
const config_name = "example"
var exampleConfig config.ExampleConfig
func initConfig() {
cnf := viper.New()
cnf.SetConfigName(config_name)
cnf.SetConfigType("yml")
cnf.AddConfigPath("./bin/config/")
//cnf.AddConfigPath("./config/")
2024-05-20 15:22:58 +08:00
cnf.AddConfigPath(".")
err := cnf.ReadInConfig()
if err != nil {
panic(fmt.Errorf("读取配置文件错误: %w", err))
}
err = cnf.Unmarshal(&exampleConfig)
if err != nil {
panic(fmt.Errorf("解析配置文件错误: %w", err))
}
slog.Info("成功加载配置", "config", exampleConfig)
}
2024-05-20 15:22:58 +08:00
func main() {
2024-05-20 15:22:58 +08:00
initConfig()
accClient := udp.NewClient(fmt.Sprintf("%v:%v", exampleConfig.Acc.RemoteIp, exampleConfig.Acc.RemotePort))
go testAcc(accClient, 0.54)
//btm server 接受数据测试
ser := udp.NewServer(fmt.Sprintf("192.168.1.150:%d", 4646), handleCanetFrames)
ser.Listen()
fmt.Println(ser, "===========")
//speedClient := udp.NewClient(fmt.Sprintf("%v:%v", exampleConfig.Speed.RemoteIp, exampleConfig.Speed.RemotePort))
//go testSpeed(speedClient, 30)
select {}
2024-05-20 15:22:58 +08:00
}
func handleCanetFrames(cfs []byte) {
2024-05-20 15:22:58 +08:00
defer func() {
if e := recover(); e != nil {
slog.Debug(fmt.Sprintf("handleCanetFrames异常[%s]", e))
2024-05-20 15:22:58 +08:00
}
}()
//一个cannet 帧 13字节
if len(cfs) > 0 && len(cfs)%13 == 0 {
cfSum := len(cfs) / 13
dms := make([]*message.CanetFrame, 0, 16) //13个应答器报文数据帧+TimeA帧+TimeB帧+结束帧
for cfi := 0; cfi < cfSum; cfi++ {
cfStart := cfi * 13
cf := message.NewCanetFrame(cfs[cfStart:cfStart+13], false)
//
switch cf.CanFrameType() {
case message.CfReq:
fmt.Println(message.CfReq)
case message.CfStatusRsp:
fmt.Println(message.CfStatusRsp)
case message.CfTimeSync:
fmt.Println(message.CfTimeSync)
case message.CfMsg:
fallthrough
case message.CfMsgTimeA:
fallthrough
case message.CfMsgTimeB:
fallthrough
case message.CfMsgEnd:
dms = append(dms, cf)
default:
slog.Warn("CanetFrame帧没有具体对应的应用帧", "CannetFrame", cf.String())
} //switch
} //for
//将数据包按ID3即0x80+offset由小到大排序
sort.SliceStable(dms, func(i, j int) bool {
return dms[i].CanId.ID3 < dms[j].CanId.ID3
})
//有数据帧但是不足16帧
if len(dms) > 0 {
if len(dms) != 16 {
slog.Warn("接收到数据帧但数据帧数量不足16帧")
} else {
slog.Warn("接收到数据帧但数据帧数量不足16帧111111")
}
}
} else {
slog.Warn("从cannet接收数据未满足条件len(cfs) > 0 && len(cfs)%13 == 0", "len(cfs)", len(cfs), "16:", hex.EncodeToString(cfs))
}
}
func recive() {
fmt.Println("11111111111111111")
_ = udp.NewServer(fmt.Sprintf(":%d", 4196), handleCanetFrames)
}
func testSpeed(client udp.UdpClient, speed float32) {
//client = udp.NewClient(fmt.Sprintf("%v:%v", exampleConfig.Speed.RemoteIp, exampleConfig.Speed.RemotePort))
ac := message.ElectricMachinery{Speed: speed, WheelDiameter: 800, IsBack: false}
2024-05-20 15:22:58 +08:00
var index int64
for {
data := ac.Encode()
//fmt.Println(hex.EncodeToString(data))
err := client.Send(data)
2024-05-20 15:22:58 +08:00
index++
if index%10 == 0 {
slog.Info(fmt.Sprintf("发送speed数据,时间戳:%v,发送次数:%v 数据:%v", time.Now().Unix(), index, hex.EncodeToString(data)))
ac = message.ElectricMachinery{Speed: 0, WheelDiameter: 800, IsBack: false}
client.Send(ac.Encode())
break
2024-05-20 15:22:58 +08:00
}
if err != nil {
slog.Error("发送数据失败", "err", err)
}
time.Sleep(time.Millisecond * 200)
}
}
// 加速度计
func testAcc(client udp.UdpClient, acc float32) {
//client = udp.NewClient(fmt.Sprintf("%v:%v", exampleConfig.Acc.RemoteIp, exampleConfig.Acc.RemotePort))
ac := message.Accelerometer{Acc: acc}
var index int64
for {
data := ac.Encode()
//fmt.Println(hex.EncodeToString(data))
err := client.Send(data)
index++
if index%10 == 0 {
slog.Info(fmt.Sprintf("发送acc数据,时间戳:%v,发送次数:%v 数据:%v", time.Now().Unix(), index, hex.EncodeToString(data)))
}
if err != nil {
slog.Error("发送数据失败", "err", err)
}
time.Sleep(time.Millisecond * 500)
}
2024-05-20 15:22:58 +08:00
}