Skip to content

Commit

Permalink
Add Laser Based I2C Ammeter Feature (MarlinFirmware#21835)
Browse files Browse the repository at this point in the history
  • Loading branch information
descipher authored and thinkyhead committed Jun 15, 2021
1 parent a3063a9 commit 2fd9971
Show file tree
Hide file tree
Showing 12 changed files with 314 additions and 34 deletions.
9 changes: 9 additions & 0 deletions Marlin/Configuration_adv.h
Original file line number Diff line number Diff line change
Expand Up @@ -3283,6 +3283,15 @@
//#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
49 changes: 49 additions & 0 deletions Marlin/src/feature/ammeter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* Marlin 3D Printer Firmware
* Copyright (c) 2021 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 <https://www.gnu.org/licenses/>.
*
*/

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

#if ENABLED(I2C_AMMETER)
#include "ammeter.h"

INA226 ina;

Ammeter ammeter;

float Ammeter::scale;
float Ammeter::current;

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 <= .0001) current = 0; // Cleanup lsb bit amplification errors
if (current < .1) scale = 1000;
return current * scale;
}

#endif //I2C_AMMETER
44 changes: 44 additions & 0 deletions Marlin/src/feature/ammeter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Marlin 3D Printer Firmware
* Copyright (c) 2021 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 <https://www.gnu.org/licenses/>.
*
*/
#pragma once

#include "../inc/MarlinConfigPre.h"

#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;

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

};

extern Ammeter ammeter;
8 changes: 8 additions & 0 deletions Marlin/src/feature/spindle_laser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
#include "../module/servo.h"
#endif

#if ENABLED(I2C_AMMETER)
#include "../feature/ammeter.h"
#endif

SpindleLaser cutter;
uint8_t SpindleLaser::power;
#if ENABLED(LASER_FEATURE)
Expand Down Expand Up @@ -74,6 +78,10 @@ void SpindleLaser::init() {
#if ENABLED(AIR_ASSIST)
OUT_WRITE(AIR_ASSIST_PIN, !AIR_ASSIST_ACTIVE); // Init Air Assist OFF
#endif
#if ENABLED(I2C_AMMETER)
ammeter.init(); // Init I2C Ammeter
#endif

}

#if ENABLED(SPINDLE_LASER_PWM)
Expand Down
23 changes: 22 additions & 1 deletion Marlin/src/lcd/HD44780/marlinui_HD44780.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@
#include "../../feature/cooler.h"
#endif

#if ENABLED(I2C_AMMETER)
#include "../../feature/ammeter.h"
#endif

#if ENABLED(AUTO_BED_LEVELING_UBL)
#include "../../feature/bedlevel/bedlevel.h"
#endif
Expand Down Expand Up @@ -588,12 +592,26 @@ FORCE_INLINE void _draw_cooler_status(const char prefix, const bool blink) {

#if ENABLED(LASER_COOLANT_FLOW_METER)
FORCE_INLINE void _draw_flowmeter_status() {
lcd_put_u8str("~ ");
lcd_put_u8str("~");
lcd_put_u8str(ftostr11ns(cooler.flowrate));
lcd_put_wchar('L');
}
#endif

#if ENABLED(I2C_AMMETER)
FORCE_INLINE void _draw_ammeter_status() {
lcd_put_u8str(" ");
ammeter.read();
if (ammeter.current <= .999) {
lcd_put_u8str(ftostr3ns(ammeter.current));
lcd_put_u8str("mA");
} else {
lcd_put_u8str(ftostr12ns(ammeter.current));
lcd_put_wchar('A');
}
}
#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 @@ -835,6 +853,9 @@ void MarlinUI::draw_status_screen() {
#if ENABLED(LASER_COOLANT_FLOW_METER)
_draw_flowmeter_status();
#endif
#if ENABLED(I2C_AMMETER)
_draw_ammeter_status();
#endif

#endif // LCD_WIDTH >= 20

Expand Down
42 changes: 41 additions & 1 deletion Marlin/src/lcd/dogm/dogm_Statusscreen.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,18 @@
#define STATUS_FLOWMETER_BYTEWIDTH BW(STATUS_FLOWMETER_WIDTH)
#endif


//
// 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)
#endif

//
// Bed
Expand Down Expand Up @@ -603,6 +614,32 @@
#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."
);
#endif
#endif

//
// Bed Bitmap Properties
//
Expand Down Expand Up @@ -696,6 +733,9 @@
#if ENABLED(LASER_COOLANT_FLOW_METER)
#define DO_DRAW_FLOWMETER 1
#endif
#if ENABLED(I2C_AMMETER)
#define DO_DRAW_AMMETER 1
#endif

#if HAS_TEMP_CHAMBER && STATUS_CHAMBER_WIDTH && HOTENDS <= 4
#define DO_DRAW_CHAMBER 1
Expand Down
71 changes: 71 additions & 0 deletions Marlin/src/lcd/dogm/status/ammeter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* Marlin 3D Printer Firmware
* Copyright (c) 2020 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 <https://www.gnu.org/licenses/>.
*
*/
#pragma once

//
// lcd/dogm/status/ammeter.h - Status Screen Laser Ammeter bitmaps
//
#if ENABLED(I2C_AMMETER)

#define STATUS_AMMETER_WIDTH 20

const unsigned char status_ammeter_bmp_mA[] PROGMEM = {
B00000000,B11111100,B00000000,
B00000011,B00000011,B00000000,
B00000100,B00000000,B10000000,
B00001000,B00000000,B01000000,
B00010000,B00000110,B00100000,
B00010000,B00001001,B00100000,
B00100000,B00001001,B00010000,
B00100011,B01001111,B00010000,
B11100010,B10101001,B00011100,
B00100010,B10101001,B00010000,
B00100010,B10101001,B00010000,
B00010000,B00000000,B00100000,
B00010000,B00000000,B00100000,
B00001000,B00000000,B01000000,
B00000100,B00000000,B10000000,
B00000011,B00000011,B00000000,
B00000000,B11111100,B00000000
};

const unsigned char status_ammeter_bmp_A[] PROGMEM = {
B00000000,B11111100,B00000000,
B00000011,B00000011,B00000000,
B00000100,B00000000,B10000000,
B00001000,B00000000,B01000000,
B00010000,B00000000,B00100000,
B00010000,B00110000,B00100000,
B00100000,B01001000,B00010000,
B00100000,B01001000,B00010000,
B11100000,B01111000,B00011100,
B00100000,B01001000,B00010000,
B00100000,B01001000,B00010000,
B00010000,B01001000,B00100000,
B00010000,B00000000,B00100000,
B00001000,B00000000,B01000000,
B00000100,B00000000,B10000000,
B00000011,B00000011,B00000000,
B00000000,B11111100,B00000000,
};

#endif
64 changes: 32 additions & 32 deletions Marlin/src/lcd/dogm/status/cooler.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,40 +29,40 @@
#define STATUS_COOLER_WIDTH 22

const unsigned char status_cooler_bmp2[] PROGMEM = {
B00000100,B00000010,B00000000,
B00000100,B10010010,B01000000,
B00010101,B00001010,B10000000,
B00001110,B00000111,B00000000,
B00111111,B10111111,B11000000,
B00001110,B00000111,B00000000,
B00010101,B00001010,B10000000,
B00100100,B00100010,B01000000,
B00000100,B00100000,B00000000,
B00000001,B00100100,B00000000,
B00000000,B10101000,B00000000,
B00000000,B01110000,B00000000,
B00000111,B11111111,B00000000,
B00000000,B01110000,B00000000,
B00000000,B10101000,B00000000,
B00000001,B00100100,B00000000
B00000001,B00000000,B10000000,
B00000001,B00100100,B10010000,
B00000101,B01000010,B10100000,
B00000011,B10000001,B11000000,
B00001111,B11101111,B11110000,
B00000011,B10000001,B11000000,
B00000101,B01000010,B10100000,
B00001001,B00001000,B10010000,
B00000001,B00001000,B00000000,
B00000000,B01001001,B00000000,
B00000000,B00101010,B00000000,
B00000000,B00011100,B00000000,
B00000001,B11111111,B11000000,
B00000000,B00011100,B00000000,
B00000000,B00101010,B00000000,
B00000000,B01001001,B00000000
};
const unsigned char status_cooler_bmp1[] PROGMEM = {
B00000100,B00000010,B00000000,
B00000100,B10010010,B01000000,
B00010101,B00001010,B10000000,
B00001010,B00000101,B00000000,
B00110001,B11011000,B11000000,
B00001010,B00000101,B00000000,
B00010101,B00001010,B10000000,
B00100100,B00100010,B01000000,
B00000100,B00100000,B00000000,
B00000001,B00100100,B00000000,
B00000000,B10101000,B00000000,
B00000000,B01010000,B00000000,
B00000111,B10001111,B00000000,
B00000000,B01010000,B00000000,
B00000000,B10101000,B00000000,
B00000001,B00100100,B00000000
B00000001,B00000000,B10000000,
B00000001,B00100100,B10010000,
B00000101,B01000010,B10100000,
B00000010,B10000001,B01000000,
B00001100,B01110110,B00110000,
B00000010,B10000001,B01000000,
B00000101,B01000010,B10100000,
B00001001,B00001000,B10010000,
B00000001,B00001000,B00000000,
B00000000,B01001001,B00000000,
B00000000,B00101010,B00000000,
B00000000,B00010100,B00000000,
B00000001,B11100011,B11000000,
B00000000,B00010100,B00000000,
B00000000,B00101010,B00000000,
B00000000,B01001001,B00000000
};

#endif
Expand Down
Loading

0 comments on commit 2fd9971

Please sign in to comment.