forked from cyoung/stratux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathahrs_table.go
109 lines (90 loc) · 1.98 KB
/
ahrs_table.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
package main
import (
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
"sync"
"time"
)
const (
SITUATION_URL = "http://127.0.0.1/getSituation"
)
type MySituation struct {
AHRSRoll float64
AHRSPitch float64
}
var Location MySituation
var situationMutex *sync.Mutex
func chkErr(err error) {
if err != nil {
fmt.Printf("error: %s\n", err.Error())
os.Exit(1)
}
}
var currentAHRSString string
func listener() {
t := time.Now()
addr := net.UDPAddr{Port: 41504, IP: net.ParseIP("0.0.0.0")}
conn, err := net.ListenUDP("udp", &addr)
if err != nil {
fmt.Printf("error listening: %s\n", err.Error())
return
}
defer conn.Close()
for {
buf := make([]byte, 1024)
n, _, err := conn.ReadFrom(buf)
if err != nil {
fmt.Printf("Err receive: %s\n", err.Error())
continue
}
buf_encoded := make([]byte, hex.EncodedLen(n))
hex.Encode(buf_encoded, buf[:n])
t2 := time.Now()
time_diff := t2.Sub(t)
t = t2
fmt.Sprintf("%d,%s\n", time_diff/time.Millisecond, buf_encoded)
currentAHRSString = string(buf_encoded)
}
}
func situationUpdater() {
situationUpdateTicker := time.NewTicker(100 * time.Millisecond)
for {
<-situationUpdateTicker.C
situationMutex.Lock()
resp, err := http.Get(SITUATION_URL)
if err != nil {
fmt.Printf("HTTP GET error: %s\n", err.Error())
situationMutex.Unlock()
continue
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Printf("HTTP GET body error: %s\n", err.Error())
resp.Body.Close()
situationMutex.Unlock()
continue
}
// fmt.Printf("body: %s\n", string(body))
err = json.Unmarshal(body, &Location)
if err != nil {
fmt.Printf("HTTP JSON unmarshal error: %s\n", err.Error())
}
resp.Body.Close()
situationMutex.Unlock()
}
}
func main() {
situationMutex = &sync.Mutex{}
go listener()
go situationUpdater()
tm := time.NewTicker(125 * time.Millisecond)
for {
<-tm.C
fmt.Printf("%f,%f,%s\n", Location.AHRSRoll, Location.AHRSPitch, currentAHRSString)
}
}