Projet voilier 4IRA1 Arnaud Vergnet Marino Benassai Bastien Picco Yohan Simard
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Accelerometer.c 1.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #include "Accelerometer.h"
  2. #include "ADC.h"
  3. #include "math.h"
  4. #include "stdio.h"
  5. #define M_PI 3.14159265358979323846
  6. // g-sel1 et g-sel1 à 0 => range de 2.5g
  7. // Donc sensibilité de +- 480 mv/g
  8. const float ZERO_G = 1.27; // 0 g
  9. const float SENSITIVITY = 1.35-0.9;
  10. // max: 1.75
  11. // min: 0.935
  12. void Accelerometer_conf(ADC_TypeDef *adc, GPIO_TypeDef * gpio, int pinx, int piny)
  13. {
  14. ADC_conf(adc);
  15. GPIO_conf(gpio, pinx, LL_GPIO_MODE_ANALOG, LL_GPIO_OUTPUT_PUSHPULL, LL_GPIO_PULL_UP);
  16. GPIO_conf(gpio, piny, LL_GPIO_MODE_ANALOG, LL_GPIO_OUTPUT_PUSHPULL, LL_GPIO_PULL_UP);
  17. }
  18. void Accelerometer_start(ADC_TypeDef *adc)
  19. {
  20. ADC_start(adc);
  21. }
  22. float voltsToG(float volts)
  23. {
  24. float readG = (volts - ZERO_G) / SENSITIVITY;
  25. if (readG > 1.0)
  26. readG = 1.0;
  27. else if (readG < -1.0)
  28. readG = -1.0;
  29. return readG;
  30. }
  31. int Accelerometer_getAngle(ADC_TypeDef *adc, int channel)
  32. {
  33. const float readV = ADC_readVolt(adc, channel);
  34. const float readG = voltsToG(readV);
  35. float angleRad = asin(readG);
  36. int angleDeg = angleRad * (180/M_PI);
  37. return angleDeg;
  38. }