xyzcal.cpp 25 KB

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