0

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

  • As far as I understand, your problem is in interpreting the values the sensor gives you? Have you consulted the datasheet of the sensor? Is there something specific in it that you can't understand? – Ilya Jul 11 '22 at 09:01
  • I managed to get the acceleration datum using (lis3dh_acceleration_raw_get function) and used it in the proper formulas to find the tilt angles, but when I put it in the debug mode, the acceleration datum are said to be zero. – Tara sarpoolaki Jul 13 '22 at 05:24
  • Do you have any breakpoints? If your program halts during communication, you won't receive data or you will receive corrupted data. Don't forget that physically shifting out stuff takes much longer than for CPU to execute next instructions. You may be sitting at a breakpoint while I2C/SPI/UART has shifted out or received only the first half a byte. Communication is really hard to debug with breakpoints (rather, it's limited in what you can do) – Ilya Jul 13 '22 at 08:06
  • I don't I guess. Now, I can get the acceleration datum and they're not all zero. But they do not change as I move or orient the board in different directions. I cannot tell if I'm testing the code wrong or if there could be something wrong with the code. The code looks fine to me, but it could be it. – Tara sarpoolaki Jul 13 '22 at 08:30
  • Please post your code (as text, not as screenshot, just reminding). Hardware setup (I2C/SPI or whatver you connect to the IC with) and how you read the data out of the IC. There has to be something. – Ilya Jul 13 '22 at 08:36
  • @Ilya I edited the question so you can see what I added to my infinite loop part of the project. You can also see the link to the library I used. – Tara sarpoolaki Jul 20 '22 at 04:42

0 Answers0