My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

I2CPositionEncoder.cpp 36KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132
  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. //todo: add support for multiple encoders on a single axis
  23. //todo: add z axis auto-leveling
  24. //todo: consolidate some of the related M codes?
  25. //todo: add endstop-replacement mode?
  26. //todo: try faster I2C speed; tweak TWI_FREQ (400000L, or faster?); or just TWBR = ((CPU_FREQ / 400000L) - 16) / 2;
  27. //todo: consider Marlin-optimized Wire library; i.e. MarlinWire, like MarlinSerial
  28. #include "MarlinConfig.h"
  29. #if ENABLED(I2C_POSITION_ENCODERS)
  30. #include "Marlin.h"
  31. #include "temperature.h"
  32. #include "stepper.h"
  33. #include "I2CPositionEncoder.h"
  34. #include "gcode.h"
  35. #include <Wire.h>
  36. void I2CPositionEncoder::init(const uint8_t address, const AxisEnum axis) {
  37. encoderAxis = axis;
  38. i2cAddress = address;
  39. initialised++;
  40. SERIAL_ECHOPAIR("Setting up encoder on ", axis_codes[encoderAxis]);
  41. SERIAL_ECHOLNPAIR(" axis, addr = ", address);
  42. position = get_position();
  43. }
  44. void I2CPositionEncoder::update() {
  45. if (!initialised || !homed || !active) return; //check encoder is set up and active
  46. position = get_position();
  47. //we don't want to stop things just because the encoder missed a message,
  48. //so we only care about responses that indicate bad magnetic strength
  49. if (!passes_test(false)) { //check encoder data is good
  50. lastErrorTime = millis();
  51. /*
  52. if (trusted) { //commented out as part of the note below
  53. trusted = false;
  54. SERIAL_ECHOPGM("Fault detected on ");
  55. SERIAL_ECHO(axis_codes[encoderAxis]);
  56. SERIAL_ECHOLNPGM(" axis encoder. Disengaging error correction until module is trusted again.");
  57. }
  58. */
  59. return;
  60. }
  61. if (!trusted) {
  62. /**
  63. * This is commented out because it introduces error and can cause bad print quality.
  64. *
  65. * This code is intended to manage situations where the encoder has reported bad magnetic strength.
  66. * This indicates that the magnetic strip was too far away from the sensor to reliably track position.
  67. * When this happens, this code resets the offset based on where the printer thinks it is. This has been
  68. * shown to introduce errors in actual position which result in drifting prints and poor print quality.
  69. * Perhaps a better method would be to disable correction on the axis with a problem, report it to the
  70. * user via the status leds on the encoder module and prompt the user to re-home the axis at which point
  71. * the encoder would be re-enabled.
  72. */
  73. /*
  74. // If the magnetic strength has been good for a certain time, start trusting the module again
  75. if (millis() - lastErrorTime > I2CPE_TIME_TRUSTED) {
  76. trusted = true;
  77. SERIAL_ECHOPGM("Untrusted encoder module on ");
  78. SERIAL_ECHO(axis_codes[encoderAxis]);
  79. SERIAL_ECHOLNPGM(" axis has been fault-free for set duration, reinstating error correction.");
  80. //the encoder likely lost its place when the error occured, so we'll reset and use the printer's
  81. //idea of where it the axis is to re-initialise
  82. float position = stepper.get_axis_position_mm(encoderAxis);
  83. int32_t positionInTicks = position * get_ticks_unit();
  84. //shift position from previous to current position
  85. zeroOffset -= (positionInTicks - get_position());
  86. #ifdef I2CPE_DEBUG
  87. SERIAL_ECHOPGM("Current position is ");
  88. SERIAL_ECHOLN(position);
  89. SERIAL_ECHOPGM("Position in encoder ticks is ");
  90. SERIAL_ECHOLN(positionInTicks);
  91. SERIAL_ECHOPGM("New zero-offset of ");
  92. SERIAL_ECHOLN(zeroOffset);
  93. SERIAL_ECHOPGM("New position reads as ");
  94. SERIAL_ECHO(get_position());
  95. SERIAL_ECHOPGM("(");
  96. SERIAL_ECHO(mm_from_count(get_position()));
  97. SERIAL_ECHOLNPGM(")");
  98. #endif
  99. }
  100. */
  101. return;
  102. }
  103. lastPosition = position;
  104. const millis_t positionTime = millis();
  105. //only do error correction if setup and enabled
  106. if (ec && ecMethod != I2CPE_ECM_NONE) {
  107. #ifdef I2CPE_EC_THRESH_PROPORTIONAL
  108. const millis_t deltaTime = positionTime - lastPositionTime;
  109. const uint32_t distance = abs(position - lastPosition),
  110. speed = distance / deltaTime;
  111. const float threshold = constrain((speed / 50), 1, 50) * ecThreshold;
  112. #else
  113. const float threshold = get_error_correct_threshold();
  114. #endif
  115. //check error
  116. #if ENABLED(I2CPE_ERR_ROLLING_AVERAGE)
  117. float sum = 0, diffSum = 0;
  118. errIdx = (errIdx >= I2CPE_ERR_ARRAY_SIZE - 1) ? 0 : errIdx + 1;
  119. err[errIdx] = get_axis_error_steps(false);
  120. LOOP_L_N(i, I2CPE_ERR_ARRAY_SIZE) {
  121. sum += err[i];
  122. if (i) diffSum += abs(err[i-1] - err[i]);
  123. }
  124. const int32_t error = int32_t(sum / (I2CPE_ERR_ARRAY_SIZE + 1)); //calculate average for error
  125. #else
  126. const int32_t error = get_axis_error_steps(false);
  127. #endif
  128. //SERIAL_ECHOPGM("Axis error steps: ");
  129. //SERIAL_ECHOLN(error);
  130. #ifdef I2CPE_ERR_THRESH_ABORT
  131. if (labs(error) > I2CPE_ERR_THRESH_ABORT * planner.axis_steps_per_mm[encoderAxis]) {
  132. //kill("Significant Error");
  133. SERIAL_ECHOPGM("Axis error greater than set threshold, aborting!");
  134. SERIAL_ECHOLN(error);
  135. safe_delay(5000);
  136. }
  137. #endif
  138. #if ENABLED(I2CPE_ERR_ROLLING_AVERAGE)
  139. if (errIdx == 0) {
  140. // in order to correct for "error" but avoid correcting for noise and non skips
  141. // it must be > threshold and have a difference average of < 10 and be < 2000 steps
  142. if (labs(error) > threshold * planner.axis_steps_per_mm[encoderAxis] &&
  143. diffSum < 10 * (I2CPE_ERR_ARRAY_SIZE - 1) && labs(error) < 2000) { //Check for persistent error (skip)
  144. SERIAL_ECHO(axis_codes[encoderAxis]);
  145. SERIAL_ECHOPAIR(" diffSum: ", diffSum / (I2CPE_ERR_ARRAY_SIZE - 1));
  146. SERIAL_ECHOPAIR(" - err detected: ", error / planner.axis_steps_per_mm[encoderAxis]);
  147. SERIAL_ECHOLNPGM("mm; correcting!");
  148. thermalManager.babystepsTodo[encoderAxis] = -LROUND(error);
  149. }
  150. }
  151. #else
  152. if (labs(error) > threshold * planner.axis_steps_per_mm[encoderAxis]) {
  153. //SERIAL_ECHOLN(error);
  154. //SERIAL_ECHOLN(position);
  155. thermalManager.babystepsTodo[encoderAxis] = -LROUND(error/2);
  156. }
  157. #endif
  158. if (labs(error) > I2CPE_ERR_CNT_THRESH * planner.axis_steps_per_mm[encoderAxis]) {
  159. const millis_t ms = millis();
  160. if (ELAPSED(ms, nextErrorCountTime)) {
  161. SERIAL_ECHOPAIR("Large error on ", axis_codes[encoderAxis]);
  162. SERIAL_ECHOPAIR(" axis. error: ", (int)error);
  163. SERIAL_ECHOLNPAIR("; diffSum: ", diffSum);
  164. errorCount++;
  165. nextErrorCountTime = ms + I2CPE_ERR_CNT_DEBOUNCE_MS;
  166. }
  167. }
  168. }
  169. lastPositionTime = positionTime;
  170. }
  171. void I2CPositionEncoder::set_homed() {
  172. if (active) {
  173. reset(); // Reset module's offset to zero (so current position is homed / zero)
  174. delay(10);
  175. zeroOffset = get_raw_count();
  176. homed++;
  177. trusted++;
  178. #ifdef I2CPE_DEBUG
  179. SERIAL_ECHO(axis_codes[encoderAxis]);
  180. SERIAL_ECHOPAIR(" axis encoder homed, offset of ", zeroOffset);
  181. SERIAL_ECHOLNPGM(" ticks.");
  182. #endif
  183. }
  184. }
  185. bool I2CPositionEncoder::passes_test(const bool report) {
  186. if (report) {
  187. if (H != I2CPE_MAG_SIG_GOOD) SERIAL_ECHOPGM("Warning. ");
  188. SERIAL_ECHO(axis_codes[encoderAxis]);
  189. SERIAL_ECHOPGM(" axis ");
  190. serialprintPGM(H == I2CPE_MAG_SIG_BAD ? PSTR("magnetic strip ") : PSTR("encoder "));
  191. switch (H) {
  192. case I2CPE_MAG_SIG_GOOD:
  193. case I2CPE_MAG_SIG_MID:
  194. SERIAL_ECHOLNPGM("passes test; field strength ");
  195. serialprintPGM(H == I2CPE_MAG_SIG_GOOD ? PSTR("good.\n") : PSTR("fair.\n"));
  196. break;
  197. default:
  198. SERIAL_ECHOLNPGM("not detected!");
  199. }
  200. }
  201. return (H == I2CPE_MAG_SIG_GOOD || H == I2CPE_MAG_SIG_MID);
  202. }
  203. float I2CPositionEncoder::get_axis_error_mm(const bool report) {
  204. float target, actual, error;
  205. target = stepper.get_axis_position_mm(encoderAxis);
  206. actual = mm_from_count(position);
  207. error = actual - target;
  208. if (labs(error) > 10000) error = 0; // ?
  209. if (report) {
  210. SERIAL_ECHO(axis_codes[encoderAxis]);
  211. SERIAL_ECHOPAIR(" axis target: ", target);
  212. SERIAL_ECHOPAIR(", actual: ", actual);
  213. SERIAL_ECHOLNPAIR(", error : ",error);
  214. }
  215. return error;
  216. }
  217. int32_t I2CPositionEncoder::get_axis_error_steps(const bool report) {
  218. if (!active) {
  219. if (report) {
  220. SERIAL_ECHO(axis_codes[encoderAxis]);
  221. SERIAL_ECHOLNPGM(" axis encoder not active!");
  222. }
  223. return 0;
  224. }
  225. float stepperTicksPerUnit;
  226. int32_t encoderTicks = position, encoderCountInStepperTicksScaled;
  227. //int32_t stepperTicks = stepper.position(encoderAxis);
  228. // With a rotary encoder we're concerned with ticks/rev; whereas with a linear we're concerned with ticks/mm
  229. stepperTicksPerUnit = (type == I2CPE_ENC_TYPE_ROTARY) ? stepperTicks : planner.axis_steps_per_mm[encoderAxis];
  230. //convert both 'ticks' into same units / base
  231. encoderCountInStepperTicksScaled = LROUND((stepperTicksPerUnit * encoderTicks) / encoderTicksPerUnit);
  232. int32_t target = stepper.position(encoderAxis),
  233. error = (encoderCountInStepperTicksScaled - target);
  234. //suppress discontinuities (might be caused by bad I2C readings...?)
  235. bool suppressOutput = (labs(error - errorPrev) > 100);
  236. if (report) {
  237. SERIAL_ECHO(axis_codes[encoderAxis]);
  238. SERIAL_ECHOPAIR(" axis target: ", target);
  239. SERIAL_ECHOPAIR(", actual: ", encoderCountInStepperTicksScaled);
  240. SERIAL_ECHOLNPAIR(", error : ", error);
  241. if (suppressOutput) SERIAL_ECHOLNPGM("Discontinuity detected, suppressing error.");
  242. }
  243. errorPrev = error;
  244. return (suppressOutput ? 0 : error);
  245. }
  246. int32_t I2CPositionEncoder::get_raw_count() {
  247. uint8_t index = 0;
  248. i2cLong encoderCount;
  249. encoderCount.val = 0x00;
  250. if (Wire.requestFrom((int)i2cAddress, 3) != 3) {
  251. //houston, we have a problem...
  252. H = I2CPE_MAG_SIG_NF;
  253. return 0;
  254. }
  255. while (Wire.available())
  256. encoderCount.bval[index++] = (uint8_t)Wire.read();
  257. //extract the magnetic strength
  258. H = (B00000011 & (encoderCount.bval[2] >> 6));
  259. //extract sign bit; sign = (encoderCount.bval[2] & B00100000);
  260. //set all upper bits to the sign value to overwrite H
  261. encoderCount.val = (encoderCount.bval[2] & B00100000) ? (encoderCount.val | 0xFFC00000) : (encoderCount.val & 0x003FFFFF);
  262. if (invert) encoderCount.val *= -1;
  263. return encoderCount.val;
  264. }
  265. bool I2CPositionEncoder::test_axis() {
  266. //only works on XYZ cartesian machines for the time being
  267. if (!(encoderAxis == X_AXIS || encoderAxis == Y_AXIS || encoderAxis == Z_AXIS)) return false;
  268. float startCoord[NUM_AXIS] = { 0 }, endCoord[NUM_AXIS] = { 0 };
  269. const float startPosition = soft_endstop_min[encoderAxis] + 10,
  270. endPosition = soft_endstop_max[encoderAxis] - 10,
  271. feedrate = FLOOR(MMM_TO_MMS((encoderAxis == Z_AXIS) ? HOMING_FEEDRATE_Z : HOMING_FEEDRATE_XY));
  272. ec = false;
  273. LOOP_NA(i) {
  274. startCoord[i] = stepper.get_axis_position_mm((AxisEnum)i);
  275. endCoord[i] = stepper.get_axis_position_mm((AxisEnum)i);
  276. }
  277. startCoord[encoderAxis] = startPosition;
  278. endCoord[encoderAxis] = endPosition;
  279. stepper.synchronize();
  280. planner.buffer_line(startCoord[X_AXIS],startCoord[Y_AXIS],startCoord[Z_AXIS],
  281. stepper.get_axis_position_mm(E_AXIS), feedrate, 0);
  282. stepper.synchronize();
  283. // if the module isn't currently trusted, wait until it is (or until it should be if things are working)
  284. if (!trusted) {
  285. int32_t startWaitingTime = millis();
  286. while (!trusted && millis() - startWaitingTime < I2CPE_TIME_TRUSTED)
  287. safe_delay(500);
  288. }
  289. if (trusted) { // if trusted, commence test
  290. planner.buffer_line(endCoord[X_AXIS], endCoord[Y_AXIS], endCoord[Z_AXIS],
  291. stepper.get_axis_position_mm(E_AXIS), feedrate, 0);
  292. stepper.synchronize();
  293. }
  294. return trusted;
  295. }
  296. void I2CPositionEncoder::calibrate_steps_mm(const uint8_t iter) {
  297. if (type != I2CPE_ENC_TYPE_LINEAR) {
  298. SERIAL_ECHOLNPGM("Steps per mm calibration is only available using linear encoders.");
  299. return;
  300. }
  301. if (!(encoderAxis == X_AXIS || encoderAxis == Y_AXIS || encoderAxis == Z_AXIS)) {
  302. SERIAL_ECHOLNPGM("Automatic steps / mm calibration not supported for this axis.");
  303. return;
  304. }
  305. float old_steps_mm, new_steps_mm,
  306. startDistance, endDistance,
  307. travelDistance, travelledDistance, total = 0,
  308. startCoord[NUM_AXIS] = { 0 }, endCoord[NUM_AXIS] = { 0 };
  309. float feedrate;
  310. int32_t startCount, stopCount;
  311. feedrate = MMM_TO_MMS((encoderAxis == Z_AXIS) ? HOMING_FEEDRATE_Z : HOMING_FEEDRATE_XY);
  312. bool oldec = ec;
  313. ec = false;
  314. startDistance = 20;
  315. endDistance = soft_endstop_max[encoderAxis] - 20;
  316. travelDistance = endDistance - startDistance;
  317. LOOP_NA(i) {
  318. startCoord[i] = stepper.get_axis_position_mm((AxisEnum)i);
  319. endCoord[i] = stepper.get_axis_position_mm((AxisEnum)i);
  320. }
  321. startCoord[encoderAxis] = startDistance;
  322. endCoord[encoderAxis] = endDistance;
  323. LOOP_L_N(i, iter) {
  324. stepper.synchronize();
  325. planner.buffer_line(startCoord[X_AXIS],startCoord[Y_AXIS],startCoord[Z_AXIS],
  326. stepper.get_axis_position_mm(E_AXIS), feedrate, 0);
  327. stepper.synchronize();
  328. delay(250);
  329. startCount = get_position();
  330. //do_blocking_move_to(endCoord[X_AXIS],endCoord[Y_AXIS],endCoord[Z_AXIS]);
  331. planner.buffer_line(endCoord[X_AXIS],endCoord[Y_AXIS],endCoord[Z_AXIS],
  332. stepper.get_axis_position_mm(E_AXIS), feedrate, 0);
  333. stepper.synchronize();
  334. //Read encoder distance
  335. delay(250);
  336. stopCount = get_position();
  337. travelledDistance = mm_from_count(abs(stopCount - startCount));
  338. SERIAL_ECHOPAIR("Attempted to travel: ", travelDistance);
  339. SERIAL_ECHOLNPGM("mm.");
  340. SERIAL_ECHOPAIR("Actually travelled: ", travelledDistance);
  341. SERIAL_ECHOLNPGM("mm.");
  342. //Calculate new axis steps per unit
  343. old_steps_mm = planner.axis_steps_per_mm[encoderAxis];
  344. new_steps_mm = (old_steps_mm * travelDistance) / travelledDistance;
  345. SERIAL_ECHOLNPAIR("Old steps per mm: ", old_steps_mm);
  346. SERIAL_ECHOLNPAIR("New steps per mm: ", new_steps_mm);
  347. //Save new value
  348. planner.axis_steps_per_mm[encoderAxis] = new_steps_mm;
  349. if (iter > 1) {
  350. total += new_steps_mm;
  351. // swap start and end points so next loop runs from current position
  352. float tempCoord = startCoord[encoderAxis];
  353. startCoord[encoderAxis] = endCoord[encoderAxis];
  354. endCoord[encoderAxis] = tempCoord;
  355. }
  356. }
  357. if (iter > 1) {
  358. total /= (float)iter;
  359. SERIAL_ECHOLNPAIR("Average steps per mm: ", total);
  360. }
  361. ec = oldec;
  362. SERIAL_ECHOLNPGM("Calculated steps per mm has been set. Please save to EEPROM (M500) if you wish to keep these values.");
  363. }
  364. void I2CPositionEncoder::reset() {
  365. Wire.beginTransmission(i2cAddress);
  366. Wire.write(I2CPE_RESET_COUNT);
  367. Wire.endTransmission();
  368. #if ENABLED(I2CPE_ERR_ROLLING_AVERAGE)
  369. ZERO(err);
  370. #endif
  371. }
  372. bool I2CPositionEncodersMgr::I2CPE_anyaxis;
  373. uint8_t I2CPositionEncodersMgr::I2CPE_addr,
  374. I2CPositionEncodersMgr::I2CPE_idx;
  375. I2CPositionEncoder I2CPositionEncodersMgr::encoders[I2CPE_ENCODER_CNT];
  376. void I2CPositionEncodersMgr::init() {
  377. Wire.begin();
  378. #if I2CPE_ENCODER_CNT > 0
  379. uint8_t i = 0;
  380. encoders[i].init(I2CPE_ENC_1_ADDR, I2CPE_ENC_1_AXIS);
  381. #ifdef I2CPE_ENC_1_TYPE
  382. encoders[i].set_type(I2CPE_ENC_1_TYPE);
  383. #endif
  384. #ifdef I2CPE_ENC_1_TICKS_UNIT
  385. encoders[i].set_ticks_unit(I2CPE_ENC_1_TICKS_UNIT);
  386. #endif
  387. #ifdef I2CPE_ENC_1_TICKS_REV
  388. encoders[i].set_stepper_ticks(I2CPE_ENC_1_TICKS_REV);
  389. #endif
  390. #ifdef I2CPE_ENC_1_INVERT
  391. encoders[i].set_inverted(I2CPE_ENC_1_INVERT);
  392. #endif
  393. #ifdef I2CPE_ENC_1_EC_METHOD
  394. encoders[i].set_ec_method(I2CPE_ENC_1_EC_METHOD);
  395. #endif
  396. #ifdef I2CPE_ENC_1_EC_THRESH
  397. encoders[i].set_ec_threshold(I2CPE_ENC_1_EC_THRESH);
  398. #endif
  399. encoders[i].set_active(encoders[i].passes_test(true));
  400. #if I2CPE_ENC_1_AXIS == E_AXIS
  401. encoders[i].set_homed();
  402. #endif
  403. #endif
  404. #if I2CPE_ENCODER_CNT > 1
  405. i++;
  406. encoders[i].init(I2CPE_ENC_2_ADDR, I2CPE_ENC_2_AXIS);
  407. #ifdef I2CPE_ENC_2_TYPE
  408. encoders[i].set_type(I2CPE_ENC_2_TYPE);
  409. #endif
  410. #ifdef I2CPE_ENC_2_TICKS_UNIT
  411. encoders[i].set_ticks_unit(I2CPE_ENC_2_TICKS_UNIT);
  412. #endif
  413. #ifdef I2CPE_ENC_2_TICKS_REV
  414. encoders[i].set_stepper_ticks(I2CPE_ENC_2_TICKS_REV);
  415. #endif
  416. #ifdef I2CPE_ENC_2_INVERT
  417. encoders[i].set_inverted(I2CPE_ENC_2_INVERT);
  418. #endif
  419. #ifdef I2CPE_ENC_2_EC_METHOD
  420. encoders[i].set_ec_method(I2CPE_ENC_2_EC_METHOD);
  421. #endif
  422. #ifdef I2CPE_ENC_2_EC_THRESH
  423. encoders[i].set_ec_threshold(I2CPE_ENC_2_EC_THRESH);
  424. #endif
  425. encoders[i].set_active(encoders[i].passes_test(true));
  426. #if I2CPE_ENC_2_AXIS == E_AXIS
  427. encoders[i].set_homed();
  428. #endif
  429. #endif
  430. #if I2CPE_ENCODER_CNT > 2
  431. i++;
  432. encoders[i].init(I2CPE_ENC_3_ADDR, I2CPE_ENC_3_AXIS);
  433. #ifdef I2CPE_ENC_3_TYPE
  434. encoders[i].set_type(I2CPE_ENC_3_TYPE);
  435. #endif
  436. #ifdef I2CPE_ENC_3_TICKS_UNIT
  437. encoders[i].set_ticks_unit(I2CPE_ENC_3_TICKS_UNIT);
  438. #endif
  439. #ifdef I2CPE_ENC_3_TICKS_REV
  440. encoders[i].set_stepper_ticks(I2CPE_ENC_3_TICKS_REV);
  441. #endif
  442. #ifdef I2CPE_ENC_3_INVERT
  443. encoders[i].set_inverted(I2CPE_ENC_3_INVERT);
  444. #endif
  445. #ifdef I2CPE_ENC_3_EC_METHOD
  446. encoders[i].set_ec_method(I2CPE_ENC_3_EC_METHOD);
  447. #endif
  448. #ifdef I2CPE_ENC_3_EC_THRESH
  449. encoders[i].set_ec_threshold(I2CPE_ENC_3_EC_THRESH);
  450. #endif
  451. encoders[i].set_active(encoders[i].passes_test(true));
  452. #if I2CPE_ENC_3_AXIS == E_AXIS
  453. encoders[i].set_homed();
  454. #endif
  455. #endif
  456. #if I2CPE_ENCODER_CNT > 3
  457. i++;
  458. encoders[i].init(I2CPE_ENC_4_ADDR, I2CPE_ENC_4_AXIS);
  459. #ifdef I2CPE_ENC_4_TYPE
  460. encoders[i].set_type(I2CPE_ENC_4_TYPE);
  461. #endif
  462. #ifdef I2CPE_ENC_4_TICKS_UNIT
  463. encoders[i].set_ticks_unit(I2CPE_ENC_4_TICKS_UNIT);
  464. #endif
  465. #ifdef I2CPE_ENC_4_TICKS_REV
  466. encoders[i].set_stepper_ticks(I2CPE_ENC_4_TICKS_REV);
  467. #endif
  468. #ifdef I2CPE_ENC_4_INVERT
  469. encoders[i].set_inverted(I2CPE_ENC_4_INVERT);
  470. #endif
  471. #ifdef I2CPE_ENC_4_EC_METHOD
  472. encoders[i].set_ec_method(I2CPE_ENC_4_EC_METHOD);
  473. #endif
  474. #ifdef I2CPE_ENC_4_EC_THRESH
  475. encoders[i].set_ec_threshold(I2CPE_ENC_4_EC_THRESH);
  476. #endif
  477. encoders[i].set_active(encoders[i].passes_test(true));
  478. #if I2CPE_ENC_4_AXIS == E_AXIS
  479. encoders[i].set_homed();
  480. #endif
  481. #endif
  482. #if I2CPE_ENCODER_CNT > 4
  483. i++;
  484. encoders[i].init(I2CPE_ENC_5_ADDR, I2CPE_ENC_5_AXIS);
  485. #ifdef I2CPE_ENC_5_TYPE
  486. encoders[i].set_type(I2CPE_ENC_5_TYPE);
  487. #endif
  488. #ifdef I2CPE_ENC_5_TICKS_UNIT
  489. encoders[i].set_ticks_unit(I2CPE_ENC_5_TICKS_UNIT);
  490. #endif
  491. #ifdef I2CPE_ENC_5_TICKS_REV
  492. encoders[i].set_stepper_ticks(I2CPE_ENC_5_TICKS_REV);
  493. #endif
  494. #ifdef I2CPE_ENC_5_INVERT
  495. encoders[i].set_inverted(I2CPE_ENC_5_INVERT);
  496. #endif
  497. #ifdef I2CPE_ENC_5_EC_METHOD
  498. encoders[i].set_ec_method(I2CPE_ENC_5_EC_METHOD);
  499. #endif
  500. #ifdef I2CPE_ENC_5_EC_THRESH
  501. encoders[i].set_ec_threshold(I2CPE_ENC_5_EC_THRESH);
  502. #endif
  503. encoders[i].set_active(encoders[i].passes_test(true));
  504. #if I2CPE_ENC_5_AXIS == E_AXIS
  505. encoders[i].set_homed();
  506. #endif
  507. #endif
  508. }
  509. void I2CPositionEncodersMgr::report_position(const int8_t idx, const bool units, const bool noOffset) {
  510. CHECK_IDX();
  511. if (units)
  512. SERIAL_ECHOLN(noOffset ? encoders[idx].mm_from_count(encoders[idx].get_raw_count()) : encoders[idx].get_position_mm());
  513. else {
  514. if (noOffset) {
  515. const int32_t raw_count = encoders[idx].get_raw_count();
  516. SERIAL_ECHO(axis_codes[encoders[idx].get_axis()]);
  517. SERIAL_CHAR(' ');
  518. for (uint8_t j = 31; j > 0; j--)
  519. SERIAL_ECHO((bool)(0x00000001 & (raw_count >> j)));
  520. SERIAL_ECHO((bool)(0x00000001 & raw_count));
  521. SERIAL_CHAR(' ');
  522. SERIAL_ECHOLN(raw_count);
  523. }
  524. else
  525. SERIAL_ECHOLN(encoders[idx].get_position());
  526. }
  527. }
  528. void I2CPositionEncodersMgr::change_module_address(const uint8_t oldaddr, const uint8_t newaddr) {
  529. // First check 'new' address is not in use
  530. Wire.beginTransmission(newaddr);
  531. if (!Wire.endTransmission()) {
  532. SERIAL_ECHOPAIR("?There is already a device with that address on the I2C bus! (", newaddr);
  533. SERIAL_ECHOLNPGM(")");
  534. return;
  535. }
  536. // Now check that we can find the module on the oldaddr address
  537. Wire.beginTransmission(oldaddr);
  538. if (Wire.endTransmission()) {
  539. SERIAL_ECHOPAIR("?No module detected at this address! (", oldaddr);
  540. SERIAL_ECHOLNPGM(")");
  541. return;
  542. }
  543. SERIAL_ECHOPAIR("Module found at ", oldaddr);
  544. SERIAL_ECHOLNPAIR(", changing address to ", newaddr);
  545. // Change the modules address
  546. Wire.beginTransmission(oldaddr);
  547. Wire.write(I2CPE_SET_ADDR);
  548. Wire.write(newaddr);
  549. Wire.endTransmission();
  550. SERIAL_ECHOLNPGM("Address changed, resetting and waiting for confirmation..");
  551. // Wait for the module to reset (can probably be improved by polling address with a timeout).
  552. safe_delay(I2CPE_REBOOT_TIME);
  553. // Look for the module at the new address.
  554. Wire.beginTransmission(newaddr);
  555. if (Wire.endTransmission()) {
  556. SERIAL_ECHOLNPGM("Address change failed! Check encoder module.");
  557. return;
  558. }
  559. SERIAL_ECHOLNPGM("Address change successful!");
  560. // Now, if this module is configured, find which encoder instance it's supposed to correspond to
  561. // and enable it (it will likely have failed initialisation on power-up, before the address change).
  562. const int8_t idx = idx_from_addr(newaddr);
  563. if (idx >= 0 && !encoders[idx].get_active()) {
  564. SERIAL_ECHO(axis_codes[encoders[idx].get_axis()]);
  565. SERIAL_ECHOLNPGM(" axis encoder was not detected on printer startup. Trying again.");
  566. encoders[idx].set_active(encoders[idx].passes_test(true));
  567. }
  568. }
  569. void I2CPositionEncodersMgr::report_module_firmware(const uint8_t address) {
  570. // First check there is a module
  571. Wire.beginTransmission(address);
  572. if (Wire.endTransmission()) {
  573. SERIAL_ECHOPAIR("?No module detected at this address! (", address);
  574. SERIAL_ECHOLNPGM(")");
  575. return;
  576. }
  577. SERIAL_ECHOPAIR("Requesting version info from module at address ", address);
  578. SERIAL_ECHOLNPGM(":");
  579. Wire.beginTransmission(address);
  580. Wire.write(I2CPE_SET_REPORT_MODE);
  581. Wire.write(I2CPE_REPORT_VERSION);
  582. Wire.endTransmission();
  583. // Read value
  584. if (Wire.requestFrom((int)address, 32)) {
  585. char c;
  586. while (Wire.available() > 0 && (c = (char)Wire.read()) > 0)
  587. SERIAL_ECHO(c);
  588. SERIAL_EOL();
  589. }
  590. // Set module back to normal (distance) mode
  591. Wire.beginTransmission(address);
  592. Wire.write(I2CPE_SET_REPORT_MODE);
  593. Wire.write(I2CPE_REPORT_DISTANCE);
  594. Wire.endTransmission();
  595. }
  596. int8_t I2CPositionEncodersMgr::parse() {
  597. I2CPE_addr = 0;
  598. if (parser.seen('A')) {
  599. if (!parser.has_value()) {
  600. SERIAL_PROTOCOLLNPGM("?A seen, but no address specified! [30-200]");
  601. return I2CPE_PARSE_ERR;
  602. };
  603. I2CPE_addr = parser.value_byte();
  604. if (!WITHIN(I2CPE_addr, 30, 200)) { // reserve the first 30 and last 55
  605. SERIAL_PROTOCOLLNPGM("?Address out of range. [30-200]");
  606. return I2CPE_PARSE_ERR;
  607. }
  608. I2CPE_idx = idx_from_addr(I2CPE_addr);
  609. if (I2CPE_idx >= I2CPE_ENCODER_CNT) {
  610. SERIAL_PROTOCOLLNPGM("?No device with this address!");
  611. return I2CPE_PARSE_ERR;
  612. }
  613. }
  614. else if (parser.seenval('I')) {
  615. if (!parser.has_value()) {
  616. SERIAL_PROTOCOLLNPAIR("?I seen, but no index specified! [0-", I2CPE_ENCODER_CNT - 1);
  617. SERIAL_PROTOCOLLNPGM("]");
  618. return I2CPE_PARSE_ERR;
  619. };
  620. I2CPE_idx = parser.value_byte();
  621. if (I2CPE_idx >= I2CPE_ENCODER_CNT) {
  622. SERIAL_PROTOCOLLNPAIR("?Index out of range. [0-", I2CPE_ENCODER_CNT - 1);
  623. SERIAL_ECHOLNPGM("]");
  624. return I2CPE_PARSE_ERR;
  625. }
  626. I2CPE_addr = encoders[I2CPE_idx].get_address();
  627. }
  628. else
  629. I2CPE_idx = 0xFF;
  630. I2CPE_anyaxis = parser.seen_axis();
  631. return I2CPE_PARSE_OK;
  632. };
  633. /**
  634. * M860: Report the position(s) of position encoder module(s).
  635. *
  636. * A<addr> Module I2C address. [30, 200].
  637. * I<index> Module index. [0, I2CPE_ENCODER_CNT - 1]
  638. * O Include homed zero-offset in returned position.
  639. * U Units in mm or raw step count.
  640. *
  641. * If A or I not specified:
  642. * X Report on X axis encoder, if present.
  643. * Y Report on Y axis encoder, if present.
  644. * Z Report on Z axis encoder, if present.
  645. * E Report on E axis encoder, if present.
  646. *
  647. */
  648. void I2CPositionEncodersMgr::M860() {
  649. if (parse()) return;
  650. const bool hasU = parser.seen('U'), hasO = parser.seen('O');
  651. if (I2CPE_idx == 0xFF) {
  652. LOOP_XYZE(i) {
  653. if (!I2CPE_anyaxis || parser.seen(axis_codes[i])) {
  654. const uint8_t idx = idx_from_axis(AxisEnum(i));
  655. if ((int8_t)idx >= 0) report_position(idx, hasU, hasO);
  656. }
  657. }
  658. }
  659. else
  660. report_position(I2CPE_idx, hasU, hasO);
  661. }
  662. /**
  663. * M861: Report the status of position encoder modules.
  664. *
  665. * A<addr> Module I2C address. [30, 200].
  666. * I<index> Module index. [0, I2CPE_ENCODER_CNT - 1]
  667. *
  668. * If A or I not specified:
  669. * X Report on X axis encoder, if present.
  670. * Y Report on Y axis encoder, if present.
  671. * Z Report on Z axis encoder, if present.
  672. * E Report on E axis encoder, if present.
  673. *
  674. */
  675. void I2CPositionEncodersMgr::M861() {
  676. if (parse()) return;
  677. if (I2CPE_idx == 0xFF) {
  678. LOOP_XYZE(i) {
  679. if (!I2CPE_anyaxis || parser.seen(axis_codes[i])) {
  680. const uint8_t idx = idx_from_axis(AxisEnum(i));
  681. if ((int8_t)idx >= 0) report_status(idx);
  682. }
  683. }
  684. }
  685. else
  686. report_status(I2CPE_idx);
  687. }
  688. /**
  689. * M862: Perform an axis continuity test for position encoder
  690. * modules.
  691. *
  692. * A<addr> Module I2C address. [30, 200].
  693. * I<index> Module index. [0, I2CPE_ENCODER_CNT - 1]
  694. *
  695. * If A or I not specified:
  696. * X Report on X axis encoder, if present.
  697. * Y Report on Y axis encoder, if present.
  698. * Z Report on Z axis encoder, if present.
  699. * E Report on E axis encoder, if present.
  700. *
  701. */
  702. void I2CPositionEncodersMgr::M862() {
  703. if (parse()) return;
  704. if (I2CPE_idx == 0xFF) {
  705. LOOP_XYZE(i) {
  706. if (!I2CPE_anyaxis || parser.seen(axis_codes[i])) {
  707. const uint8_t idx = idx_from_axis(AxisEnum(i));
  708. if ((int8_t)idx >= 0) test_axis(idx);
  709. }
  710. }
  711. }
  712. else
  713. test_axis(I2CPE_idx);
  714. }
  715. /**
  716. * M863: Perform steps-per-mm calibration for
  717. * position encoder modules.
  718. *
  719. * A<addr> Module I2C address. [30, 200].
  720. * I<index> Module index. [0, I2CPE_ENCODER_CNT - 1]
  721. * P Number of rePeats/iterations.
  722. *
  723. * If A or I not specified:
  724. * X Report on X axis encoder, if present.
  725. * Y Report on Y axis encoder, if present.
  726. * Z Report on Z axis encoder, if present.
  727. * E Report on E axis encoder, if present.
  728. *
  729. */
  730. void I2CPositionEncodersMgr::M863() {
  731. if (parse()) return;
  732. const uint8_t iterations = constrain(parser.byteval('P', 1), 1, 10);
  733. if (I2CPE_idx == 0xFF) {
  734. LOOP_XYZE(i) {
  735. if (!I2CPE_anyaxis || parser.seen(axis_codes[i])) {
  736. const uint8_t idx = idx_from_axis(AxisEnum(i));
  737. if ((int8_t)idx >= 0) calibrate_steps_mm(idx, iterations);
  738. }
  739. }
  740. }
  741. else
  742. calibrate_steps_mm(I2CPE_idx, iterations);
  743. }
  744. /**
  745. * M864: Change position encoder module I2C address.
  746. *
  747. * A<addr> Module current/old I2C address. If not present,
  748. * assumes default address (030). [30, 200].
  749. * S<addr> Module new I2C address. [30, 200].
  750. *
  751. * If S is not specified:
  752. * X Use I2CPE_PRESET_ADDR_X (030).
  753. * Y Use I2CPE_PRESET_ADDR_Y (031).
  754. * Z Use I2CPE_PRESET_ADDR_Z (032).
  755. * E Use I2CPE_PRESET_ADDR_E (033).
  756. */
  757. void I2CPositionEncodersMgr::M864() {
  758. uint8_t newAddress;
  759. if (parse()) return;
  760. if (!I2CPE_addr) I2CPE_addr = I2CPE_PRESET_ADDR_X;
  761. if (parser.seen('S')) {
  762. if (!parser.has_value()) {
  763. SERIAL_PROTOCOLLNPGM("?S seen, but no address specified! [30-200]");
  764. return;
  765. };
  766. newAddress = parser.value_byte();
  767. if (!WITHIN(newAddress, 30, 200)) {
  768. SERIAL_PROTOCOLLNPGM("?New address out of range. [30-200]");
  769. return;
  770. }
  771. }
  772. else if (!I2CPE_anyaxis) {
  773. SERIAL_PROTOCOLLNPGM("?You must specify S or [XYZE].");
  774. return;
  775. }
  776. else {
  777. if (parser.seen('X')) newAddress = I2CPE_PRESET_ADDR_X;
  778. else if (parser.seen('Y')) newAddress = I2CPE_PRESET_ADDR_Y;
  779. else if (parser.seen('Z')) newAddress = I2CPE_PRESET_ADDR_Z;
  780. else if (parser.seen('E')) newAddress = I2CPE_PRESET_ADDR_E;
  781. else return;
  782. }
  783. SERIAL_ECHOPAIR("Changing module at address ", I2CPE_addr);
  784. SERIAL_ECHOLNPAIR(" to address ", newAddress);
  785. change_module_address(I2CPE_addr, newAddress);
  786. }
  787. /**
  788. * M865: Check position encoder module firmware version.
  789. *
  790. * A<addr> Module I2C address. [30, 200].
  791. * I<index> Module index. [0, I2CPE_ENCODER_CNT - 1].
  792. *
  793. * If A or I not specified:
  794. * X Check X axis encoder, if present.
  795. * Y Check Y axis encoder, if present.
  796. * Z Check Z axis encoder, if present.
  797. * E Check E axis encoder, if present.
  798. */
  799. void I2CPositionEncodersMgr::M865() {
  800. if (parse()) return;
  801. if (!I2CPE_addr) {
  802. LOOP_XYZE(i) {
  803. if (!I2CPE_anyaxis || parser.seen(axis_codes[i])) {
  804. const uint8_t idx = idx_from_axis(AxisEnum(i));
  805. if ((int8_t)idx >= 0) report_module_firmware(encoders[idx].get_address());
  806. }
  807. }
  808. }
  809. else
  810. report_module_firmware(I2CPE_addr);
  811. }
  812. /**
  813. * M866: Report or reset position encoder module error
  814. * count.
  815. *
  816. * A<addr> Module I2C address. [30, 200].
  817. * I<index> Module index. [0, I2CPE_ENCODER_CNT - 1].
  818. * R Reset error counter.
  819. *
  820. * If A or I not specified:
  821. * X Act on X axis encoder, if present.
  822. * Y Act on Y axis encoder, if present.
  823. * Z Act on Z axis encoder, if present.
  824. * E Act on E axis encoder, if present.
  825. */
  826. void I2CPositionEncodersMgr::M866() {
  827. if (parse()) return;
  828. const bool hasR = parser.seen('R');
  829. if (I2CPE_idx == 0xFF) {
  830. LOOP_XYZE(i) {
  831. if (!I2CPE_anyaxis || parser.seen(axis_codes[i])) {
  832. const uint8_t idx = idx_from_axis(AxisEnum(i));
  833. if ((int8_t)idx >= 0) {
  834. if (hasR)
  835. reset_error_count(idx, AxisEnum(i));
  836. else
  837. report_error_count(idx, AxisEnum(i));
  838. }
  839. }
  840. }
  841. }
  842. else if (hasR)
  843. reset_error_count(I2CPE_idx, encoders[I2CPE_idx].get_axis());
  844. else
  845. report_error_count(I2CPE_idx, encoders[I2CPE_idx].get_axis());
  846. }
  847. /**
  848. * M867: Enable/disable or toggle error correction for position encoder modules.
  849. *
  850. * A<addr> Module I2C address. [30, 200].
  851. * I<index> Module index. [0, I2CPE_ENCODER_CNT - 1].
  852. * S<1|0> Enable/disable error correction. 1 enables, 0 disables. If not
  853. * supplied, toggle.
  854. *
  855. * If A or I not specified:
  856. * X Act on X axis encoder, if present.
  857. * Y Act on Y axis encoder, if present.
  858. * Z Act on Z axis encoder, if present.
  859. * E Act on E axis encoder, if present.
  860. */
  861. void I2CPositionEncodersMgr::M867() {
  862. if (parse()) return;
  863. const int8_t onoff = parser.seenval('S') ? parser.value_int() : -1;
  864. if (I2CPE_idx == 0xFF) {
  865. LOOP_XYZE(i) {
  866. if (!I2CPE_anyaxis || parser.seen(axis_codes[i])) {
  867. const uint8_t idx = idx_from_axis(AxisEnum(i));
  868. if ((int8_t)idx >= 0) {
  869. const bool ena = onoff == -1 ? !encoders[I2CPE_idx].get_ec_enabled() : !!onoff;
  870. enable_ec(idx, ena, AxisEnum(i));
  871. }
  872. }
  873. }
  874. }
  875. else {
  876. const bool ena = onoff == -1 ? !encoders[I2CPE_idx].get_ec_enabled() : !!onoff;
  877. enable_ec(I2CPE_idx, ena, encoders[I2CPE_idx].get_axis());
  878. }
  879. }
  880. /**
  881. * M868: Report or set position encoder module error correction
  882. * threshold.
  883. *
  884. * A<addr> Module I2C address. [30, 200].
  885. * I<index> Module index. [0, I2CPE_ENCODER_CNT - 1].
  886. * T New error correction threshold.
  887. *
  888. * If A not specified:
  889. * X Act on X axis encoder, if present.
  890. * Y Act on Y axis encoder, if present.
  891. * Z Act on Z axis encoder, if present.
  892. * E Act on E axis encoder, if present.
  893. */
  894. void I2CPositionEncodersMgr::M868() {
  895. if (parse()) return;
  896. const float newThreshold = parser.seenval('T') ? parser.value_float() : -9999;
  897. if (I2CPE_idx == 0xFF) {
  898. LOOP_XYZE(i) {
  899. if (!I2CPE_anyaxis || parser.seen(axis_codes[i])) {
  900. const uint8_t idx = idx_from_axis(AxisEnum(i));
  901. if ((int8_t)idx >= 0) {
  902. if (newThreshold != -9999)
  903. set_ec_threshold(idx, newThreshold, encoders[idx].get_axis());
  904. else
  905. get_ec_threshold(idx, encoders[idx].get_axis());
  906. }
  907. }
  908. }
  909. }
  910. else if (newThreshold != -9999)
  911. set_ec_threshold(I2CPE_idx, newThreshold, encoders[I2CPE_idx].get_axis());
  912. else
  913. get_ec_threshold(I2CPE_idx, encoders[I2CPE_idx].get_axis());
  914. }
  915. /**
  916. * M869: Report position encoder module error.
  917. *
  918. * A<addr> Module I2C address. [30, 200].
  919. * I<index> Module index. [0, I2CPE_ENCODER_CNT - 1].
  920. *
  921. * If A not specified:
  922. * X Act on X axis encoder, if present.
  923. * Y Act on Y axis encoder, if present.
  924. * Z Act on Z axis encoder, if present.
  925. * E Act on E axis encoder, if present.
  926. */
  927. void I2CPositionEncodersMgr::M869() {
  928. if (parse()) return;
  929. if (I2CPE_idx == 0xFF) {
  930. LOOP_XYZE(i) {
  931. if (!I2CPE_anyaxis || parser.seen(axis_codes[i])) {
  932. const uint8_t idx = idx_from_axis(AxisEnum(i));
  933. if ((int8_t)idx >= 0) report_error(idx);
  934. }
  935. }
  936. }
  937. else
  938. report_error(I2CPE_idx);
  939. }
  940. #endif // I2C_POSITION_ENCODERS