Power monitor and display (#17437)
parent
c8e99d572c
commit
424569b4c4
@ -0,0 +1,74 @@
|
|||||||
|
/**
|
||||||
|
* Marlin 3D Printer Firmware
|
||||||
|
* Copyright (C) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
||||||
|
*
|
||||||
|
* Based on Sprinter and grbl.
|
||||||
|
* Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "../inc/MarlinConfigPre.h"
|
||||||
|
|
||||||
|
#if HAS_POWER_MONITOR
|
||||||
|
|
||||||
|
#include "power_monitor.h"
|
||||||
|
|
||||||
|
#include "../lcd/ultralcd.h"
|
||||||
|
#include "../lcd/lcdprint.h"
|
||||||
|
|
||||||
|
uint8_t PowerMonitor::flags; // = 0
|
||||||
|
|
||||||
|
#if ENABLED(POWER_MONITOR_CURRENT)
|
||||||
|
pm_lpf_t<PowerMonitor::amps_adc_scale, PM_K_VALUE, PM_K_SCALE> PowerMonitor::amps;
|
||||||
|
#endif
|
||||||
|
#if ENABLED(POWER_MONITOR_VOLTAGE)
|
||||||
|
pm_lpf_t<PowerMonitor::volts_adc_scale, PM_K_VALUE, PM_K_SCALE> PowerMonitor::volts;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
millis_t PowerMonitor::display_item_ms;
|
||||||
|
uint8_t PowerMonitor::display_item;
|
||||||
|
|
||||||
|
PowerMonitor power_monitor; // Single instance - this calls the constructor
|
||||||
|
|
||||||
|
#if HAS_GRAPHICAL_LCD
|
||||||
|
|
||||||
|
#if ENABLED(POWER_MONITOR_CURRENT)
|
||||||
|
void PowerMonitor::draw_current() {
|
||||||
|
const float amps = getAmps();
|
||||||
|
lcd_put_u8str(amps < 100 ? ftostr21ns(amps) : ui16tostr4((uint16_t)amps));
|
||||||
|
lcd_put_wchar('A');
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if HAS_POWER_MONITOR_VREF
|
||||||
|
void PowerMonitor::draw_voltage() {
|
||||||
|
const float volts = getVolts();
|
||||||
|
lcd_put_u8str(volts < 100 ? ftostr21ns(volts) : ui16tostr4((uint16_t)volts));
|
||||||
|
lcd_put_wchar('V');
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if HAS_POWER_MONITOR_WATTS
|
||||||
|
void PowerMonitor::draw_power() {
|
||||||
|
const float power = getPower();
|
||||||
|
lcd_put_u8str(power < 100 ? ftostr21ns(power) : ui16tostr4((uint16_t)power));
|
||||||
|
lcd_put_wchar('W');
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // HAS_GRAPHICAL_LCD
|
||||||
|
|
||||||
|
#endif // HAS_POWER_MONITOR
|
@ -0,0 +1,140 @@
|
|||||||
|
/**
|
||||||
|
* Marlin 3D Printer Firmware
|
||||||
|
* Copyright (C) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
||||||
|
*
|
||||||
|
* Based on Sprinter and grbl.
|
||||||
|
* Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "../inc/MarlinConfig.h"
|
||||||
|
|
||||||
|
#define PM_SAMPLE_RANGE 1024
|
||||||
|
#define PM_K_VALUE 6
|
||||||
|
#define PM_K_SCALE 6
|
||||||
|
|
||||||
|
template <const float & SCALE, int K_VALUE, int K_SCALE>
|
||||||
|
struct pm_lpf_t {
|
||||||
|
uint32_t filter_buf;
|
||||||
|
float value;
|
||||||
|
void add_sample(const uint16_t sample) {
|
||||||
|
filter_buf = filter_buf - (filter_buf >> K_VALUE) + (uint32_t(sample) << K_SCALE);
|
||||||
|
}
|
||||||
|
void capture() {
|
||||||
|
value = filter_buf * (SCALE * (1.0f / (1UL << (PM_K_VALUE + PM_K_SCALE))));
|
||||||
|
}
|
||||||
|
void reset(uint16_t reset_value = 0) {
|
||||||
|
filter_buf = uint32_t(reset_value) << (K_VALUE + K_SCALE);
|
||||||
|
capture();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class PowerMonitor {
|
||||||
|
private:
|
||||||
|
#if ENABLED(POWER_MONITOR_CURRENT)
|
||||||
|
static constexpr float amps_adc_scale = float(ADC_VREF) / (POWER_MONITOR_VOLTS_PER_AMP * PM_SAMPLE_RANGE);
|
||||||
|
static pm_lpf_t<amps_adc_scale, PM_K_VALUE, PM_K_SCALE> amps;
|
||||||
|
#endif
|
||||||
|
#if ENABLED(POWER_MONITOR_VOLTAGE)
|
||||||
|
static constexpr float volts_adc_scale = float(ADC_VREF) / (POWER_MONITOR_VOLTS_PER_VOLT * PM_SAMPLE_RANGE);
|
||||||
|
static pm_lpf_t<volts_adc_scale, PM_K_VALUE, PM_K_SCALE> volts;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
public:
|
||||||
|
static uint8_t flags; // M430 flags to display current
|
||||||
|
|
||||||
|
static millis_t display_item_ms;
|
||||||
|
static uint8_t display_item;
|
||||||
|
|
||||||
|
PowerMonitor() { reset(); }
|
||||||
|
|
||||||
|
enum PM_Display_Bit : uint8_t {
|
||||||
|
PM_DISP_BIT_I, // Current display enable bit
|
||||||
|
PM_DISP_BIT_V, // Voltage display enable bit
|
||||||
|
PM_DISP_BIT_P // Power display enable bit
|
||||||
|
};
|
||||||
|
|
||||||
|
#if ENABLED(POWER_MONITOR_CURRENT)
|
||||||
|
FORCE_INLINE static float getAmps() { return amps.value; }
|
||||||
|
void add_current_sample(const uint16_t value) { amps.add_sample(value); }
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if HAS_POWER_MONITOR_VREF
|
||||||
|
#if ENABLED(POWER_MONITOR_VOLTAGE)
|
||||||
|
FORCE_INLINE static float getVolts() { return volts.value; }
|
||||||
|
#else
|
||||||
|
FORCE_INLINE static float getVolts() { return POWER_MONITOR_FIXED_VOLTAGE; } // using a specified fixed valtage as the voltage measurement
|
||||||
|
#endif
|
||||||
|
#if ENABLED(POWER_MONITOR_VOLTAGE)
|
||||||
|
void add_voltage_sample(const uint16_t value) { volts.add_sample(value); }
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if HAS_POWER_MONITOR_WATTS
|
||||||
|
FORCE_INLINE static float getPower() { return getAmps() * getVolts(); }
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if HAS_SPI_LCD
|
||||||
|
FORCE_INLINE static bool display_enabled() { return flags != 0x00; }
|
||||||
|
#if ENABLED(POWER_MONITOR_CURRENT)
|
||||||
|
static void draw_current();
|
||||||
|
FORCE_INLINE static bool current_display_enabled() { return TEST(flags, PM_DISP_BIT_I); }
|
||||||
|
FORCE_INLINE static void set_current_display(const bool b) { SET_BIT_TO(flags, PM_DISP_BIT_I, b); }
|
||||||
|
FORCE_INLINE static void toggle_current_display() { TBI(flags, PM_DISP_BIT_I); }
|
||||||
|
#endif
|
||||||
|
#if HAS_POWER_MONITOR_VREF
|
||||||
|
static void draw_voltage();
|
||||||
|
FORCE_INLINE static bool voltage_display_enabled() { return TEST(flags, PM_DISP_BIT_V); }
|
||||||
|
FORCE_INLINE static void set_voltage_display(const bool b) { SET_BIT_TO(flags, PM_DISP_BIT_V, b); }
|
||||||
|
FORCE_INLINE static void toggle_voltage_display() { TBI(flags, PM_DISP_BIT_I); }
|
||||||
|
#endif
|
||||||
|
#if HAS_POWER_MONITOR_WATTS
|
||||||
|
static void draw_power();
|
||||||
|
FORCE_INLINE static bool power_display_enabled() { return TEST(flags, PM_DISP_BIT_P); }
|
||||||
|
FORCE_INLINE static void set_power_display(const bool b) { SET_BIT_TO(flags, PM_DISP_BIT_P, b); }
|
||||||
|
FORCE_INLINE static void toggle_power_display() { TBI(flags, PM_DISP_BIT_I); }
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static void reset() {
|
||||||
|
flags = 0x00;
|
||||||
|
|
||||||
|
#if ENABLED(POWER_MONITOR_CURRENT)
|
||||||
|
amps.reset();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if ENABLED(POWER_MONITOR_VOLTAGE)
|
||||||
|
volts.reset();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if ENABLED(SDSUPPORT)
|
||||||
|
display_item_ms = 0;
|
||||||
|
display_item = 0;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
static void capture_values() {
|
||||||
|
#if ENABLED(POWER_MONITOR_CURRENT)
|
||||||
|
amps.capture();
|
||||||
|
#endif
|
||||||
|
#if ENABLED(POWER_MONITOR_VOLTAGE)
|
||||||
|
volts.capture();
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
extern PowerMonitor power_monitor;
|
@ -0,0 +1,70 @@
|
|||||||
|
/**
|
||||||
|
* Marlin 3D Printer Firmware
|
||||||
|
* Copyright (C) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
||||||
|
*
|
||||||
|
* Based on Sprinter and grbl.
|
||||||
|
* Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "../../../inc/MarlinConfig.h"
|
||||||
|
|
||||||
|
#if HAS_POWER_MONITOR
|
||||||
|
|
||||||
|
#include "../../../feature/power_monitor.h"
|
||||||
|
#include "../../../Marlin.h"
|
||||||
|
#include "../../gcode.h"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* M430: Enable/disable current LCD display
|
||||||
|
* With no parameters report the system current draw (in Amps)
|
||||||
|
*
|
||||||
|
* I[bool] - Set Display of current on the LCD
|
||||||
|
* V[bool] - Set Display of voltage on the LCD
|
||||||
|
* W[bool] - Set Display of power on the LCD
|
||||||
|
*/
|
||||||
|
void GcodeSuite::M430() {
|
||||||
|
bool do_report = true;
|
||||||
|
#if HAS_SPI_LCD
|
||||||
|
#if ENABLED(POWER_MONITOR_CURRENT)
|
||||||
|
if (parser.seen('I')) { power_monitor.set_current_display(parser.value_bool()); do_report = false; }
|
||||||
|
#endif
|
||||||
|
#if HAS_POWER_MONITOR_VREF
|
||||||
|
if (parser.seen('V')) { power_monitor.set_voltage_display(parser.value_bool()); do_report = false; }
|
||||||
|
#endif
|
||||||
|
#if HAS_POWER_MONITOR_WATTS
|
||||||
|
if (parser.seen('W')) { power_monitor.set_power_display(parser.value_bool()); do_report = false; }
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
if (do_report) {
|
||||||
|
SERIAL_ECHOLNPAIR(
|
||||||
|
#if ENABLED(POWER_MONITOR_CURRENT)
|
||||||
|
"Current: ", power_monitor.getAmps(), "A"
|
||||||
|
#if HAS_POWER_MONITOR_VREF
|
||||||
|
" "
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
#if HAS_POWER_MONITOR_VREF
|
||||||
|
"Voltage: ", power_monitor.getVolts(), "V"
|
||||||
|
#endif
|
||||||
|
#if HAS_POWER_MONITOR_WATTS
|
||||||
|
" Power: ", power_monitor.getPower(), "W"
|
||||||
|
#endif
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // HAS_POWER_MONITOR
|
@ -0,0 +1,62 @@
|
|||||||
|
/**
|
||||||
|
* Marlin 3D Printer Firmware
|
||||||
|
* Copyright (C) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
||||||
|
*
|
||||||
|
* Based on Sprinter and grbl.
|
||||||
|
* Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
//
|
||||||
|
// Power Monitor Menu
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "../../inc/MarlinConfigPre.h"
|
||||||
|
|
||||||
|
#if HAS_LCD_MENU && HAS_POWER_MONITOR
|
||||||
|
|
||||||
|
#include "menu.h"
|
||||||
|
#include "../../feature/power_monitor.h"
|
||||||
|
|
||||||
|
void menu_power_monitor() {
|
||||||
|
START_MENU();
|
||||||
|
MENU_BACK(MSG_MAIN);
|
||||||
|
|
||||||
|
#if ENABLED(POWER_MONITOR_CURRENT)
|
||||||
|
{
|
||||||
|
bool ena = power_monitor.current_display_enabled();
|
||||||
|
MENU_ITEM_EDIT_CALLBACK(bool, MSG_CURRENT, &ena, power_monitor.toggle_current_display);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if HAS_POWER_MONITOR_VREF
|
||||||
|
{
|
||||||
|
bool ena = power_monitor.voltage_display_enabled();
|
||||||
|
MENU_ITEM_EDIT_CALLBACK(bool, MSG_VOLTAGE, &ena, power_monitor.toggle_voltage_display);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if HAS_POWER_MONITOR_WATTS
|
||||||
|
{
|
||||||
|
bool ena = power_monitor.power_display_enabled();
|
||||||
|
MENU_ITEM_EDIT_CALLBACK(bool, MSG_POWER, &ena, power_monitor.toggle_power_display);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
END_MENU();
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // HAS_LCD_MENU && HAS_POWER_MONITOR
|
Loading…
Reference in New Issue