xyzcal.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  1. //xyzcal.cpp - xyz calibration with image processing
  2. #include "Configuration_prusa.h"
  3. #ifdef NEW_XYZCAL
  4. #include "xyzcal.h"
  5. #include <avr/wdt.h>
  6. #include "stepper.h"
  7. #include "temperature.h"
  8. #include "sm4.h"
  9. #define XYZCAL_PINDA_HYST_MIN 20 //50um
  10. #define XYZCAL_PINDA_HYST_MAX 100 //250um
  11. #define XYZCAL_PINDA_HYST_DIF 5 //12.5um
  12. #define ENABLE_FANCHECK_INTERRUPT() EIMSK |= (1<<7)
  13. #define DISABLE_FANCHECK_INTERRUPT() EIMSK &= ~(1<<7)
  14. #define _PINDA ((READ(Z_MIN_PIN) != Z_MIN_ENDSTOP_INVERTING)?1:0)
  15. #define DBG(args...) printf_P(args)
  16. //#define DBG(args...)
  17. #ifndef _n
  18. #define _n PSTR
  19. #endif //_n
  20. #define _X ((int16_t)count_position[X_AXIS])
  21. #define _Y ((int16_t)count_position[Y_AXIS])
  22. #define _Z ((int16_t)count_position[Z_AXIS])
  23. #define _E ((int16_t)count_position[E_AXIS])
  24. const constexpr uint8_t X_PLUS = 0;
  25. const constexpr uint8_t X_MINUS = 1;
  26. const constexpr uint8_t Y_PLUS = 0;
  27. const constexpr uint8_t Y_MINUS = 1;
  28. const constexpr uint8_t Z_PLUS = 0;
  29. const constexpr uint8_t Z_MINUS = 1;
  30. /// Max. jerk in PrusaSlicer, 10000 = 1 mm/s
  31. const constexpr uint16_t MAX_DELAY = 1000;
  32. const constexpr float MIN_SPEED = 0.01f / (MAX_DELAY * 0.000001f);
  33. /// 200 = 50 mm/s
  34. const constexpr uint16_t Z_MIN_DELAY = 200;
  35. const constexpr uint16_t Z_ACCEL = 5000;
  36. /// \returns positive value always
  37. #define ABS(a) \
  38. ({ __typeof__ (a) _a = (a); \
  39. _a >= 0 ? _a : (-_a); })
  40. /// \returns maximum of the two
  41. #define MAX(a, b) \
  42. ({ __typeof__ (a) _a = (a); \
  43. __typeof__ (b) _b = (b); \
  44. _a >= _b ? _a : _b; })
  45. /// \returns minimum of the two
  46. #define MIN(a, b) \
  47. ({ __typeof__ (a) _a = (a); \
  48. __typeof__ (b) _b = (b); \
  49. _a <= _b ? _a : _b; })
  50. /// swap values
  51. #define SWAP(a, b) \
  52. ({ __typeof__ (a) c = (a); \
  53. a = (b); \
  54. b = c; })
  55. /// Saturates value
  56. /// \returns min if value is less than min
  57. /// \returns max if value is more than min
  58. /// \returns value otherwise
  59. #define CLAMP(value, min, max) \
  60. ({ __typeof__ (value) a_ = (value); \
  61. __typeof__ (min) min_ = (min); \
  62. __typeof__ (max) max_ = (max); \
  63. ( a_ < min_ ? min_ : (a_ <= max_ ? a_ : max_)); })
  64. /// \returns square of the value
  65. #define SQR(a) \
  66. ({ __typeof__ (a) a_ = (a); \
  67. (a_ * a_); })
  68. /// position types
  69. typedef int16_t pos_i16_t;
  70. typedef long pos_i32_t;
  71. typedef float pos_mm_t;
  72. typedef int16_t usteps_t;
  73. uint8_t check_pinda_0();
  74. uint8_t check_pinda_1();
  75. void xyzcal_update_pos(uint16_t dx, uint16_t dy, uint16_t dz, uint16_t de);
  76. uint16_t xyzcal_calc_delay(uint16_t nd, uint16_t dd);
  77. uint8_t round_to_u8(float f){
  78. return (uint8_t)(f + .5f);
  79. }
  80. uint16_t round_to_u16(float f){
  81. return (uint16_t)(f + .5f);
  82. }
  83. int16_t round_to_i16(float f){
  84. return (int16_t)(f + .5f);
  85. }
  86. /// converts millimeters to integer position
  87. pos_i16_t mm_2_pos(pos_mm_t mm){
  88. return (pos_i16_t)(0.5f + mm * 100);
  89. }
  90. /// converts integer position to millimeters
  91. pos_mm_t pos_2_mm(pos_i16_t pos){
  92. return pos * 0.01f;
  93. }
  94. pos_mm_t pos_2_mm(float pos){
  95. return pos * 0.01f;
  96. }
  97. void xyzcal_meassure_enter(void)
  98. {
  99. DBG(_n("xyzcal_meassure_enter\n"));
  100. disable_heater();
  101. DISABLE_TEMPERATURE_INTERRUPT();
  102. #if (defined(FANCHECK) && defined(TACH_1) && (TACH_1 >-1))
  103. DISABLE_FANCHECK_INTERRUPT();
  104. #endif //(defined(FANCHECK) && defined(TACH_1) && (TACH_1 >-1))
  105. DISABLE_STEPPER_DRIVER_INTERRUPT();
  106. #ifdef WATCHDOG
  107. wdt_disable();
  108. #endif //WATCHDOG
  109. sm4_stop_cb = 0;
  110. sm4_update_pos_cb = xyzcal_update_pos;
  111. sm4_calc_delay_cb = xyzcal_calc_delay;
  112. }
  113. void xyzcal_meassure_leave(void)
  114. {
  115. DBG(_n("xyzcal_meassure_leave\n"));
  116. planner_abort_hard();
  117. ENABLE_TEMPERATURE_INTERRUPT();
  118. #if (defined(FANCHECK) && defined(TACH_1) && (TACH_1 >-1))
  119. ENABLE_FANCHECK_INTERRUPT();
  120. #endif //(defined(FANCHECK) && defined(TACH_1) && (TACH_1 >-1))
  121. ENABLE_STEPPER_DRIVER_INTERRUPT();
  122. #ifdef WATCHDOG
  123. wdt_enable(WDTO_4S);
  124. #endif //WATCHDOG
  125. sm4_stop_cb = 0;
  126. sm4_update_pos_cb = 0;
  127. sm4_calc_delay_cb = 0;
  128. }
  129. uint8_t check_pinda_0()
  130. {
  131. return _PINDA?0:1;
  132. }
  133. uint8_t check_pinda_1()
  134. {
  135. return _PINDA?1:0;
  136. }
  137. uint8_t xyzcal_dm = 0;
  138. void xyzcal_update_pos(uint16_t dx, uint16_t dy, uint16_t dz, uint16_t)
  139. {
  140. // DBG(_n("xyzcal_update_pos dx=%d dy=%d dz=%d dir=%02x\n"), dx, dy, dz, xyzcal_dm);
  141. if (xyzcal_dm&1) count_position[0] -= dx; else count_position[0] += dx;
  142. if (xyzcal_dm&2) count_position[1] -= dy; else count_position[1] += dy;
  143. if (xyzcal_dm&4) count_position[2] -= dz; else count_position[2] += dz;
  144. // DBG(_n(" after xyzcal_update_pos x=%ld y=%ld z=%ld\n"), count_position[0], count_position[1], count_position[2]);
  145. }
  146. uint16_t xyzcal_sm4_delay = 0;
  147. //#define SM4_ACCEL_TEST
  148. #ifdef SM4_ACCEL_TEST
  149. uint16_t xyzcal_sm4_v0 = 2000;
  150. uint16_t xyzcal_sm4_vm = 45000;
  151. uint16_t xyzcal_sm4_v = xyzcal_sm4_v0;
  152. uint16_t xyzcal_sm4_ac = 2000;
  153. uint16_t xyzcal_sm4_ac2 = (uint32_t)xyzcal_sm4_ac * 1024 / 10000;
  154. //float xyzcal_sm4_vm = 10000;
  155. #endif //SM4_ACCEL_TEST
  156. #ifdef SM4_ACCEL_TEST
  157. uint16_t xyzcal_calc_delay(uint16_t nd, uint16_t dd)
  158. {
  159. uint16_t del_us = 0;
  160. if (xyzcal_sm4_v & 0xf000) //>=4096
  161. {
  162. del_us = (uint16_t)62500 / (uint16_t)(xyzcal_sm4_v >> 4);
  163. xyzcal_sm4_v += (xyzcal_sm4_ac2 * del_us + 512) >> 10;
  164. if (xyzcal_sm4_v > xyzcal_sm4_vm) xyzcal_sm4_v = xyzcal_sm4_vm;
  165. if (del_us > 25) return del_us - 25;
  166. }
  167. else
  168. {
  169. del_us = (uint32_t)1000000 / xyzcal_sm4_v;
  170. xyzcal_sm4_v += ((uint32_t)xyzcal_sm4_ac2 * del_us + 512) >> 10;
  171. if (xyzcal_sm4_v > xyzcal_sm4_vm) xyzcal_sm4_v = xyzcal_sm4_vm;
  172. if (del_us > 50) return del_us - 50;
  173. }
  174. // uint16_t del_us = (uint16_t)(((float)1000000 / xyzcal_sm4_v) + 0.5);
  175. // uint16_t del_us = (uint32_t)1000000 / xyzcal_sm4_v;
  176. // uint16_t del_us = 100;
  177. // uint16_t del_us = (uint16_t)10000 / xyzcal_sm4_v;
  178. // v += (ac * del_us + 500) / 1000;
  179. // xyzcal_sm4_v += (xyzcal_sm4_ac * del_us) / 1000;
  180. // return xyzcal_sm4_delay;
  181. // DBG(_n("xyzcal_calc_delay nd=%d dd=%d v=%d del_us=%d\n"), nd, dd, xyzcal_sm4_v, del_us);
  182. return 0;
  183. }
  184. #else //SM4_ACCEL_TEST
  185. uint16_t xyzcal_calc_delay(uint16_t, uint16_t)
  186. {
  187. return xyzcal_sm4_delay;
  188. }
  189. #endif //SM4_ACCEL_TEST
  190. /// Moves printer to absolute position [x,y,z] defined in integer position system
  191. bool xyzcal_lineXYZ_to(int16_t x, int16_t y, int16_t z, uint16_t delay_us, int8_t check_pinda)
  192. {
  193. // DBG(_n("xyzcal_lineXYZ_to x=%d y=%d z=%d check=%d\n"), x, y, z, check_pinda);
  194. x -= (int16_t)count_position[0];
  195. y -= (int16_t)count_position[1];
  196. z -= (int16_t)count_position[2];
  197. xyzcal_dm = ((x<0)?1:0) | ((y<0)?2:0) | ((z<0)?4:0);
  198. sm4_set_dir_bits(xyzcal_dm);
  199. sm4_stop_cb = check_pinda?((check_pinda<0)?check_pinda_0:check_pinda_1):0;
  200. xyzcal_sm4_delay = delay_us;
  201. // uint32_t u = _micros();
  202. bool ret = sm4_line_xyze_ui(abs(x), abs(y), abs(z), 0) ? true : false;
  203. // u = _micros() - u;
  204. return ret;
  205. }
  206. /// Moves printer to absolute position [x,y,z] defined in millimeters
  207. bool xyzcal_lineXYZ_to_float(pos_mm_t x, pos_mm_t y, pos_mm_t z, uint16_t delay_us, int8_t check_pinda){
  208. return xyzcal_lineXYZ_to(mm_2_pos(x), mm_2_pos(y), mm_2_pos(z), delay_us, check_pinda);
  209. }
  210. bool xyzcal_spiral2(int16_t cx, int16_t cy, int16_t z0, int16_t dz, int16_t radius, int16_t rotation, uint16_t delay_us, int8_t check_pinda, uint16_t* pad)
  211. {
  212. bool ret = false;
  213. float r = 0; //radius
  214. uint16_t ad = 0; //angle [deg]
  215. float ar; //angle [rad]
  216. uint8_t dad = 0; //delta angle [deg]
  217. uint8_t dad_min = 4; //delta angle min [deg]
  218. uint8_t dad_max = 16; //delta angle max [deg]
  219. uint8_t k = 720 / (dad_max - dad_min); //delta calculation constant
  220. ad = 0;
  221. if (pad) ad = *pad % 720;
  222. DBG(_n("xyzcal_spiral2 cx=%d cy=%d z0=%d dz=%d radius=%d ad=%d\n"), cx, cy, z0, dz, radius, ad);
  223. // lcd_set_cursor(0, 4);
  224. // char text[10];
  225. // snprintf(text, 10, "%4d", z0);
  226. // lcd_print(text);
  227. for (; ad < 720; ad++)
  228. {
  229. if (radius > 0)
  230. {
  231. dad = dad_max - (ad / k);
  232. r = (float)(((uint32_t)ad) * radius) / 720;
  233. }
  234. else
  235. {
  236. dad = dad_max - ((719 - ad) / k);
  237. r = (float)(((uint32_t)(719 - ad)) * (-radius)) / 720;
  238. }
  239. ar = (ad + rotation)* (float)M_PI / 180;
  240. int x = (int)(cx + (cos(ar) * r));
  241. int y = (int)(cy + (sin(ar) * r));
  242. int z = (int)(z0 - ((float)((int32_t)dz * ad) / 720));
  243. if (xyzcal_lineXYZ_to(x, y, z, delay_us, check_pinda))
  244. {
  245. ad += dad + 1;
  246. ret = true;
  247. break;
  248. }
  249. ad += dad;
  250. }
  251. if (pad) *pad = ad;
  252. // if(ret){
  253. // lcd_set_cursor(0, 4);
  254. // lcd_print(" ");
  255. // }
  256. return ret;
  257. }
  258. bool xyzcal_spiral8(int16_t cx, int16_t cy, int16_t z0, int16_t dz, int16_t radius, uint16_t delay_us, int8_t check_pinda, uint16_t* pad)
  259. {
  260. bool ret = false;
  261. uint16_t ad = 0;
  262. if (pad) ad = *pad;
  263. DBG(_n("xyzcal_spiral8 cx=%d cy=%d z0=%d dz=%d radius=%d ad=%d\n"), cx, cy, z0, dz, radius, ad);
  264. if (!ret && (ad < 720))
  265. if ((ret = xyzcal_spiral2(cx, cy, z0 - 0*dz, dz, radius, 0, delay_us, check_pinda, &ad)) != 0)
  266. ad += 0;
  267. if (!ret && (ad < 1440))
  268. if ((ret = xyzcal_spiral2(cx, cy, z0 - 1*dz, dz, -radius, 0, delay_us, check_pinda, &ad)) != 0)
  269. ad += 720;
  270. if (!ret && (ad < 2160))
  271. if ((ret = xyzcal_spiral2(cx, cy, z0 - 2*dz, dz, radius, 180, delay_us, check_pinda, &ad)) != 0)
  272. ad += 1440;
  273. if (!ret && (ad < 2880))
  274. if ((ret = xyzcal_spiral2(cx, cy, z0 - 3*dz, dz, -radius, 180, delay_us, check_pinda, &ad)) != 0)
  275. ad += 2160;
  276. if (pad) *pad = ad;
  277. return ret;
  278. }
  279. #ifdef XYZCAL_MEASSURE_PINDA_HYSTEREZIS
  280. int8_t xyzcal_meassure_pinda_hysterezis(int16_t min_z, int16_t max_z, uint16_t delay_us, uint8_t samples)
  281. {
  282. DBG(_n("xyzcal_meassure_pinda_hysterezis\n"));
  283. int8_t ret = -1; // PINDA signal error
  284. int16_t z = _Z;
  285. int16_t sum_up = 0;
  286. int16_t sum_dn = 0;
  287. int16_t up;
  288. int16_t dn;
  289. uint8_t sample;
  290. xyzcal_lineXYZ_to(_X, _Y, min_z, delay_us, 1);
  291. xyzcal_lineXYZ_to(_X, _Y, max_z, delay_us, -1);
  292. if (!_PINDA)
  293. {
  294. for (sample = 0; sample < samples; sample++)
  295. {
  296. dn = _Z;
  297. if (!xyzcal_lineXYZ_to(_X, _Y, min_z, delay_us, 1)) break;
  298. dn = dn - _Z;
  299. up = _Z;
  300. if (!xyzcal_lineXYZ_to(_X, _Y, max_z, delay_us, -1)) break;
  301. up = _Z - up;
  302. DBG(_n("%d. up=%d dn=%d\n"), sample, up, dn);
  303. sum_up += up;
  304. sum_dn += dn;
  305. if (abs(up - dn) > XYZCAL_PINDA_HYST_DIF)
  306. {
  307. ret = -2; // difference between up-dn to high
  308. break;
  309. }
  310. }
  311. if (sample == samples)
  312. {
  313. up = sum_up / samples;
  314. dn = sum_dn / samples;
  315. uint16_t hyst = (up + dn) / 2;
  316. if (abs(up - dn) > XYZCAL_PINDA_HYST_DIF)
  317. ret = -2; // difference between up-dn to high
  318. else if ((hyst < XYZCAL_PINDA_HYST_MIN) || (hyst > XYZCAL_PINDA_HYST_MAX))
  319. ret = -3; // hysterezis out of range
  320. else
  321. ret = hyst;
  322. }
  323. }
  324. xyzcal_lineXYZ_to(_X, _Y, z, delay_us, 0);
  325. return ret;
  326. }
  327. #endif //XYZCAL_MEASSURE_PINDA_HYSTEREZIS
  328. /// Accelerate up to max.speed (defined by @min_delay_us)
  329. void accelerate(uint8_t axis, int16_t acc, uint16_t &delay_us, uint16_t min_delay_us){
  330. sm4_do_step(axis);
  331. /// keep max speed (avoid extra computation)
  332. if (acc > 0 && delay_us == min_delay_us){
  333. delayMicroseconds(delay_us);
  334. return;
  335. }
  336. // v1 = v0 + a * t
  337. // 0.01 = length of a step
  338. const float t0 = delay_us * 0.000001f;
  339. const float v1 = (0.01f / t0 + acc * t0);
  340. uint16_t t1;
  341. if (v1 <= 0.16f){ ///< slowest speed convertible to uint16_t delay
  342. t1 = MAX_DELAY; ///< already too slow so it wants to move back
  343. } else {
  344. /// don't exceed max.speed
  345. t1 = MAX(min_delay_us, round_to_u16(0.01f / v1 * 1000000.f));
  346. }
  347. /// make sure delay has changed a bit at least
  348. if (t1 == delay_us && acc != 0){
  349. if (acc > 0)
  350. t1--;
  351. else
  352. t1++;
  353. }
  354. //DBG(_n("%d "), t1);
  355. delayMicroseconds(t1);
  356. delay_us = t1;
  357. }
  358. void go_and_stop(uint8_t axis, int16_t dec, uint16_t &delay_us, uint16_t &steps){
  359. if (steps <= 0 || dec <= 0)
  360. return;
  361. /// deceleration distance in steps, s = 1/2 v^2 / a
  362. uint16_t s = round_to_u16(100 * 0.5f * SQR(0.01f) / (SQR((float)delay_us) * dec));
  363. if (steps > s){
  364. /// go steady
  365. sm4_do_step(axis);
  366. delayMicroseconds(delay_us);
  367. } else {
  368. /// decelerate
  369. accelerate(axis, -dec, delay_us, delay_us);
  370. }
  371. --steps;
  372. }
  373. /// Count number of zeros in the 32 byte array
  374. /// If the number is less than 16, it moves @min_z up
  375. /// Zeros make measuring faster but there cannot be too much
  376. bool more_zeros(uint8_t* pixels, int16_t &min_z){
  377. uint8_t hist[256];
  378. uint8_t i = 0;
  379. /// clear
  380. do {
  381. hist[i] = 0;
  382. } while (i++ < 255);
  383. /// fill
  384. for (i = 0; i < 32; ++i){
  385. ++hist[pixels[i]];
  386. }
  387. // /// print histogram
  388. // i = 0;
  389. // DBG(_n("hist: "));
  390. // do {
  391. // DBG(_n("%d "), hist[i]);
  392. // } while (i++ < 255);
  393. // DBG(_n("\n"));
  394. /// already more zeros on the line
  395. if (hist[0] >= 16){
  396. DBG(_n("zeros: %d\n"), hist[0]);
  397. return true;
  398. }
  399. /// find threshold
  400. uint8_t sum = 0;
  401. i = 0;
  402. do {
  403. sum += hist[i];
  404. if (sum >= 16)
  405. break;
  406. } while (i++ < 255);
  407. /// avoid too much zeros
  408. if (sum >= 24)
  409. --i;
  410. DBG(_n("sum %d, index %d\n"), sum, i);
  411. // DBG(_n("min_z %d\n"), min_z);
  412. min_z += i;
  413. // DBG(_n("min_z %d\n"), min_z);
  414. return false;
  415. }
  416. enum {
  417. RESTART_INIT,
  418. DO_RESTART,
  419. WAS_RESTARTED
  420. };
  421. void xyzcal_scan_pixels_32x32_Zhop(int16_t cx, int16_t cy, int16_t min_z, int16_t max_z, uint16_t delay_us, uint8_t* pixels){
  422. if (!pixels)
  423. return;
  424. int16_t z = _Z;
  425. int16_t z_trig;
  426. uint16_t line_buffer[32];
  427. uint16_t current_delay_us = MAX_DELAY; ///< defines current speed
  428. xyzcal_lineXYZ_to(cx - 1024, cy - 1024, min_z, delay_us, 0);
  429. int16_t start_z;
  430. uint16_t steps_to_go;
  431. /// restart if needed but just once
  432. /// 0 = init, 1 = do restart, 2 = restart was done, don't restart any more
  433. uint8_t restart = RESTART_INIT;
  434. do {
  435. if (restart == DO_RESTART)
  436. restart = WAS_RESTARTED;
  437. for (uint8_t r = 0; r < 32; r++){ ///< Y axis
  438. xyzcal_lineXYZ_to(_X, cy - 1024 + r * 64, z, delay_us, 0);
  439. for (int8_t d = 0; d < 2; ++d){ ///< direction
  440. xyzcal_lineXYZ_to((d & 1) ? (cx + 1024) : (cx - 1024), _Y, min_z, delay_us, 0);
  441. z = _Z;
  442. sm4_set_dir(X_AXIS, d);
  443. for (uint8_t c = 0; c < 32; c++){ ///< X axis
  444. z_trig = min_z;
  445. /// move up to un-trigger (surpress hysteresis)
  446. sm4_set_dir(Z_AXIS, Z_PLUS);
  447. /// speed up from stop, go half the way
  448. current_delay_us = MAX_DELAY;
  449. for (start_z = z; z < (max_z + start_z) / 2; ++z){
  450. if (!_PINDA){
  451. break;
  452. }
  453. accelerate(Z_AXIS_MASK, Z_ACCEL, current_delay_us, Z_MIN_DELAY);
  454. }
  455. if(_PINDA){
  456. uint16_t steps_to_go = MAX(0, max_z - z);
  457. while (_PINDA && z < max_z){
  458. go_and_stop(Z_AXIS_MASK, Z_ACCEL, current_delay_us, steps_to_go);
  459. ++z;
  460. }
  461. }
  462. /// slow down to stop
  463. while (current_delay_us < MAX_DELAY){
  464. accelerate(Z_AXIS_MASK, -Z_ACCEL, current_delay_us, Z_MIN_DELAY);
  465. ++z;
  466. }
  467. /// move down to trigger
  468. sm4_set_dir(Z_AXIS, Z_MINUS);
  469. /// speed up
  470. current_delay_us = MAX_DELAY;
  471. for (start_z = z; z > (min_z + start_z) / 2; --z){
  472. if (_PINDA){
  473. z_trig = z;
  474. break;
  475. }
  476. accelerate(Z_AXIS_MASK, Z_ACCEL, current_delay_us, Z_MIN_DELAY);
  477. }
  478. /// slow down
  479. if(!_PINDA){
  480. steps_to_go = MAX(0, z - min_z);
  481. while (!_PINDA && z > min_z){
  482. go_and_stop(Z_AXIS_MASK, Z_ACCEL, current_delay_us, steps_to_go);
  483. --z;
  484. }
  485. z_trig = z;
  486. }
  487. /// slow down to stop
  488. while (z > min_z && current_delay_us < MAX_DELAY){
  489. accelerate(Z_AXIS_MASK, -Z_ACCEL, current_delay_us, Z_MIN_DELAY);
  490. --z;
  491. }
  492. count_position[2] = z;
  493. if (d == 0){
  494. line_buffer[c] = (uint16_t)(z_trig - min_z);
  495. } else {
  496. /// data reversed in X
  497. // DBG(_n("%04x"), (line_buffer[31 - c] + (z - min_z)) / 2);
  498. /// save average of both directions
  499. pixels[(uint16_t)r * 32 + (31 - c)] = (uint8_t)MIN((uint32_t)255, ((uint32_t)line_buffer[31 - c] + (z_trig - min_z)) / 2);
  500. }
  501. /// move to the next point and move Z up diagonally (if needed)
  502. current_delay_us = MAX_DELAY;
  503. // const int8_t dir = (d & 1) ? -1 : 1;
  504. const int16_t end_x = ((d & 1) ? 1 : -1) * (64 * (16 - c) - 32) + cx;
  505. const int16_t length_x = ABS(end_x - _X);
  506. const int16_t half_x = length_x / 2;
  507. int16_t x = 0;
  508. /// don't go up if PINDA not triggered
  509. const bool up = _PINDA;
  510. int8_t axis = up ? X_AXIS_MASK | Z_AXIS_MASK : X_AXIS_MASK;
  511. sm4_set_dir(Z_AXIS, Z_PLUS);
  512. /// speed up
  513. for (x = 0; x <= half_x; ++x){
  514. accelerate(axis, Z_ACCEL, current_delay_us, Z_MIN_DELAY);
  515. if (up)
  516. ++z;
  517. }
  518. /// slow down
  519. steps_to_go = length_x - x;
  520. for (; x < length_x; ++x){
  521. go_and_stop(axis, Z_ACCEL, current_delay_us, steps_to_go);
  522. if (up)
  523. ++z;
  524. }
  525. count_position[0] = end_x;
  526. count_position[2] = z;
  527. }
  528. }
  529. /// do this in the first row only
  530. if (r == 0 && restart == RESTART_INIT){
  531. if (!more_zeros(pixels, min_z))
  532. restart = DO_RESTART;
  533. }
  534. if (restart == DO_RESTART)
  535. break;
  536. // DBG(_n("\n\n"));
  537. }
  538. } while (restart == DO_RESTART);
  539. }
  540. /// Returns rate of match
  541. /// max match = 132, min match = 0
  542. uint8_t xyzcal_match_pattern_12x12_in_32x32(uint16_t* pattern, uint8_t* pixels, uint8_t c, uint8_t r){
  543. uint8_t thr = 16;
  544. uint8_t match = 0;
  545. for (uint8_t i = 0; i < 12; ++i){
  546. for (uint8_t j = 0; j < 12; ++j){
  547. /// skip corners (3 pixels in each)
  548. if (((i == 0) || (i == 11)) && ((j < 2) || (j >= 10))) continue;
  549. if (((j == 0) || (j == 11)) && ((i < 2) || (i >= 10))) continue;
  550. const uint16_t idx = (c + j) + 32 * ((uint16_t)r + i);
  551. const bool high_pix = pixels[idx] > thr;
  552. const bool high_pat = pattern[i] & (1 << j);
  553. if (high_pix == high_pat)
  554. match++;
  555. }
  556. }
  557. return match;
  558. }
  559. /// Searches for best match of pattern by shifting it
  560. /// Returns rate of match and the best location
  561. /// max match = 132, min match = 0
  562. uint8_t xyzcal_find_pattern_12x12_in_32x32(uint8_t* pixels, uint16_t* pattern, uint8_t* pc, uint8_t* pr){
  563. if (!pixels || !pattern || !pc || !pr)
  564. return -1;
  565. uint8_t max_c = 0;
  566. uint8_t max_r = 0;
  567. uint8_t max_match = 0;
  568. // DBG(_n("Matching:\n"));
  569. /// pixel precision
  570. for (uint8_t r = 0; r < (32 - 12); ++r){
  571. for (uint8_t c = 0; c < (32 - 12); ++c){
  572. const uint8_t match = xyzcal_match_pattern_12x12_in_32x32(pattern, pixels, c, r);
  573. if (max_match < match){
  574. max_c = c;
  575. max_r = r;
  576. max_match = match;
  577. }
  578. // DBG(_n("%d "), match);
  579. }
  580. // DBG(_n("\n"));
  581. }
  582. DBG(_n("max_c=%d max_r=%d max_match=%d pixel\n"), max_c, max_r, max_match);
  583. *pc = max_c;
  584. *pr = max_r;
  585. return max_match;
  586. }
  587. uint8_t xyzcal_xycoords2point(int16_t x, int16_t y)
  588. {
  589. uint8_t ix = (x > 10000)?1:0;
  590. uint8_t iy = (y > 10000)?1:0;
  591. return iy?(3-ix):ix;
  592. }
  593. //MK3
  594. #if ((MOTHERBOARD == BOARD_EINSY_1_0a))
  595. const int16_t xyzcal_point_xcoords[4] PROGMEM = {1200, 22000, 22000, 1200};
  596. const int16_t xyzcal_point_ycoords[4] PROGMEM = {600, 600, 19800, 19800};
  597. #endif //((MOTHERBOARD == BOARD_EINSY_1_0a))
  598. //MK2.5
  599. #if ((MOTHERBOARD == BOARD_RAMBO_MINI_1_0) || (MOTHERBOARD == BOARD_RAMBO_MINI_1_3))
  600. const int16_t xyzcal_point_xcoords[4] PROGMEM = {1200, 22000, 22000, 1200};
  601. const int16_t xyzcal_point_ycoords[4] PROGMEM = {700, 700, 19800, 19800};
  602. #endif //((MOTHERBOARD == BOARD_RAMBO_MINI_1_0) || (MOTHERBOARD == BOARD_RAMBO_MINI_1_3))
  603. const uint16_t xyzcal_point_pattern[12] PROGMEM = {0x000, 0x0f0, 0x1f8, 0x3fc, 0x7fe, 0x7fe, 0x7fe, 0x7fe, 0x3fc, 0x1f8, 0x0f0, 0x000};
  604. bool xyzcal_searchZ(void)
  605. {
  606. DBG(_n("xyzcal_searchZ x=%ld y=%ld z=%ld\n"), count_position[X_AXIS], count_position[Y_AXIS], count_position[Z_AXIS]);
  607. int16_t x0 = _X;
  608. int16_t y0 = _Y;
  609. int16_t z0 = _Z;
  610. // int16_t min_z = -6000;
  611. // int16_t dz = 100;
  612. int16_t z = z0;
  613. while (z > -2300) //-6mm + 0.25mm
  614. {
  615. uint16_t ad = 0;
  616. if (xyzcal_spiral8(x0, y0, z, 100, 900, 320, 1, &ad)) //dz=100 radius=900 delay=400
  617. {
  618. int16_t x_on = _X;
  619. int16_t y_on = _Y;
  620. int16_t z_on = _Z;
  621. DBG(_n(" ON-SIGNAL at x=%d y=%d z=%d ad=%d\n"), x_on, y_on, z_on, ad);
  622. return true;
  623. }
  624. z -= 400;
  625. }
  626. DBG(_n("xyzcal_searchZ no signal\n x=%ld y=%ld z=%ld\n"), count_position[X_AXIS], count_position[Y_AXIS], count_position[Z_AXIS]);
  627. return false;
  628. }
  629. /// returns value of any location within data
  630. /// uses bilinear interpolation
  631. float get_value(uint8_t * matrix_32x32, float c, float r){
  632. if (c <= 0 || r <= 0 || c >= 31 || r >= 31)
  633. return 0;
  634. /// calculate weights of nearby points
  635. const float wc1 = c - floor(c);
  636. const float wr1 = r - floor(r);
  637. const float wc0 = 1 - wc1;
  638. const float wr0 = 1 - wr1;
  639. const float w00 = wc0 * wr0;
  640. const float w01 = wc0 * wr1;
  641. const float w10 = wc1 * wr0;
  642. const float w11 = wc1 * wr1;
  643. const uint16_t c0 = c;
  644. const uint16_t c1 = c0 + 1;
  645. const uint16_t r0 = r;
  646. const uint16_t r1 = r0 + 1;
  647. const uint16_t idx00 = c0 + 32 * r0;
  648. const uint16_t idx01 = c0 + 32 * r1;
  649. const uint16_t idx10 = c1 + 32 * r0;
  650. const uint16_t idx11 = c1 + 32 * r1;
  651. /// bilinear resampling
  652. return w00 * matrix_32x32[idx00] + w01 * matrix_32x32[idx01] + w10 * matrix_32x32[idx10] + w11 * matrix_32x32[idx11];
  653. }
  654. const constexpr float m_infinity = -1000.f;
  655. /// replaces the highest number by m_infinity
  656. void remove_highest(float *points, const uint8_t num_points){
  657. if (num_points <= 0)
  658. return;
  659. float max = points[0];
  660. uint8_t max_i = 0;
  661. for (uint8_t i = 0; i < num_points; ++i){
  662. if (max < points[i]){
  663. max = points[i];
  664. max_i = i;
  665. }
  666. }
  667. points[max_i] = m_infinity;
  668. }
  669. /// return the highest number in the list
  670. float highest(float *points, const uint8_t num_points){
  671. if (num_points <= 0)
  672. return 0;
  673. float max = points[0];
  674. for (uint8_t i = 0; i < num_points; ++i){
  675. if (max < points[i]){
  676. max = points[i];
  677. }
  678. }
  679. return max;
  680. }
  681. /// Searches for circle iteratively
  682. /// Uses points on the perimeter. If point is high it pushes circle out of the center (shift or change of radius),
  683. /// otherwise to the center.
  684. /// Algorithm is stopped after fixed number of iterations. Move is limited to 0.5 px per iteration.
  685. void dynamic_circle(uint8_t *matrix_32x32, float &x, float &y, float &r, uint8_t iterations){
  686. /// circle of 10.5 diameter has 33 in circumference, don't go much above
  687. const constexpr uint8_t num_points = 33;
  688. float points[num_points];
  689. float pi_2_div_num_points = 2 * M_PI / num_points;
  690. const constexpr uint8_t target_z = 32; ///< target z height of the circle
  691. float norm;
  692. float angle;
  693. float max_val = 0.5f;
  694. const uint8_t blocks = 7;
  695. float shifts_x[blocks];
  696. float shifts_y[blocks];
  697. float shifts_r[blocks];
  698. for (int8_t i = iterations; i > 0; --i){
  699. // DBG(_n(" [%f, %f][%f] circle\n"), x, y, r);
  700. /// read points on the circle
  701. for (uint8_t p = 0; p < num_points; ++p){
  702. angle = p * pi_2_div_num_points;
  703. points[p] = get_value(matrix_32x32, r * cos(angle) + x, r * sin(angle) + y) - target_z;
  704. // DBG(_n("%f "), points[p]);
  705. }
  706. // DBG(_n(" points\n"));
  707. /// sum blocks
  708. for (uint8_t j = 0; j < blocks; ++j){
  709. shifts_x[j] = shifts_y[j] = shifts_r[j] = 0;
  710. /// first part
  711. for (uint8_t p = 0; p < num_points * 3 / 4; ++p){
  712. uint8_t idx = (p + j * num_points / blocks) % num_points;
  713. angle = idx * pi_2_div_num_points;
  714. shifts_x[j] += cos(angle) * points[idx];
  715. shifts_y[j] += sin(angle) * points[idx];
  716. shifts_r[j] += points[idx];
  717. }
  718. }
  719. /// remove extreme values (slow but simple)
  720. for (uint8_t j = 0; j < blocks / 2; ++j){
  721. remove_highest(shifts_x, blocks);
  722. remove_highest(shifts_y, blocks);
  723. remove_highest(shifts_r, blocks);
  724. }
  725. /// median is the highest now
  726. norm = 1.f / (32.f * (num_points * 3 / 4));
  727. x += CLAMP(highest(shifts_x, blocks) * norm, -max_val, max_val);
  728. y += CLAMP(highest(shifts_y, blocks) * norm, -max_val, max_val);
  729. r += CLAMP(highest(shifts_r, blocks) * norm, -max_val, max_val);
  730. r = MAX(2, r);
  731. }
  732. DBG(_n(" [%f, %f][%f] final circle\n"), x, y, r);
  733. }
  734. /// Prints matrix in hex to debug output (serial line)
  735. void print_image(uint8_t *matrix_32x32){
  736. for (uint8_t y = 0; y < 32; ++y){
  737. const uint16_t idx_y = y * 32;
  738. for (uint8_t x = 0; x < 32; ++x){
  739. DBG(_n("%02x"), matrix_32x32[idx_y + x]);
  740. }
  741. DBG(_n("\n"));
  742. }
  743. DBG(_n("\n"));
  744. }
  745. /// scans area around the current head location and
  746. /// searches for the center of the calibration pin
  747. bool xyzcal_scan_and_process(void){
  748. DBG(_n("sizeof(block_buffer)=%d\n"), sizeof(block_t)*BLOCK_BUFFER_SIZE);
  749. bool ret = false;
  750. int16_t x = _X;
  751. int16_t y = _Y;
  752. int16_t z = _Z;
  753. uint8_t *matrix32 = (uint8_t *)block_buffer;
  754. uint16_t *pattern = (uint16_t *)(matrix32 + 32 * 32);
  755. xyzcal_scan_pixels_32x32_Zhop(x, y, z - 72, 2400, 600, matrix32);
  756. print_image(matrix32);
  757. for (uint8_t i = 0; i < 12; i++){
  758. pattern[i] = pgm_read_word((uint16_t*)(xyzcal_point_pattern + i));
  759. // DBG(_n(" pattern[%d]=%d\n"), i, pattern[i]);
  760. }
  761. /// SEARCH FOR BINARY CIRCLE
  762. uint8_t uc = 0;
  763. uint8_t ur = 0;
  764. /// max match = 132, 1/2 good = 66, 2/3 good = 88
  765. if (xyzcal_find_pattern_12x12_in_32x32(matrix32, pattern, &uc, &ur) >= 88){
  766. /// find precise circle
  767. /// move to the center of the pattern (+5.5)
  768. float xf = uc + 5.5f;
  769. float yf = ur + 5.5f;
  770. float radius = 5; ///< default radius
  771. const uint8_t iterations = 20;
  772. dynamic_circle(matrix32, xf, yf, radius, iterations);
  773. if (ABS(xf - (uc + 5.5f)) > 3 || ABS(yf - (ur + 5.5f)) > 3 || ABS(radius - 5) > 3){
  774. DBG(_n(" [%f %f][%f] mm divergence\n"), xf - (uc + 5.5f), yf - (ur + 5.5f), radius - 5);
  775. /// dynamic algorithm diverged, use original position instead
  776. xf = uc + 5.5f;
  777. yf = ur + 5.5f;
  778. }
  779. /// move to the center of area and convert to position
  780. xf = (float)x + (xf - 15.5f) * 64;
  781. yf = (float)y + (yf - 15.5f) * 64;
  782. DBG(_n(" [%f %f] mm pattern center\n"), pos_2_mm(xf), pos_2_mm(yf));
  783. x = round_to_i16(xf);
  784. y = round_to_i16(yf);
  785. xyzcal_lineXYZ_to(x, y, z, 200, 0);
  786. ret = true;
  787. }
  788. /// wipe buffer
  789. for (uint16_t i = 0; i < sizeof(block_t)*BLOCK_BUFFER_SIZE; i++)
  790. matrix32[i] = 0;
  791. return ret;
  792. }
  793. bool xyzcal_find_bed_induction_sensor_point_xy(void){
  794. bool ret = false;
  795. DBG(_n("xyzcal_find_bed_induction_sensor_point_xy x=%ld y=%ld z=%ld\n"), count_position[X_AXIS], count_position[Y_AXIS], count_position[Z_AXIS]);
  796. st_synchronize();
  797. pos_i16_t x = _X;
  798. pos_i16_t y = _Y;
  799. pos_i16_t z = _Z;
  800. uint8_t point = xyzcal_xycoords2point(x, y);
  801. x = pgm_read_word((uint16_t *)(xyzcal_point_xcoords + point));
  802. y = pgm_read_word((uint16_t *)(xyzcal_point_ycoords + point));
  803. DBG(_n("point=%d x=%d y=%d z=%d\n"), point, x, y, z);
  804. xyzcal_meassure_enter();
  805. xyzcal_lineXYZ_to(x, y, z, 200, 0);
  806. if (xyzcal_searchZ()){
  807. int16_t z = _Z;
  808. xyzcal_lineXYZ_to(x, y, z, 200, 0);
  809. ret = xyzcal_scan_and_process();
  810. }
  811. xyzcal_meassure_leave();
  812. return ret;
  813. }
  814. #endif //NEW_XYZCAL