|
@@ -0,0 +1,356 @@
|
|
1
|
+/**
|
|
2
|
+ * Marlin 3D Printer Firmware
|
|
3
|
+ * Copyright (C) 2016, 2017 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 I2CPOSENC_H
|
|
24
|
+#define I2CPOSENC_H
|
|
25
|
+
|
|
26
|
+#include "MarlinConfig.h"
|
|
27
|
+
|
|
28
|
+#if ENABLED(I2C_POSITION_ENCODERS)
|
|
29
|
+
|
|
30
|
+ #include "enum.h"
|
|
31
|
+ #include "macros.h"
|
|
32
|
+ #include "types.h"
|
|
33
|
+ #include <Wire.h>
|
|
34
|
+
|
|
35
|
+ //=========== Advanced / Less-Common Encoder Configuration Settings ==========
|
|
36
|
+
|
|
37
|
+ #define I2CPE_EC_THRESH_PROPORTIONAL // if enabled adjusts the error correction threshold
|
|
38
|
+ // proportional to the current speed of the axis allows
|
|
39
|
+ // for very small error margin at low speeds without
|
|
40
|
+ // stuttering due to reading latency at high speeds
|
|
41
|
+
|
|
42
|
+ #define I2CPE_DEBUG // enable encoder-related debug serial echos
|
|
43
|
+
|
|
44
|
+ #define I2CPE_REBOOT_TIME 5000 // time we wait for an encoder module to reboot
|
|
45
|
+ // after changing address.
|
|
46
|
+
|
|
47
|
+ #define I2CPE_MAG_SIG_GOOD 0
|
|
48
|
+ #define I2CPE_MAG_SIG_MID 1
|
|
49
|
+ #define I2CPE_MAG_SIG_BAD 2
|
|
50
|
+ #define I2CPE_MAG_SIG_NF 255
|
|
51
|
+
|
|
52
|
+ #define I2CPE_REQ_REPORT 0
|
|
53
|
+ #define I2CPE_RESET_COUNT 1
|
|
54
|
+ #define I2CPE_SET_ADDR 2
|
|
55
|
+ #define I2CPE_SET_REPORT_MODE 3
|
|
56
|
+ #define I2CPE_CLEAR_EEPROM 4
|
|
57
|
+
|
|
58
|
+ #define I2CPE_LED_PAR_MODE 10
|
|
59
|
+ #define I2CPE_LED_PAR_BRT 11
|
|
60
|
+ #define I2CPE_LED_PAR_RATE 14
|
|
61
|
+
|
|
62
|
+ #define I2CPE_REPORT_DISTANCE 0
|
|
63
|
+ #define I2CPE_REPORT_STRENGTH 1
|
|
64
|
+ #define I2CPE_REPORT_VERSION 2
|
|
65
|
+
|
|
66
|
+ // Default I2C addresses
|
|
67
|
+ #define I2CPE_PRESET_ADDR_X 30
|
|
68
|
+ #define I2CPE_PRESET_ADDR_Y 31
|
|
69
|
+ #define I2CPE_PRESET_ADDR_Z 32
|
|
70
|
+ #define I2CPE_PRESET_ADDR_E 33
|
|
71
|
+
|
|
72
|
+ #define I2CPE_DEF_AXIS X_AXIS
|
|
73
|
+ #define I2CPE_DEF_ADDR I2CPE_PRESET_ADDR_X
|
|
74
|
+
|
|
75
|
+ // Error event counter; tracks how many times there is an error exceeding a certain threshold
|
|
76
|
+ #define I2CPE_ERR_CNT_THRESH 3.00
|
|
77
|
+ #define I2CPE_ERR_CNT_DEBOUNCE_MS 2000
|
|
78
|
+
|
|
79
|
+ #if ENABLED(I2CPE_ERR_ROLLING_AVERAGE)
|
|
80
|
+ #define I2CPE_ERR_ARRAY_SIZE 32
|
|
81
|
+ #endif
|
|
82
|
+
|
|
83
|
+ // Error Correction Methods
|
|
84
|
+ #define I2CPE_ECM_NONE 0
|
|
85
|
+ #define I2CPE_ECM_MICROSTEP 1
|
|
86
|
+ #define I2CPE_ECM_PLANNER 2
|
|
87
|
+ #define I2CPE_ECM_STALLDETECT 3
|
|
88
|
+
|
|
89
|
+ // Encoder types
|
|
90
|
+ #define I2CPE_ENC_TYPE_ROTARY 0
|
|
91
|
+ #define I2CPE_ENC_TYPE_LINEAR 1
|
|
92
|
+
|
|
93
|
+ // Parser
|
|
94
|
+ #define I2CPE_PARSE_ERR 1
|
|
95
|
+ #define I2CPE_PARSE_OK 0
|
|
96
|
+
|
|
97
|
+ #define LOOP_PE(VAR) LOOP_L_N(VAR, I2CPE_ENCODER_CNT)
|
|
98
|
+ #define CHECK_IDX if (!WITHIN(idx, 0, I2CPE_ENCODER_CNT - 1)) return;
|
|
99
|
+
|
|
100
|
+ extern const char axis_codes[XYZE];
|
|
101
|
+
|
|
102
|
+ typedef union {
|
|
103
|
+ volatile long val = 0;
|
|
104
|
+ uint8_t bval[4];
|
|
105
|
+ } i2cLong;
|
|
106
|
+
|
|
107
|
+ class I2CPositionEncoder {
|
|
108
|
+ private:
|
|
109
|
+ AxisEnum encoderAxis = I2CPE_DEF_AXIS;
|
|
110
|
+
|
|
111
|
+ uint8_t i2cAddress = I2CPE_DEF_ADDR,
|
|
112
|
+ ecMethod = I2CPE_DEF_EC_METHOD,
|
|
113
|
+ type = I2CPE_DEF_TYPE,
|
|
114
|
+ H = I2CPE_MAG_SIG_NF; // Magnetic field strength
|
|
115
|
+
|
|
116
|
+ int encoderTicksPerUnit = I2CPE_DEF_ENC_TICKS_UNIT,
|
|
117
|
+ stepperTicks = I2CPE_DEF_TICKS_REV;
|
|
118
|
+
|
|
119
|
+ float ecThreshold = I2CPE_DEF_EC_THRESH;
|
|
120
|
+
|
|
121
|
+ bool homed = false,
|
|
122
|
+ trusted = false,
|
|
123
|
+ initialised = false,
|
|
124
|
+ active = false,
|
|
125
|
+ invert = false,
|
|
126
|
+ ec = true;
|
|
127
|
+
|
|
128
|
+ int errorCount = 0,
|
|
129
|
+ errorPrev = 0;
|
|
130
|
+
|
|
131
|
+ float axisOffset = 0;
|
|
132
|
+
|
|
133
|
+ long axisOffsetTicks = 0,
|
|
134
|
+ zeroOffset = 0,
|
|
135
|
+ lastPosition = 0,
|
|
136
|
+ position;
|
|
137
|
+
|
|
138
|
+ unsigned long lastPositionTime = 0,
|
|
139
|
+ lastErrorCountTime = 0,
|
|
140
|
+ lastErrorTime;
|
|
141
|
+
|
|
142
|
+ //double positionMm; //calculate
|
|
143
|
+
|
|
144
|
+ #if ENABLED(I2CPE_ERR_ROLLING_AVERAGE)
|
|
145
|
+ uint8_t errIdx = 0;
|
|
146
|
+ int err[I2CPE_ERR_ARRAY_SIZE] = {0};
|
|
147
|
+ #endif
|
|
148
|
+
|
|
149
|
+ public:
|
|
150
|
+ void init(uint8_t address, AxisEnum axis);
|
|
151
|
+ void reset();
|
|
152
|
+
|
|
153
|
+ void update();
|
|
154
|
+
|
|
155
|
+ void set_homed();
|
|
156
|
+
|
|
157
|
+ long get_raw_count();
|
|
158
|
+
|
|
159
|
+ FORCE_INLINE double mm_from_count(long count) {
|
|
160
|
+ if (type == I2CPE_ENC_TYPE_LINEAR) return count / encoderTicksPerUnit;
|
|
161
|
+ else if (type == I2CPE_ENC_TYPE_ROTARY)
|
|
162
|
+ return (count * stepperTicks) / (encoderTicksPerUnit * planner.axis_steps_per_mm[encoderAxis]);
|
|
163
|
+ return -1;
|
|
164
|
+ }
|
|
165
|
+
|
|
166
|
+ FORCE_INLINE double get_position_mm() { return mm_from_count(get_position()); }
|
|
167
|
+ FORCE_INLINE long get_position() { return get_raw_count() - zeroOffset - axisOffsetTicks; }
|
|
168
|
+
|
|
169
|
+ long get_axis_error_steps(bool report);
|
|
170
|
+ double get_axis_error_mm(bool report);
|
|
171
|
+
|
|
172
|
+ void calibrate_steps_mm(int iter);
|
|
173
|
+
|
|
174
|
+ bool passes_test(bool report);
|
|
175
|
+
|
|
176
|
+ bool test_axis(void);
|
|
177
|
+
|
|
178
|
+ FORCE_INLINE int get_error_count(void) { return errorCount; }
|
|
179
|
+ FORCE_INLINE void set_error_count(int newCount) { errorCount = newCount; }
|
|
180
|
+
|
|
181
|
+ FORCE_INLINE uint8_t get_address() { return i2cAddress; }
|
|
182
|
+ FORCE_INLINE void set_address(uint8_t addr) { i2cAddress = addr; }
|
|
183
|
+
|
|
184
|
+ FORCE_INLINE bool get_active(void) { return active; }
|
|
185
|
+ FORCE_INLINE void set_active(bool a) { active = a; }
|
|
186
|
+
|
|
187
|
+ FORCE_INLINE void set_inverted(bool i) { invert = i; }
|
|
188
|
+
|
|
189
|
+ FORCE_INLINE AxisEnum get_axis() { return encoderAxis; }
|
|
190
|
+
|
|
191
|
+ FORCE_INLINE bool get_ec_enabled() { return ec; }
|
|
192
|
+ FORCE_INLINE void set_ec_enabled(bool enabled) { ec = enabled; }
|
|
193
|
+
|
|
194
|
+ FORCE_INLINE uint8_t get_ec_method() { return ecMethod; }
|
|
195
|
+ FORCE_INLINE void set_ec_method(byte method) { ecMethod = method; }
|
|
196
|
+
|
|
197
|
+ FORCE_INLINE float get_ec_threshold() { return ecThreshold; }
|
|
198
|
+ FORCE_INLINE void set_ec_threshold(float newThreshold) { ecThreshold = newThreshold; }
|
|
199
|
+
|
|
200
|
+ FORCE_INLINE int get_encoder_ticks_mm() {
|
|
201
|
+ if (type == I2CPE_ENC_TYPE_LINEAR) return encoderTicksPerUnit;
|
|
202
|
+ else if (type == I2CPE_ENC_TYPE_ROTARY)
|
|
203
|
+ return (int)((encoderTicksPerUnit / stepperTicks) * planner.axis_steps_per_mm[encoderAxis]);
|
|
204
|
+ return 0;
|
|
205
|
+ }
|
|
206
|
+
|
|
207
|
+ FORCE_INLINE int get_ticks_unit() { return encoderTicksPerUnit; }
|
|
208
|
+ FORCE_INLINE void set_ticks_unit(int ticks) { encoderTicksPerUnit = ticks; }
|
|
209
|
+
|
|
210
|
+ FORCE_INLINE uint8_t get_type() { return type; }
|
|
211
|
+ FORCE_INLINE void set_type(byte newType) { type = newType; }
|
|
212
|
+
|
|
213
|
+ FORCE_INLINE int get_stepper_ticks() { return stepperTicks; }
|
|
214
|
+ FORCE_INLINE void set_stepper_ticks(int ticks) { stepperTicks = ticks; }
|
|
215
|
+
|
|
216
|
+ FORCE_INLINE float get_axis_offset() { return axisOffset; }
|
|
217
|
+ FORCE_INLINE void set_axis_offset(float newOffset) {
|
|
218
|
+ axisOffset = newOffset;
|
|
219
|
+ axisOffsetTicks = (long)(axisOffset * get_encoder_ticks_mm());
|
|
220
|
+ }
|
|
221
|
+
|
|
222
|
+ FORCE_INLINE void set_current_position(float newPositionMm) {
|
|
223
|
+ set_axis_offset(get_position_mm() - newPositionMm + axisOffset);
|
|
224
|
+ }
|
|
225
|
+ };
|
|
226
|
+
|
|
227
|
+ class I2CPositionEncodersMgr {
|
|
228
|
+ private:
|
|
229
|
+ bool I2CPE_anyaxis;
|
|
230
|
+ uint8_t I2CPE_addr;
|
|
231
|
+ int8_t I2CPE_idx;
|
|
232
|
+
|
|
233
|
+ public:
|
|
234
|
+ void init(void);
|
|
235
|
+
|
|
236
|
+ // consider only updating one endoder per call / tick if encoders become too time intensive
|
|
237
|
+ void update(void) { LOOP_PE(i) encoders[i].update(); }
|
|
238
|
+
|
|
239
|
+ void homed(AxisEnum axis) {
|
|
240
|
+ LOOP_PE(i)
|
|
241
|
+ if (encoders[i].get_axis() == axis) encoders[i].set_homed();
|
|
242
|
+ }
|
|
243
|
+
|
|
244
|
+ void report_position(uint8_t idx, bool units, bool noOffset);
|
|
245
|
+
|
|
246
|
+ void report_status(uint8_t idx) {
|
|
247
|
+ CHECK_IDX
|
|
248
|
+ SERIAL_ECHOPAIR("Encoder ",idx);
|
|
249
|
+ SERIAL_ECHOPGM(": ");
|
|
250
|
+ encoders[idx].get_raw_count();
|
|
251
|
+ encoders[idx].passes_test(true);
|
|
252
|
+ }
|
|
253
|
+
|
|
254
|
+ void report_error(uint8_t idx) {
|
|
255
|
+ CHECK_IDX
|
|
256
|
+ encoders[idx].get_axis_error_steps(true);
|
|
257
|
+ }
|
|
258
|
+
|
|
259
|
+ void test_axis(uint8_t idx) {
|
|
260
|
+ CHECK_IDX
|
|
261
|
+ encoders[idx].test_axis();
|
|
262
|
+ }
|
|
263
|
+
|
|
264
|
+ void calibrate_steps_mm(uint8_t idx, int iterations) {
|
|
265
|
+ CHECK_IDX
|
|
266
|
+ encoders[idx].calibrate_steps_mm(iterations);
|
|
267
|
+ }
|
|
268
|
+
|
|
269
|
+ void change_module_address(uint8_t oldaddr, uint8_t newaddr);
|
|
270
|
+ void report_module_firmware(uint8_t address);
|
|
271
|
+
|
|
272
|
+ void report_error_count(uint8_t idx, AxisEnum axis) {
|
|
273
|
+ CHECK_IDX
|
|
274
|
+ SERIAL_ECHOPAIR("Error count on ", axis_codes[axis]);
|
|
275
|
+ SERIAL_ECHOLNPAIR(" axis is ", encoders[idx].get_error_count());
|
|
276
|
+ }
|
|
277
|
+
|
|
278
|
+ void reset_error_count(uint8_t idx, AxisEnum axis) {
|
|
279
|
+ CHECK_IDX
|
|
280
|
+ encoders[idx].set_error_count(0);
|
|
281
|
+ SERIAL_ECHOPAIR("Error count on ", axis_codes[axis]);
|
|
282
|
+ SERIAL_ECHOLNPGM(" axis has been reset.");
|
|
283
|
+ }
|
|
284
|
+
|
|
285
|
+ void enable_ec(uint8_t idx, bool enabled, AxisEnum axis) {
|
|
286
|
+ CHECK_IDX
|
|
287
|
+ encoders[idx].set_ec_enabled(enabled);
|
|
288
|
+ SERIAL_ECHOPAIR("Error correction on ", axis_codes[axis]);
|
|
289
|
+ SERIAL_ECHOPGM(" axis is ");
|
|
290
|
+ serialprintPGM(encoders[idx].get_ec_enabled() ? PSTR("en") : PSTR("dis"));
|
|
291
|
+ SERIAL_ECHOLNPGM("abled.");
|
|
292
|
+ }
|
|
293
|
+
|
|
294
|
+ void set_ec_threshold(uint8_t idx, float newThreshold, AxisEnum axis) {
|
|
295
|
+ CHECK_IDX
|
|
296
|
+ encoders[idx].set_ec_threshold(newThreshold);
|
|
297
|
+ SERIAL_ECHOPAIR("Error correct threshold for ", axis_codes[axis]);
|
|
298
|
+ SERIAL_ECHOPAIR_F(" axis set to ", newThreshold);
|
|
299
|
+ SERIAL_ECHOLNPGM("mm.");
|
|
300
|
+ }
|
|
301
|
+
|
|
302
|
+ void get_ec_threshold(uint8_t idx, AxisEnum axis) {
|
|
303
|
+ CHECK_IDX
|
|
304
|
+ float threshold = encoders[idx].get_ec_threshold();
|
|
305
|
+ SERIAL_ECHOPAIR("Error correct threshold for ", axis_codes[axis]);
|
|
306
|
+ SERIAL_ECHOPAIR_F(" axis is ", threshold);
|
|
307
|
+ SERIAL_ECHOLNPGM("mm.");
|
|
308
|
+ }
|
|
309
|
+
|
|
310
|
+ int8_t idx_from_axis(AxisEnum axis) {
|
|
311
|
+ LOOP_PE(i)
|
|
312
|
+ if (encoders[i].get_axis() == axis) return i;
|
|
313
|
+
|
|
314
|
+ return -1;
|
|
315
|
+ }
|
|
316
|
+
|
|
317
|
+ int8_t idx_from_addr(uint8_t addr) {
|
|
318
|
+ LOOP_PE(i)
|
|
319
|
+ if (encoders[i].get_address() == addr) return i;
|
|
320
|
+
|
|
321
|
+ return -1;
|
|
322
|
+ }
|
|
323
|
+
|
|
324
|
+ int8_t parse();
|
|
325
|
+
|
|
326
|
+ void M860();
|
|
327
|
+ void M861();
|
|
328
|
+ void M862();
|
|
329
|
+ void M863();
|
|
330
|
+ void M864();
|
|
331
|
+ void M865();
|
|
332
|
+ void M866();
|
|
333
|
+ void M867();
|
|
334
|
+ void M868();
|
|
335
|
+ void M869();
|
|
336
|
+
|
|
337
|
+ I2CPositionEncoder encoders[I2CPE_ENCODER_CNT];
|
|
338
|
+ };
|
|
339
|
+
|
|
340
|
+ extern I2CPositionEncodersMgr I2CPEM;
|
|
341
|
+
|
|
342
|
+ FORCE_INLINE void gcode_M860() { I2CPEM.M860(); }
|
|
343
|
+ FORCE_INLINE void gcode_M861() { I2CPEM.M861(); }
|
|
344
|
+ FORCE_INLINE void gcode_M862() { I2CPEM.M862(); }
|
|
345
|
+ FORCE_INLINE void gcode_M863() { I2CPEM.M863(); }
|
|
346
|
+ FORCE_INLINE void gcode_M864() { I2CPEM.M864(); }
|
|
347
|
+ FORCE_INLINE void gcode_M865() { I2CPEM.M865(); }
|
|
348
|
+ FORCE_INLINE void gcode_M866() { I2CPEM.M866(); }
|
|
349
|
+ FORCE_INLINE void gcode_M867() { I2CPEM.M867(); }
|
|
350
|
+ FORCE_INLINE void gcode_M868() { I2CPEM.M868(); }
|
|
351
|
+ FORCE_INLINE void gcode_M869() { I2CPEM.M869(); }
|
|
352
|
+
|
|
353
|
+#endif //I2C_POSITION_ENCODERS
|
|
354
|
+#endif //I2CPOSENC_H
|
|
355
|
+
|
|
356
|
+
|