Fork of https://os.mbed.com/users/okini3939/code/WeatherMeters/
WeatherMeters.cpp@1:fa262295fa7e, 2011-01-06 (annotated)
- Committer:
- okini3939
- Date:
- Thu Jan 06 13:23:24 2011 +0000
- Revision:
- 1:fa262295fa7e
- Parent:
- 0:dc42aeff64e6
- Child:
- 2:6a62f29b1bb5
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
okini3939 | 0:dc42aeff64e6 | 1 | /* |
okini3939 | 0:dc42aeff64e6 | 2 | * mbed library to use a Sparkfun Weather Meters |
okini3939 | 0:dc42aeff64e6 | 3 | * Copyright (c) 2010 Hiroshi Suga |
okini3939 | 0:dc42aeff64e6 | 4 | * Released under the MIT License: http://mbed.org/license/mit |
okini3939 | 0:dc42aeff64e6 | 5 | */ |
okini3939 | 0:dc42aeff64e6 | 6 | |
okini3939 | 1:fa262295fa7e | 7 | /** @file WeatherMeters.cpp |
okini3939 | 1:fa262295fa7e | 8 | * @brief mbed library to use a Sparkfun Weather Meters |
okini3939 | 1:fa262295fa7e | 9 | * Anemometer, Wind vane, Rain gauge |
okini3939 | 1:fa262295fa7e | 10 | * interface: digital, analog |
okini3939 | 1:fa262295fa7e | 11 | */ |
okini3939 | 1:fa262295fa7e | 12 | |
okini3939 | 0:dc42aeff64e6 | 13 | #include "mbed.h" |
okini3939 | 0:dc42aeff64e6 | 14 | #include "WeatherMeters.h" |
okini3939 | 0:dc42aeff64e6 | 15 | |
okini3939 | 0:dc42aeff64e6 | 16 | #define WEATHER_VREF 3.3 |
okini3939 | 0:dc42aeff64e6 | 17 | |
okini3939 | 0:dc42aeff64e6 | 18 | const float tbl_windvane[16][2] = { |
okini3939 | 0:dc42aeff64e6 | 19 | {0.0, 33000}, {22.5, 6570}, {45.0, 8200}, {67.5, 891}, |
okini3939 | 0:dc42aeff64e6 | 20 | {90.0, 1000}, {112.5, 688}, {135.0, 2200}, {157.5, 1410}, |
okini3939 | 0:dc42aeff64e6 | 21 | {180.0, 3900}, {202.5, 3140}, {225.0, 16000}, {247.5, 14120}, |
okini3939 | 0:dc42aeff64e6 | 22 | {270.0, 120000}, {292.5, 42120}, {315.0, 64900}, {337.5, 21880} |
okini3939 | 0:dc42aeff64e6 | 23 | }; |
okini3939 | 0:dc42aeff64e6 | 24 | |
okini3939 | 1:fa262295fa7e | 25 | /** |
okini3939 | 1:fa262295fa7e | 26 | * @brief Initializes interface |
okini3939 | 1:fa262295fa7e | 27 | * @param p_anemometer port of Anemometer |
okini3939 | 1:fa262295fa7e | 28 | * @param p_windvane port of Wind vane (analog) |
okini3939 | 1:fa262295fa7e | 29 | * @param p_raingauge parameter of Rain gauge |
okini3939 | 1:fa262295fa7e | 30 | * @param w_mode parameter of Anemometer |
okini3939 | 1:fa262295fa7e | 31 | */ |
okini3939 | 0:dc42aeff64e6 | 32 | WeatherMeters::WeatherMeters (PinName p_anemometer, PinName p_windvane, PinName p_raingauge, Weather_mode w_mode) : int01(p_anemometer), int02(p_raingauge), ain01(p_windvane) { |
okini3939 | 0:dc42aeff64e6 | 33 | int i; |
okini3939 | 0:dc42aeff64e6 | 34 | |
okini3939 | 0:dc42aeff64e6 | 35 | windspeed = 0; windvane = 0; raingauge = 0; |
okini3939 | 0:dc42aeff64e6 | 36 | count_anemo = 0; count_rain = 0; time_anemo = 0; time_rain = 0; |
okini3939 | 0:dc42aeff64e6 | 37 | for (i = 0; i < 12; i ++) { |
okini3939 | 0:dc42aeff64e6 | 38 | buf_anemo[i] = 0; |
okini3939 | 0:dc42aeff64e6 | 39 | buf_rain[i] = 0; |
okini3939 | 0:dc42aeff64e6 | 40 | } |
okini3939 | 0:dc42aeff64e6 | 41 | |
okini3939 | 0:dc42aeff64e6 | 42 | mode = w_mode; |
okini3939 | 0:dc42aeff64e6 | 43 | int01.fall(this, &WeatherMeters::int_anemometer); |
okini3939 | 0:dc42aeff64e6 | 44 | int02.fall(this, &WeatherMeters::int_raingauge); |
okini3939 | 0:dc42aeff64e6 | 45 | ticker01.attach(this, &WeatherMeters::int_timer, 0.25); |
okini3939 | 0:dc42aeff64e6 | 46 | } |
okini3939 | 0:dc42aeff64e6 | 47 | |
okini3939 | 1:fa262295fa7e | 48 | /** |
okini3939 | 1:fa262295fa7e | 49 | * @brief Get wind speed |
okini3939 | 1:fa262295fa7e | 50 | * @return wind speed (m/s) |
okini3939 | 1:fa262295fa7e | 51 | */ |
okini3939 | 0:dc42aeff64e6 | 52 | float WeatherMeters::get_windspeed() { |
okini3939 | 0:dc42aeff64e6 | 53 | float s; |
okini3939 | 0:dc42aeff64e6 | 54 | |
okini3939 | 0:dc42aeff64e6 | 55 | if (mode == Weather_auto) { |
okini3939 | 0:dc42aeff64e6 | 56 | s = (float)windspeed * 0.667 / 3.0; // 0.667m/s/Hz (/3s) |
okini3939 | 0:dc42aeff64e6 | 57 | windspeed = 0; |
okini3939 | 0:dc42aeff64e6 | 58 | } else { |
okini3939 | 0:dc42aeff64e6 | 59 | s = (float)count_anemo * 0.667 / ((float)time_anemo / 4.0); // 0.667m/s/Hz |
okini3939 | 0:dc42aeff64e6 | 60 | time_anemo = 0; |
okini3939 | 0:dc42aeff64e6 | 61 | count_anemo = 0; |
okini3939 | 0:dc42aeff64e6 | 62 | } |
okini3939 | 0:dc42aeff64e6 | 63 | return s; |
okini3939 | 0:dc42aeff64e6 | 64 | } |
okini3939 | 0:dc42aeff64e6 | 65 | |
okini3939 | 1:fa262295fa7e | 66 | /** |
okini3939 | 1:fa262295fa7e | 67 | * @brief Get wind vane |
okini3939 | 1:fa262295fa7e | 68 | * @return wind vane (`) |
okini3939 | 1:fa262295fa7e | 69 | */ |
okini3939 | 0:dc42aeff64e6 | 70 | float WeatherMeters::get_windvane() { |
okini3939 | 0:dc42aeff64e6 | 71 | int i; |
okini3939 | 0:dc42aeff64e6 | 72 | float v; |
okini3939 | 0:dc42aeff64e6 | 73 | |
okini3939 | 0:dc42aeff64e6 | 74 | if (windspeed == 0) windvane = ain01; |
okini3939 | 0:dc42aeff64e6 | 75 | v = windvane * WEATHER_VREF; // V |
okini3939 | 0:dc42aeff64e6 | 76 | v = v / ((WEATHER_VREF - v) / 10000.0); // ohm |
okini3939 | 0:dc42aeff64e6 | 77 | for (i = 0; i < 16; i ++) { |
okini3939 | 0:dc42aeff64e6 | 78 | if (v > tbl_windvane[i][1] * 0.9 && v < tbl_windvane[i][1] * 1.1) { |
okini3939 | 0:dc42aeff64e6 | 79 | return tbl_windvane[i][0]; |
okini3939 | 0:dc42aeff64e6 | 80 | } |
okini3939 | 0:dc42aeff64e6 | 81 | } |
okini3939 | 0:dc42aeff64e6 | 82 | return 0; |
okini3939 | 0:dc42aeff64e6 | 83 | } |
okini3939 | 0:dc42aeff64e6 | 84 | |
okini3939 | 1:fa262295fa7e | 85 | /** |
okini3939 | 1:fa262295fa7e | 86 | * @brief Get rain gauge |
okini3939 | 1:fa262295fa7e | 87 | * @return wind speed (mm/h) |
okini3939 | 1:fa262295fa7e | 88 | */ |
okini3939 | 0:dc42aeff64e6 | 89 | float WeatherMeters::get_raingauge() { |
okini3939 | 0:dc42aeff64e6 | 90 | return (float)raingauge * 0.2794; // 0.2794mm/Hz |
okini3939 | 0:dc42aeff64e6 | 91 | } |
okini3939 | 0:dc42aeff64e6 | 92 | |
okini3939 | 0:dc42aeff64e6 | 93 | void WeatherMeters::int_anemometer () { |
okini3939 | 0:dc42aeff64e6 | 94 | count_anemo ++; |
okini3939 | 0:dc42aeff64e6 | 95 | } |
okini3939 | 0:dc42aeff64e6 | 96 | |
okini3939 | 0:dc42aeff64e6 | 97 | void WeatherMeters::int_raingauge () { |
okini3939 | 0:dc42aeff64e6 | 98 | count_rain ++; |
okini3939 | 0:dc42aeff64e6 | 99 | } |
okini3939 | 0:dc42aeff64e6 | 100 | |
okini3939 | 0:dc42aeff64e6 | 101 | void WeatherMeters::int_timer () { |
okini3939 | 0:dc42aeff64e6 | 102 | int i, n; |
okini3939 | 0:dc42aeff64e6 | 103 | |
okini3939 | 0:dc42aeff64e6 | 104 | if (mode == Weather_auto) { |
okini3939 | 0:dc42aeff64e6 | 105 | n = 0; |
okini3939 | 0:dc42aeff64e6 | 106 | for (i = 11; i > 0; i --) { |
okini3939 | 0:dc42aeff64e6 | 107 | buf_anemo[i] = buf_anemo[i - 1]; |
okini3939 | 0:dc42aeff64e6 | 108 | n = n + buf_anemo[i]; |
okini3939 | 0:dc42aeff64e6 | 109 | } |
okini3939 | 0:dc42aeff64e6 | 110 | buf_anemo[0] = count_anemo; |
okini3939 | 0:dc42aeff64e6 | 111 | n = n + buf_anemo[0]; |
okini3939 | 0:dc42aeff64e6 | 112 | count_anemo = 0; |
okini3939 | 0:dc42aeff64e6 | 113 | if (n > windspeed) { |
okini3939 | 0:dc42aeff64e6 | 114 | windspeed = n; |
okini3939 | 0:dc42aeff64e6 | 115 | windvane = ain01; |
okini3939 | 0:dc42aeff64e6 | 116 | } |
okini3939 | 0:dc42aeff64e6 | 117 | } else { |
okini3939 | 0:dc42aeff64e6 | 118 | time_anemo ++; |
okini3939 | 0:dc42aeff64e6 | 119 | } |
okini3939 | 0:dc42aeff64e6 | 120 | |
okini3939 | 0:dc42aeff64e6 | 121 | time_rain ++; |
okini3939 | 0:dc42aeff64e6 | 122 | if (time_rain >= 4 * 300) { |
okini3939 | 0:dc42aeff64e6 | 123 | n = 0; |
okini3939 | 0:dc42aeff64e6 | 124 | for (i = 11; i > 0; i --) { |
okini3939 | 0:dc42aeff64e6 | 125 | buf_rain[i] = buf_rain[i - 1]; |
okini3939 | 0:dc42aeff64e6 | 126 | n = n + buf_rain[i]; |
okini3939 | 0:dc42aeff64e6 | 127 | } |
okini3939 | 0:dc42aeff64e6 | 128 | buf_rain[0] = count_rain; |
okini3939 | 0:dc42aeff64e6 | 129 | n = n + buf_rain[0]; |
okini3939 | 0:dc42aeff64e6 | 130 | count_rain = 0; |
okini3939 | 0:dc42aeff64e6 | 131 | time_rain = 0; |
okini3939 | 0:dc42aeff64e6 | 132 | raingauge = n; |
okini3939 | 0:dc42aeff64e6 | 133 | } |
okini3939 | 0:dc42aeff64e6 | 134 | } |