-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
150 lines (130 loc) · 3.89 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"flag"
"fmt"
"log"
"time"
"github.com/beevik/ntp"
"github.com/bluenviron/goroslib/v2"
"github.com/bluenviron/goroslib/v2/pkg/msgs/sensor_msgs"
"github.com/nats-io/nats.go"
)
func connect(host string) *nats.EncodedConn {
nc, err := nats.Connect(host, nats.Timeout(1*time.Minute))
if err != nil {
panic(err)
}
c, err := nats.NewEncodedConn(nc, nats.JSON_ENCODER)
if err != nil {
panic(err)
}
return c
}
var nodeName = flag.String("name", "", "Name of node, defaults to same name as type.")
var nodeType = flag.String("type", "", "Type of node, e.g. server")
var natsAddr = flag.String("host", "10.20.33.130", "URL to NATS server host.")
var ntpAddr = flag.String("ntp", "10.47.6.47", "URL to NTP server.")
var enableROS = flag.Bool("ros", false, "Enable ROS.")
func main() {
flag.Parse()
if len(*nodeName) == 0 {
*nodeName = *nodeType
}
fmt.Printf("Starting %s!\n", *nodeName)
natsClient := connect(*natsAddr)
ntpClient, err := ConnectNTP(*ntpAddr)
if err != nil {
log.Fatal(err)
}
var node *Node
if *nodeType == "coordinator" {
node = NewNode(*nodeName, natsClient, ntpClient, coordinator())
node.attr["paused"] = 0
} else if *nodeType == "sensor" {
node = NewNode(*nodeName, natsClient, ntpClient, func(node *Node) {
size, _ := node.attr["DATA_SIZE"]
seq, _ := node.attr["DATA_SEQ"]
data, err := RandomBytes(size)
if err != nil {
panic(err)
}
message := Packet{}
message.Header.Stamp = time.Now().UnixNano()
message.Header.Seq = int64(seq)
message.Data = data
message.Chk = Checksum(data, 0)
message.T1 = time.Now().UnixNano()
message.E1 = node.ntpClient.Resp.ClockOffset.Nanoseconds()
node.nc.Publish(fmt.Sprintf("%s.data", node.name), &message)
node.attr["DATA_SEQ"] += 1
})
node.attr["DATA_SIZE"] = 1000
node.attr["DATA_SEQ"] = 0
} else if *nodeType == "server" {
node = NewNode(*nodeName, natsClient, ntpClient, func(node *Node) {})
node.attr["COMPUTE_TIME"] = 0
node.nc.Subscribe("sensor.data", func(p *Packet) {
p.T2 = time.Now().UnixNano()
p.E2 = node.ntpClient.GetOffset()
if c_time, ok := node.attr["COMPUTE_TIME"]; ok {
time.Sleep(time.Duration(c_time * 1e6))
}
p.T3 = time.Now().UnixNano()
p.E3 = node.ntpClient.GetOffset()
node.nc.Publish(fmt.Sprintf("%s.data", node.name), &p)
})
} else if *nodeType == "vehicle" {
var state VehicleState
var gps sensor_msgs.NavSatFix
if *enableROS {
n, err := goroslib.NewNode(goroslib.NodeConf{
Name: "goroslib_sub",
MasterAddress: "localhost:11311",
})
if err != nil {
panic(err)
}
defer n.Close()
subState, err := goroslib.NewSubscriber(goroslib.SubscriberConf{
Node: n,
Topic: "state",
Callback: func(msg *VehicleState) { state = *msg },
})
if err != nil {
panic(err)
}
defer subState.Close()
subGps, err := goroslib.NewSubscriber(goroslib.SubscriberConf{
Node: n,
Topic: "gps/filtered",
Callback: func(msg *sensor_msgs.NavSatFix) { gps = *msg },
})
if err != nil {
panic(err)
}
defer subGps.Close()
}
node = NewNode(*nodeName, natsClient, ntpClient, func(node *Node) {})
node.nc.Subscribe("server.data", func(p *Packet) {
p.Header.FrameID = node.name
p.T4 = time.Now().UnixNano()
p.E4 = node.ntpClient.GetOffset()
p.X = state.X
p.Y = state.Y
p.Yaw = state.Yaw
p.V = state.V
p.Latitude = gps.Latitude
p.Longitude = gps.Longitude
p.Chk = Checksum(p.Data, p.Chk) // NOTE: After this, if chk == 0 then it's good. The message was not corrupted.
p.Data = []byte{} // NOTE: We empty it so all data isn't stored. Use for something else? Maybe time sync error?
node.logs = append(node.logs, *p)
})
} else {
log.Fatalf("Unsupported node type \"%s\".", *nodeType)
}
go node.ntpClient.QueryLoop(func(r ntp.Response) bool {
val, _ := node.attr["alive"]
return val != 0
})
node.run()
}