My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

encoder_i2c.cpp 35KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144
  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 <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 "encoder_i2c.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)
  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_ECHO(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_ECHO(axis_codes[encoderAxis]);
  160. SERIAL_ECHOLNPAIR(" : LARGE ERR ", int(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_ECHO(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_ECHO(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_ECHO(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_ECHO(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_ECHO(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_ECHO(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((int)i2cAddress, 3) != 3) {
  251. //houston, we have a problem...
  252. H = I2CPE_MAG_SIG_NF;
  253. return 0;
  254. }
  255. while (Wire.available())
  256. encoderCount.bval[index++] = (uint8_t)Wire.read();
  257. //extract the magnetic strength
  258. H = (B00000011 & (encoderCount.bval[2] >> 6));
  259. //extract sign bit; sign = (encoderCount.bval[2] & B00100000);
  260. //set all upper bits to the sign value to overwrite H
  261. encoderCount.val = (encoderCount.bval[2] & B00100000) ? (encoderCount.val | 0xFFC00000) : (encoderCount.val & 0x003FFFFF);
  262. if (invert) encoderCount.val *= -1;
  263. return encoderCount.val;
  264. }
  265. bool I2CPositionEncoder::test_axis() {
  266. //only works on XYZ cartesian machines for the time being
  267. if (!(encoderAxis == X_AXIS || encoderAxis == Y_AXIS || encoderAxis == Z_AXIS)) return false;
  268. const float startPosition = soft_endstop.min[encoderAxis] + 10,
  269. endPosition = soft_endstop.max[encoderAxis] - 10;
  270. const feedRate_t fr_mm_s = FLOOR(MMM_TO_MMS((encoderAxis == Z_AXIS) ? HOMING_FEEDRATE_Z : HOMING_FEEDRATE_XY));
  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 = MMM_TO_MMS((encoderAxis == Z_AXIS) ? HOMING_FEEDRATE_Z : HOMING_FEEDRATE_XY);
  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. #if ENABLED(I2CPE_ERR_ROLLING_AVERAGE)
  366. ZERO(err);
  367. #endif
  368. }
  369. bool I2CPositionEncodersMgr::I2CPE_anyaxis;
  370. uint8_t I2CPositionEncodersMgr::I2CPE_addr,
  371. I2CPositionEncodersMgr::I2CPE_idx;
  372. I2CPositionEncoder I2CPositionEncodersMgr::encoders[I2CPE_ENCODER_CNT];
  373. void I2CPositionEncodersMgr::init() {
  374. Wire.begin();
  375. #if I2CPE_ENCODER_CNT > 0
  376. uint8_t i = 0;
  377. encoders[i].init(I2CPE_ENC_1_ADDR, I2CPE_ENC_1_AXIS);
  378. #ifdef I2CPE_ENC_1_TYPE
  379. encoders[i].set_type(I2CPE_ENC_1_TYPE);
  380. #endif
  381. #ifdef I2CPE_ENC_1_TICKS_UNIT
  382. encoders[i].set_ticks_unit(I2CPE_ENC_1_TICKS_UNIT);
  383. #endif
  384. #ifdef I2CPE_ENC_1_TICKS_REV
  385. encoders[i].set_stepper_ticks(I2CPE_ENC_1_TICKS_REV);
  386. #endif
  387. #ifdef I2CPE_ENC_1_INVERT
  388. encoders[i].set_inverted(I2CPE_ENC_1_INVERT);
  389. #endif
  390. #ifdef I2CPE_ENC_1_EC_METHOD
  391. encoders[i].set_ec_method(I2CPE_ENC_1_EC_METHOD);
  392. #endif
  393. #ifdef I2CPE_ENC_1_EC_THRESH
  394. encoders[i].set_ec_threshold(I2CPE_ENC_1_EC_THRESH);
  395. #endif
  396. encoders[i].set_active(encoders[i].passes_test(true));
  397. #if I2CPE_ENC_1_AXIS == E_AXIS
  398. encoders[i].set_homed();
  399. #endif
  400. #endif
  401. #if I2CPE_ENCODER_CNT > 1
  402. i++;
  403. encoders[i].init(I2CPE_ENC_2_ADDR, I2CPE_ENC_2_AXIS);
  404. #ifdef I2CPE_ENC_2_TYPE
  405. encoders[i].set_type(I2CPE_ENC_2_TYPE);
  406. #endif
  407. #ifdef I2CPE_ENC_2_TICKS_UNIT
  408. encoders[i].set_ticks_unit(I2CPE_ENC_2_TICKS_UNIT);
  409. #endif
  410. #ifdef I2CPE_ENC_2_TICKS_REV
  411. encoders[i].set_stepper_ticks(I2CPE_ENC_2_TICKS_REV);
  412. #endif
  413. #ifdef I2CPE_ENC_2_INVERT
  414. encoders[i].set_inverted(I2CPE_ENC_2_INVERT);
  415. #endif
  416. #ifdef I2CPE_ENC_2_EC_METHOD
  417. encoders[i].set_ec_method(I2CPE_ENC_2_EC_METHOD);
  418. #endif
  419. #ifdef I2CPE_ENC_2_EC_THRESH
  420. encoders[i].set_ec_threshold(I2CPE_ENC_2_EC_THRESH);
  421. #endif
  422. encoders[i].set_active(encoders[i].passes_test(true));
  423. #if I2CPE_ENC_2_AXIS == E_AXIS
  424. encoders[i].set_homed();
  425. #endif
  426. #endif
  427. #if I2CPE_ENCODER_CNT > 2
  428. i++;
  429. encoders[i].init(I2CPE_ENC_3_ADDR, I2CPE_ENC_3_AXIS);
  430. #ifdef I2CPE_ENC_3_TYPE
  431. encoders[i].set_type(I2CPE_ENC_3_TYPE);
  432. #endif
  433. #ifdef I2CPE_ENC_3_TICKS_UNIT
  434. encoders[i].set_ticks_unit(I2CPE_ENC_3_TICKS_UNIT);
  435. #endif
  436. #ifdef I2CPE_ENC_3_TICKS_REV
  437. encoders[i].set_stepper_ticks(I2CPE_ENC_3_TICKS_REV);
  438. #endif
  439. #ifdef I2CPE_ENC_3_INVERT
  440. encoders[i].set_inverted(I2CPE_ENC_3_INVERT);
  441. #endif
  442. #ifdef I2CPE_ENC_3_EC_METHOD
  443. encoders[i].set_ec_method(I2CPE_ENC_3_EC_METHOD);
  444. #endif
  445. #ifdef I2CPE_ENC_3_EC_THRESH
  446. encoders[i].set_ec_threshold(I2CPE_ENC_3_EC_THRESH);
  447. #endif
  448. encoders[i].set_active(encoders[i].passes_test(true));
  449. #if I2CPE_ENC_3_AXIS == E_AXIS
  450. encoders[i].set_homed();
  451. #endif
  452. #endif
  453. #if I2CPE_ENCODER_CNT > 3
  454. i++;
  455. encoders[i].init(I2CPE_ENC_4_ADDR, I2CPE_ENC_4_AXIS);
  456. #ifdef I2CPE_ENC_4_TYPE
  457. encoders[i].set_type(I2CPE_ENC_4_TYPE);
  458. #endif
  459. #ifdef I2CPE_ENC_4_TICKS_UNIT
  460. encoders[i].set_ticks_unit(I2CPE_ENC_4_TICKS_UNIT);
  461. #endif
  462. #ifdef I2CPE_ENC_4_TICKS_REV
  463. encoders[i].set_stepper_ticks(I2CPE_ENC_4_TICKS_REV);
  464. #endif
  465. #ifdef I2CPE_ENC_4_INVERT
  466. encoders[i].set_inverted(I2CPE_ENC_4_INVERT);
  467. #endif
  468. #ifdef I2CPE_ENC_4_EC_METHOD
  469. encoders[i].set_ec_method(I2CPE_ENC_4_EC_METHOD);
  470. #endif
  471. #ifdef I2CPE_ENC_4_EC_THRESH
  472. encoders[i].set_ec_threshold(I2CPE_ENC_4_EC_THRESH);
  473. #endif
  474. encoders[i].set_active(encoders[i].passes_test(true));
  475. #if I2CPE_ENC_4_AXIS == E_AXIS
  476. encoders[i].set_homed();
  477. #endif
  478. #endif
  479. #if I2CPE_ENCODER_CNT > 4
  480. i++;
  481. encoders[i].init(I2CPE_ENC_5_ADDR, I2CPE_ENC_5_AXIS);
  482. #ifdef I2CPE_ENC_5_TYPE
  483. encoders[i].set_type(I2CPE_ENC_5_TYPE);
  484. #endif
  485. #ifdef I2CPE_ENC_5_TICKS_UNIT
  486. encoders[i].set_ticks_unit(I2CPE_ENC_5_TICKS_UNIT);
  487. #endif
  488. #ifdef I2CPE_ENC_5_TICKS_REV
  489. encoders[i].set_stepper_ticks(I2CPE_ENC_5_TICKS_REV);
  490. #endif
  491. #ifdef I2CPE_ENC_5_INVERT
  492. encoders[i].set_inverted(I2CPE_ENC_5_INVERT);
  493. #endif
  494. #ifdef I2CPE_ENC_5_EC_METHOD
  495. encoders[i].set_ec_method(I2CPE_ENC_5_EC_METHOD);
  496. #endif
  497. #ifdef I2CPE_ENC_5_EC_THRESH
  498. encoders[i].set_ec_threshold(I2CPE_ENC_5_EC_THRESH);
  499. #endif
  500. encoders[i].set_active(encoders[i].passes_test(true));
  501. #if I2CPE_ENC_5_AXIS == E_AXIS
  502. encoders[i].set_homed();
  503. #endif
  504. #endif
  505. #if I2CPE_ENCODER_CNT > 5
  506. i++;
  507. encoders[i].init(I2CPE_ENC_6_ADDR, I2CPE_ENC_6_AXIS);
  508. #ifdef I2CPE_ENC_6_TYPE
  509. encoders[i].set_type(I2CPE_ENC_6_TYPE);
  510. #endif
  511. #ifdef I2CPE_ENC_6_TICKS_UNIT
  512. encoders[i].set_ticks_unit(I2CPE_ENC_6_TICKS_UNIT);
  513. #endif
  514. #ifdef I2CPE_ENC_6_TICKS_REV
  515. encoders[i].set_stepper_ticks(I2CPE_ENC_6_TICKS_REV);
  516. #endif
  517. #ifdef I2CPE_ENC_6_INVERT
  518. encoders[i].set_inverted(I2CPE_ENC_6_INVERT);
  519. #endif
  520. #ifdef I2CPE_ENC_6_EC_METHOD
  521. encoders[i].set_ec_method(I2CPE_ENC_6_EC_METHOD);
  522. #endif
  523. #ifdef I2CPE_ENC_6_EC_THRESH
  524. encoders[i].set_ec_threshold(I2CPE_ENC_6_EC_THRESH);
  525. #endif
  526. encoders[i].set_active(encoders[i].passes_test(true));
  527. #if I2CPE_ENC_6_AXIS == E_AXIS
  528. encoders[i].set_homed();
  529. #endif
  530. #endif
  531. }
  532. void I2CPositionEncodersMgr::report_position(const int8_t idx, const bool units, const bool noOffset) {
  533. CHECK_IDX();
  534. if (units)
  535. SERIAL_ECHOLN(noOffset ? encoders[idx].mm_from_count(encoders[idx].get_raw_count()) : encoders[idx].get_position_mm());
  536. else {
  537. if (noOffset) {
  538. const int32_t raw_count = encoders[idx].get_raw_count();
  539. SERIAL_ECHO(axis_codes[encoders[idx].get_axis()]);
  540. SERIAL_CHAR(' ');
  541. for (uint8_t j = 31; j > 0; j--)
  542. SERIAL_ECHO((bool)(0x00000001 & (raw_count >> j)));
  543. SERIAL_ECHO((bool)(0x00000001 & raw_count));
  544. SERIAL_CHAR(' ');
  545. SERIAL_ECHOLN(raw_count);
  546. }
  547. else
  548. SERIAL_ECHOLN(encoders[idx].get_position());
  549. }
  550. }
  551. void I2CPositionEncodersMgr::change_module_address(const uint8_t oldaddr, const uint8_t newaddr) {
  552. // First check 'new' address is not in use
  553. Wire.beginTransmission(I2C_ADDRESS(newaddr));
  554. if (!Wire.endTransmission()) {
  555. SERIAL_ECHOLNPAIR("?There is already a device with that address on the I2C bus! (", newaddr, ")");
  556. return;
  557. }
  558. // Now check that we can find the module on the oldaddr address
  559. Wire.beginTransmission(I2C_ADDRESS(oldaddr));
  560. if (Wire.endTransmission()) {
  561. SERIAL_ECHOLNPAIR("?No module detected at this address! (", oldaddr, ")");
  562. return;
  563. }
  564. SERIAL_ECHOLNPAIR("Module found at ", oldaddr, ", changing address to ", newaddr);
  565. // Change the modules address
  566. Wire.beginTransmission(I2C_ADDRESS(oldaddr));
  567. Wire.write(I2CPE_SET_ADDR);
  568. Wire.write(newaddr);
  569. Wire.endTransmission();
  570. SERIAL_ECHOLNPGM("Address changed, resetting and waiting for confirmation..");
  571. // Wait for the module to reset (can probably be improved by polling address with a timeout).
  572. safe_delay(I2CPE_REBOOT_TIME);
  573. // Look for the module at the new address.
  574. Wire.beginTransmission(I2C_ADDRESS(newaddr));
  575. if (Wire.endTransmission()) {
  576. SERIAL_ECHOLNPGM("Address change failed! Check encoder module.");
  577. return;
  578. }
  579. SERIAL_ECHOLNPGM("Address change successful!");
  580. // Now, if this module is configured, find which encoder instance it's supposed to correspond to
  581. // and enable it (it will likely have failed initialization on power-up, before the address change).
  582. const int8_t idx = idx_from_addr(newaddr);
  583. if (idx >= 0 && !encoders[idx].get_active()) {
  584. SERIAL_ECHO(axis_codes[encoders[idx].get_axis()]);
  585. SERIAL_ECHOLNPGM(" axis encoder was not detected on printer startup. Trying again.");
  586. encoders[idx].set_active(encoders[idx].passes_test(true));
  587. }
  588. }
  589. void I2CPositionEncodersMgr::report_module_firmware(const uint8_t address) {
  590. // First check there is a module
  591. Wire.beginTransmission(I2C_ADDRESS(address));
  592. if (Wire.endTransmission()) {
  593. SERIAL_ECHOLNPAIR("?No module detected at this address! (", address, ")");
  594. return;
  595. }
  596. SERIAL_ECHOLNPAIR("Requesting version info from module at address ", address, ":");
  597. Wire.beginTransmission(I2C_ADDRESS(address));
  598. Wire.write(I2CPE_SET_REPORT_MODE);
  599. Wire.write(I2CPE_REPORT_VERSION);
  600. Wire.endTransmission();
  601. // Read value
  602. if (Wire.requestFrom((int)address, 32)) {
  603. char c;
  604. while (Wire.available() > 0 && (c = (char)Wire.read()) > 0)
  605. SERIAL_ECHO(c);
  606. SERIAL_EOL();
  607. }
  608. // Set module back to normal (distance) mode
  609. Wire.beginTransmission(I2C_ADDRESS(address));
  610. Wire.write(I2CPE_SET_REPORT_MODE);
  611. Wire.write(I2CPE_REPORT_DISTANCE);
  612. Wire.endTransmission();
  613. }
  614. int8_t I2CPositionEncodersMgr::parse() {
  615. I2CPE_addr = 0;
  616. if (parser.seen('A')) {
  617. if (!parser.has_value()) {
  618. SERIAL_ECHOLNPGM("?A seen, but no address specified! [30-200]");
  619. return I2CPE_PARSE_ERR;
  620. };
  621. I2CPE_addr = parser.value_byte();
  622. if (!WITHIN(I2CPE_addr, 30, 200)) { // reserve the first 30 and last 55
  623. SERIAL_ECHOLNPGM("?Address out of range. [30-200]");
  624. return I2CPE_PARSE_ERR;
  625. }
  626. I2CPE_idx = idx_from_addr(I2CPE_addr);
  627. if (I2CPE_idx >= I2CPE_ENCODER_CNT) {
  628. SERIAL_ECHOLNPGM("?No device with this address!");
  629. return I2CPE_PARSE_ERR;
  630. }
  631. }
  632. else if (parser.seenval('I')) {
  633. if (!parser.has_value()) {
  634. SERIAL_ECHOLNPAIR("?I seen, but no index specified! [0-", I2CPE_ENCODER_CNT - 1, "]");
  635. return I2CPE_PARSE_ERR;
  636. };
  637. I2CPE_idx = parser.value_byte();
  638. if (I2CPE_idx >= I2CPE_ENCODER_CNT) {
  639. SERIAL_ECHOLNPAIR("?Index out of range. [0-", I2CPE_ENCODER_CNT - 1, "]");
  640. return I2CPE_PARSE_ERR;
  641. }
  642. I2CPE_addr = encoders[I2CPE_idx].get_address();
  643. }
  644. else
  645. I2CPE_idx = 0xFF;
  646. I2CPE_anyaxis = parser.seen_axis();
  647. return I2CPE_PARSE_OK;
  648. };
  649. /**
  650. * M860: Report the position(s) of position encoder module(s).
  651. *
  652. * A<addr> Module I2C address. [30, 200].
  653. * I<index> Module index. [0, I2CPE_ENCODER_CNT - 1]
  654. * O Include homed zero-offset in returned position.
  655. * U Units in mm or raw step count.
  656. *
  657. * If A or I not specified:
  658. * X Report on X axis encoder, if present.
  659. * Y Report on Y axis encoder, if present.
  660. * Z Report on Z axis encoder, if present.
  661. * E Report on E axis encoder, if present.
  662. *
  663. */
  664. void I2CPositionEncodersMgr::M860() {
  665. if (parse()) return;
  666. const bool hasU = parser.seen('U'), hasO = parser.seen('O');
  667. if (I2CPE_idx == 0xFF) {
  668. LOOP_XYZE(i) {
  669. if (!I2CPE_anyaxis || parser.seen(axis_codes[i])) {
  670. const uint8_t idx = idx_from_axis(AxisEnum(i));
  671. if ((int8_t)idx >= 0) report_position(idx, hasU, hasO);
  672. }
  673. }
  674. }
  675. else
  676. report_position(I2CPE_idx, hasU, hasO);
  677. }
  678. /**
  679. * M861: Report the status of position encoder modules.
  680. *
  681. * A<addr> Module I2C address. [30, 200].
  682. * I<index> Module index. [0, I2CPE_ENCODER_CNT - 1]
  683. *
  684. * If A or I not specified:
  685. * X Report on X axis encoder, if present.
  686. * Y Report on Y axis encoder, if present.
  687. * Z Report on Z axis encoder, if present.
  688. * E Report on E axis encoder, if present.
  689. *
  690. */
  691. void I2CPositionEncodersMgr::M861() {
  692. if (parse()) return;
  693. if (I2CPE_idx == 0xFF) {
  694. LOOP_XYZE(i) {
  695. if (!I2CPE_anyaxis || parser.seen(axis_codes[i])) {
  696. const uint8_t idx = idx_from_axis(AxisEnum(i));
  697. if ((int8_t)idx >= 0) report_status(idx);
  698. }
  699. }
  700. }
  701. else
  702. report_status(I2CPE_idx);
  703. }
  704. /**
  705. * M862: Perform an axis continuity test for position encoder
  706. * modules.
  707. *
  708. * A<addr> Module I2C address. [30, 200].
  709. * I<index> Module index. [0, I2CPE_ENCODER_CNT - 1]
  710. *
  711. * If A or I not specified:
  712. * X Report on X axis encoder, if present.
  713. * Y Report on Y axis encoder, if present.
  714. * Z Report on Z axis encoder, if present.
  715. * E Report on E axis encoder, if present.
  716. *
  717. */
  718. void I2CPositionEncodersMgr::M862() {
  719. if (parse()) return;
  720. if (I2CPE_idx == 0xFF) {
  721. LOOP_XYZE(i) {
  722. if (!I2CPE_anyaxis || parser.seen(axis_codes[i])) {
  723. const uint8_t idx = idx_from_axis(AxisEnum(i));
  724. if ((int8_t)idx >= 0) test_axis(idx);
  725. }
  726. }
  727. }
  728. else
  729. test_axis(I2CPE_idx);
  730. }
  731. /**
  732. * M863: Perform steps-per-mm calibration for
  733. * position encoder modules.
  734. *
  735. * A<addr> Module I2C address. [30, 200].
  736. * I<index> Module index. [0, I2CPE_ENCODER_CNT - 1]
  737. * P Number of rePeats/iterations.
  738. *
  739. * If A or I not specified:
  740. * X Report on X axis encoder, if present.
  741. * Y Report on Y axis encoder, if present.
  742. * Z Report on Z axis encoder, if present.
  743. * E Report on E axis encoder, if present.
  744. *
  745. */
  746. void I2CPositionEncodersMgr::M863() {
  747. if (parse()) return;
  748. const uint8_t iterations = constrain(parser.byteval('P', 1), 1, 10);
  749. if (I2CPE_idx == 0xFF) {
  750. LOOP_XYZE(i) {
  751. if (!I2CPE_anyaxis || parser.seen(axis_codes[i])) {
  752. const uint8_t idx = idx_from_axis(AxisEnum(i));
  753. if ((int8_t)idx >= 0) calibrate_steps_mm(idx, iterations);
  754. }
  755. }
  756. }
  757. else
  758. calibrate_steps_mm(I2CPE_idx, iterations);
  759. }
  760. /**
  761. * M864: Change position encoder module I2C address.
  762. *
  763. * A<addr> Module current/old I2C address. If not present,
  764. * assumes default address (030). [30, 200].
  765. * S<addr> Module new I2C address. [30, 200].
  766. *
  767. * If S is not specified:
  768. * X Use I2CPE_PRESET_ADDR_X (030).
  769. * Y Use I2CPE_PRESET_ADDR_Y (031).
  770. * Z Use I2CPE_PRESET_ADDR_Z (032).
  771. * E Use I2CPE_PRESET_ADDR_E (033).
  772. */
  773. void I2CPositionEncodersMgr::M864() {
  774. uint8_t newAddress;
  775. if (parse()) return;
  776. if (!I2CPE_addr) I2CPE_addr = I2CPE_PRESET_ADDR_X;
  777. if (parser.seen('S')) {
  778. if (!parser.has_value()) {
  779. SERIAL_ECHOLNPGM("?S seen, but no address specified! [30-200]");
  780. return;
  781. };
  782. newAddress = parser.value_byte();
  783. if (!WITHIN(newAddress, 30, 200)) {
  784. SERIAL_ECHOLNPGM("?New address out of range. [30-200]");
  785. return;
  786. }
  787. }
  788. else if (!I2CPE_anyaxis) {
  789. SERIAL_ECHOLNPGM("?You must specify S or [XYZE].");
  790. return;
  791. }
  792. else {
  793. if (parser.seen('X')) newAddress = I2CPE_PRESET_ADDR_X;
  794. else if (parser.seen('Y')) newAddress = I2CPE_PRESET_ADDR_Y;
  795. else if (parser.seen('Z')) newAddress = I2CPE_PRESET_ADDR_Z;
  796. else if (parser.seen('E')) newAddress = I2CPE_PRESET_ADDR_E;
  797. else return;
  798. }
  799. SERIAL_ECHOLNPAIR("Changing module at address ", I2CPE_addr, " to address ", newAddress);
  800. change_module_address(I2CPE_addr, newAddress);
  801. }
  802. /**
  803. * M865: Check position encoder module firmware version.
  804. *
  805. * A<addr> Module I2C address. [30, 200].
  806. * I<index> Module index. [0, I2CPE_ENCODER_CNT - 1].
  807. *
  808. * If A or I not specified:
  809. * X Check X axis encoder, if present.
  810. * Y Check Y axis encoder, if present.
  811. * Z Check Z axis encoder, if present.
  812. * E Check E axis encoder, if present.
  813. */
  814. void I2CPositionEncodersMgr::M865() {
  815. if (parse()) return;
  816. if (!I2CPE_addr) {
  817. LOOP_XYZE(i) {
  818. if (!I2CPE_anyaxis || parser.seen(axis_codes[i])) {
  819. const uint8_t idx = idx_from_axis(AxisEnum(i));
  820. if ((int8_t)idx >= 0) report_module_firmware(encoders[idx].get_address());
  821. }
  822. }
  823. }
  824. else
  825. report_module_firmware(I2CPE_addr);
  826. }
  827. /**
  828. * M866: Report or reset position encoder module error
  829. * count.
  830. *
  831. * A<addr> Module I2C address. [30, 200].
  832. * I<index> Module index. [0, I2CPE_ENCODER_CNT - 1].
  833. * R Reset error counter.
  834. *
  835. * If A or I not specified:
  836. * X Act on X axis encoder, if present.
  837. * Y Act on Y axis encoder, if present.
  838. * Z Act on Z axis encoder, if present.
  839. * E Act on E axis encoder, if present.
  840. */
  841. void I2CPositionEncodersMgr::M866() {
  842. if (parse()) return;
  843. const bool hasR = parser.seen('R');
  844. if (I2CPE_idx == 0xFF) {
  845. LOOP_XYZE(i) {
  846. if (!I2CPE_anyaxis || parser.seen(axis_codes[i])) {
  847. const uint8_t idx = idx_from_axis(AxisEnum(i));
  848. if ((int8_t)idx >= 0) {
  849. if (hasR)
  850. reset_error_count(idx, AxisEnum(i));
  851. else
  852. report_error_count(idx, AxisEnum(i));
  853. }
  854. }
  855. }
  856. }
  857. else if (hasR)
  858. reset_error_count(I2CPE_idx, encoders[I2CPE_idx].get_axis());
  859. else
  860. report_error_count(I2CPE_idx, encoders[I2CPE_idx].get_axis());
  861. }
  862. /**
  863. * M867: Enable/disable or toggle error correction for position encoder modules.
  864. *
  865. * A<addr> Module I2C address. [30, 200].
  866. * I<index> Module index. [0, I2CPE_ENCODER_CNT - 1].
  867. * S<1|0> Enable/disable error correction. 1 enables, 0 disables. If not
  868. * supplied, toggle.
  869. *
  870. * If A or I not specified:
  871. * X Act on X axis encoder, if present.
  872. * Y Act on Y axis encoder, if present.
  873. * Z Act on Z axis encoder, if present.
  874. * E Act on E axis encoder, if present.
  875. */
  876. void I2CPositionEncodersMgr::M867() {
  877. if (parse()) return;
  878. const int8_t onoff = parser.seenval('S') ? parser.value_int() : -1;
  879. if (I2CPE_idx == 0xFF) {
  880. LOOP_XYZE(i) {
  881. if (!I2CPE_anyaxis || parser.seen(axis_codes[i])) {
  882. const uint8_t idx = idx_from_axis(AxisEnum(i));
  883. if ((int8_t)idx >= 0) {
  884. const bool ena = onoff == -1 ? !encoders[I2CPE_idx].get_ec_enabled() : !!onoff;
  885. enable_ec(idx, ena, AxisEnum(i));
  886. }
  887. }
  888. }
  889. }
  890. else {
  891. const bool ena = onoff == -1 ? !encoders[I2CPE_idx].get_ec_enabled() : !!onoff;
  892. enable_ec(I2CPE_idx, ena, encoders[I2CPE_idx].get_axis());
  893. }
  894. }
  895. /**
  896. * M868: Report or set position encoder module error correction
  897. * threshold.
  898. *
  899. * A<addr> Module I2C address. [30, 200].
  900. * I<index> Module index. [0, I2CPE_ENCODER_CNT - 1].
  901. * T New error correction threshold.
  902. *
  903. * If A not specified:
  904. * X Act on X axis encoder, if present.
  905. * Y Act on Y axis encoder, if present.
  906. * Z Act on Z axis encoder, if present.
  907. * E Act on E axis encoder, if present.
  908. */
  909. void I2CPositionEncodersMgr::M868() {
  910. if (parse()) return;
  911. const float newThreshold = parser.seenval('T') ? parser.value_float() : -9999;
  912. if (I2CPE_idx == 0xFF) {
  913. LOOP_XYZE(i) {
  914. if (!I2CPE_anyaxis || parser.seen(axis_codes[i])) {
  915. const uint8_t idx = idx_from_axis(AxisEnum(i));
  916. if ((int8_t)idx >= 0) {
  917. if (newThreshold != -9999)
  918. set_ec_threshold(idx, newThreshold, encoders[idx].get_axis());
  919. else
  920. get_ec_threshold(idx, encoders[idx].get_axis());
  921. }
  922. }
  923. }
  924. }
  925. else if (newThreshold != -9999)
  926. set_ec_threshold(I2CPE_idx, newThreshold, encoders[I2CPE_idx].get_axis());
  927. else
  928. get_ec_threshold(I2CPE_idx, encoders[I2CPE_idx].get_axis());
  929. }
  930. /**
  931. * M869: Report position encoder module error.
  932. *
  933. * A<addr> Module I2C address. [30, 200].
  934. * I<index> Module index. [0, I2CPE_ENCODER_CNT - 1].
  935. *
  936. * If A not specified:
  937. * X Act on X axis encoder, if present.
  938. * Y Act on Y axis encoder, if present.
  939. * Z Act on Z axis encoder, if present.
  940. * E Act on E axis encoder, if present.
  941. */
  942. void I2CPositionEncodersMgr::M869() {
  943. if (parse()) return;
  944. if (I2CPE_idx == 0xFF) {
  945. LOOP_XYZE(i) {
  946. if (!I2CPE_anyaxis || parser.seen(axis_codes[i])) {
  947. const uint8_t idx = idx_from_axis(AxisEnum(i));
  948. if ((int8_t)idx >= 0) report_error(idx);
  949. }
  950. }
  951. }
  952. else
  953. report_error(I2CPE_idx);
  954. }
  955. #endif // I2C_POSITION_ENCODERS