Updated OpenMote example and platform.

This commit is contained in:
Pere Tuset 2016-04-25 01:50:23 +02:00
parent d9f4d97a41
commit 70d94133ea
3 changed files with 109 additions and 21 deletions

View File

@ -85,25 +85,29 @@ PROCESS_THREAD(openmote_demo_process, ev, data)
static struct etimer et; static struct etimer et;
static int16_t counter; static int16_t counter;
static uint16_t adxl346_present, sht21_present, max44009_present; 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_EXITHANDLER(broadcast_close(&bc))
PROCESS_BEGIN(); PROCESS_BEGIN();
/* Initialize and activate the ADXL346 sensor */ /* Initialize and calibrate the ADXL346 sensor */
adxl346_present = SENSORS_ACTIVATE(adxl346); adxl346_present = SENSORS_ACTIVATE(adxl346);
if(adxl346_present == ADXL346_ERROR) { if(adxl346_present == ADXL346_ERROR) {
printf("ADXL346 sensor is NOT present!\n"); printf("ADXL346 sensor is NOT present!\n");
leds_on(LEDS_YELLOW); leds_on(LEDS_YELLOW);
} else {
adxl346.configure(ADXL346_CALIB_OFFSET, 0);
} }
/* Initialize the MAX44009 sensor */
max44009_present = SENSORS_ACTIVATE(max44009); max44009_present = SENSORS_ACTIVATE(max44009);
if(max44009_present == MAX44009_ERROR) { if(max44009_present == MAX44009_ERROR) {
printf("MAX44009 sensor is NOT present!\n"); printf("MAX44009 sensor is NOT present!\n");
leds_on(LEDS_ORANGE); leds_on(LEDS_ORANGE);
} }
/* Initialize the SHT21 sensor */
sht21_present = SENSORS_ACTIVATE(sht21); sht21_present = SENSORS_ACTIVATE(sht21);
if(sht21_present == SHT21_ERROR) { if(sht21_present == SHT21_ERROR) {
printf("SHT21 sensor is NOT present!\n"); printf("SHT21 sensor is NOT present!\n");
@ -123,12 +127,12 @@ PROCESS_THREAD(openmote_demo_process, ev, data)
if(ev == PROCESS_EVENT_TIMER) { if(ev == PROCESS_EVENT_TIMER) {
if(adxl346_present != ADXL346_ERROR) { if(adxl346_present != ADXL346_ERROR) {
leds_on(LEDS_YELLOW); leds_on(LEDS_YELLOW);
accel = adxl346.value(ADXL346_READ_X); accel = adxl346.value(ADXL346_READ_X_mG);
printf("X Acceleration: %u\n", accel); printf("X Acceleration: %d.%u G\n", accel / 1000, accel % 1000);
accel = adxl346.value(ADXL346_READ_Y); accel = adxl346.value(ADXL346_READ_Y_mG);
printf("Y Acceleration: %u\n", accel); printf("Y Acceleration: %d.%u G\n", accel / 1000, accel % 1000);
accel = adxl346.value(ADXL346_READ_Z); accel = adxl346.value(ADXL346_READ_Z_mG);
printf("Z Acceleration: %u\n", accel); printf("Z Acceleration: %d.%u G\n", accel / 1000, accel % 1000);
leds_off(LEDS_YELLOW); leds_off(LEDS_YELLOW);
} }

View File

@ -153,6 +153,9 @@
#define ADXL346_DATA_FORMAT_RANGE_PM_4g (1) #define ADXL346_DATA_FORMAT_RANGE_PM_4g (1)
#define ADXL346_DATA_FORMAT_RANGE_PM_8g (2) #define ADXL346_DATA_FORMAT_RANGE_PM_8g (2)
#define ADXL346_DATA_FORMAT_RANGE_PM_16g (3) #define ADXL346_DATA_FORMAT_RANGE_PM_16g (3)
#define ADXL346_USER_CONFIGURATION (ADXL346_DATA_FORMAT_RANGE_PM_2g)
/** @} */ /** @} */
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
static uint8_t enabled; static uint8_t enabled;
@ -163,13 +166,11 @@ adxl346_init(void)
uint8_t config[2]; uint8_t config[2];
config[0] = ADXL346_BW_RATE_ADDR; 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)); i2c_burst_send(ADXL346_ADDRESS, config, sizeof(config));
config[0] = ADXL346_DATA_FORMAT_ADDR; config[0] = ADXL346_DATA_FORMAT_ADDR;
config[1] = (ADXL346_DATA_FORMAT_SELF_TEST | config[1] = (ADXL346_USER_CONFIGURATION);
ADXL346_DATA_FORMAT_FULL_RES |
ADXL346_DATA_FORMAT_RANGE_PM_16g);
i2c_burst_send(ADXL346_ADDRESS, config, sizeof(config)); i2c_burst_send(ADXL346_ADDRESS, config, sizeof(config));
config[0] = ADXL346_POWER_CTL_ADDR; config[0] = ADXL346_POWER_CTL_ADDR;
@ -185,23 +186,86 @@ adxl346_is_present(void)
i2c_single_send(ADXL346_ADDRESS, ADXL346_DEVID_ADDR); i2c_single_send(ADXL346_ADDRESS, ADXL346_DEVID_ADDR);
i2c_single_receive(ADXL346_ADDRESS, &is_present); 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) adxl346_read_accel(uint8_t addr1, uint8_t addr2)
{ {
uint8_t acceleration[2]; uint8_t acceleration[2];
uint16_t z; int16_t result;
i2c_single_send(ADXL346_ADDRESS, addr1); i2c_single_send(ADXL346_ADDRESS, addr1);
i2c_single_receive(ADXL346_ADDRESS, &acceleration[0]); i2c_single_receive(ADXL346_ADDRESS, &acceleration[0]);
i2c_single_send(ADXL346_ADDRESS, addr2); i2c_single_send(ADXL346_ADDRESS, addr2);
i2c_single_receive(ADXL346_ADDRESS, &acceleration[1]); 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 static int
@ -218,6 +282,7 @@ status(int type)
static int static int
value(int type) value(int type)
{ {
int16_t accel;
if(!enabled) { if(!enabled) {
PRINTF("ADXL346: sensor not started\n"); PRINTF("ADXL346: sensor not started\n");
return ADXL346_ERROR; return ADXL346_ERROR;
@ -225,10 +290,19 @@ value(int type)
if(type == ADXL346_READ_X) { if(type == ADXL346_READ_X) {
return adxl346_read_accel(ADXL346_DATAX0_ADDR, ADXL346_DATAX1_ADDR); 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); return adxl346_read_accel(ADXL346_DATAY0_ADDR, ADXL346_DATAY1_ADDR);
} else if(type == ADXL346_READ_Z) { } else if(type == ADXL346_READ_Z) {
return adxl346_read_accel(ADXL346_DATAZ0_ADDR, ADXL346_DATAZ1_ADDR); 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 { } else {
PRINTF("ADXL346: invalid value requested\n"); PRINTF("ADXL346: invalid value requested\n");
return ADXL346_ERROR; return ADXL346_ERROR;
@ -243,6 +317,7 @@ configure(int type, int value)
if(type == ADXL346_ACTIVATE) { if(type == ADXL346_ACTIVATE) {
if(!adxl346_is_present()) { if(!adxl346_is_present()) {
PRINTF("ADXL346: is not present\n"); PRINTF("ADXL346: is not present\n");
enabled = 0;
return ADXL346_ERROR; return ADXL346_ERROR;
} else { } else {
adxl346_init(); 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; return ADXL346_ERROR;
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/

View File

@ -51,9 +51,13 @@
#define ADXL346_SUCCESS (0) #define ADXL346_SUCCESS (0)
#define ADXL346_ACTIVATE (SENSORS_ACTIVE) #define ADXL346_ACTIVATE (SENSORS_ACTIVE)
#define ADXL346_READ_X (2) #define ADXL346_READ_X (2)
#define ADXL346_READ_Y (3) #define ADXL346_READ_X_mG (3)
#define ADXL346_READ_Z (4) #define ADXL346_READ_Y (4)
#define ADXL346_NONE (5) #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" #define ADXL346_SENSOR "ADXL346 Sensor"
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/