forked from ZebraDevs/rosgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
42 lines (36 loc) · 1.01 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
package main
//go:generate gengo msg std_msgs/String
import (
"fmt"
"os"
"std_msgs"
"time"
"github.com/fetchrobotics/rosgo/ros"
)
func main() {
node, err := ros.NewNode("/talker", os.Args)
if err != nil {
fmt.Println(err)
os.Exit(-1)
}
defer node.Shutdown()
node.Logger().SetSeverity(ros.LogLevelDebug)
pub := node.NewPublisherWithCallbacks("/chatter", std_msgs.MsgString, onConnect, onDisconnect)
for node.OK() {
node.SpinOnce()
var msg std_msgs.String
msg.Data = fmt.Sprintf("hello %s", time.Now().String())
fmt.Println(msg.Data)
pub.Publish(&msg)
time.Sleep(time.Second)
}
}
func onConnect(pub ros.SingleSubscriberPublisher) {
fmt.Printf("-------Connect callback: node %s topic %s\n", pub.GetSubscriberName(), pub.GetTopic())
var msg std_msgs.String
msg.Data = fmt.Sprintf("hello %s", pub.GetSubscriberName())
pub.Publish(&msg)
}
func onDisconnect(pub ros.SingleSubscriberPublisher) {
fmt.Printf("-------Disconnect callback: node %s topic %s\n", pub.GetSubscriberName(), pub.GetTopic())
}