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

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