No Description
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.

adc.c 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * adc.c
  3. *
  4. * Copyright (c) 2024 Thomas Buck (thomas@xythobuz.de)
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * See <http://www.gnu.org/licenses/>.
  17. */
  18. #include <stdio.h>
  19. #include <math.h>
  20. #include "pico/stdlib.h"
  21. #include "hardware/adc.h"
  22. #include "adc.h"
  23. #define LIPO_USE_PERCENTAGE_CURVE
  24. #define ADC_NUM 2
  25. #define ADC_PIN (26 + ADC_NUM)
  26. #define ADC_VREF 3.3
  27. #define ADC_RANGE (1 << 12)
  28. #define ADC_CONVERT (ADC_VREF / (ADC_RANGE - 1))
  29. #define BAT_R1 10000.0f
  30. #define BAT_R2 18000.0f
  31. #define FILTER_OLD 0.95f
  32. #define FILTER_NEW (1.0f - FILTER_OLD)
  33. #ifndef LIPO_USE_PERCENTAGE_CURVE
  34. static const float full_battery = 4.1f;
  35. static const float empty_battery = 3.2f;
  36. #endif // ! LIPO_USE_PERCENTAGE_CURVE
  37. static float filtered = 0.0f;
  38. static float bat_read(void) {
  39. float v_adc = adc_read() * ADC_CONVERT;
  40. // Vadc = Vbat * R2 / (R1 + R2)
  41. float v_bat = v_adc / (BAT_R2 / (BAT_R1 + BAT_R2));
  42. return v_bat;
  43. }
  44. void bat_init(void) {
  45. adc_init();
  46. adc_gpio_init( ADC_PIN);
  47. adc_select_input( ADC_NUM);
  48. filtered = bat_read();
  49. }
  50. float bat_get(void) {
  51. filtered = (filtered * FILTER_OLD) + (bat_read() * FILTER_NEW);
  52. return filtered;
  53. }
  54. float bat_to_percent(float voltage) {
  55. float percentage = 0.0f;
  56. #ifdef LIPO_USE_PERCENTAGE_CURVE
  57. /*
  58. * Try to linearize the LiPo discharge curve.
  59. * https://electronics.stackexchange.com/a/551667
  60. *
  61. * Seems to work relatively well, although
  62. * "stopping" at 3.5V feels a bit high to me.
  63. */
  64. percentage = 123.0f - (123.0f / powf(1.0f + powf(voltage / 3.7f, 80.0f), 0.165f));
  65. #else // LIPO_USE_PERCENTAGE_CURVE
  66. percentage = 100.0f * ((voltage - empty_battery) / (full_battery - empty_battery));
  67. #endif // LIPO_USE_PERCENTAGE_CURVE
  68. return MIN(MAX(percentage, 0.0f), 100.0f);
  69. }