Pedometer function of pedometer bracelet based on F103 and X-NUCLEO-IKS01A3
[Copy link]
This post was last edited by sylar^z on 2019-8-11 21:21
This pedometer bracelet design is based on the F103 core board and the X-NUCLEO-IKS01A3 sensor development board. The pedometer function is realized through the LSM6DSO sensor on the X-NUCLEO-IKS01A3.
LSM6DSO is a 6-axis sensor, including 3-axis acceleration and 3-axis gyroscope. The power supply voltage is 1.7V-3.6V, and it can communicate through the commonly used SPI and I2C. LSM6DSO has rich motion detection functions, including pedometer, free fall, wake-up, 6d/4d direction, click and double-click, etc.
The pedometer bracelet uses the step detection of LSM6DSO to realize the step counting function. The function is added to the normal transplantation of the X-NUCLEO-IKS01A3 driver program (see "Driver transplantation of pedometer bracelet based on F103 and X-NUCLEO-IKS01A3").
First, some changes were made to the previous program to allow multiple sensors to work simultaneously, and a pin PB7 was added to detect the interrupt of INT1 of LSM6DSO. The next step is to add the step counting function. After completing the initialization of LSM6DSO, it is necessary to enable the step counting function of LSM6DSO, configure the corresponding parameters and interrupt output, and clear the current step count. Then it is to detect walking and calculate the step count.
1. Set the output frequency and full-scale value of LSM6DSO
2. Enable the step counting algorithm and set the mode.
The available modes are as follows:
Depending on the selected mode, the following three registers will be configured to achieve the step counting mode setting.
3. Set the step counting interrupt to output through INT1
The INT1 pin can be bound to multiple functions or states for simultaneous use. When an interrupt is detected, the event source is determined by reading the corresponding event status register.
The step counting interrupt belongs to the category INT1_EMB_FUNC
Enable the pedometer function of LSM6DSO, and configure the corresponding parameters and interrupt output
/**
* @brief Enable pedometer
* @param pObj the device pObj
* @retval 0 in case of success, an error code otherwise
*/
int32_t LSM6DSO_ACC_Enable_Pedometer(LSM6DSO_Object_t *pObj)
{
lsm6dso_pin_int1_route_t val;
/* Output Data Rate selection */
if (LSM6DSO_ACC_SetOutputDataRate(pObj, 26.0f) != LSM6DSO_OK)
{
return LSM6DSO_ERROR;
}
/* Full scale selection */
if (LSM6DSO_ACC_SetFullScale(pObj, 2) != LSM6DSO_OK)
{
return LSM6DSO_ERROR;
}
/* Enable pedometer algorithm. */
if (lsm6dso_pedo_sens_set(&(pObj->Ctx), LSM6DSO_PEDO_BASE_MODE) != LSM6DSO_OK)
{
return LSM6DSO_ERROR;
}
/* Enable step detector on INT1 pin */
if (lsm6dso_pin_int1_route_get(&(pObj->Ctx), &val) != LSM6DSO_OK)
{
return LSM6DSO_ERROR;
}
val.emb_func_int1.int1_step_detector = PROPERTY_ENABLE;
if (lsm6dso_pin_int1_route_set(&(pObj->Ctx), &val) != LSM6DSO_OK)
{
return LSM6DSO_ERROR;
}
return LSM6DSO_OK;
}
4. Event confirmation and handling
Check the STEP_DETECTED flag in EMB_FUNC_SRC. If it is 1, it means walking behavior is detected. The number of steps counted is increased by 1.
/**
* @brief Get the status of all hardware events
* @param pObj the device pObj
* @param Status the status of all hardware events
* @retval 0 in case of success, an error code otherwise
*/
int32_t LSM6DSO_ACC_Get_Event_Status(LSM6DSO_Object_t *pObj, LSM6DSO_Event_Status_t *Status)
{
uint8_t tilt_ia;
lsm6dso_wake_up_src_t wake_up_src;
lsm6dso_tap_src_t tap_src;
lsm6dso_d6d_src_t d6d_src;
lsm6dso_emb_func_src_t func_src;
lsm6dso_md1_cfg_t md1_cfg;
lsm6dso_md2_cfg_t md2_cfg;
lsm6dso_emb_func_int1_t int1_ctrl;
lsm6dso_emb_func_int2_t int2_ctrl;
(void)memset((void *)Status, 0x0, sizeof(LSM6DSO_Event_Status_t));
if (lsm6dso_read_reg(&(pObj->Ctx), LSM6DSO_WAKE_UP_SRC, (uint8_t *)&wake_up_src, 1) != LSM6DSO_OK)
{
return LSM6DSO_ERROR;
}
if (lsm6dso_read_reg(&(pObj->Ctx), LSM6DSO_TAP_SRC, (uint8_t *)&tap_src, 1) != LSM6DSO_OK)
{
return LSM6DSO_ERROR;
}
if (lsm6dso_read_reg(&(pObj->Ctx), LSM6DSO_D6D_SRC, (uint8_t *)&d6d_src, 1) != LSM6DSO_OK)
{
return LSM6DSO_ERROR;
}
if (lsm6dso_mem_bank_set(&(pObj->Ctx), LSM6DSO_EMBEDDED_FUNC_BANK) != LSM6DSO_OK)
{
return LSM6DSO_ERROR;
}
if (lsm6dso_read_reg(&(pObj->Ctx), LSM6DSO_EMB_FUNC_SRC, (uint8_t *)&func_src, 1) != LSM6DSO_OK)
{
return LSM6DSO_ERROR;
}
if (lsm6dso_read_reg(&(pObj->Ctx), LSM6DSO_EMB_FUNC_INT1, (uint8_t *)&int1_ctrl, 1) != LSM6DSO_OK)
{
return LSM6DSO_ERROR;
}
if (lsm6dso_read_reg(&(pObj->Ctx), LSM6DSO_EMB_FUNC_INT2, (uint8_t *)&int2_ctrl, 1) != LSM6DSO_OK)
{
return LSM6DSO_ERROR;
}
if (lsm6dso_mem_bank_set(&(pObj->Ctx), LSM6DSO_USER_BANK) != 0)
{
return LSM6DSO_ERROR;
}
if (lsm6dso_read_reg(&(pObj->Ctx), LSM6DSO_MD1_CFG, (uint8_t *)&md1_cfg, 1) != LSM6DSO_OK)
{
return LSM6DSO_ERROR;
}
if (lsm6dso_read_reg(&(pObj->Ctx), LSM6DSO_MD2_CFG, (uint8_t *)&md2_cfg, 1) != LSM6DSO_OK)
{
return LSM6DSO_ERROR;
}
if (lsm6dso_tilt_flag_data_ready_get(&(pObj->Ctx), &tilt_ia) != LSM6DSO_OK)
{
return LSM6DSO_ERROR;
}
if ((md1_cfg.int1_ff == 1U) || (md2_cfg.int2_ff == 1U))
{
if (wake_up_src.ff_ia == 1U)
{
Status->FreeFallStatus = 1;
}
}
if ((md1_cfg.int1_wu == 1U) || (md2_cfg.int2_wu == 1U))
{
if (wake_up_src.wu_ia == 1U)
{
Status->WakeUpStatus = 1;
}
}
if ((md1_cfg.int1_single_tap == 1U) || (md2_cfg.int2_single_tap == 1U))
{
if (tap_src.single_tap == 1U)
{
Status->TapStatus = 1;
}
}
if ((md1_cfg.int1_double_tap == 1U) || (md2_cfg.int2_double_tap == 1U))
{
if (tap_src.double_tap == 1U)
{
Status->DoubleTapStatus = 1;
}
}
if ((md1_cfg.int1_6d == 1U) || (md2_cfg.int2_6d == 1U))
{
if (d6d_src.d6d_ia == 1U)
{
Status->D6DOrientationStatus = 1;
}
}
if (int1_ctrl.int1_step_detector == 1U)
{
if (func_src.step_detected == 1U)
{
Status->StepStatus = 1;
}
}
if ((int1_ctrl.int1_tilt == 1U) || (int2_ctrl.int2_tilt == 1U))
{
if (tilt_ia == 1U)
{
Status->TiltStatus = 1;
}
}
return LSM6DSO_OK;
}
Attached source code:
CORE-STM32F103C8_FOR_X-NUCLEO_IKS01A3-PEDOMETER.rar
(13.26 MB, downloads: 52)
LSM6DSO manual:
lsm6dso.pdf
(2.76 MB, downloads: 4)
|