1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- package test
- import (
- "cicv-data-closedloop/pjisuv_msgs"
- "fmt"
- "log"
- "os"
- "testing"
- "time"
- "github.com/bluenviron/goroslib/v2"
- "github.com/foxglove/go-rosbag"
- )
- func TestWriteRosBag(t *testing.T) {
- // create a node and connect to the master
- n, err := goroslib.NewNode(goroslib.NodeConf{
- Name: "goroslib_sub",
- MasterAddress: "127.0.0.1:11311",
- })
- if err != nil {
- panic(err)
- }
- defer n.Close()
- // create a subscriber
- sub, err := goroslib.NewSubscriber(goroslib.SubscriberConf{
- Node: n,
- Topic: "test_topic",
- Callback: func(msg *pjisuv_msgs.PerceptionLocalization) {
- log.Printf("Incoming: %+v\n", msg)
- },
- })
- if err != nil {
- panic(err)
- }
- defer sub.Close()
- // // wait for CTRL-C
- // c := make(chan os.Signal, 1)
- // signal.Notify(c, os.Interrupt)
- // <-c
- // ------------------------------------------------------------------------------------
- // 指定文件路径
- filePath := "C:\\Users\\mlxengingin\\Desktop\\1118\\my-bag.bag"
- // 以读写模式打开(或创建并清空)文件
- f, err := os.OpenFile(filePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
- if err != nil {
- // 打开(或创建并清空)文件时出错
- panic(fmt.Errorf("无法打开或创建文件: %v", err))
- }
- defer f.Close() // 确保文件在函数结束时被关闭
- // 后续的文件写入操作...
- t.Log("文件已成功打开(或创建并清空)。")
- writer, err := rosbag.NewWriter(f)
- if err != nil {
- panic(err)
- }
- err = writer.WriteConnection(&rosbag.Connection{
- Conn: 1,
- Topic: "/cicv_location",
- Data: rosbag.ConnectionHeader{
- Topic: "/cicv_location",
- Type: "perception_msgs/PerceptionLocalization",
- MD5Sum: "abc",
- MessageDefinition: []byte{0x01, 0x02, 0x03},
- //// optional fields
- //CallerID: &callerID,
- //Latching: &latching,
- },
- })
- if err != nil {
- panic(err)
- }
- for i := 0; i < 10000; i++ {
- nowInt := time.Now().Unix()
- data := pjisuv_msgs.PerceptionLocalization{}
- err = writer.WriteMessage(&rosbag.Message{
- Conn: 1,
- Time: uint64(nowInt),
- Data: []byte(data),
- })
- if err != nil {
- panic(err)
- }
- t.Log("当前时间", uint64(nowInt))
- t.Log("保存数据", data)
- }
- }
|