S&B Volcano vaporizer remote control with Pi Pico W
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

lipo.c 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. #include <math.h>
  21. #include "stdbool.h"
  22. #include "hardware/adc.h"
  23. #include "config.h"
  24. #include "lipo.h"
  25. #if CYW43_USES_VSYS_PIN
  26. #include "pico/cyw43_arch.h"
  27. #endif
  28. #ifndef PICO_POWER_SAMPLE_COUNT
  29. #define PICO_POWER_SAMPLE_COUNT 3
  30. #endif
  31. // Pin used for ADC 0
  32. #define PICO_FIRST_ADC_PIN 26
  33. static const float full_battery = 4.1f;
  34. static const float empty_battery = 3.2f;
  35. static const float low_pass_factor = 0.9f;
  36. bool lipo_charging(void) {
  37. #if defined CYW43_WL_GPIO_VBUS_PIN
  38. return cyw43_arch_gpio_get(CYW43_WL_GPIO_VBUS_PIN);
  39. #elif defined PICO_VBUS_PIN
  40. gpio_set_function(PICO_VBUS_PIN, GPIO_FUNC_SIO);
  41. return gpio_get(PICO_VBUS_PIN);
  42. #else
  43. #error "No VBUS Pin available!"
  44. #endif
  45. }
  46. float lipo_voltage(void) {
  47. #ifndef PICO_VSYS_PIN
  48. #error "No VSYS Pin available!"
  49. #endif
  50. #if CYW43_USES_VSYS_PIN
  51. cyw43_thread_enter();
  52. // Make sure cyw43 is awake
  53. bool charging = cyw43_arch_gpio_get(CYW43_WL_GPIO_VBUS_PIN);
  54. #endif
  55. // setup adc
  56. adc_gpio_init(PICO_VSYS_PIN);
  57. adc_select_input(PICO_VSYS_PIN - PICO_FIRST_ADC_PIN);
  58. adc_fifo_setup(true, false, 0, false, false);
  59. adc_run(true);
  60. // We seem to read low values initially - this seems to fix it
  61. int ignore_count = PICO_POWER_SAMPLE_COUNT;
  62. while (!adc_fifo_is_empty() || ignore_count-- > 0) {
  63. (void)adc_fifo_get_blocking();
  64. }
  65. // read vsys
  66. uint32_t vsys = 0;
  67. for(int i = 0; i < PICO_POWER_SAMPLE_COUNT; i++) {
  68. uint16_t val = adc_fifo_get_blocking();
  69. vsys += val;
  70. }
  71. adc_run(false);
  72. adc_fifo_drain();
  73. vsys /= PICO_POWER_SAMPLE_COUNT;
  74. #if CYW43_USES_VSYS_PIN
  75. cyw43_thread_exit();
  76. #endif
  77. // Generate voltage
  78. const float conversion_factor = 3.3f / (1 << 12);
  79. float v_now = vsys * 3 * conversion_factor;
  80. static float v_prev = NAN;
  81. if (charging) {
  82. v_prev = NAN;
  83. return v_now;
  84. } else {
  85. if (isnan(v_prev)) {
  86. v_prev = v_now;
  87. }
  88. v_prev = (v_prev * low_pass_factor) + (v_now * (1.0f - low_pass_factor));
  89. return v_prev;
  90. }
  91. }
  92. float lipo_percentage(float voltage) {
  93. float percentage = 100.0f * ((voltage - empty_battery) / (full_battery - empty_battery));
  94. if (percentage >= 99.9f) {
  95. percentage = 99.9f;
  96. } else if (percentage < 0.0f) {
  97. percentage = 0.0f;
  98. }
  99. return percentage;
  100. }