xyzcal.cpp 21 KB

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