forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flight_path.go
131 lines (113 loc) · 3.41 KB
/
flight_path.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
package sims
import (
"math"
"time"
"github.com/grafana/grafana-plugin-sdk-go/data"
)
type flightSim struct {
key simulationKey
cfg flightConfig
}
var (
_ Simulation = (*flightSim)(nil)
)
type flightDataPoint struct {
time time.Time
lat float64 // gps
lng float64 // gps
heading float64 // degree
altitude float64 // above ground
}
type flightConfig struct {
CenterLat float64 `json:"centerLat"`
CenterLng float64 `json:"centerLng"`
Radius float64 `json:"radius"`
AltitudeMin float64 `json:"altitudeMin"`
AltitudeMax float64 `json:"altitudeMax"`
Period float64 `json:"period"` // angular speed (seconds)
}
func (s *flightSim) GetState() simulationState {
return simulationState{
Key: s.key,
Config: s.cfg,
}
}
func (s *flightSim) SetConfig(vals map[string]interface{}) error {
return updateConfigObjectFromJSON(s.cfg, vals)
}
func (s *flightSim) NewFrame(size int) *data.Frame {
frame := data.NewFrameOfFieldTypes("", size,
data.FieldTypeTime, // time
data.FieldTypeFloat64, // lat
data.FieldTypeFloat64, // lng
data.FieldTypeFloat64, // heading
data.FieldTypeFloat64, // altitude
)
frame.Fields[0].Name = "time"
frame.Fields[1].Name = "lat"
frame.Fields[2].Name = "lng"
frame.Fields[3].Name = "heading"
frame.Fields[4].Name = "altitude"
return frame
}
func (s *flightSim) GetValues(t time.Time) map[string]interface{} {
p := s.cfg.getNextPoint(t)
return map[string]interface{}{
"time": p.time,
"lat": p.lat,
"lng": p.lng,
"heading": p.heading,
"altitude": p.altitude,
}
}
func (s *flightSim) Close() error {
return nil
}
func newFlightSimInfo() simulationInfo {
sf := flightConfig{
CenterLat: 37.83, // San francisco
CenterLng: -122.42487,
Radius: 0.01, // raw gps degrees
AltitudeMin: 350,
AltitudeMax: 400,
Period: 10, //model.Get("period").MustFloat64(10),
}
df := data.NewFrame("")
df.Fields = append(df.Fields, data.NewField("centerLat", nil, []float64{sf.CenterLat}))
df.Fields = append(df.Fields, data.NewField("centerLng", nil, []float64{sf.CenterLng}))
df.Fields = append(df.Fields, data.NewField("radius", nil, []float64{sf.Radius}))
df.Fields = append(df.Fields, data.NewField("altitudeMin", nil, []float64{sf.AltitudeMin}))
df.Fields = append(df.Fields, data.NewField("altitudeMax", nil, []float64{sf.AltitudeMax}))
f := data.NewField("period", nil, []float64{sf.Period})
f.Config = &data.FieldConfig{Unit: "s"} // seconds
df.Fields = append(df.Fields, f)
return simulationInfo{
Type: "flight",
Name: "Flight",
Description: "simple circling airplain",
ConfigFields: df,
OnlyForward: false,
create: func(cfg simulationState) (Simulation, error) {
s := &flightSim{
key: cfg.Key,
cfg: sf, // default value
}
err := updateConfigObjectFromJSON(&s.cfg, cfg.Config) // override any fields
return s, err
},
}
}
func (f *flightConfig) getNextPoint(t time.Time) flightDataPoint {
periodNS := int64(f.Period * float64(time.Second))
ms := t.UnixNano() % periodNS
per := float64(ms) / float64(periodNS)
rad := per * 2.0 * math.Pi // 0 >> 2Pi
delta := f.AltitudeMax - f.AltitudeMin
return flightDataPoint{
time: t,
lat: f.CenterLat + math.Sin(rad)*f.Radius,
lng: f.CenterLng + math.Cos(rad)*f.Radius,
heading: (rad * 180) / math.Pi, // (math.Atanh(rad) * 180.0) / math.Pi, // in degrees
altitude: f.AltitudeMin + (delta * per), // clif
}
}