-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwinddir.py
More file actions
124 lines (79 loc) · 2.89 KB
/
Copy pathwinddir.py
File metadata and controls
124 lines (79 loc) · 2.89 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
#!/usr/bin/env python3
import busio, digitalio, board, logging
import adafruit_mcp3xxx.mcp3008 as MCP
from adafruit_mcp3xxx.analog_in import AnalogIn
import numpy as np
import RPi.GPIO as GPIO
Logger = logging.getLogger(__name__)
def get_channel_values():
GPIO.setmode(GPIO.BCM)
#create SPI interface
spi = busio.SPI(clock=board.SCK, MISO=board.MISO, MOSI=board.MOSI)
#create chip select instance
cs = digitalio.DigitalInOut(board.D22)
#create MCP3008 instance
mcp = MCP.MCP3008(spi, cs)
#create analog input channels
channels = []
mcp_pins = [MCP.P0, MCP.P1, MCP.P2, MCP.P3, MCP.P4, MCP.P5, MCP.P6, MCP.P7]
for pin in mcp_pins:
channels.append(AnalogIn(mcp, pin))
return channels
#get wind direction when multiple switches are triggered simultaneously
def getdirfrommultigoodvals(isvalid, dirs):
ii = 0
seqs = []
seqs[0] = [isvalid[0]]
#parsing indices into lists of consecutive values (e.g. [1,2,5,6,7] => [[1,2],[5,6,7]])
for i in isvalid[1:]:
if i == seqs[ii][-1] + 1:
seqs[ii].append(i)
else:
ii += 1
seqs.append([i])
#finding longest consecutive sequence
seqlens = []
for clist in seqs:
seqlens.append(len(clist))
mainseq = seqs[np.argmax(seqlens)]
ld = len(dirs)
if not (ld-1 in isvalid and 0 in isvalid): #if channels with high voltages don't cross over 360->0
winddir = np.mean(dirs[mainseq])
else:
#pull lists with start and end indices
maxlen = np.max(seqlens)
for s in seqs:
if ld-1 in s:
sE = s
elif 0 in s:
sS = s
if len(sE) + len(sS) > maxlen: #use new sequences
sS += 360
winddir = np.mean(dirs[sS + sE])
if winddir >= 360:
winddir -= 360
else:
winddir = np.mean(dirs[mainseq])
return winddir
# gets wind direction using threshold where
def get_winddir_from_voltages(channels, threshold):
winddir = -1
dirs = np.array([0, 45, 90, 135, 180, 225, 270, 315])
winddir = dirs[np.argmax(channels)]
return winddir
def getwinddirection():
channels = get_channel_values()
chvoltages = []
chvoltstr = "WDir Voltages: "
for i,ch in enumerate(channels):
chvoltages.append(ch.voltage)
chvoltstr += f"CH:{i}={ch.voltage:3.2f}V, "
chvoltages = np.asarray(chvoltages)
Logger.debug(chvoltstr)
winddir = get_winddir_from_voltages(chvoltages, 0.8)
if winddir == -1:
winddir = 0
return winddir
if __name__ == "__main__":
channels = get_channel_values()
print_voltages(channels)