forked from bluenviron/gomavlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.go
360 lines (303 loc) · 9.17 KB
/
node.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
Package gomavlib is a library that implements Mavlink 2.0 and 1.0 in the Go
programming language. It can power UGVs, UAVs, ground stations, monitoring
systems or routers acting in a Mavlink network.
Mavlink is a lighweight and transport-independent protocol that is mostly used
to communicate with unmanned ground vehicles (UGV) and unmanned aerial vehicles
(UAV, drones, quadcopters, multirotors). It is supported by the most common
open-source flight controllers (Ardupilot and PX4).
Basic example (more are available at https://github.com/team-rocos/gomavlib/tree/master/examples):
package main
import (
"fmt"
"github.com/team-rocos/gomavlib"
"github.com/team-rocos/gomavlib/dialects/ardupilotmega"
)
func main() {
node, err := gomavlib.NewNode(gomavlib.NodeConf{
Endpoints: []gomavlib.EndpointConf{
gomavlib.EndpointSerial{"/dev/ttyUSB0:57600"},
},
Dialect: ardupilotmega.Dialect,
OutVersion: gomavlib.V2,
OutSystemId: 10,
})
if err != nil {
panic(err)
}
defer node.Close()
for evt := range node.Events() {
if frm,ok := evt.(*gomavlib.EventFrame); ok {
fmt.Printf("received: id=%d, %+v\n", frm.Message().GetId(), frm.Message())
}
}
}
*/
package gomavlib
import (
"fmt"
"sync"
"time"
)
const (
// constant for ip-based endpoints
_NET_BUFFER_SIZE = 512 // frames cannot go beyond len(header) + 255 + len(check) + len(sig)
_NET_CONNECT_TIMEOUT = 10 * time.Second
_NET_RECONNECT_PERIOD = 2 * time.Second
_NET_READ_TIMEOUT = 60 * time.Second
_NET_WRITE_TIMEOUT = 10 * time.Second
)
type goroutinePool sync.WaitGroup
type goroutinePoolRunnable interface {
run()
}
func (wg *goroutinePool) Start(what goroutinePoolRunnable) {
(*sync.WaitGroup)(wg).Add(1)
go func() {
defer (*sync.WaitGroup)(wg).Done()
what.run()
}()
}
func (wg *goroutinePool) Wait() {
(*sync.WaitGroup)(wg).Wait()
}
// NodeConf allows to configure a Node.
type NodeConf struct {
// the endpoints with which this node will
// communicate. Each endpoint contains zero or more channels
Endpoints []EndpointConf
// (optional) the dialect which contains the messages that will be encoded and decoded.
// If not provided, messages are decoded in the MessageRaw struct.
D Dialect
// (optional) the secret key used to validate incoming frames.
// Non signed frames are discarded, as well as frames with a version < 2.0.
InKey *Key
// Mavlink version used to encode messages. See Version
// for the available options.
OutVersion Version
// the system id, added to every outgoing frame and used to identify this
// node in the network.
OutSystemId byte
// (optional) the component id, added to every outgoing frame, defaults to 1.
OutComponentId byte
// (optional) the secret key used to sign outgoing frames.
// This feature requires a version >= 2.0.
OutKey *Key
// (optional) disables the periodic sending of heartbeats to open channels.
HeartbeatDisable bool
// (optional) the period between heartbeats. It defaults to 5 seconds.
HeartbeatPeriod time.Duration
// (optional) the system type advertised by heartbeats.
// It defaults to MAV_TYPE_GCS
HeartbeatSystemType int
// (optional) the autopilot type advertised by heartbeats.
// It defaults to MAV_AUTOPILOT_GENERIC
HeartbeatAutopilotType int
// (optional) automatically request streams to detected Ardupilot devices,
// that need an explicit request in order to emit telemetry stream.
StreamRequestEnable bool
// (optional) the requested stream frequency in Hz. It defaults to 4.
StreamRequestFrequency int
}
// Node is a high-level Mavlink encoder and decoder that works with endpoints.
type Node struct {
conf NodeConf
eventsOut chan Event
eventsIn chan eventIn
pool goroutinePool
channelAccepters map[*channelAccepter]struct{}
channels map[*Channel]struct{}
nodeHeartbeat *nodeHeartbeat
nodeStreamRequest *nodeStreamRequest
}
// NewNode allocates a Node. See NodeConf for the options.
func NewNode(conf NodeConf) (*Node, error) {
if len(conf.Endpoints) == 0 {
return nil, fmt.Errorf("at least one endpoint must be provided")
}
if conf.HeartbeatPeriod == 0 {
conf.HeartbeatPeriod = 5 * time.Second
}
if conf.HeartbeatSystemType == 0 {
conf.HeartbeatSystemType = 6 // MAV_TYPE_GCS
}
if conf.HeartbeatAutopilotType == 0 {
conf.HeartbeatAutopilotType = 0 // MAV_AUTOPILOT_GENERIC
}
if conf.StreamRequestFrequency == 0 {
conf.StreamRequestFrequency = 4
}
// check Parser configuration here, since Parser is created dynamically
if conf.OutVersion == 0 {
return nil, fmt.Errorf("OutVersion not provided")
}
if conf.OutSystemId < 1 {
return nil, fmt.Errorf("SystemId must be >= 1")
}
if conf.OutComponentId < 1 {
conf.OutComponentId = 1
}
if conf.OutKey != nil && conf.OutVersion != V2 {
return nil, fmt.Errorf("OutKey requires V2 frames")
}
n := &Node{
conf: conf,
// these can be unbuffered as long as eventsIn's goroutine
// does not write to eventsOut
eventsOut: make(chan Event),
eventsIn: make(chan eventIn),
channelAccepters: make(map[*channelAccepter]struct{}),
channels: make(map[*Channel]struct{}),
}
closeExisting := func() {
for ca := range n.channels {
ca.rwc.Close()
}
for ca := range n.channelAccepters {
ca.eca.Close()
}
}
// endpoints
for _, tconf := range conf.Endpoints {
tp, err := tconf.init()
if err != nil {
closeExisting()
return nil, err
}
switch ttp := tp.(type) {
case endpointChannelAccepter:
ca, err := newChannelAccepter(n, ttp)
if err != nil {
closeExisting()
return nil, err
}
n.channelAccepters[ca] = struct{}{}
case endpointChannelSingle:
ch, err := newChannel(n, ttp, ttp.Label(), ttp)
if err != nil {
closeExisting()
return nil, err
}
n.channels[ch] = struct{}{}
default:
panic(fmt.Errorf("endpoint %T does not implement any interface", tp))
}
}
// modules
n.nodeHeartbeat = newNodeHeartbeat(n)
n.nodeStreamRequest = newNodeStreamRequest(n)
if n.nodeHeartbeat != nil {
n.pool.Start(n.nodeHeartbeat)
}
if n.nodeStreamRequest != nil {
n.pool.Start(n.nodeStreamRequest)
}
for ch := range n.channels {
n.pool.Start(ch)
}
for ca := range n.channelAccepters {
n.pool.Start(ca)
}
n.pool.Start(n)
return n, nil
}
func (n *Node) run() {
outer:
for rawEvt := range n.eventsIn {
switch evt := rawEvt.(type) {
case *eventInChannelNew:
n.channels[evt.ch] = struct{}{}
n.pool.Start(evt.ch)
case *eventInChannelClosed:
delete(n.channels, evt.ch)
evt.ch.close()
case *eventInWriteTo:
if _, ok := n.channels[evt.ch]; ok == false {
return
}
evt.ch.writeChan <- evt.what
case *eventInWriteAll:
for ch := range n.channels {
ch.writeChan <- evt.what
}
case *eventInWriteExcept:
for ch := range n.channels {
if ch != evt.except {
ch.writeChan <- evt.what
}
}
case *eventInClose:
break outer
}
}
// consume events up to close()
go func() {
for range n.eventsIn {
}
}()
if n.nodeHeartbeat != nil {
n.nodeHeartbeat.close()
}
if n.nodeStreamRequest != nil {
n.nodeStreamRequest.close()
}
for ca := range n.channelAccepters {
ca.close()
}
for ch := range n.channels {
ch.close()
}
}
// Close halts node operations and waits for all routines to return.
func (n *Node) Close() {
// consume events up to close()
// in case user is not calling Events()
go func() {
for range n.eventsOut {
}
}()
n.eventsIn <- &eventInClose{}
n.pool.Wait()
close(n.eventsIn)
close(n.eventsOut)
}
// Events returns a channel from which receiving events. Possible events are:
// *EventChannelOpen
// *EventChannelClose
// *EventFrame
// *EventParseError
// *EventStreamRequested
// See individual events for meaning and content.
func (n *Node) Events() chan Event {
return n.eventsOut
}
// WriteMessageTo writes a message to given channel.
func (n *Node) WriteMessageTo(channel *Channel, message Message) {
n.eventsIn <- &eventInWriteTo{channel, message}
}
// WriteMessageAll writes a message to all channels.
func (n *Node) WriteMessageAll(message Message) {
n.eventsIn <- &eventInWriteAll{message}
}
// WriteMessageExcept writes a message to all channels except specified channel.
func (n *Node) WriteMessageExcept(exceptChannel *Channel, message Message) {
n.eventsIn <- &eventInWriteExcept{exceptChannel, message}
}
// WriteFrameTo writes a frame to given channel.
// This function is intended only for routing pre-existing frames to other nodes,
// since all frame fields must be filled manually.
func (n *Node) WriteFrameTo(channel *Channel, frame Frame) {
n.eventsIn <- &eventInWriteTo{channel, frame}
}
// WriteFrameAll writes a frame to all channels.
// This function is intended only for routing pre-existing frames to other nodes,
// since all frame fields must be filled manually.
func (n *Node) WriteFrameAll(frame Frame) {
n.eventsIn <- &eventInWriteAll{frame}
}
// WriteFrameExcept writes a frame to all channels except specified channel.
// This function is intended only for routing pre-existing frames to other nodes,
// since all frame fields must be filled manually.
func (n *Node) WriteFrameExcept(exceptChannel *Channel, frame Frame) {
n.eventsIn <- &eventInWriteExcept{exceptChannel, frame}
}