Fork of https://os.mbed.com/users/okini3939/code/WeatherMeters/

Committer:
okini3939
Date:
Thu Oct 14 11:58:22 2010 +0000
Revision:
0:dc42aeff64e6
Child:
1:fa262295fa7e

        

Who changed what in which revision?

UserRevisionLine numberNew 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 0:dc42aeff64e6 7 #include "mbed.h"
okini3939 0:dc42aeff64e6 8 #include "WeatherMeters.h"
okini3939 0:dc42aeff64e6 9
okini3939 0:dc42aeff64e6 10 #define WEATHER_VREF 3.3
okini3939 0:dc42aeff64e6 11
okini3939 0:dc42aeff64e6 12 const float tbl_windvane[16][2] = {
okini3939 0:dc42aeff64e6 13 {0.0, 33000}, {22.5, 6570}, {45.0, 8200}, {67.5, 891},
okini3939 0:dc42aeff64e6 14 {90.0, 1000}, {112.5, 688}, {135.0, 2200}, {157.5, 1410},
okini3939 0:dc42aeff64e6 15 {180.0, 3900}, {202.5, 3140}, {225.0, 16000}, {247.5, 14120},
okini3939 0:dc42aeff64e6 16 {270.0, 120000}, {292.5, 42120}, {315.0, 64900}, {337.5, 21880}
okini3939 0:dc42aeff64e6 17 };
okini3939 0:dc42aeff64e6 18
okini3939 0:dc42aeff64e6 19 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 20 int i;
okini3939 0:dc42aeff64e6 21
okini3939 0:dc42aeff64e6 22 windspeed = 0; windvane = 0; raingauge = 0;
okini3939 0:dc42aeff64e6 23 count_anemo = 0; count_rain = 0; time_anemo = 0; time_rain = 0;
okini3939 0:dc42aeff64e6 24 for (i = 0; i < 12; i ++) {
okini3939 0:dc42aeff64e6 25 buf_anemo[i] = 0;
okini3939 0:dc42aeff64e6 26 buf_rain[i] = 0;
okini3939 0:dc42aeff64e6 27 }
okini3939 0:dc42aeff64e6 28
okini3939 0:dc42aeff64e6 29 mode = w_mode;
okini3939 0:dc42aeff64e6 30 int01.fall(this, &WeatherMeters::int_anemometer);
okini3939 0:dc42aeff64e6 31 int02.fall(this, &WeatherMeters::int_raingauge);
okini3939 0:dc42aeff64e6 32 ticker01.attach(this, &WeatherMeters::int_timer, 0.25);
okini3939 0:dc42aeff64e6 33 }
okini3939 0:dc42aeff64e6 34
okini3939 0:dc42aeff64e6 35 float WeatherMeters::get_windspeed() {
okini3939 0:dc42aeff64e6 36 float s;
okini3939 0:dc42aeff64e6 37
okini3939 0:dc42aeff64e6 38 if (mode == Weather_auto) {
okini3939 0:dc42aeff64e6 39 s = (float)windspeed * 0.667 / 3.0; // 0.667m/s/Hz (/3s)
okini3939 0:dc42aeff64e6 40 windspeed = 0;
okini3939 0:dc42aeff64e6 41 } else {
okini3939 0:dc42aeff64e6 42 s = (float)count_anemo * 0.667 / ((float)time_anemo / 4.0); // 0.667m/s/Hz
okini3939 0:dc42aeff64e6 43 time_anemo = 0;
okini3939 0:dc42aeff64e6 44 count_anemo = 0;
okini3939 0:dc42aeff64e6 45 }
okini3939 0:dc42aeff64e6 46 return s;
okini3939 0:dc42aeff64e6 47 }
okini3939 0:dc42aeff64e6 48
okini3939 0:dc42aeff64e6 49 float WeatherMeters::get_windvane() {
okini3939 0:dc42aeff64e6 50 int i;
okini3939 0:dc42aeff64e6 51 float v;
okini3939 0:dc42aeff64e6 52
okini3939 0:dc42aeff64e6 53 if (windspeed == 0) windvane = ain01;
okini3939 0:dc42aeff64e6 54 v = windvane * WEATHER_VREF; // V
okini3939 0:dc42aeff64e6 55 v = v / ((WEATHER_VREF - v) / 10000.0); // ohm
okini3939 0:dc42aeff64e6 56 for (i = 0; i < 16; i ++) {
okini3939 0:dc42aeff64e6 57 if (v > tbl_windvane[i][1] * 0.9 && v < tbl_windvane[i][1] * 1.1) {
okini3939 0:dc42aeff64e6 58 return tbl_windvane[i][0];
okini3939 0:dc42aeff64e6 59 }
okini3939 0:dc42aeff64e6 60 }
okini3939 0:dc42aeff64e6 61 return 0;
okini3939 0:dc42aeff64e6 62 }
okini3939 0:dc42aeff64e6 63
okini3939 0:dc42aeff64e6 64 float WeatherMeters::get_raingauge() {
okini3939 0:dc42aeff64e6 65 return (float)raingauge * 0.2794; // 0.2794mm/Hz
okini3939 0:dc42aeff64e6 66 }
okini3939 0:dc42aeff64e6 67
okini3939 0:dc42aeff64e6 68 void WeatherMeters::int_anemometer () {
okini3939 0:dc42aeff64e6 69 count_anemo ++;
okini3939 0:dc42aeff64e6 70 }
okini3939 0:dc42aeff64e6 71
okini3939 0:dc42aeff64e6 72 void WeatherMeters::int_raingauge () {
okini3939 0:dc42aeff64e6 73 count_rain ++;
okini3939 0:dc42aeff64e6 74 }
okini3939 0:dc42aeff64e6 75
okini3939 0:dc42aeff64e6 76 void WeatherMeters::int_timer () {
okini3939 0:dc42aeff64e6 77 int i, n;
okini3939 0:dc42aeff64e6 78
okini3939 0:dc42aeff64e6 79 if (mode == Weather_auto) {
okini3939 0:dc42aeff64e6 80 n = 0;
okini3939 0:dc42aeff64e6 81 for (i = 11; i > 0; i --) {
okini3939 0:dc42aeff64e6 82 buf_anemo[i] = buf_anemo[i - 1];
okini3939 0:dc42aeff64e6 83 n = n + buf_anemo[i];
okini3939 0:dc42aeff64e6 84 }
okini3939 0:dc42aeff64e6 85 buf_anemo[0] = count_anemo;
okini3939 0:dc42aeff64e6 86 n = n + buf_anemo[0];
okini3939 0:dc42aeff64e6 87 count_anemo = 0;
okini3939 0:dc42aeff64e6 88 if (n > windspeed) {
okini3939 0:dc42aeff64e6 89 windspeed = n;
okini3939 0:dc42aeff64e6 90 windvane = ain01;
okini3939 0:dc42aeff64e6 91 }
okini3939 0:dc42aeff64e6 92 } else {
okini3939 0:dc42aeff64e6 93 time_anemo ++;
okini3939 0:dc42aeff64e6 94 }
okini3939 0:dc42aeff64e6 95
okini3939 0:dc42aeff64e6 96 time_rain ++;
okini3939 0:dc42aeff64e6 97 if (time_rain >= 4 * 300) {
okini3939 0:dc42aeff64e6 98 n = 0;
okini3939 0:dc42aeff64e6 99 for (i = 11; i > 0; i --) {
okini3939 0:dc42aeff64e6 100 buf_rain[i] = buf_rain[i - 1];
okini3939 0:dc42aeff64e6 101 n = n + buf_rain[i];
okini3939 0:dc42aeff64e6 102 }
okini3939 0:dc42aeff64e6 103 buf_rain[0] = count_rain;
okini3939 0:dc42aeff64e6 104 n = n + buf_rain[0];
okini3939 0:dc42aeff64e6 105 count_rain = 0;
okini3939 0:dc42aeff64e6 106 time_rain = 0;
okini3939 0:dc42aeff64e6 107 raingauge = n;
okini3939 0:dc42aeff64e6 108 }
okini3939 0:dc42aeff64e6 109 }