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 34KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2019 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 "../inc/MarlinConfig.h"
  29. #if ENABLED(I2C_POSITION_ENCODERS)
  30. #include "I2CPositionEncoder.h"
  31. #include "../module/temperature.h"
  32. #include "../module/stepper.h"
  33. #include "../gcode/parser.h"
  34. #include "../feature/babystep.h"
  35. #include <Wire.h>
  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 ", 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_ECHOLMPAIR("Fault detected on ", 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. /*
  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 ", 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_ECHO(mm_from_count(get_position()));
  88. SERIAL_ECHOLNPGM(")");
  89. #endif
  90. }
  91. */
  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) && ABS(error) < 2000) { // Check for persistent error (skip)
  133. errPrst[errPrstIdx++] = error; // Error must persist for I2CPE_ERR_PRST_ARRAY_SIZE error cycles. This also serves to improve the average accuracy
  134. if (errPrstIdx >= I2CPE_ERR_PRST_ARRAY_SIZE) {
  135. float sumP = 0;
  136. LOOP_L_N(i, I2CPE_ERR_PRST_ARRAY_SIZE) sumP += errPrst[i];
  137. const int32_t errorP = int32_t(sumP * RECIPROCAL(I2CPE_ERR_PRST_ARRAY_SIZE));
  138. SERIAL_ECHO(axis_codes[encoderAxis]);
  139. SERIAL_ECHOLNPAIR(" - err detected: ", errorP * planner.steps_to_mm[encoderAxis], "mm; correcting!");
  140. babystep.add_steps(encoderAxis, -LROUND(errorP));
  141. errPrstIdx = 0;
  142. }
  143. }
  144. else
  145. errPrstIdx = 0;
  146. }
  147. #else
  148. if (ABS(error) > threshold * planner.settings.axis_steps_per_mm[encoderAxis]) {
  149. //SERIAL_ECHOLN(error);
  150. //SERIAL_ECHOLN(position);
  151. babystep.add_steps(encoderAxis, -LROUND(error / 2));
  152. }
  153. #endif
  154. if (ABS(error) > I2CPE_ERR_CNT_THRESH * planner.settings.axis_steps_per_mm[encoderAxis]) {
  155. const millis_t ms = millis();
  156. if (ELAPSED(ms, nextErrorCountTime)) {
  157. SERIAL_ECHOLNPAIR("Large error on ", axis_codes[encoderAxis], " axis. error: ", (int)error, "; diffSum: ", diffSum);
  158. errorCount++;
  159. nextErrorCountTime = ms + I2CPE_ERR_CNT_DEBOUNCE_MS;
  160. }
  161. }
  162. }
  163. lastPositionTime = positionTime;
  164. }
  165. void I2CPositionEncoder::set_homed() {
  166. if (active) {
  167. reset(); // Reset module's offset to zero (so current position is homed / zero)
  168. delay(10);
  169. zeroOffset = get_raw_count();
  170. homed++;
  171. trusted++;
  172. #ifdef I2CPE_DEBUG
  173. SERIAL_ECHO(axis_codes[encoderAxis]);
  174. SERIAL_ECHOLNPAIR(" axis encoder homed, offset of ", zeroOffset, " ticks.");
  175. #endif
  176. }
  177. }
  178. void I2CPositionEncoder::set_unhomed() {
  179. zeroOffset = 0;
  180. homed = trusted = false;
  181. #ifdef I2CPE_DEBUG
  182. SERIAL_ECHO(axis_codes[encoderAxis]);
  183. SERIAL_ECHOLNPGM(" axis encoder unhomed.");
  184. #endif
  185. }
  186. bool I2CPositionEncoder::passes_test(const bool report) {
  187. if (report) {
  188. if (H != I2CPE_MAG_SIG_GOOD) SERIAL_ECHOPGM("Warning. ");
  189. SERIAL_ECHO(axis_codes[encoderAxis]);
  190. serial_ternary(H == I2CPE_MAG_SIG_BAD, PSTR(" axis "), PSTR("magnetic strip "), PSTR("encoder "));
  191. switch (H) {
  192. case I2CPE_MAG_SIG_GOOD:
  193. case I2CPE_MAG_SIG_MID:
  194. SERIAL_ECHO_TERNARY(H == I2CPE_MAG_SIG_GOOD, "passes test; field strength ", "good", "fair", ".\n");
  195. break;
  196. default:
  197. SERIAL_ECHOLNPGM("not detected!");
  198. }
  199. }
  200. return (H == I2CPE_MAG_SIG_GOOD || H == I2CPE_MAG_SIG_MID);
  201. }
  202. float I2CPositionEncoder::get_axis_error_mm(const bool report) {
  203. float target, actual, error;
  204. target = planner.get_axis_position_mm(encoderAxis);
  205. actual = mm_from_count(position);
  206. error = actual - target;
  207. if (ABS(error) > 10000) error = 0; // ?
  208. if (report) {
  209. SERIAL_ECHO(axis_codes[encoderAxis]);
  210. SERIAL_ECHOLNPAIR(" axis target: ", target, ", actual: ", actual, ", error : ",error);
  211. }
  212. return error;
  213. }
  214. int32_t I2CPositionEncoder::get_axis_error_steps(const bool report) {
  215. if (!active) {
  216. if (report) {
  217. SERIAL_ECHO(axis_codes[encoderAxis]);
  218. SERIAL_ECHOLNPGM(" axis encoder not active!");
  219. }
  220. return 0;
  221. }
  222. float stepperTicksPerUnit;
  223. int32_t encoderTicks = position, encoderCountInStepperTicksScaled;
  224. //int32_t stepperTicks = stepper.position(encoderAxis);
  225. // With a rotary encoder we're concerned with ticks/rev; whereas with a linear we're concerned with ticks/mm
  226. stepperTicksPerUnit = (type == I2CPE_ENC_TYPE_ROTARY) ? stepperTicks : planner.settings.axis_steps_per_mm[encoderAxis];
  227. //convert both 'ticks' into same units / base
  228. encoderCountInStepperTicksScaled = LROUND((stepperTicksPerUnit * encoderTicks) / encoderTicksPerUnit);
  229. int32_t target = stepper.position(encoderAxis),
  230. error = (encoderCountInStepperTicksScaled - target);
  231. //suppress discontinuities (might be caused by bad I2C readings...?)
  232. const bool suppressOutput = (ABS(error - errorPrev) > 100);
  233. if (report) {
  234. SERIAL_ECHO(axis_codes[encoderAxis]);
  235. SERIAL_ECHOLNPAIR(" axis target: ", target, ", actual: ", encoderCountInStepperTicksScaled, ", error : ", error);
  236. if (suppressOutput) SERIAL_ECHOLNPGM("Discontinuity detected, suppressing error.");
  237. }
  238. errorPrev = error;
  239. return (suppressOutput ? 0 : error);
  240. }
  241. int32_t I2CPositionEncoder::get_raw_count() {
  242. uint8_t index = 0;
  243. i2cLong encoderCount;
  244. encoderCount.val = 0x00;
  245. if (Wire.requestFrom((int)i2cAddress, 3) != 3) {
  246. //houston, we have a problem...
  247. H = I2CPE_MAG_SIG_NF;
  248. return 0;
  249. }
  250. while (Wire.available())
  251. encoderCount.bval[index++] = (uint8_t)Wire.read();
  252. //extract the magnetic strength
  253. H = (B00000011 & (encoderCount.bval[2] >> 6));
  254. //extract sign bit; sign = (encoderCount.bval[2] & B00100000);
  255. //set all upper bits to the sign value to overwrite H
  256. encoderCount.val = (encoderCount.bval[2] & B00100000) ? (encoderCount.val | 0xFFC00000) : (encoderCount.val & 0x003FFFFF);
  257. if (invert) encoderCount.val *= -1;
  258. return encoderCount.val;
  259. }
  260. bool I2CPositionEncoder::test_axis() {
  261. //only works on XYZ cartesian machines for the time being
  262. if (!(encoderAxis == X_AXIS || encoderAxis == Y_AXIS || encoderAxis == Z_AXIS)) return false;
  263. const float startPosition = soft_endstop.min[encoderAxis] + 10,
  264. endPosition = soft_endstop.max[encoderAxis] - 10;
  265. const feedRate_t fr_mm_s = FLOOR(MMM_TO_MMS((encoderAxis == Z_AXIS) ? HOMING_FEEDRATE_Z : HOMING_FEEDRATE_XY));
  266. ec = false;
  267. xyze_pos_t startCoord, endCoord;
  268. LOOP_XYZ(a) {
  269. startCoord[a] = planner.get_axis_position_mm((AxisEnum)a);
  270. endCoord[a] = planner.get_axis_position_mm((AxisEnum)a);
  271. }
  272. startCoord[encoderAxis] = startPosition;
  273. endCoord[encoderAxis] = endPosition;
  274. planner.synchronize();
  275. startCoord.e = planner.get_axis_position_mm(E_AXIS);
  276. planner.buffer_line(startCoord, fr_mm_s, 0);
  277. planner.synchronize();
  278. // if the module isn't currently trusted, wait until it is (or until it should be if things are working)
  279. if (!trusted) {
  280. int32_t startWaitingTime = millis();
  281. while (!trusted && millis() - startWaitingTime < I2CPE_TIME_TRUSTED)
  282. safe_delay(500);
  283. }
  284. if (trusted) { // if trusted, commence test
  285. endCoord.e = planner.get_axis_position_mm(E_AXIS);
  286. planner.buffer_line(endCoord, fr_mm_s, 0);
  287. planner.synchronize();
  288. }
  289. return trusted;
  290. }
  291. void I2CPositionEncoder::calibrate_steps_mm(const uint8_t iter) {
  292. if (type != I2CPE_ENC_TYPE_LINEAR) {
  293. SERIAL_ECHOLNPGM("Steps/mm calibration requires linear encoder.");
  294. return;
  295. }
  296. if (!(encoderAxis == X_AXIS || encoderAxis == Y_AXIS || encoderAxis == Z_AXIS)) {
  297. SERIAL_ECHOLNPGM("Steps/mm calibration not supported for this axis.");
  298. return;
  299. }
  300. float old_steps_mm, new_steps_mm,
  301. startDistance, endDistance,
  302. travelDistance, travelledDistance, total = 0;
  303. int32_t startCount, stopCount;
  304. const feedRate_t fr_mm_s = MMM_TO_MMS((encoderAxis == Z_AXIS) ? HOMING_FEEDRATE_Z : HOMING_FEEDRATE_XY);
  305. bool oldec = ec;
  306. ec = false;
  307. startDistance = 20;
  308. endDistance = soft_endstop.max[encoderAxis] - 20;
  309. travelDistance = endDistance - startDistance;
  310. xyze_pos_t startCoord, endCoord;
  311. LOOP_XYZ(a) {
  312. startCoord[a] = planner.get_axis_position_mm((AxisEnum)a);
  313. endCoord[a] = planner.get_axis_position_mm((AxisEnum)a);
  314. }
  315. startCoord[encoderAxis] = startDistance;
  316. endCoord[encoderAxis] = endDistance;
  317. planner.synchronize();
  318. LOOP_L_N(i, iter) {
  319. startCoord.e = planner.get_axis_position_mm(E_AXIS);
  320. planner.buffer_line(startCoord, fr_mm_s, 0);
  321. planner.synchronize();
  322. delay(250);
  323. startCount = get_position();
  324. //do_blocking_move_to(endCoord);
  325. endCoord.e = planner.get_axis_position_mm(E_AXIS);
  326. planner.buffer_line(endCoord, fr_mm_s, 0);
  327. planner.synchronize();
  328. //Read encoder distance
  329. delay(250);
  330. stopCount = get_position();
  331. travelledDistance = mm_from_count(ABS(stopCount - startCount));
  332. SERIAL_ECHOLNPAIR("Attempted travel: ", travelDistance, "mm");
  333. SERIAL_ECHOLNPAIR(" Actual travel: ", travelledDistance, "mm");
  334. //Calculate new axis steps per unit
  335. old_steps_mm = planner.settings.axis_steps_per_mm[encoderAxis];
  336. new_steps_mm = (old_steps_mm * travelDistance) / travelledDistance;
  337. SERIAL_ECHOLNPAIR("Old steps/mm: ", old_steps_mm);
  338. SERIAL_ECHOLNPAIR("New steps/mm: ", new_steps_mm);
  339. //Save new value
  340. planner.settings.axis_steps_per_mm[encoderAxis] = new_steps_mm;
  341. if (iter > 1) {
  342. total += new_steps_mm;
  343. // swap start and end points so next loop runs from current position
  344. const float tempCoord = startCoord[encoderAxis];
  345. startCoord[encoderAxis] = endCoord[encoderAxis];
  346. endCoord[encoderAxis] = tempCoord;
  347. }
  348. }
  349. if (iter > 1) {
  350. total /= (float)iter;
  351. SERIAL_ECHOLNPAIR("Average steps/mm: ", total);
  352. }
  353. ec = oldec;
  354. SERIAL_ECHOLNPGM("Calculated steps/mm set. Use M500 to save to EEPROM.");
  355. }
  356. void I2CPositionEncoder::reset() {
  357. Wire.beginTransmission(I2C_ADDRESS(i2cAddress));
  358. Wire.write(I2CPE_RESET_COUNT);
  359. Wire.endTransmission();
  360. #if ENABLED(I2CPE_ERR_ROLLING_AVERAGE)
  361. ZERO(err);
  362. #endif
  363. }
  364. bool I2CPositionEncodersMgr::I2CPE_anyaxis;
  365. uint8_t I2CPositionEncodersMgr::I2CPE_addr,
  366. I2CPositionEncodersMgr::I2CPE_idx;
  367. I2CPositionEncoder I2CPositionEncodersMgr::encoders[I2CPE_ENCODER_CNT];
  368. void I2CPositionEncodersMgr::init() {
  369. Wire.begin();
  370. #if I2CPE_ENCODER_CNT > 0
  371. uint8_t i = 0;
  372. encoders[i].init(I2CPE_ENC_1_ADDR, I2CPE_ENC_1_AXIS);
  373. #ifdef I2CPE_ENC_1_TYPE
  374. encoders[i].set_type(I2CPE_ENC_1_TYPE);
  375. #endif
  376. #ifdef I2CPE_ENC_1_TICKS_UNIT
  377. encoders[i].set_ticks_unit(I2CPE_ENC_1_TICKS_UNIT);
  378. #endif
  379. #ifdef I2CPE_ENC_1_TICKS_REV
  380. encoders[i].set_stepper_ticks(I2CPE_ENC_1_TICKS_REV);
  381. #endif
  382. #ifdef I2CPE_ENC_1_INVERT
  383. encoders[i].set_inverted(I2CPE_ENC_1_INVERT);
  384. #endif
  385. #ifdef I2CPE_ENC_1_EC_METHOD
  386. encoders[i].set_ec_method(I2CPE_ENC_1_EC_METHOD);
  387. #endif
  388. #ifdef I2CPE_ENC_1_EC_THRESH
  389. encoders[i].set_ec_threshold(I2CPE_ENC_1_EC_THRESH);
  390. #endif
  391. encoders[i].set_active(encoders[i].passes_test(true));
  392. #if I2CPE_ENC_1_AXIS == E_AXIS
  393. encoders[i].set_homed();
  394. #endif
  395. #endif
  396. #if I2CPE_ENCODER_CNT > 1
  397. i++;
  398. encoders[i].init(I2CPE_ENC_2_ADDR, I2CPE_ENC_2_AXIS);
  399. #ifdef I2CPE_ENC_2_TYPE
  400. encoders[i].set_type(I2CPE_ENC_2_TYPE);
  401. #endif
  402. #ifdef I2CPE_ENC_2_TICKS_UNIT
  403. encoders[i].set_ticks_unit(I2CPE_ENC_2_TICKS_UNIT);
  404. #endif
  405. #ifdef I2CPE_ENC_2_TICKS_REV
  406. encoders[i].set_stepper_ticks(I2CPE_ENC_2_TICKS_REV);
  407. #endif
  408. #ifdef I2CPE_ENC_2_INVERT
  409. encoders[i].set_inverted(I2CPE_ENC_2_INVERT);
  410. #endif
  411. #ifdef I2CPE_ENC_2_EC_METHOD
  412. encoders[i].set_ec_method(I2CPE_ENC_2_EC_METHOD);
  413. #endif
  414. #ifdef I2CPE_ENC_2_EC_THRESH
  415. encoders[i].set_ec_threshold(I2CPE_ENC_2_EC_THRESH);
  416. #endif
  417. encoders[i].set_active(encoders[i].passes_test(true));
  418. #if I2CPE_ENC_2_AXIS == E_AXIS
  419. encoders[i].set_homed();
  420. #endif
  421. #endif
  422. #if I2CPE_ENCODER_CNT > 2
  423. i++;
  424. encoders[i].init(I2CPE_ENC_3_ADDR, I2CPE_ENC_3_AXIS);
  425. #ifdef I2CPE_ENC_3_TYPE
  426. encoders[i].set_type(I2CPE_ENC_3_TYPE);
  427. #endif
  428. #ifdef I2CPE_ENC_3_TICKS_UNIT
  429. encoders[i].set_ticks_unit(I2CPE_ENC_3_TICKS_UNIT);
  430. #endif
  431. #ifdef I2CPE_ENC_3_TICKS_REV
  432. encoders[i].set_stepper_ticks(I2CPE_ENC_3_TICKS_REV);
  433. #endif
  434. #ifdef I2CPE_ENC_3_INVERT
  435. encoders[i].set_inverted(I2CPE_ENC_3_INVERT);
  436. #endif
  437. #ifdef I2CPE_ENC_3_EC_METHOD
  438. encoders[i].set_ec_method(I2CPE_ENC_3_EC_METHOD);
  439. #endif
  440. #ifdef I2CPE_ENC_3_EC_THRESH
  441. encoders[i].set_ec_threshold(I2CPE_ENC_3_EC_THRESH);
  442. #endif
  443. encoders[i].set_active(encoders[i].passes_test(true));
  444. #if I2CPE_ENC_3_AXIS == E_AXIS
  445. encoders[i].set_homed();
  446. #endif
  447. #endif
  448. #if I2CPE_ENCODER_CNT > 3
  449. i++;
  450. encoders[i].init(I2CPE_ENC_4_ADDR, I2CPE_ENC_4_AXIS);
  451. #ifdef I2CPE_ENC_4_TYPE
  452. encoders[i].set_type(I2CPE_ENC_4_TYPE);
  453. #endif
  454. #ifdef I2CPE_ENC_4_TICKS_UNIT
  455. encoders[i].set_ticks_unit(I2CPE_ENC_4_TICKS_UNIT);
  456. #endif
  457. #ifdef I2CPE_ENC_4_TICKS_REV
  458. encoders[i].set_stepper_ticks(I2CPE_ENC_4_TICKS_REV);
  459. #endif
  460. #ifdef I2CPE_ENC_4_INVERT
  461. encoders[i].set_inverted(I2CPE_ENC_4_INVERT);
  462. #endif
  463. #ifdef I2CPE_ENC_4_EC_METHOD
  464. encoders[i].set_ec_method(I2CPE_ENC_4_EC_METHOD);
  465. #endif
  466. #ifdef I2CPE_ENC_4_EC_THRESH
  467. encoders[i].set_ec_threshold(I2CPE_ENC_4_EC_THRESH);
  468. #endif
  469. encoders[i].set_active(encoders[i].passes_test(true));
  470. #if I2CPE_ENC_4_AXIS == E_AXIS
  471. encoders[i].set_homed();
  472. #endif
  473. #endif
  474. #if I2CPE_ENCODER_CNT > 4
  475. i++;
  476. encoders[i].init(I2CPE_ENC_5_ADDR, I2CPE_ENC_5_AXIS);
  477. #ifdef I2CPE_ENC_5_TYPE
  478. encoders[i].set_type(I2CPE_ENC_5_TYPE);
  479. #endif
  480. #ifdef I2CPE_ENC_5_TICKS_UNIT
  481. encoders[i].set_ticks_unit(I2CPE_ENC_5_TICKS_UNIT);
  482. #endif
  483. #ifdef I2CPE_ENC_5_TICKS_REV
  484. encoders[i].set_stepper_ticks(I2CPE_ENC_5_TICKS_REV);
  485. #endif
  486. #ifdef I2CPE_ENC_5_INVERT
  487. encoders[i].set_inverted(I2CPE_ENC_5_INVERT);
  488. #endif
  489. #ifdef I2CPE_ENC_5_EC_METHOD
  490. encoders[i].set_ec_method(I2CPE_ENC_5_EC_METHOD);
  491. #endif
  492. #ifdef I2CPE_ENC_5_EC_THRESH
  493. encoders[i].set_ec_threshold(I2CPE_ENC_5_EC_THRESH);
  494. #endif
  495. encoders[i].set_active(encoders[i].passes_test(true));
  496. #if I2CPE_ENC_5_AXIS == E_AXIS
  497. encoders[i].set_homed();
  498. #endif
  499. #endif
  500. #if I2CPE_ENCODER_CNT > 5
  501. i++;
  502. encoders[i].init(I2CPE_ENC_6_ADDR, I2CPE_ENC_6_AXIS);
  503. #ifdef I2CPE_ENC_6_TYPE
  504. encoders[i].set_type(I2CPE_ENC_6_TYPE);
  505. #endif
  506. #ifdef I2CPE_ENC_6_TICKS_UNIT
  507. encoders[i].set_ticks_unit(I2CPE_ENC_6_TICKS_UNIT);
  508. #endif
  509. #ifdef I2CPE_ENC_6_TICKS_REV
  510. encoders[i].set_stepper_ticks(I2CPE_ENC_6_TICKS_REV);
  511. #endif
  512. #ifdef I2CPE_ENC_6_INVERT
  513. encoders[i].set_inverted(I2CPE_ENC_6_INVERT);
  514. #endif
  515. #ifdef I2CPE_ENC_6_EC_METHOD
  516. encoders[i].set_ec_method(I2CPE_ENC_6_EC_METHOD);
  517. #endif
  518. #ifdef I2CPE_ENC_6_EC_THRESH
  519. encoders[i].set_ec_threshold(I2CPE_ENC_6_EC_THRESH);
  520. #endif
  521. encoders[i].set_active(encoders[i].passes_test(true));
  522. #if I2CPE_ENC_6_AXIS == E_AXIS
  523. encoders[i].set_homed();
  524. #endif
  525. #endif
  526. }
  527. void I2CPositionEncodersMgr::report_position(const int8_t idx, const bool units, const bool noOffset) {
  528. CHECK_IDX();
  529. if (units)
  530. SERIAL_ECHOLN(noOffset ? encoders[idx].mm_from_count(encoders[idx].get_raw_count()) : encoders[idx].get_position_mm());
  531. else {
  532. if (noOffset) {
  533. const int32_t raw_count = encoders[idx].get_raw_count();
  534. SERIAL_ECHO(axis_codes[encoders[idx].get_axis()]);
  535. SERIAL_CHAR(' ');
  536. for (uint8_t j = 31; j > 0; j--)
  537. SERIAL_ECHO((bool)(0x00000001 & (raw_count >> j)));
  538. SERIAL_ECHO((bool)(0x00000001 & raw_count));
  539. SERIAL_CHAR(' ');
  540. SERIAL_ECHOLN(raw_count);
  541. }
  542. else
  543. SERIAL_ECHOLN(encoders[idx].get_position());
  544. }
  545. }
  546. void I2CPositionEncodersMgr::change_module_address(const uint8_t oldaddr, const uint8_t newaddr) {
  547. // First check 'new' address is not in use
  548. Wire.beginTransmission(I2C_ADDRESS(newaddr));
  549. if (!Wire.endTransmission()) {
  550. SERIAL_ECHOLNPAIR("?There is already a device with that address on the I2C bus! (", newaddr, ")");
  551. return;
  552. }
  553. // Now check that we can find the module on the oldaddr address
  554. Wire.beginTransmission(I2C_ADDRESS(oldaddr));
  555. if (Wire.endTransmission()) {
  556. SERIAL_ECHOLNPAIR("?No module detected at this address! (", oldaddr, ")");
  557. return;
  558. }
  559. SERIAL_ECHOLNPAIR("Module found at ", oldaddr, ", changing address to ", newaddr);
  560. // Change the modules address
  561. Wire.beginTransmission(I2C_ADDRESS(oldaddr));
  562. Wire.write(I2CPE_SET_ADDR);
  563. Wire.write(newaddr);
  564. Wire.endTransmission();
  565. SERIAL_ECHOLNPGM("Address changed, resetting and waiting for confirmation..");
  566. // Wait for the module to reset (can probably be improved by polling address with a timeout).
  567. safe_delay(I2CPE_REBOOT_TIME);
  568. // Look for the module at the new address.
  569. Wire.beginTransmission(I2C_ADDRESS(newaddr));
  570. if (Wire.endTransmission()) {
  571. SERIAL_ECHOLNPGM("Address change failed! Check encoder module.");
  572. return;
  573. }
  574. SERIAL_ECHOLNPGM("Address change successful!");
  575. // Now, if this module is configured, find which encoder instance it's supposed to correspond to
  576. // and enable it (it will likely have failed initialization on power-up, before the address change).
  577. const int8_t idx = idx_from_addr(newaddr);
  578. if (idx >= 0 && !encoders[idx].get_active()) {
  579. SERIAL_ECHO(axis_codes[encoders[idx].get_axis()]);
  580. SERIAL_ECHOLNPGM(" axis encoder was not detected on printer startup. Trying again.");
  581. encoders[idx].set_active(encoders[idx].passes_test(true));
  582. }
  583. }
  584. void I2CPositionEncodersMgr::report_module_firmware(const uint8_t address) {
  585. // First check there is a module
  586. Wire.beginTransmission(I2C_ADDRESS(address));
  587. if (Wire.endTransmission()) {
  588. SERIAL_ECHOLNPAIR("?No module detected at this address! (", address, ")");
  589. return;
  590. }
  591. SERIAL_ECHOLNPAIR("Requesting version info from module at address ", address, ":");
  592. Wire.beginTransmission(I2C_ADDRESS(address));
  593. Wire.write(I2CPE_SET_REPORT_MODE);
  594. Wire.write(I2CPE_REPORT_VERSION);
  595. Wire.endTransmission();
  596. // Read value
  597. if (Wire.requestFrom((int)address, 32)) {
  598. char c;
  599. while (Wire.available() > 0 && (c = (char)Wire.read()) > 0)
  600. SERIAL_ECHO(c);
  601. SERIAL_EOL();
  602. }
  603. // Set module back to normal (distance) mode
  604. Wire.beginTransmission(I2C_ADDRESS(address));
  605. Wire.write(I2CPE_SET_REPORT_MODE);
  606. Wire.write(I2CPE_REPORT_DISTANCE);
  607. Wire.endTransmission();
  608. }
  609. int8_t I2CPositionEncodersMgr::parse() {
  610. I2CPE_addr = 0;
  611. if (parser.seen('A')) {
  612. if (!parser.has_value()) {
  613. SERIAL_ECHOLNPGM("?A seen, but no address specified! [30-200]");
  614. return I2CPE_PARSE_ERR;
  615. };
  616. I2CPE_addr = parser.value_byte();
  617. if (!WITHIN(I2CPE_addr, 30, 200)) { // reserve the first 30 and last 55
  618. SERIAL_ECHOLNPGM("?Address out of range. [30-200]");
  619. return I2CPE_PARSE_ERR;
  620. }
  621. I2CPE_idx = idx_from_addr(I2CPE_addr);
  622. if (I2CPE_idx >= I2CPE_ENCODER_CNT) {
  623. SERIAL_ECHOLNPGM("?No device with this address!");
  624. return I2CPE_PARSE_ERR;
  625. }
  626. }
  627. else if (parser.seenval('I')) {
  628. if (!parser.has_value()) {
  629. SERIAL_ECHOLNPAIR("?I seen, but no index specified! [0-", I2CPE_ENCODER_CNT - 1, "]");
  630. return I2CPE_PARSE_ERR;
  631. };
  632. I2CPE_idx = parser.value_byte();
  633. if (I2CPE_idx >= I2CPE_ENCODER_CNT) {
  634. SERIAL_ECHOLNPAIR("?Index out of range. [0-", I2CPE_ENCODER_CNT - 1, "]");
  635. return I2CPE_PARSE_ERR;
  636. }
  637. I2CPE_addr = encoders[I2CPE_idx].get_address();
  638. }
  639. else
  640. I2CPE_idx = 0xFF;
  641. I2CPE_anyaxis = parser.seen_axis();
  642. return I2CPE_PARSE_OK;
  643. };
  644. /**
  645. * M860: Report the position(s) of position encoder module(s).
  646. *
  647. * A<addr> Module I2C address. [30, 200].
  648. * I<index> Module index. [0, I2CPE_ENCODER_CNT - 1]
  649. * O Include homed zero-offset in returned position.
  650. * U Units in mm or raw step count.
  651. *
  652. * If A or I not specified:
  653. * X Report on X axis encoder, if present.
  654. * Y Report on Y axis encoder, if present.
  655. * Z Report on Z axis encoder, if present.
  656. * E Report on E axis encoder, if present.
  657. *
  658. */
  659. void I2CPositionEncodersMgr::M860() {
  660. if (parse()) return;
  661. const bool hasU = parser.seen('U'), hasO = parser.seen('O');
  662. if (I2CPE_idx == 0xFF) {
  663. LOOP_XYZE(i) {
  664. if (!I2CPE_anyaxis || parser.seen(axis_codes[i])) {
  665. const uint8_t idx = idx_from_axis(AxisEnum(i));
  666. if ((int8_t)idx >= 0) report_position(idx, hasU, hasO);
  667. }
  668. }
  669. }
  670. else
  671. report_position(I2CPE_idx, hasU, hasO);
  672. }
  673. /**
  674. * M861: Report the status of position encoder modules.
  675. *
  676. * A<addr> Module I2C address. [30, 200].
  677. * I<index> Module index. [0, I2CPE_ENCODER_CNT - 1]
  678. *
  679. * If A or I not specified:
  680. * X Report on X axis encoder, if present.
  681. * Y Report on Y axis encoder, if present.
  682. * Z Report on Z axis encoder, if present.
  683. * E Report on E axis encoder, if present.
  684. *
  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. */
  713. void I2CPositionEncodersMgr::M862() {
  714. if (parse()) return;
  715. if (I2CPE_idx == 0xFF) {
  716. LOOP_XYZE(i) {
  717. if (!I2CPE_anyaxis || parser.seen(axis_codes[i])) {
  718. const uint8_t idx = idx_from_axis(AxisEnum(i));
  719. if ((int8_t)idx >= 0) test_axis(idx);
  720. }
  721. }
  722. }
  723. else
  724. test_axis(I2CPE_idx);
  725. }
  726. /**
  727. * M863: Perform steps-per-mm calibration for
  728. * position encoder modules.
  729. *
  730. * A<addr> Module I2C address. [30, 200].
  731. * I<index> Module index. [0, I2CPE_ENCODER_CNT - 1]
  732. * P Number of rePeats/iterations.
  733. *
  734. * If A or I not specified:
  735. * X Report on X axis encoder, if present.
  736. * Y Report on Y axis encoder, if present.
  737. * Z Report on Z axis encoder, if present.
  738. * E Report on E axis encoder, if present.
  739. *
  740. */
  741. void I2CPositionEncodersMgr::M863() {
  742. if (parse()) return;
  743. const uint8_t iterations = constrain(parser.byteval('P', 1), 1, 10);
  744. if (I2CPE_idx == 0xFF) {
  745. LOOP_XYZE(i) {
  746. if (!I2CPE_anyaxis || parser.seen(axis_codes[i])) {
  747. const uint8_t idx = idx_from_axis(AxisEnum(i));
  748. if ((int8_t)idx >= 0) calibrate_steps_mm(idx, iterations);
  749. }
  750. }
  751. }
  752. else
  753. calibrate_steps_mm(I2CPE_idx, iterations);
  754. }
  755. /**
  756. * M864: Change position encoder module I2C address.
  757. *
  758. * A<addr> Module current/old I2C address. If not present,
  759. * assumes default address (030). [30, 200].
  760. * S<addr> Module new I2C address. [30, 200].
  761. *
  762. * If S is not specified:
  763. * X Use I2CPE_PRESET_ADDR_X (030).
  764. * Y Use I2CPE_PRESET_ADDR_Y (031).
  765. * Z Use I2CPE_PRESET_ADDR_Z (032).
  766. * E Use I2CPE_PRESET_ADDR_E (033).
  767. */
  768. void I2CPositionEncodersMgr::M864() {
  769. uint8_t newAddress;
  770. if (parse()) return;
  771. if (!I2CPE_addr) I2CPE_addr = I2CPE_PRESET_ADDR_X;
  772. if (parser.seen('S')) {
  773. if (!parser.has_value()) {
  774. SERIAL_ECHOLNPGM("?S seen, but no address specified! [30-200]");
  775. return;
  776. };
  777. newAddress = parser.value_byte();
  778. if (!WITHIN(newAddress, 30, 200)) {
  779. SERIAL_ECHOLNPGM("?New address out of range. [30-200]");
  780. return;
  781. }
  782. }
  783. else if (!I2CPE_anyaxis) {
  784. SERIAL_ECHOLNPGM("?You must specify S or [XYZE].");
  785. return;
  786. }
  787. else {
  788. if (parser.seen('X')) newAddress = I2CPE_PRESET_ADDR_X;
  789. else if (parser.seen('Y')) newAddress = I2CPE_PRESET_ADDR_Y;
  790. else if (parser.seen('Z')) newAddress = I2CPE_PRESET_ADDR_Z;
  791. else if (parser.seen('E')) newAddress = I2CPE_PRESET_ADDR_E;
  792. else return;
  793. }
  794. SERIAL_ECHOLNPAIR("Changing module at address ", I2CPE_addr, " to address ", newAddress);
  795. change_module_address(I2CPE_addr, newAddress);
  796. }
  797. /**
  798. * M865: Check position encoder module firmware version.
  799. *
  800. * A<addr> Module I2C address. [30, 200].
  801. * I<index> Module index. [0, I2CPE_ENCODER_CNT - 1].
  802. *
  803. * If A or I not specified:
  804. * X Check X axis encoder, if present.
  805. * Y Check Y axis encoder, if present.
  806. * Z Check Z axis encoder, if present.
  807. * E Check E axis encoder, if present.
  808. */
  809. void I2CPositionEncodersMgr::M865() {
  810. if (parse()) return;
  811. if (!I2CPE_addr) {
  812. LOOP_XYZE(i) {
  813. if (!I2CPE_anyaxis || parser.seen(axis_codes[i])) {
  814. const uint8_t idx = idx_from_axis(AxisEnum(i));
  815. if ((int8_t)idx >= 0) report_module_firmware(encoders[idx].get_address());
  816. }
  817. }
  818. }
  819. else
  820. report_module_firmware(I2CPE_addr);
  821. }
  822. /**
  823. * M866: Report or reset position encoder module error
  824. * count.
  825. *
  826. * A<addr> Module I2C address. [30, 200].
  827. * I<index> Module index. [0, I2CPE_ENCODER_CNT - 1].
  828. * R Reset error counter.
  829. *
  830. * If A or I not specified:
  831. * X Act on X axis encoder, if present.
  832. * Y Act on Y axis encoder, if present.
  833. * Z Act on Z axis encoder, if present.
  834. * E Act on E axis encoder, if present.
  835. */
  836. void I2CPositionEncodersMgr::M866() {
  837. if (parse()) return;
  838. const bool hasR = parser.seen('R');
  839. if (I2CPE_idx == 0xFF) {
  840. LOOP_XYZE(i) {
  841. if (!I2CPE_anyaxis || parser.seen(axis_codes[i])) {
  842. const uint8_t idx = idx_from_axis(AxisEnum(i));
  843. if ((int8_t)idx >= 0) {
  844. if (hasR)
  845. reset_error_count(idx, AxisEnum(i));
  846. else
  847. report_error_count(idx, AxisEnum(i));
  848. }
  849. }
  850. }
  851. }
  852. else if (hasR)
  853. reset_error_count(I2CPE_idx, encoders[I2CPE_idx].get_axis());
  854. else
  855. report_error_count(I2CPE_idx, encoders[I2CPE_idx].get_axis());
  856. }
  857. /**
  858. * M867: Enable/disable or toggle error correction for position encoder modules.
  859. *
  860. * A<addr> Module I2C address. [30, 200].
  861. * I<index> Module index. [0, I2CPE_ENCODER_CNT - 1].
  862. * S<1|0> Enable/disable error correction. 1 enables, 0 disables. If not
  863. * supplied, toggle.
  864. *
  865. * If A or I not specified:
  866. * X Act on X axis encoder, if present.
  867. * Y Act on Y axis encoder, if present.
  868. * Z Act on Z axis encoder, if present.
  869. * E Act on E axis encoder, if present.
  870. */
  871. void I2CPositionEncodersMgr::M867() {
  872. if (parse()) return;
  873. const int8_t onoff = parser.seenval('S') ? parser.value_int() : -1;
  874. if (I2CPE_idx == 0xFF) {
  875. LOOP_XYZE(i) {
  876. if (!I2CPE_anyaxis || parser.seen(axis_codes[i])) {
  877. const uint8_t idx = idx_from_axis(AxisEnum(i));
  878. if ((int8_t)idx >= 0) {
  879. const bool ena = onoff == -1 ? !encoders[I2CPE_idx].get_ec_enabled() : !!onoff;
  880. enable_ec(idx, ena, AxisEnum(i));
  881. }
  882. }
  883. }
  884. }
  885. else {
  886. const bool ena = onoff == -1 ? !encoders[I2CPE_idx].get_ec_enabled() : !!onoff;
  887. enable_ec(I2CPE_idx, ena, encoders[I2CPE_idx].get_axis());
  888. }
  889. }
  890. /**
  891. * M868: Report or set position encoder module error correction
  892. * threshold.
  893. *
  894. * A<addr> Module I2C address. [30, 200].
  895. * I<index> Module index. [0, I2CPE_ENCODER_CNT - 1].
  896. * T New error correction threshold.
  897. *
  898. * If A not specified:
  899. * X Act on X axis encoder, if present.
  900. * Y Act on Y axis encoder, if present.
  901. * Z Act on Z axis encoder, if present.
  902. * E Act on E axis encoder, if present.
  903. */
  904. void I2CPositionEncodersMgr::M868() {
  905. if (parse()) return;
  906. const float newThreshold = parser.seenval('T') ? parser.value_float() : -9999;
  907. if (I2CPE_idx == 0xFF) {
  908. LOOP_XYZE(i) {
  909. if (!I2CPE_anyaxis || parser.seen(axis_codes[i])) {
  910. const uint8_t idx = idx_from_axis(AxisEnum(i));
  911. if ((int8_t)idx >= 0) {
  912. if (newThreshold != -9999)
  913. set_ec_threshold(idx, newThreshold, encoders[idx].get_axis());
  914. else
  915. get_ec_threshold(idx, encoders[idx].get_axis());
  916. }
  917. }
  918. }
  919. }
  920. else if (newThreshold != -9999)
  921. set_ec_threshold(I2CPE_idx, newThreshold, encoders[I2CPE_idx].get_axis());
  922. else
  923. get_ec_threshold(I2CPE_idx, encoders[I2CPE_idx].get_axis());
  924. }
  925. /**
  926. * M869: Report position encoder module error.
  927. *
  928. * A<addr> Module I2C address. [30, 200].
  929. * I<index> Module index. [0, I2CPE_ENCODER_CNT - 1].
  930. *
  931. * If A not specified:
  932. * X Act on X axis encoder, if present.
  933. * Y Act on Y axis encoder, if present.
  934. * Z Act on Z axis encoder, if present.
  935. * E Act on E axis encoder, if present.
  936. */
  937. void I2CPositionEncodersMgr::M869() {
  938. if (parse()) return;
  939. if (I2CPE_idx == 0xFF) {
  940. LOOP_XYZE(i) {
  941. if (!I2CPE_anyaxis || parser.seen(axis_codes[i])) {
  942. const uint8_t idx = idx_from_axis(AxisEnum(i));
  943. if ((int8_t)idx >= 0) report_error(idx);
  944. }
  945. }
  946. }
  947. else
  948. report_error(I2CPE_idx);
  949. }
  950. #endif // I2C_POSITION_ENCODERS