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.

encoder_i2c.cpp 34KB

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