Browse Source

WIP. Adding bed leveling code.

Edward Patel 9 years ago
parent
commit
0e51e53813
6 changed files with 184 additions and 13 deletions
  1. 16
    0
      Marlin/Configuration.h
  2. 70
    3
      Marlin/Marlin_main.cpp
  3. 7
    0
      Marlin/mesh_bed_leveling.cpp
  4. 69
    0
      Marlin/mesh_bed_leveling.h
  5. 16
    5
      Marlin/planner.cpp
  6. 6
    5
      Marlin/planner.h

+ 16
- 0
Marlin/Configuration.h View File

@@ -373,6 +373,22 @@ const bool Z_MAX_ENDSTOP_INVERTING = false; // set to true to invert the logic o
373 373
 //#define ENDSTOPPULLUP_FIL_RUNOUT // Uncomment to use internal pullup for filament runout pins if the sensor is defined.
374 374
 
375 375
 //===========================================================================
376
+//============================ Manual Bed Leveling ==========================
377
+//===========================================================================
378
+
379
+#define MANUAL_BED_LEVELING  // Add display menu option for bed leveling
380
+#define MESH_BED_LEVELING    // Enable mesh bed leveling
381
+
382
+#if defined(MESH_BED_LEVELING)
383
+  #define MESH_MIN_X 10
384
+  #define MESH_MAX_X (X_MAX_POS - MESH_MIN_X)
385
+  #define MESH_MIN_Y 10
386
+  #define MESH_MAX_Y (Y_MAX_POS - MESH_MIN_Y)
387
+  #define MESH_NUM_X_POINTS 4
388
+  #define MESH_NUM_Y_POINTS 3
389
+#endif  // MESH_BED_LEVELING
390
+
391
+//===========================================================================
376 392
 //============================= Bed Auto Leveling ===========================
377 393
 //===========================================================================
378 394
 

+ 70
- 3
Marlin/Marlin_main.cpp View File

@@ -41,6 +41,10 @@
41 41
 
42 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 48
 #include "ultralcd.h"
45 49
 #include "planner.h"
46 50
 #include "stepper.h"
@@ -4987,6 +4991,65 @@ void calculate_delta(float cartesian[3])
4987 4991
 }
4988 4992
 #endif
4989 4993
 
4994
+#if defined(MESH_BED_LEVELING)
4995
+#if !defined(MIN)
4996
+#define MIN(_v1, _v2) (((_v1) < (_v2)) ? (_v1) : (_v2))
4997
+#endif  // ! MIN
4998
+// This function is used to split lines on mesh borders so each segment is only part of one mesh area
4999
+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)
5000
+{
5001
+  int pix = mbl.select_x_index(current_position[X_AXIS]);
5002
+  int piy = mbl.select_y_index(current_position[Y_AXIS]);
5003
+  int ix = mbl.select_x_index(x);
5004
+  int iy = mbl.select_y_index(y);
5005
+  pix = MIN(pix, MESH_NUM_X_POINTS-2);
5006
+  piy = MIN(piy, MESH_NUM_Y_POINTS-2);
5007
+  ix = MIN(ix, MESH_NUM_X_POINTS-2);
5008
+  iy = MIN(iy, MESH_NUM_Y_POINTS-2);
5009
+  if (ix > pix && (x_splits)&(1<<ix)) {
5010
+    float nx = mbl.get_x(ix);
5011
+    float normalized_dist = (nx - current_position[X_AXIS])/(x - current_position[X_AXIS]);
5012
+    float ny = current_position[Y_AXIS] + (y - current_position[Y_AXIS]) * normalized_dist;
5013
+    float ne = current_position[E_AXIS] + (e - current_position[E_AXIS]) * normalized_dist;
5014
+    x_splits ^= 1 << ix;
5015
+    mesh_plan_buffer_line(nx, ny, z, ne, feed_rate, extruder, x_splits, y_splits);
5016
+    mesh_plan_buffer_line(x, y, z, e, feed_rate, extruder, x_splits, y_splits);
5017
+    return;
5018
+  } else if (ix < pix && (x_splits)&(1<<pix)) {
5019
+    float nx = mbl.get_x(pix);
5020
+    float normalized_dist = (nx - current_position[X_AXIS])/(x - current_position[X_AXIS]);
5021
+    float ny = current_position[Y_AXIS] + (y - current_position[Y_AXIS]) * normalized_dist;
5022
+    float ne = current_position[E_AXIS] + (e - current_position[E_AXIS]) * normalized_dist;
5023
+    x_splits ^= 1 << pix;
5024
+    mesh_plan_buffer_line(nx, ny, z, ne, feed_rate, extruder, x_splits, y_splits);
5025
+    mesh_plan_buffer_line(x, y, z, e, feed_rate, extruder, x_splits, y_splits);
5026
+    return;
5027
+  } else if (iy > piy && (y_splits)&(1<<iy)) {
5028
+    float ny = mbl.get_y(iy);
5029
+    float normalized_dist = (ny - current_position[Y_AXIS])/(y - current_position[Y_AXIS]);
5030
+    float nx = current_position[X_AXIS] + (x - current_position[X_AXIS]) * normalized_dist;
5031
+    float ne = current_position[E_AXIS] + (e - current_position[E_AXIS]) * normalized_dist;
5032
+    y_splits ^= 1 << iy;
5033
+    mesh_plan_buffer_line(nx, ny, z, ne, feed_rate, extruder, x_splits, y_splits);
5034
+    mesh_plan_buffer_line(x, y, z, e, feed_rate, extruder, x_splits, y_splits);
5035
+    return;
5036
+  } else if (iy < piy && (y_splits)&(1<<piy)) {
5037
+    float ny = mbl.get_y(piy);
5038
+    float normalized_dist = (ny - current_position[Y_AXIS])/(y - current_position[Y_AXIS]);
5039
+    float nx = current_position[X_AXIS] + (x - current_position[X_AXIS]) * normalized_dist;
5040
+    float ne = current_position[E_AXIS] + (e - current_position[E_AXIS]) * normalized_dist;
5041
+    y_splits ^= 1 << piy;
5042
+    mesh_plan_buffer_line(nx, ny, z, ne, feed_rate, extruder, x_splits, y_splits);
5043
+    mesh_plan_buffer_line(x, y, z, e, feed_rate, extruder, x_splits, y_splits);
5044
+    return;
5045
+  }
5046
+  plan_buffer_line(x, y, z, e, feedrate, extruder);
5047
+  for(int8_t i=0; i < NUM_AXIS; i++) {
5048
+    current_position[i] = destination[i];
5049
+  }
5050
+}
5051
+#endif  // MESH_BED_LEVELING
5052
+
4990 5053
 void prepare_move()
4991 5054
 {
4992 5055
   clamp_to_software_endstops(destination);
@@ -5102,10 +5165,14 @@ for (int s = 1; s <= steps; s++) {
5102 5165
 #if ! (defined DELTA || defined SCARA)
5103 5166
   // Do not use feedmultiply for E or Z only moves
5104 5167
   if( (current_position[X_AXIS] == destination [X_AXIS]) && (current_position[Y_AXIS] == destination [Y_AXIS])) {
5105
-      plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
5106
-  }
5107
-  else {
5168
+    plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
5169
+  } else {
5170
+#if defined(MESH_BED_LEVELING)
5171
+    mesh_plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate*feedmultiply/60/100.0, active_extruder);
5172
+    return;
5173
+#else
5108 5174
     plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate*feedmultiply/60/100.0, active_extruder);
5175
+#endif  // MESH_BED_LEVELING
5109 5176
   }
5110 5177
 #endif // !(DELTA || SCARA)
5111 5178
 

+ 7
- 0
Marlin/mesh_bed_leveling.cpp View File

@@ -0,0 +1,7 @@
1
+#include "mesh_bed_leveling.h"
2
+
3
+#if defined(MESH_BED_LEVELING)
4
+
5
+mesh_bed_leveling mbl;
6
+
7
+#endif  // MESH_BED_LEVELING

+ 69
- 0
Marlin/mesh_bed_leveling.h View File

@@ -0,0 +1,69 @@
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
+    
11
+    float z_values[MESH_NUM_Y_POINTS][MESH_NUM_X_POINTS];
12
+    
13
+    mesh_bed_leveling() {
14
+        reset();
15
+    }
16
+    
17
+    void reset() {
18
+        for (int y=0; y<MESH_NUM_Y_POINTS; y++) {
19
+            for (int x=0; x<MESH_NUM_X_POINTS; x++) {
20
+                z_values[y][x] = 0;
21
+            }
22
+        }
23
+    }
24
+    
25
+    float get_x(int i) { return MESH_MIN_X + MESH_X_DIST*i; }
26
+    float get_y(int i) { return MESH_MIN_Y + MESH_Y_DIST*i; }
27
+    void set_z(int ix, int iy, float z) { z_values[iy][ix] = z; }
28
+    
29
+    int select_x_index(float x) {
30
+        int i = 1;
31
+        while (x > get_x(i) && i < MESH_NUM_X_POINTS-1) {
32
+            i++;
33
+        }
34
+        return i-1;
35
+    }
36
+    
37
+    int select_y_index(float y) {
38
+        int i = 1;
39
+        while (y > get_y(i) && i < MESH_NUM_Y_POINTS-1) {
40
+            i++;
41
+        }
42
+        return i-1;
43
+    }
44
+    
45
+    float calc_z0(float a0, float a1, float z1, float a2, float z2) {
46
+        float delta_z = (z2 - z1)/(a2 - a1);
47
+        float delta_a = a0 - a1;
48
+        return z1 + delta_a * delta_z;
49
+    }
50
+    
51
+    float get_z(float x0, float y0) {
52
+        int x_index = select_x_index(x0);
53
+        int y_index = select_y_index(y0);
54
+        float z1 = calc_z0(x0,
55
+                           get_x(x_index), z_values[y_index][x_index],
56
+                           get_x(x_index+1), z_values[y_index][x_index+1]);
57
+        float z2 = calc_z0(x0,
58
+                           get_x(x_index), z_values[y_index+1][x_index],
59
+                           get_x(x_index+1), z_values[y_index+1][x_index+1]);
60
+        float z0 = calc_z0(y0,
61
+                           get_y(y_index), z1,
62
+                           get_y(y_index+1), z2);
63
+        return z0;
64
+    }
65
+};
66
+
67
+extern mesh_bed_leveling mbl;
68
+
69
+#endif  // MESH_BED_LEVELING

+ 16
- 5
Marlin/planner.cpp View File

@@ -58,6 +58,10 @@
58 58
 #include "ultralcd.h"
59 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 66
 //============================= public variables ============================
63 67
 //===========================================================================
@@ -530,7 +534,7 @@ float junction_deviation = 0.1;
530 534
 // Add a new linear movement to the buffer. steps_x, _y and _z is the absolute position in 
531 535
 // mm. Microseconds specify how many microseconds the move should take to perform. To aid acceleration
532 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 538
 void plan_buffer_line(float x, float y, float z, const float &e, float feed_rate, const uint8_t &extruder)
535 539
 #else
536 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,6 +552,10 @@ void plan_buffer_line(const float &x, const float &y, const float &z, const floa
548 552
     lcd_update();
549 553
   }
550 554
 
555
+#if defined(MESH_BED_LEVELING)
556
+  z += mbl.get_z(x, y);
557
+#endif  // MESH_BED_LEVELING
558
+
551 559
 #ifdef ENABLE_AUTO_BED_LEVELING
552 560
   apply_rotation_xyz(plan_bed_level_matrix, x, y, z);
553 561
 #endif // ENABLE_AUTO_BED_LEVELING
@@ -1078,14 +1086,17 @@ vector_3 plan_get_position() {
1078 1086
 }
1079 1087
 #endif // ENABLE_AUTO_BED_LEVELING
1080 1088
 
1081
-#ifdef ENABLE_AUTO_BED_LEVELING
1089
+#if defined(ENABLE_AUTO_BED_LEVELING) || defined(MESH_BED_LEVELING)
1082 1090
 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 1091
 #else
1086 1092
 void plan_set_position(const float &x, const float &y, const float &z, const float &e)
1093
+#endif  // ENABLE_AUTO_BED_LEVELING || MESH_BED_LEVELING
1087 1094
 {
1088
-#endif // ENABLE_AUTO_BED_LEVELING
1095
+#if defined(ENABLE_AUTO_BED_LEVELING)
1096
+  apply_rotation_xyz(plan_bed_level_matrix, x, y, z);
1097
+#elif defined(MESH_BED_LEVELING)
1098
+  z += mbl.get_z(x, y);
1099
+#endif  // ENABLE_AUTO_BED_LEVELING
1089 1100
 
1090 1101
   position[X_AXIS] = lround(x*axis_steps_per_unit[X_AXIS]);
1091 1102
   position[Y_AXIS] = lround(y*axis_steps_per_unit[Y_AXIS]);

+ 6
- 5
Marlin/planner.h View File

@@ -82,21 +82,22 @@ void plan_init();
82 82
 // Add a new linear movement to the buffer. x, y and z is the signed, absolute target position in 
83 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 86
 void plan_buffer_line(float x, float y, float z, const float &e, float feed_rate, const uint8_t &extruder);
87
-
88 87
 // Get the position applying the bed level matrix if enabled
88
+#if defined(ENABLE_AUTO_BED_LEVELING)
89 89
 vector_3 plan_get_position();
90
+#endif  // ENABLE_AUTO_BED_LEVELING
90 91
 #else
91 92
 void plan_buffer_line(const float &x, const float &y, const float &z, const float &e, float feed_rate, const uint8_t &extruder);
92
-#endif // ENABLE_AUTO_BED_LEVELING
93
+#endif  // ENABLE_AUTO_BED_LEVELING || MESH_BED_LEVELING
93 94
 
94 95
 // Set position. Used for G92 instructions.
95
-#ifdef ENABLE_AUTO_BED_LEVELING
96
+#if defined(ENABLE_AUTO_BED_LEVELING) || defined(MESH_BED_LEVELING)
96 97
 void plan_set_position(float x, float y, float z, const float &e);
97 98
 #else
98 99
 void plan_set_position(const float &x, const float &y, const float &z, const float &e);
99
-#endif // ENABLE_AUTO_BED_LEVELING
100
+#endif // ENABLE_AUTO_BED_LEVELING || MESH_BED_LEVELING
100 101
 
101 102
 void plan_set_e_position(const float &e);
102 103
 

Loading…
Cancel
Save