xyzcal.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  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. #define X_PLUS 0
  25. #define X_MINUS 1
  26. #define Y_PLUS 0
  27. #define Y_MINUS 1
  28. #define Z_PLUS 0
  29. #define Z_MINUS 1
  30. #define _PI 3.14159265F
  31. /// \returns positive value always
  32. #define ABS(a) \
  33. ({ __typeof__ (a) _a = (a); \
  34. _a >= 0 ? _a : (-_a); })
  35. /// \returns maximum of the two
  36. #define MAX(a, b) \
  37. ({ __typeof__ (a) _a = (a); \
  38. __typeof__ (b) _b = (b); \
  39. _a >= _b ? _a : _b; })
  40. /// \returns minimum of the two
  41. #define MIN(a, b) \
  42. ({ __typeof__ (a) _a = (a); \
  43. __typeof__ (b) _b = (b); \
  44. _a <= _b ? _a : _b; })
  45. /// swap values
  46. #define SWAP(a, b) \
  47. ({ __typeof__ (a) c = (a); \
  48. a = (b); \
  49. b = c; })
  50. /// Saturates value
  51. /// \returns min if value is less than min
  52. /// \returns max if value is more than min
  53. /// \returns value otherwise
  54. #define CLAMP(value, min, max) \
  55. ({ __typeof__ (value) a_ = (value); \
  56. __typeof__ (min) min_ = (min); \
  57. __typeof__ (max) max_ = (max); \
  58. ( a_ < min_ ? min_ : (a_ <= max_ ? a_ : max_)); })
  59. /// position types
  60. typedef int16_t pos_i16_t;
  61. typedef long pos_i32_t;
  62. typedef float pos_mm_t;
  63. typedef int16_t usteps_t;
  64. uint8_t check_pinda_0();
  65. uint8_t check_pinda_1();
  66. void xyzcal_update_pos(uint16_t dx, uint16_t dy, uint16_t dz, uint16_t de);
  67. uint16_t xyzcal_calc_delay(uint16_t nd, uint16_t dd);
  68. uint8_t round_to_u8(float f){
  69. return (uint8_t)(f + .5f);
  70. }
  71. uint16_t round_to_u16(float f){
  72. return (uint16_t)(f + .5f);
  73. }
  74. int16_t round_to_i16(float f){
  75. return (int16_t)(f + .5f);
  76. }
  77. /// converts millimeters to integer position
  78. pos_i16_t mm_2_pos(pos_mm_t mm){
  79. return (pos_i16_t)(0.5f + mm * 100);
  80. }
  81. /// converts integer position to millimeters
  82. pos_mm_t pos_2_mm(pos_i16_t pos){
  83. return pos * 0.01f;
  84. }
  85. pos_mm_t pos_2_mm(float pos){
  86. return pos * 0.01f;
  87. }
  88. void xyzcal_meassure_enter(void)
  89. {
  90. DBG(_n("xyzcal_meassure_enter\n"));
  91. disable_heater();
  92. DISABLE_TEMPERATURE_INTERRUPT();
  93. #if (defined(FANCHECK) && defined(TACH_1) && (TACH_1 >-1))
  94. DISABLE_FANCHECK_INTERRUPT();
  95. #endif //(defined(FANCHECK) && defined(TACH_1) && (TACH_1 >-1))
  96. DISABLE_STEPPER_DRIVER_INTERRUPT();
  97. #ifdef WATCHDOG
  98. wdt_disable();
  99. #endif //WATCHDOG
  100. sm4_stop_cb = 0;
  101. sm4_update_pos_cb = xyzcal_update_pos;
  102. sm4_calc_delay_cb = xyzcal_calc_delay;
  103. }
  104. void xyzcal_meassure_leave(void)
  105. {
  106. DBG(_n("xyzcal_meassure_leave\n"));
  107. planner_abort_hard();
  108. ENABLE_TEMPERATURE_INTERRUPT();
  109. #if (defined(FANCHECK) && defined(TACH_1) && (TACH_1 >-1))
  110. ENABLE_FANCHECK_INTERRUPT();
  111. #endif //(defined(FANCHECK) && defined(TACH_1) && (TACH_1 >-1))
  112. ENABLE_STEPPER_DRIVER_INTERRUPT();
  113. #ifdef WATCHDOG
  114. wdt_enable(WDTO_4S);
  115. #endif //WATCHDOG
  116. sm4_stop_cb = 0;
  117. sm4_update_pos_cb = 0;
  118. sm4_calc_delay_cb = 0;
  119. }
  120. uint8_t check_pinda_0()
  121. {
  122. return _PINDA?0:1;
  123. }
  124. uint8_t check_pinda_1()
  125. {
  126. return _PINDA?1:0;
  127. }
  128. uint8_t xyzcal_dm = 0;
  129. void xyzcal_update_pos(uint16_t dx, uint16_t dy, uint16_t dz, uint16_t)
  130. {
  131. // DBG(_n("xyzcal_update_pos dx=%d dy=%d dz=%d dir=%02x\n"), dx, dy, dz, xyzcal_dm);
  132. if (xyzcal_dm&1) count_position[0] -= dx; else count_position[0] += dx;
  133. if (xyzcal_dm&2) count_position[1] -= dy; else count_position[1] += dy;
  134. if (xyzcal_dm&4) count_position[2] -= dz; else count_position[2] += dz;
  135. // DBG(_n(" after xyzcal_update_pos x=%ld y=%ld z=%ld\n"), count_position[0], count_position[1], count_position[2]);
  136. }
  137. uint16_t xyzcal_sm4_delay = 0;
  138. //#define SM4_ACCEL_TEST
  139. #ifdef SM4_ACCEL_TEST
  140. uint16_t xyzcal_sm4_v0 = 2000;
  141. uint16_t xyzcal_sm4_vm = 45000;
  142. uint16_t xyzcal_sm4_v = xyzcal_sm4_v0;
  143. uint16_t xyzcal_sm4_ac = 2000;
  144. uint16_t xyzcal_sm4_ac2 = (uint32_t)xyzcal_sm4_ac * 1024 / 10000;
  145. //float xyzcal_sm4_vm = 10000;
  146. #endif //SM4_ACCEL_TEST
  147. #ifdef SM4_ACCEL_TEST
  148. uint16_t xyzcal_calc_delay(uint16_t nd, uint16_t dd)
  149. {
  150. uint16_t del_us = 0;
  151. if (xyzcal_sm4_v & 0xf000) //>=4096
  152. {
  153. del_us = (uint16_t)62500 / (uint16_t)(xyzcal_sm4_v >> 4);
  154. xyzcal_sm4_v += (xyzcal_sm4_ac2 * del_us + 512) >> 10;
  155. if (xyzcal_sm4_v > xyzcal_sm4_vm) xyzcal_sm4_v = xyzcal_sm4_vm;
  156. if (del_us > 25) return del_us - 25;
  157. }
  158. else
  159. {
  160. del_us = (uint32_t)1000000 / xyzcal_sm4_v;
  161. xyzcal_sm4_v += ((uint32_t)xyzcal_sm4_ac2 * del_us + 512) >> 10;
  162. if (xyzcal_sm4_v > xyzcal_sm4_vm) xyzcal_sm4_v = xyzcal_sm4_vm;
  163. if (del_us > 50) return del_us - 50;
  164. }
  165. // uint16_t del_us = (uint16_t)(((float)1000000 / xyzcal_sm4_v) + 0.5);
  166. // uint16_t del_us = (uint32_t)1000000 / xyzcal_sm4_v;
  167. // uint16_t del_us = 100;
  168. // uint16_t del_us = (uint16_t)10000 / xyzcal_sm4_v;
  169. // v += (ac * del_us + 500) / 1000;
  170. // xyzcal_sm4_v += (xyzcal_sm4_ac * del_us) / 1000;
  171. // return xyzcal_sm4_delay;
  172. // DBG(_n("xyzcal_calc_delay nd=%d dd=%d v=%d del_us=%d\n"), nd, dd, xyzcal_sm4_v, del_us);
  173. return 0;
  174. }
  175. #else //SM4_ACCEL_TEST
  176. uint16_t xyzcal_calc_delay(uint16_t, uint16_t)
  177. {
  178. return xyzcal_sm4_delay;
  179. }
  180. #endif //SM4_ACCEL_TEST
  181. /// Moves printer to absolute position [x,y,z] defined in integer position system
  182. bool xyzcal_lineXYZ_to(int16_t x, int16_t y, int16_t z, uint16_t delay_us, int8_t check_pinda)
  183. {
  184. // DBG(_n("xyzcal_lineXYZ_to x=%d y=%d z=%d check=%d\n"), x, y, z, check_pinda);
  185. x -= (int16_t)count_position[0];
  186. y -= (int16_t)count_position[1];
  187. z -= (int16_t)count_position[2];
  188. xyzcal_dm = ((x<0)?1:0) | ((y<0)?2:0) | ((z<0)?4:0);
  189. sm4_set_dir_bits(xyzcal_dm);
  190. sm4_stop_cb = check_pinda?((check_pinda<0)?check_pinda_0:check_pinda_1):0;
  191. xyzcal_sm4_delay = delay_us;
  192. // uint32_t u = _micros();
  193. bool ret = sm4_line_xyze_ui(abs(x), abs(y), abs(z), 0) ? true : false;
  194. // u = _micros() - u;
  195. return ret;
  196. }
  197. /// Moves printer to absolute position [x,y,z] defined in millimeters
  198. 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){
  199. return xyzcal_lineXYZ_to(mm_2_pos(x), mm_2_pos(y), mm_2_pos(z), delay_us, check_pinda);
  200. }
  201. 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)
  202. {
  203. bool ret = false;
  204. float r = 0; //radius
  205. uint8_t n = 0; //point number
  206. uint16_t ad = 0; //angle [deg]
  207. float ar; //angle [rad]
  208. uint8_t dad = 0; //delta angle [deg]
  209. uint8_t dad_min = 4; //delta angle min [deg]
  210. uint8_t dad_max = 16; //delta angle max [deg]
  211. uint8_t k = 720 / (dad_max - dad_min); //delta calculation constant
  212. ad = 0;
  213. if (pad) ad = *pad % 720;
  214. DBG(_n("xyzcal_spiral2 cx=%d cy=%d z0=%d dz=%d radius=%d ad=%d\n"), cx, cy, z0, dz, radius, ad);
  215. // lcd_set_cursor(0, 4);
  216. // char text[10];
  217. // snprintf(text, 10, "%4d", z0);
  218. // lcd_print(text);
  219. for (; ad < 720; ad++)
  220. {
  221. if (radius > 0)
  222. {
  223. dad = dad_max - (ad / k);
  224. r = (float)(((uint32_t)ad) * radius) / 720;
  225. }
  226. else
  227. {
  228. dad = dad_max - ((719 - ad) / k);
  229. r = (float)(((uint32_t)(719 - ad)) * (-radius)) / 720;
  230. }
  231. ar = (ad + rotation)* (float)_PI / 180;
  232. float _cos = cos(ar);
  233. float _sin = sin(ar);
  234. int x = (int)(cx + (_cos * r));
  235. int y = (int)(cy + (_sin * r));
  236. int z = (int)(z0 - ((float)((int32_t)dz * ad) / 720));
  237. if (xyzcal_lineXYZ_to(x, y, z, delay_us, check_pinda))
  238. {
  239. ad += dad + 1;
  240. ret = true;
  241. break;
  242. }
  243. n++;
  244. ad += dad;
  245. }
  246. if (pad) *pad = ad;
  247. // if(ret){
  248. // lcd_set_cursor(0, 4);
  249. // lcd_print(" ");
  250. // }
  251. return ret;
  252. }
  253. 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)
  254. {
  255. bool ret = false;
  256. uint16_t ad = 0;
  257. if (pad) ad = *pad;
  258. DBG(_n("xyzcal_spiral8 cx=%d cy=%d z0=%d dz=%d radius=%d ad=%d\n"), cx, cy, z0, dz, radius, ad);
  259. if (!ret && (ad < 720))
  260. if ((ret = xyzcal_spiral2(cx, cy, z0 - 0*dz, dz, radius, 0, delay_us, check_pinda, &ad)) != 0)
  261. ad += 0;
  262. if (!ret && (ad < 1440))
  263. if ((ret = xyzcal_spiral2(cx, cy, z0 - 1*dz, dz, -radius, 0, delay_us, check_pinda, &ad)) != 0)
  264. ad += 720;
  265. if (!ret && (ad < 2160))
  266. if ((ret = xyzcal_spiral2(cx, cy, z0 - 2*dz, dz, radius, 180, delay_us, check_pinda, &ad)) != 0)
  267. ad += 1440;
  268. if (!ret && (ad < 2880))
  269. if ((ret = xyzcal_spiral2(cx, cy, z0 - 3*dz, dz, -radius, 180, delay_us, check_pinda, &ad)) != 0)
  270. ad += 2160;
  271. if (pad) *pad = ad;
  272. return ret;
  273. }
  274. #ifdef XYZCAL_MEASSURE_PINDA_HYSTEREZIS
  275. int8_t xyzcal_meassure_pinda_hysterezis(int16_t min_z, int16_t max_z, uint16_t delay_us, uint8_t samples)
  276. {
  277. DBG(_n("xyzcal_meassure_pinda_hysterezis\n"));
  278. int8_t ret = -1; // PINDA signal error
  279. int16_t z = _Z;
  280. int16_t sum_up = 0;
  281. int16_t sum_dn = 0;
  282. int16_t up;
  283. int16_t dn;
  284. uint8_t sample;
  285. xyzcal_lineXYZ_to(_X, _Y, min_z, delay_us, 1);
  286. xyzcal_lineXYZ_to(_X, _Y, max_z, delay_us, -1);
  287. if (!_PINDA)
  288. {
  289. for (sample = 0; sample < samples; sample++)
  290. {
  291. dn = _Z;
  292. if (!xyzcal_lineXYZ_to(_X, _Y, min_z, delay_us, 1)) break;
  293. dn = dn - _Z;
  294. up = _Z;
  295. if (!xyzcal_lineXYZ_to(_X, _Y, max_z, delay_us, -1)) break;
  296. up = _Z - up;
  297. DBG(_n("%d. up=%d dn=%d\n"), sample, up, dn);
  298. sum_up += up;
  299. sum_dn += dn;
  300. if (abs(up - dn) > XYZCAL_PINDA_HYST_DIF)
  301. {
  302. ret = -2; // difference between up-dn to high
  303. break;
  304. }
  305. }
  306. if (sample == samples)
  307. {
  308. up = sum_up / samples;
  309. dn = sum_dn / samples;
  310. uint16_t hyst = (up + dn) / 2;
  311. if (abs(up - dn) > XYZCAL_PINDA_HYST_DIF)
  312. ret = -2; // difference between up-dn to high
  313. else if ((hyst < XYZCAL_PINDA_HYST_MIN) || (hyst > XYZCAL_PINDA_HYST_MAX))
  314. ret = -3; // hysterezis out of range
  315. else
  316. ret = hyst;
  317. }
  318. }
  319. xyzcal_lineXYZ_to(_X, _Y, z, delay_us, 0);
  320. return ret;
  321. }
  322. #endif //XYZCAL_MEASSURE_PINDA_HYSTEREZIS
  323. 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){
  324. if(!pixels)
  325. return;
  326. int16_t z = _Z;
  327. uint16_t line_buffer[32];
  328. xyzcal_lineXYZ_to(cx, cy, z, delay_us, 0);
  329. for (uint8_t r = 0; r < 32; r++){ ///< Y axis
  330. xyzcal_lineXYZ_to(_X, cy - 1024 + r * 64, z, delay_us, 0);
  331. for (int8_t d = 0; d < 2; ++d){ ///< direction
  332. // xyzcal_lineXYZ_to((d & 1) ? (cx + 1024) : (cx - 1024), _Y, z, 2 * delay_us, 0);
  333. // xyzcal_lineXYZ_to(_X, _Y, min_z, delay_us, 1);
  334. // xyzcal_lineXYZ_to(_X, _Y, max_z, delay_us, -1);
  335. xyzcal_lineXYZ_to((d & 1) ? (cx + 1024) : (cx - 1024), _Y, min_z, delay_us, 0);
  336. z = _Z;
  337. sm4_set_dir(X_AXIS, d);
  338. for (uint8_t c = 0; c < 32; c++){ ///< X axis
  339. /// move up to un-trigger (surpress hysteresis)
  340. sm4_set_dir(Z_AXIS, Z_PLUS);
  341. while (z < max_z && _PINDA){
  342. sm4_do_step(Z_AXIS_MASK);
  343. delayMicroseconds(delay_us);
  344. z++;
  345. }
  346. int16_t last_top_z = z;
  347. /// move down to trigger
  348. sm4_set_dir(Z_AXIS, Z_MINUS);
  349. while (z > min_z && !_PINDA){
  350. sm4_do_step(Z_AXIS_MASK);
  351. delayMicroseconds(delay_us);
  352. z--;
  353. }
  354. count_position[2] = z;
  355. if (d == 0){
  356. line_buffer[c] = (uint16_t)(z - min_z);
  357. } else {
  358. /// data reversed in X
  359. // DBG(_n("%04x"), (line_buffer[31 - c] + (z - min_z)) / 2);
  360. /// save average of both directions
  361. pixels[(uint16_t)r * 32 + (31 - c)] = (uint8_t)MIN((uint32_t)255, ((uint32_t)line_buffer[31 - c] + (z - min_z)) / 2);
  362. }
  363. /// move to the next point
  364. xyzcal_lineXYZ_to(((d & 1) ? 1 : -1) * (64 * (16 - c) - 32) + cx, _Y, (last_top_z + z) / 2, delay_us, 0);
  365. z = _Z;
  366. }
  367. }
  368. // DBG(_n("\n\n"));
  369. }
  370. }
  371. /// Returns rate of match
  372. /// max match = 132, min match = 0
  373. uint8_t xyzcal_match_pattern_12x12_in_32x32(uint16_t* pattern, uint8_t* pixels, uint8_t c, uint8_t r){
  374. uint8_t thr = 16;
  375. uint8_t match = 0;
  376. for (uint8_t i = 0; i < 12; ++i){
  377. for (uint8_t j = 0; j < 12; ++j){
  378. /// skip corners (3 pixels in each)
  379. if (((i == 0) || (i == 11)) && ((j < 2) || (j >= 10))) continue;
  380. if (((j == 0) || (j == 11)) && ((i < 2) || (i >= 10))) continue;
  381. const uint16_t idx = (c + j) + 32 * ((uint16_t)r + i);
  382. const bool high_pix = pixels[idx] > thr;
  383. const bool high_pat = pattern[i] & (1 << j);
  384. if (high_pix == high_pat)
  385. match++;
  386. }
  387. }
  388. return match;
  389. }
  390. /// Searches for best match of pattern by shifting it
  391. /// Returns rate of match and the best location
  392. /// max match = 132, min match = 0
  393. uint8_t xyzcal_find_pattern_12x12_in_32x32(uint8_t* pixels, uint16_t* pattern, uint8_t* pc, uint8_t* pr){
  394. if (!pixels || !pattern || !pc || !pr)
  395. return -1;
  396. uint8_t max_c = 0;
  397. uint8_t max_r = 0;
  398. uint8_t max_match = 0;
  399. // DBG(_n("Matching:\n"));
  400. /// pixel precision
  401. for (uint8_t r = 0; r < (32 - 12); ++r){
  402. for (uint8_t c = 0; c < (32 - 12); ++c){
  403. const uint8_t match = xyzcal_match_pattern_12x12_in_32x32(pattern, pixels, c, r);
  404. if (max_match < match){
  405. max_c = c;
  406. max_r = r;
  407. max_match = match;
  408. }
  409. // DBG(_n("%d "), match);
  410. }
  411. // DBG(_n("\n"));
  412. }
  413. DBG(_n("max_c=%f max_r=%f max_match=%d pixel\n"), max_c, max_r, max_match);
  414. *pc = max_c;
  415. *pr = max_r;
  416. return max_match;
  417. }
  418. uint8_t xyzcal_xycoords2point(int16_t x, int16_t y)
  419. {
  420. uint8_t ix = (x > 10000)?1:0;
  421. uint8_t iy = (y > 10000)?1:0;
  422. return iy?(3-ix):ix;
  423. }
  424. //MK3
  425. #if ((MOTHERBOARD == BOARD_EINSY_1_0a))
  426. const int16_t xyzcal_point_xcoords[4] PROGMEM = {1200, 22000, 22000, 1200};
  427. const int16_t xyzcal_point_ycoords[4] PROGMEM = {600, 600, 19800, 19800};
  428. #endif //((MOTHERBOARD == BOARD_EINSY_1_0a))
  429. //MK2.5
  430. #if ((MOTHERBOARD == BOARD_RAMBO_MINI_1_0) || (MOTHERBOARD == BOARD_RAMBO_MINI_1_3))
  431. const int16_t xyzcal_point_xcoords[4] PROGMEM = {1200, 22000, 22000, 1200};
  432. const int16_t xyzcal_point_ycoords[4] PROGMEM = {700, 700, 19800, 19800};
  433. #endif //((MOTHERBOARD == BOARD_RAMBO_MINI_1_0) || (MOTHERBOARD == BOARD_RAMBO_MINI_1_3))
  434. const uint16_t xyzcal_point_pattern[12] PROGMEM = {0x000, 0x0f0, 0x1f8, 0x3fc, 0x7fe, 0x7fe, 0x7fe, 0x7fe, 0x3fc, 0x1f8, 0x0f0, 0x000};
  435. bool xyzcal_searchZ(void)
  436. {
  437. DBG(_n("xyzcal_searchZ x=%ld y=%ld z=%ld\n"), count_position[X_AXIS], count_position[Y_AXIS], count_position[Z_AXIS]);
  438. int16_t x0 = _X;
  439. int16_t y0 = _Y;
  440. int16_t z0 = _Z;
  441. // int16_t min_z = -6000;
  442. // int16_t dz = 100;
  443. int16_t z = z0;
  444. while (z > -2300) //-6mm + 0.25mm
  445. {
  446. uint16_t ad = 0;
  447. if (xyzcal_spiral8(x0, y0, z, 100, 900, 320, 1, &ad)) //dz=100 radius=900 delay=400
  448. {
  449. int16_t x_on = _X;
  450. int16_t y_on = _Y;
  451. int16_t z_on = _Z;
  452. DBG(_n(" ON-SIGNAL at x=%d y=%d z=%d ad=%d\n"), x_on, y_on, z_on, ad);
  453. return true;
  454. }
  455. z -= 400;
  456. }
  457. 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]);
  458. return false;
  459. }
  460. /// returns value of any location within data
  461. /// uses bilinear interpolation
  462. float get_value(uint8_t * matrix_32x32, float c, float r){
  463. if (c <= 0 || r <= 0 || c >= 31 || r >= 31)
  464. return 0;
  465. /// calculate weights of nearby points
  466. const float wc1 = c - floor(c);
  467. const float wr1 = r - floor(r);
  468. const float wc0 = 1 - wc1;
  469. const float wr0 = 1 - wr1;
  470. const float w00 = wc0 * wr0;
  471. const float w01 = wc0 * wr1;
  472. const float w10 = wc1 * wr0;
  473. const float w11 = wc1 * wr1;
  474. const uint16_t c0 = c;
  475. const uint16_t c1 = c0 + 1;
  476. const uint16_t r0 = r;
  477. const uint16_t r1 = r0 + 1;
  478. const uint16_t idx00 = c0 + 32 * r0;
  479. const uint16_t idx01 = c0 + 32 * r1;
  480. const uint16_t idx10 = c1 + 32 * r0;
  481. const uint16_t idx11 = c1 + 32 * r1;
  482. /// bilinear resampling
  483. return w00 * matrix_32x32[idx00] + w01 * matrix_32x32[idx01] + w10 * matrix_32x32[idx10] + w11 * matrix_32x32[idx11];
  484. }
  485. const constexpr float m_infinity = -1000.f;
  486. /// replaces the highest number by m_infinity
  487. void remove_highest(float *points, const uint8_t num_points){
  488. if (num_points <= 0)
  489. return;
  490. float max = points[0];
  491. uint8_t max_i = 0;
  492. for (uint8_t i = 0; i < num_points; ++i){
  493. if (max < points[i]){
  494. max = points[i];
  495. max_i = i;
  496. }
  497. }
  498. points[max_i] = m_infinity;
  499. }
  500. /// return the highest number in the list
  501. float highest(float *points, const uint8_t num_points){
  502. if (num_points <= 0)
  503. return 0;
  504. float max = points[0];
  505. for (uint8_t i = 0; i < num_points; ++i){
  506. if (max < points[i]){
  507. max = points[i];
  508. }
  509. }
  510. return max;
  511. }
  512. /// Searches for circle iteratively
  513. /// Uses points on the perimeter. If point is high it pushes circle out of the center (shift or change of radius),
  514. /// otherwise to the center.
  515. /// Algorithm is stopped after fixed number of iterations. Move is limited to 0.5 px per iteration.
  516. void dynamic_circle(uint8_t *matrix_32x32, float &x, float &y, float &r, uint8_t iterations){
  517. /// circle of 10.5 diameter has 33 in circumference, don't go much above
  518. const constexpr uint8_t num_points = 33;
  519. float points[num_points];
  520. float pi_2_div_num_points = 2 * M_PI / num_points;
  521. const constexpr uint8_t target_z = 32; ///< target z height of the circle
  522. float norm;
  523. float angle;
  524. float max_val = 0.5f;
  525. const uint8_t blocks = 7;
  526. float shifts_x[blocks];
  527. float shifts_y[blocks];
  528. float shifts_r[blocks];
  529. for (int8_t i = iterations; i > 0; --i){
  530. // DBG(_n(" [%f, %f][%f] circle\n"), x, y, r);
  531. /// read points on the circle
  532. for (uint8_t p = 0; p < num_points; ++p){
  533. angle = p * pi_2_div_num_points;
  534. points[p] = get_value(matrix_32x32, r * cos(angle) + x, r * sin(angle) + y) - target_z;
  535. // DBG(_n("%f "), points[p]);
  536. }
  537. // DBG(_n(" points\n"));
  538. /// sum blocks
  539. for (uint8_t j = 0; j < blocks; ++j){
  540. shifts_x[j] = shifts_y[j] = shifts_r[j] = 0;
  541. /// first part
  542. for (uint8_t p = 0; p < num_points * 3 / 4; ++p){
  543. uint8_t idx = (p + j * num_points / blocks) % num_points;
  544. angle = idx * pi_2_div_num_points;
  545. shifts_x[j] += cos(angle) * points[idx];
  546. shifts_y[j] += sin(angle) * points[idx];
  547. shifts_r[j] += points[idx];
  548. }
  549. }
  550. /// remove extreme values (slow but simple)
  551. for (uint8_t j = 0; j < blocks / 2; ++j){
  552. remove_highest(shifts_x, blocks);
  553. remove_highest(shifts_y, blocks);
  554. remove_highest(shifts_r, blocks);
  555. }
  556. /// median is the highest now
  557. norm = 1.f / (32.f * (num_points * 3 / 4));
  558. x += CLAMP(highest(shifts_x, blocks) * norm, -max_val, max_val);
  559. y += CLAMP(highest(shifts_y, blocks) * norm, -max_val, max_val);
  560. r += CLAMP(highest(shifts_r, blocks) * norm, -max_val, max_val);
  561. r = MAX(2, r);
  562. }
  563. DBG(_n(" [%f, %f][%f] final circle\n"), x, y, r);
  564. }
  565. /// Prints matrix in hex to debug output (serial line)
  566. void print_image(uint8_t *matrix_32x32){
  567. for (uint8_t y = 0; y < 32; ++y){
  568. const uint16_t idx_y = y * 32;
  569. for (uint8_t x = 0; x < 32; ++x){
  570. DBG(_n("%02x"), matrix_32x32[idx_y + x]);
  571. }
  572. DBG(_n("\n"));
  573. }
  574. DBG(_n("\n"));
  575. }
  576. /// scans area around the current head location and
  577. /// searches for the center of the calibration pin
  578. bool xyzcal_scan_and_process(void){
  579. DBG(_n("sizeof(block_buffer)=%d\n"), sizeof(block_t)*BLOCK_BUFFER_SIZE);
  580. bool ret = false;
  581. int16_t x = _X;
  582. int16_t y = _Y;
  583. int16_t z = _Z;
  584. uint8_t *matrix32 = (uint8_t *)block_buffer;
  585. uint16_t *pattern = (uint16_t *)(matrix32 + 32 * 32);
  586. xyzcal_scan_pixels_32x32_Zhop(x, y, z - 72, 2400, 300, matrix32);
  587. print_image(matrix32);
  588. for (uint8_t i = 0; i < 12; i++){
  589. pattern[i] = pgm_read_word((uint16_t*)(xyzcal_point_pattern + i));
  590. // DBG(_n(" pattern[%d]=%d\n"), i, pattern[i]);
  591. }
  592. /// SEARCH FOR BINARY CIRCLE
  593. uint8_t uc = 0;
  594. uint8_t ur = 0;
  595. /// max match = 132, 1/2 good = 66, 2/3 good = 88
  596. if (xyzcal_find_pattern_12x12_in_32x32(matrix32, pattern, &uc, &ur) >= 88){
  597. /// find precise circle
  598. /// move to the center of the pattern (+5.5)
  599. float xf = uc + 5.5f;
  600. float yf = ur + 5.5f;
  601. float radius = 5; ///< default radius
  602. const uint8_t iterations = 20;
  603. dynamic_circle(matrix32, xf, yf, radius, iterations);
  604. if (ABS(xf - uc + 5.5f) > 3 || ABS(yf - ur + 5.5f) > 3 || ABS(radius - 5) > 3){
  605. /// dynamic algorithm diverged, use original position instead
  606. xf = uc + 5.5f;
  607. yf = ur + 5.5f;
  608. }
  609. /// move to the center of area and convert to position
  610. xf = (float)x + (xf - 15.5f) * 64;
  611. yf = (float)y + (yf - 15.5f) * 64;
  612. DBG(_n(" [%f %f] mm pattern center\n"), pos_2_mm(xf), pos_2_mm(yf));
  613. x = round_to_i16(xf);
  614. y = round_to_i16(yf);
  615. xyzcal_lineXYZ_to(x, y, z, 200, 0);
  616. ret = true;
  617. }
  618. /// wipe buffer
  619. for (uint16_t i = 0; i < sizeof(block_t)*BLOCK_BUFFER_SIZE; i++)
  620. matrix32[i] = 0;
  621. return ret;
  622. }
  623. bool xyzcal_find_bed_induction_sensor_point_xy(void){
  624. bool ret = false;
  625. 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]);
  626. st_synchronize();
  627. pos_i16_t x = _X;
  628. pos_i16_t y = _Y;
  629. pos_i16_t z = _Z;
  630. uint8_t point = xyzcal_xycoords2point(x, y);
  631. x = pgm_read_word((uint16_t *)(xyzcal_point_xcoords + point));
  632. y = pgm_read_word((uint16_t *)(xyzcal_point_ycoords + point));
  633. DBG(_n("point=%d x=%d y=%d z=%d\n"), point, x, y, z);
  634. xyzcal_meassure_enter();
  635. xyzcal_lineXYZ_to(x, y, z, 200, 0);
  636. if (xyzcal_searchZ()){
  637. int16_t z = _Z;
  638. xyzcal_lineXYZ_to(x, y, z, 200, 0);
  639. ret = xyzcal_scan_and_process();
  640. }
  641. xyzcal_meassure_leave();
  642. return ret;
  643. }
  644. #endif //NEW_XYZCAL