xyzcal.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  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. #define _n PSTR
  18. #define _X ((int16_t)count_position[X_AXIS])
  19. #define _Y ((int16_t)count_position[Y_AXIS])
  20. #define _Z ((int16_t)count_position[Z_AXIS])
  21. #define _E ((int16_t)count_position[E_AXIS])
  22. #define _PI 3.14159265F
  23. extern long count_position[NUM_AXIS];
  24. uint8_t check_pinda_0();
  25. uint8_t check_pinda_1();
  26. void xyzcal_update_pos(uint16_t dx, uint16_t dy, uint16_t dz, uint16_t de);
  27. uint16_t xyzcal_calc_delay(uint16_t nd, uint16_t dd);
  28. void xyzcal_meassure_enter(void)
  29. {
  30. DBG(_n("xyzcal_meassure_enter\n"));
  31. disable_heater();
  32. DISABLE_TEMPERATURE_INTERRUPT();
  33. #if (defined(FANCHECK) && defined(TACH_1) && (TACH_1 >-1))
  34. DISABLE_FANCHECK_INTERRUPT();
  35. #endif //(defined(FANCHECK) && defined(TACH_1) && (TACH_1 >-1))
  36. DISABLE_STEPPER_DRIVER_INTERRUPT();
  37. #ifdef WATCHDOG
  38. wdt_disable();
  39. #endif //WATCHDOG
  40. sm4_stop_cb = 0;
  41. sm4_update_pos_cb = xyzcal_update_pos;
  42. sm4_calc_delay_cb = xyzcal_calc_delay;
  43. }
  44. void xyzcal_meassure_leave(void)
  45. {
  46. DBG(_n("xyzcal_meassure_leave\n"));
  47. planner_abort_hard();
  48. ENABLE_TEMPERATURE_INTERRUPT();
  49. #if (defined(FANCHECK) && defined(TACH_1) && (TACH_1 >-1))
  50. ENABLE_FANCHECK_INTERRUPT();
  51. #endif //(defined(FANCHECK) && defined(TACH_1) && (TACH_1 >-1))
  52. ENABLE_STEPPER_DRIVER_INTERRUPT();
  53. #ifdef WATCHDOG
  54. wdt_enable(WDTO_4S);
  55. #endif //WATCHDOG
  56. sm4_stop_cb = 0;
  57. sm4_update_pos_cb = 0;
  58. sm4_calc_delay_cb = 0;
  59. }
  60. uint8_t check_pinda_0()
  61. {
  62. return _PINDA?0:1;
  63. }
  64. uint8_t check_pinda_1()
  65. {
  66. return _PINDA?1:0;
  67. }
  68. uint8_t xyzcal_dm = 0;
  69. void xyzcal_update_pos(uint16_t dx, uint16_t dy, uint16_t dz, uint16_t de)
  70. {
  71. // DBG(_n("xyzcal_update_pos dx=%d dy=%d dz=%d dir=%02x\n"), dx, dy, dz, xyzcal_dm);
  72. if (xyzcal_dm&1) count_position[0] -= dx; else count_position[0] += dx;
  73. if (xyzcal_dm&2) count_position[1] -= dy; else count_position[1] += dy;
  74. if (xyzcal_dm&4) count_position[2] -= dz; else count_position[2] += dz;
  75. // DBG(_n(" after xyzcal_update_pos x=%ld y=%ld z=%ld\n"), count_position[0], count_position[1], count_position[2]);
  76. }
  77. uint16_t xyzcal_sm4_delay = 0;
  78. //#define SM4_ACCEL_TEST
  79. #ifdef SM4_ACCEL_TEST
  80. uint16_t xyzcal_sm4_v0 = 2000;
  81. uint16_t xyzcal_sm4_vm = 45000;
  82. uint16_t xyzcal_sm4_v = xyzcal_sm4_v0;
  83. uint16_t xyzcal_sm4_ac = 2000;
  84. uint16_t xyzcal_sm4_ac2 = (uint32_t)xyzcal_sm4_ac * 1024 / 10000;
  85. //float xyzcal_sm4_vm = 10000;
  86. #endif //SM4_ACCEL_TEST
  87. uint16_t xyzcal_calc_delay(uint16_t nd, uint16_t dd)
  88. {
  89. return xyzcal_sm4_delay;
  90. #ifdef SM4_ACCEL_TEST
  91. uint16_t del_us = 0;
  92. if (xyzcal_sm4_v & 0xf000) //>=4096
  93. {
  94. del_us = (uint16_t)62500 / (uint16_t)(xyzcal_sm4_v >> 4);
  95. xyzcal_sm4_v += (xyzcal_sm4_ac2 * del_us + 512) >> 10;
  96. if (xyzcal_sm4_v > xyzcal_sm4_vm) xyzcal_sm4_v = xyzcal_sm4_vm;
  97. if (del_us > 25) return del_us - 25;
  98. }
  99. else
  100. {
  101. del_us = (uint32_t)1000000 / xyzcal_sm4_v;
  102. xyzcal_sm4_v += ((uint32_t)xyzcal_sm4_ac2 * del_us + 512) >> 10;
  103. if (xyzcal_sm4_v > xyzcal_sm4_vm) xyzcal_sm4_v = xyzcal_sm4_vm;
  104. if (del_us > 50) return del_us - 50;
  105. }
  106. // uint16_t del_us = (uint16_t)(((float)1000000 / xyzcal_sm4_v) + 0.5);
  107. // uint16_t del_us = (uint32_t)1000000 / xyzcal_sm4_v;
  108. // uint16_t del_us = 100;
  109. // uint16_t del_us = (uint16_t)10000 / xyzcal_sm4_v;
  110. // v += (ac * del_us + 500) / 1000;
  111. // xyzcal_sm4_v += (xyzcal_sm4_ac * del_us) / 1000;
  112. // return xyzcal_sm4_delay;
  113. // DBG(_n("xyzcal_calc_delay nd=%d dd=%d v=%d del_us=%d\n"), nd, dd, xyzcal_sm4_v, del_us);
  114. return 0;
  115. #endif //SM4_ACCEL_TEST
  116. }
  117. bool xyzcal_lineXYZ_to(int16_t x, int16_t y, int16_t z, uint16_t delay_us, int8_t check_pinda)
  118. {
  119. // DBG(_n("xyzcal_lineXYZ_to x=%d y=%d z=%d check=%d\n"), x, y, z, check_pinda);
  120. x -= (int16_t)count_position[0];
  121. y -= (int16_t)count_position[1];
  122. z -= (int16_t)count_position[2];
  123. xyzcal_dm = ((x<0)?1:0) | ((y<0)?2:0) | ((z<0)?4:0);
  124. sm4_set_dir_bits(xyzcal_dm);
  125. sm4_stop_cb = check_pinda?((check_pinda<0)?check_pinda_0:check_pinda_1):0;
  126. xyzcal_sm4_delay = delay_us;
  127. // uint32_t u = micros();
  128. bool ret = sm4_line_xyze_ui(abs(x), abs(y), abs(z), 0)?true:false;
  129. // u = micros() - u;
  130. return ret;
  131. }
  132. 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)
  133. {
  134. bool ret = false;
  135. float r = 0; //radius
  136. uint8_t n = 0; //point number
  137. uint16_t ad = 0; //angle [deg]
  138. float ar; //angle [rad]
  139. uint8_t dad = 0; //delta angle [deg]
  140. uint8_t dad_min = 4; //delta angle min [deg]
  141. uint8_t dad_max = 16; //delta angle max [deg]
  142. uint8_t k = 720 / (dad_max - dad_min); //delta calculation constant
  143. ad = 0;
  144. if (pad) ad = *pad % 720;
  145. DBG(_n("xyzcal_spiral2 cx=%d cy=%d z0=%d dz=%d radius=%d ad=%d\n"), cx, cy, z0, dz, radius, ad);
  146. for (; ad < 720; ad++)
  147. {
  148. if (radius > 0)
  149. {
  150. dad = dad_max - (ad / k);
  151. r = (float)(((uint32_t)ad) * radius) / 720;
  152. }
  153. else
  154. {
  155. dad = dad_max - ((719 - ad) / k);
  156. r = (float)(((uint32_t)(719 - ad)) * (-radius)) / 720;
  157. }
  158. ar = (ad + rotation)* (float)_PI / 180;
  159. float _cos = cos(ar);
  160. float _sin = sin(ar);
  161. int x = (int)(cx + (_cos * r));
  162. int y = (int)(cy + (_sin * r));
  163. int z = (int)(z0 - ((float)((int32_t)dz * ad) / 720));
  164. if (xyzcal_lineXYZ_to(x, y, z, delay_us, check_pinda))
  165. {
  166. ad += dad + 1;
  167. ret = true;
  168. break;
  169. }
  170. n++;
  171. ad += dad;
  172. }
  173. if (pad) *pad = ad;
  174. return ret;
  175. }
  176. 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)
  177. {
  178. bool ret = false;
  179. uint16_t ad = 0;
  180. if (pad) ad = *pad;
  181. DBG(_n("xyzcal_spiral8 cx=%d cy=%d z0=%d dz=%d radius=%d ad=%d\n"), cx, cy, z0, dz, radius, ad);
  182. if (!ret && (ad < 720))
  183. if (ret = xyzcal_spiral2(cx, cy, z0 - 0*dz, dz, radius, 0, delay_us, check_pinda, &ad))
  184. ad += 0;
  185. if (!ret && (ad < 1440))
  186. if (ret = xyzcal_spiral2(cx, cy, z0 - 1*dz, dz, -radius, 0, delay_us, check_pinda, &ad))
  187. ad += 720;
  188. if (!ret && (ad < 2160))
  189. if (ret = xyzcal_spiral2(cx, cy, z0 - 2*dz, dz, radius, 180, delay_us, check_pinda, &ad))
  190. ad += 1440;
  191. if (!ret && (ad < 2880))
  192. if (ret = xyzcal_spiral2(cx, cy, z0 - 3*dz, dz, -radius, 180, delay_us, check_pinda, &ad))
  193. ad += 2160;
  194. if (pad) *pad = ad;
  195. return ret;
  196. }
  197. #ifdef XYZCAL_MEASSURE_PINDA_HYSTEREZIS
  198. int8_t xyzcal_meassure_pinda_hysterezis(int16_t min_z, int16_t max_z, uint16_t delay_us, uint8_t samples)
  199. {
  200. DBG(_n("xyzcal_meassure_pinda_hysterezis\n"));
  201. int8_t ret = -1; // PINDA signal error
  202. int16_t z = _Z;
  203. int16_t sum_up = 0;
  204. int16_t sum_dn = 0;
  205. int16_t up;
  206. int16_t dn;
  207. uint8_t sample;
  208. xyzcal_lineXYZ_to(_X, _Y, min_z, delay_us, 1);
  209. xyzcal_lineXYZ_to(_X, _Y, max_z, delay_us, -1);
  210. if (!_PINDA)
  211. {
  212. for (sample = 0; sample < samples; sample++)
  213. {
  214. dn = _Z;
  215. if (!xyzcal_lineXYZ_to(_X, _Y, min_z, delay_us, 1)) break;
  216. dn = dn - _Z;
  217. up = _Z;
  218. if (!xyzcal_lineXYZ_to(_X, _Y, max_z, delay_us, -1)) break;
  219. up = _Z - up;
  220. DBG(_n("%d. up=%d dn=%d\n"), sample, up, dn);
  221. sum_up += up;
  222. sum_dn += dn;
  223. if (abs(up - dn) > XYZCAL_PINDA_HYST_DIF)
  224. {
  225. ret = -2; // difference between up-dn to high
  226. break;
  227. }
  228. }
  229. if (sample == samples)
  230. {
  231. up = sum_up / samples;
  232. dn = sum_dn / samples;
  233. uint16_t hyst = (up + dn) / 2;
  234. if (abs(up - dn) > XYZCAL_PINDA_HYST_DIF)
  235. ret = -2; // difference between up-dn to high
  236. else if ((hyst < XYZCAL_PINDA_HYST_MIN) || (hyst > XYZCAL_PINDA_HYST_MAX))
  237. ret = -3; // hysterezis out of range
  238. else
  239. ret = hyst;
  240. }
  241. }
  242. xyzcal_lineXYZ_to(_X, _Y, z, delay_us, 0);
  243. return ret;
  244. }
  245. #endif //XYZCAL_MEASSURE_PINDA_HYSTEREZIS
  246. void xyzcal_scan_pixels_32x32(int16_t cx, int16_t cy, int16_t min_z, int16_t max_z, uint16_t delay_us, uint8_t* pixels)
  247. {
  248. DBG(_n("xyzcal_scan_pixels_32x32 cx=%d cy=%d min_z=%d max_z=%d\n"), cx, cy, min_z, max_z);
  249. // xyzcal_lineXYZ_to(cx - 1024, cy - 1024, max_z, 2*delay_us, 0);
  250. // xyzcal_lineXYZ_to(cx, cy, max_z, delay_us, 0);
  251. int16_t z = (int16_t)count_position[2];
  252. xyzcal_lineXYZ_to(cx, cy, z, 2*delay_us, 0);
  253. for (uint8_t r = 0; r < 32; r++)
  254. {
  255. int8_t _pinda = _PINDA;
  256. xyzcal_lineXYZ_to((r&1)?(cx+1024):(cx-1024), cy - 1024 + r*64, z, 2*delay_us, 0);
  257. xyzcal_lineXYZ_to(_X, _Y, min_z, delay_us, 1);
  258. xyzcal_lineXYZ_to(_X, _Y, max_z, delay_us, -1);
  259. z = (int16_t)count_position[2];
  260. sm4_set_dir(X_AXIS, (r&1)?1:0);
  261. for (uint8_t c = 0; c < 32; c++)
  262. {
  263. uint16_t sum = 0;
  264. int16_t z_sum = 0;
  265. for (uint8_t i = 0; i < 64; i++)
  266. {
  267. int8_t pinda = _PINDA;
  268. int16_t pix = z - min_z;
  269. pix += (pinda)?23:-24;
  270. if (pix < 0) pix = 0;
  271. if (pix > 255) pix = 255;
  272. sum += pix;
  273. z_sum += z;
  274. // if (_pinda != pinda)
  275. // {
  276. // if (pinda)
  277. // DBG(_n("!1 x=%d z=%d\n"), c*64+i, z+23);
  278. // else
  279. // DBG(_n("!0 x=%d z=%d\n"), c*64+i, z-24);
  280. // }
  281. sm4_set_dir(Z_AXIS, !pinda);
  282. if (!pinda)
  283. {
  284. if (z > min_z)
  285. {
  286. sm4_do_step(Z_AXIS_MASK);
  287. z--;
  288. }
  289. }
  290. else
  291. {
  292. if (z < max_z)
  293. {
  294. sm4_do_step(Z_AXIS_MASK);
  295. z++;
  296. }
  297. }
  298. sm4_do_step(X_AXIS_MASK);
  299. delayMicroseconds(600);
  300. _pinda = pinda;
  301. }
  302. sum >>= 6; //div 64
  303. if (z_sum < 0)
  304. {
  305. z_sum = -z_sum;
  306. z_sum >>= 6; //div 64
  307. z_sum = -z_sum;
  308. }
  309. else
  310. z_sum >>= 6; //div 64
  311. if (pixels) pixels[((uint16_t)r<<5) + ((r&1)?(31-c):c)] = sum;
  312. // DBG(_n("c=%d r=%d l=%d z=%d\n"), c, r, sum, z_sum);
  313. count_position[0] += (r&1)?-64:64;
  314. count_position[2] = z;
  315. }
  316. if (pixels)
  317. for (uint8_t c = 0; c < 32; c++)
  318. DBG(_n("%02x"), pixels[((uint16_t)r<<5) + c]);
  319. DBG(_n("\n"));
  320. }
  321. // xyzcal_lineXYZ_to(cx, cy, z, 2*delay_us, 0);
  322. }
  323. void xyzcal_histo_pixels_32x32(uint8_t* pixels, uint16_t* histo)
  324. {
  325. for (uint8_t l = 0; l < 16; l++)
  326. histo[l] = 0;
  327. for (uint8_t r = 0; r < 32; r++)
  328. for (uint8_t c = 0; c < 32; c++)
  329. {
  330. uint8_t pix = pixels[((uint16_t)r<<5) + c];
  331. histo[pix >> 4]++;
  332. }
  333. for (uint8_t l = 0; l < 16; l++)
  334. DBG(_n(" %2d %d\n"), l, histo[l]);
  335. }
  336. void xyzcal_adjust_pixels(uint8_t* pixels, uint16_t* histo)
  337. {
  338. uint8_t l;
  339. uint16_t max_c = histo[0];
  340. uint8_t max_l = 0;
  341. for (l = 1; l < 16; l++)
  342. {
  343. uint16_t c = histo[l];
  344. if (c > max_c)
  345. {
  346. max_c = c;
  347. max_l = l;
  348. }
  349. }
  350. DBG(_n("max_c=%2d max_l=%d\n"), max_c, max_l);
  351. for (l = 15; l > 8; l--)
  352. if (histo[l] >= 10)
  353. break;
  354. uint8_t pix_min = (max_l + 2) << 4;
  355. uint8_t pix_max = l << 4;
  356. uint8_t pix_dif = pix_max - pix_min;
  357. DBG(_n(" min=%d max=%d dif=%d\n"), pix_min, pix_max, pix_dif);
  358. for (int16_t i = 0; i < 32*32; i++)
  359. {
  360. uint16_t pix = pixels[i];
  361. if (pix > pix_min) pix -= pix_min;
  362. else pix = 0;
  363. pix <<= 8;
  364. pix /= pix_dif;
  365. // if (pix < 0) pix = 0;
  366. if (pix > 255) pix = 255;
  367. pixels[i] = (uint8_t)pix;
  368. }
  369. for (uint8_t r = 0; r < 32; r++)
  370. {
  371. for (uint8_t c = 0; c < 32; c++)
  372. DBG(_n("%02x"), pixels[((uint16_t)r<<5) + c]);
  373. DBG(_n("\n"));
  374. }
  375. }
  376. /*
  377. void xyzcal_draw_pattern_12x12_in_32x32(uint8_t* pattern, uint32_t* pixels, int w, int h, uint8_t x, uint8_t y, uint32_t and, uint32_t or)
  378. {
  379. for (int i = 0; i < 8; i++)
  380. for (int j = 0; j < 8; j++)
  381. {
  382. int idx = (x + j) + w * (y + i);
  383. if (pattern[i] & (1 << j))
  384. {
  385. pixels[idx] &= and;
  386. pixels[idx] |= or;
  387. }
  388. }
  389. }
  390. */
  391. int16_t xyzcal_match_pattern_12x12_in_32x32(uint16_t* pattern, uint8_t* pixels, uint8_t c, uint8_t r)
  392. {
  393. uint8_t thr = 32;
  394. int16_t match = 0;
  395. for (uint8_t i = 0; i < 12; i++)
  396. for (uint8_t j = 0; j < 12; j++)
  397. {
  398. if (((i == 0) || (i == 11)) && ((j < 2) || (j >= 10))) continue; //skip corners
  399. if (((j == 0) || (j == 11)) && ((i < 2) || (i >= 10))) continue;
  400. uint16_t idx = (c + j) + 32 * (r + i);
  401. uint8_t val = pixels[idx];
  402. if (pattern[i] & (1 << j))
  403. {
  404. if (val > thr) match ++;
  405. else match --;
  406. }
  407. else
  408. {
  409. if (val <= thr) match ++;
  410. else match --;
  411. }
  412. }
  413. return match;
  414. }
  415. int16_t xyzcal_find_pattern_12x12_in_32x32(uint8_t* pixels, uint16_t* pattern, uint8_t* pc, uint8_t* pr)
  416. {
  417. uint8_t max_c = 0;
  418. uint8_t max_r = 0;
  419. int16_t max_match = 0;
  420. for (uint8_t r = 0; r < (32 - 12); r++)
  421. for (uint8_t c = 0; c < (32 - 12); c++)
  422. {
  423. int16_t match = xyzcal_match_pattern_12x12_in_32x32(pattern, pixels, c, r);
  424. if (max_match < match)
  425. {
  426. max_c = c;
  427. max_r = r;
  428. max_match = match;
  429. }
  430. }
  431. DBG(_n("max_c=%d max_r=%d max_match=%d\n"), max_c, max_r, max_match);
  432. if (pc) *pc = max_c;
  433. if (pr) *pr = max_r;
  434. return max_match;
  435. }
  436. #ifdef XYZCAL_FIND_POINT_CENTER
  437. int8_t xyzcal_find_point_center(int16_t x0, int16_t y0, int16_t z0, int16_t min_z, int16_t max_z, uint16_t delay_us, uint8_t turns)
  438. {
  439. uint8_t n;
  440. uint16_t ad;
  441. float ar;
  442. float _cos;
  443. float _sin;
  444. int16_t r_min = 0;
  445. int16_t r_max = 0;
  446. int16_t x_min = 0;
  447. int16_t x_max = 0;
  448. int16_t y_min = 0;
  449. int16_t y_max = 0;
  450. int16_t r = 10;
  451. int16_t x = x0;
  452. int16_t y = y0;
  453. int16_t z = z0;
  454. int8_t _pinda = _PINDA;
  455. for (n = 0; n < turns; n++)
  456. {
  457. uint32_t r_sum = 0;
  458. for (ad = 0; ad < 720; ad++)
  459. {
  460. ar = ad * _PI / 360;
  461. _cos = cos(ar);
  462. _sin = sin(ar);
  463. x = x0 + (int)(_cos * r);
  464. y = y0 + (int)(_sin * r);
  465. xyzcal_lineXYZ_to(x, y, z, 1000, 0);
  466. int8_t pinda = _PINDA;
  467. if (pinda)
  468. r += 1;
  469. else
  470. {
  471. r -= 1;
  472. ad--;
  473. r_sum -= r;
  474. }
  475. if (ad == 0)
  476. {
  477. x_min = x0;
  478. x_max = x0;
  479. y_min = y0;
  480. y_max = y0;
  481. r_min = r;
  482. r_max = r;
  483. }
  484. else if (pinda)
  485. {
  486. if (x_min > x) x_min = (2*x + x_min) / 3;
  487. if (x_max < x) x_max = (2*x + x_max) / 3;
  488. if (y_min > y) y_min = (2*y + y_min) / 3;
  489. if (y_max < y) y_max = (2*y + y_max) / 3;
  490. /* if (x_min > x) x_min = x;
  491. if (x_max < x) x_max = x;
  492. if (y_min > y) y_min = y;
  493. if (y_max < y) y_max = y;*/
  494. if (r_min > r) r_min = r;
  495. if (r_max < r) r_max = r;
  496. }
  497. r_sum += r;
  498. /* if (_pinda != pinda)
  499. {
  500. if (pinda)
  501. DBG(_n("!1 x=%d y=%d\n"), x, y);
  502. else
  503. DBG(_n("!0 x=%d y=%d\n"), x, y);
  504. }*/
  505. _pinda = pinda;
  506. // DBG(_n("x=%d y=%d rx=%d ry=%d\n"), x, y, rx, ry);
  507. }
  508. DBG(_n("x_min=%d x_max=%d y_min=%d y_max=%d r_min=%d r_max=%d r_avg=%d\n"), x_min, x_max, y_min, y_max, r_min, r_max, r_sum / 720);
  509. if ((n > 2) && (n & 1))
  510. {
  511. x0 += (x_min + x_max);
  512. y0 += (y_min + y_max);
  513. x0 /= 3;
  514. y0 /= 3;
  515. int rx = (x_max - x_min) / 2;
  516. int ry = (y_max - y_min) / 2;
  517. r = (rx + ry) / 3;//(rx < ry)?rx:ry;
  518. DBG(_n("x0=%d y0=%d r=%d\n"), x0, y0, r);
  519. }
  520. }
  521. xyzcal_lineXYZ_to(x0, y0, z, 200, 0);
  522. }
  523. #endif //XYZCAL_FIND_POINT_CENTER
  524. uint8_t xyzcal_xycoords2point(int16_t x, int16_t y)
  525. {
  526. uint8_t ix = (x > 10000)?1:0;
  527. uint8_t iy = (y > 10000)?1:0;
  528. return iy?(3-ix):ix;
  529. }
  530. //MK3
  531. #if ((MOTHERBOARD == 310))
  532. const int16_t PROGMEM xyzcal_point_xcoords[4] = {1200, 22000, 22000, 1200};
  533. const int16_t PROGMEM xyzcal_point_ycoords[4] = {600, 600, 19800, 19800};
  534. #endif //((MOTHERBOARD == 310))
  535. //MK2.5
  536. #if ((MOTHERBOARD == 200) || (MOTHERBOARD == 203))
  537. const int16_t PROGMEM xyzcal_point_xcoords[4] = {1200, 22000, 22000, 1200};
  538. const int16_t PROGMEM xyzcal_point_ycoords[4] = {700, 700, 19800, 19800};
  539. #endif //((MOTHERBOARD == 200) || (MOTHERBOARD == 203))
  540. const uint16_t PROGMEM xyzcal_point_pattern[12] = {0x000, 0x0f0, 0x1f8, 0x3fc, 0x7fe, 0x7fe, 0x7fe, 0x7fe, 0x3fc, 0x1f8, 0x0f0, 0x000};
  541. bool xyzcal_searchZ(void)
  542. {
  543. DBG(_n("xyzcal_searchZ x=%ld y=%ld z=%ld\n"), count_position[X_AXIS], count_position[Y_AXIS], count_position[Z_AXIS]);
  544. int16_t x0 = _X;
  545. int16_t y0 = _Y;
  546. int16_t z0 = _Z;
  547. // int16_t min_z = -6000;
  548. // int16_t dz = 100;
  549. int16_t z = z0;
  550. while (z > -2300) //-6mm + 0.25mm
  551. {
  552. uint16_t ad = 0;
  553. if (xyzcal_spiral8(x0, y0, z, 100, 900, 320, 1, &ad)) //dz=100 radius=900 delay=400
  554. {
  555. int16_t x_on = _X;
  556. int16_t y_on = _Y;
  557. int16_t z_on = _Z;
  558. DBG(_n(" ON-SIGNAL at x=%d y=%d z=%d ad=%d\n"), x_on, y_on, z_on, ad);
  559. return true;
  560. }
  561. z -= 400;
  562. }
  563. 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]);
  564. return false;
  565. }
  566. bool xyzcal_scan_and_process(void)
  567. {
  568. DBG(_n("sizeof(block_buffer)=%d\n"), sizeof(block_t)*BLOCK_BUFFER_SIZE);
  569. // DBG(_n("sizeof(pixels)=%d\n"), 32*32);
  570. // DBG(_n("sizeof(histo)=%d\n"), 2*16);
  571. // DBG(_n("sizeof(pattern)=%d\n"), 2*12);
  572. DBG(_n("sizeof(total)=%d\n"), 32*32+2*16+2*12);
  573. bool ret = false;
  574. int16_t x = _X;
  575. int16_t y = _Y;
  576. int16_t z = _Z;
  577. uint8_t* pixels = (uint8_t*)block_buffer;
  578. xyzcal_scan_pixels_32x32(x, y, z - 72, 2400, 200, pixels);
  579. uint16_t* histo = (uint16_t*)(pixels + 32*32);
  580. xyzcal_histo_pixels_32x32(pixels, histo);
  581. xyzcal_adjust_pixels(pixels, histo);
  582. uint16_t* pattern = (uint16_t*)(histo + 2*16);
  583. for (uint8_t i = 0; i < 12; i++)
  584. {
  585. pattern[i] = pgm_read_word_far((uint16_t*)(xyzcal_point_pattern + i));
  586. // DBG(_n(" pattern[%d]=%d\n"), i, pattern[i]);
  587. }
  588. uint8_t c = 0;
  589. uint8_t r = 0;
  590. if (xyzcal_find_pattern_12x12_in_32x32(pixels, pattern, &c, &r) > 66) //total pixels=144, corner=12 (1/2 = 66)
  591. {
  592. DBG(_n(" pattern found at %d %d\n"), c, r);
  593. c += 6;
  594. r += 6;
  595. x += ((int16_t)c - 16) << 6;
  596. y += ((int16_t)r - 16) << 6;
  597. DBG(_n(" x=%d y=%d z=%d\n"), x, y, z);
  598. xyzcal_lineXYZ_to(x, y, z, 200, 0);
  599. ret = true;
  600. }
  601. for (uint16_t i = 0; i < sizeof(block_t)*BLOCK_BUFFER_SIZE; i++)
  602. pixels[i] = 0;
  603. return ret;
  604. }
  605. bool xyzcal_find_bed_induction_sensor_point_xy(void)
  606. {
  607. 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]);
  608. bool ret = false;
  609. st_synchronize();
  610. int16_t x = _X;
  611. int16_t y = _Y;
  612. int16_t z = _Z;
  613. uint8_t point = xyzcal_xycoords2point(x, y);
  614. x = pgm_read_word_far((uint16_t*)(xyzcal_point_xcoords + point));
  615. y = pgm_read_word_far((uint16_t*)(xyzcal_point_ycoords + point));
  616. DBG(_n("point=%d x=%d y=%d z=%d\n"), point, x, y, z);
  617. xyzcal_meassure_enter();
  618. xyzcal_lineXYZ_to(x, y, z, 200, 0);
  619. if (xyzcal_searchZ())
  620. {
  621. int16_t z = _Z;
  622. xyzcal_lineXYZ_to(x, y, z, 200, 0);
  623. if (xyzcal_scan_and_process())
  624. {
  625. ret = true;
  626. }
  627. }
  628. xyzcal_meassure_leave();
  629. return ret;
  630. }
  631. #endif //NEW_XYZCAL