My Marlin configs for Fabrikator Mini and CTC i3 Pro B
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.

power_monitor.h 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  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. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #pragma once
  23. #include "../inc/MarlinConfig.h"
  24. #define PM_SAMPLE_RANGE HAL_ADC_RANGE
  25. #define PM_K_VALUE 6
  26. #define PM_K_SCALE 6
  27. template <const float & SCALE, int K_VALUE, int K_SCALE>
  28. struct pm_lpf_t {
  29. uint32_t filter_buf;
  30. float value;
  31. void add_sample(const uint16_t sample) {
  32. filter_buf += (uint32_t(sample) << K_SCALE) - (filter_buf >> K_VALUE);
  33. }
  34. void capture() {
  35. value = filter_buf * (SCALE * (1.0f / (1UL << (PM_K_VALUE + PM_K_SCALE))));
  36. }
  37. void reset(uint16_t reset_value = 0) {
  38. filter_buf = uint32_t(reset_value) << (K_VALUE + K_SCALE);
  39. capture();
  40. }
  41. };
  42. class PowerMonitor {
  43. private:
  44. #if ENABLED(POWER_MONITOR_CURRENT)
  45. static constexpr float amps_adc_scale = float(ADC_VREF) / (POWER_MONITOR_VOLTS_PER_AMP * PM_SAMPLE_RANGE);
  46. static pm_lpf_t<amps_adc_scale, PM_K_VALUE, PM_K_SCALE> amps;
  47. #endif
  48. #if ENABLED(POWER_MONITOR_VOLTAGE)
  49. static constexpr float volts_adc_scale = float(ADC_VREF) / (POWER_MONITOR_VOLTS_PER_VOLT * PM_SAMPLE_RANGE);
  50. static pm_lpf_t<volts_adc_scale, PM_K_VALUE, PM_K_SCALE> volts;
  51. #endif
  52. public:
  53. static uint8_t flags; // M430 flags to display current
  54. static millis_t display_item_ms;
  55. static uint8_t display_item;
  56. PowerMonitor() { reset(); }
  57. enum PM_Display_Bit : uint8_t {
  58. PM_DISP_BIT_I, // Current display enable bit
  59. PM_DISP_BIT_V, // Voltage display enable bit
  60. PM_DISP_BIT_P // Power display enable bit
  61. };
  62. #if ENABLED(POWER_MONITOR_CURRENT)
  63. FORCE_INLINE static float getAmps() { return amps.value + (POWER_MONITOR_CURRENT_OFFSET); }
  64. void add_current_sample(const uint16_t value) { amps.add_sample(value); }
  65. #endif
  66. #if ENABLED(POWER_MONITOR_VOLTAGE)
  67. FORCE_INLINE static float getVolts() { return volts.value + (POWER_MONITOR_VOLTAGE_OFFSET); }
  68. void add_voltage_sample(const uint16_t value) { volts.add_sample(value); }
  69. #else
  70. FORCE_INLINE static float getVolts() { return POWER_MONITOR_FIXED_VOLTAGE; }
  71. #endif
  72. #if HAS_POWER_MONITOR_WATTS
  73. FORCE_INLINE static float getPower() { return getAmps() * getVolts(); }
  74. #endif
  75. #if HAS_WIRED_LCD
  76. #if HAS_MARLINUI_U8GLIB && DISABLED(LIGHTWEIGHT_UI)
  77. FORCE_INLINE static bool display_enabled() { return flags != 0x00; }
  78. #endif
  79. #if ENABLED(POWER_MONITOR_CURRENT)
  80. static void draw_current();
  81. FORCE_INLINE static bool current_display_enabled() { return TEST(flags, PM_DISP_BIT_I); }
  82. FORCE_INLINE static void set_current_display(const bool b) { SET_BIT_TO(flags, PM_DISP_BIT_I, b); }
  83. FORCE_INLINE static void toggle_current_display() { TBI(flags, PM_DISP_BIT_I); }
  84. #endif
  85. #if ENABLED(POWER_MONITOR_VOLTAGE)
  86. static void draw_voltage();
  87. FORCE_INLINE static bool voltage_display_enabled() { return TEST(flags, PM_DISP_BIT_V); }
  88. FORCE_INLINE static void set_voltage_display(const bool b) { SET_BIT_TO(flags, PM_DISP_BIT_V, b); }
  89. FORCE_INLINE static void toggle_voltage_display() { TBI(flags, PM_DISP_BIT_V); }
  90. #endif
  91. #if HAS_POWER_MONITOR_WATTS
  92. static void draw_power();
  93. FORCE_INLINE static bool power_display_enabled() { return TEST(flags, PM_DISP_BIT_P); }
  94. FORCE_INLINE static void set_power_display(const bool b) { SET_BIT_TO(flags, PM_DISP_BIT_P, b); }
  95. FORCE_INLINE static void toggle_power_display() { TBI(flags, PM_DISP_BIT_P); }
  96. #endif
  97. #endif
  98. static void reset() {
  99. flags = 0x00;
  100. #if ENABLED(POWER_MONITOR_CURRENT)
  101. amps.reset();
  102. #endif
  103. #if ENABLED(POWER_MONITOR_VOLTAGE)
  104. volts.reset();
  105. #endif
  106. #if ENABLED(SDSUPPORT)
  107. display_item_ms = 0;
  108. display_item = 0;
  109. #endif
  110. }
  111. static void capture_values() {
  112. #if ENABLED(POWER_MONITOR_CURRENT)
  113. amps.capture();
  114. #endif
  115. #if ENABLED(POWER_MONITOR_VOLTAGE)
  116. volts.capture();
  117. #endif
  118. }
  119. };
  120. extern PowerMonitor power_monitor;