-
Notifications
You must be signed in to change notification settings - Fork 62
/
main.go
65 lines (56 loc) · 1.13 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"log"
"os"
"os/signal"
"time"
"github.com/bluenviron/goroslib/v2"
"github.com/bluenviron/goroslib/v2/pkg/msg"
)
// define a custom message.
// unlike the standard library, a .msg file is not needed.
// a structure definition is enough.
type TestMessage struct {
msg.Package `ros:"my_package"`
FirstField uint32
SecondField string
}
func main() {
// create a node and connect to the master
n, err := goroslib.NewNode(goroslib.NodeConf{
Name: "goroslib_pub",
MasterAddress: "127.0.0.1:11311",
})
if err != nil {
panic(err)
}
defer n.Close()
// create a publisher
pub, err := goroslib.NewPublisher(goroslib.PublisherConf{
Node: n,
Topic: "test_topic",
Msg: &TestMessage{},
})
if err != nil {
panic(err)
}
defer pub.Close()
r := n.TimeRate(1 * time.Second)
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
for {
select {
// publish a message every second
case <-r.SleepChan():
msg := &TestMessage{
FirstField: 3,
SecondField: "test message",
}
log.Printf("Outgoing: %+v\n", msg)
pub.Write(msg)
// handle CTRL-C
case <-c:
return
}
}
}