Commit 2726bc9c authored by Torbjørn Ludvigsen's avatar Torbjørn Ludvigsen 👷

Makes output of script easier to use

parent 4807cada
......@@ -30,54 +30,47 @@ python ./simulation.py
```
If it works, then your output looks similar to
```python
cost: 0.254896
Output Anchors:
[[ 0. -1164.30732212 -143.53179478]
[ 998.78058643 585.33185503 -114.97805252]
[ -977.11333826 518.87558222 -105.60105899]
[ 0. 0. 2874.86861506]]
Errors:
[[ 0.00000000e+00 -5.23073221e+01 -2.85317948e+01]
[ 2.87805864e+01 3.53318550e+01 2.19474805e-02]
[ -7.11333826e+00 -3.11244178e+01 9.39894101e+00]
[ 0.00000000e+00 0.00000000e+00 9.86861506e+00]]
```
samples: 11
total cost: 0.254896
cost per sample: 0.023172
#define ANCHOR_A_Y -1164
#define ANCHOR_A_Z -144
#define ANCHOR_B_X 999
#define ANCHOR_B_Y 585
#define ANCHOR_B_Z -115
#define ANCHOR_C_X -977
#define ANCHOR_C_Y 519
#define ANCHOR_C_Z -106
#define ANCHOR_D_Z 2875
M665 W-1164.31 E-143.53 R998.78 T585.33 Y-114.98 U-977.11 I518.88 O-105.60 P2874.87
```
Note that these values are only test data and does not correspond to your Hangprinter setup (yet).
## How to Collect Data Points?
Data collection depends on Mechaduinos and well calibrated line buildup compensation.
As of Jan 31, 2018, this is the procedure (using HangprinterMarlin, not stock Marlin yet).
As of Jan 31, 2018, this is the procedure:
- Go into torque mode on all motors: `G95 A35 B35 C35 D35`.
Adjust torque magnitude as you prefer.
- Drag mover to the origin and zero counters: `G92 X0 Y0 Z0`
- Mark reference point for all encoders: `G96 A B C D`
- Repeat 10 - 20 times:
- Mark reference point for all encoders: `G96 A B C D` (Stock Marlin accepts `G96` as a short hand for `G96 A B C D`)
- Repeat 12 - ca 20 times:
- Drag mover to position of data point collection.
- Collect data point: `M114 S1` (in firmware before Feb 6, 2018: `G97 A B C D`)
- Collect data point: `M114 S1` (Old firmwares, before Feb 6, 2018 used: `G97 A B C D`)
## How to Insert Data Points?
Before you run the simulation, open `simulation.py` and modify the main function.
Replace `?` with your approximated values (not mandatory but useful).
Replace `??` with data points collected with your Hangprinter.
At least 10 data points at a mean distance ~1m from the origin are recommended.
```python
if __name__ == "__main__":
# Rough approximations from manual measuring.
# Does not affect optimization result. Only used for manual sanity check.
az = ?
bz = ?
cz = ?
anchors = np.array([[ 0.0, ?, az],
[ ?, ?, bz],
[ ?, ?, cz],
[ 0.0, 0.0, ?]])
...
# Replace this with your collected data
samp = np.array([
[??, ??, ??, ??],
[??, ??, ??, ??]
])
...
```
When values are inserted, run again with
```bash
......@@ -85,23 +78,62 @@ python ./simulation.py
```
## Output Explanation
```python
cost: 0.254896
Output Anchors:
[[ 0. ANCHOR_A_Y ANCHOR_A_Z]
[ ANCHOR_B_X ANCHOR_B_Y ANCHOR_B_Z]
[ ANCHOR_C_X ANCHOR_C_Y ANCHOR_C_Z]
[ 0. 0. ANCHOR_D_Z]]
Errors:
[[ 0. err_A_y err_A_z]
[ err_B_x err_B_y err_B_z]
[ err_C_x err_C_y err_C_z]
[ 0. 0. err_D_z]]
The first block give some stats trying to describe the quality of the output parameters
```
samples: 11
total cost: 0.254896
cost per sample: 0.023172
```
It's recommended to use 12 samples or more.
Using fewer samples makes it probable that the solver finds bogus anchor positions that still minimizes cost.
Ideal data points collected on an ideal machine would give `cost: 0.0`.
Ideal data points collected on an ideal machine would give `total cost: 0.000000` for any sample size.
In real life this does not happen.
The cost value is there to let you compare your different data sets of equal size.
The `Output Anchors`-set associated with the lowest cost is generally your best one.
The `Errors` is your manual sanity check.
`Errors` are calculated by differentiating with your approximations denoted `?` above.
The `cost per sample` value let you compare results from your different data sets of unequal size.
The second block contains the anchor positions that the script found.
They are formatted so they can be pasted directly into Marlin's `Configuration.h`.
```
#define ANCHOR_A_Y -1164
#define ANCHOR_A_Z -144
#define ANCHOR_B_X 999
#define ANCHOR_B_Y 585
#define ANCHOR_B_Z -115
#define ANCHOR_C_X -977
#define ANCHOR_C_Y 519
#define ANCHOR_C_Z -106
#define ANCHOR_D_Z 2875
```
The gcode line that is the third block can be used to set anchor calibration values on a running Hangprinter without re-uploading firmware.
```
M665 W-1164.31 E-143.53 R998.78 T585.33 Y-114.98 U-977.11 I518.88 O-105.60 P2874.87
```
If you have `EEPROM_SETTINGS` enabled you can save these values with `M500`.
If you don't save them they will be reset when you restart your machine.
## Debug
The script accepts a `-d` or `--debug` flag.
It calculates the difference between your manually measured anchor positions, and the anchor positions calculated by the script:
```
Err_A_Y: -52.307
Err_A_Z: -28.532
Err_B_X: 28.781
Err_B_Y: 35.332
Err_B_Z: 0.022
Err_C_X: -7.113
Err_C_Y: -31.124
Err_C_Z: 9.399
Err_D_Z: 9.869
```
For these values to be meaningful you must have inserted your manually measured values into the `anchors` array in the script:
```python
# Rough approximations from manual measuring.
# Does not affect optimization result. Only used for manual sanity check.
anchors = np.array([[ 0.0, ay?, az?],
[ bx?, by?, bz?],
[ cx?, cy?, cz?],
[ 0.0, 0.0, dz?]])
```
......@@ -2,6 +2,7 @@
"""
from __future__ import division # Always want 3/2 = 1.5
import numpy as np
import argparse
# Tips on how to use differential solver:
# build/lib.linux-x86_64-2.7/mystic/differential_evolution.py
......@@ -154,10 +155,13 @@ def cost(anchors, pos, samp):
def cost_sq(anchors, pos, samp):
"""
(A_ax-x_i)^2 + (A_ay-y_i)^2 + (A_az-z_i)^2 - (A_ax^2 + A_ay^2 + A_az^2) - t_ia +
(A_bx-x_i)^2 + (A_by-y_i)^2 + (A_bz-z_i)^2 - (A_bx^2 + A_by^2 + A_bz^2) - t_ib +
(A_cx-x_i)^2 + (A_cy-y_i)^2 + (A_cz-z_i)^2 - (A_cx^2 + A_cy^2 + A_cz^2) - t_ic +
(A_dx-x_i)^2 + (A_dy-y_i)^2 + (A_dz-z_i)^2 - (A_dx^2 + A_dy^2 + A_dz^2) - t_id
For all samples sum
(Sample value if anchor position A and cartesian position x were guessed - actual sample)^2
(sqrt((A_ax-x_i)^2 + (A_ay-y_i)^2 + (A_az-z_i)^2) - sqrt(A_ax^2 + A_ay^2 + A_az^2) - t_ia)^2 +
(sqrt((A_bx-x_i)^2 + (A_by-y_i)^2 + (A_bz-z_i)^2) - sqrt(A_bx^2 + A_by^2 + A_bz^2) - t_ib)^2 +
(sqrt((A_cx-x_i)^2 + (A_cy-y_i)^2 + (A_cz-z_i)^2) - sqrt(A_cx^2 + A_cy^2 + A_cz^2) - t_ic)^2 +
(sqrt((A_dx-x_i)^2 + (A_dy-y_i)^2 + (A_dz-z_i)^2) - sqrt(A_dx^2 + A_dy^2 + A_dz^2) - t_id)^2
"""
return np.sum(pow((samples_relative_to_origo_no_fuzz(anchors, pos) - samp), 2)) # Sum of squares
......@@ -275,17 +279,40 @@ def solve(samp, _cost = cost_sq):
return solver0.bestSolution
def print_anch(anch):
print("\n#define ANCHOR_A_Y %5d" % round(anch[A,Y]))
print("#define ANCHOR_A_Z %5d" % round(anch[A,Z]))
print("#define ANCHOR_B_X %5d" % round(anch[B,X]))
print("#define ANCHOR_B_Y %5d" % round(anch[B,Y]))
print("#define ANCHOR_B_Z %5d" % round(anch[B,Z]))
print("#define ANCHOR_C_X %5d" % round(anch[C,X]))
print("#define ANCHOR_C_Y %5d" % round(anch[C,Y]))
print("#define ANCHOR_C_Z %5d" % round(anch[C,Z]))
print("#define ANCHOR_D_Z %5d" % round(anch[D,Z]))
print("\nM665 W%.2f E%.2f R%.2f T%.2f Y%.2f U%.2f I%.2f O%.2f P%.2f" % (anch[A,Y],anch[A,Z],anch[B,X],anch[B,Y],anch[B,Z],anch[C,X],anch[C,Y],anch[C,Z],anch[D,Z]))
def print_anch_err(sol_anch, anchors):
print("\nErr_A_Y: %9.3f" % (sol_anch[A,Y] - anchors[A,Y]))
print("Err_A_Z: %9.3f" % (sol_anch[A,Z] - anchors[A,Z]))
print("Err_B_X: %9.3f" % (sol_anch[B,X] - anchors[B,X]))
print("Err_B_Y: %9.3f" % (sol_anch[B,Y] - anchors[B,Y]))
print("Err_B_Z: %9.3f" % (sol_anch[B,Z] - anchors[B,Z]))
print("Err_C_X: %9.3f" % (sol_anch[C,X] - anchors[C,X]))
print("Err_C_Y: %9.3f" % (sol_anch[C,Y] - anchors[C,Y]))
print("Err_C_Z: %9.3f" % (sol_anch[C,Z] - anchors[C,Z]))
print("Err_D_Z: %9.3f" % (sol_anch[D,Z] - anchors[D,Z]))
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Figure out where Hangprinter anchors are by looking at line difference samples.')
parser.add_argument('-d', '--debug', help='Print debug information', action='store_true')
args = vars(parser.parse_args())
# Rough approximations from manual measuring.
# Does not affect optimization result. Only used for manual sanity check.
az = -115.
bz = -115.
cz = -115.
anchors = np.array([[ 0.0, -1112.0, az],
[ 970.0, 550.0, bz],
[-970.0, 550.0, cz],
anchors = np.array([[ 0.0, -1112.0, -115.],
[ 970.0, 550.0, -115.],
[-970.0, 550.0, -115.],
[ 0.0, 0.0, 2865.0]])
# Replace this with your collected data
samp = np.array([
[400.53 , 175.53 , 166.10 , -656.90],
......@@ -307,8 +334,10 @@ if __name__ == "__main__":
solution = solve(samp, cost_sq)
sol_anch = anchorsvec2matrix(solution[0:params_anch])
the_cost = cost_sq(anchorsvec2matrix(solution[0:params_anch]), np.reshape(solution[params_anch:], (u,3)), samp)
print("cost: %f" % the_cost)
print("Output Anchors: ")
print(sol_anch)
print("Errors: ")
print(sol_anch - anchors)
print("samples: %d" % u)
print("total cost: %f" % the_cost)
print("cost per sample: %f" % (the_cost/u))
print_anch(sol_anch)
if (args['debug']):
print_anch_err(sol_anch, anchors)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment