z_probe.cpp

changeset 0
2c8ba1964db7
child 1
b584642d4f58
equal deleted inserted replaced
-1:000000000000 0:2c8ba1964db7
1 #include "z_probe.h"
2 #if defined(PROBE_PIN) && (PROBE_PIN > -1)
3 #include "Marlin.h"
4 #include "stepper.h"
5 #include "temperature.h"
6
7 float Probe_Bed(float x_pos, float y_pos, int n)
8 {
9 //returns Probed Z average height
10 float ProbeDepth[n];
11 float ProbeDepthAvg=0;
12
13 //force bed heater off for probing
14 int save_bed_targ = target_raw_bed;
15 target_raw_bed = 0;
16 WRITE(HEATER_BED_PIN,LOW);
17
18 if (Z_HOME_DIR==-1)
19 {
20 //int probe_flag =1;
21 float meas = 0;
22 int fails = 0;
23 saved_feedrate = feedrate;
24 saved_feedmultiply = feedmultiply;
25 feedmultiply = 100;
26 //previous_millis_cmd = millis();
27
28 //Move to probe position
29 if (x_pos >= 0) destination[X_AXIS]=x_pos;
30 if (y_pos >= 0) destination[Y_AXIS]=y_pos;
31 //destination[Z_AXIS]=current_position[Z_AXIS];
32 destination[Z_AXIS]=Z_HOME_RETRACT_MM;
33 feedrate = 9000;
34 prepare_move();
35
36 enable_endstops(true);
37 SERIAL_ECHO("PRE-PROBE current_position[Z_AXIS]=");SERIAL_ECHOLN(current_position[Z_AXIS]);
38
39 SERIAL_ECHOLN("Ready to probe...");
40
41 //Probe bed n times
42 //*******************************************************************************************Bed Loop*************************************
43 for(int8_t i=0; i < n ; i++)
44 {
45 //int z = 0;
46
47 //fast probe
48 //plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
49 destination[Z_AXIS] = 1.1 * max_length[Z_AXIS] * Z_HOME_DIR;
50 feedrate = homing_feedrate[Z_AXIS];
51 plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
52 st_synchronize();
53
54 //feedrate = 0.0;
55
56 SERIAL_ECHO("current_position[Z_AXIS]=");SERIAL_ECHOLN(current_position[Z_AXIS]);
57 if(endstop_z_hit == true)
58 {
59 SERIAL_ECHO("endstops_trigsteps[Z_AXIS]=");SERIAL_ECHOLN(endstops_trigsteps[Z_AXIS]);
60 ProbeDepth[i]= endstops_trigsteps[Z_AXIS] / axis_steps_per_unit[Z_AXIS];
61 meas = ProbeDepth[i];
62 SERIAL_ECHO("ProbeDepth[");SERIAL_ECHO(i);SERIAL_ECHO("]=");SERIAL_ECHOLN(ProbeDepth[i]);
63 //*************************************************************************************************************
64 if (i > 0 ) //Second probe has happened so compare results
65 {
66 if (abs(ProbeDepth[i] - ProbeDepth[i - 1]) > .05)
67 { //keep going until readings match to avoid sticky bed
68 SERIAL_ECHO("Probing again: ");
69 SERIAL_ECHO(ProbeDepth[i]); SERIAL_ECHO(" - "); SERIAL_ECHO(ProbeDepth[i - 1]);SERIAL_ECHO(" = "); SERIAL_ECHOLN(abs(ProbeDepth[i] - ProbeDepth[i - 1]));
70 meas = ProbeDepth[i];
71 i--; i--; //Throw out both that don't match because we don't know which one is accurate
72 if(fails++ > 4) break;
73 }
74 }
75 }else{
76 SERIAL_ECHOLN("Probe not triggered.");
77 i=n-1;
78 }
79 //**************************************************************************************************************************************************
80 //fast move clear
81 plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], meas, current_position[E_AXIS]);
82 destination[Z_AXIS] = Z_HOME_RETRACT_MM;
83 feedrate = fast_home_feedrate[Z_AXIS];
84 plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
85 st_synchronize();
86
87 //check z stop isn't still triggered
88 if ( READ(X_MIN_PIN) != X_ENDSTOPS_INVERTING )
89 {
90 SERIAL_ECHOLN("Poking Stuck Bed:");
91 destination[Z_AXIS] = -1; prepare_move();
92 destination[Z_AXIS] = Z_HOME_RETRACT_MM; prepare_move();
93 st_synchronize();
94 i--; //Throw out this meaningless measurement
95 }
96 feedrate = 0;
97 } //end probe loop
98 #ifdef ENDSTOPS_ONLY_FOR_HOMING
99 enable_endstops(false);
100 #endif
101
102 feedrate = saved_feedrate;
103 feedmultiply = saved_feedmultiply;
104 //previous_millis_cmd = millis();
105 endstops_hit_on_purpose();
106 }
107 for(int8_t i=0;i<n;i++)
108 {
109 ProbeDepthAvg += ProbeDepth[i];
110 }
111 ProbeDepthAvg /= n;
112 SERIAL_ECHO("Probed Z="); SERIAL_ECHOLN(ProbeDepthAvg);
113 SERIAL_ECHO("RAW current_position[Z_AXIS]=");SERIAL_ECHOLN(current_position[Z_AXIS]);
114 plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], Z_HOME_RETRACT_MM, current_position[E_AXIS]);
115 current_position[Z_AXIS] = Z_HOME_RETRACT_MM;
116
117 target_raw_bed = save_bed_targ;
118 return ProbeDepthAvg;
119 }
120
121 void probe_init()
122 {
123 SET_INPUT(PROBE_PIN);
124 WRITE(PROBE_PIN,HIGH);
125 }
126
127 /*Crash1 - G29 to Probe and stop on Bed
128 G29 will probe bed at least twice at 3 points and take an average. G30 will probe bed at it's current location.
129 Z stop should be set slightly below bed height. Solder stub wire to each hole in huxley bed and attach a ring terminal under spring.
130 Wire bed probe to A2 on Melzi and duplicate cap/resistor circuit in schematic.
131
132 Use something like this in the start.gcode file:
133 G29 ;Probe bed for Z height
134 G92 Z0 ;Set Z to Probed Depth
135 G1 Z5 F200 ;Lift Z out of way
136 */
137 void probe_3points()
138 {
139 float Probe_Avg, Point1, Point2, Point3;
140 Point1 = Probe_Bed(max_length[X_AXIS] - 15,15,PROBE_N);
141 Point2 = Probe_Bed(max_length[X_AXIS] - 15,max_length[Y_AXIS] - 15,PROBE_N) ;
142 Point3 = Probe_Bed(15,max_length[Y_AXIS] / 2,PROBE_N);
143 Probe_Avg = (Point1 + Point2 + Point3) / 3;
144 //destination[2] = Probe_Avg;
145 //feedrate = homing_feedrate[Z_AXIS];
146 //prepare_move();
147 SERIAL_ECHOLN("**************************************");
148 SERIAL_ECHO("Point1 ="); SERIAL_ECHOLN(Point1);
149 SERIAL_ECHO("Point2 ="); SERIAL_ECHOLN(Point2);
150 SERIAL_ECHO("Point3 ="); SERIAL_ECHOLN(Point3);
151 SERIAL_ECHO("Probed Average="); SERIAL_ECHOLN(Probe_Avg);
152 SERIAL_ECHOLN("**************************************");
153 }
154
155 void probe_1point()
156 {
157 float Point;
158 Point = Probe_Bed(-1,-1,PROBE_N);
159 //destination[2] = Point +1;
160 //feedrate = homing_feedrate[Z_AXIS];
161 //prepare_move();
162 SERIAL_ECHOLN("**************************************");
163 SERIAL_ECHO("Probed Z="); SERIAL_ECHOLN(Point);
164 }
165
166 void probe_status()
167 {
168 SERIAL_ECHO("Probe Status = "); SERIAL_ECHOLN(READ(PROBE_PIN));
169 }
170
171 #endif //defined(PROBE_PIN) > -1
172
173

mercurial