-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
mesh_bed_calibration.cpp
3150 lines (2918 loc) · 120 KB
/
mesh_bed_calibration.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "Configuration.h"
#include "ConfigurationStore.h"
#include "util.h"
#include "language.h"
#include "lcd.h"
#include "mesh_bed_calibration.h"
#include "mesh_bed_leveling.h"
#include "stepper.h"
#include "ultralcd.h"
#include "temperature.h"
#ifdef TMC2130
#include "tmc2130.h"
#endif //TMC2130
#define DBG(args...) printf_P(args)
uint8_t world2machine_correction_mode;
float world2machine_rotation_and_skew[2][2];
float world2machine_rotation_and_skew_inv[2][2];
float world2machine_shift[2];
// Weight of the Y coordinate for the least squares fitting of the bed induction sensor targets.
// Only used for the first row of the points, which may not befully in reach of the sensor.
#define WEIGHT_FIRST_ROW_X_HIGH (1.f)
#define WEIGHT_FIRST_ROW_X_LOW (0.35f)
#define WEIGHT_FIRST_ROW_Y_HIGH (0.3f)
#define WEIGHT_FIRST_ROW_Y_LOW (0.0f)
// Scaling of the real machine axes against the programmed dimensions in the firmware.
// The correction is tiny, here around 0.5mm on 250mm length.
//#define MACHINE_AXIS_SCALE_X ((250.f - 0.5f) / 250.f)
//#define MACHINE_AXIS_SCALE_Y ((250.f - 0.5f) / 250.f)
#define MACHINE_AXIS_SCALE_X 1.f
#define MACHINE_AXIS_SCALE_Y 1.f
#define BED_CALIBRATION_POINT_OFFSET_MAX_EUCLIDIAN (0.8f)
#define BED_CALIBRATION_POINT_OFFSET_MAX_1ST_ROW_X (0.8f)
#define BED_CALIBRATION_POINT_OFFSET_MAX_1ST_ROW_Y (1.5f)
#define MIN_BED_SENSOR_POINT_RESPONSE_DMR (2.0f)
//#define Y_MIN_POS_FOR_BED_CALIBRATION (MANUAL_Y_HOME_POS-0.2f)
#define Y_MIN_POS_FOR_BED_CALIBRATION (Y_MIN_POS)
// Distances toward the print bed edge may not be accurate.
#define Y_MIN_POS_CALIBRATION_POINT_ACCURATE (Y_MIN_POS + 3.f)
// When the measured point center is out of reach of the sensor, Y coordinate will be ignored
// by the Least Squares fitting and the X coordinate will be weighted low.
#define Y_MIN_POS_CALIBRATION_POINT_OUT_OF_REACH (Y_MIN_POS - 0.5f)
// 0.12 degrees equals to an offset of 0.5mm on 250mm length.
const float bed_skew_angle_mild = (0.12f * M_PI / 180.f);
// 0.25 degrees equals to an offset of 1.1mm on 250mm length.
const float bed_skew_angle_extreme = (0.25f * M_PI / 180.f);
// Positions of the bed reference points in the machine coordinates, referenced to the P.I.N.D.A sensor.
// The points are ordered in a zig-zag fashion to speed up the calibration.
#ifdef HEATBED_V2
/**
* [0,0] bed print area point X coordinate in bed coordinates ver. 05d/24V
*/
#define BED_PRINT_ZERO_REF_X 2.f
/**
* [0,0] bed print area point Y coordinate in bed coordinates ver. 05d/24V
*/
#define BED_PRINT_ZERO_REF_Y 9.4f
/**
* @brief Positions of the bed reference points in print area coordinates. ver. 05d/24V
*
* Numeral constants are in bed coordinates, subtracting macro defined values converts it to print area coordinates.
*
* The points are the following:
* MK2: center front, center right, center rear, center left.
* MK25 and MK3: front left, front right, rear right, rear left
*/
const float bed_ref_points_4[] PROGMEM = {
37.f - BED_PRINT_ZERO_REF_X - X_PROBE_OFFSET_FROM_EXTRUDER - SHEET_PRINT_ZERO_REF_X,
18.4f - BED_PRINT_ZERO_REF_Y - Y_PROBE_OFFSET_FROM_EXTRUDER - SHEET_PRINT_ZERO_REF_Y,
245.f - BED_PRINT_ZERO_REF_X - X_PROBE_OFFSET_FROM_EXTRUDER - SHEET_PRINT_ZERO_REF_X,
18.4f - BED_PRINT_ZERO_REF_Y - Y_PROBE_OFFSET_FROM_EXTRUDER - SHEET_PRINT_ZERO_REF_Y,
245.f - BED_PRINT_ZERO_REF_X - X_PROBE_OFFSET_FROM_EXTRUDER - SHEET_PRINT_ZERO_REF_X,
210.4f - BED_PRINT_ZERO_REF_Y - Y_PROBE_OFFSET_FROM_EXTRUDER - SHEET_PRINT_ZERO_REF_Y,
37.f - BED_PRINT_ZERO_REF_X - X_PROBE_OFFSET_FROM_EXTRUDER - SHEET_PRINT_ZERO_REF_X,
210.4f - BED_PRINT_ZERO_REF_Y - Y_PROBE_OFFSET_FROM_EXTRUDER - SHEET_PRINT_ZERO_REF_Y
};
#else
// Positions of the bed reference points in the machine coordinates, referenced to the P.I.N.D.A sensor.
// The points are the following: center front, center right, center rear, center left.
const float bed_ref_points_4[] PROGMEM = {
115.f - BED_ZERO_REF_X, 8.4f - BED_ZERO_REF_Y,
216.f - BED_ZERO_REF_X, 104.4f - BED_ZERO_REF_Y,
115.f - BED_ZERO_REF_X, 202.4f - BED_ZERO_REF_Y,
13.f - BED_ZERO_REF_X, 104.4f - BED_ZERO_REF_Y
};
#endif //not HEATBED_V2
static inline float sqr(float x) { return x * x; }
#ifdef HEATBED_V2
static inline bool point_on_1st_row(const uint8_t /*i*/)
{
return false;
}
#else //HEATBED_V2
static inline bool point_on_1st_row(const uint8_t i)
{
return (i < 3);
}
#endif //HEATBED_V2
// Weight of a point coordinate in a least squares optimization.
// The first row of points may not be fully reachable
// and the y values may be shortened a bit by the bed carriage
// pulling the belt up.
static inline float point_weight_x(const uint8_t i, const float &y)
{
float w = 1.f;
if (point_on_1st_row(i)) {
if (y >= Y_MIN_POS_CALIBRATION_POINT_ACCURATE) {
w = WEIGHT_FIRST_ROW_X_HIGH;
} else if (y < Y_MIN_POS_CALIBRATION_POINT_OUT_OF_REACH) {
// If the point is fully outside, give it some weight.
w = WEIGHT_FIRST_ROW_X_LOW;
} else {
// Linearly interpolate the weight from 1 to WEIGHT_FIRST_ROW_X.
float t = (y - Y_MIN_POS_CALIBRATION_POINT_OUT_OF_REACH) / (Y_MIN_POS_CALIBRATION_POINT_ACCURATE - Y_MIN_POS_CALIBRATION_POINT_OUT_OF_REACH);
w = (1.f - t) * WEIGHT_FIRST_ROW_X_LOW + t * WEIGHT_FIRST_ROW_X_HIGH;
}
}
return w;
}
// Weight of a point coordinate in a least squares optimization.
// The first row of points may not be fully reachable
// and the y values may be shortened a bit by the bed carriage
// pulling the belt up.
static inline float point_weight_y(const uint8_t i, const float &y)
{
float w = 1.f;
if (point_on_1st_row(i)) {
if (y >= Y_MIN_POS_CALIBRATION_POINT_ACCURATE) {
w = WEIGHT_FIRST_ROW_Y_HIGH;
} else if (y < Y_MIN_POS_CALIBRATION_POINT_OUT_OF_REACH) {
// If the point is fully outside, give it some weight.
w = WEIGHT_FIRST_ROW_Y_LOW;
} else {
// Linearly interpolate the weight from 1 to WEIGHT_FIRST_ROW_X.
float t = (y - Y_MIN_POS_CALIBRATION_POINT_OUT_OF_REACH) / (Y_MIN_POS_CALIBRATION_POINT_ACCURATE - Y_MIN_POS_CALIBRATION_POINT_OUT_OF_REACH);
w = (1.f - t) * WEIGHT_FIRST_ROW_Y_LOW + t * WEIGHT_FIRST_ROW_Y_HIGH;
}
}
return w;
}
/**
* @brief Calculate machine skew and offset
*
* Non-Linear Least Squares fitting of the bed to the measured induction points
* using the Gauss-Newton method.
* This method will maintain a unity length of the machine axes,
* which is the correct approach if the sensor points are not measured precisely.
* @param measured_pts Matrix of 2D points (maximum 18 floats)
* @param npts Number of points (maximum 9)
* @param true_pts
* @param [out] vec_x Resulting correction matrix. X axis vector
* @param [out] vec_y Resulting correction matrix. Y axis vector
* @param [out] cntr Resulting correction matrix. [0;0] pont offset
* @param verbosity_level
* @return BedSkewOffsetDetectionResultType
*/
BedSkewOffsetDetectionResultType calculate_machine_skew_and_offset_LS(
const float *measured_pts,
uint8_t npts,
const float *true_pts,
float *vec_x,
float *vec_y,
float *cntr,
int8_t
#ifdef SUPPORT_VERBOSITY
verbosity_level
#endif //SUPPORT_VERBOSITY
)
{
float angleDiff;
#ifdef SUPPORT_VERBOSITY
if (verbosity_level >= 10) {
SERIAL_ECHOLNPGM("calculate machine skew and offset LS");
// Show the initial state, before the fitting.
SERIAL_ECHOPGM("X vector, initial: ");
MYSERIAL.print(vec_x[0], 5);
SERIAL_ECHOPGM(", ");
MYSERIAL.print(vec_x[1], 5);
SERIAL_ECHOLNPGM("");
SERIAL_ECHOPGM("Y vector, initial: ");
MYSERIAL.print(vec_y[0], 5);
SERIAL_ECHOPGM(", ");
MYSERIAL.print(vec_y[1], 5);
SERIAL_ECHOLNPGM("");
SERIAL_ECHOPGM("center, initial: ");
MYSERIAL.print(cntr[0], 5);
SERIAL_ECHOPGM(", ");
MYSERIAL.print(cntr[1], 5);
SERIAL_ECHOLNPGM("");
for (uint8_t i = 0; i < npts; ++i) {
SERIAL_ECHOPGM("point #");
MYSERIAL.print(int(i));
SERIAL_ECHOPGM(" measured: (");
MYSERIAL.print(measured_pts[i * 2], 5);
SERIAL_ECHOPGM(", ");
MYSERIAL.print(measured_pts[i * 2 + 1], 5);
SERIAL_ECHOPGM("); target: (");
MYSERIAL.print(pgm_read_float(true_pts + i * 2), 5);
SERIAL_ECHOPGM(", ");
MYSERIAL.print(pgm_read_float(true_pts + i * 2 + 1), 5);
SERIAL_ECHOPGM("), error: ");
MYSERIAL.print(sqrt(
sqr(pgm_read_float(true_pts + i * 2) - measured_pts[i * 2]) +
sqr(pgm_read_float(true_pts + i * 2 + 1) - measured_pts[i * 2 + 1])), 5);
SERIAL_ECHOLNPGM("");
}
delay_keep_alive(100);
}
#endif // SUPPORT_VERBOSITY
// Run some iterations of the Gauss-Newton method of non-linear least squares.
// Initial set of parameters:
// X,Y offset
cntr[0] = 0.f;
cntr[1] = 0.f;
// Rotation of the machine X axis from the bed X axis.
float a1 = 0;
// Rotation of the machine Y axis from the bed Y axis.
float a2 = 0;
for (int8_t iter = 0; iter < 100; ++iter) {
float c1 = cos(a1) * MACHINE_AXIS_SCALE_X;
float s1 = sin(a1) * MACHINE_AXIS_SCALE_X;
float c2 = cos(a2) * MACHINE_AXIS_SCALE_Y;
float s2 = sin(a2) * MACHINE_AXIS_SCALE_Y;
// Prepare the Normal equation for the Gauss-Newton method.
float A[4][4] = { 0.f };
float b[4] = { 0.f };
float acc;
delay_keep_alive(0); //manage heater, reset watchdog, manage inactivity
for (uint8_t r = 0; r < 4; ++r) {
for (uint8_t c = 0; c < 4; ++c) {
acc = 0;
// J^T times J
for (uint8_t i = 0; i < npts; ++i) {
// First for the residuum in the x axis:
if (r != 1 && c != 1) {
float a =
(r == 0) ? 1.f :
((r == 2) ? (-s1 * measured_pts[2 * i]) :
(-c2 * measured_pts[2 * i + 1]));
float b =
(c == 0) ? 1.f :
((c == 2) ? (-s1 * measured_pts[2 * i]) :
(-c2 * measured_pts[2 * i + 1]));
float w = point_weight_x(i, measured_pts[2 * i + 1]);
acc += a * b * w;
}
// Second for the residuum in the y axis.
// The first row of the points have a low weight, because their position may not be known
// with a sufficient accuracy.
if (r != 0 && c != 0) {
float a =
(r == 1) ? 1.f :
((r == 2) ? ( c1 * measured_pts[2 * i]) :
(-s2 * measured_pts[2 * i + 1]));
float b =
(c == 1) ? 1.f :
((c == 2) ? ( c1 * measured_pts[2 * i]) :
(-s2 * measured_pts[2 * i + 1]));
float w = point_weight_y(i, measured_pts[2 * i + 1]);
acc += a * b * w;
}
}
A[r][c] = acc;
}
// J^T times f(x)
acc = 0.f;
for (uint8_t i = 0; i < npts; ++i) {
{
float j =
(r == 0) ? 1.f :
((r == 1) ? 0.f :
((r == 2) ? (-s1 * measured_pts[2 * i]) :
(-c2 * measured_pts[2 * i + 1])));
float fx = c1 * measured_pts[2 * i] - s2 * measured_pts[2 * i + 1] + cntr[0] - pgm_read_float(true_pts + i * 2);
float w = point_weight_x(i, measured_pts[2 * i + 1]);
acc += j * fx * w;
}
{
float j =
(r == 0) ? 0.f :
((r == 1) ? 1.f :
((r == 2) ? ( c1 * measured_pts[2 * i]) :
(-s2 * measured_pts[2 * i + 1])));
float fy = s1 * measured_pts[2 * i] + c2 * measured_pts[2 * i + 1] + cntr[1] - pgm_read_float(true_pts + i * 2 + 1);
float w = point_weight_y(i, measured_pts[2 * i + 1]);
acc += j * fy * w;
}
}
b[r] = -acc;
}
// Solve for h by a Gauss iteration method.
float h[4] = { 0.f };
for (uint8_t gauss_iter = 0; gauss_iter < 100; ++gauss_iter) {
h[0] = (b[0] - A[0][1] * h[1] - A[0][2] * h[2] - A[0][3] * h[3]) / A[0][0];
h[1] = (b[1] - A[1][0] * h[0] - A[1][2] * h[2] - A[1][3] * h[3]) / A[1][1];
h[2] = (b[2] - A[2][0] * h[0] - A[2][1] * h[1] - A[2][3] * h[3]) / A[2][2];
h[3] = (b[3] - A[3][0] * h[0] - A[3][1] * h[1] - A[3][2] * h[2]) / A[3][3];
}
// and update the current position with h.
// It may be better to use the Levenberg-Marquart method here,
// but because we are very close to the solution alread,
// the simple Gauss-Newton non-linear Least Squares method works well enough.
cntr[0] += h[0];
cntr[1] += h[1];
a1 += h[2];
a2 += h[3];
#ifdef SUPPORT_VERBOSITY
if (verbosity_level >= 20) {
SERIAL_ECHOPGM("iteration: ");
MYSERIAL.print(int(iter));
SERIAL_ECHOPGM("; correction vector: ");
MYSERIAL.print(h[0], 5);
SERIAL_ECHOPGM(", ");
MYSERIAL.print(h[1], 5);
SERIAL_ECHOPGM(", ");
MYSERIAL.print(h[2], 5);
SERIAL_ECHOPGM(", ");
MYSERIAL.print(h[3], 5);
SERIAL_ECHOLNPGM("");
SERIAL_ECHOPGM("corrected x/y: ");
MYSERIAL.print(cntr[0], 5);
SERIAL_ECHOPGM(", ");
MYSERIAL.print(cntr[0], 5);
SERIAL_ECHOLNPGM("");
SERIAL_ECHOPGM("corrected angles: ");
MYSERIAL.print(180.f * a1 / M_PI, 5);
SERIAL_ECHOPGM(", ");
MYSERIAL.print(180.f * a2 / M_PI, 5);
SERIAL_ECHOLNPGM("");
}
#endif // SUPPORT_VERBOSITY
}
vec_x[0] = cos(a1) * MACHINE_AXIS_SCALE_X;
vec_x[1] = sin(a1) * MACHINE_AXIS_SCALE_X;
vec_y[0] = -sin(a2) * MACHINE_AXIS_SCALE_Y;
vec_y[1] = cos(a2) * MACHINE_AXIS_SCALE_Y;
BedSkewOffsetDetectionResultType result = BED_SKEW_OFFSET_DETECTION_PERFECT;
{
angleDiff = fabs(a2 - a1);
/// XY skew and Y-bed skew
DBG(_n("Measured skews: %f %f\n"), degrees(a2 - a1), degrees(a2));
eeprom_update_float_notify((float *)(EEPROM_XYZ_CAL_SKEW), angleDiff); //storing xyz cal. skew to be able to show in support menu later
if (angleDiff > bed_skew_angle_mild)
result = (angleDiff > bed_skew_angle_extreme) ?
BED_SKEW_OFFSET_DETECTION_SKEW_EXTREME :
BED_SKEW_OFFSET_DETECTION_SKEW_MILD;
if (fabs(a1) > bed_skew_angle_extreme ||
fabs(a2) > bed_skew_angle_extreme)
result = BED_SKEW_OFFSET_DETECTION_SKEW_EXTREME;
}
#ifdef SUPPORT_VERBOSITY
if (verbosity_level >= 1) {
SERIAL_ECHOPGM("correction angles: ");
MYSERIAL.print(180.f * a1 / M_PI, 5);
SERIAL_ECHOPGM(", ");
MYSERIAL.print(180.f * a2 / M_PI, 5);
SERIAL_ECHOLNPGM("");
}
if (verbosity_level >= 10) {
// Show the adjusted state, before the fitting.
SERIAL_ECHOPGM("X vector new, inverted: ");
MYSERIAL.print(vec_x[0], 5);
SERIAL_ECHOPGM(", ");
MYSERIAL.print(vec_x[1], 5);
SERIAL_ECHOLNPGM("");
SERIAL_ECHOPGM("Y vector new, inverted: ");
MYSERIAL.print(vec_y[0], 5);
SERIAL_ECHOPGM(", ");
MYSERIAL.print(vec_y[1], 5);
SERIAL_ECHOLNPGM("");
SERIAL_ECHOPGM("center new, inverted: ");
MYSERIAL.print(cntr[0], 5);
SERIAL_ECHOPGM(", ");
MYSERIAL.print(cntr[1], 5);
SERIAL_ECHOLNPGM("");
delay_keep_alive(100);
SERIAL_ECHOLNPGM("Error after correction: ");
}
#endif // SUPPORT_VERBOSITY
// Measure the error after correction.
for (uint8_t i = 0; i < npts; ++i) {
float x = vec_x[0] * measured_pts[i * 2] + vec_y[0] * measured_pts[i * 2 + 1] + cntr[0];
float y = vec_x[1] * measured_pts[i * 2] + vec_y[1] * measured_pts[i * 2 + 1] + cntr[1];
float errX = pgm_read_float(true_pts + i * 2) - x;
float errY = pgm_read_float(true_pts + i * 2 + 1) - y;
float err = hypot(errX, errY);
#ifdef SUPPORT_VERBOSITY
if (verbosity_level >= 10) {
SERIAL_ECHOPGM("point #");
MYSERIAL.print(int(i));
SERIAL_ECHOLNPGM(":");
}
#endif // SUPPORT_VERBOSITY
if (point_on_1st_row(i)) {
#ifdef SUPPORT_VERBOSITY
if(verbosity_level >= 20) SERIAL_ECHOPGM("Point on first row");
#endif // SUPPORT_VERBOSITY
float w = point_weight_y(i, measured_pts[2 * i + 1]);
if (errX > BED_CALIBRATION_POINT_OFFSET_MAX_1ST_ROW_X ||
(w != 0.f && errY > BED_CALIBRATION_POINT_OFFSET_MAX_1ST_ROW_Y)) {
result = BED_SKEW_OFFSET_DETECTION_FITTING_FAILED;
#ifdef SUPPORT_VERBOSITY
if (verbosity_level >= 20) {
SERIAL_ECHOPGM(", weigth Y: ");
MYSERIAL.print(w);
if (errX > BED_CALIBRATION_POINT_OFFSET_MAX_1ST_ROW_X) SERIAL_ECHOPGM(", error X > max. error X");
if (w != 0.f && errY > BED_CALIBRATION_POINT_OFFSET_MAX_1ST_ROW_Y) SERIAL_ECHOPGM(", error Y > max. error Y");
}
#endif // SUPPORT_VERBOSITY
}
}
else {
#ifdef SUPPORT_VERBOSITY
if(verbosity_level >=20 ) SERIAL_ECHOPGM("Point not on first row");
#endif // SUPPORT_VERBOSITY
if (err > BED_CALIBRATION_POINT_OFFSET_MAX_EUCLIDIAN) {
result = BED_SKEW_OFFSET_DETECTION_FITTING_FAILED;
#ifdef SUPPORT_VERBOSITY
if(verbosity_level >= 20) SERIAL_ECHOPGM(", error > max. error euclidian");
#endif // SUPPORT_VERBOSITY
}
}
#ifdef SUPPORT_VERBOSITY
if (verbosity_level >= 10) {
SERIAL_ECHOLNPGM("");
SERIAL_ECHOPGM("measured: (");
MYSERIAL.print(measured_pts[i * 2], 5);
SERIAL_ECHOPGM(", ");
MYSERIAL.print(measured_pts[i * 2 + 1], 5);
SERIAL_ECHOPGM("); corrected: (");
MYSERIAL.print(x, 5);
SERIAL_ECHOPGM(", ");
MYSERIAL.print(y, 5);
SERIAL_ECHOPGM("); target: (");
MYSERIAL.print(pgm_read_float(true_pts + i * 2), 5);
SERIAL_ECHOPGM(", ");
MYSERIAL.print(pgm_read_float(true_pts + i * 2 + 1), 5);
SERIAL_ECHOLNPGM(")");
SERIAL_ECHOPGM("error: ");
MYSERIAL.print(err);
SERIAL_ECHOPGM(", error X: ");
MYSERIAL.print(errX);
SERIAL_ECHOPGM(", error Y: ");
MYSERIAL.print(errY);
SERIAL_ECHOLNPGM("");
SERIAL_ECHOLNPGM("");
}
#endif // SUPPORT_VERBOSITY
}
#ifdef SUPPORT_VERBOSITY
if (verbosity_level >= 20) {
SERIAL_ECHOLNPGM("Max. errors:");
SERIAL_ECHOPGM("Max. error X:");
MYSERIAL.println(BED_CALIBRATION_POINT_OFFSET_MAX_1ST_ROW_X);
SERIAL_ECHOPGM("Max. error Y:");
MYSERIAL.println(BED_CALIBRATION_POINT_OFFSET_MAX_1ST_ROW_Y);
SERIAL_ECHOPGM("Max. error euclidian:");
MYSERIAL.println(BED_CALIBRATION_POINT_OFFSET_MAX_EUCLIDIAN);
SERIAL_ECHOLNPGM("");
}
#endif // SUPPORT_VERBOSITY
if (result == BED_SKEW_OFFSET_DETECTION_PERFECT) {
#ifdef SUPPORT_VERBOSITY
if (verbosity_level > 0)
SERIAL_ECHOLNPGM("Very little skew detected. Orthogonalizing the axes.");
#endif // SUPPORT_VERBOSITY
// Orthogonalize the axes.
a1 = 0.5f * (a1 + a2);
vec_x[0] = cos(a1) * MACHINE_AXIS_SCALE_X;
vec_x[1] = sin(a1) * MACHINE_AXIS_SCALE_X;
vec_y[0] = -sin(a1) * MACHINE_AXIS_SCALE_Y;
vec_y[1] = cos(a1) * MACHINE_AXIS_SCALE_Y;
// Refresh the offset.
cntr[0] = 0.f;
cntr[1] = 0.f;
float wx = 0.f;
float wy = 0.f;
for (int8_t i = 0; i < npts; ++ i) {
float x = vec_x[0] * measured_pts[i * 2] + vec_y[0] * measured_pts[i * 2 + 1];
float y = vec_x[1] * measured_pts[i * 2] + vec_y[1] * measured_pts[i * 2 + 1];
float w = point_weight_x(i, y);
cntr[0] += w * (pgm_read_float(true_pts + i * 2) - x);
wx += w;
#ifdef SUPPORT_VERBOSITY
if (verbosity_level >= 20) {
MYSERIAL.print(i);
SERIAL_ECHOLNPGM("");
SERIAL_ECHOLNPGM("Weight_x:");
MYSERIAL.print(w);
SERIAL_ECHOLNPGM("");
SERIAL_ECHOLNPGM("cntr[0]:");
MYSERIAL.print(cntr[0]);
SERIAL_ECHOLNPGM("");
SERIAL_ECHOLNPGM("wx:");
MYSERIAL.print(wx);
}
#endif // SUPPORT_VERBOSITY
w = point_weight_y(i, y);
cntr[1] += w * (pgm_read_float(true_pts + i * 2 + 1) - y);
wy += w;
#ifdef SUPPORT_VERBOSITY
if (verbosity_level >= 20) {
SERIAL_ECHOLNPGM("");
SERIAL_ECHOLNPGM("Weight_y:");
MYSERIAL.print(w);
SERIAL_ECHOLNPGM("");
SERIAL_ECHOLNPGM("cntr[1]:");
MYSERIAL.print(cntr[1]);
SERIAL_ECHOLNPGM("");
SERIAL_ECHOLNPGM("wy:");
MYSERIAL.print(wy);
SERIAL_ECHOLNPGM("");
SERIAL_ECHOLNPGM("");
}
#endif // SUPPORT_VERBOSITY
}
cntr[0] /= wx;
cntr[1] /= wy;
#ifdef SUPPORT_VERBOSITY
if (verbosity_level >= 20) {
SERIAL_ECHOLNPGM("");
SERIAL_ECHOLNPGM("Final cntr values:");
SERIAL_ECHOLNPGM("cntr[0]:");
MYSERIAL.print(cntr[0]);
SERIAL_ECHOLNPGM("");
SERIAL_ECHOLNPGM("cntr[1]:");
MYSERIAL.print(cntr[1]);
SERIAL_ECHOLNPGM("");
}
#endif // SUPPORT_VERBOSITY
}
// Invert the transformation matrix made of vec_x, vec_y and cntr.
{
float d = vec_x[0] * vec_y[1] - vec_x[1] * vec_y[0];
float Ainv[2][2] = {
{ vec_y[1] / d, -vec_y[0] / d },
{ -vec_x[1] / d, vec_x[0] / d }
};
float cntrInv[2] = {
-Ainv[0][0] * cntr[0] - Ainv[0][1] * cntr[1],
-Ainv[1][0] * cntr[0] - Ainv[1][1] * cntr[1]
};
vec_x[0] = Ainv[0][0];
vec_x[1] = Ainv[1][0];
vec_y[0] = Ainv[0][1];
vec_y[1] = Ainv[1][1];
cntr[0] = cntrInv[0];
cntr[1] = cntrInv[1];
}
#ifdef SUPPORT_VERBOSITY
if (verbosity_level >= 1) {
// Show the adjusted state, before the fitting.
SERIAL_ECHOPGM("X vector, adjusted: ");
MYSERIAL.print(vec_x[0], 5);
SERIAL_ECHOPGM(", ");
MYSERIAL.print(vec_x[1], 5);
SERIAL_ECHOLNPGM("");
SERIAL_ECHOPGM("Y vector, adjusted: ");
MYSERIAL.print(vec_y[0], 5);
SERIAL_ECHOPGM(", ");
MYSERIAL.print(vec_y[1], 5);
SERIAL_ECHOLNPGM("");
SERIAL_ECHOPGM("center, adjusted: ");
MYSERIAL.print(cntr[0], 5);
SERIAL_ECHOPGM(", ");
MYSERIAL.print(cntr[1], 5);
SERIAL_ECHOLNPGM("");
delay_keep_alive(100);
}
if (verbosity_level >= 2) {
SERIAL_ECHOLNPGM("Difference after correction: ");
for (uint8_t i = 0; i < npts; ++i) {
float x = vec_x[0] * pgm_read_float(true_pts + i * 2) + vec_y[0] * pgm_read_float(true_pts + i * 2 + 1) + cntr[0];
float y = vec_x[1] * pgm_read_float(true_pts + i * 2) + vec_y[1] * pgm_read_float(true_pts + i * 2 + 1) + cntr[1];
SERIAL_ECHOPGM("point #");
MYSERIAL.print(int(i));
SERIAL_ECHOPGM("measured: (");
MYSERIAL.print(measured_pts[i * 2], 5);
SERIAL_ECHOPGM(", ");
MYSERIAL.print(measured_pts[i * 2 + 1], 5);
SERIAL_ECHOPGM("); measured-corrected: (");
MYSERIAL.print(x, 5);
SERIAL_ECHOPGM(", ");
MYSERIAL.print(y, 5);
SERIAL_ECHOPGM("); target: (");
MYSERIAL.print(pgm_read_float(true_pts + i * 2), 5);
SERIAL_ECHOPGM(", ");
MYSERIAL.print(pgm_read_float(true_pts + i * 2 + 1), 5);
SERIAL_ECHOPGM("), error: ");
MYSERIAL.print( hypot(measured_pts[i * 2] - x, measured_pts[i * 2 + 1] - y) );
SERIAL_ECHOLNPGM("");
}
if (verbosity_level >= 20) {
SERIAL_ECHOLNPGM("");
SERIAL_ECHOLNPGM("Calculate offset and skew returning result:");
MYSERIAL.print(int(result));
SERIAL_ECHOLNPGM("");
SERIAL_ECHOLNPGM("");
}
delay_keep_alive(100);
}
#endif // SUPPORT_VERBOSITY
return result;
}
/**
* @brief Erase calibration data stored in EEPROM
*/
void reset_bed_offset_and_skew()
{
eeprom_update_dword_notify((uint32_t*)(EEPROM_BED_CALIBRATION_CENTER+0), 0x0FFFFFFFF);
eeprom_update_dword_notify((uint32_t*)(EEPROM_BED_CALIBRATION_CENTER+4), 0x0FFFFFFFF);
eeprom_update_dword_notify((uint32_t*)(EEPROM_BED_CALIBRATION_VEC_X +0), 0x0FFFFFFFF);
eeprom_update_dword_notify((uint32_t*)(EEPROM_BED_CALIBRATION_VEC_X +4), 0x0FFFFFFFF);
eeprom_update_dword_notify((uint32_t*)(EEPROM_BED_CALIBRATION_VEC_Y +0), 0x0FFFFFFFF);
eeprom_update_dword_notify((uint32_t*)(EEPROM_BED_CALIBRATION_VEC_Y +4), 0x0FFFFFFFF);
// Reset the 8 16bit offsets.
for (int8_t i = 0; i < 4; ++ i)
eeprom_update_dword_notify((uint32_t*)(EEPROM_BED_CALIBRATION_Z_JITTER+i*4), 0x0FFFFFFFF);
}
bool is_bed_z_jitter_data_valid()
// offsets of the Z heiths of the calibration points from the first point are saved as 16bit signed int, scaled to tenths of microns
// if at least one 16bit integer has different value then -1 (0x0FFFF), data are considered valid and function returns true, otherwise it returns false
{
for (int8_t i = 0; i < 8; ++i) {
if (eeprom_read_word((uint16_t*)(EEPROM_BED_CALIBRATION_Z_JITTER + i * 2)) != 0x0FFFF)
return true;
}
return false;
}
static void world2machine_update(const float vec_x[2], const float vec_y[2], const float cntr[2])
{
world2machine_rotation_and_skew[0][0] = vec_x[0];
world2machine_rotation_and_skew[1][0] = vec_x[1];
world2machine_rotation_and_skew[0][1] = vec_y[0];
world2machine_rotation_and_skew[1][1] = vec_y[1];
world2machine_shift[0] = cntr[0];
world2machine_shift[1] = cntr[1];
// No correction.
world2machine_correction_mode = WORLD2MACHINE_CORRECTION_NONE;
if (world2machine_shift[0] != 0.f || world2machine_shift[1] != 0.f)
// Shift correction.
world2machine_correction_mode |= WORLD2MACHINE_CORRECTION_SHIFT;
if (world2machine_rotation_and_skew[0][0] != 1.f || world2machine_rotation_and_skew[0][1] != 0.f ||
world2machine_rotation_and_skew[1][0] != 0.f || world2machine_rotation_and_skew[1][1] != 1.f) {
// Rotation & skew correction.
world2machine_correction_mode |= WORLD2MACHINE_CORRECTION_SKEW;
// Invert the world2machine matrix.
float d = world2machine_rotation_and_skew[0][0] * world2machine_rotation_and_skew[1][1] - world2machine_rotation_and_skew[1][0] * world2machine_rotation_and_skew[0][1];
world2machine_rotation_and_skew_inv[0][0] = world2machine_rotation_and_skew[1][1] / d;
world2machine_rotation_and_skew_inv[0][1] = -world2machine_rotation_and_skew[0][1] / d;
world2machine_rotation_and_skew_inv[1][0] = -world2machine_rotation_and_skew[1][0] / d;
world2machine_rotation_and_skew_inv[1][1] = world2machine_rotation_and_skew[0][0] / d;
} else {
world2machine_rotation_and_skew_inv[0][0] = 1.f;
world2machine_rotation_and_skew_inv[0][1] = 0.f;
world2machine_rotation_and_skew_inv[1][0] = 0.f;
world2machine_rotation_and_skew_inv[1][1] = 1.f;
}
}
/**
* @brief Set calibration matrix to identity
*
* In contrast with world2machine_revert_to_uncorrected(), it doesn't wait for finishing moves
* nor updates the current position with the absolute values.
*/
void world2machine_reset()
{
const float vx[] = { 1.f, 0.f };
const float vy[] = { 0.f, 1.f };
const float cntr[] = { 0.f, 0.f };
world2machine_update(vx, vy, cntr);
}
/**
* @brief Get calibration matrix default value
*
* This is used if no valid calibration data can be read from EEPROM.
* @param [out] vec_x axis x vector
* @param [out] vec_y axis y vector
* @param [out] cntr offset vector
*/
static void world2machine_default(float vec_x[2], float vec_y[2], float cntr[2])
{
vec_x[0] = 1.f;
vec_x[1] = 0.f;
vec_y[0] = 0.f;
vec_y[1] = 1.f;
cntr[0] = 0.f;
#ifdef DEFAULT_Y_OFFSET
cntr[1] = DEFAULT_Y_OFFSET;
#else
cntr[1] = 0.f;
#endif
}
/**
* @brief Set calibration matrix to identity and update current position with absolute position
*
* Wait for the motors to stop and then update the current position with the absolute values.
*/
void world2machine_revert_to_uncorrected()
{
if (world2machine_correction_mode != WORLD2MACHINE_CORRECTION_NONE) {
world2machine_reset();
st_synchronize();
current_position[X_AXIS] = st_get_position_mm(X_AXIS);
current_position[Y_AXIS] = st_get_position_mm(Y_AXIS);
}
}
static inline bool vec_undef(const float v[2])
{
const uint32_t *vx = (const uint32_t*)v;
return vx[0] == 0xFFFFFFFF || vx[1] == 0xFFFFFFFF;
}
/**
* @brief Read calibration data from EEPROM
*
* If no calibration data has been stored in EEPROM or invalid,
* world2machine_default() is used.
*
* If stored calibration data is invalid, EEPROM storage is cleared.
* @param [out] vec_x axis x vector
* @param [out] vec_y axis y vector
* @param [out] cntr offset vector
*/
void world2machine_read_valid(float vec_x[2], float vec_y[2], float cntr[2])
{
eeprom_read_block(&vec_x[0], (float*)(EEPROM_BED_CALIBRATION_VEC_X), 8);
eeprom_read_block(&vec_y[0], (float*)(EEPROM_BED_CALIBRATION_VEC_Y), 8);
eeprom_read_block(&cntr[0], (float*)(EEPROM_BED_CALIBRATION_CENTER), 8);
bool reset = false;
if (vec_undef(cntr) || vec_undef(vec_x) || vec_undef(vec_y))
{
#if 0
SERIAL_ECHOLNPGM("Undefined bed correction matrix.");
#endif
reset = true;
}
else
{
// Length of the vec_x shall be close to unity.
float l = hypot(vec_x[0], vec_x[1]);
if (l < 0.9 || l > 1.1)
{
#if 0
SERIAL_ECHOLNPGM("X vector length:");
MYSERIAL.println(l);
SERIAL_ECHOLNPGM("Invalid bed correction matrix. Length of the X vector out of range.");
#endif
reset = true;
}
// Length of the vec_y shall be close to unity.
l = hypot(vec_y[0], vec_y[1]);
if (l < 0.9 || l > 1.1)
{
#if 0
SERIAL_ECHOLNPGM("Y vector length:");
MYSERIAL.println(l);
SERIAL_ECHOLNPGM("Invalid bed correction matrix. Length of the Y vector out of range.");
#endif
reset = true;
}
// Correction of the zero point shall be reasonably small.
l = hypot(cntr[0], cntr[1]);
if (l > 15.f)
{
#if 0
SERIAL_ECHOLNPGM("Zero point correction:");
MYSERIAL.println(l);
SERIAL_ECHOLNPGM("Invalid bed correction matrix. Shift out of range.");
#endif
reset = true;
}
// vec_x and vec_y shall be nearly perpendicular.
l = vec_x[0] * vec_y[0] + vec_x[1] * vec_y[1];
if (fabs(l) > 0.1f)
{
#if 0
SERIAL_ECHOLNPGM("Invalid bed correction matrix. X/Y axes are far from being perpendicular.");
#endif
reset = true;
}
}
if (reset)
{
#if 0
SERIAL_ECHOLNPGM("Invalid bed correction matrix. Resetting to identity.");
#endif
reset_bed_offset_and_skew();
world2machine_default(vec_x, vec_y, cntr);
}
}
/**
* @brief Read and apply validated calibration data from EEPROM
*/
void world2machine_initialize()
{
#if 0
SERIAL_ECHOLNPGM("world2machine_initialize");
#endif
float vec_x[2];
float vec_y[2];
float cntr[2];
world2machine_read_valid(vec_x, vec_y, cntr);
world2machine_update(vec_x, vec_y, cntr);
#if 0
SERIAL_ECHOPGM("world2machine_initialize() loaded: ");
MYSERIAL.print(world2machine_rotation_and_skew[0][0], 5);
SERIAL_ECHOPGM(", ");
MYSERIAL.print(world2machine_rotation_and_skew[0][1], 5);
SERIAL_ECHOPGM(", ");
MYSERIAL.print(world2machine_rotation_and_skew[1][0], 5);
SERIAL_ECHOPGM(", ");
MYSERIAL.print(world2machine_rotation_and_skew[1][1], 5);
SERIAL_ECHOPGM(", offset ");
MYSERIAL.print(world2machine_shift[0], 5);
SERIAL_ECHOPGM(", ");
MYSERIAL.print(world2machine_shift[1], 5);
SERIAL_ECHOLNPGM("");
#endif
}
/**
* @brief Update current position after switching to corrected coordinates
*
* When switching from absolute to corrected coordinates,
* this will get the absolute coordinates from the servos,
* applies the inverse world2machine transformation
* and stores the result into current_position[x,y].
*/
void world2machine_update_current()
{
float x = current_position[X_AXIS] - world2machine_shift[0];
float y = current_position[Y_AXIS] - world2machine_shift[1];
current_position[X_AXIS] = world2machine_rotation_and_skew_inv[0][0] * x + world2machine_rotation_and_skew_inv[0][1] * y;
current_position[Y_AXIS] = world2machine_rotation_and_skew_inv[1][0] * x + world2machine_rotation_and_skew_inv[1][1] * y;
}
static inline void go_xyz(float x, float y, float z, float fr)
{
plan_buffer_line(x, y, z, current_position[E_AXIS], fr);
st_synchronize();
}
static inline void go_xy(float x, float y, float fr)
{
plan_buffer_line(x, y, current_position[Z_AXIS], current_position[E_AXIS], fr);
st_synchronize();
}
static inline void go_to_current(float fr)
{
plan_buffer_line_curposXYZE(fr);
st_synchronize();
}
static inline void update_current_position_xyz()
{
current_position[X_AXIS] = st_get_position_mm(X_AXIS);
current_position[Y_AXIS] = st_get_position_mm(Y_AXIS);
current_position[Z_AXIS] = st_get_position_mm(Z_AXIS);
plan_set_position_curposXYZE();
}
static inline void update_current_position_z()
{
current_position[Z_AXIS] = st_get_position_mm(Z_AXIS);
plan_set_z_position(current_position[Z_AXIS]);
}
// At the current position, find the Z stop.
bool find_bed_induction_sensor_point_z(float minimum_z, uint8_t n_iter, int
#ifdef SUPPORT_VERBOSITY
verbosity_level
#endif //SUPPORT_VERBOSITY
)
{
bool high_deviation_occured = false;
bedPWMDisabled = 1;
#ifdef TMC2130
bool bHighPowerForced = false;
if (tmc2130_mode == TMC2130_MODE_SILENT)
{
FORCE_HIGH_POWER_START;
bHighPowerForced = true;
}
#endif
//printf_P(PSTR("Min. Z: %f\n"), minimum_z);
#ifdef SUPPORT_VERBOSITY
if(verbosity_level >= 10) SERIAL_ECHOLNPGM("find bed induction sensor point z");
#endif // SUPPORT_VERBOSITY
bool endstops_enabled = enable_endstops(true);
bool endstop_z_enabled = enable_z_endstop(false);
float z = 0.f;
endstop_z_hit_on_purpose();
// move down until you find the bed
current_position[Z_AXIS] = minimum_z;
go_to_current(homing_feedrate[Z_AXIS]/60);
// we have to let the planner know where we are right now as it is not where we said to go.
update_current_position_z();
if (! endstop_z_hit_on_purpose())
{
//printf_P(PSTR("endstop not hit 1, current_pos[Z]: %f \n"), current_position[Z_AXIS]);
goto error;
}
#ifdef TMC2130
if (!READ(Z_TMC2130_DIAG))
{
//printf_P(PSTR("crash detected 1, current_pos[Z]: %f \n"), current_position[Z_AXIS]);
goto error; //crash Z detected
}
#endif //TMC2130
for (uint8_t i = 0; i < n_iter; ++ i)
{
current_position[Z_AXIS] += high_deviation_occured ? 0.5 : 0.2;
float z_bckp = current_position[Z_AXIS];
go_to_current(homing_feedrate[Z_AXIS]/60);
// Move back down slowly to find bed.
current_position[Z_AXIS] = minimum_z;
//printf_P(PSTR("init Z = %f, min_z = %f, i = %d\n"), z_bckp, minimum_z, i);
go_to_current(homing_feedrate[Z_AXIS]/(4*60));
// we have to let the planner know where we are right now as it is not where we said to go.
update_current_position_z();
//printf_P(PSTR("Zs: %f, Z: %f, delta Z: %f"), z_bckp, current_position[Z_AXIS], (z_bckp - current_position[Z_AXIS]));
if (fabs(current_position[Z_AXIS] - z_bckp) < 0.025) {
//printf_P(PSTR("PINDA triggered immediately, move Z higher and repeat measurement\n"));
raise_z(0.5);
current_position[Z_AXIS] = minimum_z;
go_to_current(homing_feedrate[Z_AXIS]/(4*60));
// we have to let the planner know where we are right now as it is not where we said to go.
update_current_position_z();
}
if (!endstop_z_hit_on_purpose())
{
//printf_P(PSTR("i = %d, endstop not hit 2, current_pos[Z]: %f \n"), i, current_position[Z_AXIS]);
goto error;