forked from bluenviron/gomavlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_heartbeat.go
More file actions
78 lines (66 loc) · 1.77 KB
/
node_heartbeat.go
File metadata and controls
78 lines (66 loc) · 1.77 KB
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
package gomavlib
import (
"time"
)
type nodeHeartbeat struct {
n *Node
terminate chan struct{}
}
func newNodeHeartbeat(n *Node) *nodeHeartbeat {
// module is disabled
if n.conf.HeartbeatDisable == true {
return nil
}
// dialect must be enabled
if n.conf.D == nil {
return nil
}
// heartbeat message must exist in dialect and correspond to standard
mp, ok := (n.conf.D).getMsgById(0)
if ok == false || (*mp).getCRCExtra() != 50 {
return nil
}
h := &nodeHeartbeat{
n: n,
terminate: make(chan struct{}, 1),
}
return h
}
func (h *nodeHeartbeat) close() {
h.terminate <- struct{}{}
}
func (h *nodeHeartbeat) run() {
// take version from dialect if possible
mavlinkVersion := uint64(3)
if h.n.conf.D != nil {
mavlinkVersion = uint64(h.n.conf.D.getVersion())
}
ticker := time.NewTicker(h.n.conf.HeartbeatPeriod)
defer ticker.Stop()
for {
select {
case <-ticker.C:
dm, _ := h.n.conf.D.getMsgById(0)
msg := (*dm).newMsg()
if _, ok := msg.(*DynamicMessage); ok {
msg.SetField("type", uint8(h.n.conf.HeartbeatSystemType))
msg.SetField("autopilot", uint8(h.n.conf.HeartbeatAutopilotType))
msg.SetField("base_mode", uint8(0))
msg.SetField("custom_mode", uint32(0))
msg.SetField("system_status", uint8(4)) // MAV_STATE_ACTIVE
msg.SetField("mavlink_version", uint8(mavlinkVersion))
h.n.WriteMessageAll(msg)
} else {
msg.SetField("Type", int8(h.n.conf.HeartbeatSystemType))
msg.SetField("Autopilot", int8(h.n.conf.HeartbeatAutopilotType))
msg.SetField("BaseMode", int8(0))
msg.SetField("CustomMode", uint32(0))
msg.SetField("SystemStatus", int8(4)) // MAV_STATE_ACTIVE
msg.SetField("MavlinkVersion", uint8(mavlinkVersion))
h.n.WriteMessageAll(msg)
}
case <-h.terminate:
return
}
}
}