|
@@ -1,26 +1,61 @@
|
|
|
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) {
|
|
|
|
|
|
- // Check if the file exists, if not create it
|
|
|
- f, err := os.OpenFile("my-bag.bag", os.O_RDWR|os.O_CREATE, 0644)
|
|
|
+ // 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 func(f *os.File) {
|
|
|
- err := f.Close()
|
|
|
- if err != nil {
|
|
|
- fmt.Printf("Error closing file: %s\n", err)
|
|
|
- }
|
|
|
- }(f)
|
|
|
+ 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 {
|
|
@@ -29,10 +64,10 @@ func TestWriteRosBag(t *testing.T) {
|
|
|
|
|
|
err = writer.WriteConnection(&rosbag.Connection{
|
|
|
Conn: 1,
|
|
|
- Topic: "/foo",
|
|
|
+ Topic: "/cicv_location",
|
|
|
Data: rosbag.ConnectionHeader{
|
|
|
- Topic: "/foo",
|
|
|
- Type: "Foo",
|
|
|
+ Topic: "/cicv_location",
|
|
|
+ Type: "perception_msgs/PerceptionLocalization",
|
|
|
MD5Sum: "abc",
|
|
|
MessageDefinition: []byte{0x01, 0x02, 0x03},
|
|
|
|
|
@@ -45,17 +80,19 @@ func TestWriteRosBag(t *testing.T) {
|
|
|
panic(err)
|
|
|
}
|
|
|
|
|
|
- for i := 0; i < 100; i++ {
|
|
|
- data := "hello"
|
|
|
+ for i := 0; i < 10000; i++ {
|
|
|
+ nowInt := time.Now().Unix()
|
|
|
+ data := pjisuv_msgs.PerceptionLocalization{}
|
|
|
err = writer.WriteMessage(&rosbag.Message{
|
|
|
- Conn: 0,
|
|
|
- Time: uint64(i),
|
|
|
+ Conn: 1,
|
|
|
+ Time: uint64(nowInt),
|
|
|
Data: []byte(data),
|
|
|
})
|
|
|
if err != nil {
|
|
|
panic(err)
|
|
|
}
|
|
|
- fmt.Println("保存数据", data)
|
|
|
+ t.Log("当前时间", uint64(nowInt))
|
|
|
+ t.Log("保存数据", data)
|
|
|
}
|
|
|
|
|
|
}
|