-
Notifications
You must be signed in to change notification settings - Fork 5
/
event_loop.go
228 lines (211 loc) · 5.12 KB
/
event_loop.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
package main
import (
"encoding/binary"
"fmt"
"github.com/mattn/go-tty"
"log"
"os"
"os/signal"
"strings"
"syscall"
"time"
)
const (
ASTATE_Unknown = iota
ASTATE_Ready
ASTATE_Armed
ASTATE_Disarmed
)
func get_status(v SChan) (status uint64, armflags uint32) {
if v.cmd == msp2_INAV_STATUS {
status = binary.LittleEndian.Uint64(v.data[13:21])
} else {
status = uint64(binary.LittleEndian.Uint32(v.data[6:10]))
}
if v.cmd == msp_STATUS_EX {
armflags = uint32(binary.LittleEndian.Uint16(v.data[13:15]))
} else {
armflags = binary.LittleEndian.Uint32(v.data[9:13])
}
return status, armflags
}
func (m *MSPSerial) find_status_cmd() (stscmd uint16) {
// MSP STatus inquiry, INAV version dependent
if m.vcapi > 0x200 {
if m.fcvers >= 0x010801 {
stscmd = msp2_INAV_STATUS
} else {
stscmd = msp_STATUS_EX
}
} else {
stscmd = msp_STATUS
}
return stscmd
}
func (m *MSPSerial) test_rx(setthr int, verbose bool, autoarm bool) {
phase := PHASE_Quiescent
stscmd := m.find_status_cmd()
fs := false
xboxflags := uint64(0)
xarmflags := uint32(0)
dpending := false
tty, err := tty.Open()
if err != nil {
log.Fatal(err)
}
defer tty.Close()
evchan := make(chan rune)
go func() {
for {
r, err := tty.ReadRune()
if err != nil {
log.Panic(err)
}
evchan <- r
}
}()
cc := make(chan os.Signal, 1)
signal.Notify(cc, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
fmt.Println("Keypresses: 'A'/'a': toggle arming, 'Q'/'q': quit, 'F': quit to failsafe")
if setthr > 999 && setthr < 2001 {
fmt.Println(" '+'/'-' raise / lower throttle by 25µs")
}
log.Printf("Start TX loop")
ticker := time.NewTicker(time.Duration(*every) * time.Millisecond)
for done := false; done == false; {
select {
case <-ticker.C:
tdata := m.serialise_rx(phase, setthr, fs)
m.Send_msp(msp_SET_RAW_RC, tdata)
if verbose {
txdata := deserialise_rx(tdata)
log.Printf("Tx: %v\n", txdata)
}
case v := <-m.c0:
if v.ok {
switch v.cmd {
case msp_SET_RAW_RC:
if verbose {
m.Send_msp(msp_RC, nil)
} else {
m.Send_msp(stscmd, nil)
}
case msp_RC:
rxdata := deserialise_rx(v.data)
log.Printf("Rx: %v\n", rxdata)
m.Send_msp(stscmd, nil)
case msp2_INAV_STATUS, msp_STATUS_EX, msp_STATUS:
boxflags, armflags := get_status(v)
if boxflags != xboxflags || xarmflags != armflags {
log.Printf("Box: %s (%x) Arm: %s\n", m.format_box(boxflags), boxflags, arm_status(armflags))
fs = ((boxflags & m.fail_mask) == m.fail_mask)
if boxflags&m.arm_mask == 0 { // not armed
if armflags < 0x80 { // ready to arm
if autoarm {
phase = PHASE_Arming
autoarm = false
} else {
phase = PHASE_Quiescent
done = dpending
}
}
} else { // Armed
phase = PHASE_LowThrottle
}
xboxflags = boxflags
xarmflags = armflags
}
default:
}
} else {
log.Printf("MSP %d (%x) failed\n", v.cmd, v.cmd)
done = true
}
case ev := <-evchan:
switch ev {
case 'A', 'a':
switch phase {
case PHASE_Quiescent:
log.Println("Arming commanded")
phase = PHASE_Arming
case PHASE_LowThrottle:
log.Println("Disarming commanded")
phase = PHASE_Disarming
default:
}
case 'F':
log.Println("Exit to F/S commanded")
done = true
case 'Q', 'q':
log.Println("Quit commanded")
phase, done, dpending = safe_quit(phase)
case 'v', 'V':
verbose = !verbose
case '+', '-':
if setthr > 999 && setthr < 2001 {
if ev == '+' {
setthr += 25
} else {
setthr -= 25
}
if setthr > 2000 {
setthr = 2000
} else if setthr < 1000 {
setthr = 1000
}
log.Printf("Throttle commanded: %d\n", setthr)
}
}
case <-cc:
log.Println("Interrupt")
phase, done, dpending = safe_quit(phase)
}
}
}
func safe_quit(phase int) (int, bool, bool) {
dpending := false
done := false
if phase == PHASE_LowThrottle || phase == PHASE_Disarming {
dpending = true
phase = PHASE_Disarming
} else {
done = true
}
return phase, done, dpending
}
func arm_status(status uint32) string {
armfails := [...]string{
"", /* 1 */
"", /* 2 */
"Armed", /* 4 */
"Ever armed", /* 8 */
"", /* 10 */ // HITL
"", /* 20 */ // SITL
"", /* 40 */
"F/S", /* 80 */
"Level", /* 100 */
"Calibrate", /* 200 */
"Overload", /* 400 */
"NavUnsafe", "MagCal", "AccCal", "ArmSwitch", "H/WFail",
"BoxF/S", "BoxKill", "RCLink", "Throttle", "CLI",
"CMS", "OSD", "Roll/Pitch", "Autotrim", "OOM",
"Settings", "PWM Out", "PreArm", "DSHOTBeep", "Land", "Other",
}
var sarry []string
if status < 0x80 {
if status&(1<<2) != 0 {
sarry = append(sarry, armfails[2])
}
if len(sarry) == 0 {
sarry = append(sarry, "Ready to arm")
}
} else {
for i := 0; i < len(armfails); i++ {
if ((status & (1 << i)) != 0) && armfails[i] != "" {
sarry = append(sarry, armfails[i])
}
}
}
sarry = append(sarry, fmt.Sprintf("(0x%x)", status))
return strings.Join(sarry, " ")
}