Browse Source

Merge pull request #1619 from epatel/manual-bed-leveling+mesh-bed-level

Manual bed leveling + mesh bed leveling
Scott Lahteine 9 years ago
parent
commit
9b639b4135

+ 17
- 0
Marlin/Configuration.h View File

375
 //#define ENDSTOPPULLUP_FIL_RUNOUT // Uncomment to use internal pullup for filament runout pins if the sensor is defined.
375
 //#define ENDSTOPPULLUP_FIL_RUNOUT // Uncomment to use internal pullup for filament runout pins if the sensor is defined.
376
 
376
 
377
 //===========================================================================
377
 //===========================================================================
378
+//============================ Manual Bed Leveling ==========================
379
+//===========================================================================
380
+
381
+// #define MANUAL_BED_LEVELING  // Add display menu option for bed leveling
382
+// #define MESH_BED_LEVELING    // Enable mesh bed leveling
383
+
384
+#if defined(MESH_BED_LEVELING)
385
+  #define MESH_MIN_X 10
386
+  #define MESH_MAX_X (X_MAX_POS - MESH_MIN_X)
387
+  #define MESH_MIN_Y 10
388
+  #define MESH_MAX_Y (Y_MAX_POS - MESH_MIN_Y)
389
+  #define MESH_NUM_X_POINTS 3  // Don't use more than 7 points per axis, implementation limited
390
+  #define MESH_NUM_Y_POINTS 3
391
+  #define MESH_HOME_SEARCH_Z 4  // Z after Home, bed somewhere below but above 0.0
392
+#endif  // MESH_BED_LEVELING
393
+
394
+//===========================================================================
378
 //============================= Bed Auto Leveling ===========================
395
 //============================= Bed Auto Leveling ===========================
379
 //===========================================================================
396
 //===========================================================================
380
 
397
 

+ 63
- 2
Marlin/ConfigurationStore.cpp View File

20
  *  max_e_jerk
20
  *  max_e_jerk
21
  *  add_homing (x3)
21
  *  add_homing (x3)
22
  *
22
  *
23
+ * Mesh bed leveling:
24
+ *  active
25
+ *  mesh_num_x
26
+ *  mesh_num_y
27
+ *  z_values[][]
28
+ *
23
  * DELTA:
29
  * DELTA:
24
  *  endstop_adj (x3)
30
  *  endstop_adj (x3)
25
  *  delta_radius
31
  *  delta_radius
69
 #include "ultralcd.h"
75
 #include "ultralcd.h"
70
 #include "ConfigurationStore.h"
76
 #include "ConfigurationStore.h"
71
 
77
 
78
+#if defined(MESH_BED_LEVELING)
79
+   #include "mesh_bed_leveling.h"
80
+#endif  // MESH_BED_LEVELING
81
+
72
 void _EEPROM_writeData(int &pos, uint8_t* value, uint8_t size) {
82
 void _EEPROM_writeData(int &pos, uint8_t* value, uint8_t size) {
73
   uint8_t c;
83
   uint8_t c;
74
   while(size--) {
84
   while(size--) {
105
 // wrong data being written to the variables.
115
 // wrong data being written to the variables.
106
 // ALSO:  always make sure the variables in the Store and retrieve sections are in the same order.
116
 // ALSO:  always make sure the variables in the Store and retrieve sections are in the same order.
107
 
117
 
108
-#define EEPROM_VERSION "V16"
118
+#define EEPROM_VERSION "V17"
109
 
119
 
110
 #ifdef EEPROM_SETTINGS
120
 #ifdef EEPROM_SETTINGS
111
 
121
 
128
   EEPROM_WRITE_VAR(i, max_e_jerk);
138
   EEPROM_WRITE_VAR(i, max_e_jerk);
129
   EEPROM_WRITE_VAR(i, add_homing);
139
   EEPROM_WRITE_VAR(i, add_homing);
130
 
140
 
141
+  uint8_t mesh_num_x = 3;
142
+  uint8_t mesh_num_y = 3;
143
+  #if defined(MESH_BED_LEVELING)
144
+    // Compile time test that sizeof(mbl.z_values) is as expected
145
+    typedef char c_assert[(sizeof(mbl.z_values) == MESH_NUM_X_POINTS*MESH_NUM_Y_POINTS*sizeof(dummy)) ? 1 : -1];
146
+    mesh_num_x = MESH_NUM_X_POINTS;
147
+    mesh_num_y = MESH_NUM_Y_POINTS;
148
+    EEPROM_WRITE_VAR(i, mbl.active);
149
+    EEPROM_WRITE_VAR(i, mesh_num_x);
150
+    EEPROM_WRITE_VAR(i, mesh_num_y);
151
+    EEPROM_WRITE_VAR(i, mbl.z_values);
152
+  #else
153
+    uint8_t dummy_uint8 = 0;
154
+    EEPROM_WRITE_VAR(i, dummy_uint8);
155
+    EEPROM_WRITE_VAR(i, mesh_num_x);
156
+    EEPROM_WRITE_VAR(i, mesh_num_y);
157
+    dummy = 0.0f;
158
+    for (int q=0; q<mesh_num_x*mesh_num_y; q++) {
159
+      EEPROM_WRITE_VAR(i, dummy);
160
+    }
161
+  #endif  // MESH_BED_LEVELING
162
+
131
   #ifdef DELTA
163
   #ifdef DELTA
132
     EEPROM_WRITE_VAR(i, endstop_adj);               // 3 floats
164
     EEPROM_WRITE_VAR(i, endstop_adj);               // 3 floats
133
     EEPROM_WRITE_VAR(i, delta_radius);              // 1 float
165
     EEPROM_WRITE_VAR(i, delta_radius);              // 1 float
250
     EEPROM_READ_VAR(i, max_feedrate);
282
     EEPROM_READ_VAR(i, max_feedrate);
251
     EEPROM_READ_VAR(i, max_acceleration_units_per_sq_second);
283
     EEPROM_READ_VAR(i, max_acceleration_units_per_sq_second);
252
 
284
 
253
-        // steps per sq second need to be updated to agree with the units per sq second (as they are what is used in the planner)
285
+    // steps per sq second need to be updated to agree with the units per sq second (as they are what is used in the planner)
254
     reset_acceleration_rates();
286
     reset_acceleration_rates();
255
 
287
 
256
     EEPROM_READ_VAR(i, acceleration);
288
     EEPROM_READ_VAR(i, acceleration);
264
     EEPROM_READ_VAR(i, max_e_jerk);
296
     EEPROM_READ_VAR(i, max_e_jerk);
265
     EEPROM_READ_VAR(i, add_homing);
297
     EEPROM_READ_VAR(i, add_homing);
266
 
298
 
299
+    uint8_t mesh_num_x = 0;
300
+    uint8_t mesh_num_y = 0;
301
+    #if defined(MESH_BED_LEVELING)
302
+      EEPROM_READ_VAR(i, mbl.active);
303
+      EEPROM_READ_VAR(i, mesh_num_x);
304
+      EEPROM_READ_VAR(i, mesh_num_y);
305
+      if (mesh_num_x != MESH_NUM_X_POINTS ||
306
+          mesh_num_y != MESH_NUM_Y_POINTS) {
307
+        mbl.reset();
308
+        for (int q=0; q<mesh_num_x*mesh_num_y; q++) {
309
+          EEPROM_READ_VAR(i, dummy);
310
+        }
311
+      } else {
312
+        EEPROM_READ_VAR(i, mbl.z_values);
313
+      }
314
+    #else
315
+      uint8_t dummy_uint8 = 0;
316
+      EEPROM_READ_VAR(i, dummy_uint8);
317
+      EEPROM_READ_VAR(i, mesh_num_x);
318
+      EEPROM_READ_VAR(i, mesh_num_y);
319
+      for (int q=0; q<mesh_num_x*mesh_num_y; q++) {
320
+        EEPROM_READ_VAR(i, dummy);
321
+      }
322
+    #endif  // MESH_BED_LEVELING
323
+
267
     #ifdef DELTA
324
     #ifdef DELTA
268
       EEPROM_READ_VAR(i, endstop_adj);                // 3 floats
325
       EEPROM_READ_VAR(i, endstop_adj);                // 3 floats
269
       EEPROM_READ_VAR(i, delta_radius);               // 1 float
326
       EEPROM_READ_VAR(i, delta_radius);               // 1 float
392
   max_e_jerk = DEFAULT_EJERK;
449
   max_e_jerk = DEFAULT_EJERK;
393
   add_homing[X_AXIS] = add_homing[Y_AXIS] = add_homing[Z_AXIS] = 0;
450
   add_homing[X_AXIS] = add_homing[Y_AXIS] = add_homing[Z_AXIS] = 0;
394
 
451
 
452
+  #if defined(MESH_BED_LEVELING)
453
+    mbl.active = 0;
454
+  #endif  // MESH_BED_LEVELING
455
+
395
   #ifdef DELTA
456
   #ifdef DELTA
396
     endstop_adj[X_AXIS] = endstop_adj[Y_AXIS] = endstop_adj[Z_AXIS] = 0;
457
     endstop_adj[X_AXIS] = endstop_adj[Y_AXIS] = endstop_adj[Z_AXIS] = 0;
397
     delta_radius =  DELTA_RADIUS;
458
     delta_radius =  DELTA_RADIUS;

+ 197
- 3
Marlin/Marlin_main.cpp View File

41
 
41
 
42
 #define SERVO_LEVELING defined(ENABLE_AUTO_BED_LEVELING) && PROBE_SERVO_DEACTIVATION_DELAY > 0
42
 #define SERVO_LEVELING defined(ENABLE_AUTO_BED_LEVELING) && PROBE_SERVO_DEACTIVATION_DELAY > 0
43
 
43
 
44
+#if defined(MESH_BED_LEVELING)
45
+  #include "mesh_bed_leveling.h"
46
+#endif  // MESH_BED_LEVELING
47
+
44
 #include "ultralcd.h"
48
 #include "ultralcd.h"
45
 #include "planner.h"
49
 #include "planner.h"
46
 #include "stepper.h"
50
 #include "stepper.h"
1737
     #endif
1741
     #endif
1738
   #endif
1742
   #endif
1739
 
1743
 
1744
+  #if defined(MESH_BED_LEVELING)
1745
+    uint8_t mbl_was_active = mbl.active;
1746
+    mbl.active = 0;
1747
+  #endif  // MESH_BED_LEVELING
1748
+
1740
   saved_feedrate = feedrate;
1749
   saved_feedrate = feedrate;
1741
   saved_feedmultiply = feedmultiply;
1750
   saved_feedmultiply = feedmultiply;
1742
   feedmultiply = 100;
1751
   feedmultiply = 100;
1951
     enable_endstops(false);
1960
     enable_endstops(false);
1952
   #endif
1961
   #endif
1953
 
1962
 
1963
+  #if defined(MESH_BED_LEVELING)
1964
+    if (mbl_was_active) {
1965
+      current_position[X_AXIS] = mbl.get_x(0);
1966
+      current_position[Y_AXIS] = mbl.get_y(0);
1967
+      destination[X_AXIS] = current_position[X_AXIS];
1968
+      destination[Y_AXIS] = current_position[Y_AXIS];
1969
+      destination[Z_AXIS] = current_position[Z_AXIS];
1970
+      destination[E_AXIS] = current_position[E_AXIS];
1971
+      feedrate = homing_feedrate[X_AXIS];
1972
+      plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate, active_extruder);
1973
+      st_synchronize();
1974
+      current_position[Z_AXIS] = MESH_HOME_SEARCH_Z;
1975
+      plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
1976
+      mbl.active = 1;
1977
+    }
1978
+  #endif
1979
+
1954
   feedrate = saved_feedrate;
1980
   feedrate = saved_feedrate;
1955
   feedmultiply = saved_feedmultiply;
1981
   feedmultiply = saved_feedmultiply;
1956
   previous_millis_cmd = millis();
1982
   previous_millis_cmd = millis();
1957
   endstops_hit_on_purpose();
1983
   endstops_hit_on_purpose();
1958
 }
1984
 }
1959
 
1985
 
1986
+#if defined(MESH_BED_LEVELING)
1987
+
1988
+  inline void gcode_G29() {
1989
+    static int probe_point = -1;
1990
+    int state = 0;
1991
+    if (code_seen('S') || code_seen('s')) {
1992
+      state = code_value_long();
1993
+      if (state < 0 || state > 2) {
1994
+        SERIAL_PROTOCOLPGM("S out of range (0-2).\n");
1995
+        return;
1996
+      }
1997
+    }
1998
+
1999
+    if (state == 0) { // Dump mesh_bed_leveling
2000
+      if (mbl.active) {
2001
+        SERIAL_PROTOCOLPGM("Num X,Y: ");
2002
+        SERIAL_PROTOCOL(MESH_NUM_X_POINTS);
2003
+        SERIAL_PROTOCOLPGM(",");
2004
+        SERIAL_PROTOCOL(MESH_NUM_Y_POINTS);
2005
+        SERIAL_PROTOCOLPGM("\nZ search height: ");
2006
+        SERIAL_PROTOCOL(MESH_HOME_SEARCH_Z);
2007
+        SERIAL_PROTOCOLPGM("\nMeasured points:\n");              
2008
+        for (int y=0; y<MESH_NUM_Y_POINTS; y++) {
2009
+          for (int x=0; x<MESH_NUM_X_POINTS; x++) {
2010
+            SERIAL_PROTOCOLPGM("  ");              
2011
+            SERIAL_PROTOCOL_F(mbl.z_values[y][x], 5);
2012
+          }
2013
+          SERIAL_EOL;
2014
+        }
2015
+      } else {
2016
+        SERIAL_PROTOCOLPGM("Mesh bed leveling not active.\n");
2017
+      }
2018
+
2019
+    } else if (state == 1) { // Begin probing mesh points
2020
+
2021
+      mbl.reset();
2022
+      probe_point = 0;
2023
+      enquecommands_P(PSTR("G28"));
2024
+      enquecommands_P(PSTR("G29 S2"));
2025
+
2026
+    } else if (state == 2) { // Goto next point
2027
+
2028
+      if (probe_point < 0) {
2029
+        SERIAL_PROTOCOLPGM("Mesh probing not started.\n");
2030
+        return;
2031
+      }
2032
+      int ix, iy;
2033
+      if (probe_point == 0) {
2034
+        current_position[Z_AXIS] = MESH_HOME_SEARCH_Z;
2035
+        plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
2036
+      } else {
2037
+        ix = (probe_point-1) % MESH_NUM_X_POINTS;
2038
+        iy = (probe_point-1) / MESH_NUM_X_POINTS;
2039
+        if (iy&1) { // Zig zag
2040
+          ix = (MESH_NUM_X_POINTS - 1) - ix;
2041
+        }
2042
+        mbl.set_z(ix, iy, current_position[Z_AXIS]);
2043
+        current_position[Z_AXIS] = MESH_HOME_SEARCH_Z;
2044
+        plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], homing_feedrate[X_AXIS]/60, active_extruder);
2045
+        st_synchronize();
2046
+      }
2047
+      if (probe_point == MESH_NUM_X_POINTS*MESH_NUM_Y_POINTS) {
2048
+        SERIAL_PROTOCOLPGM("Mesh done.\n");
2049
+        probe_point = -1;
2050
+        mbl.active = 1;
2051
+        enquecommands_P(PSTR("G28"));
2052
+        return;
2053
+      }
2054
+      ix = probe_point % MESH_NUM_X_POINTS;
2055
+      iy = probe_point / MESH_NUM_X_POINTS;
2056
+      if (iy&1) { // Zig zag
2057
+        ix = (MESH_NUM_X_POINTS - 1) - ix;
2058
+      }
2059
+      current_position[X_AXIS] = mbl.get_x(ix);
2060
+      current_position[Y_AXIS] = mbl.get_y(iy);
2061
+      plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], homing_feedrate[X_AXIS]/60, active_extruder);
2062
+      st_synchronize();
2063
+      probe_point++;
2064
+    }
2065
+  }
2066
+
2067
+#endif
2068
+
1960
 #ifdef ENABLE_AUTO_BED_LEVELING
2069
 #ifdef ENABLE_AUTO_BED_LEVELING
1961
 
2070
 
1962
   // Define the possible boundaries for probing based on set limits
2071
   // Define the possible boundaries for probing based on set limits
4661
       gcode_G28();
4770
       gcode_G28();
4662
       break;
4771
       break;
4663
 
4772
 
4773
+    #if defined(MESH_BED_LEVELING)
4774
+      case 29: // G29 Handle mesh based leveling
4775
+        gcode_G29();
4776
+        break;
4777
+    #endif
4778
+
4664
     #ifdef ENABLE_AUTO_BED_LEVELING
4779
     #ifdef ENABLE_AUTO_BED_LEVELING
4665
 
4780
 
4666
       case 29: // G29 Detailed Z-Probe, probes the bed at 3 or more points.
4781
       case 29: // G29 Detailed Z-Probe, probes the bed at 3 or more points.
5280
 }
5395
 }
5281
 #endif //DELTA
5396
 #endif //DELTA
5282
 
5397
 
5398
+#if defined(MESH_BED_LEVELING)
5399
+#if !defined(MIN)
5400
+#define MIN(_v1, _v2) (((_v1) < (_v2)) ? (_v1) : (_v2))
5401
+#endif  // ! MIN
5402
+// This function is used to split lines on mesh borders so each segment is only part of one mesh area
5403
+void mesh_plan_buffer_line(float x, float y, float z, const float e, float feed_rate, const uint8_t &extruder, uint8_t x_splits=0xff, uint8_t y_splits=0xff)
5404
+{
5405
+  if (!mbl.active) {
5406
+    plan_buffer_line(x, y, z, e, feed_rate, extruder);
5407
+    for(int8_t i=0; i < NUM_AXIS; i++) {
5408
+      current_position[i] = destination[i];
5409
+    }
5410
+    return;
5411
+  }
5412
+  int pix = mbl.select_x_index(current_position[X_AXIS]);
5413
+  int piy = mbl.select_y_index(current_position[Y_AXIS]);
5414
+  int ix = mbl.select_x_index(x);
5415
+  int iy = mbl.select_y_index(y);
5416
+  pix = MIN(pix, MESH_NUM_X_POINTS-2);
5417
+  piy = MIN(piy, MESH_NUM_Y_POINTS-2);
5418
+  ix = MIN(ix, MESH_NUM_X_POINTS-2);
5419
+  iy = MIN(iy, MESH_NUM_Y_POINTS-2);
5420
+  if (pix == ix && piy == iy) {
5421
+    // Start and end on same mesh square
5422
+    plan_buffer_line(x, y, z, e, feed_rate, extruder);
5423
+    for(int8_t i=0; i < NUM_AXIS; i++) {
5424
+      current_position[i] = destination[i];
5425
+    }
5426
+    return;
5427
+  }
5428
+  float nx, ny, ne, normalized_dist;
5429
+  if (ix > pix && (x_splits) & BIT(ix)) {
5430
+    nx = mbl.get_x(ix);
5431
+    normalized_dist = (nx - current_position[X_AXIS])/(x - current_position[X_AXIS]);
5432
+    ny = current_position[Y_AXIS] + (y - current_position[Y_AXIS]) * normalized_dist;
5433
+    ne = current_position[E_AXIS] + (e - current_position[E_AXIS]) * normalized_dist;
5434
+    x_splits ^= BIT(ix);
5435
+  } else if (ix < pix && (x_splits) & BIT(pix)) {
5436
+    nx = mbl.get_x(pix);
5437
+    normalized_dist = (nx - current_position[X_AXIS])/(x - current_position[X_AXIS]);
5438
+    ny = current_position[Y_AXIS] + (y - current_position[Y_AXIS]) * normalized_dist;
5439
+    ne = current_position[E_AXIS] + (e - current_position[E_AXIS]) * normalized_dist;
5440
+    x_splits ^= BIT(pix);
5441
+  } else if (iy > piy && (y_splits) & BIT(iy)) {
5442
+    ny = mbl.get_y(iy);
5443
+    normalized_dist = (ny - current_position[Y_AXIS])/(y - current_position[Y_AXIS]);
5444
+    nx = current_position[X_AXIS] + (x - current_position[X_AXIS]) * normalized_dist;
5445
+    ne = current_position[E_AXIS] + (e - current_position[E_AXIS]) * normalized_dist;
5446
+    y_splits ^= BIT(iy);
5447
+  } else if (iy < piy && (y_splits) & BIT(piy)) {
5448
+    ny = mbl.get_y(piy);
5449
+    normalized_dist = (ny - current_position[Y_AXIS])/(y - current_position[Y_AXIS]);
5450
+    nx = current_position[X_AXIS] + (x - current_position[X_AXIS]) * normalized_dist;
5451
+    ne = current_position[E_AXIS] + (e - current_position[E_AXIS]) * normalized_dist;
5452
+    y_splits ^= BIT(piy);
5453
+  } else {
5454
+    // Already split on a border
5455
+    plan_buffer_line(x, y, z, e, feed_rate, extruder);
5456
+    for(int8_t i=0; i < NUM_AXIS; i++) {
5457
+      current_position[i] = destination[i];
5458
+    }
5459
+    return;
5460
+  }
5461
+  // Do the split and look for more borders
5462
+  destination[X_AXIS] = nx;
5463
+  destination[Y_AXIS] = ny;
5464
+  destination[E_AXIS] = ne;
5465
+  mesh_plan_buffer_line(nx, ny, z, ne, feed_rate, extruder, x_splits, y_splits);
5466
+  destination[X_AXIS] = x;
5467
+  destination[Y_AXIS] = y;
5468
+  destination[E_AXIS] = e;
5469
+  mesh_plan_buffer_line(x, y, z, e, feed_rate, extruder, x_splits, y_splits);
5470
+}
5471
+#endif  // MESH_BED_LEVELING
5472
+
5283
 void prepare_move()
5473
 void prepare_move()
5284
 {
5474
 {
5285
   clamp_to_software_endstops(destination);
5475
   clamp_to_software_endstops(destination);
5395
 #if ! (defined DELTA || defined SCARA)
5585
 #if ! (defined DELTA || defined SCARA)
5396
   // Do not use feedmultiply for E or Z only moves
5586
   // Do not use feedmultiply for E or Z only moves
5397
   if( (current_position[X_AXIS] == destination [X_AXIS]) && (current_position[Y_AXIS] == destination [Y_AXIS])) {
5587
   if( (current_position[X_AXIS] == destination [X_AXIS]) && (current_position[Y_AXIS] == destination [Y_AXIS])) {
5398
-      plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
5399
-  }
5400
-  else {
5588
+    plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
5589
+  } else {
5590
+#if defined(MESH_BED_LEVELING)
5591
+    mesh_plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate*feedmultiply/60/100.0, active_extruder);
5592
+    return;
5593
+#else
5401
     plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate*feedmultiply/60/100.0, active_extruder);
5594
     plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate*feedmultiply/60/100.0, active_extruder);
5595
+#endif  // MESH_BED_LEVELING
5402
   }
5596
   }
5403
 #endif // !(DELTA || SCARA)
5597
 #endif // !(DELTA || SCARA)
5404
 
5598
 

+ 3
- 0
Marlin/language_en.h View File

95
 #ifndef MSG_MOVE_AXIS
95
 #ifndef MSG_MOVE_AXIS
96
 #define MSG_MOVE_AXIS                       "Move axis"
96
 #define MSG_MOVE_AXIS                       "Move axis"
97
 #endif
97
 #endif
98
+#ifndef MSG_LEVEL_BED
99
+#define MSG_LEVEL_BED                       "Level bed"
100
+#endif
98
 #ifndef MSG_MOVE_X
101
 #ifndef MSG_MOVE_X
99
 #define MSG_MOVE_X                          "Move X"
102
 #define MSG_MOVE_X                          "Move X"
100
 #endif
103
 #endif

+ 20
- 0
Marlin/mesh_bed_leveling.cpp View File

1
+#include "mesh_bed_leveling.h"
2
+
3
+#if defined(MESH_BED_LEVELING)
4
+
5
+mesh_bed_leveling mbl;
6
+
7
+mesh_bed_leveling::mesh_bed_leveling() {
8
+    reset();
9
+}
10
+    
11
+void mesh_bed_leveling::reset() {
12
+    for (int y=0; y<MESH_NUM_Y_POINTS; y++) {
13
+        for (int x=0; x<MESH_NUM_X_POINTS; x++) {
14
+            z_values[y][x] = 0;
15
+        }
16
+    }
17
+    active = 0;
18
+}
19
+
20
+#endif  // MESH_BED_LEVELING

+ 61
- 0
Marlin/mesh_bed_leveling.h View File

1
+#include "Marlin.h"
2
+
3
+#if defined(MESH_BED_LEVELING)
4
+
5
+#define MESH_X_DIST ((MESH_MAX_X - MESH_MIN_X)/(MESH_NUM_X_POINTS - 1))
6
+#define MESH_Y_DIST ((MESH_MAX_Y - MESH_MIN_Y)/(MESH_NUM_Y_POINTS - 1))
7
+
8
+class mesh_bed_leveling {
9
+public:
10
+    uint8_t active;
11
+    float z_values[MESH_NUM_Y_POINTS][MESH_NUM_X_POINTS];
12
+    
13
+    mesh_bed_leveling();
14
+    
15
+    void reset();
16
+    
17
+    float get_x(int i) { return MESH_MIN_X + MESH_X_DIST*i; }
18
+    float get_y(int i) { return MESH_MIN_Y + MESH_Y_DIST*i; }
19
+    void set_z(int ix, int iy, float z) { z_values[iy][ix] = z; }
20
+    
21
+    int select_x_index(float x) {
22
+        int i = 1;
23
+        while (x > get_x(i) && i < MESH_NUM_X_POINTS-1) {
24
+            i++;
25
+        }
26
+        return i-1;
27
+    }
28
+    
29
+    int select_y_index(float y) {
30
+        int i = 1;
31
+        while (y > get_y(i) && i < MESH_NUM_Y_POINTS-1) {
32
+            i++;
33
+        }
34
+        return i-1;
35
+    }
36
+    
37
+    float calc_z0(float a0, float a1, float z1, float a2, float z2) {
38
+        float delta_z = (z2 - z1)/(a2 - a1);
39
+        float delta_a = a0 - a1;
40
+        return z1 + delta_a * delta_z;
41
+    }
42
+    
43
+    float get_z(float x0, float y0) {
44
+        int x_index = select_x_index(x0);
45
+        int y_index = select_y_index(y0);
46
+        float z1 = calc_z0(x0,
47
+                           get_x(x_index), z_values[y_index][x_index],
48
+                           get_x(x_index+1), z_values[y_index][x_index+1]);
49
+        float z2 = calc_z0(x0,
50
+                           get_x(x_index), z_values[y_index+1][x_index],
51
+                           get_x(x_index+1), z_values[y_index+1][x_index+1]);
52
+        float z0 = calc_z0(y0,
53
+                           get_y(y_index), z1,
54
+                           get_y(y_index+1), z2);
55
+        return z0;
56
+    }
57
+};
58
+
59
+extern mesh_bed_leveling mbl;
60
+
61
+#endif  // MESH_BED_LEVELING

+ 20
- 5
Marlin/planner.cpp View File

58
 #include "ultralcd.h"
58
 #include "ultralcd.h"
59
 #include "language.h"
59
 #include "language.h"
60
 
60
 
61
+#if defined(MESH_BED_LEVELING)
62
+  #include "mesh_bed_leveling.h"
63
+#endif  // MESH_BED_LEVELING
64
+
61
 //===========================================================================
65
 //===========================================================================
62
 //============================= public variables ============================
66
 //============================= public variables ============================
63
 //===========================================================================
67
 //===========================================================================
530
 // Add a new linear movement to the buffer. steps_x, _y and _z is the absolute position in 
534
 // Add a new linear movement to the buffer. steps_x, _y and _z is the absolute position in 
531
 // mm. Microseconds specify how many microseconds the move should take to perform. To aid acceleration
535
 // mm. Microseconds specify how many microseconds the move should take to perform. To aid acceleration
532
 // calculation the caller must also provide the physical length of the line in millimeters.
536
 // calculation the caller must also provide the physical length of the line in millimeters.
533
-#ifdef ENABLE_AUTO_BED_LEVELING
537
+#if defined(ENABLE_AUTO_BED_LEVELING) || defined(MESH_BED_LEVELING)
534
 void plan_buffer_line(float x, float y, float z, const float &e, float feed_rate, const uint8_t &extruder)
538
 void plan_buffer_line(float x, float y, float z, const float &e, float feed_rate, const uint8_t &extruder)
535
 #else
539
 #else
536
 void plan_buffer_line(const float &x, const float &y, const float &z, const float &e, float feed_rate, const uint8_t &extruder)
540
 void plan_buffer_line(const float &x, const float &y, const float &z, const float &e, float feed_rate, const uint8_t &extruder)
548
     lcd_update();
552
     lcd_update();
549
   }
553
   }
550
 
554
 
555
+#if defined(MESH_BED_LEVELING)
556
+  if (mbl.active) {
557
+    z += mbl.get_z(x, y);
558
+  }
559
+#endif  // MESH_BED_LEVELING
560
+
551
 #ifdef ENABLE_AUTO_BED_LEVELING
561
 #ifdef ENABLE_AUTO_BED_LEVELING
552
   apply_rotation_xyz(plan_bed_level_matrix, x, y, z);
562
   apply_rotation_xyz(plan_bed_level_matrix, x, y, z);
553
 #endif // ENABLE_AUTO_BED_LEVELING
563
 #endif // ENABLE_AUTO_BED_LEVELING
1078
 }
1088
 }
1079
 #endif // ENABLE_AUTO_BED_LEVELING
1089
 #endif // ENABLE_AUTO_BED_LEVELING
1080
 
1090
 
1081
-#ifdef ENABLE_AUTO_BED_LEVELING
1091
+#if defined(ENABLE_AUTO_BED_LEVELING) || defined(MESH_BED_LEVELING)
1082
 void plan_set_position(float x, float y, float z, const float &e)
1092
 void plan_set_position(float x, float y, float z, const float &e)
1083
-{
1084
-  apply_rotation_xyz(plan_bed_level_matrix, x, y, z);
1085
 #else
1093
 #else
1086
 void plan_set_position(const float &x, const float &y, const float &z, const float &e)
1094
 void plan_set_position(const float &x, const float &y, const float &z, const float &e)
1095
+#endif  // ENABLE_AUTO_BED_LEVELING || MESH_BED_LEVELING
1087
 {
1096
 {
1088
-#endif // ENABLE_AUTO_BED_LEVELING
1097
+#if defined(ENABLE_AUTO_BED_LEVELING)
1098
+  apply_rotation_xyz(plan_bed_level_matrix, x, y, z);
1099
+#elif defined(MESH_BED_LEVELING)
1100
+  if (mbl.active) {
1101
+    z += mbl.get_z(x, y);
1102
+  }
1103
+#endif  // ENABLE_AUTO_BED_LEVELING
1089
 
1104
 
1090
   position[X_AXIS] = lround(x*axis_steps_per_unit[X_AXIS]);
1105
   position[X_AXIS] = lround(x*axis_steps_per_unit[X_AXIS]);
1091
   position[Y_AXIS] = lround(y*axis_steps_per_unit[Y_AXIS]);
1106
   position[Y_AXIS] = lround(y*axis_steps_per_unit[Y_AXIS]);

+ 6
- 5
Marlin/planner.h View File

82
 // Add a new linear movement to the buffer. x, y and z is the signed, absolute target position in 
82
 // Add a new linear movement to the buffer. x, y and z is the signed, absolute target position in 
83
 // millimaters. Feed rate specifies the speed of the motion.
83
 // millimaters. Feed rate specifies the speed of the motion.
84
 
84
 
85
-#ifdef ENABLE_AUTO_BED_LEVELING
85
+#if defined(ENABLE_AUTO_BED_LEVELING) || defined(MESH_BED_LEVELING)
86
 void plan_buffer_line(float x, float y, float z, const float &e, float feed_rate, const uint8_t &extruder);
86
 void plan_buffer_line(float x, float y, float z, const float &e, float feed_rate, const uint8_t &extruder);
87
-
87
+#if defined(ENABLE_AUTO_BED_LEVELING)
88
   #ifndef DELTA
88
   #ifndef DELTA
89
   // Get the position applying the bed level matrix if enabled
89
   // Get the position applying the bed level matrix if enabled
90
   vector_3 plan_get_position();
90
   vector_3 plan_get_position();
91
   #endif
91
   #endif
92
+#endif  // ENABLE_AUTO_BED_LEVELING
92
 #else
93
 #else
93
 void plan_buffer_line(const float &x, const float &y, const float &z, const float &e, float feed_rate, const uint8_t &extruder);
94
 void plan_buffer_line(const float &x, const float &y, const float &z, const float &e, float feed_rate, const uint8_t &extruder);
94
-#endif // ENABLE_AUTO_BED_LEVELING
95
+#endif  // ENABLE_AUTO_BED_LEVELING || MESH_BED_LEVELING
95
 
96
 
96
 // Set position. Used for G92 instructions.
97
 // Set position. Used for G92 instructions.
97
-#ifdef ENABLE_AUTO_BED_LEVELING
98
+#if defined(ENABLE_AUTO_BED_LEVELING) || defined(MESH_BED_LEVELING)
98
 void plan_set_position(float x, float y, float z, const float &e);
99
 void plan_set_position(float x, float y, float z, const float &e);
99
 #else
100
 #else
100
 void plan_set_position(const float &x, const float &y, const float &z, const float &e);
101
 void plan_set_position(const float &x, const float &y, const float &z, const float &e);
101
-#endif // ENABLE_AUTO_BED_LEVELING
102
+#endif // ENABLE_AUTO_BED_LEVELING || MESH_BED_LEVELING
102
 
103
 
103
 void plan_set_e_position(const float &e);
104
 void plan_set_e_position(const float &e);
104
 
105
 

+ 88
- 1
Marlin/ultralcd.cpp View File

70
 static void lcd_delta_calibrate_menu();
70
 static void lcd_delta_calibrate_menu();
71
 #endif // DELTA_CALIBRATION_MENU
71
 #endif // DELTA_CALIBRATION_MENU
72
 
72
 
73
+#if defined(MANUAL_BED_LEVELING)
74
+#include "mesh_bed_leveling.h"
75
+static void _lcd_level_bed();
76
+static void _lcd_level_bed_homing();
77
+static void lcd_level_bed();
78
+#endif  // MANUAL_BED_LEVELING
79
+
73
 static void lcd_quick_feedback();//Cause an LCD refresh, and give the user visual or audible feedback that something has happened
80
 static void lcd_quick_feedback();//Cause an LCD refresh, and give the user visual or audible feedback that something has happened
74
 
81
 
75
 /* Different types of actions that can be used in menu items. */
82
 /* Different types of actions that can be used in menu items. */
630
     }
637
     }
631
   #endif
638
   #endif
632
   MENU_ITEM(submenu, MSG_MOVE_AXIS, lcd_move_menu);
639
   MENU_ITEM(submenu, MSG_MOVE_AXIS, lcd_move_menu);
640
+
641
+  #if defined(MANUAL_BED_LEVELING)
642
+    MENU_ITEM(submenu, MSG_LEVEL_BED, lcd_level_bed);
643
+  #endif
633
 	
644
 	
634
   END_MENU();
645
   END_MENU();
635
 }
646
 }
1341
     #endif
1352
     #endif
1342
 
1353
 
1343
     #ifdef ULTIPANEL
1354
     #ifdef ULTIPANEL
1344
-      if (currentMenu != lcd_status_screen && millis() > timeoutToStatus) {
1355
+      if (currentMenu != lcd_status_screen &&
1356
+        #if defined(MANUAL_BED_LEVELING)
1357
+          currentMenu != _lcd_level_bed && 
1358
+          currentMenu != _lcd_level_bed_homing && 
1359
+        #endif  // MANUAL_BED_LEVELING
1360
+          millis() > timeoutToStatus) {
1345
         lcd_return_to_status();
1361
         lcd_return_to_status();
1346
         lcdDrawUpdate = 2;
1362
         lcdDrawUpdate = 2;
1347
       }
1363
       }
1760
   return conv;
1776
   return conv;
1761
 }
1777
 }
1762
 
1778
 
1779
+#if defined(MANUAL_BED_LEVELING)
1780
+static int _lcd_level_bed_position;
1781
+static void _lcd_level_bed()
1782
+{
1783
+  if (encoderPosition != 0) {
1784
+    refresh_cmd_timeout();
1785
+    current_position[Z_AXIS] += float((int)encoderPosition) * 0.05;
1786
+    if (min_software_endstops && current_position[Z_AXIS] < Z_MIN_POS) current_position[Z_AXIS] = Z_MIN_POS;
1787
+    if (max_software_endstops && current_position[Z_AXIS] > Z_MAX_POS) current_position[Z_AXIS] = Z_MAX_POS;
1788
+    encoderPosition = 0;
1789
+    plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], manual_feedrate[Z_AXIS]/60, active_extruder);
1790
+    lcdDrawUpdate = 1;
1791
+  }
1792
+  if (lcdDrawUpdate) lcd_implementation_drawedit(PSTR("Z"), ftostr32(current_position[Z_AXIS]));
1793
+  static bool debounce_click = false;
1794
+  if (LCD_CLICKED) {
1795
+    if (!debounce_click) {
1796
+      debounce_click = true;
1797
+      int ix = _lcd_level_bed_position % MESH_NUM_X_POINTS;
1798
+      int iy = _lcd_level_bed_position / MESH_NUM_X_POINTS;
1799
+      mbl.set_z(ix, iy, current_position[Z_AXIS]);
1800
+      _lcd_level_bed_position++;
1801
+      if (_lcd_level_bed_position == MESH_NUM_X_POINTS*MESH_NUM_Y_POINTS) {
1802
+        current_position[Z_AXIS] = MESH_HOME_SEARCH_Z;
1803
+        plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], manual_feedrate[X_AXIS]/60, active_extruder);
1804
+        mbl.active = 1;
1805
+        enquecommands_P(PSTR("G28"));
1806
+        lcd_return_to_status();
1807
+      } else {
1808
+        current_position[Z_AXIS] = MESH_HOME_SEARCH_Z;
1809
+        plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], manual_feedrate[X_AXIS]/60, active_extruder);
1810
+        ix = _lcd_level_bed_position % MESH_NUM_X_POINTS;
1811
+        iy = _lcd_level_bed_position / MESH_NUM_X_POINTS;
1812
+        if (iy&1) { // Zig zag
1813
+          ix = (MESH_NUM_X_POINTS - 1) - ix;
1814
+        }
1815
+        current_position[X_AXIS] = mbl.get_x(ix);
1816
+        current_position[Y_AXIS] = mbl.get_y(iy);
1817
+        plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], manual_feedrate[X_AXIS]/60, active_extruder);
1818
+        lcdDrawUpdate = 1;
1819
+      }
1820
+    }
1821
+  } else {
1822
+    debounce_click = false;
1823
+  }
1824
+}
1825
+static void _lcd_level_bed_homing()
1826
+{
1827
+  if (axis_known_position[X_AXIS] &&
1828
+      axis_known_position[Y_AXIS] &&
1829
+      axis_known_position[Z_AXIS]) {
1830
+    current_position[Z_AXIS] = MESH_HOME_SEARCH_Z;
1831
+    plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
1832
+    current_position[X_AXIS] = MESH_MIN_X;
1833
+    current_position[Y_AXIS] = MESH_MIN_Y;
1834
+    plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], manual_feedrate[X_AXIS]/60, active_extruder);
1835
+    _lcd_level_bed_position = 0;
1836
+    lcd_goto_menu(_lcd_level_bed);
1837
+  }
1838
+}
1839
+static void lcd_level_bed()
1840
+{
1841
+  axis_known_position[X_AXIS] = false;
1842
+  axis_known_position[Y_AXIS] = false;
1843
+  axis_known_position[Z_AXIS] = false;
1844
+  mbl.reset();
1845
+  enquecommands_P(PSTR("G28"));
1846
+  lcd_goto_menu(_lcd_level_bed_homing);
1847
+}
1848
+#endif  // MANUAL_BED_LEVELING
1849
+
1763
 #endif //ULTRA_LCD
1850
 #endif //ULTRA_LCD

Loading…
Cancel
Save