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

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