2017-06-09 07:06:23 -05:00
|
|
|
/**
|
|
|
|
* Marlin 3D Printer Firmware
|
2019-02-12 15:06:53 -06:00
|
|
|
* Copyright (C) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
2017-06-09 07:06:23 -05:00
|
|
|
*
|
|
|
|
* Based on Sprinter and grbl.
|
|
|
|
* Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
2018-11-04 02:25:55 -06:00
|
|
|
#pragma once
|
2017-06-09 07:06:23 -05:00
|
|
|
|
2017-09-06 06:28:32 -05:00
|
|
|
#include "../inc/MarlinConfig.h"
|
2017-06-09 07:06:23 -05:00
|
|
|
|
2017-09-06 06:28:32 -05:00
|
|
|
#include "../module/planner.h"
|
2017-06-09 07:06:23 -05:00
|
|
|
|
2017-09-06 06:28:32 -05:00
|
|
|
#include <Wire.h>
|
2017-06-09 07:06:23 -05:00
|
|
|
|
2017-09-06 06:28:32 -05:00
|
|
|
//=========== Advanced / Less-Common Encoder Configuration Settings ==========
|
2017-06-09 07:06:23 -05:00
|
|
|
|
2017-09-06 06:28:32 -05:00
|
|
|
#define I2CPE_EC_THRESH_PROPORTIONAL // if enabled adjusts the error correction threshold
|
|
|
|
// proportional to the current speed of the axis allows
|
|
|
|
// for very small error margin at low speeds without
|
|
|
|
// stuttering due to reading latency at high speeds
|
2017-06-09 07:06:23 -05:00
|
|
|
|
2017-09-06 06:28:32 -05:00
|
|
|
#define I2CPE_DEBUG // enable encoder-related debug serial echos
|
2017-06-09 07:06:23 -05:00
|
|
|
|
2017-09-06 06:28:32 -05:00
|
|
|
#define I2CPE_REBOOT_TIME 5000 // time we wait for an encoder module to reboot
|
|
|
|
// after changing address.
|
2017-06-09 07:06:23 -05:00
|
|
|
|
2017-09-06 06:28:32 -05:00
|
|
|
#define I2CPE_MAG_SIG_GOOD 0
|
|
|
|
#define I2CPE_MAG_SIG_MID 1
|
|
|
|
#define I2CPE_MAG_SIG_BAD 2
|
|
|
|
#define I2CPE_MAG_SIG_NF 255
|
2017-06-09 07:06:23 -05:00
|
|
|
|
2017-09-06 06:28:32 -05:00
|
|
|
#define I2CPE_REQ_REPORT 0
|
|
|
|
#define I2CPE_RESET_COUNT 1
|
|
|
|
#define I2CPE_SET_ADDR 2
|
|
|
|
#define I2CPE_SET_REPORT_MODE 3
|
|
|
|
#define I2CPE_CLEAR_EEPROM 4
|
2017-06-09 07:06:23 -05:00
|
|
|
|
2017-09-06 06:28:32 -05:00
|
|
|
#define I2CPE_LED_PAR_MODE 10
|
|
|
|
#define I2CPE_LED_PAR_BRT 11
|
|
|
|
#define I2CPE_LED_PAR_RATE 14
|
2017-06-09 07:06:23 -05:00
|
|
|
|
2017-09-06 06:28:32 -05:00
|
|
|
#define I2CPE_REPORT_DISTANCE 0
|
|
|
|
#define I2CPE_REPORT_STRENGTH 1
|
|
|
|
#define I2CPE_REPORT_VERSION 2
|
2017-06-09 07:06:23 -05:00
|
|
|
|
2017-09-06 06:28:32 -05:00
|
|
|
// Default I2C addresses
|
|
|
|
#define I2CPE_PRESET_ADDR_X 30
|
|
|
|
#define I2CPE_PRESET_ADDR_Y 31
|
|
|
|
#define I2CPE_PRESET_ADDR_Z 32
|
|
|
|
#define I2CPE_PRESET_ADDR_E 33
|
2017-06-09 07:06:23 -05:00
|
|
|
|
2017-09-06 06:28:32 -05:00
|
|
|
#define I2CPE_DEF_AXIS X_AXIS
|
|
|
|
#define I2CPE_DEF_ADDR I2CPE_PRESET_ADDR_X
|
2017-06-09 07:06:23 -05:00
|
|
|
|
2017-09-06 06:28:32 -05:00
|
|
|
// Error event counter; tracks how many times there is an error exceeding a certain threshold
|
|
|
|
#define I2CPE_ERR_CNT_THRESH 3.00
|
|
|
|
#define I2CPE_ERR_CNT_DEBOUNCE_MS 2000
|
2017-06-09 07:06:23 -05:00
|
|
|
|
2017-09-06 06:28:32 -05:00
|
|
|
#if ENABLED(I2CPE_ERR_ROLLING_AVERAGE)
|
|
|
|
#define I2CPE_ERR_ARRAY_SIZE 32
|
2018-02-08 03:57:11 -06:00
|
|
|
#define I2CPE_ERR_PRST_ARRAY_SIZE 10
|
2017-09-06 06:28:32 -05:00
|
|
|
#endif
|
2017-06-09 07:06:23 -05:00
|
|
|
|
2017-09-06 06:28:32 -05:00
|
|
|
// Error Correction Methods
|
|
|
|
#define I2CPE_ECM_NONE 0
|
|
|
|
#define I2CPE_ECM_MICROSTEP 1
|
|
|
|
#define I2CPE_ECM_PLANNER 2
|
|
|
|
#define I2CPE_ECM_STALLDETECT 3
|
2017-06-09 07:06:23 -05:00
|
|
|
|
2017-09-06 06:28:32 -05:00
|
|
|
// Encoder types
|
|
|
|
#define I2CPE_ENC_TYPE_ROTARY 0
|
|
|
|
#define I2CPE_ENC_TYPE_LINEAR 1
|
2017-06-09 07:06:23 -05:00
|
|
|
|
2017-09-06 06:28:32 -05:00
|
|
|
// Parser
|
|
|
|
#define I2CPE_PARSE_ERR 1
|
|
|
|
#define I2CPE_PARSE_OK 0
|
2017-06-09 07:06:23 -05:00
|
|
|
|
2017-09-06 06:28:32 -05:00
|
|
|
#define LOOP_PE(VAR) LOOP_L_N(VAR, I2CPE_ENCODER_CNT)
|
|
|
|
#define CHECK_IDX() do{ if (!WITHIN(idx, 0, I2CPE_ENCODER_CNT - 1)) return; }while(0)
|
2017-06-09 07:06:23 -05:00
|
|
|
|
2017-09-06 06:28:32 -05:00
|
|
|
extern const char axis_codes[XYZE];
|
2017-06-09 07:06:23 -05:00
|
|
|
|
2017-09-06 06:28:32 -05:00
|
|
|
typedef union {
|
|
|
|
volatile int32_t val = 0;
|
|
|
|
uint8_t bval[4];
|
|
|
|
} i2cLong;
|
2017-06-09 07:06:23 -05:00
|
|
|
|
2017-09-06 06:28:32 -05:00
|
|
|
class I2CPositionEncoder {
|
2017-06-09 07:06:23 -05:00
|
|
|
private:
|
2017-06-09 10:22:49 -05:00
|
|
|
AxisEnum encoderAxis = I2CPE_DEF_AXIS;
|
2017-06-09 07:06:23 -05:00
|
|
|
|
2017-06-09 10:22:49 -05:00
|
|
|
uint8_t i2cAddress = I2CPE_DEF_ADDR,
|
|
|
|
ecMethod = I2CPE_DEF_EC_METHOD,
|
|
|
|
type = I2CPE_DEF_TYPE,
|
|
|
|
H = I2CPE_MAG_SIG_NF; // Magnetic field strength
|
2017-06-09 07:06:23 -05:00
|
|
|
|
2017-06-09 10:22:49 -05:00
|
|
|
int encoderTicksPerUnit = I2CPE_DEF_ENC_TICKS_UNIT,
|
|
|
|
stepperTicks = I2CPE_DEF_TICKS_REV,
|
|
|
|
errorCount = 0,
|
|
|
|
errorPrev = 0;
|
2017-06-09 07:06:23 -05:00
|
|
|
|
2017-06-09 10:22:49 -05:00
|
|
|
float ecThreshold = I2CPE_DEF_EC_THRESH;
|
2017-06-09 07:06:23 -05:00
|
|
|
|
2017-06-09 10:22:49 -05:00
|
|
|
bool homed = false,
|
|
|
|
trusted = false,
|
2018-08-22 17:14:38 -05:00
|
|
|
initialized = false,
|
2017-06-09 10:22:49 -05:00
|
|
|
active = false,
|
|
|
|
invert = false,
|
|
|
|
ec = true;
|
2017-06-09 07:06:23 -05:00
|
|
|
|
2017-11-04 15:26:29 -05:00
|
|
|
int32_t zeroOffset = 0,
|
2017-06-09 10:22:49 -05:00
|
|
|
lastPosition = 0,
|
|
|
|
position;
|
2017-06-09 07:06:23 -05:00
|
|
|
|
2017-06-09 10:22:49 -05:00
|
|
|
millis_t lastPositionTime = 0,
|
|
|
|
nextErrorCountTime = 0,
|
|
|
|
lastErrorTime;
|
2017-06-09 07:06:23 -05:00
|
|
|
|
|
|
|
#if ENABLED(I2CPE_ERR_ROLLING_AVERAGE)
|
2018-02-08 03:57:11 -06:00
|
|
|
uint8_t errIdx = 0, errPrstIdx = 0;
|
|
|
|
int err[I2CPE_ERR_ARRAY_SIZE] = { 0 },
|
|
|
|
errPrst[I2CPE_ERR_PRST_ARRAY_SIZE] = { 0 };
|
2017-06-09 07:06:23 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
public:
|
2017-06-09 10:22:49 -05:00
|
|
|
void init(const uint8_t address, const AxisEnum axis);
|
2017-06-09 07:06:23 -05:00
|
|
|
void reset();
|
|
|
|
|
|
|
|
void update();
|
|
|
|
|
|
|
|
void set_homed();
|
2018-10-29 14:01:36 -05:00
|
|
|
void set_unhomed();
|
2017-06-09 07:06:23 -05:00
|
|
|
|
2017-06-09 10:22:49 -05:00
|
|
|
int32_t get_raw_count();
|
2017-06-09 07:06:23 -05:00
|
|
|
|
2017-06-09 10:22:49 -05:00
|
|
|
FORCE_INLINE float mm_from_count(const int32_t count) {
|
|
|
|
switch (type) {
|
|
|
|
default: return -1;
|
|
|
|
case I2CPE_ENC_TYPE_LINEAR:
|
|
|
|
return count / encoderTicksPerUnit;
|
|
|
|
case I2CPE_ENC_TYPE_ROTARY:
|
2018-10-10 09:45:20 -05:00
|
|
|
return (count * stepperTicks) / (encoderTicksPerUnit * planner.settings.axis_steps_per_mm[encoderAxis]);
|
2017-06-09 10:22:49 -05:00
|
|
|
}
|
2017-06-09 07:06:23 -05:00
|
|
|
}
|
|
|
|
|
2017-06-09 10:22:49 -05:00
|
|
|
FORCE_INLINE float get_position_mm() { return mm_from_count(get_position()); }
|
2017-11-04 15:26:29 -05:00
|
|
|
FORCE_INLINE int32_t get_position() { return get_raw_count() - zeroOffset; }
|
2017-06-09 07:06:23 -05:00
|
|
|
|
2017-06-09 10:22:49 -05:00
|
|
|
int32_t get_axis_error_steps(const bool report);
|
|
|
|
float get_axis_error_mm(const bool report);
|
2017-06-09 07:06:23 -05:00
|
|
|
|
2017-06-09 10:22:49 -05:00
|
|
|
void calibrate_steps_mm(const uint8_t iter);
|
2017-06-09 07:06:23 -05:00
|
|
|
|
2017-06-09 10:22:49 -05:00
|
|
|
bool passes_test(const bool report);
|
2017-06-09 07:06:23 -05:00
|
|
|
|
|
|
|
bool test_axis(void);
|
|
|
|
|
|
|
|
FORCE_INLINE int get_error_count(void) { return errorCount; }
|
2017-06-09 10:22:49 -05:00
|
|
|
FORCE_INLINE void set_error_count(const int newCount) { errorCount = newCount; }
|
2017-06-09 07:06:23 -05:00
|
|
|
|
|
|
|
FORCE_INLINE uint8_t get_address() { return i2cAddress; }
|
2017-06-09 10:22:49 -05:00
|
|
|
FORCE_INLINE void set_address(const uint8_t addr) { i2cAddress = addr; }
|
2017-06-09 07:06:23 -05:00
|
|
|
|
|
|
|
FORCE_INLINE bool get_active(void) { return active; }
|
2017-06-09 10:22:49 -05:00
|
|
|
FORCE_INLINE void set_active(const bool a) { active = a; }
|
2017-06-09 07:06:23 -05:00
|
|
|
|
2017-06-09 10:22:49 -05:00
|
|
|
FORCE_INLINE void set_inverted(const bool i) { invert = i; }
|
2017-06-09 07:06:23 -05:00
|
|
|
|
|
|
|
FORCE_INLINE AxisEnum get_axis() { return encoderAxis; }
|
|
|
|
|
|
|
|
FORCE_INLINE bool get_ec_enabled() { return ec; }
|
2017-06-09 10:22:49 -05:00
|
|
|
FORCE_INLINE void set_ec_enabled(const bool enabled) { ec = enabled; }
|
2017-06-09 07:06:23 -05:00
|
|
|
|
|
|
|
FORCE_INLINE uint8_t get_ec_method() { return ecMethod; }
|
2017-06-09 10:22:49 -05:00
|
|
|
FORCE_INLINE void set_ec_method(const byte method) { ecMethod = method; }
|
2017-06-09 07:06:23 -05:00
|
|
|
|
|
|
|
FORCE_INLINE float get_ec_threshold() { return ecThreshold; }
|
2017-06-09 10:22:49 -05:00
|
|
|
FORCE_INLINE void set_ec_threshold(const float newThreshold) { ecThreshold = newThreshold; }
|
2017-06-09 07:06:23 -05:00
|
|
|
|
|
|
|
FORCE_INLINE int get_encoder_ticks_mm() {
|
2017-06-09 10:22:49 -05:00
|
|
|
switch (type) {
|
|
|
|
default: return 0;
|
|
|
|
case I2CPE_ENC_TYPE_LINEAR:
|
|
|
|
return encoderTicksPerUnit;
|
|
|
|
case I2CPE_ENC_TYPE_ROTARY:
|
2018-10-10 09:45:20 -05:00
|
|
|
return (int)((encoderTicksPerUnit / stepperTicks) * planner.settings.axis_steps_per_mm[encoderAxis]);
|
2017-06-09 10:22:49 -05:00
|
|
|
}
|
2017-06-09 07:06:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
FORCE_INLINE int get_ticks_unit() { return encoderTicksPerUnit; }
|
2017-06-09 10:22:49 -05:00
|
|
|
FORCE_INLINE void set_ticks_unit(const int ticks) { encoderTicksPerUnit = ticks; }
|
2017-06-09 07:06:23 -05:00
|
|
|
|
|
|
|
FORCE_INLINE uint8_t get_type() { return type; }
|
2017-06-09 10:22:49 -05:00
|
|
|
FORCE_INLINE void set_type(const byte newType) { type = newType; }
|
2017-06-09 07:06:23 -05:00
|
|
|
|
|
|
|
FORCE_INLINE int get_stepper_ticks() { return stepperTicks; }
|
2017-06-09 10:22:49 -05:00
|
|
|
FORCE_INLINE void set_stepper_ticks(const int ticks) { stepperTicks = ticks; }
|
2017-09-06 06:28:32 -05:00
|
|
|
};
|
2017-06-09 07:06:23 -05:00
|
|
|
|
2017-09-06 06:28:32 -05:00
|
|
|
class I2CPositionEncodersMgr {
|
2017-06-09 07:06:23 -05:00
|
|
|
private:
|
2017-06-09 10:22:49 -05:00
|
|
|
static bool I2CPE_anyaxis;
|
|
|
|
static uint8_t I2CPE_addr, I2CPE_idx;
|
2017-06-09 07:06:23 -05:00
|
|
|
|
|
|
|
public:
|
2017-06-09 10:22:49 -05:00
|
|
|
|
|
|
|
static void init(void);
|
2017-06-09 07:06:23 -05:00
|
|
|
|
|
|
|
// consider only updating one endoder per call / tick if encoders become too time intensive
|
2017-06-09 10:22:49 -05:00
|
|
|
static void update(void) { LOOP_PE(i) encoders[i].update(); }
|
2017-06-09 07:06:23 -05:00
|
|
|
|
2017-06-09 10:22:49 -05:00
|
|
|
static void homed(const AxisEnum axis) {
|
2017-06-09 07:06:23 -05:00
|
|
|
LOOP_PE(i)
|
|
|
|
if (encoders[i].get_axis() == axis) encoders[i].set_homed();
|
|
|
|
}
|
|
|
|
|
2018-10-29 14:01:36 -05:00
|
|
|
static void unhomed(const AxisEnum axis) {
|
|
|
|
LOOP_PE(i)
|
|
|
|
if (encoders[i].get_axis() == axis) encoders[i].set_unhomed();
|
|
|
|
}
|
|
|
|
|
2017-06-09 10:22:49 -05:00
|
|
|
static void report_position(const int8_t idx, const bool units, const bool noOffset);
|
2017-06-09 07:06:23 -05:00
|
|
|
|
2017-06-09 10:22:49 -05:00
|
|
|
static void report_status(const int8_t idx) {
|
|
|
|
CHECK_IDX();
|
2019-03-05 06:46:19 -06:00
|
|
|
SERIAL_ECHOLNPAIR("Encoder ", idx, ": ");
|
2017-06-09 07:06:23 -05:00
|
|
|
encoders[idx].get_raw_count();
|
|
|
|
encoders[idx].passes_test(true);
|
|
|
|
}
|
|
|
|
|
2017-06-09 10:22:49 -05:00
|
|
|
static void report_error(const int8_t idx) {
|
|
|
|
CHECK_IDX();
|
2017-06-09 07:06:23 -05:00
|
|
|
encoders[idx].get_axis_error_steps(true);
|
|
|
|
}
|
|
|
|
|
2017-06-09 10:22:49 -05:00
|
|
|
static void test_axis(const int8_t idx) {
|
|
|
|
CHECK_IDX();
|
2017-06-09 07:06:23 -05:00
|
|
|
encoders[idx].test_axis();
|
|
|
|
}
|
|
|
|
|
2017-06-09 10:22:49 -05:00
|
|
|
static void calibrate_steps_mm(const int8_t idx, const int iterations) {
|
|
|
|
CHECK_IDX();
|
2017-06-09 07:06:23 -05:00
|
|
|
encoders[idx].calibrate_steps_mm(iterations);
|
|
|
|
}
|
|
|
|
|
2017-06-09 10:22:49 -05:00
|
|
|
static void change_module_address(const uint8_t oldaddr, const uint8_t newaddr);
|
|
|
|
static void report_module_firmware(const uint8_t address);
|
2017-06-09 07:06:23 -05:00
|
|
|
|
2017-06-09 10:22:49 -05:00
|
|
|
static void report_error_count(const int8_t idx, const AxisEnum axis) {
|
|
|
|
CHECK_IDX();
|
2019-03-05 06:46:19 -06:00
|
|
|
SERIAL_ECHOLNPAIR("Error count on ", axis_codes[axis], " axis is ", encoders[idx].get_error_count());
|
2017-06-09 07:06:23 -05:00
|
|
|
}
|
|
|
|
|
2017-06-09 10:22:49 -05:00
|
|
|
static void reset_error_count(const int8_t idx, const AxisEnum axis) {
|
|
|
|
CHECK_IDX();
|
2017-06-09 07:06:23 -05:00
|
|
|
encoders[idx].set_error_count(0);
|
2019-03-05 06:46:19 -06:00
|
|
|
SERIAL_ECHOLNPAIR("Error count on ", axis_codes[axis], " axis has been reset.");
|
2017-06-09 07:06:23 -05:00
|
|
|
}
|
|
|
|
|
2017-06-09 10:22:49 -05:00
|
|
|
static void enable_ec(const int8_t idx, const bool enabled, const AxisEnum axis) {
|
|
|
|
CHECK_IDX();
|
2017-06-09 07:06:23 -05:00
|
|
|
encoders[idx].set_ec_enabled(enabled);
|
2019-03-05 06:46:19 -06:00
|
|
|
SERIAL_ECHOPAIR("Error correction on ", axis_codes[axis], " axis is ");
|
2017-06-09 07:06:23 -05:00
|
|
|
serialprintPGM(encoders[idx].get_ec_enabled() ? PSTR("en") : PSTR("dis"));
|
|
|
|
SERIAL_ECHOLNPGM("abled.");
|
|
|
|
}
|
|
|
|
|
2017-06-09 10:22:49 -05:00
|
|
|
static void set_ec_threshold(const int8_t idx, const float newThreshold, const AxisEnum axis) {
|
|
|
|
CHECK_IDX();
|
2017-06-09 07:06:23 -05:00
|
|
|
encoders[idx].set_ec_threshold(newThreshold);
|
2019-03-05 06:46:19 -06:00
|
|
|
SERIAL_ECHOLNPAIR("Error correct threshold for ", axis_codes[axis], " axis set to ", FIXFLOAT(newThreshold), "mm.");
|
2017-06-09 07:06:23 -05:00
|
|
|
}
|
|
|
|
|
2017-06-09 10:22:49 -05:00
|
|
|
static void get_ec_threshold(const int8_t idx, const AxisEnum axis) {
|
|
|
|
CHECK_IDX();
|
|
|
|
const float threshold = encoders[idx].get_ec_threshold();
|
2019-03-05 06:46:19 -06:00
|
|
|
SERIAL_ECHOLNPAIR("Error correct threshold for ", axis_codes[axis], " axis is ", FIXFLOAT(threshold), "mm.");
|
2017-06-09 07:06:23 -05:00
|
|
|
}
|
|
|
|
|
2017-06-09 10:22:49 -05:00
|
|
|
static int8_t idx_from_axis(const AxisEnum axis) {
|
2017-06-09 07:06:23 -05:00
|
|
|
LOOP_PE(i)
|
|
|
|
if (encoders[i].get_axis() == axis) return i;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-06-09 10:22:49 -05:00
|
|
|
static int8_t idx_from_addr(const uint8_t addr) {
|
2017-06-09 07:06:23 -05:00
|
|
|
LOOP_PE(i)
|
|
|
|
if (encoders[i].get_address() == addr) return i;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-06-09 10:22:49 -05:00
|
|
|
static int8_t parse();
|
2017-06-09 07:06:23 -05:00
|
|
|
|
2017-06-09 10:22:49 -05:00
|
|
|
static void M860();
|
|
|
|
static void M861();
|
|
|
|
static void M862();
|
|
|
|
static void M863();
|
|
|
|
static void M864();
|
|
|
|
static void M865();
|
|
|
|
static void M866();
|
|
|
|
static void M867();
|
|
|
|
static void M868();
|
|
|
|
static void M869();
|
2017-06-09 07:06:23 -05:00
|
|
|
|
2017-06-09 10:22:49 -05:00
|
|
|
static I2CPositionEncoder encoders[I2CPE_ENCODER_CNT];
|
2017-09-06 06:28:32 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
extern I2CPositionEncodersMgr I2CPEM;
|