Skip to content

Commit

Permalink
🎨 Laser Ammeter followup (MarlinFirmware#22079)
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkyhead committed Jun 15, 2021
1 parent 2fd9971 commit 3f103c9
Show file tree
Hide file tree
Showing 13 changed files with 90 additions and 116 deletions.
21 changes: 11 additions & 10 deletions Marlin/Configuration_adv.h
Original file line number Diff line number Diff line change
Expand Up @@ -3283,15 +3283,6 @@
//#define AIR_ASSIST_PIN 44 // Override the default Air Assist pin
#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
#ifdef SPINDLE_SERVO
#define SPINDLE_SERVO_NR 0 // Index of servo used for spindle control
Expand Down Expand Up @@ -3424,8 +3415,18 @@
#define SPINDLE_LASER_POWERDOWN_DELAY 50 // (ms) Delay to allow the spindle to stop

#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 // SPINDLE_FEATURE || LASER_FEATURE

/**
* Synchronous Laser Control with M106/M107
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/HAL/STM32/eeprom_flash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

// Better: "utility/stm32_eeprom.h", but only after updating stm32duino to 2.0.0
// Use EEPROM.h for compatibility, for now.
#include <EEPROM.h>
#include <EEPROM.h>

/**
* The STM32 HAL supports chips that deal with "pages" and some with "sectors" and some that
Expand Down
43 changes: 24 additions & 19 deletions Marlin/src/feature/ammeter.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/**
* Marlin 3D Printer Firmware
* Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
*
Expand All @@ -23,27 +23,32 @@
#include "../inc/MarlinConfig.h"

#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;
float Ammeter::current;
INA226 ina;

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);
}
Ammeter ammeter;

float Ammeter::read() {
scale = 1;
current = ina.readShuntCurrent();
if (current <= .0001) current = 0; // Cleanup lsb bit amplification errors
if (current < .1) scale = 1000;
return current * scale;
}
float Ammeter::scale;
float Ammeter::current;

#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
13 changes: 4 additions & 9 deletions Marlin/src/feature/ammeter.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,14 @@
#include <Wire.h>
#include <INA226.h>

#ifndef I2C_AMMETER_IMAX
#define I2C_AMMETER_IMAX .500 // Calibration range 500 Milli Amps
#endif

class Ammeter {
private:
static float scale;
static float scale;

public:
static float current;
static void init();
static float read();

static float current;
static void init();
static float read();
};

extern Ammeter ammeter;
3 changes: 1 addition & 2 deletions Marlin/src/feature/spindle_laser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,8 @@ void SpindleLaser::init() {
OUT_WRITE(AIR_ASSIST_PIN, !AIR_ASSIST_ACTIVE); // Init Air Assist OFF
#endif
#if ENABLED(I2C_AMMETER)
ammeter.init(); // Init I2C Ammeter
ammeter.init(); // Init I2C Ammeter
#endif

}

#if ENABLED(SPINDLE_LASER_PWM)
Expand Down
4 changes: 4 additions & 0 deletions Marlin/src/inc/Conditionals_LCD.h
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,10 @@

#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
#if ENABLED(TOUCH_SCREEN)
#define STD_ENCODER_PULSES_PER_STEP 2
Expand Down
31 changes: 9 additions & 22 deletions Marlin/src/lcd/HD44780/marlinui_HD44780.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,7 @@

#elif EITHER(LCD_I2C_TYPE_MCP23017, LCD_I2C_TYPE_MCP23008)

LCD_CLASS lcd(LCD_I2C_ADDRESS
#ifdef DETECT_DEVICE
, 1
#endif
);
LCD_CLASS lcd(LCD_I2C_ADDRESS OPTARG(DETECT_I2C_LCD_DEVICE, 1));

#elif ENABLED(LCD_I2C_TYPE_PCA8574)

Expand Down Expand Up @@ -380,11 +376,7 @@ void MarlinUI::init_lcd() {
}

bool MarlinUI::detected() {
return (true
#if EITHER(LCD_I2C_TYPE_MCP23017, LCD_I2C_TYPE_MCP23008) && defined(DETECT_DEVICE)
&& lcd.LcdDetected() == 1
#endif
);
return TERN1(DETECT_I2C_LCD_DEVICE, lcd.LcdDetected() == 1);
}

#if HAS_SLOW_BUTTONS
Expand Down Expand Up @@ -602,10 +594,11 @@ FORCE_INLINE void _draw_cooler_status(const char prefix, const bool blink) {
FORCE_INLINE void _draw_ammeter_status() {
lcd_put_u8str(" ");
ammeter.read();
if (ammeter.current <= .999) {
lcd_put_u8str(ftostr3ns(ammeter.current));
if (ammeter.current <= 0.999f) {
lcd_put_u8str(ui16tostr3rj(uint16_t(ammeter.current * 1000 + 0.5f)));
lcd_put_u8str("mA");
} else {
}
else {
lcd_put_u8str(ftostr12ns(ammeter.current));
lcd_put_wchar('A');
}
Expand Down Expand Up @@ -847,15 +840,9 @@ void MarlinUI::draw_status_screen() {
#endif
#endif

#if HAS_COOLER
_draw_cooler_status('*', blink);
#endif
#if ENABLED(LASER_COOLANT_FLOW_METER)
_draw_flowmeter_status();
#endif
#if ENABLED(I2C_AMMETER)
_draw_ammeter_status();
#endif
TERN_(HAS_COOLER, _draw_cooler_status('*', blink));
TERN_(LASER_COOLANT_FLOW_METER, _draw_flowmeter_status());
TERN_(I2C_AMMETER, _draw_ammeter_status());

#endif // LCD_WIDTH >= 20

Expand Down
60 changes: 29 additions & 31 deletions Marlin/src/lcd/dogm/dogm_Statusscreen.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,13 @@
//
// Laser Ammeter
//
#if !STATUS_AMMETER_WIDTH && ENABLED(I2C_AMMETER)
#include "status/ammeter.h"
#endif
#ifndef STATUS_AMMETER_WIDTH
#define STATUS_AMMETER_WIDTH 0
#endif
#ifndef STATUS_AMMETER_BYTEWIDTH
#define STATUS_AMMETER_BYTEWIDTH BW(STATUS_AMMETER_WIDTH)
#if ENABLED(I2C_AMMETER)
#if !STATUS_AMMETER_WIDTH
#include "status/ammeter.h"
#endif
#ifndef STATUS_AMMETER_WIDTH
#define STATUS_AMMETER_WIDTH 0
#endif
#endif

//
Expand Down Expand Up @@ -614,30 +613,29 @@
#endif
#endif

#if ENABLED(I2C_AMMETER)
#if STATUS_AMMETER_WIDTH

#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."
);
//
// I2C Laser Ammeter
//
#if ENABLED(I2C_AMMETER) && STATUS_AMMETER_WIDTH
#ifndef STATUS_AMMETER_BYTEWIDTH
#define STATUS_AMMETER_BYTEWIDTH BW(STATUS_AMMETER_WIDTH)
#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

//
Expand Down
11 changes: 4 additions & 7 deletions Marlin/src/lcd/dogm/status/ammeter.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* 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.
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
Expand All @@ -24,11 +24,10 @@
//
// 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,
B00000011,B00000011,B00000000,
B00000100,B00000000,B10000000,
Expand All @@ -46,7 +45,7 @@
B00000100,B00000000,B10000000,
B00000011,B00000011,B00000000,
B00000000,B11111100,B00000000
};
};

const unsigned char status_ammeter_bmp_A[] PROGMEM = {
B00000000,B11111100,B00000000,
Expand All @@ -67,5 +66,3 @@ const unsigned char status_ammeter_bmp_A[] PROGMEM = {
B00000011,B00000011,B00000000,
B00000000,B11111100,B00000000,
};

#endif
4 changes: 2 additions & 2 deletions Marlin/src/lcd/dogm/status_screen_DOGM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ FORCE_INLINE void _draw_centered_temp(const celsius_t temp, const uint8_t tx, co

#if DO_DRAW_AMMETER
FORCE_INLINE void _draw_centered_current(const float current, const uint8_t tx, const uint8_t ty) {
const char *str = ftostr31ns(current);
const char *str = ftostr31ns(current);
const uint8_t len = str[0] != ' ' ? 3 : str[1] != ' ' ? 2 : 1;
lcd_put_u8str(tx - len * (INFO_FONT_WIDTH) / 2 + 1, ty, &str[3-len]);
}
Expand Down Expand Up @@ -697,7 +697,7 @@ void MarlinUI::draw_status_screen() {
const uint8_t ammetery = STATUS_AMMETER_Y(status_ammeter_bmp_mA),
ammeterh = STATUS_AMMETER_HEIGHT(status_ammeter_bmp_mA);
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

// Heated Bed
Expand Down
9 changes: 0 additions & 9 deletions Marlin/src/libs/numtostr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,15 +217,6 @@ const char* ftostr41ns(const_float_t f) {
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
const char* ftostr42_52(const_float_t f) {
if (f <= -10 || f >= 100) return ftostr52(f); // -23.45 / 123.45
Expand Down
3 changes: 0 additions & 3 deletions Marlin/src/libs/numtostr.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,6 @@ const char* ftostr31ns(const_float_t x);
// Convert unsigned float to string with 123.4 format
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
const char* ftostr42_52(const_float_t x);

Expand Down
2 changes: 1 addition & 1 deletion buildroot/tests/mega2560
Original file line number Diff line number Diff line change
Expand Up @@ -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 }' \
AXIS_RELATIVE_MODES '{ false, false, false }'
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"

Expand Down

0 comments on commit 3f103c9

Please sign in to comment.