Quellcode durchsuchen

Merge pull request #4967 from thinkyhead/rc_dac_menu

Revised DAC Drive Strength Menu
Scott Lahteine vor 8 Jahren
Ursprung
Commit
fc93796e42
6 geänderte Dateien mit 97 neuen und 31 gelöschten Zeilen
  1. 23
    17
      Marlin/dac_mcp4728.cpp
  2. 3
    1
      Marlin/dac_mcp4728.h
  3. 9
    0
      Marlin/language_en.h
  4. 19
    13
      Marlin/stepper_dac.cpp
  5. 2
    0
      Marlin/stepper_dac.h
  6. 41
    0
      Marlin/ultralcd.cpp

+ 23
- 17
Marlin/dac_mcp4728.cpp Datei anzeigen

@@ -34,7 +34,7 @@
34 34
 
35 35
 #if ENABLED(DAC_STEPPER_CURRENT)
36 36
 
37
-uint16_t     mcp4728_values[4];
37
+uint16_t mcp4728_values[XYZE];
38 38
 
39 39
 /**
40 40
  * Begin I2C, get current values (input register and eeprom) of mcp4728
@@ -42,16 +42,13 @@ uint16_t     mcp4728_values[4];
42 42
 void mcp4728_init() {
43 43
   Wire.begin();
44 44
   Wire.requestFrom(int(DAC_DEV_ADDRESS), 24);
45
-  while(Wire.available()) {
46
-    int deviceID = Wire.read();
47
-    int hiByte = Wire.read();
48
-    int loByte = Wire.read();
45
+  while (Wire.available()) {
46
+    char deviceID = Wire.read(),
47
+         hiByte = Wire.read(),
48
+         loByte = Wire.read();
49 49
 
50
-    int isEEPROM = (deviceID & 0B00001000) >> 3;
51
-    int channel = (deviceID & 0B00110000) >> 4;
52
-    if (isEEPROM != 1) {
53
-      mcp4728_values[channel] = word((hiByte & 0B00001111), loByte);
54
-    }
50
+    if (!(deviceID & 0x08))
51
+      mcp4728_values[(deviceID & 0x30) >> 4] = word((hiByte & 0x0F), loByte);
55 52
   }
56 53
 }
57 54
 
@@ -71,8 +68,8 @@ uint8_t mcp4728_analogWrite(uint8_t channel, uint16_t value) {
71 68
 uint8_t mcp4728_eepromWrite() {
72 69
   Wire.beginTransmission(DAC_DEV_ADDRESS);
73 70
   Wire.write(SEQWRITE);
74
-  for (uint8_t channel=0; channel <= 3; channel++) {
75
-    Wire.write(DAC_STEPPER_VREF << 7 | 0 << 5 | DAC_STEPPER_GAIN << 4 | highByte(mcp4728_values[channel]));
71
+  for (uint8_t channel = 0; channel < COUNT(channel); channel++) {
72
+    Wire.write(DAC_STEPPER_VREF << 7 | DAC_STEPPER_GAIN << 4 | highByte(mcp4728_values[channel]));
76 73
     Wire.write(lowByte(mcp4728_values[channel]));
77 74
   }
78 75
   return Wire.endTransmission();
@@ -83,7 +80,7 @@ uint8_t mcp4728_eepromWrite() {
83 80
  */
84 81
 uint8_t mcp4728_setVref_all(uint8_t value) {
85 82
   Wire.beginTransmission(DAC_DEV_ADDRESS);
86
-  Wire.write(VREFWRITE | value << 3 | value << 2 | value << 1 | value);
83
+  Wire.write(GAINWRITE | (value ? 0x0F : 0x00));
87 84
   return Wire.endTransmission();
88 85
 }
89 86
 /**
@@ -91,7 +88,7 @@ uint8_t mcp4728_setVref_all(uint8_t value) {
91 88
  */
92 89
 uint8_t mcp4728_setGain_all(uint8_t value) {
93 90
   Wire.beginTransmission(DAC_DEV_ADDRESS);
94
-  Wire.write(GAINWRITE | value << 3 | value << 2 | value << 1 | value);
91
+  Wire.write(GAINWRITE | (value ? 0x0F : 0x00));
95 92
   return Wire.endTransmission();
96 93
 }
97 94
 
@@ -105,13 +102,22 @@ uint16_t mcp4728_getValue(uint8_t channel) { return mcp4728_values[channel]; }
105 102
  * Return Vout
106 103
  *
107 104
 uint16_t mcp4728_getVout(uint8_t channel) {
108
-  uint32_t vref = 2048;
109
-  uint32_t vOut = (vref * mcp4728_values[channel] * (_DAC_STEPPER_GAIN + 1)) / 4096;
105
+  uint32_t vref = 2048,
106
+           vOut = (vref * mcp4728_values[channel] * (_DAC_STEPPER_GAIN + 1)) / 4096;
110 107
   if (vOut > defaultVDD) vOut = defaultVDD;
111 108
   return vOut;
112 109
 }
113 110
 */
114 111
 
112
+/* Returns DAC values as a 0-100 percentage of drive strength */
113
+uint16_t mcp4728_getDrvPct(uint8_t channel) { return uint16_t(100.0 * mcp4728_values[channel] / (DAC_STEPPER_MAX) + 0.5); }
114
+
115
+/* Recieves all Drive strengths as 0-100 percent values, updates DAC Values array and calls fastwrite to update the DAC */
116
+void mcp4728_setDrvPct(int16_t pct[XYZE]) {
117
+  LOOP_XYZE(i) mcp4728_values[i] = 0.01 * pct[i] * (DAC_STEPPER_MAX);
118
+  mcp4728_fastWrite();
119
+}
120
+
115 121
 /**
116 122
  * FastWrite input register values - All DAC ouput update. refer to DATASHEET 5.6.1
117 123
  * DAC Input and PowerDown bits update.
@@ -119,7 +125,7 @@ uint16_t mcp4728_getVout(uint8_t channel) {
119 125
  */
120 126
 uint8_t mcp4728_fastWrite() {
121 127
   Wire.beginTransmission(DAC_DEV_ADDRESS);
122
-  for (uint8_t channel=0; channel <= 3; channel++) {
128
+  for (uint8_t channel = 0; channel < COUNT(channel); channel++) {
123 129
     Wire.write(highByte(mcp4728_values[channel]));
124 130
     Wire.write(lowByte(mcp4728_values[channel]));
125 131
   }

+ 3
- 1
Marlin/dac_mcp4728.h Datei anzeigen

@@ -32,7 +32,7 @@
32 32
 #if ENABLED(DAC_STEPPER_CURRENT)
33 33
 #include "Wire.h"
34 34
 
35
-#define defaultVDD     5000
35
+#define defaultVDD     DAC_STEPPER_MAX //was 5000 but differs with internal Vref
36 36
 #define BASE_ADDR      0x60
37 37
 #define RESET          0B00000110
38 38
 #define WAKE           0B00001001
@@ -59,6 +59,8 @@ uint8_t mcp4728_setGain_all(uint8_t value);
59 59
 uint16_t mcp4728_getValue(uint8_t channel);
60 60
 uint8_t mcp4728_fastWrite();
61 61
 uint8_t mcp4728_simpleCommand(byte simpleCommand);
62
+uint16_t mcp4728_getDrvPct(uint8_t channel);
63
+void mcp4728_setDrvPct(int16_t pct[XYZE]);
62 64
 
63 65
 #endif
64 66
 #endif

+ 9
- 0
Marlin/language_en.h Datei anzeigen

@@ -544,6 +544,15 @@
544 544
 #ifndef MSG_FILAMENT_CHANGE_OPTION_RESUME
545 545
   #define MSG_FILAMENT_CHANGE_OPTION_RESUME   "Resume print"
546 546
 #endif
547
+#ifndef MSG_DRIVE_STRENGTH
548
+  #define MSG_DRIVE_STRENGTH                  "Drive Strength"
549
+#endif
550
+#ifndef MSG_DAC_PERCENT
551
+  #define MSG_DAC_PERCENT                     "Driver %"
552
+#endif
553
+#ifndef MSG_DAC_EEPROM_WRITE
554
+  #define MSG_DAC_EEPROM_WRITE                "DAC EEPROM Write"
555
+#endif
547 556
 #if LCD_HEIGHT >= 4
548 557
   #ifndef MSG_FILAMENT_CHANGE_INIT_1
549 558
     #define MSG_FILAMENT_CHANGE_INIT_1          "Wait for start"

+ 19
- 13
Marlin/stepper_dac.cpp Datei anzeigen

@@ -49,6 +49,7 @@
49 49
 
50 50
   bool dac_present = false;
51 51
   const uint8_t dac_order[NUM_AXIS] = DAC_STEPPER_ORDER;
52
+  uint16_t dac_channel_pct[XYZE];
52 53
 
53 54
   int dac_init() {
54 55
     #if PIN_EXISTS(DAC_DISABLE)
@@ -72,7 +73,7 @@
72 73
 
73 74
     NOMORE(val, 100);
74 75
 
75
-    mcp4728_analogWrite(dac_order[channel], val * DAC_STEPPER_MAX / 100);
76
+    mcp4728_analogWrite(dac_order[channel], val * 0.01 * (DAC_STEPPER_MAX));
76 77
     mcp4728_simpleCommand(UPDATE);
77 78
   }
78 79
 
@@ -85,8 +86,14 @@
85 86
     mcp4728_simpleCommand(UPDATE);
86 87
   }
87 88
 
88
-  static float dac_perc(int8_t n) { return 100.0 * mcp4728_getValue(dac_order[n]) / DAC_STEPPER_MAX; }
89
-  static float dac_amps(int8_t n) { return ((2.048 * mcp4728_getValue(dac_order[n])) / 4096.0) / (8.0 * DAC_STEPPER_SENSE); }
89
+  static float dac_perc(int8_t n) { return 100.0 * mcp4728_getValue(dac_order[n]) * (1.0 / (DAC_STEPPER_MAX)); }
90
+  static float dac_amps(int8_t n) { return mcp4728_getDrvPct(dac_order[n]) * (DAC_STEPPER_MAX) * (0.125 * (DAC_STEPPER_SENSE)); }
91
+  
92
+  int16_t dac_current_get_percent(AxisEnum axis) { return mcp4728_getDrvPct(dac_order[axis]); }
93
+  void dac_current_set_percents(int16_t pct[XYZE]) {
94
+    LOOP_XYZE(i) dac_channel_pct[i] = pct[dac_order[i]];
95
+    mcp4728_setDrvPct(dac_channel_pct);
96
+  }
90 97
 
91 98
   void dac_print_values() {
92 99
     if (!dac_present) return;
@@ -94,16 +101,15 @@
94 101
     SERIAL_ECHO_START;
95 102
     SERIAL_ECHOLNPGM("Stepper current values in % (Amps):");
96 103
     SERIAL_ECHO_START;
97
-    SERIAL_ECHOPAIR(" X:",  dac_perc(0));
98
-    SERIAL_ECHOPAIR(" (",   dac_amps(0));
99
-    SERIAL_ECHOPAIR(") Y:", dac_perc(1));
100
-    SERIAL_ECHOPAIR(" (",   dac_amps(1));
101
-    SERIAL_ECHOPAIR(") Z:", dac_perc(2));
102
-    SERIAL_ECHOPAIR(" (",   dac_amps(2));
103
-    SERIAL_ECHOPAIR(") E:", dac_perc(3));
104
-    SERIAL_ECHOPAIR(" (",   dac_amps(3));
105
-    SERIAL_CHAR(')');
106
-    SERIAL_EOL;
104
+    SERIAL_ECHOPAIR(" X:",  dac_perc(X_AXIS)); 
105
+    SERIAL_ECHOPAIR(" (",   dac_amps(X_AXIS));
106
+    SERIAL_ECHOPAIR(") Y:", dac_perc(Y_AXIS));
107
+    SERIAL_ECHOPAIR(" (",   dac_amps(Y_AXIS));
108
+    SERIAL_ECHOPAIR(") Z:", dac_perc(Z_AXIS));
109
+    SERIAL_ECHOPAIR(" (",   dac_amps(Z_AXIS));
110
+    SERIAL_ECHOPAIR(") E:", dac_perc(E_AXIS));
111
+    SERIAL_ECHOPAIR(" (",   dac_amps(E_AXIS));
112
+    SERIAL_ECHOLN(")");
107 113
   }
108 114
 
109 115
   void dac_commit_eeprom() {

+ 2
- 0
Marlin/stepper_dac.h Datei anzeigen

@@ -51,5 +51,7 @@ void dac_current_percent(uint8_t channel, float val);
51 51
 void dac_current_raw(uint8_t channel, uint16_t val);
52 52
 void dac_print_values();
53 53
 void dac_commit_eeprom();
54
+int16_t dac_current_get_percent(AxisEnum axis);
55
+void dac_current_set_percents(int16_t pct[XYZE]);
54 56
 
55 57
 #endif // STEPPER_DAC_H

+ 41
- 0
Marlin/ultralcd.cpp Datei anzeigen

@@ -62,6 +62,11 @@ millis_t next_lcd_update_ms;
62 62
 
63 63
 uint8_t lcdDrawUpdate = LCDVIEW_CLEAR_CALL_REDRAW; // Set when the LCD needs to draw, decrements after every draw. Set to 2 in LCD routines so the LCD gets at least 1 full redraw (first redraw is partial)
64 64
 
65
+#if ENABLED(DAC_STEPPER_CURRENT)
66
+  #include "stepper_dac.h" //was dac_mcp4728.h MarlinMain uses stepper dac for the m-codes
67
+  uint16_t driverPercent[XYZE];
68
+#endif
69
+
65 70
 #if ENABLED(ULTIPANEL)
66 71
 
67 72
   // place-holders for Ki and Kd edits
@@ -114,6 +119,13 @@ uint8_t lcdDrawUpdate = LCDVIEW_CLEAR_CALL_REDRAW; // Set when the LCD needs to
114 119
   static void lcd_control_motion_menu();
115 120
   static void lcd_control_volumetric_menu();
116 121
 
122
+  #if ENABLED(DAC_STEPPER_CURRENT)
123
+    static void dac_driver_commit();
124
+    static void dac_driver_getValues();
125
+    static void lcd_dac_menu();
126
+    static void lcd_dac_write_eeprom();
127
+  #endif
128
+
117 129
   #if ENABLED(LCD_INFO_MENU)
118 130
     #if ENABLED(PRINTCOUNTER)
119 131
       static void lcd_info_stats_menu();
@@ -848,6 +860,31 @@ void kill_screen(const char* lcd_msg) {
848 860
 
849 861
   /**
850 862
    *
863
+   * "Driver current control" submenu items
864
+   *
865
+   */
866
+  #if ENABLED(DAC_STEPPER_CURRENT)
867
+    static void dac_driver_getValues() { LOOP_XYZE(i) driverPercent[i] = dac_current_get_percent((AxisEnum)i); }
868
+
869
+    static void dac_driver_commit() { dac_current_set_percents(driverPercent); }
870
+
871
+    static void dac_driver_eeprom_write() { dac_commit_eeprom(); }
872
+
873
+    static void lcd_dac_menu() {
874
+      dac_driver_getValues();
875
+      START_MENU();    
876
+      MENU_ITEM(back, MSG_CONTROL);
877
+      MENU_ITEM_EDIT_CALLBACK(int3, MSG_X " " MSG_DAC_PERCENT, &driverPercent[X_AXIS], 0, 100, dac_driver_commit);
878
+      MENU_ITEM_EDIT_CALLBACK(int3, MSG_Y " " MSG_DAC_PERCENT, &driverPercent[Y_AXIS], 0, 100, dac_driver_commit);
879
+      MENU_ITEM_EDIT_CALLBACK(int3, MSG_Z " " MSG_DAC_PERCENT, &driverPercent[Z_AXIS], 0, 100, dac_driver_commit);
880
+      MENU_ITEM_EDIT_CALLBACK(int3, MSG_E " " MSG_DAC_PERCENT, &driverPercent[E_AXIS], 0, 100, dac_driver_commit);
881
+      MENU_ITEM(function, MSG_DAC_EEPROM_WRITE, dac_driver_eeprom_write);
882
+      END_MENU();
883
+    }
884
+  #endif
885
+
886
+  /**
887
+   *
851 888
    * "Prepare" submenu items
852 889
    *
853 890
    */
@@ -1529,6 +1566,10 @@ void kill_screen(const char* lcd_msg) {
1529 1566
     #if ENABLED(FWRETRACT)
1530 1567
       MENU_ITEM(submenu, MSG_RETRACT, lcd_control_retract_menu);
1531 1568
     #endif
1569
+    #if ENABLED(DAC_STEPPER_CURRENT)
1570
+      MENU_ITEM(submenu, MSG_DRIVE_STRENGTH, lcd_dac_menu); 
1571
+    #endif
1572
+
1532 1573
     #if ENABLED(EEPROM_SETTINGS)
1533 1574
       MENU_ITEM(function, MSG_STORE_EPROM, Config_StoreSettings);
1534 1575
       MENU_ITEM(function, MSG_LOAD_EPROM, Config_RetrieveSettings);

Laden…
Abbrechen
Speichern