From 70d94133ea2fc6b3050932886aacd53a6d9514e4 Mon Sep 17 00:00:00 2001 From: Pere Tuset Date: Mon, 25 Apr 2016 01:50:23 +0200 Subject: [PATCH] Updated OpenMote example and platform. --- examples/openmote-cc2538/openmote-demo.c | 20 +++-- platform/openmote-cc2538/dev/adxl346.c | 100 ++++++++++++++++++++--- platform/openmote-cc2538/dev/adxl346.h | 10 ++- 3 files changed, 109 insertions(+), 21 deletions(-) diff --git a/examples/openmote-cc2538/openmote-demo.c b/examples/openmote-cc2538/openmote-demo.c index 46a301511..393203a0c 100644 --- a/examples/openmote-cc2538/openmote-demo.c +++ b/examples/openmote-cc2538/openmote-demo.c @@ -85,25 +85,29 @@ PROCESS_THREAD(openmote_demo_process, ev, data) static struct etimer et; static int16_t counter; static uint16_t adxl346_present, sht21_present, max44009_present; - static uint16_t accel, light, temperature, humidity; + static int16_t accel, light, temperature, humidity; PROCESS_EXITHANDLER(broadcast_close(&bc)) PROCESS_BEGIN(); - /* Initialize and activate the ADXL346 sensor */ + /* Initialize and calibrate the ADXL346 sensor */ adxl346_present = SENSORS_ACTIVATE(adxl346); if(adxl346_present == ADXL346_ERROR) { printf("ADXL346 sensor is NOT present!\n"); leds_on(LEDS_YELLOW); + } else { + adxl346.configure(ADXL346_CALIB_OFFSET, 0); } + /* Initialize the MAX44009 sensor */ max44009_present = SENSORS_ACTIVATE(max44009); if(max44009_present == MAX44009_ERROR) { printf("MAX44009 sensor is NOT present!\n"); leds_on(LEDS_ORANGE); } + /* Initialize the SHT21 sensor */ sht21_present = SENSORS_ACTIVATE(sht21); if(sht21_present == SHT21_ERROR) { printf("SHT21 sensor is NOT present!\n"); @@ -123,12 +127,12 @@ PROCESS_THREAD(openmote_demo_process, ev, data) if(ev == PROCESS_EVENT_TIMER) { if(adxl346_present != ADXL346_ERROR) { leds_on(LEDS_YELLOW); - accel = adxl346.value(ADXL346_READ_X); - printf("X Acceleration: %u\n", accel); - accel = adxl346.value(ADXL346_READ_Y); - printf("Y Acceleration: %u\n", accel); - accel = adxl346.value(ADXL346_READ_Z); - printf("Z Acceleration: %u\n", accel); + accel = adxl346.value(ADXL346_READ_X_mG); + printf("X Acceleration: %d.%u G\n", accel / 1000, accel % 1000); + accel = adxl346.value(ADXL346_READ_Y_mG); + printf("Y Acceleration: %d.%u G\n", accel / 1000, accel % 1000); + accel = adxl346.value(ADXL346_READ_Z_mG); + printf("Z Acceleration: %d.%u G\n", accel / 1000, accel % 1000); leds_off(LEDS_YELLOW); } diff --git a/platform/openmote-cc2538/dev/adxl346.c b/platform/openmote-cc2538/dev/adxl346.c index 7fb406051..09d725474 100644 --- a/platform/openmote-cc2538/dev/adxl346.c +++ b/platform/openmote-cc2538/dev/adxl346.c @@ -153,6 +153,9 @@ #define ADXL346_DATA_FORMAT_RANGE_PM_4g (1) #define ADXL346_DATA_FORMAT_RANGE_PM_8g (2) #define ADXL346_DATA_FORMAT_RANGE_PM_16g (3) + +#define ADXL346_USER_CONFIGURATION (ADXL346_DATA_FORMAT_RANGE_PM_2g) + /** @} */ /*---------------------------------------------------------------------------*/ static uint8_t enabled; @@ -163,13 +166,11 @@ adxl346_init(void) uint8_t config[2]; config[0] = ADXL346_BW_RATE_ADDR; - config[1] = (ADXL346_BW_RATE_RATE(11)); + config[1] = (ADXL346_BW_RATE_RATE(6)); i2c_burst_send(ADXL346_ADDRESS, config, sizeof(config)); config[0] = ADXL346_DATA_FORMAT_ADDR; - config[1] = (ADXL346_DATA_FORMAT_SELF_TEST | - ADXL346_DATA_FORMAT_FULL_RES | - ADXL346_DATA_FORMAT_RANGE_PM_16g); + config[1] = (ADXL346_USER_CONFIGURATION); i2c_burst_send(ADXL346_ADDRESS, config, sizeof(config)); config[0] = ADXL346_POWER_CTL_ADDR; @@ -185,23 +186,86 @@ adxl346_is_present(void) i2c_single_send(ADXL346_ADDRESS, ADXL346_DEVID_ADDR); i2c_single_receive(ADXL346_ADDRESS, &is_present); - return is_present == ADXL346_DEVID_VALUE; + return (is_present == ADXL346_DEVID_VALUE); } /*---------------------------------------------------------------------------*/ -static uint16_t +static int16_t adxl346_read_accel(uint8_t addr1, uint8_t addr2) { uint8_t acceleration[2]; - uint16_t z; + int16_t result; i2c_single_send(ADXL346_ADDRESS, addr1); i2c_single_receive(ADXL346_ADDRESS, &acceleration[0]); i2c_single_send(ADXL346_ADDRESS, addr2); i2c_single_receive(ADXL346_ADDRESS, &acceleration[1]); - z = (acceleration[0] << 8) | acceleration[1]; + result = (acceleration[1] << 8) | acceleration[0]; + + return result; +} +/*---------------------------------------------------------------------------*/ +static int16_t +adxl346_convert_accel(int16_t accel) +{ + int32_t result; + + result = (1000 * accel) / 256; + + return (int16_t) result; +} +/*---------------------------------------------------------------------------*/ +static void +adxl346_calibrate_offset(void) +{ + int32_t accum_x = 0; + int32_t accum_y = 0; + int32_t accum_z = 0; + uint8_t config[2]; + int8_t offset; + + config[0] = ADXL346_OFSX_ADDR; + config[1] = 0; + i2c_burst_send(ADXL346_ADDRESS, config, sizeof(config)); + config[0] = ADXL346_OFSY_ADDR; + config[1] = 0; + i2c_burst_send(ADXL346_ADDRESS, config, sizeof(config)); + config[0] = ADXL346_OFSZ_ADDR; + config[1] = 0; + i2c_burst_send(ADXL346_ADDRESS, config, sizeof(config)); + + uint16_t i; + for (i = 0; i < 100; i++) { + uint16_t x, y, z; + + x = adxl346_read_accel(ADXL346_DATAX0_ADDR, ADXL346_DATAX1_ADDR); + accum_x += x; + + y = adxl346_read_accel(ADXL346_DATAY0_ADDR, ADXL346_DATAY1_ADDR); + accum_y += y; + + z = adxl346_read_accel(ADXL346_DATAZ0_ADDR, ADXL346_DATAZ1_ADDR); + accum_z += z; + } + + offset = (64 * accum_x) / 25600; + config[0] = ADXL346_OFSX_ADDR; + config[1] = -offset; + i2c_burst_send(ADXL346_ADDRESS, config, sizeof(config)); + PRINTF("ADXL346: X calibration offset is %d\n", offset); + + offset = (64 * accum_y) / 25600; + config[0] = ADXL346_OFSY_ADDR; + config[1] = -offset; + i2c_burst_send(ADXL346_ADDRESS, config, sizeof(config)); + PRINTF("ADXL346: Y calibration offset is %d\n", offset); + + offset = (64 * accum_z) / 25600; + config[0] = ADXL346_OFSZ_ADDR; + config[1] = -offset; + i2c_burst_send(ADXL346_ADDRESS, config, sizeof(config)); + PRINTF("ADXL346: Z calibration offset is %d\n", offset); - return z; } /*---------------------------------------------------------------------------*/ static int @@ -218,6 +282,7 @@ status(int type) static int value(int type) { + int16_t accel; if(!enabled) { PRINTF("ADXL346: sensor not started\n"); return ADXL346_ERROR; @@ -225,10 +290,19 @@ value(int type) if(type == ADXL346_READ_X) { return adxl346_read_accel(ADXL346_DATAX0_ADDR, ADXL346_DATAX1_ADDR); - } else if(type == ADXL346_READ_Y) { + } else if(type == ADXL346_READ_Y) { return adxl346_read_accel(ADXL346_DATAY0_ADDR, ADXL346_DATAY1_ADDR); } else if(type == ADXL346_READ_Z) { return adxl346_read_accel(ADXL346_DATAZ0_ADDR, ADXL346_DATAZ1_ADDR); + } else if(type == ADXL346_READ_X_mG) { + accel = adxl346_read_accel(ADXL346_DATAX0_ADDR, ADXL346_DATAX1_ADDR); + return adxl346_convert_accel(accel); + } else if(type == ADXL346_READ_Y_mG) { + accel = adxl346_read_accel(ADXL346_DATAY0_ADDR, ADXL346_DATAY1_ADDR); + return adxl346_convert_accel(accel); + } else if(type == ADXL346_READ_Z_mG) { + accel = adxl346_read_accel(ADXL346_DATAZ0_ADDR, ADXL346_DATAZ1_ADDR); + return adxl346_convert_accel(accel); } else { PRINTF("ADXL346: invalid value requested\n"); return ADXL346_ERROR; @@ -243,6 +317,7 @@ configure(int type, int value) if(type == ADXL346_ACTIVATE) { if(!adxl346_is_present()) { PRINTF("ADXL346: is not present\n"); + enabled = 0; return ADXL346_ERROR; } else { adxl346_init(); @@ -251,6 +326,11 @@ configure(int type, int value) } } + if(type == ADXL346_CALIB_OFFSET && enabled) { + adxl346_calibrate_offset(); + return ADXL346_SUCCESS; + } + return ADXL346_ERROR; } /*---------------------------------------------------------------------------*/ diff --git a/platform/openmote-cc2538/dev/adxl346.h b/platform/openmote-cc2538/dev/adxl346.h index acd0d21f5..401e601d1 100644 --- a/platform/openmote-cc2538/dev/adxl346.h +++ b/platform/openmote-cc2538/dev/adxl346.h @@ -51,9 +51,13 @@ #define ADXL346_SUCCESS (0) #define ADXL346_ACTIVATE (SENSORS_ACTIVE) #define ADXL346_READ_X (2) -#define ADXL346_READ_Y (3) -#define ADXL346_READ_Z (4) -#define ADXL346_NONE (5) +#define ADXL346_READ_X_mG (3) +#define ADXL346_READ_Y (4) +#define ADXL346_READ_Y_mG (5) +#define ADXL346_READ_Z (6) +#define ADXL346_READ_Z_mG (7) +#define ADXL346_CALIB_OFFSET (8) +#define ADXL346_NONE (9) /*---------------------------------------------------------------------------*/ #define ADXL346_SENSOR "ADXL346 Sensor" /*---------------------------------------------------------------------------*/