1 |
ron |
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 |
#define I2CPE_ERR_PRST_ARRAY_SIZE 10
|
|
|
82 |
#endif
|
|
|
83 |
|
|
|
84 |
// Error Correction Methods
|
|
|
85 |
#define I2CPE_ECM_NONE 0
|
|
|
86 |
#define I2CPE_ECM_MICROSTEP 1
|
|
|
87 |
#define I2CPE_ECM_PLANNER 2
|
|
|
88 |
#define I2CPE_ECM_STALLDETECT 3
|
|
|
89 |
|
|
|
90 |
// Encoder types
|
|
|
91 |
#define I2CPE_ENC_TYPE_ROTARY 0
|
|
|
92 |
#define I2CPE_ENC_TYPE_LINEAR 1
|
|
|
93 |
|
|
|
94 |
// Parser
|
|
|
95 |
#define I2CPE_PARSE_ERR 1
|
|
|
96 |
#define I2CPE_PARSE_OK 0
|
|
|
97 |
|
|
|
98 |
#define LOOP_PE(VAR) LOOP_L_N(VAR, I2CPE_ENCODER_CNT)
|
|
|
99 |
#define CHECK_IDX() do{ if (!WITHIN(idx, 0, I2CPE_ENCODER_CNT - 1)) return; }while(0)
|
|
|
100 |
|
|
|
101 |
typedef union {
|
|
|
102 |
volatile int32_t val = 0;
|
|
|
103 |
uint8_t bval[4];
|
|
|
104 |
} i2cLong;
|
|
|
105 |
|
|
|
106 |
class I2CPositionEncoder {
|
|
|
107 |
private:
|
|
|
108 |
AxisEnum encoderAxis = I2CPE_DEF_AXIS;
|
|
|
109 |
|
|
|
110 |
uint8_t i2cAddress = I2CPE_DEF_ADDR,
|
|
|
111 |
ecMethod = I2CPE_DEF_EC_METHOD,
|
|
|
112 |
type = I2CPE_DEF_TYPE,
|
|
|
113 |
H = I2CPE_MAG_SIG_NF; // Magnetic field strength
|
|
|
114 |
|
|
|
115 |
int encoderTicksPerUnit = I2CPE_DEF_ENC_TICKS_UNIT,
|
|
|
116 |
stepperTicks = I2CPE_DEF_TICKS_REV,
|
|
|
117 |
errorCount = 0,
|
|
|
118 |
errorPrev = 0;
|
|
|
119 |
|
|
|
120 |
float ecThreshold = I2CPE_DEF_EC_THRESH;
|
|
|
121 |
|
|
|
122 |
bool homed = false,
|
|
|
123 |
trusted = false,
|
|
|
124 |
initialised = false,
|
|
|
125 |
active = false,
|
|
|
126 |
invert = false,
|
|
|
127 |
ec = true;
|
|
|
128 |
|
|
|
129 |
int32_t zeroOffset = 0,
|
|
|
130 |
lastPosition = 0,
|
|
|
131 |
position;
|
|
|
132 |
|
|
|
133 |
millis_t lastPositionTime = 0,
|
|
|
134 |
nextErrorCountTime = 0,
|
|
|
135 |
lastErrorTime;
|
|
|
136 |
|
|
|
137 |
#if ENABLED(I2CPE_ERR_ROLLING_AVERAGE)
|
|
|
138 |
uint8_t errIdx = 0, errPrstIdx = 0;
|
|
|
139 |
int err[I2CPE_ERR_ARRAY_SIZE] = { 0 },
|
|
|
140 |
errPrst[I2CPE_ERR_PRST_ARRAY_SIZE] = { 0 };
|
|
|
141 |
#endif
|
|
|
142 |
|
|
|
143 |
public:
|
|
|
144 |
void init(const uint8_t address, const AxisEnum axis);
|
|
|
145 |
void reset();
|
|
|
146 |
|
|
|
147 |
void update();
|
|
|
148 |
|
|
|
149 |
void set_homed();
|
|
|
150 |
|
|
|
151 |
int32_t get_raw_count();
|
|
|
152 |
|
|
|
153 |
FORCE_INLINE float mm_from_count(const int32_t count) {
|
|
|
154 |
switch (type) {
|
|
|
155 |
default: return -1;
|
|
|
156 |
case I2CPE_ENC_TYPE_LINEAR:
|
|
|
157 |
return count / encoderTicksPerUnit;
|
|
|
158 |
case I2CPE_ENC_TYPE_ROTARY:
|
|
|
159 |
return (count * stepperTicks) / (encoderTicksPerUnit * planner.axis_steps_per_mm[encoderAxis]);
|
|
|
160 |
}
|
|
|
161 |
}
|
|
|
162 |
|
|
|
163 |
FORCE_INLINE float get_position_mm() { return mm_from_count(get_position()); }
|
|
|
164 |
FORCE_INLINE int32_t get_position() { return get_raw_count() - zeroOffset; }
|
|
|
165 |
|
|
|
166 |
int32_t get_axis_error_steps(const bool report);
|
|
|
167 |
float get_axis_error_mm(const bool report);
|
|
|
168 |
|
|
|
169 |
void calibrate_steps_mm(const uint8_t iter);
|
|
|
170 |
|
|
|
171 |
bool passes_test(const bool report);
|
|
|
172 |
|
|
|
173 |
bool test_axis(void);
|
|
|
174 |
|
|
|
175 |
FORCE_INLINE int get_error_count(void) { return errorCount; }
|
|
|
176 |
FORCE_INLINE void set_error_count(const int newCount) { errorCount = newCount; }
|
|
|
177 |
|
|
|
178 |
FORCE_INLINE uint8_t get_address() { return i2cAddress; }
|
|
|
179 |
FORCE_INLINE void set_address(const uint8_t addr) { i2cAddress = addr; }
|
|
|
180 |
|
|
|
181 |
FORCE_INLINE bool get_active(void) { return active; }
|
|
|
182 |
FORCE_INLINE void set_active(const bool a) { active = a; }
|
|
|
183 |
|
|
|
184 |
FORCE_INLINE void set_inverted(const bool i) { invert = i; }
|
|
|
185 |
|
|
|
186 |
FORCE_INLINE AxisEnum get_axis() { return encoderAxis; }
|
|
|
187 |
|
|
|
188 |
FORCE_INLINE bool get_ec_enabled() { return ec; }
|
|
|
189 |
FORCE_INLINE void set_ec_enabled(const bool enabled) { ec = enabled; }
|
|
|
190 |
|
|
|
191 |
FORCE_INLINE uint8_t get_ec_method() { return ecMethod; }
|
|
|
192 |
FORCE_INLINE void set_ec_method(const byte method) { ecMethod = method; }
|
|
|
193 |
|
|
|
194 |
FORCE_INLINE float get_ec_threshold() { return ecThreshold; }
|
|
|
195 |
FORCE_INLINE void set_ec_threshold(const float newThreshold) { ecThreshold = newThreshold; }
|
|
|
196 |
|
|
|
197 |
FORCE_INLINE int get_encoder_ticks_mm() {
|
|
|
198 |
switch (type) {
|
|
|
199 |
default: return 0;
|
|
|
200 |
case I2CPE_ENC_TYPE_LINEAR:
|
|
|
201 |
return encoderTicksPerUnit;
|
|
|
202 |
case I2CPE_ENC_TYPE_ROTARY:
|
|
|
203 |
return (int)((encoderTicksPerUnit / stepperTicks) * planner.axis_steps_per_mm[encoderAxis]);
|
|
|
204 |
}
|
|
|
205 |
}
|
|
|
206 |
|
|
|
207 |
FORCE_INLINE int get_ticks_unit() { return encoderTicksPerUnit; }
|
|
|
208 |
FORCE_INLINE void set_ticks_unit(const int ticks) { encoderTicksPerUnit = ticks; }
|
|
|
209 |
|
|
|
210 |
FORCE_INLINE uint8_t get_type() { return type; }
|
|
|
211 |
FORCE_INLINE void set_type(const byte newType) { type = newType; }
|
|
|
212 |
|
|
|
213 |
FORCE_INLINE int get_stepper_ticks() { return stepperTicks; }
|
|
|
214 |
FORCE_INLINE void set_stepper_ticks(const int ticks) { stepperTicks = ticks; }
|
|
|
215 |
};
|
|
|
216 |
|
|
|
217 |
class I2CPositionEncodersMgr {
|
|
|
218 |
private:
|
|
|
219 |
static bool I2CPE_anyaxis;
|
|
|
220 |
static uint8_t I2CPE_addr, I2CPE_idx;
|
|
|
221 |
|
|
|
222 |
public:
|
|
|
223 |
|
|
|
224 |
static void init(void);
|
|
|
225 |
|
|
|
226 |
// consider only updating one endoder per call / tick if encoders become too time intensive
|
|
|
227 |
static void update(void) { LOOP_PE(i) encoders[i].update(); }
|
|
|
228 |
|
|
|
229 |
static void homed(const AxisEnum axis) {
|
|
|
230 |
LOOP_PE(i)
|
|
|
231 |
if (encoders[i].get_axis() == axis) encoders[i].set_homed();
|
|
|
232 |
}
|
|
|
233 |
|
|
|
234 |
static void report_position(const int8_t idx, const bool units, const bool noOffset);
|
|
|
235 |
|
|
|
236 |
static void report_status(const int8_t idx) {
|
|
|
237 |
CHECK_IDX();
|
|
|
238 |
SERIAL_ECHOPAIR("Encoder ",idx);
|
|
|
239 |
SERIAL_ECHOPGM(": ");
|
|
|
240 |
encoders[idx].get_raw_count();
|
|
|
241 |
encoders[idx].passes_test(true);
|
|
|
242 |
}
|
|
|
243 |
|
|
|
244 |
static void report_error(const int8_t idx) {
|
|
|
245 |
CHECK_IDX();
|
|
|
246 |
encoders[idx].get_axis_error_steps(true);
|
|
|
247 |
}
|
|
|
248 |
|
|
|
249 |
static void test_axis(const int8_t idx) {
|
|
|
250 |
CHECK_IDX();
|
|
|
251 |
encoders[idx].test_axis();
|
|
|
252 |
}
|
|
|
253 |
|
|
|
254 |
static void calibrate_steps_mm(const int8_t idx, const int iterations) {
|
|
|
255 |
CHECK_IDX();
|
|
|
256 |
encoders[idx].calibrate_steps_mm(iterations);
|
|
|
257 |
}
|
|
|
258 |
|
|
|
259 |
static void change_module_address(const uint8_t oldaddr, const uint8_t newaddr);
|
|
|
260 |
static void report_module_firmware(const uint8_t address);
|
|
|
261 |
|
|
|
262 |
static void report_error_count(const int8_t idx, const AxisEnum axis) {
|
|
|
263 |
CHECK_IDX();
|
|
|
264 |
SERIAL_ECHOPAIR("Error count on ", axis_codes[axis]);
|
|
|
265 |
SERIAL_ECHOLNPAIR(" axis is ", encoders[idx].get_error_count());
|
|
|
266 |
}
|
|
|
267 |
|
|
|
268 |
static void reset_error_count(const int8_t idx, const AxisEnum axis) {
|
|
|
269 |
CHECK_IDX();
|
|
|
270 |
encoders[idx].set_error_count(0);
|
|
|
271 |
SERIAL_ECHOPAIR("Error count on ", axis_codes[axis]);
|
|
|
272 |
SERIAL_ECHOLNPGM(" axis has been reset.");
|
|
|
273 |
}
|
|
|
274 |
|
|
|
275 |
static void enable_ec(const int8_t idx, const bool enabled, const AxisEnum axis) {
|
|
|
276 |
CHECK_IDX();
|
|
|
277 |
encoders[idx].set_ec_enabled(enabled);
|
|
|
278 |
SERIAL_ECHOPAIR("Error correction on ", axis_codes[axis]);
|
|
|
279 |
SERIAL_ECHOPGM(" axis is ");
|
|
|
280 |
serialprintPGM(encoders[idx].get_ec_enabled() ? PSTR("en") : PSTR("dis"));
|
|
|
281 |
SERIAL_ECHOLNPGM("abled.");
|
|
|
282 |
}
|
|
|
283 |
|
|
|
284 |
static void set_ec_threshold(const int8_t idx, const float newThreshold, const AxisEnum axis) {
|
|
|
285 |
CHECK_IDX();
|
|
|
286 |
encoders[idx].set_ec_threshold(newThreshold);
|
|
|
287 |
SERIAL_ECHOPAIR("Error correct threshold for ", axis_codes[axis]);
|
|
|
288 |
SERIAL_ECHOPAIR_F(" axis set to ", newThreshold);
|
|
|
289 |
SERIAL_ECHOLNPGM("mm.");
|
|
|
290 |
}
|
|
|
291 |
|
|
|
292 |
static void get_ec_threshold(const int8_t idx, const AxisEnum axis) {
|
|
|
293 |
CHECK_IDX();
|
|
|
294 |
const float threshold = encoders[idx].get_ec_threshold();
|
|
|
295 |
SERIAL_ECHOPAIR("Error correct threshold for ", axis_codes[axis]);
|
|
|
296 |
SERIAL_ECHOPAIR_F(" axis is ", threshold);
|
|
|
297 |
SERIAL_ECHOLNPGM("mm.");
|
|
|
298 |
}
|
|
|
299 |
|
|
|
300 |
static int8_t idx_from_axis(const AxisEnum axis) {
|
|
|
301 |
LOOP_PE(i)
|
|
|
302 |
if (encoders[i].get_axis() == axis) return i;
|
|
|
303 |
return -1;
|
|
|
304 |
}
|
|
|
305 |
|
|
|
306 |
static int8_t idx_from_addr(const uint8_t addr) {
|
|
|
307 |
LOOP_PE(i)
|
|
|
308 |
if (encoders[i].get_address() == addr) return i;
|
|
|
309 |
return -1;
|
|
|
310 |
}
|
|
|
311 |
|
|
|
312 |
static int8_t parse();
|
|
|
313 |
|
|
|
314 |
static void M860();
|
|
|
315 |
static void M861();
|
|
|
316 |
static void M862();
|
|
|
317 |
static void M863();
|
|
|
318 |
static void M864();
|
|
|
319 |
static void M865();
|
|
|
320 |
static void M866();
|
|
|
321 |
static void M867();
|
|
|
322 |
static void M868();
|
|
|
323 |
static void M869();
|
|
|
324 |
|
|
|
325 |
static I2CPositionEncoder encoders[I2CPE_ENCODER_CNT];
|
|
|
326 |
};
|
|
|
327 |
|
|
|
328 |
extern I2CPositionEncodersMgr I2CPEM;
|
|
|
329 |
|
|
|
330 |
FORCE_INLINE static void gcode_M860() { I2CPEM.M860(); }
|
|
|
331 |
FORCE_INLINE static void gcode_M861() { I2CPEM.M861(); }
|
|
|
332 |
FORCE_INLINE static void gcode_M862() { I2CPEM.M862(); }
|
|
|
333 |
FORCE_INLINE static void gcode_M863() { I2CPEM.M863(); }
|
|
|
334 |
FORCE_INLINE static void gcode_M864() { I2CPEM.M864(); }
|
|
|
335 |
FORCE_INLINE static void gcode_M865() { I2CPEM.M865(); }
|
|
|
336 |
FORCE_INLINE static void gcode_M866() { I2CPEM.M866(); }
|
|
|
337 |
FORCE_INLINE static void gcode_M867() { I2CPEM.M867(); }
|
|
|
338 |
FORCE_INLINE static void gcode_M868() { I2CPEM.M868(); }
|
|
|
339 |
FORCE_INLINE static void gcode_M869() { I2CPEM.M869(); }
|
|
|
340 |
|
|
|
341 |
#endif //I2C_POSITION_ENCODERS
|
|
|
342 |
#endif //I2CPOSENC_H
|