Tilt sensing with Lis3dh and stm32

608 views Asked by At

I am using the LIS3DH sensor with STM32F4 and I want to calculate the tilt angle. I am aware that I am supposed to calculate the angles(Yaw-Pitch-Roll(ψ-θ-φ)) using some formulas but I don't know how to calculate values obtained after applying accelerometer calibration on raw measurement data(Ax1,Ay1, Az1). And how can I make sure that the sensor understands that it is in its steady state before being moved?

while (1)
  {
//      HAL_GPIO_TogglePin(GPIOE,GPIO_PIN_0);
//    HAL_Delay(100);
        
    /* USER CODE END WHILE */
    lis3dh_reg_t reg;
    /* Read output only if new value available */
    lis3dh_xl_data_ready_get(&dev_ctx, &reg.byte);

        
    if (reg.byte) {
      /* Read accelerometer data */
      memset(data_raw_acceleration, 0x00, 3 * sizeof(int16_t));
      lis3dh_acceleration_raw_get(&dev_ctx, data_raw_acceleration);
      acceleration_mg[0] =lis3dh_from_fs2_hr_to_mg(data_raw_acceleration[0]);
      acceleration_mg[1] =lis3dh_from_fs2_hr_to_mg(data_raw_acceleration[1]);
      acceleration_mg[2] =lis3dh_from_fs2_hr_to_mg(data_raw_acceleration[2]);
      sprintf((char *)tx_buffer,"Acceleration [mg]:%4.2f\t%4.2f\t%4.2f\r\n",acceleration_mg[0], acceleration_mg[1], acceleration_mg[2]);
      tx_com(tx_buffer, strlen((char const *)tx_buffer));
        }                  
        roll = atan2(acceleration_mg[1] , acceleration_mg[2]) * 57.3;
    pitch = atan2((-acceleration_mg[0]) , sqrt(acceleration_mg[1] *acceleration_mg[1] +acceleration_mg[2] *acceleration_mg[2])) * 57.3;
        if (pitch==-0.0645269975){
            HAL_GPIO_TogglePin(GPIOD,GPIO_PIN_0);
        }
    /* USER CODE BEGIN 3 */
  }

I'm using the library shared in: https://github.com/s54mtb/LEDS/blob/master/Src/lis3dh_driver.c

0

There are 0 answers