S&B Volcano vaporizer remote control with Pi Pico W
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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * lipo.c
  3. *
  4. * https://github.com/raspberrypi/pico-examples/blob/master/adc/read_vsys/power_status.c
  5. *
  6. * Copyright (c) 2023 Thomas Buck (thomas@xythobuz.de)
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * See <http://www.gnu.org/licenses/>.
  19. */
  20. #define LIPO_USE_PERCENTAGE_CURVE
  21. #include <math.h>
  22. #include "stdbool.h"
  23. #include "hardware/adc.h"
  24. #include "config.h"
  25. #include "lipo.h"
  26. #if CYW43_USES_VSYS_PIN
  27. #include "pico/cyw43_arch.h"
  28. #endif
  29. #ifndef PICO_POWER_SAMPLE_COUNT
  30. #define PICO_POWER_SAMPLE_COUNT 3
  31. #endif
  32. // Pin used for ADC 0
  33. #define PICO_FIRST_ADC_PIN 26
  34. #ifndef LIPO_USE_PERCENTAGE_CURVE
  35. static const float full_battery = 4.1f;
  36. static const float empty_battery = 3.2f;
  37. #endif // ! LIPO_USE_PERCENTAGE_CURVE
  38. static const float low_pass_factor = 0.9f;
  39. bool lipo_charging(void) {
  40. #if defined CYW43_WL_GPIO_VBUS_PIN
  41. return cyw43_arch_gpio_get(CYW43_WL_GPIO_VBUS_PIN);
  42. #elif defined PICO_VBUS_PIN
  43. gpio_set_function(PICO_VBUS_PIN, GPIO_FUNC_SIO);
  44. return gpio_get(PICO_VBUS_PIN);
  45. #else
  46. #error "No VBUS Pin available!"
  47. #endif
  48. }
  49. float lipo_voltage(void) {
  50. #ifndef PICO_VSYS_PIN
  51. #error "No VSYS Pin available!"
  52. #endif
  53. #if CYW43_USES_VSYS_PIN
  54. cyw43_thread_enter();
  55. // Make sure cyw43 is awake
  56. bool charging = cyw43_arch_gpio_get(CYW43_WL_GPIO_VBUS_PIN);
  57. #endif
  58. // setup adc
  59. adc_gpio_init(PICO_VSYS_PIN);
  60. adc_select_input(PICO_VSYS_PIN - PICO_FIRST_ADC_PIN);
  61. adc_fifo_setup(true, false, 0, false, false);
  62. adc_run(true);
  63. // We seem to read low values initially - this seems to fix it
  64. int ignore_count = PICO_POWER_SAMPLE_COUNT;
  65. while (!adc_fifo_is_empty() || ignore_count-- > 0) {
  66. (void)adc_fifo_get_blocking();
  67. }
  68. // read vsys
  69. uint32_t vsys = 0;
  70. for(int i = 0; i < PICO_POWER_SAMPLE_COUNT; i++) {
  71. uint16_t val = adc_fifo_get_blocking();
  72. vsys += val;
  73. }
  74. adc_run(false);
  75. adc_fifo_drain();
  76. vsys /= PICO_POWER_SAMPLE_COUNT;
  77. #if CYW43_USES_VSYS_PIN
  78. cyw43_thread_exit();
  79. #endif
  80. // Generate voltage
  81. const float conversion_factor = 3.3f / (1 << 12);
  82. float v_now = vsys * 3 * conversion_factor;
  83. static float v_prev = NAN;
  84. if (charging) {
  85. v_prev = NAN;
  86. return v_now;
  87. } else {
  88. if (isnan(v_prev)) {
  89. v_prev = v_now;
  90. }
  91. v_prev = (v_prev * low_pass_factor) + (v_now * (1.0f - low_pass_factor));
  92. return v_prev;
  93. }
  94. }
  95. float lipo_percentage(float voltage) {
  96. float percentage;
  97. #ifdef LIPO_USE_PERCENTAGE_CURVE
  98. /*
  99. * Try to linearize the LiPo discharge curve.
  100. * https://electronics.stackexchange.com/a/551667
  101. *
  102. * Seems to work relatively well, although
  103. * "stopping" at 3.5V feels a bit high to me.
  104. */
  105. percentage = 123.0f - (123.0f / powf(1.0f + powf(voltage / 3.7f, 80.0f), 0.165f));
  106. #else // LIPO_USE_PERCENTAGE_CURVE
  107. percentage = 100.0f * ((voltage - empty_battery) / (full_battery - empty_battery));
  108. #endif // LIPO_USE_PERCENTAGE_CURVE
  109. if (percentage >= 99.9f) {
  110. percentage = 99.9f;
  111. } else if (percentage < 0.0f) {
  112. percentage = 0.0f;
  113. }
  114. return percentage;
  115. }