/
flightData.go
104 lines (94 loc) · 3.13 KB
/
flightData.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
/**
*Copyright (c) 2018 Stephen Merrony
*
*This software is released under the MIT License.
*https://opensource.org/licenses/MIT
*/
package main
import (
"fmt"
"log"
"math"
"github.com/SMerrony/tello"
)
// fdListener should be run as a Goroutine to consume FD updates on the chan as they arrive.
// It is started by connectCB() in droneCBs.go when the Tello is connected.
func fdListener(fdChan <-chan tello.FlightData) {
for {
select {
case tmpFd := <-fdChan:
flightDataMu.Lock()
flightData = tmpFd
flightDataMu.Unlock()
if tmpFd.DownVisualState {
log.Println("Down visual state")
}
if tmpFd.OnGround {
log.Println("On Ground")
}
if tmpFd.LightStrength == 0 {
liveTrack.addPositionIfChanged(tmpFd)
}
case <-fdStopChan:
log.Println("Info: Flight Data Listener stopping")
return
}
}
}
// updateFlightDataTCB should be run periodically to check for condition we should alert the user about
// and update the flight status display.
func updateFlightDataTCB() bool {
msg := ""
flightDataMu.RLock()
// first, the message overlaid on the video display
if len(flightData.SSID) > 0 {
switch {
case flightData.BatteryCritical:
msg = "Battery Critical "
case flightData.BatteryLow:
msg = "Battery Low "
}
if flightData.WifiStrength < 30 {
msg += "Wifi < 30% "
} else if flightData.WifiStrength < 50 {
msg += "Wifi < 50% "
}
if flightData.LightStrength == 1 {
msg += "Low Light "
}
if flightData.WindState {
msg += "Too Windy "
}
}
// now the flight status display
statFields[fYaw].value.SetText(fmt.Sprintf("%d°", flightData.IMU.Yaw))
statFields[fLoBattThres].value.SetText(fmt.Sprintf("%d%%", flightData.LowBatteryThreshold))
statFields[fTemp].value.SetText(fmt.Sprintf("%dC", flightData.IMU.Temperature))
statFields[fDrvdSpeed].value.SetText(fmt.Sprintf("%.1fm/s", math.Sqrt(float64(flightData.NorthSpeed*flightData.NorthSpeed)+float64(flightData.EastSpeed*flightData.EastSpeed))))
statFields[fVertSpeed].value.SetText(fmt.Sprintf("%dm/s", flightData.VerticalSpeed))
statFields[fGndSpeed].value.SetText(fmt.Sprintf("%dm/s", flightData.GroundSpeed))
statFields[fFwdSpeed].value.SetText(fmt.Sprintf("%dm/s", flightData.NorthSpeed))
statFields[fLatSpeed].value.SetText(fmt.Sprintf("%dm/s", flightData.EastSpeed))
statFields[fBattLow].value.SetText(boolToYN(flightData.BatteryLow))
statFields[fBattCrit].value.SetText(boolToYN(flightData.BatteryCritical))
statFields[fBattErr].value.SetText(boolToYN(flightData.BatteryState))
statFields[fXPos].value.SetText(fmt.Sprintf("%.2fm", flightData.MVO.PositionX))
statFields[fYPos].value.SetText(fmt.Sprintf("%.2fm", flightData.MVO.PositionY))
statFields[fZPos].value.SetText(fmt.Sprintf("%.2fm", flightData.MVO.PositionZ))
statFields[fOnGround].value.SetText(boolToYN(flightData.OnGround))
statFields[fFlying].value.SetText(boolToYN(flightData.Flying))
statFields[fWindy].value.SetText(boolToYN(flightData.WindState))
flightDataMu.RUnlock()
if msg == "" {
videoWgt.clearMessage()
} else {
videoWgt.setMessage(msg)
}
return true // continue the timer
}
func boolToYN(b bool) string {
if b {
return "Y"
}
return "N"
}