xyzcal.cpp 26 KB

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