Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkyhead committed Mar 29, 2021
1 parent 4517668 commit f4583cd
Show file tree
Hide file tree
Showing 18 changed files with 153 additions and 119 deletions.
21 changes: 12 additions & 9 deletions Marlin/Configuration_adv.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,16 +203,19 @@
#define COOLER_FAN_FACTOR 25 // PWM increase per °C above target
#endif
#endif

//
// Laser flowmeter feature
// Laser Coolant Flow Meter
//
//#define FLOWMETER_FEATURE
#if ENABLED(FLOWMETER_FEATURE)
#define FLOWMETER_PIN 20 // Requires an external interupt enabled pin, e.g. RAMPS pins 2,3,18,19,20,21
#define FLOWMETER_PPL 5880 // Flow meter pulses per liter.
#define FLOWMETER_SAFETY true // Prevents running the CO2 laser tube without a minimum water flow rate of FLOWMETER_MIN liters/min.
#define FLOWMETER_MIN 1.5 // Minimum flow required when FLOWMETER_SAFETY is true
#define FLOWMETER_INTERVAL 1000 // Flow rate calculation interval in milliseconds
//#define LASER_COOLANT_FLOW_METER
#if ENABLED(LASER_COOLANT_FLOW_METER)
#define FLOWMETER_PIN 20 // Requires an external interrupt-enabled pin (e.g., RAMPS 2,3,18,19,20,21)
#define FLOWMETER_PPL 5880 // (pulses/liter) Flow meter pulses-per-liter on the input pin
#define FLOWMETER_INTERVAL 1000 // (ms) Flow rate calculation interval in milliseconds
#define FLOWMETER_SAFETY // Prevent running the laser without the minimum flow rate set below
#if ENABLED(FLOWMETER_SAFETY)
#define FLOWMETER_MIN_LITERS_PER_MINUTE 1.5 // (liters/min) Minimum flow required when enabled
#endif
#endif

/**
Expand Down Expand Up @@ -1550,7 +1553,7 @@
#define STATUS_CHAMBER_ANIM // Use a second bitmap to indicate chamber heating
//#define STATUS_CUTTER_ANIM // Use a second bitmap to indicate spindle / laser active
//#define STATUS_COOLER_ANIM // Use a second bitmap to indicate laser cooling
//#define STATUS_FLOWMETER_ANIM // Use a multiple bitmaps to indicate coolant flow
//#define STATUS_FLOWMETER_ANIM // Use multiple bitmaps to indicate coolant flow
//#define STATUS_ALT_BED_BITMAP // Use the alternative bed bitmap
//#define STATUS_ALT_FAN_BITMAP // Use the alternative fan bitmap
//#define STATUS_FAN_FRAMES 3 // :[0,1,2,3,4] Number of fan animation frames
Expand Down
5 changes: 1 addition & 4 deletions Marlin/src/core/language.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@
#define STR_COUNT_A " Count A:"
#define STR_WATCHDOG_FIRED "Watchdog timeout. Reset required."
#define STR_ERR_KILLED "Printer halted. kill() called!"
#define STR_FLOWMETER_FAULT "Coolant flow fault, flowmeter safety is active, attention required."
#define STR_FLOWMETER_FAULT "Coolant flow fault. Flowmeter safety is active. Attention required."
#define STR_ERR_STOPPED "Printer stopped due to errors. Fix the error and use M999 to restart. (Temperature is reset. Set it after restarting)"
#define STR_ERR_SERIAL_MISMATCH "Serial status mismatch"
#define STR_BUSY_PROCESSING "busy: processing"
Expand Down Expand Up @@ -321,8 +321,6 @@
#define LCD_STR_FEEDRATE "\x06"
#define LCD_STR_CLOCK "\x07"
#define LCD_STR_ARROW_RIGHT ">" /* from the default character set */
#define LCD_STR_ASTERISK "\x2A"
#define LCD_STR_TILDE "\x7E"

#else
//
Expand All @@ -339,7 +337,6 @@
#define LCD_STR_THERMOMETER "\x08"
#define LCD_STR_DEGREE "\x09"


#define LCD_STR_SPECIAL_MAX '\x09'
// Maximum here is 0x1F because 0x20 is ' ' (space) and the normal charsets begin.
// Better stay below 0x10 because DISPLAY_CHARSET_HD44780_WESTERN begins here.
Expand Down
28 changes: 17 additions & 11 deletions Marlin/src/feature/cooler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,26 @@

#include "../inc/MarlinConfig.h"

#if HAS_COOLER || HAS_FLOWMETER
#if HAS_COOLER

#include "cooler.h"
Cooler cooler;

float Cooler::flowrate; // Flow meter reading in liters per minute, if less than 1.5Lpm, laser will be shut off with saftey true
volatile uint16_t Cooler::flowpulses; // flowmeter irq pulse count
millis_t Cooler::flow_calc_time = FLOWMETER_INTERVAL;
uint8_t Cooler::mode = 0; // 0 = CO2 Liquid cooling, 1 = Laser Diode TEC Heatsink Cooling
uint16_t Cooler::capacity; // Cooling capacity in watts
uint16_t Cooler::load; // Cooling load in watts
bool Cooler::flowmeter = false;
bool Cooler::state = false; // on = true, off = false
bool Cooler::fault = false; // Cooler is in a fault state On = True, Off = False
bool Cooler::flowmeter_safety = true; // on = true, off = false
uint8_t Cooler::mode = 0;
uint16_t Cooler::capacity;
uint16_t Cooler::load;
bool Cooler::enabled = false;

#if ENABLED(LASER_COOLANT_FLOW_METER)
bool Cooler::flowmeter = false;
millis_t Cooler::flowmeter_next_ms; // = 0
volatile uint16_t Cooler::flowpulses;
float Cooler::flowrate;
#endif

#if ENABLED(FLOWMETER_SAFETY)
bool Cooler::flowsafety_enabled = true;
bool Cooler::fault = false;
#endif

#endif // HAS_COOLER
98 changes: 69 additions & 29 deletions Marlin/src/feature/cooler.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,50 +21,90 @@
*/
#pragma once

#include <stdint.h>
#include "../inc/MarlinConfigPre.h"

#define _MSG_COOLER(M) MSG_COOLER_##M
#define MSG_COOLER(M) _MSG_COOLER(M)
#define FLOW_RATE_MLPP 0.170068 // ml per pulse
#ifndef FLOWMETER_PPL
#define FLOWMETER_PPL 5880 // Pulses per liter
#endif
#ifndef FLOWMETER_INTERVAL
#define FLOWMETER_INTERVAL 1000 // Default interval if not define in configuration_adv.h
#define FLOWMETER_INTERVAL 1000 // milliseconds
#endif

// Cooling device

class Cooler {
public:
static float flowrate; // Flow meter reading in liters, 0 will result in shutdown if equiped
static volatile uint16_t flowpulses; // Flowmeter irq pulse count
static millis_t flow_calc_time; // Elasped time duration used for calculation of the flowrate
static uint8_t mode; // 0 = CO2 Liquid cooling, 1 = Laser Diode TEC Heatsink Cooling
static uint16_t capacity; // Cooling capacity in watts
static uint16_t load; // Cooling load in watts
static bool flowmeter; // Enabled = True, Disabled = False
static bool state; // On = True, Off = False
static bool flowmeter_safety; // On = True, Off = False
static bool fault; // Cooler is in a fault state On = True, Off = False
static bool is_enabled() { return state; }
static bool flowsaftey_is_enabled() { return flowmeter_safety; }
static void enable() { state = true; }
static void disable() { state = false; }
static void flowsafety_enable() { flowmeter_safety = true; }
static void flowsafety_disable() { flowmeter_safety = false; }
static void set_mode(const uint8_t m) { mode = m; }
static void set_flowmeter(const bool sflag) { flowmeter = sflag; }
static uint16_t capacity; // Cooling capacity in watts
static uint16_t load; // Cooling load in watts

static bool enabled;
static void enable() { enabled = true; }
static void disable() { enabled = false; }
static void toggle() { enabled = !enabled; }

static uint8_t mode; // 0 = CO2 Liquid cooling, 1 = Laser Diode TEC Heatsink Cooling
static void set_mode(const uint8_t m) { mode = m; }

#if ENABLED(LASER_COOLANT_FLOW_METER)
static float flowrate; // Flow meter reading in liters-per-minute.
static bool flowmeter; // Flag to monitor the flow
static volatile uint16_t flowpulses; // Flowmeter IRQ pulse count
static millis_t flowmeter_next_ms; // Next time at which to calculate flow

static void set_flowmeter(const bool sflag) {
if (flowmeter != sflag) {
flowmeter = sflag;
if (sflag) {
flowpulses = 0;
flowmeter_next_ms = millis() + FLOWMETER_INTERVAL;
}
}
}

#if ENABLED(HAS_FLOWMETER) && PIN_EXISTS(FLOWMETER)
static void flowmeter_ISR() { flowpulses ++; } // Keep minimal,all we need here is the count
static void flowmeter_enable() {
// To calculate flow we only need to count pulses
static void flowmeter_ISR() { flowpulses++; }

// Enable / Disable the flow meter interrupt
static void flowmeter_interrupt_enable() {
attachInterrupt(digitalPinToInterrupt(FLOWMETER_PIN), flowmeter_ISR, RISING);
}
static void flowmeter_disable() {
static void flowmeter_interrupt_disable() {
detachInterrupt(digitalPinToInterrupt(FLOWMETER_PIN));
}

// Enable / Disable the flow meter interrupt
static void flowmeter_enable() { set_flowmeter(true); flowpulses = 0; flowmeter_interrupt_enable(); }
static void flowmeter_disable() { set_flowmeter(false); flowmeter_interrupt_disable(); flowpulses = 0; }

// Get the total flow (in liters per minute) since the last reading
static void calc_flowrate() {
flowrate = flowpulses * 60 * (1000 / FLOWMETER_INTERVAL) * FLOW_RATE_MLPP; // Total flow until now
flowpulses = 0;
//flowmeter_interrupt_disable();
// const uint16_t pulses = flowpulses;
//flowmeter_interrupt_enable();
flowrate = flowpulses * 60.0f * (1000.0f / (FLOWMETER_INTERVAL)) * (1000.0f / (FLOWMETER_PPL));
flowpulses = 0;
}

// Userland task to update the flow meter
static void flowmeter_task(const millis_t ms=millis()) {
if (!flowmeter) // !! The flow meter must always be on !!
flowmeter_enable(); // Init and prime
if (ELAPSED(ms, flowmeter_next_ms)) {
calc_flowrate();
flowmeter_next_ms = ms + FLOWMETER_INTERVAL;
}
}

#if ENABLED(FLOWMETER_SAFETY)
static bool fault; // Flag that the cooler is in a fault state
static bool flowsafety_enabled; // Flag to disable the cutter if flow rate is too low
static void flowsafety_toggle() { flowsafety_enabled = !flowsafety_enabled; }
static bool check_flow_too_low() {
const bool too_low = flowsafety_enabled && flowrate < (FLOWMETER_MIN_LITERS_PER_MINUTE);
if (too_low) fault = true;
return too_low;
}
#endif
#endif
};

Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/feature/spindle_laser.h
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ class SpindleLaser {
* If not set defaults to 80% power
*/
static inline void test_fire_pulse() {
TERN_(USE_BEEPER, buzzer.tone(30, 3000));
TERN_(USE_BEEPER, buzzer.tone(30, 3000));
enable_forward(); // Turn Laser on (Spindle speak but same funct)
delay(testPulse); // Delay for time set by user in pulse ms menu screen.
disable(); // Turn laser off
Expand Down
6 changes: 3 additions & 3 deletions Marlin/src/gcode/gcode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ GcodeSuite gcode;
#include "../feature/spindle_laser.h"
#endif

#if HAS_FLOWMETER && FLOWMETER_SAFETY
#if ENABLED(FLOWMETER_SAFETY)
#include "../feature/cooler.h"
#endif

Expand Down Expand Up @@ -282,8 +282,8 @@ void GcodeSuite::process_parsed_command(const bool no_ok/*=false*/) {
}
#endif

#if HAS_FLOWMETER && FLOWMETER_SAFETY
if (cooler.fault == true) {
#if ENABLED(FLOWMETER_SAFETY)
if (cooler.fault) {
SERIAL_ECHO_MSG(STR_FLOWMETER_FAULT);
return;
}
Expand Down
3 changes: 0 additions & 3 deletions Marlin/src/inc/Conditionals_post.h
Original file line number Diff line number Diff line change
Expand Up @@ -2137,9 +2137,6 @@
#if HAS_HEATED_BED || HAS_TEMP_CHAMBER
#define BED_OR_CHAMBER 1
#endif
#if PIN_EXISTS(FLOWMETER)
#define HAS_FLOWMETER 1
#endif
#if HAS_TEMP_HOTEND || BED_OR_CHAMBER || HAS_TEMP_PROBE || HAS_TEMP_COOLER
#define HAS_TEMP_SENSOR 1
#endif
Expand Down
4 changes: 2 additions & 2 deletions Marlin/src/inc/SanityCheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -1895,8 +1895,8 @@ static_assert(hbm[Z_AXIS] >= 0, "HOMING_BUMP_MM.Z must be greater than or equal
#error "TEMP_SENSOR_COOLER requires LASER_FEATURE and TEMP_COOLER_PIN."
#endif

#if ENABLED(FLOWMETER_FEATURE) && !(PIN_EXISTS(FLOWMETER) && ENABLED(LASER_FEATURE))
#error "FLOWMETER_FEATURE requires FLOWMETER_PIN and LASER_FEATURE."
#if ENABLED(LASER_COOLANT_FLOW_METER) && !(PIN_EXISTS(FLOWMETER) && ENABLED(LASER_FEATURE))
#error "LASER_COOLANT_FLOW_METER requires FLOWMETER_PIN and LASER_FEATURE."
#endif

#if ENABLED(CHAMBER_FAN) && !(defined(CHAMBER_FAN_MODE) && WITHIN(CHAMBER_FAN_MODE, 0, 2))
Expand Down
4 changes: 2 additions & 2 deletions Marlin/src/lcd/HD44780/marlinui_HD44780.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ FORCE_INLINE void _draw_cooler_status(const char prefix, const bool blink) {
lcd_put_u8str(ftostr11ns(cooler.flowrate));
lcd_put_wchar('L');
}
#endif
#endif

FORCE_INLINE void _draw_bed_status(const bool blink) {
_draw_heater_status(H_BED, TERN0(HAS_LEVELING, blink && planner.leveling_active) ? '_' : LCD_STR_BEDTEMP[0], blink);
Expand Down Expand Up @@ -825,7 +825,7 @@ void MarlinUI::draw_status_screen() {
#endif

#if HAS_COOLER
_draw_cooler_status(LCD_STR_ASTERISK[0], blink);
_draw_cooler_status('*', blink);
#endif
#if HAS_FLOWMETER
_draw_flowmeter_status();
Expand Down
6 changes: 3 additions & 3 deletions Marlin/src/lcd/dogm/dogm_Statusscreen.h
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@
//
// Cooler Bitmap Properties
//
#if HAS_COOLER
#if HAS_COOLER
#if STATUS_COOLER_WIDTH

#ifndef STATUS_COOLER_X
Expand Down Expand Up @@ -587,12 +587,12 @@
#endif

static_assert(
sizeof(status_flowmeter_bmp1) == (STATUS_FLOWMETER_BYTEWIDTH) * (STATUS_FLOWMETER_HEIGHT(0)),
sizeof(status_flowmeter_bmp1) == (STATUS_FLOWMETER_BYTEWIDTH) * STATUS_FLOWMETER_HEIGHT(0),
"Status flowmeter bitmap (status_flowmeter_bmp1) dimensions don't match data."
);
#ifdef STATUS_COOLER_ANIM
static_assert(
sizeof(status_flowmeter_bmp2) == (STATUS_FLOWMETER_BYTEWIDTH) * (STATUS_FLOWMETER_HEIGHT(1)),
sizeof(status_flowmeter_bmp2) == (STATUS_FLOWMETER_BYTEWIDTH) * STATUS_FLOWMETER_HEIGHT(1),
"Status flowmeter bitmap (status_flowmeter_bmp2) dimensions don't match data."
);
#endif
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/lcd/dogm/status/cooler.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
B00000001,B01000000,
B00000010,B10100000,
B00000100,B10010000
};
};
#endif

#if HAS_FLOWMETER
Expand Down
30 changes: 13 additions & 17 deletions Marlin/src/lcd/dogm/status_screen_DOGM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
#include "../../feature/spindle_laser.h"
#endif

#if HAS_COOLER || HAS_FLOWMETER
#if HAS_COOLER || HAS_FLOWMETER
#include "../../feature/cooler.h"
#endif

Expand Down Expand Up @@ -199,7 +199,7 @@ FORCE_INLINE void _draw_centered_temp(const celsius_t temp, const uint8_t tx, co
lcd_put_u8str(tx - len * (INFO_FONT_WIDTH) / 2 + 1, ty, &str[3-len]);
lcd_put_u8str("L");
}
#endif
#endif

#if DO_DRAW_HOTENDS

Expand Down Expand Up @@ -384,22 +384,18 @@ FORCE_INLINE void _draw_centered_temp(const celsius_t temp, const uint8_t tx, co
}
#endif

#if HAS_COOLER
#if DO_DRAW_COOLER
FORCE_INLINE void _draw_cooler_status() {
if (PAGE_CONTAINS(28 - INFO_FONT_ASCENT, 28 - 1))
_draw_centered_temp(thermalManager.degCooler(), STATUS_COOLER_TEXT_X, 28);
}
#endif
#if DO_DRAW_COOLER
FORCE_INLINE void _draw_cooler_status() {
if (PAGE_CONTAINS(28 - INFO_FONT_ASCENT, 28 - 1))
_draw_centered_temp(thermalManager.degCooler(), STATUS_COOLER_TEXT_X, 28);
}
#endif

#if HAS_FLOWMETER
#if DO_DRAW_FLOWMETER
FORCE_INLINE void _draw_flowmeter_status() {
if (PAGE_CONTAINS(28 - INFO_FONT_ASCENT, 28 - 1))
_draw_centered_flowrate(cooler.flowrate , STATUS_FLOWMETER_TEXT_X, 28);
}
#endif
#if DO_DRAW_FLOWMETER
FORCE_INLINE void _draw_flowmeter_status() {
if (PAGE_CONTAINS(28 - INFO_FONT_ASCENT, 28 - 1))
_draw_centered_flowrate(cooler.flowrate, STATUS_FLOWMETER_TEXT_X, 28);
}
#endif

//
Expand Down Expand Up @@ -664,7 +660,7 @@ void MarlinUI::draw_status_screen() {
const uint8_t coolery = STATUS_COOLER_Y(status_cooler_bmp1),
coolerh = STATUS_COOLER_HEIGHT(status_cooler_bmp1);
if (PAGE_CONTAINS(coolery, coolery + coolerh - 1))
u8g.drawBitmapP(STATUS_COOLER_X, coolery, STATUS_COOLER_BYTEWIDTH, coolerh, blink && cooler.is_enabled() ? status_cooler_bmp2 : status_cooler_bmp1);
u8g.drawBitmapP(STATUS_COOLER_X, coolery, STATUS_COOLER_BYTEWIDTH, coolerh, blink && cooler.enabled ? status_cooler_bmp2 : status_cooler_bmp1);
#endif

// Laser Cooler Flow Meter
Expand Down
6 changes: 2 additions & 4 deletions Marlin/src/lcd/marlinui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1514,10 +1514,8 @@ void MarlinUI::update() {
}

void MarlinUI::flow_fault() {
LCD_MESSAGEPGM(MSG_FLOWMETER_FAULT);
#if HAS_BUZZER
BUZZ(1000,440);
#endif
LCD_ALERTMESSAGEPGM(MSG_FLOWMETER_FAULT);
TERN_(HAS_BUZZER, buzz(1000, 440));
TERN_(HAS_LCD_MENU, return_to_status());
}

Expand Down

0 comments on commit f4583cd

Please sign in to comment.