Added interrupt configuration for the weather meter

This commit is contained in:
Antonio Lignan 2016-01-17 02:22:19 +01:00
parent 103911f619
commit aacbcd904f
3 changed files with 155 additions and 82 deletions

View file

@ -55,7 +55,9 @@
#include <stdio.h> #include <stdio.h>
#include <stdint.h> #include <stdint.h>
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
#define READ_SENSOR_PERIOD CLOCK_SECOND #define READ_SENSOR_PERIOD CLOCK_SECOND
#define ANEMOMETER_THRESHOLD_TICK 8
#define RAIN_GAUGE_THRESHOLD_TICK 15
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
static struct etimer et; static struct etimer et;
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
@ -63,15 +65,17 @@ PROCESS(test_weather_meter_sensors, "Test Weather meter sensors");
AUTOSTART_PROCESSES(&test_weather_meter_sensors); AUTOSTART_PROCESSES(&test_weather_meter_sensors);
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
static void static void
rain_callback(uint8_t value) rain_callback(uint16_t value)
{ {
printf("*** Rain\n"); printf("*** Rain gauge over threshold (%u ticks)\n", value);
weather_meter.configure(WEATHER_METER_RAIN_GAUGE_INT_OVER,
(value + RAIN_GAUGE_THRESHOLD_TICK));
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
static void static void
wind_speed_callback(uint8_t value) wind_speed_callback(uint16_t value)
{ {
printf("*** Wind speed\n"); printf("*** Wind speed over threshold (%u ticks)\n", value);
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
PROCESS_THREAD(test_weather_meter_sensors, ev, data) PROCESS_THREAD(test_weather_meter_sensors, ev, data)
@ -86,8 +90,17 @@ PROCESS_THREAD(test_weather_meter_sensors, ev, data)
WEATHER_METER_REGISTER_ANEMOMETER_INT(wind_speed_callback); WEATHER_METER_REGISTER_ANEMOMETER_INT(wind_speed_callback);
WEATHER_METER_REGISTER_RAIN_GAUGE_INT(rain_callback); WEATHER_METER_REGISTER_RAIN_GAUGE_INT(rain_callback);
/* Enable the sensors, this has to be called before any of the interrupt calls
* like the ones below
*/
SENSORS_ACTIVATE(weather_meter); SENSORS_ACTIVATE(weather_meter);
/* And the upper threshold value to compare and generate an interrupt */
weather_meter.configure(WEATHER_METER_ANEMOMETER_INT_OVER,
ANEMOMETER_THRESHOLD_TICK);
weather_meter.configure(WEATHER_METER_RAIN_GAUGE_INT_OVER,
RAIN_GAUGE_THRESHOLD_TICK);
etimer_set(&et, READ_SENSOR_PERIOD); etimer_set(&et, READ_SENSOR_PERIOD);
while(1) { while(1) {
@ -100,17 +113,17 @@ PROCESS_THREAD(test_weather_meter_sensors, ev, data)
#if WEATHER_METER_RAIN_RETURN_TICKS #if WEATHER_METER_RAIN_RETURN_TICKS
rain *= WEATHER_METER_AUX_RAIN_MM; rain *= WEATHER_METER_AUX_RAIN_MM;
if(rain > (WEATHER_METER_AUX_RAIN_MM * 3)) { if(rain > (WEATHER_METER_AUX_RAIN_MM * 3)) {
printf("Rain: %lu.%lu mm\n", (rain / 10000), (rain % 10000)); printf("Rain: %lu.%lu mm, ", (rain / 10000), (rain % 10000));
#else #else
if(rain >= 10) { if(rain >= 10) {
printf("Rain: %u.%u mm\n", (rain / 10), (rain % 10)); printf("Rain: %u.%u mm, ", (rain / 10), (rain % 10));
#endif #endif
} else { } else {
printf("Rain: 0.%lu mm\n", rain); printf("Rain: 0.%lu mm, ", rain);
} }
printf("Wind direction: %u metres/hour\n", wind_dir); printf("Wind (dir: %u deg, ", wind_dir);
printf("Wind speed %u\n", wind_speed); printf("speed %u m/h)\n", wind_speed);
etimer_reset(&et); etimer_reset(&et);
} }

View file

@ -47,6 +47,7 @@
#include "contiki.h" #include "contiki.h"
#include "dev/adc-sensors.h" #include "dev/adc-sensors.h"
#include "dev/weather-meter.h" #include "dev/weather-meter.h"
#include "dev/zoul-sensors.h"
#include "lib/sensors.h" #include "lib/sensors.h"
#include "dev/sys-ctrl.h" #include "dev/sys-ctrl.h"
#include "dev/gpio.h" #include "dev/gpio.h"
@ -54,22 +55,24 @@
#include "sys/timer.h" #include "sys/timer.h"
#include "sys/etimer.h" #include "sys/etimer.h"
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
#define DEBUG 1 #define DEBUG 0
#if DEBUG #if DEBUG
#define PRINTF(...) printf(__VA_ARGS__) #define PRINTF(...) printf(__VA_ARGS__)
#else #else
#define PRINTF(...) #define PRINTF(...)
#endif #endif
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
#define DEBOUNCE_DURATION (CLOCK_SECOND >> 6) #define DEBOUNCE_DURATION (CLOCK_SECOND >> 4)
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
#define ANEMOMETER_SENSOR_PORT_BASE GPIO_PORT_TO_BASE(ANEMOMETER_SENSOR_PORT) #define ANEMOMETER_SENSOR_PORT_BASE GPIO_PORT_TO_BASE(ANEMOMETER_SENSOR_PORT)
#define ANEMOMETER_SENSOR_PIN_MASK GPIO_PIN_MASK(ANEMOMETER_SENSOR_PIN) #define ANEMOMETER_SENSOR_PIN_MASK GPIO_PIN_MASK(ANEMOMETER_SENSOR_PIN)
#define RAIN_GAUGE_SENSOR_PORT_BASE GPIO_PORT_TO_BASE(RAIN_GAUGE_SENSOR_PORT) #define RAIN_GAUGE_SENSOR_PORT_BASE GPIO_PORT_TO_BASE(RAIN_GAUGE_SENSOR_PORT)
#define RAIN_GAUGE_SENSOR_PIN_MASK GPIO_PIN_MASK(RAIN_GAUGE_SENSOR_PIN) #define RAIN_GAUGE_SENSOR_PIN_MASK GPIO_PIN_MASK(RAIN_GAUGE_SENSOR_PIN)
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
void (*rain_gauge_int_callback)(uint8_t value); void (*rain_gauge_int_callback)(uint16_t value);
void (*anemometer_int_callback)(uint8_t value); void (*anemometer_int_callback)(uint16_t value);
/*---------------------------------------------------------------------------*/
static uint8_t enabled;
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
process_event_t anemometer_int_event; process_event_t anemometer_int_event;
process_event_t rain_gauge_int_event; process_event_t rain_gauge_int_event;
@ -80,12 +83,14 @@ static struct timer debouncetimer;
typedef struct { typedef struct {
uint16_t ticks; uint16_t ticks;
uint16_t value; uint16_t value;
} a_ticking_sensor_t; uint8_t int_en;
uint16_t int_thres;
} weather_meter_sensors_t;
typedef struct { typedef struct {
uint16_t wind_vane; uint16_t wind_vane;
a_ticking_sensor_t rain_gauge; weather_meter_sensors_t rain_gauge;
a_ticking_sensor_t anemometer; weather_meter_sensors_t anemometer;
} weather_meter_sensors; } weather_meter_sensors;
static weather_meter_sensors weather_sensors; static weather_meter_sensors weather_sensors;
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
@ -103,9 +108,19 @@ PROCESS_THREAD(weather_meter_int_process, ev, data)
while(1) { while(1) {
PROCESS_YIELD(); PROCESS_YIELD();
/* The anemometer ticks twice per rotation, and a wind speed of 2.4 km/h if((ev == anemometer_int_event) && (weather_sensors.anemometer.int_en)) {
* makes the switch close every second, convert RPM to linear velocity if(weather_sensors.anemometer.ticks >=
*/ weather_sensors.anemometer.int_thres) {
anemometer_int_callback(weather_sensors.anemometer.ticks);
}
}
if((ev == rain_gauge_int_event) && (weather_sensors.rain_gauge.int_en)) {
if(weather_sensors.rain_gauge.ticks >=
weather_sensors.rain_gauge.int_thres) {
rain_gauge_int_callback(weather_sensors.rain_gauge.ticks);
}
}
if(ev == PROCESS_EVENT_TIMER) { if(ev == PROCESS_EVENT_TIMER) {
@ -113,9 +128,10 @@ PROCESS_THREAD(weather_meter_int_process, ev, data)
GPIO_DISABLE_INTERRUPT(ANEMOMETER_SENSOR_PORT_BASE, GPIO_DISABLE_INTERRUPT(ANEMOMETER_SENSOR_PORT_BASE,
ANEMOMETER_SENSOR_PIN_MASK); ANEMOMETER_SENSOR_PIN_MASK);
/* Two ticks per rotation */ /* The anemometer ticks twice per rotation, and a wind speed of 2.4 km/h
* makes the switch close every second, convert RPM to linear velocity
*/
rpm = weather_sensors.anemometer.ticks * 30; rpm = weather_sensors.anemometer.ticks * 30;
mph = rpm * WEATHER_METER_AUX_ANGULAR; mph = rpm * WEATHER_METER_AUX_ANGULAR;
mph /= 1000; mph /= 1000;
@ -130,9 +146,6 @@ PROCESS_THREAD(weather_meter_int_process, ev, data)
ANEMOMETER_SENSOR_PIN_MASK); ANEMOMETER_SENSOR_PIN_MASK);
etimer_restart(&et); etimer_restart(&et);
} }
// anemometer_int_callback(0);
// rain_gauge_int_callback(0);
} }
PROCESS_END(); PROCESS_END();
} }
@ -175,6 +188,11 @@ value(int type)
return WEATHER_METER_ERROR; return WEATHER_METER_ERROR;
} }
if(!enabled) {
PRINTF("Weather: module is not configured\n");
return WEATHER_METER_ERROR;
}
switch(type) { switch(type) {
case WEATHER_METER_WIND_VANE: case WEATHER_METER_WIND_VANE:
/* FIXME: return the values in degrees */ /* FIXME: return the values in degrees */
@ -203,56 +221,92 @@ value(int type)
static int static int
configure(int type, int value) configure(int type, int value)
{ {
if(type != WEATHER_METER_ACTIVE) { if((type != WEATHER_METER_ACTIVE) &&
(type != WEATHER_METER_ANEMOMETER_INT_OVER) &&
(type != WEATHER_METER_RAIN_GAUGE_INT_OVER) &&
(type != WEATHER_METER_ANEMOMETER_INT_DIS) &&
(type != WEATHER_METER_RAIN_GAUGE_INT_DIS)) {
PRINTF("Weather: invalid configuration option\n"); PRINTF("Weather: invalid configuration option\n");
return WEATHER_METER_ERROR; return WEATHER_METER_ERROR;
} }
weather_sensors.wind_vane = 0; if(type == WEATHER_METER_ACTIVE) {
weather_sensors.anemometer.ticks = 0;
weather_sensors.anemometer.value = 0;
weather_sensors.rain_gauge.ticks = 0;
weather_sensors.rain_gauge.value = 0;
if(!value) { weather_sensors.anemometer.int_en = 0;
anemometer_int_callback = NULL; weather_sensors.rain_gauge.int_en = 0;
rain_gauge_int_callback = NULL; weather_sensors.anemometer.ticks = 0;
GPIO_DISABLE_INTERRUPT(ANEMOMETER_SENSOR_PORT_BASE, weather_sensors.rain_gauge.ticks = 0;
ANEMOMETER_SENSOR_PIN_MASK);
GPIO_DISABLE_INTERRUPT(RAIN_GAUGE_SENSOR_PORT_BASE, if(!value) {
RAIN_GAUGE_SENSOR_PIN_MASK); anemometer_int_callback = NULL;
rain_gauge_int_callback = NULL;
GPIO_DISABLE_INTERRUPT(ANEMOMETER_SENSOR_PORT_BASE,
ANEMOMETER_SENSOR_PIN_MASK);
GPIO_DISABLE_INTERRUPT(RAIN_GAUGE_SENSOR_PORT_BASE,
RAIN_GAUGE_SENSOR_PIN_MASK);
process_exit(&weather_meter_int_process);
enabled = 0;
PRINTF("Weather: disabled\n");
return WEATHER_METER_SUCCESS;
}
/* Configure the wind vane */
adc_sensors.configure(SENSORS_HW_INIT, WIND_VANE_ADC);
/* Configure anemometer interruption */
GPIO_SOFTWARE_CONTROL(ANEMOMETER_SENSOR_PORT_BASE, ANEMOMETER_SENSOR_PIN_MASK);
GPIO_SET_INPUT(ANEMOMETER_SENSOR_PORT_BASE, ANEMOMETER_SENSOR_PIN_MASK);
GPIO_DETECT_RISING(ANEMOMETER_SENSOR_PORT_BASE, ANEMOMETER_SENSOR_PIN_MASK);
GPIO_TRIGGER_SINGLE_EDGE(ANEMOMETER_SENSOR_PORT_BASE,
ANEMOMETER_SENSOR_PIN_MASK);
ioc_set_over(ANEMOMETER_SENSOR_PORT, ANEMOMETER_SENSOR_PIN, IOC_OVERRIDE_DIS);
gpio_register_callback(weather_meter_interrupt_handler, ANEMOMETER_SENSOR_PORT,
ANEMOMETER_SENSOR_PIN);
/* Configure rain gauge interruption */
GPIO_SOFTWARE_CONTROL(RAIN_GAUGE_SENSOR_PORT_BASE, RAIN_GAUGE_SENSOR_PIN_MASK);
GPIO_SET_INPUT(RAIN_GAUGE_SENSOR_PORT_BASE, RAIN_GAUGE_SENSOR_PIN_MASK);
GPIO_DETECT_RISING(RAIN_GAUGE_SENSOR_PORT_BASE, RAIN_GAUGE_SENSOR_PIN_MASK);
GPIO_TRIGGER_SINGLE_EDGE(RAIN_GAUGE_SENSOR_PORT_BASE,
RAIN_GAUGE_SENSOR_PIN_MASK);
ioc_set_over(RAIN_GAUGE_SENSOR_PORT, RAIN_GAUGE_SENSOR_PIN, IOC_OVERRIDE_DIS);
gpio_register_callback(weather_meter_interrupt_handler, RAIN_GAUGE_SENSOR_PORT,
RAIN_GAUGE_SENSOR_PIN);
process_start(&weather_meter_int_process, NULL);
GPIO_ENABLE_INTERRUPT(ANEMOMETER_SENSOR_PORT_BASE, ANEMOMETER_SENSOR_PIN_MASK);
GPIO_ENABLE_INTERRUPT(RAIN_GAUGE_SENSOR_PORT_BASE, RAIN_GAUGE_SENSOR_PIN_MASK);
nvic_interrupt_enable(ANEMOMETER_SENSOR_VECTOR);
nvic_interrupt_enable(RAIN_GAUGE_SENSOR_VECTOR);
enabled = 1;
PRINTF("Weather: started\n");
return WEATHER_METER_SUCCESS; return WEATHER_METER_SUCCESS;
} }
/* Configure the wind vane */ switch(type) {
adc_sensors.configure(SENSORS_HW_INIT, WIND_VANE_ADC); case WEATHER_METER_ANEMOMETER_INT_OVER:
weather_sensors.anemometer.int_en = 1;
/* Configure anemometer interruption */ weather_sensors.anemometer.int_thres = value;
GPIO_SOFTWARE_CONTROL(ANEMOMETER_SENSOR_PORT_BASE, ANEMOMETER_SENSOR_PIN_MASK); PRINTF("Weather: anemometer threshold %u\n", value);
GPIO_SET_INPUT(ANEMOMETER_SENSOR_PORT_BASE, ANEMOMETER_SENSOR_PIN_MASK); break;
GPIO_DETECT_RISING(ANEMOMETER_SENSOR_PORT_BASE, ANEMOMETER_SENSOR_PIN_MASK); case WEATHER_METER_RAIN_GAUGE_INT_OVER:
GPIO_TRIGGER_SINGLE_EDGE(ANEMOMETER_SENSOR_PORT_BASE, weather_sensors.rain_gauge.int_en = 1;
ANEMOMETER_SENSOR_PIN_MASK); weather_sensors.rain_gauge.int_thres = value;
ioc_set_over(ANEMOMETER_SENSOR_PORT, ANEMOMETER_SENSOR_PIN, IOC_OVERRIDE_DIS); PRINTF("Weather: rain gauge threshold %u\n", value);
gpio_register_callback(weather_meter_interrupt_handler, ANEMOMETER_SENSOR_PORT, break;
ANEMOMETER_SENSOR_PIN); case WEATHER_METER_ANEMOMETER_INT_DIS:
PRINTF("Weather: anemometer int disabled\n");
/* Configure rain gauge interruption */ weather_sensors.anemometer.int_en = 0;
GPIO_SOFTWARE_CONTROL(RAIN_GAUGE_SENSOR_PORT_BASE, RAIN_GAUGE_SENSOR_PIN_MASK); break;
GPIO_SET_INPUT(RAIN_GAUGE_SENSOR_PORT_BASE, RAIN_GAUGE_SENSOR_PIN_MASK); case WEATHER_METER_RAIN_GAUGE_INT_DIS:
GPIO_DETECT_RISING(RAIN_GAUGE_SENSOR_PORT_BASE, RAIN_GAUGE_SENSOR_PIN_MASK); PRINTF("Weather: rain gauge int disabled\n");
GPIO_TRIGGER_SINGLE_EDGE(RAIN_GAUGE_SENSOR_PORT_BASE, weather_sensors.rain_gauge.int_en = 0;
RAIN_GAUGE_SENSOR_PIN_MASK); break;
ioc_set_over(RAIN_GAUGE_SENSOR_PORT, RAIN_GAUGE_SENSOR_PIN, IOC_OVERRIDE_DIS); default:
gpio_register_callback(weather_meter_interrupt_handler, RAIN_GAUGE_SENSOR_PORT, return WEATHER_METER_ERROR;
RAIN_GAUGE_SENSOR_PIN); }
process_start(&weather_meter_int_process, NULL);
GPIO_ENABLE_INTERRUPT(ANEMOMETER_SENSOR_PORT_BASE, ANEMOMETER_SENSOR_PIN_MASK);
GPIO_ENABLE_INTERRUPT(RAIN_GAUGE_SENSOR_PORT_BASE, RAIN_GAUGE_SENSOR_PIN_MASK);
nvic_interrupt_enable(ANEMOMETER_SENSOR_VECTOR);
nvic_interrupt_enable(RAIN_GAUGE_SENSOR_VECTOR);
return WEATHER_METER_SUCCESS; return WEATHER_METER_SUCCESS;
} }

View file

@ -44,6 +44,7 @@
*/ */
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
#include "lib/sensors.h" #include "lib/sensors.h"
#include "dev/zoul-sensors.h"
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
#ifndef WEATHER_METER_H_ #ifndef WEATHER_METER_H_
#define WEATHER_METER_H_ #define WEATHER_METER_H_
@ -52,24 +53,29 @@
* \name Weather meter sensor return and operation values * \name Weather meter sensor return and operation values
* @{ * @{
*/ */
#define WEATHER_METER_ANEMOMETER 0x00 #define WEATHER_METER_ANEMOMETER 0x00
#define WEATHER_METER_RAIN_GAUGE 0x01 #define WEATHER_METER_RAIN_GAUGE 0x01
#define WEATHER_METER_WIND_VANE 0x02 #define WEATHER_METER_WIND_VANE 0x02
#define WEATHER_METER_ACTIVE SENSORS_ACTIVE #define WEATHER_METER_ACTIVE SENSORS_ACTIVE
#define WEATHER_METER_SUCCESS 0 #define WEATHER_METER_ANEMOMETER_INT_OVER HW_INT_OVER_THRS
#define WEATHER_METER_ERROR (-1) #define WEATHER_METER_ANEMOMETER_INT_DIS HW_INT_DISABLE
#define WEATHER_METER_RAIN_GAUGE_INT_OVER (HW_INT_OVER_THRS << 1)
#define WEATHER_METER_RAIN_GAUGE_INT_DIS (HW_INT_DISABLE << 1)
#define WEATHER_METER_ANEMOMETER_RADIUS 65 /**< 65.2 mm pin to cup centre */ #define WEATHER_METER_SUCCESS 0
#define WEATHER_METER_AUX_CAL 377 /**< 2*pi*60 (376.992 rounded) */ #define WEATHER_METER_ERROR (-1)
#define WEATHER_METER_AUX_ANGULAR (WEATHER_METER_ANEMOMETER_RADIUS * \
WEATHER_METER_AUX_CAL) #define WEATHER_METER_ANEMOMETER_RADIUS 65 /**< 65.2 mm pin to cup centre */
#define WEATHER_METER_AUX_RAIN_MM 2794 /**< 0.2794mm per tick */ #define WEATHER_METER_AUX_CAL 377 /**< 2*pi*60 (376.992 rounded) */
#define WEATHER_METER_AUX_ANGULAR (WEATHER_METER_ANEMOMETER_RADIUS * \
WEATHER_METER_AUX_CAL)
#define WEATHER_METER_AUX_RAIN_MM 2794 /**< 0.2794mm per tick */
#ifdef WEATHER_METER_RAIN_CONF_RETURN #ifdef WEATHER_METER_RAIN_CONF_RETURN
#define WEATHER_METER_RAIN_RETURN_TICKS WEATHER_METER_RAIN_CONF_RETURN #define WEATHER_METER_RAIN_RETURN_TICKS WEATHER_METER_RAIN_CONF_RETURN
#else #else
#define WEATHER_METER_RAIN_RETURN_TICKS 1 #define WEATHER_METER_RAIN_RETURN_TICKS 1
#endif #endif
/** @} */ /** @} */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
@ -79,8 +85,8 @@
*/ */
#define WEATHER_METER_REGISTER_ANEMOMETER_INT(ptr) anemometer_int_callback = ptr; #define WEATHER_METER_REGISTER_ANEMOMETER_INT(ptr) anemometer_int_callback = ptr;
#define WEATHER_METER_REGISTER_RAIN_GAUGE_INT(ptr) rain_gauge_int_callback = ptr; #define WEATHER_METER_REGISTER_RAIN_GAUGE_INT(ptr) rain_gauge_int_callback = ptr;
extern void (*anemometer_int_callback)(uint8_t value); extern void (*anemometer_int_callback)(uint16_t value);
extern void (*rain_gauge_int_callback)(uint8_t value); extern void (*rain_gauge_int_callback)(uint16_t value);
/** @} */ /** @} */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/** /**