forked from otamajakusi/roslibgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
97 lines (84 loc) · 2.22 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package main
import (
"encoding/json"
"fmt"
"github.com/gaochuwuhan/roslibgo"
//"github.com/pkg/profile"
"sync"
"time"
)
type StdMsgs_String struct {
Data string `json:"data"`
}
// std_srvs/SetBool
type StdSrvs_SetBoolRequest struct {
Data bool `json:"data"`
}
type StdSrvs_SetBoolResponse struct {
Success bool `json:"success"`
Message string `json:"message"`
}
func callbackA(data json.RawMessage) {
var msg StdMsgs_String
json.Unmarshal(data, &msg)
fmt.Println("a:", msg.Data)
}
func callbackB(data json.RawMessage) {
var msg StdMsgs_String
json.Unmarshal(data, &msg)
fmt.Println("b:", msg.Data)
}
func serviceCallback(request json.RawMessage) (bool, json.RawMessage) {
var req StdSrvs_SetBoolRequest
json.Unmarshal(request, &req)
res := StdSrvs_SetBoolResponse{Success: true, Message: "I'm OK"}
raw, _ := json.Marshal(res)
return true, raw
}
func main() {
//defer profile.Start(profile.MemProfile, profile.ProfilePath(".")).Stop()
var wg sync.WaitGroup
ros, _ := roslibgo.NewRos("ws://192.168.90.2:9090")
ros.Run()
// srv := roslibgo.NewService(ros, "/x", "std_srvs/SetBool")
// call := roslibgo.NewService(ros, "/x", "std_srvs/SetBool")
// srv.Advertise(serviceCallback)
// data, _ := json.Marshal(StdSrvs_SetBoolRequest{Data: true})
// callLoop := func() {
// for i := 0; i < 10; i++ {
// resp, err := call.Call(json.RawMessage(data))
// if err != nil {
// fmt.Println("error:", err)
// break
// }
// fmt.Println("callback:", string(resp))
// time.Sleep(10 * time.Millisecond)
// }
// }
// go callLoop()
suba := roslibgo.NewTopic(ros, "/a", "std_msgs/String")
suba.Subscribe(callbackA)
pubLoop := func(name string, str string) func() {
pub := roslibgo.NewTopic(ros, name, "std_msgs/String")
return func() {
for i := 0; ; i++ {
//fmt.Printf("--- %s: %d\n", str, i)
s, _ := json.Marshal(StdMsgs_String{Data: fmt.Sprintf("%s: %d", str, i)})
pub.Publish(json.RawMessage(s))
//_, _ = pub, s
time.Sleep(1 * time.Second)
}
wg.Done()
}
}
a := pubLoop("/a", "hoge")
b := pubLoop("/b", "fuga")
wg.Add(1)
go a()
time.Sleep(5 * time.Second)
subb := roslibgo.NewTopic(ros, "/b", "std_msgs/String")
subb.Subscribe(callbackB)
wg.Add(1)
go b()
wg.Wait()
}