-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithm.go
More file actions
173 lines (158 loc) · 5.02 KB
/
algorithm.go
File metadata and controls
173 lines (158 loc) · 5.02 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
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
package main
import (
"fmt"
"math"
"os"
"time"
//dep workaround
_ "gopkg.in/vmihailenco/msgpack.v3"
_ "github.com/vmihailenco/msgpack"
l7g "github.com/immesys/chirp-l7g"
)
//You can put some state variables and type definitions here
type path struct {
Src int
Dst int
}
type algorithmstate struct {
//This is just example information copied from python
//You can put anything you want here
AnemometerID string
IsDuct bool
CalibrationPeriod time.Duration
IsCalibrating bool
PrevRelPhase map[path]float64
PrevAbsPhase map[path]float64
CurAbsPhase map[path]float64
CalibrationPhases map[path][]float64
CalibrationIndexes map[path][]int
CalibratedIndex map[path]int
Paths []path
}
var states map[string]*algorithmstate
func main() {
fmt.Printf("Version 3.6 ====\n")
localtty := ""
if len(os.Args) > 1 {
localtty = os.Args[1]
}
//Register and run our algorithm.
err := l7g.RunDPA([]byte(ourEntity), Initialize, OnNewDataSet,
//This is the algorithm vendor
"ucberkeley",
//This is the algorithm version
"1.0",
//This is the address of the local connection or blank to disable
localtty)
fmt.Printf("fatal error: %v\n", err)
}
func Initialize(emit l7g.Emitter) {
states = make(map[string]*algorithmstate)
//If you want the algorithm output to be on standard out as well
//to allow for local use, do this
emit.MirrorToStandardOutput(true)
}
// OnNewDataSet encapsulates the algorithm. You can store the emitter and
// use it asynchronously if required. You can see the documentation for the
// parameters at https://godoc.org/github.com/immesys/chirp-l7g
// The popHdr and data arrays are always length 4 and represent the data
// for four sets of readings with primary = 0..3. They are always contiguous
// in time. If the set is incomplete due to data loss, the missing elements
// will have nil in them.
// Sometimes the popHdr will be nil for an element even if the data is present
// this indicates a packet that was lost but reconstructed using forward
// error correction codes.
func OnNewDataSet(info *l7g.SetInfo, popHdr []*l7g.L7GHeader, data []*l7g.ChirpHeader, emit l7g.Emitter) {
//We only want to process complete sets of data
// if !info.Complete {
// return
// }
//This string is the complete ID of this anemometer
//You can use this as a key into buffers of historic state
idstring := info.MAC
state, found := states[idstring]
if !found {
//Initialize new algorithm state here
//perhaps make it calibrate or something
state = &algorithmstate{
AnemometerID: idstring,
IsDuct: info.IsDuct,
}
states[idstring] = state
}
//Initialize the output data object
//We will build up the data inside this as we process the input data
outputdata := l7g.OutputData{
Timestamp: info.TimeOfFirst.UnixNano(),
Sensor: info.MAC,
}
//Over all paths
//fmt.Printf("data len is %d\n", len(data))
// for src := 0; src < len(data); src++ {
// for dst := 0; dst < len(data); dst++ {
// if info.IsDuct6 {
// if src < 3 && dst < 3 {
// continue
// }
// if dst >= 3 && dst >= 3 {
// continue
// }
// } else {
// if src == dst {
// //We don't use data from primary
// continue
// }
// }
// // spew.Dump(info)
// // fmt.Printf("src=%d dst=%d\n", src, dst)
// // spew.Dump(data)
// p := path{src, dst}
// maxIndex := data[src].MaxIndex[dst]
// if maxIndex < 3 {
// //This might indicate something is wrong
// //It also means that our IQ values start from 0
// //not from maxIndex-3
// }
// Ivalue2beforeMax := data[src].IValues[dst][1] //0 is 3 before
// Qvalue2beforeMax := data[src].QValues[dst][1]
//
// //Use this as well as other data
// _ = p
// _ = maxIndex
// _ = Ivalue2beforeMax
// _ = Qvalue2beforeMax
//
// //To populate outputdata
// outputdata.Tofs = append(outputdata.Tofs, l7g.TOFMeasure{
// Src: src,
// Dst: dst,
// //Here you should actually put the time of flight in microseconds
// Val: 555,
// })
// outputdata.Temperatures = append(outputdata.Temperatures, l7g.TempMeasure{
// Src: src,
// Dst: dst,
// //Here you should actually put the temperature in celsius
// Val: 25.5,
// })
// }
// }
//If you have a new velocity estimate, you should also add that here
//These are in m/s
outputdata.Velocities.Valid = true
outputdata.Velocities.X = 1 //due north
outputdata.Velocities.Y = 2 //due west
outputdata.Velocities.Z = 3 //vertical
//Also in m/s
outputdata.Velocities.Mag = math.Sqrt(1 + 4 + 9)
// Phi, the azimuthal angle is the degrees counterclockwize from North (X)
outputdata.Velocities.Phi = 55
// Theta, the polar angle is the degrees from vertical
outputdata.Velocities.Theta = 66
//If you have other information output from the algorithm you can
//add that in here
outputdata.Extradata = append(outputdata.Extradata, "the algorithm has not been filled in yet")
//Emit the data on the data bus (and if MirrorToStandardOut is true, also
//to standard output)
emit.Data(outputdata)
}