I2CPositionEncoder.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140
  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 "parser.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 = planner.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_CHAR('(');
  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 (ABS(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 (ABS(error) > threshold * planner.axis_steps_per_mm[encoderAxis] &&
  143. diffSum < 10 * (I2CPE_ERR_ARRAY_SIZE - 1) && ABS(error) < 2000) { // Check for persistent error (skip)
  144. errPrst[errPrstIdx++] = error; // Error must persist for I2CPE_ERR_PRST_ARRAY_SIZE error cycles. This also serves to improve the average accuracy
  145. if (errPrstIdx >= I2CPE_ERR_PRST_ARRAY_SIZE) {
  146. float sumP = 0;
  147. LOOP_L_N(i, I2CPE_ERR_PRST_ARRAY_SIZE) sumP += errPrst[i];
  148. const int32_t errorP = int32_t(sumP * (1.0f / (I2CPE_ERR_PRST_ARRAY_SIZE)));
  149. SERIAL_ECHO(axis_codes[encoderAxis]);
  150. SERIAL_ECHOPAIR(" - err detected: ", errorP * planner.steps_to_mm[encoderAxis]);
  151. SERIAL_ECHOLNPGM("mm; correcting!");
  152. thermalManager.babystepsTodo[encoderAxis] = -LROUND(errorP);
  153. errPrstIdx = 0;
  154. }
  155. }
  156. else
  157. errPrstIdx = 0;
  158. }
  159. #else
  160. if (ABS(error) > threshold * planner.axis_steps_per_mm[encoderAxis]) {
  161. //SERIAL_ECHOLN(error);
  162. //SERIAL_ECHOLN(position);
  163. thermalManager.babystepsTodo[encoderAxis] = -LROUND(error / 2);
  164. }
  165. #endif
  166. if (ABS(error) > I2CPE_ERR_CNT_THRESH * planner.axis_steps_per_mm[encoderAxis]) {
  167. const millis_t ms = millis();
  168. if (ELAPSED(ms, nextErrorCountTime)) {
  169. SERIAL_ECHOPAIR("Large error on ", axis_codes[encoderAxis]);
  170. SERIAL_ECHOPAIR(" axis. error: ", (int)error);
  171. SERIAL_ECHOLNPAIR("; diffSum: ", diffSum);
  172. errorCount++;
  173. nextErrorCountTime = ms + I2CPE_ERR_CNT_DEBOUNCE_MS;
  174. }
  175. }
  176. }
  177. lastPositionTime = positionTime;
  178. }
  179. void I2CPositionEncoder::set_homed() {
  180. if (active) {
  181. reset(); // Reset module's offset to zero (so current position is homed / zero)
  182. delay(10);
  183. zeroOffset = get_raw_count();
  184. homed++;
  185. trusted++;
  186. #ifdef I2CPE_DEBUG
  187. SERIAL_ECHO(axis_codes[encoderAxis]);
  188. SERIAL_ECHOPAIR(" axis encoder homed, offset of ", zeroOffset);
  189. SERIAL_ECHOLNPGM(" ticks.");
  190. #endif
  191. }
  192. }
  193. bool I2CPositionEncoder::passes_test(const bool report) {
  194. if (report) {
  195. if (H != I2CPE_MAG_SIG_GOOD) SERIAL_ECHOPGM("Warning. ");
  196. SERIAL_ECHO(axis_codes[encoderAxis]);
  197. SERIAL_ECHOPGM(" axis ");
  198. serialprintPGM(H == I2CPE_MAG_SIG_BAD ? PSTR("magnetic strip ") : PSTR("encoder "));
  199. switch (H) {
  200. case I2CPE_MAG_SIG_GOOD:
  201. case I2CPE_MAG_SIG_MID:
  202. SERIAL_ECHOLNPGM("passes test; field strength ");
  203. serialprintPGM(H == I2CPE_MAG_SIG_GOOD ? PSTR("good.\n") : PSTR("fair.\n"));
  204. break;
  205. default:
  206. SERIAL_ECHOLNPGM("not detected!");
  207. }
  208. }
  209. return (H == I2CPE_MAG_SIG_GOOD || H == I2CPE_MAG_SIG_MID);
  210. }
  211. float I2CPositionEncoder::get_axis_error_mm(const bool report) {
  212. float target, actual, error;
  213. target = planner.get_axis_position_mm(encoderAxis);
  214. actual = mm_from_count(position);
  215. error = actual - target;
  216. if (ABS(error) > 10000) error = 0; // ?
  217. if (report) {
  218. SERIAL_ECHO(axis_codes[encoderAxis]);
  219. SERIAL_ECHOPAIR(" axis target: ", target);
  220. SERIAL_ECHOPAIR(", actual: ", actual);
  221. SERIAL_ECHOLNPAIR(", error : ",error);
  222. }
  223. return error;
  224. }
  225. int32_t I2CPositionEncoder::get_axis_error_steps(const bool report) {
  226. if (!active) {
  227. if (report) {
  228. SERIAL_ECHO(axis_codes[encoderAxis]);
  229. SERIAL_ECHOLNPGM(" axis encoder not active!");
  230. }
  231. return 0;
  232. }
  233. float stepperTicksPerUnit;
  234. int32_t encoderTicks = position, encoderCountInStepperTicksScaled;
  235. //int32_t stepperTicks = stepper.position(encoderAxis);
  236. // With a rotary encoder we're concerned with ticks/rev; whereas with a linear we're concerned with ticks/mm
  237. stepperTicksPerUnit = (type == I2CPE_ENC_TYPE_ROTARY) ? stepperTicks : planner.axis_steps_per_mm[encoderAxis];
  238. //convert both 'ticks' into same units / base
  239. encoderCountInStepperTicksScaled = LROUND((stepperTicksPerUnit * encoderTicks) / encoderTicksPerUnit);
  240. int32_t target = stepper.position(encoderAxis),
  241. error = (encoderCountInStepperTicksScaled - target);
  242. //suppress discontinuities (might be caused by bad I2C readings...?)
  243. const bool suppressOutput = (ABS(error - errorPrev) > 100);
  244. if (report) {
  245. SERIAL_ECHO(axis_codes[encoderAxis]);
  246. SERIAL_ECHOPAIR(" axis target: ", target);
  247. SERIAL_ECHOPAIR(", actual: ", encoderCountInStepperTicksScaled);
  248. SERIAL_ECHOLNPAIR(", error : ", error);
  249. if (suppressOutput) SERIAL_ECHOLNPGM("Discontinuity detected, suppressing error.");
  250. }
  251. errorPrev = error;
  252. return (suppressOutput ? 0 : error);
  253. }
  254. int32_t I2CPositionEncoder::get_raw_count() {
  255. uint8_t index = 0;
  256. i2cLong encoderCount;
  257. encoderCount.val = 0x00;
  258. if (Wire.requestFrom((int)i2cAddress, 3) != 3) {
  259. //houston, we have a problem...
  260. H = I2CPE_MAG_SIG_NF;
  261. return 0;
  262. }
  263. while (Wire.available())
  264. encoderCount.bval[index++] = (uint8_t)Wire.read();
  265. //extract the magnetic strength
  266. H = (B00000011 & (encoderCount.bval[2] >> 6));
  267. //extract sign bit; sign = (encoderCount.bval[2] & B00100000);
  268. //set all upper bits to the sign value to overwrite H
  269. encoderCount.val = (encoderCount.bval[2] & B00100000) ? (encoderCount.val | 0xFFC00000) : (encoderCount.val & 0x003FFFFF);
  270. if (invert) encoderCount.val *= -1;
  271. return encoderCount.val;
  272. }
  273. bool I2CPositionEncoder::test_axis() {
  274. //only works on XYZ cartesian machines for the time being
  275. if (!(encoderAxis == X_AXIS || encoderAxis == Y_AXIS || encoderAxis == Z_AXIS)) return false;
  276. float startCoord[NUM_AXIS] = { 0 }, endCoord[NUM_AXIS] = { 0 };
  277. const float startPosition = soft_endstop_min[encoderAxis] + 10,
  278. endPosition = soft_endstop_max[encoderAxis] - 10,
  279. feedrate = FLOOR(MMM_TO_MMS((encoderAxis == Z_AXIS) ? HOMING_FEEDRATE_Z : HOMING_FEEDRATE_XY));
  280. ec = false;
  281. LOOP_NA(i) {
  282. startCoord[i] = planner.get_axis_position_mm((AxisEnum)i);
  283. endCoord[i] = planner.get_axis_position_mm((AxisEnum)i);
  284. }
  285. startCoord[encoderAxis] = startPosition;
  286. endCoord[encoderAxis] = endPosition;
  287. planner.synchronize();
  288. planner.buffer_line(startCoord[X_AXIS], startCoord[Y_AXIS], startCoord[Z_AXIS],
  289. planner.get_axis_position_mm(E_AXIS), feedrate, 0);
  290. planner.synchronize();
  291. // if the module isn't currently trusted, wait until it is (or until it should be if things are working)
  292. if (!trusted) {
  293. int32_t startWaitingTime = millis();
  294. while (!trusted && millis() - startWaitingTime < I2CPE_TIME_TRUSTED)
  295. safe_delay(500);
  296. }
  297. if (trusted) { // if trusted, commence test
  298. planner.buffer_line(endCoord[X_AXIS], endCoord[Y_AXIS], endCoord[Z_AXIS],
  299. planner.get_axis_position_mm(E_AXIS), feedrate, 0);
  300. planner.synchronize();
  301. }
  302. return trusted;
  303. }
  304. void I2CPositionEncoder::calibrate_steps_mm(const uint8_t iter) {
  305. if (type != I2CPE_ENC_TYPE_LINEAR) {
  306. SERIAL_ECHOLNPGM("Steps per mm calibration is only available using linear encoders.");
  307. return;
  308. }
  309. if (!(encoderAxis == X_AXIS || encoderAxis == Y_AXIS || encoderAxis == Z_AXIS)) {
  310. SERIAL_ECHOLNPGM("Automatic steps / mm calibration not supported for this axis.");
  311. return;
  312. }
  313. float old_steps_mm, new_steps_mm,
  314. startDistance, endDistance,
  315. travelDistance, travelledDistance, total = 0,
  316. startCoord[NUM_AXIS] = { 0 }, endCoord[NUM_AXIS] = { 0 };
  317. float feedrate;
  318. int32_t startCount, stopCount;
  319. feedrate = MMM_TO_MMS((encoderAxis == Z_AXIS) ? HOMING_FEEDRATE_Z : HOMING_FEEDRATE_XY);
  320. bool oldec = ec;
  321. ec = false;
  322. startDistance = 20;
  323. endDistance = soft_endstop_max[encoderAxis] - 20;
  324. travelDistance = endDistance - startDistance;
  325. LOOP_NA(i) {
  326. startCoord[i] = planner.get_axis_position_mm((AxisEnum)i);
  327. endCoord[i] = planner.get_axis_position_mm((AxisEnum)i);
  328. }
  329. startCoord[encoderAxis] = startDistance;
  330. endCoord[encoderAxis] = endDistance;
  331. planner.synchronize();
  332. LOOP_L_N(i, iter) {
  333. planner.buffer_line(startCoord[X_AXIS], startCoord[Y_AXIS], startCoord[Z_AXIS],
  334. planner.get_axis_position_mm(E_AXIS), feedrate, 0);
  335. planner.synchronize();
  336. delay(250);
  337. startCount = get_position();
  338. //do_blocking_move_to(endCoord[X_AXIS],endCoord[Y_AXIS],endCoord[Z_AXIS]);
  339. planner.buffer_line(endCoord[X_AXIS], endCoord[Y_AXIS], endCoord[Z_AXIS],
  340. planner.get_axis_position_mm(E_AXIS), feedrate, 0);
  341. planner.synchronize();
  342. //Read encoder distance
  343. delay(250);
  344. stopCount = get_position();
  345. travelledDistance = mm_from_count(ABS(stopCount - startCount));
  346. SERIAL_ECHOPAIR("Attempted to travel: ", travelDistance);
  347. SERIAL_ECHOLNPGM("mm.");
  348. SERIAL_ECHOPAIR("Actually travelled: ", travelledDistance);
  349. SERIAL_ECHOLNPGM("mm.");
  350. //Calculate new axis steps per unit
  351. old_steps_mm = planner.axis_steps_per_mm[encoderAxis];
  352. new_steps_mm = (old_steps_mm * travelDistance) / travelledDistance;
  353. SERIAL_ECHOLNPAIR("Old steps per mm: ", old_steps_mm);
  354. SERIAL_ECHOLNPAIR("New steps per mm: ", new_steps_mm);
  355. //Save new value
  356. planner.axis_steps_per_mm[encoderAxis] = new_steps_mm;
  357. if (iter > 1) {
  358. total += new_steps_mm;
  359. // swap start and end points so next loop runs from current position
  360. float tempCoord = startCoord[encoderAxis];
  361. startCoord[encoderAxis] = endCoord[encoderAxis];
  362. endCoord[encoderAxis] = tempCoord;
  363. }
  364. }
  365. if (iter > 1) {
  366. total /= (float)iter;
  367. SERIAL_ECHOLNPAIR("Average steps per mm: ", total);
  368. }
  369. ec = oldec;
  370. SERIAL_ECHOLNPGM("Calculated steps per mm has been set. Please save to EEPROM (M500) if you wish to keep these values.");
  371. }
  372. void I2CPositionEncoder::reset() {
  373. Wire.beginTransmission(i2cAddress);
  374. Wire.write(I2CPE_RESET_COUNT);
  375. Wire.endTransmission();
  376. #if ENABLED(I2CPE_ERR_ROLLING_AVERAGE)
  377. ZERO(err);
  378. #endif
  379. }
  380. bool I2CPositionEncodersMgr::I2CPE_anyaxis;
  381. uint8_t I2CPositionEncodersMgr::I2CPE_addr,
  382. I2CPositionEncodersMgr::I2CPE_idx;
  383. I2CPositionEncoder I2CPositionEncodersMgr::encoders[I2CPE_ENCODER_CNT];
  384. void I2CPositionEncodersMgr::init() {
  385. Wire.begin();
  386. #if I2CPE_ENCODER_CNT > 0
  387. uint8_t i = 0;
  388. encoders[i].init(I2CPE_ENC_1_ADDR, I2CPE_ENC_1_AXIS);
  389. #ifdef I2CPE_ENC_1_TYPE
  390. encoders[i].set_type(I2CPE_ENC_1_TYPE);
  391. #endif
  392. #ifdef I2CPE_ENC_1_TICKS_UNIT
  393. encoders[i].set_ticks_unit(I2CPE_ENC_1_TICKS_UNIT);
  394. #endif
  395. #ifdef I2CPE_ENC_1_TICKS_REV
  396. encoders[i].set_stepper_ticks(I2CPE_ENC_1_TICKS_REV);
  397. #endif
  398. #ifdef I2CPE_ENC_1_INVERT
  399. encoders[i].set_inverted(I2CPE_ENC_1_INVERT);
  400. #endif
  401. #ifdef I2CPE_ENC_1_EC_METHOD
  402. encoders[i].set_ec_method(I2CPE_ENC_1_EC_METHOD);
  403. #endif
  404. #ifdef I2CPE_ENC_1_EC_THRESH
  405. encoders[i].set_ec_threshold(I2CPE_ENC_1_EC_THRESH);
  406. #endif
  407. encoders[i].set_active(encoders[i].passes_test(true));
  408. #if I2CPE_ENC_1_AXIS == E_AXIS
  409. encoders[i].set_homed();
  410. #endif
  411. #endif
  412. #if I2CPE_ENCODER_CNT > 1
  413. i++;
  414. encoders[i].init(I2CPE_ENC_2_ADDR, I2CPE_ENC_2_AXIS);
  415. #ifdef I2CPE_ENC_2_TYPE
  416. encoders[i].set_type(I2CPE_ENC_2_TYPE);
  417. #endif
  418. #ifdef I2CPE_ENC_2_TICKS_UNIT
  419. encoders[i].set_ticks_unit(I2CPE_ENC_2_TICKS_UNIT);
  420. #endif
  421. #ifdef I2CPE_ENC_2_TICKS_REV
  422. encoders[i].set_stepper_ticks(I2CPE_ENC_2_TICKS_REV);
  423. #endif
  424. #ifdef I2CPE_ENC_2_INVERT
  425. encoders[i].set_inverted(I2CPE_ENC_2_INVERT);
  426. #endif
  427. #ifdef I2CPE_ENC_2_EC_METHOD
  428. encoders[i].set_ec_method(I2CPE_ENC_2_EC_METHOD);
  429. #endif
  430. #ifdef I2CPE_ENC_2_EC_THRESH
  431. encoders[i].set_ec_threshold(I2CPE_ENC_2_EC_THRESH);
  432. #endif
  433. encoders[i].set_active(encoders[i].passes_test(true));
  434. #if I2CPE_ENC_2_AXIS == E_AXIS
  435. encoders[i].set_homed();
  436. #endif
  437. #endif
  438. #if I2CPE_ENCODER_CNT > 2
  439. i++;
  440. encoders[i].init(I2CPE_ENC_3_ADDR, I2CPE_ENC_3_AXIS);
  441. #ifdef I2CPE_ENC_3_TYPE
  442. encoders[i].set_type(I2CPE_ENC_3_TYPE);
  443. #endif
  444. #ifdef I2CPE_ENC_3_TICKS_UNIT
  445. encoders[i].set_ticks_unit(I2CPE_ENC_3_TICKS_UNIT);
  446. #endif
  447. #ifdef I2CPE_ENC_3_TICKS_REV
  448. encoders[i].set_stepper_ticks(I2CPE_ENC_3_TICKS_REV);
  449. #endif
  450. #ifdef I2CPE_ENC_3_INVERT
  451. encoders[i].set_inverted(I2CPE_ENC_3_INVERT);
  452. #endif
  453. #ifdef I2CPE_ENC_3_EC_METHOD
  454. encoders[i].set_ec_method(I2CPE_ENC_3_EC_METHOD);
  455. #endif
  456. #ifdef I2CPE_ENC_3_EC_THRESH
  457. encoders[i].set_ec_threshold(I2CPE_ENC_3_EC_THRESH);
  458. #endif
  459. encoders[i].set_active(encoders[i].passes_test(true));
  460. #if I2CPE_ENC_3_AXIS == E_AXIS
  461. encoders[i].set_homed();
  462. #endif
  463. #endif
  464. #if I2CPE_ENCODER_CNT > 3
  465. i++;
  466. encoders[i].init(I2CPE_ENC_4_ADDR, I2CPE_ENC_4_AXIS);
  467. #ifdef I2CPE_ENC_4_TYPE
  468. encoders[i].set_type(I2CPE_ENC_4_TYPE);
  469. #endif
  470. #ifdef I2CPE_ENC_4_TICKS_UNIT
  471. encoders[i].set_ticks_unit(I2CPE_ENC_4_TICKS_UNIT);
  472. #endif
  473. #ifdef I2CPE_ENC_4_TICKS_REV
  474. encoders[i].set_stepper_ticks(I2CPE_ENC_4_TICKS_REV);
  475. #endif
  476. #ifdef I2CPE_ENC_4_INVERT
  477. encoders[i].set_inverted(I2CPE_ENC_4_INVERT);
  478. #endif
  479. #ifdef I2CPE_ENC_4_EC_METHOD
  480. encoders[i].set_ec_method(I2CPE_ENC_4_EC_METHOD);
  481. #endif
  482. #ifdef I2CPE_ENC_4_EC_THRESH
  483. encoders[i].set_ec_threshold(I2CPE_ENC_4_EC_THRESH);
  484. #endif
  485. encoders[i].set_active(encoders[i].passes_test(true));
  486. #if I2CPE_ENC_4_AXIS == E_AXIS
  487. encoders[i].set_homed();
  488. #endif
  489. #endif
  490. #if I2CPE_ENCODER_CNT > 4
  491. i++;
  492. encoders[i].init(I2CPE_ENC_5_ADDR, I2CPE_ENC_5_AXIS);
  493. #ifdef I2CPE_ENC_5_TYPE
  494. encoders[i].set_type(I2CPE_ENC_5_TYPE);
  495. #endif
  496. #ifdef I2CPE_ENC_5_TICKS_UNIT
  497. encoders[i].set_ticks_unit(I2CPE_ENC_5_TICKS_UNIT);
  498. #endif
  499. #ifdef I2CPE_ENC_5_TICKS_REV
  500. encoders[i].set_stepper_ticks(I2CPE_ENC_5_TICKS_REV);
  501. #endif
  502. #ifdef I2CPE_ENC_5_INVERT
  503. encoders[i].set_inverted(I2CPE_ENC_5_INVERT);
  504. #endif
  505. #ifdef I2CPE_ENC_5_EC_METHOD
  506. encoders[i].set_ec_method(I2CPE_ENC_5_EC_METHOD);
  507. #endif
  508. #ifdef I2CPE_ENC_5_EC_THRESH
  509. encoders[i].set_ec_threshold(I2CPE_ENC_5_EC_THRESH);
  510. #endif
  511. encoders[i].set_active(encoders[i].passes_test(true));
  512. #if I2CPE_ENC_5_AXIS == E_AXIS
  513. encoders[i].set_homed();
  514. #endif
  515. #endif
  516. }
  517. void I2CPositionEncodersMgr::report_position(const int8_t idx, const bool units, const bool noOffset) {
  518. CHECK_IDX();
  519. if (units)
  520. SERIAL_ECHOLN(noOffset ? encoders[idx].mm_from_count(encoders[idx].get_raw_count()) : encoders[idx].get_position_mm());
  521. else {
  522. if (noOffset) {
  523. const int32_t raw_count = encoders[idx].get_raw_count();
  524. SERIAL_ECHO(axis_codes[encoders[idx].get_axis()]);
  525. SERIAL_CHAR(' ');
  526. for (uint8_t j = 31; j > 0; j--)
  527. SERIAL_ECHO((bool)(0x00000001 & (raw_count >> j)));
  528. SERIAL_ECHO((bool)(0x00000001 & raw_count));
  529. SERIAL_CHAR(' ');
  530. SERIAL_ECHOLN(raw_count);
  531. }
  532. else
  533. SERIAL_ECHOLN(encoders[idx].get_position());
  534. }
  535. }
  536. void I2CPositionEncodersMgr::change_module_address(const uint8_t oldaddr, const uint8_t newaddr) {
  537. // First check 'new' address is not in use
  538. Wire.beginTransmission(newaddr);
  539. if (!Wire.endTransmission()) {
  540. SERIAL_ECHOPAIR("?There is already a device with that address on the I2C bus! (", newaddr);
  541. SERIAL_ECHOLNPGM(")");
  542. return;
  543. }
  544. // Now check that we can find the module on the oldaddr address
  545. Wire.beginTransmission(oldaddr);
  546. if (Wire.endTransmission()) {
  547. SERIAL_ECHOPAIR("?No module detected at this address! (", oldaddr);
  548. SERIAL_ECHOLNPGM(")");
  549. return;
  550. }
  551. SERIAL_ECHOPAIR("Module found at ", oldaddr);
  552. SERIAL_ECHOLNPAIR(", changing address to ", newaddr);
  553. // Change the modules address
  554. Wire.beginTransmission(oldaddr);
  555. Wire.write(I2CPE_SET_ADDR);
  556. Wire.write(newaddr);
  557. Wire.endTransmission();
  558. SERIAL_ECHOLNPGM("Address changed, resetting and waiting for confirmation..");
  559. // Wait for the module to reset (can probably be improved by polling address with a timeout).
  560. safe_delay(I2CPE_REBOOT_TIME);
  561. // Look for the module at the new address.
  562. Wire.beginTransmission(newaddr);
  563. if (Wire.endTransmission()) {
  564. SERIAL_ECHOLNPGM("Address change failed! Check encoder module.");
  565. return;
  566. }
  567. SERIAL_ECHOLNPGM("Address change successful!");
  568. // Now, if this module is configured, find which encoder instance it's supposed to correspond to
  569. // and enable it (it will likely have failed initialisation on power-up, before the address change).
  570. const int8_t idx = idx_from_addr(newaddr);
  571. if (idx >= 0 && !encoders[idx].get_active()) {
  572. SERIAL_ECHO(axis_codes[encoders[idx].get_axis()]);
  573. SERIAL_ECHOLNPGM(" axis encoder was not detected on printer startup. Trying again.");
  574. encoders[idx].set_active(encoders[idx].passes_test(true));
  575. }
  576. }
  577. void I2CPositionEncodersMgr::report_module_firmware(const uint8_t address) {
  578. // First check there is a module
  579. Wire.beginTransmission(address);
  580. if (Wire.endTransmission()) {
  581. SERIAL_ECHOPAIR("?No module detected at this address! (", address);
  582. SERIAL_ECHOLNPGM(")");
  583. return;
  584. }
  585. SERIAL_ECHOPAIR("Requesting version info from module at address ", address);
  586. SERIAL_ECHOLNPGM(":");
  587. Wire.beginTransmission(address);
  588. Wire.write(I2CPE_SET_REPORT_MODE);
  589. Wire.write(I2CPE_REPORT_VERSION);
  590. Wire.endTransmission();
  591. // Read value
  592. if (Wire.requestFrom((int)address, 32)) {
  593. char c;
  594. while (Wire.available() > 0 && (c = (char)Wire.read()) > 0)
  595. SERIAL_ECHO(c);
  596. SERIAL_EOL();
  597. }
  598. // Set module back to normal (distance) mode
  599. Wire.beginTransmission(address);
  600. Wire.write(I2CPE_SET_REPORT_MODE);
  601. Wire.write(I2CPE_REPORT_DISTANCE);
  602. Wire.endTransmission();
  603. }
  604. int8_t I2CPositionEncodersMgr::parse() {
  605. I2CPE_addr = 0;
  606. if (parser.seen('A')) {
  607. if (!parser.has_value()) {
  608. SERIAL_PROTOCOLLNPGM("?A seen, but no address specified! [30-200]");
  609. return I2CPE_PARSE_ERR;
  610. };
  611. I2CPE_addr = parser.value_byte();
  612. if (!WITHIN(I2CPE_addr, 30, 200)) { // reserve the first 30 and last 55
  613. SERIAL_PROTOCOLLNPGM("?Address out of range. [30-200]");
  614. return I2CPE_PARSE_ERR;
  615. }
  616. I2CPE_idx = idx_from_addr(I2CPE_addr);
  617. if (I2CPE_idx >= I2CPE_ENCODER_CNT) {
  618. SERIAL_PROTOCOLLNPGM("?No device with this address!");
  619. return I2CPE_PARSE_ERR;
  620. }
  621. }
  622. else if (parser.seenval('I')) {
  623. if (!parser.has_value()) {
  624. SERIAL_PROTOCOLLNPAIR("?I seen, but no index specified! [0-", I2CPE_ENCODER_CNT - 1);
  625. SERIAL_PROTOCOLLNPGM("]");
  626. return I2CPE_PARSE_ERR;
  627. };
  628. I2CPE_idx = parser.value_byte();
  629. if (I2CPE_idx >= I2CPE_ENCODER_CNT) {
  630. SERIAL_PROTOCOLLNPAIR("?Index out of range. [0-", I2CPE_ENCODER_CNT - 1);
  631. SERIAL_ECHOLNPGM("]");
  632. return I2CPE_PARSE_ERR;
  633. }
  634. I2CPE_addr = encoders[I2CPE_idx].get_address();
  635. }
  636. else
  637. I2CPE_idx = 0xFF;
  638. I2CPE_anyaxis = parser.seen_axis();
  639. return I2CPE_PARSE_OK;
  640. };
  641. /**
  642. * M860: Report the position(s) of position encoder module(s).
  643. *
  644. * A<addr> Module I2C address. [30, 200].
  645. * I<index> Module index. [0, I2CPE_ENCODER_CNT - 1]
  646. * O Include homed zero-offset in returned position.
  647. * U Units in mm or raw step count.
  648. *
  649. * If A or I not specified:
  650. * X Report on X axis encoder, if present.
  651. * Y Report on Y axis encoder, if present.
  652. * Z Report on Z axis encoder, if present.
  653. * E Report on E axis encoder, if present.
  654. *
  655. */
  656. void I2CPositionEncodersMgr::M860() {
  657. if (parse()) return;
  658. const bool hasU = parser.seen('U'), hasO = parser.seen('O');
  659. if (I2CPE_idx == 0xFF) {
  660. LOOP_XYZE(i) {
  661. if (!I2CPE_anyaxis || parser.seen(axis_codes[i])) {
  662. const uint8_t idx = idx_from_axis(AxisEnum(i));
  663. if ((int8_t)idx >= 0) report_position(idx, hasU, hasO);
  664. }
  665. }
  666. }
  667. else
  668. report_position(I2CPE_idx, hasU, hasO);
  669. }
  670. /**
  671. * M861: Report the status of position encoder modules.
  672. *
  673. * A<addr> Module I2C address. [30, 200].
  674. * I<index> Module index. [0, I2CPE_ENCODER_CNT - 1]
  675. *
  676. * If A or I not specified:
  677. * X Report on X axis encoder, if present.
  678. * Y Report on Y axis encoder, if present.
  679. * Z Report on Z axis encoder, if present.
  680. * E Report on E axis encoder, if present.
  681. *
  682. */
  683. void I2CPositionEncodersMgr::M861() {
  684. if (parse()) return;
  685. if (I2CPE_idx == 0xFF) {
  686. LOOP_XYZE(i) {
  687. if (!I2CPE_anyaxis || parser.seen(axis_codes[i])) {
  688. const uint8_t idx = idx_from_axis(AxisEnum(i));
  689. if ((int8_t)idx >= 0) report_status(idx);
  690. }
  691. }
  692. }
  693. else
  694. report_status(I2CPE_idx);
  695. }
  696. /**
  697. * M862: Perform an axis continuity test for position encoder
  698. * modules.
  699. *
  700. * A<addr> Module I2C address. [30, 200].
  701. * I<index> Module index. [0, I2CPE_ENCODER_CNT - 1]
  702. *
  703. * If A or I not specified:
  704. * X Report on X axis encoder, if present.
  705. * Y Report on Y axis encoder, if present.
  706. * Z Report on Z axis encoder, if present.
  707. * E Report on E axis encoder, if present.
  708. *
  709. */
  710. void I2CPositionEncodersMgr::M862() {
  711. if (parse()) return;
  712. if (I2CPE_idx == 0xFF) {
  713. LOOP_XYZE(i) {
  714. if (!I2CPE_anyaxis || parser.seen(axis_codes[i])) {
  715. const uint8_t idx = idx_from_axis(AxisEnum(i));
  716. if ((int8_t)idx >= 0) test_axis(idx);
  717. }
  718. }
  719. }
  720. else
  721. test_axis(I2CPE_idx);
  722. }
  723. /**
  724. * M863: Perform steps-per-mm calibration for
  725. * position encoder modules.
  726. *
  727. * A<addr> Module I2C address. [30, 200].
  728. * I<index> Module index. [0, I2CPE_ENCODER_CNT - 1]
  729. * P Number of rePeats/iterations.
  730. *
  731. * If A or I not specified:
  732. * X Report on X axis encoder, if present.
  733. * Y Report on Y axis encoder, if present.
  734. * Z Report on Z axis encoder, if present.
  735. * E Report on E axis encoder, if present.
  736. *
  737. */
  738. void I2CPositionEncodersMgr::M863() {
  739. if (parse()) return;
  740. const uint8_t iterations = constrain(parser.byteval('P', 1), 1, 10);
  741. if (I2CPE_idx == 0xFF) {
  742. LOOP_XYZE(i) {
  743. if (!I2CPE_anyaxis || parser.seen(axis_codes[i])) {
  744. const uint8_t idx = idx_from_axis(AxisEnum(i));
  745. if ((int8_t)idx >= 0) calibrate_steps_mm(idx, iterations);
  746. }
  747. }
  748. }
  749. else
  750. calibrate_steps_mm(I2CPE_idx, iterations);
  751. }
  752. /**
  753. * M864: Change position encoder module I2C address.
  754. *
  755. * A<addr> Module current/old I2C address. If not present,
  756. * assumes default address (030). [30, 200].
  757. * S<addr> Module new I2C address. [30, 200].
  758. *
  759. * If S is not specified:
  760. * X Use I2CPE_PRESET_ADDR_X (030).
  761. * Y Use I2CPE_PRESET_ADDR_Y (031).
  762. * Z Use I2CPE_PRESET_ADDR_Z (032).
  763. * E Use I2CPE_PRESET_ADDR_E (033).
  764. */
  765. void I2CPositionEncodersMgr::M864() {
  766. uint8_t newAddress;
  767. if (parse()) return;
  768. if (!I2CPE_addr) I2CPE_addr = I2CPE_PRESET_ADDR_X;
  769. if (parser.seen('S')) {
  770. if (!parser.has_value()) {
  771. SERIAL_PROTOCOLLNPGM("?S seen, but no address specified! [30-200]");
  772. return;
  773. };
  774. newAddress = parser.value_byte();
  775. if (!WITHIN(newAddress, 30, 200)) {
  776. SERIAL_PROTOCOLLNPGM("?New address out of range. [30-200]");
  777. return;
  778. }
  779. }
  780. else if (!I2CPE_anyaxis) {
  781. SERIAL_PROTOCOLLNPGM("?You must specify S or [XYZE].");
  782. return;
  783. }
  784. else {
  785. if (parser.seen('X')) newAddress = I2CPE_PRESET_ADDR_X;
  786. else if (parser.seen('Y')) newAddress = I2CPE_PRESET_ADDR_Y;
  787. else if (parser.seen('Z')) newAddress = I2CPE_PRESET_ADDR_Z;
  788. else if (parser.seen('E')) newAddress = I2CPE_PRESET_ADDR_E;
  789. else return;
  790. }
  791. SERIAL_ECHOPAIR("Changing module at address ", I2CPE_addr);
  792. SERIAL_ECHOLNPAIR(" 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