🎨 Laser Ammeter followup (#22079)

Followup to #21835
This commit is contained in:
Scott Lahteine 2021-06-08 07:51:28 -05:00 committed by Scott Lahteine
parent 2fd9971f41
commit 3f103c91f0
13 changed files with 90 additions and 116 deletions

View file

@ -3283,15 +3283,6 @@
//#define AIR_ASSIST_PIN 44 // Override the default Air Assist pin //#define AIR_ASSIST_PIN 44 // Override the default Air Assist pin
#endif #endif
//
// Laser I2C Ammeter (High precision INA226 low/high side module)
//
//#define I2C_AMMETER
#if ENABLED(I2C_AMMETER)
#define I2C_AMMETER_IMAX .1 // Calibration value for the expected current range in Amps (use float e.g. 1.0)
#define I2C_AMMETER_SHUNT_RESISTOR .1 // Calibration shunt resistor value in ohms
#endif
//#define SPINDLE_SERVO // A servo converting an angle to spindle power //#define SPINDLE_SERVO // A servo converting an angle to spindle power
#ifdef SPINDLE_SERVO #ifdef SPINDLE_SERVO
#define SPINDLE_SERVO_NR 0 // Index of servo used for spindle control #define SPINDLE_SERVO_NR 0 // Index of servo used for spindle control
@ -3424,8 +3415,18 @@
#define SPINDLE_LASER_POWERDOWN_DELAY 50 // (ms) Delay to allow the spindle to stop #define SPINDLE_LASER_POWERDOWN_DELAY 50 // (ms) Delay to allow the spindle to stop
#endif #endif
//
// Laser I2C Ammeter (High precision INA226 low/high side module)
//
//#define I2C_AMMETER
#if ENABLED(I2C_AMMETER)
#define I2C_AMMETER_IMAX 0.1 // (Amps) Calibration value for the expected current range
#define I2C_AMMETER_SHUNT_RESISTOR 0.1 // (Ohms) Calibration shunt resistor value
#endif
#endif #endif
#endif #endif // SPINDLE_FEATURE || LASER_FEATURE
/** /**
* Synchronous Laser Control with M106/M107 * Synchronous Laser Control with M106/M107

View file

@ -1,4 +1,4 @@
/** /**
* Marlin 3D Printer Firmware * Marlin 3D Printer Firmware
* Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
* *
@ -23,27 +23,32 @@
#include "../inc/MarlinConfig.h" #include "../inc/MarlinConfig.h"
#if ENABLED(I2C_AMMETER) #if ENABLED(I2C_AMMETER)
#include "ammeter.h"
INA226 ina; #include "ammeter.h"
Ammeter ammeter; #ifndef I2C_AMMETER_IMAX
#define I2C_AMMETER_IMAX 0.500 // Calibration range 500 Milliamps
#endif
float Ammeter::scale; INA226 ina;
float Ammeter::current;
void Ammeter::init() { Ammeter ammeter;
ina.begin();
ina.configure(INA226_AVERAGES_16, INA226_BUS_CONV_TIME_1100US, INA226_SHUNT_CONV_TIME_1100US, INA226_MODE_SHUNT_BUS_CONT);
ina.calibrate(I2C_AMMETER_SHUNT_RESISTOR,I2C_AMMETER_IMAX);
}
float Ammeter::read() { float Ammeter::scale;
scale = 1; float Ammeter::current;
current = ina.readShuntCurrent();
if (current <= .0001) current = 0; // Cleanup lsb bit amplification errors
if (current < .1) scale = 1000;
return current * scale;
}
#endif //I2C_AMMETER void Ammeter::init() {
ina.begin();
ina.configure(INA226_AVERAGES_16, INA226_BUS_CONV_TIME_1100US, INA226_SHUNT_CONV_TIME_1100US, INA226_MODE_SHUNT_BUS_CONT);
ina.calibrate(I2C_AMMETER_SHUNT_RESISTOR, I2C_AMMETER_IMAX);
}
float Ammeter::read() {
scale = 1;
current = ina.readShuntCurrent();
if (current <= 0.0001f) current = 0; // Clean up least-significant-bit amplification errors
if (current < 0.1f) scale = 1000;
return current * scale;
}
#endif // I2C_AMMETER

View file

@ -26,19 +26,14 @@
#include <Wire.h> #include <Wire.h>
#include <INA226.h> #include <INA226.h>
#ifndef I2C_AMMETER_IMAX
#define I2C_AMMETER_IMAX .500 // Calibration range 500 Milli Amps
#endif
class Ammeter { class Ammeter {
private: private:
static float scale; static float scale;
public: public:
static float current; static float current;
static void init(); static void init();
static float read(); static float read();
}; };
extern Ammeter ammeter; extern Ammeter ammeter;

View file

@ -79,9 +79,8 @@ void SpindleLaser::init() {
OUT_WRITE(AIR_ASSIST_PIN, !AIR_ASSIST_ACTIVE); // Init Air Assist OFF OUT_WRITE(AIR_ASSIST_PIN, !AIR_ASSIST_ACTIVE); // Init Air Assist OFF
#endif #endif
#if ENABLED(I2C_AMMETER) #if ENABLED(I2C_AMMETER)
ammeter.init(); // Init I2C Ammeter ammeter.init(); // Init I2C Ammeter
#endif #endif
} }
#if ENABLED(SPINDLE_LASER_PWM) #if ENABLED(SPINDLE_LASER_PWM)

View file

@ -405,6 +405,10 @@
#endif #endif
#if EITHER(LCD_I2C_TYPE_MCP23017, LCD_I2C_TYPE_MCP23008) && DISABLED(NO_LCD_DETECT)
#define DETECT_I2C_LCD_DEVICE 1
#endif
#ifndef STD_ENCODER_PULSES_PER_STEP #ifndef STD_ENCODER_PULSES_PER_STEP
#if ENABLED(TOUCH_SCREEN) #if ENABLED(TOUCH_SCREEN)
#define STD_ENCODER_PULSES_PER_STEP 2 #define STD_ENCODER_PULSES_PER_STEP 2

View file

@ -68,11 +68,7 @@
#elif EITHER(LCD_I2C_TYPE_MCP23017, LCD_I2C_TYPE_MCP23008) #elif EITHER(LCD_I2C_TYPE_MCP23017, LCD_I2C_TYPE_MCP23008)
LCD_CLASS lcd(LCD_I2C_ADDRESS LCD_CLASS lcd(LCD_I2C_ADDRESS OPTARG(DETECT_I2C_LCD_DEVICE, 1));
#ifdef DETECT_DEVICE
, 1
#endif
);
#elif ENABLED(LCD_I2C_TYPE_PCA8574) #elif ENABLED(LCD_I2C_TYPE_PCA8574)
@ -380,11 +376,7 @@ void MarlinUI::init_lcd() {
} }
bool MarlinUI::detected() { bool MarlinUI::detected() {
return (true return TERN1(DETECT_I2C_LCD_DEVICE, lcd.LcdDetected() == 1);
#if EITHER(LCD_I2C_TYPE_MCP23017, LCD_I2C_TYPE_MCP23008) && defined(DETECT_DEVICE)
&& lcd.LcdDetected() == 1
#endif
);
} }
#if HAS_SLOW_BUTTONS #if HAS_SLOW_BUTTONS
@ -602,10 +594,11 @@ FORCE_INLINE void _draw_cooler_status(const char prefix, const bool blink) {
FORCE_INLINE void _draw_ammeter_status() { FORCE_INLINE void _draw_ammeter_status() {
lcd_put_u8str(" "); lcd_put_u8str(" ");
ammeter.read(); ammeter.read();
if (ammeter.current <= .999) { if (ammeter.current <= 0.999f) {
lcd_put_u8str(ftostr3ns(ammeter.current)); lcd_put_u8str(ui16tostr3rj(uint16_t(ammeter.current * 1000 + 0.5f)));
lcd_put_u8str("mA"); lcd_put_u8str("mA");
} else { }
else {
lcd_put_u8str(ftostr12ns(ammeter.current)); lcd_put_u8str(ftostr12ns(ammeter.current));
lcd_put_wchar('A'); lcd_put_wchar('A');
} }
@ -847,15 +840,9 @@ void MarlinUI::draw_status_screen() {
#endif #endif
#endif #endif
#if HAS_COOLER TERN_(HAS_COOLER, _draw_cooler_status('*', blink));
_draw_cooler_status('*', blink); TERN_(LASER_COOLANT_FLOW_METER, _draw_flowmeter_status());
#endif TERN_(I2C_AMMETER, _draw_ammeter_status());
#if ENABLED(LASER_COOLANT_FLOW_METER)
_draw_flowmeter_status();
#endif
#if ENABLED(I2C_AMMETER)
_draw_ammeter_status();
#endif
#endif // LCD_WIDTH >= 20 #endif // LCD_WIDTH >= 20

View file

@ -110,14 +110,13 @@
// //
// Laser Ammeter // Laser Ammeter
// //
#if !STATUS_AMMETER_WIDTH && ENABLED(I2C_AMMETER) #if ENABLED(I2C_AMMETER)
#include "status/ammeter.h" #if !STATUS_AMMETER_WIDTH
#endif #include "status/ammeter.h"
#ifndef STATUS_AMMETER_WIDTH #endif
#define STATUS_AMMETER_WIDTH 0 #ifndef STATUS_AMMETER_WIDTH
#endif #define STATUS_AMMETER_WIDTH 0
#ifndef STATUS_AMMETER_BYTEWIDTH #endif
#define STATUS_AMMETER_BYTEWIDTH BW(STATUS_AMMETER_WIDTH)
#endif #endif
// //
@ -614,30 +613,29 @@
#endif #endif
#endif #endif
#if ENABLED(I2C_AMMETER) //
#if STATUS_AMMETER_WIDTH // I2C Laser Ammeter
//
#ifndef STATUS_AMMETER_X #if ENABLED(I2C_AMMETER) && STATUS_AMMETER_WIDTH
#define STATUS_AMMETER_X (LCD_PIXEL_WIDTH - (STATUS_AMMETER_BYTEWIDTH + STATUS_FLOWMETER_BYTEWIDTH + STATUS_FAN_BYTEWIDTH + STATUS_CUTTER_BYTEWIDTH + STATUS_COOLER_BYTEWIDTH) * 8) #ifndef STATUS_AMMETER_BYTEWIDTH
#endif #define STATUS_AMMETER_BYTEWIDTH BW(STATUS_AMMETER_WIDTH)
#ifndef STATUS_AMMETER_HEIGHT
#define STATUS_AMMETER_HEIGHT(S) (sizeof(status_ammeter_bmp1) / (STATUS_AMMETER_BYTEWIDTH))
#endif
#ifndef STATUS_AMMETER_Y
#define STATUS_AMMETER_Y(S) (18 - STATUS_AMMETER_HEIGHT(S))
#endif
#ifndef STATUS_AMMETER_TEXT_X
#define STATUS_AMMETER_TEXT_X (STATUS_AMMETER_X + 7)
#endif
static_assert(
sizeof(status_ammeter_bmp1) == (STATUS_AMMETER_BYTEWIDTH) * STATUS_AMMETER_HEIGHT(0),
"Status ammeter bitmap (status_ammeter_bmp1) dimensions don't match data."
);
#endif #endif
#ifndef STATUS_AMMETER_X
#define STATUS_AMMETER_X (LCD_PIXEL_WIDTH - (STATUS_AMMETER_BYTEWIDTH + STATUS_FLOWMETER_BYTEWIDTH + STATUS_FAN_BYTEWIDTH + STATUS_CUTTER_BYTEWIDTH + STATUS_COOLER_BYTEWIDTH) * 8)
#endif
#ifndef STATUS_AMMETER_HEIGHT
#define STATUS_AMMETER_HEIGHT(S) (sizeof(status_ammeter_bmp1) / (STATUS_AMMETER_BYTEWIDTH))
#endif
#ifndef STATUS_AMMETER_Y
#define STATUS_AMMETER_Y(S) (18 - STATUS_AMMETER_HEIGHT(S))
#endif
#ifndef STATUS_AMMETER_TEXT_X
#define STATUS_AMMETER_TEXT_X (STATUS_AMMETER_X + 7)
#endif
static_assert(
sizeof(status_ammeter_bmp1) == (STATUS_AMMETER_BYTEWIDTH) * STATUS_AMMETER_HEIGHT(0),
"Status ammeter bitmap (status_ammeter_bmp1) dimensions don't match data."
);
#endif #endif
// //

View file

@ -1,6 +1,6 @@
/** /**
* Marlin 3D Printer Firmware * Marlin 3D Printer Firmware
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] * Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
* *
* Based on Sprinter and grbl. * Based on Sprinter and grbl.
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
@ -24,11 +24,10 @@
// //
// lcd/dogm/status/ammeter.h - Status Screen Laser Ammeter bitmaps // lcd/dogm/status/ammeter.h - Status Screen Laser Ammeter bitmaps
// //
#if ENABLED(I2C_AMMETER)
#define STATUS_AMMETER_WIDTH 20 #define STATUS_AMMETER_WIDTH 20
const unsigned char status_ammeter_bmp_mA[] PROGMEM = { const unsigned char status_ammeter_bmp_mA[] PROGMEM = {
B00000000,B11111100,B00000000, B00000000,B11111100,B00000000,
B00000011,B00000011,B00000000, B00000011,B00000011,B00000000,
B00000100,B00000000,B10000000, B00000100,B00000000,B10000000,
@ -46,7 +45,7 @@
B00000100,B00000000,B10000000, B00000100,B00000000,B10000000,
B00000011,B00000011,B00000000, B00000011,B00000011,B00000000,
B00000000,B11111100,B00000000 B00000000,B11111100,B00000000
}; };
const unsigned char status_ammeter_bmp_A[] PROGMEM = { const unsigned char status_ammeter_bmp_A[] PROGMEM = {
B00000000,B11111100,B00000000, B00000000,B11111100,B00000000,
@ -67,5 +66,3 @@ const unsigned char status_ammeter_bmp_A[] PROGMEM = {
B00000011,B00000011,B00000000, B00000011,B00000011,B00000000,
B00000000,B11111100,B00000000, B00000000,B11111100,B00000000,
}; };
#endif

View file

@ -697,7 +697,7 @@ void MarlinUI::draw_status_screen() {
const uint8_t ammetery = STATUS_AMMETER_Y(status_ammeter_bmp_mA), const uint8_t ammetery = STATUS_AMMETER_Y(status_ammeter_bmp_mA),
ammeterh = STATUS_AMMETER_HEIGHT(status_ammeter_bmp_mA); ammeterh = STATUS_AMMETER_HEIGHT(status_ammeter_bmp_mA);
if (PAGE_CONTAINS(ammetery, ammetery + ammeterh - 1)) if (PAGE_CONTAINS(ammetery, ammetery + ammeterh - 1))
u8g.drawBitmapP(STATUS_AMMETER_X, ammetery, STATUS_AMMETER_BYTEWIDTH, ammeterh, (ammeter.current < .1) ? status_ammeter_bmp_mA : status_ammeter_bmp_A); u8g.drawBitmapP(STATUS_AMMETER_X, ammetery, STATUS_AMMETER_BYTEWIDTH, ammeterh, (ammeter.current < 0.1f) ? status_ammeter_bmp_mA : status_ammeter_bmp_A);
#endif #endif
// Heated Bed // Heated Bed

View file

@ -217,15 +217,6 @@ const char* ftostr41ns(const_float_t f) {
return &conv[2]; return &conv[2];
} }
// Convert unsigned float to string with 123 format
const char* ftostr3ns(const_float_t f) {
const long i = UINTFLOAT(f, 3);
conv[4] = DIGIMOD(i, 100);
conv[5] = DIGIMOD(i, 10);
conv[6] = DIGIMOD(i, 1);
return &conv[4];
}
// Convert signed float to fixed-length string with 12.34 / _2.34 / -2.34 or -23.45 / 123.45 format // Convert signed float to fixed-length string with 12.34 / _2.34 / -2.34 or -23.45 / 123.45 format
const char* ftostr42_52(const_float_t f) { const char* ftostr42_52(const_float_t f) {
if (f <= -10 || f >= 100) return ftostr52(f); // -23.45 / 123.45 if (f <= -10 || f >= 100) return ftostr52(f); // -23.45 / 123.45

View file

@ -74,9 +74,6 @@ const char* ftostr31ns(const_float_t x);
// Convert unsigned float to string with 123.4 format // Convert unsigned float to string with 123.4 format
const char* ftostr41ns(const_float_t x); const char* ftostr41ns(const_float_t x);
// Convert unsigned float to string with 123 format
const char* ftostr3ns(const_float_t x);
// Convert signed float to fixed-length string with 12.34 / _2.34 / -2.34 or -23.45 / 123.45 format // Convert signed float to fixed-length string with 12.34 / _2.34 / -2.34 or -23.45 / 123.45 format
const char* ftostr42_52(const_float_t x); const char* ftostr42_52(const_float_t x);

View file

@ -193,7 +193,7 @@ opt_set MOTHERBOARD BOARD_RAMPS_14_EFB EXTRUDERS 0 LCD_LANGUAGE en TEMP_SENSOR_C
MANUAL_FEEDRATE '{ 50*60, 50*60, 4*60 }' \ MANUAL_FEEDRATE '{ 50*60, 50*60, 4*60 }' \
AXIS_RELATIVE_MODES '{ false, false, false }' AXIS_RELATIVE_MODES '{ false, false, false }'
opt_enable REPRAP_DISCOUNT_SMART_CONTROLLER SDSUPPORT EEPROM_SETTINGS EEPROM_BOOT_SILENT EEPROM_AUTO_INIT \ opt_enable REPRAP_DISCOUNT_SMART_CONTROLLER SDSUPPORT EEPROM_SETTINGS EEPROM_BOOT_SILENT EEPROM_AUTO_INIT \
LASER_FEATURE AIR_EVACUATION AIR_EVACUATION_PIN AIR_ASSIST AIR_ASSIST_PIN LASER_COOLANT_FLOW_METER LASER_FEATURE AIR_EVACUATION AIR_EVACUATION_PIN AIR_ASSIST AIR_ASSIST_PIN LASER_COOLANT_FLOW_METER I2C_AMMETER
exec_test $1 $2 "REPRAP MEGA2560 RAMPS | Laser Feature | Air Evacuation | Air Assist | Cooler | Flowmeter | 44780 LCD " "$3" exec_test $1 $2 "REPRAP MEGA2560 RAMPS | Laser Feature | Air Evacuation | Air Assist | Cooler | Flowmeter | 44780 LCD " "$3"