Browse Source

🎨 Laser Ammeter followup (#22079)

Followup to #21835
Scott Lahteine 3 years ago
parent
commit
d320545066
No account linked to committer's email address

+ 11
- 10
Marlin/Configuration_adv.h View File

3283
     //#define AIR_ASSIST_PIN            44     // Override the default Air Assist pin
3283
     //#define AIR_ASSIST_PIN            44     // Override the default Air Assist pin
3284
   #endif
3284
   #endif
3285
 
3285
 
3286
-  //
3287
-  // Laser I2C Ammeter (High precision INA226 low/high side module)
3288
-  //
3289
-  //#define I2C_AMMETER
3290
-  #if ENABLED(I2C_AMMETER)
3291
-    #define I2C_AMMETER_IMAX            .1     // Calibration value for the expected current range in Amps (use float e.g. 1.0)
3292
-    #define I2C_AMMETER_SHUNT_RESISTOR  .1     // Calibration shunt resistor value in ohms
3293
-  #endif
3294
-
3295
   //#define SPINDLE_SERVO                      // A servo converting an angle to spindle power
3286
   //#define SPINDLE_SERVO                      // A servo converting an angle to spindle power
3296
   #ifdef SPINDLE_SERVO
3287
   #ifdef SPINDLE_SERVO
3297
     #define SPINDLE_SERVO_NR   0               // Index of servo used for spindle control
3288
     #define SPINDLE_SERVO_NR   0               // Index of servo used for spindle control
3424
       #define SPINDLE_LASER_POWERDOWN_DELAY   50 // (ms) Delay to allow the spindle to stop
3415
       #define SPINDLE_LASER_POWERDOWN_DELAY   50 // (ms) Delay to allow the spindle to stop
3425
 
3416
 
3426
     #endif
3417
     #endif
3418
+
3419
+    //
3420
+    // Laser I2C Ammeter (High precision INA226 low/high side module)
3421
+    //
3422
+    //#define I2C_AMMETER
3423
+    #if ENABLED(I2C_AMMETER)
3424
+      #define I2C_AMMETER_IMAX            0.1    // (Amps) Calibration value for the expected current range
3425
+      #define I2C_AMMETER_SHUNT_RESISTOR  0.1    // (Ohms) Calibration shunt resistor value
3426
+    #endif
3427
+
3427
   #endif
3428
   #endif
3428
-#endif
3429
+#endif // SPINDLE_FEATURE || LASER_FEATURE
3429
 
3430
 
3430
 /**
3431
 /**
3431
  * Synchronous Laser Control with M106/M107
3432
  * Synchronous Laser Control with M106/M107

+ 1
- 1
Marlin/src/HAL/STM32/eeprom_flash.cpp View File

30
 
30
 
31
 // Better: "utility/stm32_eeprom.h", but only after updating stm32duino to 2.0.0
31
 // Better: "utility/stm32_eeprom.h", but only after updating stm32duino to 2.0.0
32
 // Use EEPROM.h for compatibility, for now.
32
 // Use EEPROM.h for compatibility, for now.
33
-#include <EEPROM.h> 
33
+#include <EEPROM.h>
34
 
34
 
35
 /**
35
 /**
36
  * The STM32 HAL supports chips that deal with "pages" and some with "sectors" and some that
36
  * The STM32 HAL supports chips that deal with "pages" and some with "sectors" and some that

+ 24
- 19
Marlin/src/feature/ammeter.cpp View File

1
-  /**
1
+/**
2
  * Marlin 3D Printer Firmware
2
  * Marlin 3D Printer Firmware
3
  * Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
3
  * Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4
  *
4
  *
23
 #include "../inc/MarlinConfig.h"
23
 #include "../inc/MarlinConfig.h"
24
 
24
 
25
 #if ENABLED(I2C_AMMETER)
25
 #if ENABLED(I2C_AMMETER)
26
-  #include "ammeter.h"
27
 
26
 
28
-  INA226 ina;
27
+#include "ammeter.h"
29
 
28
 
30
-  Ammeter ammeter;
29
+#ifndef I2C_AMMETER_IMAX
30
+  #define I2C_AMMETER_IMAX     0.500  // Calibration range 500 Milliamps
31
+#endif
31
 
32
 
32
-  float Ammeter::scale;
33
-  float Ammeter::current;
33
+INA226 ina;
34
 
34
 
35
-  void Ammeter::init() {
36
-    ina.begin();
37
-    ina.configure(INA226_AVERAGES_16, INA226_BUS_CONV_TIME_1100US, INA226_SHUNT_CONV_TIME_1100US, INA226_MODE_SHUNT_BUS_CONT);
38
-    ina.calibrate(I2C_AMMETER_SHUNT_RESISTOR,I2C_AMMETER_IMAX);
39
-  }
35
+Ammeter ammeter;
40
 
36
 
41
-  float Ammeter::read() {
42
-      scale = 1;
43
-      current = ina.readShuntCurrent();
44
-      if (current <= .0001) current = 0;  // Cleanup lsb bit amplification errors
45
-      if (current < .1) scale = 1000; 
46
-      return current * scale;
47
-  }
37
+float Ammeter::scale;
38
+float Ammeter::current;
48
 
39
 
49
-#endif //I2C_AMMETER
40
+void Ammeter::init() {
41
+  ina.begin();
42
+  ina.configure(INA226_AVERAGES_16, INA226_BUS_CONV_TIME_1100US, INA226_SHUNT_CONV_TIME_1100US, INA226_MODE_SHUNT_BUS_CONT);
43
+  ina.calibrate(I2C_AMMETER_SHUNT_RESISTOR, I2C_AMMETER_IMAX);
44
+}
45
+
46
+float Ammeter::read() {
47
+  scale = 1;
48
+  current = ina.readShuntCurrent();
49
+  if (current <= 0.0001f) current = 0;  // Clean up least-significant-bit amplification errors
50
+  if (current < 0.1f) scale = 1000;
51
+  return current * scale;
52
+}
53
+
54
+#endif // I2C_AMMETER

+ 4
- 9
Marlin/src/feature/ammeter.h View File

26
 #include <Wire.h>
26
 #include <Wire.h>
27
 #include <INA226.h>
27
 #include <INA226.h>
28
 
28
 
29
-#ifndef I2C_AMMETER_IMAX    
30
-  #define I2C_AMMETER_IMAX      .500 // Calibration range 500 Milli Amps
31
-#endif
32
-
33
 class Ammeter {
29
 class Ammeter {
34
 private:
30
 private:
35
-    static float scale;
31
+  static float scale;
36
 
32
 
37
 public:
33
 public:
38
-    static float current;
39
-    static void init();
40
-    static float read();
41
- 
34
+  static float current;
35
+  static void init();
36
+  static float read();
42
 };
37
 };
43
 
38
 
44
 extern Ammeter ammeter;
39
 extern Ammeter ammeter;

+ 1
- 2
Marlin/src/feature/spindle_laser.cpp View File

79
     OUT_WRITE(AIR_ASSIST_PIN, !AIR_ASSIST_ACTIVE);                    // Init Air Assist OFF
79
     OUT_WRITE(AIR_ASSIST_PIN, !AIR_ASSIST_ACTIVE);                    // Init Air Assist OFF
80
   #endif
80
   #endif
81
   #if ENABLED(I2C_AMMETER)
81
   #if ENABLED(I2C_AMMETER)
82
-    ammeter.init();                    // Init I2C Ammeter
82
+    ammeter.init();                                                   // Init I2C Ammeter
83
   #endif
83
   #endif
84
-  
85
 }
84
 }
86
 
85
 
87
 #if ENABLED(SPINDLE_LASER_PWM)
86
 #if ENABLED(SPINDLE_LASER_PWM)

+ 4
- 0
Marlin/src/inc/Conditionals_LCD.h View File

405
 
405
 
406
 #endif
406
 #endif
407
 
407
 
408
+#if EITHER(LCD_I2C_TYPE_MCP23017, LCD_I2C_TYPE_MCP23008) && DISABLED(NO_LCD_DETECT)
409
+  #define DETECT_I2C_LCD_DEVICE 1
410
+#endif
411
+
408
 #ifndef STD_ENCODER_PULSES_PER_STEP
412
 #ifndef STD_ENCODER_PULSES_PER_STEP
409
   #if ENABLED(TOUCH_SCREEN)
413
   #if ENABLED(TOUCH_SCREEN)
410
     #define STD_ENCODER_PULSES_PER_STEP 2
414
     #define STD_ENCODER_PULSES_PER_STEP 2

+ 9
- 22
Marlin/src/lcd/HD44780/marlinui_HD44780.cpp View File

68
 
68
 
69
 #elif EITHER(LCD_I2C_TYPE_MCP23017, LCD_I2C_TYPE_MCP23008)
69
 #elif EITHER(LCD_I2C_TYPE_MCP23017, LCD_I2C_TYPE_MCP23008)
70
 
70
 
71
-  LCD_CLASS lcd(LCD_I2C_ADDRESS
72
-    #ifdef DETECT_DEVICE
73
-      , 1
74
-    #endif
75
-  );
71
+  LCD_CLASS lcd(LCD_I2C_ADDRESS OPTARG(DETECT_I2C_LCD_DEVICE, 1));
76
 
72
 
77
 #elif ENABLED(LCD_I2C_TYPE_PCA8574)
73
 #elif ENABLED(LCD_I2C_TYPE_PCA8574)
78
 
74
 
380
 }
376
 }
381
 
377
 
382
 bool MarlinUI::detected() {
378
 bool MarlinUI::detected() {
383
-  return (true
384
-    #if EITHER(LCD_I2C_TYPE_MCP23017, LCD_I2C_TYPE_MCP23008) && defined(DETECT_DEVICE)
385
-      && lcd.LcdDetected() == 1
386
-    #endif
387
-  );
379
+  return TERN1(DETECT_I2C_LCD_DEVICE, lcd.LcdDetected() == 1);
388
 }
380
 }
389
 
381
 
390
 #if HAS_SLOW_BUTTONS
382
 #if HAS_SLOW_BUTTONS
602
   FORCE_INLINE void _draw_ammeter_status() {
594
   FORCE_INLINE void _draw_ammeter_status() {
603
     lcd_put_u8str(" ");
595
     lcd_put_u8str(" ");
604
     ammeter.read();
596
     ammeter.read();
605
-    if (ammeter.current <= .999) {
606
-      lcd_put_u8str(ftostr3ns(ammeter.current));
597
+    if (ammeter.current <= 0.999f) {
598
+      lcd_put_u8str(ui16tostr3rj(uint16_t(ammeter.current * 1000 + 0.5f)));
607
       lcd_put_u8str("mA");
599
       lcd_put_u8str("mA");
608
-    } else {
600
+    }
601
+    else {
609
       lcd_put_u8str(ftostr12ns(ammeter.current));
602
       lcd_put_u8str(ftostr12ns(ammeter.current));
610
       lcd_put_wchar('A');
603
       lcd_put_wchar('A');
611
     }
604
     }
847
         #endif
840
         #endif
848
       #endif
841
       #endif
849
 
842
 
850
-      #if HAS_COOLER
851
-        _draw_cooler_status('*', blink);
852
-      #endif
853
-      #if ENABLED(LASER_COOLANT_FLOW_METER)
854
-        _draw_flowmeter_status();
855
-      #endif
856
-      #if ENABLED(I2C_AMMETER)
857
-        _draw_ammeter_status();
858
-      #endif
843
+      TERN_(HAS_COOLER, _draw_cooler_status('*', blink));
844
+      TERN_(LASER_COOLANT_FLOW_METER, _draw_flowmeter_status());
845
+      TERN_(I2C_AMMETER, _draw_ammeter_status());
859
 
846
 
860
     #endif // LCD_WIDTH >= 20
847
     #endif // LCD_WIDTH >= 20
861
 
848
 

+ 29
- 31
Marlin/src/lcd/dogm/dogm_Statusscreen.h View File

110
 //
110
 //
111
 // Laser Ammeter
111
 // Laser Ammeter
112
 //
112
 //
113
-#if !STATUS_AMMETER_WIDTH && ENABLED(I2C_AMMETER)
114
-  #include "status/ammeter.h"
115
-#endif
116
-#ifndef STATUS_AMMETER_WIDTH
117
-  #define STATUS_AMMETER_WIDTH 0
118
-#endif
119
-#ifndef STATUS_AMMETER_BYTEWIDTH
120
-  #define STATUS_AMMETER_BYTEWIDTH BW(STATUS_AMMETER_WIDTH)
113
+#if ENABLED(I2C_AMMETER)
114
+  #if !STATUS_AMMETER_WIDTH
115
+    #include "status/ammeter.h"
116
+  #endif
117
+  #ifndef STATUS_AMMETER_WIDTH
118
+    #define STATUS_AMMETER_WIDTH 0
119
+  #endif
121
 #endif
120
 #endif
122
 
121
 
123
 //
122
 //
614
   #endif
613
   #endif
615
 #endif
614
 #endif
616
 
615
 
617
-#if ENABLED(I2C_AMMETER)
618
-  #if STATUS_AMMETER_WIDTH
619
-
620
-    #ifndef STATUS_AMMETER_X
621
-      #define STATUS_AMMETER_X (LCD_PIXEL_WIDTH - (STATUS_AMMETER_BYTEWIDTH + STATUS_FLOWMETER_BYTEWIDTH + STATUS_FAN_BYTEWIDTH + STATUS_CUTTER_BYTEWIDTH + STATUS_COOLER_BYTEWIDTH) * 8)
622
-    #endif
623
-
624
-    #ifndef STATUS_AMMETER_HEIGHT
625
-      #define STATUS_AMMETER_HEIGHT(S) (sizeof(status_ammeter_bmp1) / (STATUS_AMMETER_BYTEWIDTH))
626
-    #endif
627
-
628
-    #ifndef STATUS_AMMETER_Y
629
-      #define STATUS_AMMETER_Y(S) (18 - STATUS_AMMETER_HEIGHT(S))
630
-    #endif
631
-
632
-    #ifndef STATUS_AMMETER_TEXT_X
633
-      #define STATUS_AMMETER_TEXT_X (STATUS_AMMETER_X + 7)
634
-    #endif
635
-
636
-    static_assert(
637
-      sizeof(status_ammeter_bmp1) == (STATUS_AMMETER_BYTEWIDTH) * STATUS_AMMETER_HEIGHT(0),
638
-      "Status ammeter bitmap (status_ammeter_bmp1) dimensions don't match data."
639
-    );
616
+//
617
+// I2C Laser Ammeter
618
+//
619
+#if ENABLED(I2C_AMMETER) && STATUS_AMMETER_WIDTH
620
+  #ifndef STATUS_AMMETER_BYTEWIDTH
621
+    #define STATUS_AMMETER_BYTEWIDTH BW(STATUS_AMMETER_WIDTH)
640
   #endif
622
   #endif
623
+  #ifndef STATUS_AMMETER_X
624
+    #define STATUS_AMMETER_X (LCD_PIXEL_WIDTH - (STATUS_AMMETER_BYTEWIDTH + STATUS_FLOWMETER_BYTEWIDTH + STATUS_FAN_BYTEWIDTH + STATUS_CUTTER_BYTEWIDTH + STATUS_COOLER_BYTEWIDTH) * 8)
625
+  #endif
626
+  #ifndef STATUS_AMMETER_HEIGHT
627
+    #define STATUS_AMMETER_HEIGHT(S) (sizeof(status_ammeter_bmp1) / (STATUS_AMMETER_BYTEWIDTH))
628
+  #endif
629
+  #ifndef STATUS_AMMETER_Y
630
+    #define STATUS_AMMETER_Y(S) (18 - STATUS_AMMETER_HEIGHT(S))
631
+  #endif
632
+  #ifndef STATUS_AMMETER_TEXT_X
633
+    #define STATUS_AMMETER_TEXT_X (STATUS_AMMETER_X + 7)
634
+  #endif
635
+  static_assert(
636
+    sizeof(status_ammeter_bmp1) == (STATUS_AMMETER_BYTEWIDTH) * STATUS_AMMETER_HEIGHT(0),
637
+    "Status ammeter bitmap (status_ammeter_bmp1) dimensions don't match data."
638
+  );
641
 #endif
639
 #endif
642
 
640
 
643
 //
641
 //

+ 4
- 7
Marlin/src/lcd/dogm/status/ammeter.h View File

1
 /**
1
 /**
2
  * Marlin 3D Printer Firmware
2
  * Marlin 3D Printer Firmware
3
- * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
3
+ * Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4
  *
4
  *
5
  * Based on Sprinter and grbl.
5
  * Based on Sprinter and grbl.
6
  * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
6
  * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
24
 //
24
 //
25
 // lcd/dogm/status/ammeter.h - Status Screen Laser Ammeter bitmaps
25
 // lcd/dogm/status/ammeter.h - Status Screen Laser Ammeter bitmaps
26
 //
26
 //
27
-#if ENABLED(I2C_AMMETER)
28
 
27
 
29
-  #define STATUS_AMMETER_WIDTH 20
28
+#define STATUS_AMMETER_WIDTH 20
30
 
29
 
31
-  const unsigned char status_ammeter_bmp_mA[] PROGMEM = {
30
+const unsigned char status_ammeter_bmp_mA[] PROGMEM = {
32
   B00000000,B11111100,B00000000,
31
   B00000000,B11111100,B00000000,
33
   B00000011,B00000011,B00000000,
32
   B00000011,B00000011,B00000000,
34
   B00000100,B00000000,B10000000,
33
   B00000100,B00000000,B10000000,
46
   B00000100,B00000000,B10000000,
45
   B00000100,B00000000,B10000000,
47
   B00000011,B00000011,B00000000,
46
   B00000011,B00000011,B00000000,
48
   B00000000,B11111100,B00000000
47
   B00000000,B11111100,B00000000
49
-  };
48
+};
50
 
49
 
51
 const unsigned char status_ammeter_bmp_A[] PROGMEM = {
50
 const unsigned char status_ammeter_bmp_A[] PROGMEM = {
52
   B00000000,B11111100,B00000000,
51
   B00000000,B11111100,B00000000,
67
   B00000011,B00000011,B00000000,
66
   B00000011,B00000011,B00000000,
68
   B00000000,B11111100,B00000000,
67
   B00000000,B11111100,B00000000,
69
 };
68
 };
70
-
71
-#endif

+ 2
- 2
Marlin/src/lcd/dogm/status_screen_DOGM.cpp View File

207
 
207
 
208
 #if DO_DRAW_AMMETER
208
 #if DO_DRAW_AMMETER
209
   FORCE_INLINE void _draw_centered_current(const float current, const uint8_t tx, const uint8_t ty) {
209
   FORCE_INLINE void _draw_centered_current(const float current, const uint8_t tx, const uint8_t ty) {
210
-    const char *str = ftostr31ns(current);           
210
+    const char *str = ftostr31ns(current);
211
     const uint8_t len = str[0] != ' ' ? 3 : str[1] != ' ' ? 2 : 1;
211
     const uint8_t len = str[0] != ' ' ? 3 : str[1] != ' ' ? 2 : 1;
212
     lcd_put_u8str(tx - len * (INFO_FONT_WIDTH) / 2 + 1, ty, &str[3-len]);
212
     lcd_put_u8str(tx - len * (INFO_FONT_WIDTH) / 2 + 1, ty, &str[3-len]);
213
   }
213
   }
697
       const uint8_t ammetery = STATUS_AMMETER_Y(status_ammeter_bmp_mA),
697
       const uint8_t ammetery = STATUS_AMMETER_Y(status_ammeter_bmp_mA),
698
                     ammeterh = STATUS_AMMETER_HEIGHT(status_ammeter_bmp_mA);
698
                     ammeterh = STATUS_AMMETER_HEIGHT(status_ammeter_bmp_mA);
699
        if (PAGE_CONTAINS(ammetery, ammetery + ammeterh - 1))
699
        if (PAGE_CONTAINS(ammetery, ammetery + ammeterh - 1))
700
-        u8g.drawBitmapP(STATUS_AMMETER_X, ammetery, STATUS_AMMETER_BYTEWIDTH, ammeterh, (ammeter.current < .1) ? status_ammeter_bmp_mA : status_ammeter_bmp_A);
700
+        u8g.drawBitmapP(STATUS_AMMETER_X, ammetery, STATUS_AMMETER_BYTEWIDTH, ammeterh, (ammeter.current < 0.1f) ? status_ammeter_bmp_mA : status_ammeter_bmp_A);
701
     #endif
701
     #endif
702
 
702
 
703
     // Heated Bed
703
     // Heated Bed

+ 0
- 9
Marlin/src/libs/numtostr.cpp View File

217
   return &conv[2];
217
   return &conv[2];
218
 }
218
 }
219
 
219
 
220
-// Convert unsigned float to string with 123 format
221
-const char* ftostr3ns(const_float_t f) {
222
-  const long i = UINTFLOAT(f, 3);
223
-  conv[4] = DIGIMOD(i, 100);
224
-  conv[5] = DIGIMOD(i, 10);
225
-  conv[6] = DIGIMOD(i, 1);
226
-  return &conv[4];
227
-}
228
-
229
 // Convert signed float to fixed-length string with 12.34 / _2.34 / -2.34 or -23.45 / 123.45 format
220
 // Convert signed float to fixed-length string with 12.34 / _2.34 / -2.34 or -23.45 / 123.45 format
230
 const char* ftostr42_52(const_float_t f) {
221
 const char* ftostr42_52(const_float_t f) {
231
   if (f <= -10 || f >= 100) return ftostr52(f); // -23.45 / 123.45
222
   if (f <= -10 || f >= 100) return ftostr52(f); // -23.45 / 123.45

+ 0
- 3
Marlin/src/libs/numtostr.h View File

74
 // Convert unsigned float to string with 123.4 format
74
 // Convert unsigned float to string with 123.4 format
75
 const char* ftostr41ns(const_float_t x);
75
 const char* ftostr41ns(const_float_t x);
76
 
76
 
77
-// Convert unsigned float to string with 123 format
78
-const char* ftostr3ns(const_float_t x);
79
-
80
 // Convert signed float to fixed-length string with 12.34 / _2.34 / -2.34 or -23.45 / 123.45 format
77
 // Convert signed float to fixed-length string with 12.34 / _2.34 / -2.34 or -23.45 / 123.45 format
81
 const char* ftostr42_52(const_float_t x);
78
 const char* ftostr42_52(const_float_t x);
82
 
79
 

+ 1
- 1
buildroot/tests/mega2560 View File

193
         MANUAL_FEEDRATE '{ 50*60, 50*60, 4*60 }' \
193
         MANUAL_FEEDRATE '{ 50*60, 50*60, 4*60 }' \
194
         AXIS_RELATIVE_MODES '{ false, false, false }'
194
         AXIS_RELATIVE_MODES '{ false, false, false }'
195
 opt_enable REPRAP_DISCOUNT_SMART_CONTROLLER SDSUPPORT EEPROM_SETTINGS EEPROM_BOOT_SILENT EEPROM_AUTO_INIT \
195
 opt_enable REPRAP_DISCOUNT_SMART_CONTROLLER SDSUPPORT EEPROM_SETTINGS EEPROM_BOOT_SILENT EEPROM_AUTO_INIT \
196
-           LASER_FEATURE AIR_EVACUATION AIR_EVACUATION_PIN AIR_ASSIST AIR_ASSIST_PIN LASER_COOLANT_FLOW_METER
196
+           LASER_FEATURE AIR_EVACUATION AIR_EVACUATION_PIN AIR_ASSIST AIR_ASSIST_PIN LASER_COOLANT_FLOW_METER I2C_AMMETER
197
 
197
 
198
 exec_test $1 $2 "REPRAP MEGA2560 RAMPS | Laser Feature | Air Evacuation | Air Assist | Cooler | Flowmeter | 44780 LCD " "$3"
198
 exec_test $1 $2 "REPRAP MEGA2560 RAMPS | Laser Feature | Air Evacuation | Air Assist | Cooler | Flowmeter | 44780 LCD " "$3"
199
 
199
 

Loading…
Cancel
Save