Subversion Repositories Tronxy-X3A-Marlin

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 ron 1
/**
2
 * Marlin 3D Printer Firmware
3
 * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4
 *
5
 * Based on Sprinter and grbl.
6
 * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 */
22
 
23
#ifndef _MESH_BED_LEVELING_H_
24
#define _MESH_BED_LEVELING_H_
25
 
26
#include "MarlinConfig.h"
27
 
28
enum MeshLevelingState : char {
29
  MeshReport,
30
  MeshStart,
31
  MeshNext,
32
  MeshSet,
33
  MeshSetZOffset,
34
  MeshReset
35
};
36
 
37
#define MESH_X_DIST ((MESH_MAX_X - (MESH_MIN_X)) / (GRID_MAX_POINTS_X - 1))
38
#define MESH_Y_DIST ((MESH_MAX_Y - (MESH_MIN_Y)) / (GRID_MAX_POINTS_Y - 1))
39
 
40
class mesh_bed_leveling {
41
public:
42
  static float z_offset,
43
               z_values[GRID_MAX_POINTS_X][GRID_MAX_POINTS_Y],
44
               index_to_xpos[GRID_MAX_POINTS_X],
45
               index_to_ypos[GRID_MAX_POINTS_Y];
46
 
47
  mesh_bed_leveling();
48
 
49
  static void report_mesh();
50
 
51
  static void reset();
52
 
53
  FORCE_INLINE static bool has_mesh() {
54
    for (uint8_t x = 0; x < GRID_MAX_POINTS_X; x++)
55
      for (uint8_t y = 0; y < GRID_MAX_POINTS_Y; y++)
56
        if (z_values[x][y]) return true;
57
    return false;
58
  }
59
 
60
  static void set_z(const int8_t px, const int8_t py, const float &z) { z_values[px][py] = z; }
61
 
62
  static inline void zigzag(const int8_t index, int8_t &px, int8_t &py) {
63
    px = index % (GRID_MAX_POINTS_X);
64
    py = index / (GRID_MAX_POINTS_X);
65
    if (py & 1) px = (GRID_MAX_POINTS_X - 1) - px; // Zig zag
66
  }
67
 
68
  static void set_zigzag_z(const int8_t index, const float &z) {
69
    int8_t px, py;
70
    zigzag(index, px, py);
71
    set_z(px, py, z);
72
  }
73
 
74
  static int8_t cell_index_x(const float &x) {
75
    int8_t cx = (x - (MESH_MIN_X)) * (1.0f / (MESH_X_DIST));
76
    return constrain(cx, 0, (GRID_MAX_POINTS_X) - 2);
77
  }
78
 
79
  static int8_t cell_index_y(const float &y) {
80
    int8_t cy = (y - (MESH_MIN_Y)) * (1.0f / (MESH_Y_DIST));
81
    return constrain(cy, 0, (GRID_MAX_POINTS_Y) - 2);
82
  }
83
 
84
  static int8_t probe_index_x(const float &x) {
85
    int8_t px = (x - (MESH_MIN_X) + 0.5f * (MESH_X_DIST)) * (1.0f / (MESH_X_DIST));
86
    return WITHIN(px, 0, GRID_MAX_POINTS_X - 1) ? px : -1;
87
  }
88
 
89
  static int8_t probe_index_y(const float &y) {
90
    int8_t py = (y - (MESH_MIN_Y) + 0.5f * (MESH_Y_DIST)) * (1.0f / (MESH_Y_DIST));
91
    return WITHIN(py, 0, GRID_MAX_POINTS_Y - 1) ? py : -1;
92
  }
93
 
94
  static float calc_z0(const float &a0, const float &a1, const float &z1, const float &a2, const float &z2) {
95
    const float delta_z = (z2 - z1) / (a2 - a1),
96
                delta_a = a0 - a1;
97
    return z1 + delta_a * delta_z;
98
  }
99
 
100
  static float get_z(const float &x0, const float &y0
101
    #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
102
      , const float &factor
103
    #endif
104
  ) {
105
    const int8_t cx = cell_index_x(x0), cy = cell_index_y(y0);
106
    const float z1 = calc_z0(x0, index_to_xpos[cx], z_values[cx][cy], index_to_xpos[cx + 1], z_values[cx + 1][cy]),
107
                z2 = calc_z0(x0, index_to_xpos[cx], z_values[cx][cy + 1], index_to_xpos[cx + 1], z_values[cx + 1][cy + 1]),
108
                z0 = calc_z0(y0, index_to_ypos[cy], z1, index_to_ypos[cy + 1], z2);
109
 
110
    return z_offset + z0
111
      #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
112
        * factor
113
      #endif
114
    ;
115
  }
116
};
117
 
118
extern mesh_bed_leveling mbl;
119
 
120
#endif // _MESH_BED_LEVELING_H_