-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from aws/grafana_demo_updates
Updated the weather station math to more closely match the Sparkfun reference code
- Loading branch information
Showing
6 changed files
with
231 additions
and
34 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+2.33 KB
examples/python/sara_example/__pycache__/weather_station.cpython-39.pyc
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
from weather_station import weather_station | ||
import random | ||
import time | ||
|
||
direction = [ 350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0, | ||
350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0, | ||
350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0, | ||
350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0, | ||
350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0, | ||
350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0, | ||
10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, | ||
10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, | ||
10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, | ||
10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, | ||
10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, | ||
10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, | ||
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, | ||
350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0, | ||
350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0, | ||
350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0, | ||
350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0, | ||
350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0, | ||
350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0, | ||
10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, | ||
10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, | ||
10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, | ||
10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, | ||
10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, | ||
10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, | ||
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, | ||
350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0, | ||
350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0, | ||
350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0, | ||
350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0, | ||
350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0, | ||
350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0, | ||
10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, | ||
10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, | ||
10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, | ||
10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, | ||
10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, | ||
10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, | ||
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, | ||
350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0, | ||
350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0, | ||
350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0, | ||
350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0, | ||
350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0, | ||
350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0, | ||
10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, | ||
10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, | ||
10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, | ||
10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, | ||
10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, | ||
10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, | ||
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, | ||
350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0, | ||
350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0, | ||
350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0, | ||
350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0, | ||
350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0, | ||
350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0,350.0, | ||
10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, | ||
10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, | ||
10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, | ||
10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, | ||
10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, | ||
10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, | ||
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, | ||
] | ||
|
||
ws= weather_station() | ||
|
||
for d in direction: | ||
ws.addWind( random.random() * 50.0, d ) | ||
|
||
print("gust speed " + str(ws.windGust1minSpeed)) | ||
print("gust direction " + str(ws.windGust1minDirection)) | ||
print("10 min gust speed " + str(ws.wind10MinGustMPH)) | ||
print("10 min gust direction " + str(ws.wind10MinGustDirection)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import time | ||
|
||
class weather_station: | ||
windGust1minSpeed:float | ||
windGust1minDirection:float | ||
windDataSpeed = [] | ||
windDataDirection = [] | ||
windGust = [] | ||
gustDataCounter:int | ||
windDataCounter:int | ||
|
||
# report this information | ||
windSpeed:float | ||
windDirection:float | ||
wind2MinAverageMPH:float | ||
wind2MinAverageDirection:float | ||
wind10MinGustMPH:float | ||
wind10MinGustDirection:float | ||
|
||
def __init__(self): | ||
print("starting weather station") | ||
self.windGust1minSpeed = 0.0 | ||
self.gustDataCounter = 60 | ||
self.windDataCounter = 120 | ||
self.windGust1minDirection = 0.0 | ||
self.windGust1minSpeed = 0.0 | ||
self.wind2MinAverageMPH = 0.0 | ||
self.wind2MinAverageDirection = 0.0 | ||
self.wind10MinGustDirection = 0.0 | ||
self.wind10MinGustMPH = 0.0 | ||
self.windSpeed = 0.0 | ||
self.windDirection = 0.0 | ||
|
||
# run every 1 minute assumes it is called every second | ||
def _doGusts(self, speed: float, direction:float): | ||
self.gustDataCounter -= 1 | ||
if speed > self.windGust1minSpeed: | ||
self.windGust1minSpeed = speed | ||
self.windGust1minDirection = direction | ||
|
||
if self.gustDataCounter == 0: | ||
self.gustDataCounter = 60 | ||
# update the 10 minute wind gust statistics every minute | ||
gdata = (self.windGust1minSpeed, self.windGust1minDirection) | ||
self.windGust.append( gdata ) | ||
self.windGust1minSpeed = 0 | ||
while len(self.windGust) > 10: # ensure we only have 10 minutes of data | ||
self.windGust.pop(0) | ||
gust = (0.0,0.0) | ||
for g in self.windGust: | ||
if g[0] > gust[0]: | ||
gust = g | ||
self.wind10MinGustMPH = gust[0] | ||
self.wind10MinGustDirection = gust[1] | ||
|
||
def addWind(self, speed: float , direction:float): | ||
self.windDataCounter -= 1 | ||
self.windSpeed = speed | ||
self.windDirection = direction | ||
self.windDataSpeed.append( speed ) | ||
self.windDataDirection.append( direction ) | ||
|
||
self._doGusts(speed,direction) | ||
|
||
if self.windDataCounter == 0: | ||
self.windDataCounter = 120 | ||
# time to compute wind statistics (every 2 minutes) | ||
self._calcWeather() | ||
self.windDataDirection.clear() | ||
self.windDataSpeed.clear() | ||
|
||
def _addDirection(self, direction1:float, direction2:float )->float: | ||
delta = direction1 - direction2 | ||
if delta < -180.0: | ||
return delta + 360 | ||
elif delta > 180.0: | ||
return delta - 360 | ||
else: | ||
return delta | ||
|
||
def _calcWeather(self): | ||
speedAverage = 0 | ||
for w in self.windDataSpeed: | ||
speedAverage += w | ||
self.wind2MinAverageMPH = speedAverage / len(self.windDataSpeed) | ||
|
||
sum = self.windDataDirection[0] | ||
D = self.windDataDirection[0] | ||
for d in self.windDataDirection[1:]: | ||
D += self._addDirection(d, D) | ||
sum += D | ||
self.wind2MinAverageDirection = sum / len(self.windDataDirection) | ||
|
||
|
||
|