-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathflight.go
131 lines (103 loc) · 2.44 KB
/
flight.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
// GoBalloon
// flight.go - Flight controller code
//
// (c) 2014, Christopher Snell
package main
import (
"github.com/chrissnell/GoBalloon/gps"
"github.com/mrmorphic/hwio"
"log"
"sync"
"time"
)
func FlightComputer(g *gps.GPSReading, wg *sync.WaitGroup) {
var maxalt float64
var once sync.Once
var timer *time.Timer
wg.Add(1)
defer wg.Done()
for {
select {
case <-shutdownFlight:
return
default:
pos := g.Get()
if pos.Lat != 0 && pos.Lon != 0 {
if pos.Altitude > maxalt {
maxalt = pos.Altitude
}
if *debug {
log.Printf("MAX ALT: %v\n", maxalt)
}
if maxalt > 17000 && pos.Altitude < 15000 {
once.Do(func() { SoundBuzzer(wg) })
}
}
timer = time.NewTimer(time.Second * 5)
<-timer.C
}
}
}
func InitiateCutdown() {
// P8-10
// GPIO_68 on poinout diagrams
// (Power/reset side of board, 5th row down from power/reset, on outside column)
// Valid pins: GPIO2_3 (pin 8, P8)
// GPIO2_4 (pin 10, P8)
// GPIO2_2 (pin 7, P8)
// GPIO1_13 (pin 11, P8)
var pin string = "gpio1_13"
outputPin, err := hwio.GetPinWithMode(pin, hwio.OUTPUT)
if err != nil {
log.Printf("InitiateCutdown() :: Error getting GPIO pin: %v\n", err)
}
aprsMessage <- "Preparing to cutdown in 30 sec"
timer := time.NewTimer(time.Second * 30)
<-timer.C
log.Println("--- CUTTING DOWN ---")
hwio.DigitalWrite(outputPin, hwio.HIGH)
timer = time.NewTimer(time.Second * 10)
<-timer.C
hwio.DigitalWrite(outputPin, hwio.LOW)
hwio.CloseAll()
log.Println("InitiateCutdown() :: Closed all pins")
}
func SoundBuzzer(wg *sync.WaitGroup) {
var timer, timer2 *time.Timer
var pin string = "gpio2_2"
toggle := make(chan bool)
wg.Add(1)
defer wg.Done()
log.Println("Activating buzzer")
outputPin, err := hwio.GetPinWithMode(pin, hwio.OUTPUT)
if err != nil {
log.Printf("Error getting GPIO pin: %v\n", err)
}
go func() {
for {
timer = time.NewTimer(time.Millisecond * 1000)
<-timer.C
toggle <- true
timer2 = time.NewTimer(time.Millisecond * 50)
<-timer2.C
toggle <- false
}
}()
for {
select {
case <-shutdownFlight:
log.Println("SoundBuzzer() :: Break")
hwio.DigitalWrite(outputPin, hwio.LOW)
hwio.CloseAll()
log.Println("SoundBuzzer() :: Closed all pins")
return
case t := <-toggle:
log.Printf("SoundBuzzer() :: Toggling buzzer: %v\n", t)
if t {
hwio.DigitalWrite(outputPin, hwio.HIGH)
} else {
hwio.DigitalWrite(outputPin, hwio.LOW)
}
}
}
}