Commit | Line | Data |
---|---|---|
df27a6a3 | 1 | /* |
201bcb94 AW |
2 | This file is part of Smoothie (http://smoothieware.org/). The motion control part is heavily based on Grbl (https://github.com/simen/grbl). |
3 | Smoothie is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. | |
4 | Smoothie is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. | |
df27a6a3 | 5 | You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>. |
201bcb94 AW |
6 | */ |
7 | ||
8 | #include "libs/Module.h" | |
9 | #include "libs/Kernel.h" | |
10 | #include "modules/communication/utils/Gcode.h" | |
3fceb8eb | 11 | #include "modules/robot/Conveyor.h" |
807b9b57 | 12 | #include "modules/robot/ActuatorCoordinates.h" |
201bcb94 AW |
13 | #include "Endstops.h" |
14 | #include "libs/nuts_bolts.h" | |
750277f8 | 15 | #include "libs/Pin.h" |
670fa10b | 16 | #include "libs/StepperMotor.h" |
201bcb94 | 17 | #include "wait_api.h" // mbed.h lib |
61134a65 | 18 | #include "Robot.h" |
61134a65 JM |
19 | #include "Config.h" |
20 | #include "SlowTicker.h" | |
21 | #include "Planner.h" | |
7af0714f JM |
22 | #include "checksumm.h" |
23 | #include "utils.h" | |
8d54c34c | 24 | #include "ConfigValue.h" |
fc7b9a7b | 25 | #include "libs/StreamOutput.h" |
9f6f04a5 JM |
26 | #include "PublicDataRequest.h" |
27 | #include "EndstopsPublicAccess.h" | |
3c947f85 | 28 | #include "StreamOutputPool.h" |
a157d099 | 29 | #include "StepTicker.h" |
7552475b | 30 | #include "BaseSolution.h" |
e551657a | 31 | #include "SerialMessage.h" |
201bcb94 | 32 | |
80605954 JM |
33 | #include <ctype.h> |
34 | ||
33e4cc02 JM |
35 | #define ALPHA_AXIS 0 |
36 | #define BETA_AXIS 1 | |
37 | #define GAMMA_AXIS 2 | |
9a993543 JM |
38 | #define X_AXIS 0 |
39 | #define Y_AXIS 1 | |
40 | #define Z_AXIS 2 | |
33e4cc02 | 41 | |
33e4cc02 JM |
42 | #define endstops_module_enable_checksum CHECKSUM("endstops_enable") |
43 | #define corexy_homing_checksum CHECKSUM("corexy_homing") | |
44 | #define delta_homing_checksum CHECKSUM("delta_homing") | |
11a39396 | 45 | #define rdelta_homing_checksum CHECKSUM("rdelta_homing") |
3e1f5b74 | 46 | #define scara_homing_checksum CHECKSUM("scara_homing") |
33e4cc02 JM |
47 | |
48 | #define alpha_min_endstop_checksum CHECKSUM("alpha_min_endstop") | |
49 | #define beta_min_endstop_checksum CHECKSUM("beta_min_endstop") | |
50 | #define gamma_min_endstop_checksum CHECKSUM("gamma_min_endstop") | |
51 | ||
52 | #define alpha_max_endstop_checksum CHECKSUM("alpha_max_endstop") | |
53 | #define beta_max_endstop_checksum CHECKSUM("beta_max_endstop") | |
54 | #define gamma_max_endstop_checksum CHECKSUM("gamma_max_endstop") | |
55 | ||
56 | #define alpha_trim_checksum CHECKSUM("alpha_trim") | |
57 | #define beta_trim_checksum CHECKSUM("beta_trim") | |
58 | #define gamma_trim_checksum CHECKSUM("gamma_trim") | |
59 | ||
abf706e6 JM |
60 | #define alpha_max_travel_checksum CHECKSUM("alpha_max_travel") |
61 | #define beta_max_travel_checksum CHECKSUM("beta_max_travel") | |
62 | #define gamma_max_travel_checksum CHECKSUM("gamma_max_travel") | |
63 | ||
33e4cc02 JM |
64 | // these values are in steps and should be deprecated |
65 | #define alpha_fast_homing_rate_checksum CHECKSUM("alpha_fast_homing_rate") | |
66 | #define beta_fast_homing_rate_checksum CHECKSUM("beta_fast_homing_rate") | |
67 | #define gamma_fast_homing_rate_checksum CHECKSUM("gamma_fast_homing_rate") | |
68 | ||
69 | #define alpha_slow_homing_rate_checksum CHECKSUM("alpha_slow_homing_rate") | |
70 | #define beta_slow_homing_rate_checksum CHECKSUM("beta_slow_homing_rate") | |
71 | #define gamma_slow_homing_rate_checksum CHECKSUM("gamma_slow_homing_rate") | |
72 | ||
73 | #define alpha_homing_retract_checksum CHECKSUM("alpha_homing_retract") | |
74 | #define beta_homing_retract_checksum CHECKSUM("beta_homing_retract") | |
75 | #define gamma_homing_retract_checksum CHECKSUM("gamma_homing_retract") | |
33e4cc02 JM |
76 | |
77 | // same as above but in user friendly mm/s and mm | |
78 | #define alpha_fast_homing_rate_mm_checksum CHECKSUM("alpha_fast_homing_rate_mm_s") | |
79 | #define beta_fast_homing_rate_mm_checksum CHECKSUM("beta_fast_homing_rate_mm_s") | |
80 | #define gamma_fast_homing_rate_mm_checksum CHECKSUM("gamma_fast_homing_rate_mm_s") | |
81 | ||
82 | #define alpha_slow_homing_rate_mm_checksum CHECKSUM("alpha_slow_homing_rate_mm_s") | |
83 | #define beta_slow_homing_rate_mm_checksum CHECKSUM("beta_slow_homing_rate_mm_s") | |
84 | #define gamma_slow_homing_rate_mm_checksum CHECKSUM("gamma_slow_homing_rate_mm_s") | |
85 | ||
86 | #define alpha_homing_retract_mm_checksum CHECKSUM("alpha_homing_retract_mm") | |
87 | #define beta_homing_retract_mm_checksum CHECKSUM("beta_homing_retract_mm") | |
88 | #define gamma_homing_retract_mm_checksum CHECKSUM("gamma_homing_retract_mm") | |
89 | ||
90 | #define endstop_debounce_count_checksum CHECKSUM("endstop_debounce_count") | |
7778d1ce | 91 | #define endstop_debounce_ms_checksum CHECKSUM("endstop_debounce_ms") |
33e4cc02 JM |
92 | |
93 | #define alpha_homing_direction_checksum CHECKSUM("alpha_homing_direction") | |
94 | #define beta_homing_direction_checksum CHECKSUM("beta_homing_direction") | |
95 | #define gamma_homing_direction_checksum CHECKSUM("gamma_homing_direction") | |
1c658603 | 96 | |
33e4cc02 JM |
97 | #define alpha_min_checksum CHECKSUM("alpha_min") |
98 | #define beta_min_checksum CHECKSUM("beta_min") | |
99 | #define gamma_min_checksum CHECKSUM("gamma_min") | |
100 | ||
101 | #define alpha_max_checksum CHECKSUM("alpha_max") | |
102 | #define beta_max_checksum CHECKSUM("beta_max") | |
103 | #define gamma_max_checksum CHECKSUM("gamma_max") | |
104 | ||
3c947f85 JM |
105 | #define alpha_limit_enable_checksum CHECKSUM("alpha_limit_enable") |
106 | #define beta_limit_enable_checksum CHECKSUM("beta_limit_enable") | |
107 | #define gamma_limit_enable_checksum CHECKSUM("gamma_limit_enable") | |
108 | ||
374d0777 | 109 | #define home_z_first_checksum CHECKSUM("home_z_first") |
80605954 | 110 | #define homing_order_checksum CHECKSUM("homing_order") |
2ddf75fd | 111 | #define move_to_origin_checksum CHECKSUM("move_to_origin_after_home") |
80605954 | 112 | |
c8bac202 | 113 | #define STEPPER THEROBOT->actuators |
dd0a7cfa | 114 | #define STEPS_PER_MM(a) (STEPPER[a]->get_steps_per_mm()) |
56ce2b5a | 115 | |
2b3cedc7 | 116 | |
3c947f85 | 117 | // Homing States |
e714bd32 | 118 | enum { |
2ddf75fd | 119 | MOVING_TO_ENDSTOP_FAST, // homing move |
2ddf75fd | 120 | MOVING_TO_ENDSTOP_SLOW, // homing move |
98e30679 | 121 | MOVING_BACK, // homing move |
fafb45df | 122 | NOT_HOMING, |
3c947f85 | 123 | BACK_OFF_HOME, |
2ddf75fd | 124 | MOVE_TO_ORIGIN, |
3c947f85 JM |
125 | LIMIT_TRIGGERED |
126 | }; | |
127 | ||
33e4cc02 JM |
128 | Endstops::Endstops() |
129 | { | |
201bcb94 | 130 | this->status = NOT_HOMING; |
33e4cc02 | 131 | home_offset[0] = home_offset[1] = home_offset[2] = 0.0F; |
c8bac202 | 132 | debounce.fill(0); |
fdfa00d2 | 133 | homed.reset(); |
201bcb94 AW |
134 | } |
135 | ||
33e4cc02 JM |
136 | void Endstops::on_module_loaded() |
137 | { | |
7dee696d | 138 | // Do not do anything if not enabled |
314ab8f7 | 139 | if ( THEKERNEL->config->value( endstops_module_enable_checksum )->by_default(true)->as_bool() == false ) { |
4e722c00 | 140 | delete this; |
33e4cc02 JM |
141 | return; |
142 | } | |
7d62445b | 143 | |
9f6f04a5 JM |
144 | register_for_event(ON_GCODE_RECEIVED); |
145 | register_for_event(ON_GET_PUBLIC_DATA); | |
7d6fe308 | 146 | register_for_event(ON_SET_PUBLIC_DATA); |
201bcb94 | 147 | |
750277f8 | 148 | // Settings |
7492a02e | 149 | this->load_config(); |
c8bac202 JM |
150 | |
151 | THEKERNEL->slow_ticker->attach(1000, this, &Endstops::read_endstops); | |
201bcb94 AW |
152 | } |
153 | ||
750277f8 | 154 | // Get config |
7492a02e | 155 | void Endstops::load_config() |
33e4cc02 | 156 | { |
56ce2b5a JM |
157 | this->pins[0].from_string( THEKERNEL->config->value(alpha_min_endstop_checksum )->by_default("nc" )->as_string())->as_input(); |
158 | this->pins[1].from_string( THEKERNEL->config->value(beta_min_endstop_checksum )->by_default("nc" )->as_string())->as_input(); | |
159 | this->pins[2].from_string( THEKERNEL->config->value(gamma_min_endstop_checksum )->by_default("nc" )->as_string())->as_input(); | |
160 | this->pins[3].from_string( THEKERNEL->config->value(alpha_max_endstop_checksum )->by_default("nc" )->as_string())->as_input(); | |
161 | this->pins[4].from_string( THEKERNEL->config->value(beta_max_endstop_checksum )->by_default("nc" )->as_string())->as_input(); | |
162 | this->pins[5].from_string( THEKERNEL->config->value(gamma_max_endstop_checksum )->by_default("nc" )->as_string())->as_input(); | |
163 | ||
164 | // These are the old ones in steps still here for backwards compatibility | |
165 | this->fast_rates[0] = THEKERNEL->config->value(alpha_fast_homing_rate_checksum )->by_default(4000 )->as_number() / STEPS_PER_MM(0); | |
166 | this->fast_rates[1] = THEKERNEL->config->value(beta_fast_homing_rate_checksum )->by_default(4000 )->as_number() / STEPS_PER_MM(1); | |
167 | this->fast_rates[2] = THEKERNEL->config->value(gamma_fast_homing_rate_checksum )->by_default(6400 )->as_number() / STEPS_PER_MM(2); | |
168 | this->slow_rates[0] = THEKERNEL->config->value(alpha_slow_homing_rate_checksum )->by_default(2000 )->as_number() / STEPS_PER_MM(0); | |
169 | this->slow_rates[1] = THEKERNEL->config->value(beta_slow_homing_rate_checksum )->by_default(2000 )->as_number() / STEPS_PER_MM(1); | |
170 | this->slow_rates[2] = THEKERNEL->config->value(gamma_slow_homing_rate_checksum )->by_default(3200 )->as_number() / STEPS_PER_MM(2); | |
171 | this->retract_mm[0] = THEKERNEL->config->value(alpha_homing_retract_checksum )->by_default(400 )->as_number() / STEPS_PER_MM(0); | |
172 | this->retract_mm[1] = THEKERNEL->config->value(beta_homing_retract_checksum )->by_default(400 )->as_number() / STEPS_PER_MM(1); | |
173 | this->retract_mm[2] = THEKERNEL->config->value(gamma_homing_retract_checksum )->by_default(1600 )->as_number() / STEPS_PER_MM(2); | |
5de98d7c JM |
174 | |
175 | // newer mm based config values override the old ones, convert to steps/mm and steps, defaults to what was set in the older config settings above | |
56ce2b5a JM |
176 | this->fast_rates[0] = THEKERNEL->config->value(alpha_fast_homing_rate_mm_checksum )->by_default(this->fast_rates[0])->as_number(); |
177 | this->fast_rates[1] = THEKERNEL->config->value(beta_fast_homing_rate_mm_checksum )->by_default(this->fast_rates[1])->as_number(); | |
178 | this->fast_rates[2] = THEKERNEL->config->value(gamma_fast_homing_rate_mm_checksum )->by_default(this->fast_rates[2])->as_number(); | |
179 | this->slow_rates[0] = THEKERNEL->config->value(alpha_slow_homing_rate_mm_checksum )->by_default(this->slow_rates[0])->as_number(); | |
180 | this->slow_rates[1] = THEKERNEL->config->value(beta_slow_homing_rate_mm_checksum )->by_default(this->slow_rates[1])->as_number(); | |
181 | this->slow_rates[2] = THEKERNEL->config->value(gamma_slow_homing_rate_mm_checksum )->by_default(this->slow_rates[2])->as_number(); | |
182 | this->retract_mm[0] = THEKERNEL->config->value(alpha_homing_retract_mm_checksum )->by_default(this->retract_mm[0])->as_number(); | |
183 | this->retract_mm[1] = THEKERNEL->config->value(beta_homing_retract_mm_checksum )->by_default(this->retract_mm[1])->as_number(); | |
184 | this->retract_mm[2] = THEKERNEL->config->value(gamma_homing_retract_mm_checksum )->by_default(this->retract_mm[2])->as_number(); | |
5de98d7c | 185 | |
7778d1ce | 186 | // NOTE the debouce count is in milliseconds so probably does not need to beset anymore |
1c658603 JM |
187 | this->debounce_ms= THEKERNEL->config->value(endstop_debounce_ms_checksum )->by_default(0)->as_number(); |
188 | this->debounce_count= THEKERNEL->config->value(endstop_debounce_count_checksum )->by_default(100)->as_number(); | |
5de98d7c | 189 | |
409ff5b3 | 190 | // get homing direction and convert to boolean where true is home to min, and false is home to max |
1c658603 JM |
191 | this->home_direction[0]= THEKERNEL->config->value(alpha_homing_direction_checksum)->by_default("home_to_min")->as_string() != "home_to_max"; |
192 | this->home_direction[1]= THEKERNEL->config->value(beta_homing_direction_checksum)->by_default("home_to_min")->as_string() != "home_to_max"; | |
193 | this->home_direction[2]= THEKERNEL->config->value(gamma_homing_direction_checksum)->by_default("home_to_min")->as_string() != "home_to_max"; | |
47bbe224 | 194 | |
1c658603 JM |
195 | this->homing_position[0]= this->home_direction[0] ? THEKERNEL->config->value(alpha_min_checksum)->by_default(0)->as_number() : THEKERNEL->config->value(alpha_max_checksum)->by_default(200)->as_number(); |
196 | this->homing_position[1]= this->home_direction[1] ? THEKERNEL->config->value(beta_min_checksum )->by_default(0)->as_number() : THEKERNEL->config->value(beta_max_checksum )->by_default(200)->as_number(); | |
197 | this->homing_position[2]= this->home_direction[2] ? THEKERNEL->config->value(gamma_min_checksum)->by_default(0)->as_number() : THEKERNEL->config->value(gamma_max_checksum)->by_default(200)->as_number(); | |
f29b0272 | 198 | |
abf706e6 | 199 | // used to set maximum movement on homing, set by alpha_max_travel if defined |
831abf97 | 200 | // for backward compatibility uses alpha_max if not defined. |
abf706e6 | 201 | // TO BE deprecated |
c8bac202 JM |
202 | this->alpha_max= THEKERNEL->config->value(alpha_max_checksum)->by_default(500)->as_number(); |
203 | this->beta_max= THEKERNEL->config->value(beta_max_checksum)->by_default(500)->as_number(); | |
204 | this->gamma_max= THEKERNEL->config->value(gamma_max_checksum)->by_default(500)->as_number(); | |
205 | ||
831abf97 JM |
206 | this->alpha_max= THEKERNEL->config->value(alpha_max_travel_checksum)->by_default(alpha_max*2)->as_number(); |
207 | this->beta_max= THEKERNEL->config->value(beta_max_travel_checksum)->by_default(beta_max*2)->as_number(); | |
208 | this->gamma_max= THEKERNEL->config->value(gamma_max_travel_checksum)->by_default(gamma_max*2)->as_number(); | |
abf706e6 | 209 | |
314ab8f7 MM |
210 | this->is_corexy = THEKERNEL->config->value(corexy_homing_checksum)->by_default(false)->as_bool(); |
211 | this->is_delta = THEKERNEL->config->value(delta_homing_checksum)->by_default(false)->as_bool(); | |
11a39396 | 212 | this->is_rdelta = THEKERNEL->config->value(rdelta_homing_checksum)->by_default(false)->as_bool(); |
504f0e3e JM |
213 | this->is_scara = THEKERNEL->config->value(scara_homing_checksum)->by_default(false)->as_bool(); |
214 | ||
374d0777 JM |
215 | this->home_z_first = THEKERNEL->config->value(home_z_first_checksum)->by_default(false)->as_bool(); |
216 | ||
80605954 | 217 | // see if an order has been specified, must be three characters, XYZ or YXZ etc |
e714bd32 JM |
218 | string order = THEKERNEL->config->value(homing_order_checksum)->by_default("")->as_string(); |
219 | this->homing_order = 0; | |
6ded1a99 | 220 | if(order.size() == 3 && !(this->is_delta || this->is_rdelta)) { |
e714bd32 | 221 | int shift = 0; |
80605954 | 222 | for(auto c : order) { |
e714bd32 | 223 | uint8_t i = toupper(c) - 'X'; |
80605954 | 224 | if(i > 2) { // bad value |
e714bd32 | 225 | this->homing_order = 0; |
80605954 JM |
226 | break; |
227 | } | |
228 | homing_order |= (i << shift); | |
229 | shift += 2; | |
230 | } | |
231 | } | |
232 | ||
192bca32 JM |
233 | // endstop trim used by deltas to do soft adjusting |
234 | // on a delta homing to max, a negative trim value will move the carriage down, and a positive will move it up | |
235 | this->trim_mm[0] = THEKERNEL->config->value(alpha_trim_checksum )->by_default(0 )->as_number(); | |
236 | this->trim_mm[1] = THEKERNEL->config->value(beta_trim_checksum )->by_default(0 )->as_number(); | |
237 | this->trim_mm[2] = THEKERNEL->config->value(gamma_trim_checksum )->by_default(0 )->as_number(); | |
3c947f85 JM |
238 | |
239 | // limits enabled | |
e714bd32 JM |
240 | this->limit_enable[X_AXIS] = THEKERNEL->config->value(alpha_limit_enable_checksum)->by_default(false)->as_bool(); |
241 | this->limit_enable[Y_AXIS] = THEKERNEL->config->value(beta_limit_enable_checksum)->by_default(false)->as_bool(); | |
242 | this->limit_enable[Z_AXIS] = THEKERNEL->config->value(gamma_limit_enable_checksum)->by_default(false)->as_bool(); | |
3c947f85 | 243 | |
c8bac202 | 244 | // set to true by default for deltas due to trim, false on cartesians |
e714bd32 | 245 | this->move_to_origin_after_home = THEKERNEL->config->value(move_to_origin_checksum)->by_default(is_delta)->as_bool(); |
2b3cedc7 | 246 | |
e714bd32 | 247 | if(this->limit_enable[X_AXIS] || this->limit_enable[Y_AXIS] || this->limit_enable[Z_AXIS]) { |
3c947f85 | 248 | register_for_event(ON_IDLE); |
6ded1a99 | 249 | if(this->is_delta || this->is_rdelta) { |
7f3e6350 | 250 | // we must enable all the limits not just one |
e714bd32 JM |
251 | this->limit_enable[X_AXIS] = true; |
252 | this->limit_enable[Y_AXIS] = true; | |
253 | this->limit_enable[Z_AXIS] = true; | |
7f3e6350 | 254 | } |
3c947f85 | 255 | } |
44127aca | 256 | |
11a39396 JM |
257 | // |
258 | if(this->is_delta || this->is_rdelta) { | |
44127aca | 259 | // some things must be the same or they will die, so force it here to avoid config errors |
e714bd32 JM |
260 | this->fast_rates[1] = this->fast_rates[2] = this->fast_rates[0]; |
261 | this->slow_rates[1] = this->slow_rates[2] = this->slow_rates[0]; | |
262 | this->retract_mm[1] = this->retract_mm[2] = this->retract_mm[0]; | |
263 | this->home_direction[1] = this->home_direction[2] = this->home_direction[0]; | |
42bbc035 JM |
264 | // NOTE homing_position for rdelta is the angle of the actuator not the cartesian position |
265 | if(!this->is_rdelta) this->homing_position[0] = this->homing_position[1] = 0; | |
44127aca | 266 | } |
3c947f85 JM |
267 | } |
268 | ||
28166daf JM |
269 | bool Endstops::debounced_get(int pin) |
270 | { | |
e714bd32 | 271 | uint8_t debounce = 0; |
28166daf JM |
272 | while(this->pins[pin].get()) { |
273 | if ( ++debounce >= this->debounce_count ) { | |
274 | // pin triggered | |
275 | return true; | |
276 | } | |
277 | } | |
278 | return false; | |
279 | } | |
280 | ||
e714bd32 | 281 | static const char *endstop_names[] = {"min_x", "min_y", "min_z", "max_x", "max_y", "max_z"}; |
3c947f85 JM |
282 | |
283 | void Endstops::on_idle(void *argument) | |
284 | { | |
c3d7feb6 JM |
285 | if(this->status == LIMIT_TRIGGERED) { |
286 | // if we were in limit triggered see if it has been cleared | |
287 | for( int c = X_AXIS; c <= Z_AXIS; c++ ) { | |
288 | if(this->limit_enable[c]) { | |
289 | std::array<int, 2> minmax{{0, 3}}; | |
290 | // check min and max endstops | |
291 | for (int i : minmax) { | |
e714bd32 | 292 | int n = c + i; |
c3d7feb6 JM |
293 | if(this->pins[n].get()) { |
294 | // still triggered, so exit | |
e714bd32 | 295 | bounce_cnt = 0; |
c3d7feb6 JM |
296 | return; |
297 | } | |
298 | } | |
299 | } | |
300 | } | |
ee1c3181 JM |
301 | if(++bounce_cnt > 10) { // can use less as it calls on_idle in between |
302 | // clear the state | |
e714bd32 | 303 | this->status = NOT_HOMING; |
ee1c3181 | 304 | } |
4befe777 | 305 | return; |
c3d7feb6 | 306 | |
e714bd32 | 307 | } else if(this->status != NOT_HOMING) { |
c3d7feb6 JM |
308 | // don't check while homing |
309 | return; | |
310 | } | |
3c947f85 JM |
311 | |
312 | for( int c = X_AXIS; c <= Z_AXIS; c++ ) { | |
313 | if(this->limit_enable[c] && STEPPER[c]->is_moving()) { | |
314 | std::array<int, 2> minmax{{0, 3}}; | |
315 | // check min and max endstops | |
316 | for (int i : minmax) { | |
e714bd32 | 317 | int n = c + i; |
28166daf JM |
318 | if(debounced_get(n)) { |
319 | // endstop triggered | |
320 | THEKERNEL->streams->printf("Limit switch %s was hit - reset or M999 required\n", endstop_names[n]); | |
e714bd32 | 321 | this->status = LIMIT_TRIGGERED; |
28166daf JM |
322 | // disables heaters and motors, ignores incoming Gcode and flushes block queue |
323 | THEKERNEL->call_event(ON_HALT, nullptr); | |
324 | return; | |
3c947f85 JM |
325 | } |
326 | } | |
327 | } | |
328 | } | |
329 | } | |
330 | ||
331 | // if limit switches are enabled, then we must move off of the endstop otherwise we won't be able to move | |
5bfcd44a | 332 | // checks if triggered and only backs off if triggered |
98e30679 | 333 | void Endstops::back_off_home(std::bitset<3> axis) |
3c947f85 | 334 | { |
e714bd32 | 335 | std::vector<std::pair<char, float>> params; |
3c947f85 | 336 | this->status = BACK_OFF_HOME; |
7f3e6350 JM |
337 | |
338 | // these are handled differently | |
1a6870b3 | 339 | if(is_delta) { |
7f3e6350 | 340 | // Move off of the endstop using a regular relative move in Z only |
e714bd32 | 341 | params.push_back({'Z', this->retract_mm[Z_AXIS] * (this->home_direction[Z_AXIS] ? 1 : -1)}); |
44127aca | 342 | |
e714bd32 | 343 | } else { |
7f3e6350 | 344 | // cartesians, concatenate all the moves we need to do into one gcode |
44127aca | 345 | for( int c = X_AXIS; c <= Z_AXIS; c++ ) { |
98e30679 | 346 | if(!axis[c]) continue; // only for axes we asked to move |
7f3e6350 JM |
347 | |
348 | // if not triggered no need to move off | |
28166daf | 349 | if(this->limit_enable[c] && debounced_get(c + (this->home_direction[c] ? 0 : 3)) ) { |
e714bd32 | 350 | params.push_back({c + 'X', this->retract_mm[c] * (this->home_direction[c] ? 1 : -1)}); |
44127aca | 351 | } |
3c947f85 JM |
352 | } |
353 | } | |
7f3e6350 JM |
354 | |
355 | if(!params.empty()) { | |
356 | // Move off of the endstop using a regular relative move | |
7c801094 | 357 | params.insert(params.begin(), {'G', 0}); |
addf2490 JM |
358 | // use X slow rate to move, Z should have a max speed set anyway |
359 | params.push_back({'F', this->slow_rates[X_AXIS] * 60.0F}); | |
7c801094 JM |
360 | char gcode_buf[64]; |
361 | append_parameters(gcode_buf, params, sizeof(gcode_buf)); | |
7f3e6350 | 362 | Gcode gc(gcode_buf, &(StreamOutput::NullStream)); |
1d323f9a JM |
363 | THEROBOT->push_state(); |
364 | THEROBOT->inch_mode = false; // needs to be in mm | |
365 | THEROBOT->absolute_mode = false; // needs to be relative mode | |
366 | THEROBOT->on_gcode_received(&gc); // send to robot directly | |
7f3e6350 | 367 | // Wait for above to finish |
04782655 | 368 | THECONVEYOR->wait_for_idle(); |
c8bac202 | 369 | THEROBOT->pop_state(); |
7f3e6350 JM |
370 | } |
371 | ||
3c947f85 | 372 | this->status = NOT_HOMING; |
a0e0d592 BG |
373 | } |
374 | ||
2b3cedc7 | 375 | // If enabled will move the head to 0,0 after homing, but only if X and Y were set to home |
1d323f9a | 376 | void Endstops::move_to_origin(std::bitset<3> axis) |
2b3cedc7 | 377 | { |
1d323f9a | 378 | if(!is_delta && (!axis[X_AXIS] || !axis[Y_AXIS])) return; // ignore if X and Y not homing, unless delta |
2b3cedc7 | 379 | |
fafb45df | 380 | // Do we need to check if we are already at 0,0? probably not as the G0 will not do anything if we are |
c8bac202 | 381 | // float pos[3]; THEROBOT->get_axis_position(pos); if(pos[0] == 0 && pos[1] == 0) return; |
fafb45df | 382 | |
2ddf75fd | 383 | this->status = MOVE_TO_ORIGIN; |
2b3cedc7 | 384 | // Move to center using a regular move, use slower of X and Y fast rate |
e714bd32 | 385 | float rate = std::min(this->fast_rates[0], this->fast_rates[1]) * 60.0F; |
2b3cedc7 | 386 | char buf[32]; |
1d323f9a JM |
387 | THEROBOT->push_state(); |
388 | THEROBOT->inch_mode = false; // needs to be in mm | |
5209dd4d | 389 | THEROBOT->absolute_mode = true; |
e551657a JM |
390 | snprintf(buf, sizeof(buf), "G53 G0 X0 Y0 F%1.4f", rate); // must use machine coordinates in case G92 or WCS is in effect |
391 | struct SerialMessage message; | |
392 | message.message = buf; | |
393 | message.stream = &(StreamOutput::NullStream); | |
394 | THEKERNEL->call_event(ON_CONSOLE_LINE_RECEIVED, &message ); // as it is a multi G code command | |
2b3cedc7 | 395 | // Wait for above to finish |
04782655 | 396 | THECONVEYOR->wait_for_idle(); |
c8bac202 | 397 | THEROBOT->pop_state(); |
2b3cedc7 JM |
398 | this->status = NOT_HOMING; |
399 | } | |
400 | ||
c8bac202 JM |
401 | // Called every millisecond in an ISR |
402 | uint32_t Endstops::read_endstops(uint32_t dummy) | |
33e4cc02 | 403 | { |
98e30679 | 404 | if(this->status != MOVING_TO_ENDSTOP_SLOW && this->status != MOVING_TO_ENDSTOP_FAST) return 0; // not doing anything we need to monitor for |
798295c1 | 405 | |
c8bac202 JM |
406 | if(!is_corexy) { |
407 | // check each axis | |
408 | for ( int m = X_AXIS; m <= Z_AXIS; m++ ) { | |
409 | if(STEPPER[m]->is_moving()) { | |
410 | // if it is moving then we check the associated endstop, and debounce it | |
411 | if(this->pins[m + (this->home_direction[m] ? 0 : 3)].get()) { | |
7778d1ce | 412 | if(debounce[m] < debounce_ms) { |
c8bac202 JM |
413 | debounce[m]++; |
414 | } else { | |
415 | // we signal the motor to stop, which will preempt any moves on that axis | |
416 | STEPPER[m]->stop_moving(); | |
a0e0d592 | 417 | } |
c8bac202 | 418 | |
33e4cc02 | 419 | } else { |
a0e0d592 | 420 | // The endstop was not hit yet |
c8bac202 | 421 | debounce[m] = 0; |
323cca60 | 422 | } |
a0e0d592 BG |
423 | } |
424 | } | |
798295c1 | 425 | |
c8bac202 JM |
426 | } else { |
427 | // corexy is different as the actuators are not directly related to the XY axis | |
428 | // so we check the axis that is currently homing then stop all motors | |
429 | for ( int m = X_AXIS; m <= Z_AXIS; m++ ) { | |
430 | if(axis_to_home[m]) { | |
431 | if(this->pins[m + (this->home_direction[m] ? 0 : 3)].get()) { | |
7778d1ce | 432 | if(debounce[m] < debounce_ms) { |
c8bac202 JM |
433 | debounce[m]++; |
434 | } else { | |
435 | // we signal all the motors to stop, as on corexy X and Y motors will move for X and Y axis homing and we only hom eone axis at a time | |
436 | STEPPER[X_AXIS]->stop_moving(); | |
437 | STEPPER[Y_AXIS]->stop_moving(); | |
438 | STEPPER[Z_AXIS]->stop_moving(); | |
439 | } | |
f29b0272 | 440 | |
c8bac202 JM |
441 | } else { |
442 | // The endstop was not hit yet | |
443 | debounce[m] = 0; | |
444 | } | |
f29b0272 JM |
445 | } |
446 | } | |
447 | } | |
448 | ||
c8bac202 | 449 | return 0; |
3db88866 JM |
450 | } |
451 | ||
374d0777 | 452 | void Endstops::home_xy() |
33e4cc02 | 453 | { |
c8bac202 JM |
454 | if(axis_to_home[X_AXIS] && axis_to_home[Y_AXIS]) { |
455 | // Home XY first so as not to slow them down by homing Z at the same time | |
abf706e6 | 456 | float delta[3] {alpha_max, beta_max, 0}; |
98e30679 JM |
457 | if(this->home_direction[X_AXIS]) delta[X_AXIS]= -delta[X_AXIS]; |
458 | if(this->home_direction[Y_AXIS]) delta[Y_AXIS]= -delta[Y_AXIS]; | |
c8bac202 | 459 | float feed_rate = std::min(fast_rates[X_AXIS], fast_rates[Y_AXIS]); |
121094a5 | 460 | THEROBOT->delta_move(delta, feed_rate, 3); |
c8bac202 | 461 | |
c8bac202 JM |
462 | } else if(axis_to_home[X_AXIS]) { |
463 | // now home X only | |
abf706e6 | 464 | float delta[3] {alpha_max, 0, 0}; |
98e30679 | 465 | if(this->home_direction[X_AXIS]) delta[X_AXIS]= -delta[X_AXIS]; |
121094a5 | 466 | THEROBOT->delta_move(delta, fast_rates[X_AXIS], 3); |
c8bac202 JM |
467 | |
468 | } else if(axis_to_home[Y_AXIS]) { | |
469 | // now home Y only | |
abf706e6 | 470 | float delta[3] {0, beta_max, 0}; |
98e30679 | 471 | if(this->home_direction[Y_AXIS]) delta[Y_AXIS]= -delta[Y_AXIS]; |
121094a5 | 472 | THEROBOT->delta_move(delta, fast_rates[Y_AXIS], 3); |
c8bac202 | 473 | } |
2441ba94 JM |
474 | |
475 | // Wait for axis to have homed | |
476 | THECONVEYOR->wait_for_idle(); | |
374d0777 JM |
477 | } |
478 | ||
479 | void Endstops::home(std::bitset<3> a) | |
480 | { | |
481 | // reset debounce counts | |
482 | debounce.fill(0); | |
483 | ||
fdfa00d2 | 484 | // turn off any compensation transform so Z does not move as XY home |
c1ebb1fe JM |
485 | auto savect= THEROBOT->compensationTransform; |
486 | THEROBOT->compensationTransform= nullptr; | |
487 | ||
374d0777 JM |
488 | this->axis_to_home= a; |
489 | ||
490 | // Start moving the axes to the origin | |
491 | this->status = MOVING_TO_ENDSTOP_FAST; | |
492 | ||
493 | THEROBOT->disable_segmentation= true; // we must disable segmentation as this won't work with it enabled | |
494 | ||
495 | if(!home_z_first) home_xy(); | |
c8bac202 JM |
496 | |
497 | if(axis_to_home[Z_AXIS]) { | |
498 | // now home z | |
1c658603 | 499 | float delta[3] {0, 0, gamma_max}; // we go the max z |
98e30679 | 500 | if(this->home_direction[Z_AXIS]) delta[Z_AXIS]= -delta[Z_AXIS]; |
121094a5 | 501 | THEROBOT->delta_move(delta, fast_rates[Z_AXIS], 3); |
c8bac202 | 502 | // wait for Z |
04782655 | 503 | THECONVEYOR->wait_for_idle(); |
c8bac202 JM |
504 | } |
505 | ||
374d0777 JM |
506 | if(home_z_first) home_xy(); |
507 | ||
95bb7f04 | 508 | // TODO should check that the endstops were hit and it did not stop short for some reason |
04782655 JM |
509 | // we did not complete movement the full distance if we hit the endstops |
510 | THEROBOT->reset_position_from_current_actuator_position(); | |
95bb7f04 | 511 | |
c8bac202 | 512 | // Move back a small distance for all homing axis |
33e4cc02 | 513 | this->status = MOVING_BACK; |
95bb7f04 JM |
514 | float delta[3]{0,0,0}; |
515 | // use minimum feed rate of all three axes that are being homed (sub optimal, but necessary) | |
516 | float feed_rate= slow_rates[X_AXIS]; | |
c8bac202 JM |
517 | for ( int c = X_AXIS; c <= Z_AXIS; c++ ) { |
518 | if(axis_to_home[c]) { | |
519 | delta[c]= this->retract_mm[c]; | |
98e30679 JM |
520 | if(!this->home_direction[c]) delta[c]= -delta[c]; |
521 | feed_rate= std::min(slow_rates[c], feed_rate); | |
174d9961 JM |
522 | } |
523 | } | |
33e4cc02 | 524 | |
121094a5 | 525 | THEROBOT->delta_move(delta, feed_rate, 3); |
c8bac202 | 526 | // wait until finished |
04782655 | 527 | THECONVEYOR->wait_for_idle(); |
f29b0272 | 528 | |
04782655 | 529 | // Start moving the axes towards the endstops slowly |
c8bac202 | 530 | this->status = MOVING_TO_ENDSTOP_SLOW; |
72833629 | 531 | for ( int c = X_AXIS; c <= Z_AXIS; c++ ) { |
98e30679 | 532 | if(axis_to_home[c]) { |
04782655 | 533 | delta[c]= this->retract_mm[c]*2; // move further than we moved off to make sure we hit it cleanly |
98e30679 JM |
534 | if(this->home_direction[c]) delta[c]= -delta[c]; |
535 | }else{ | |
536 | delta[c]= 0; | |
537 | } | |
81f02e89 | 538 | } |
121094a5 | 539 | THEROBOT->delta_move(delta, feed_rate, 3); |
c8bac202 | 540 | // wait until finished |
04782655 JM |
541 | THECONVEYOR->wait_for_idle(); |
542 | ||
543 | // TODO should check that the endstops were hit and it did not stop short for some reason | |
544 | // we did not complete movement the full distance if we hit the endstops | |
545 | THEROBOT->reset_position_from_current_actuator_position(); | |
0058d8d4 | 546 | |
04782655 | 547 | THEROBOT->disable_segmentation= false; |
6d142b73 | 548 | |
c1ebb1fe JM |
549 | // restore compensationTransform |
550 | THEROBOT->compensationTransform= savect; | |
551 | ||
fdfa00d2 JM |
552 | // set flag indicating axis was homed, it stays set once set until H/W reset or unhomed |
553 | if(!homed[X_AXIS] && axis_to_home[X_AXIS]) homed.set(X_AXIS); | |
554 | if(!homed[Y_AXIS] && axis_to_home[Y_AXIS]) homed.set(Y_AXIS); | |
555 | if(!homed[Z_AXIS] && axis_to_home[Z_AXIS]) homed.set(Z_AXIS); | |
556 | ||
ca287785 | 557 | this->status = NOT_HOMING; |
81f02e89 JM |
558 | } |
559 | ||
a2f1ce04 | 560 | void Endstops::process_home_command(Gcode* gcode) |
c339d634 | 561 | { |
a2f1ce04 JM |
562 | if( (gcode->subcode == 0 && THEKERNEL->is_grbl_mode()) || (gcode->subcode == 2 && !THEKERNEL->is_grbl_mode()) ) { |
563 | // G28 in grbl mode or G28.2 in normal mode will do a rapid to the predefined position | |
564 | // TODO spec says if XYZ specified move to them first then move to MCS of specifed axis | |
6a95dd48 JM |
565 | THEROBOT->push_state(); |
566 | THEROBOT->inch_mode = false; // needs to be in mm | |
5209dd4d | 567 | THEROBOT->absolute_mode = true; |
a2f1ce04 JM |
568 | char buf[32]; |
569 | snprintf(buf, sizeof(buf), "G53 G0 X%f Y%f", saved_position[X_AXIS], saved_position[Y_AXIS]); // must use machine coordinates in case G92 or WCS is in effect | |
570 | struct SerialMessage message; | |
571 | message.message = buf; | |
572 | message.stream = &(StreamOutput::NullStream); | |
573 | THEKERNEL->call_event(ON_CONSOLE_LINE_RECEIVED, &message ); // as it is a multi G code command | |
6a95dd48 JM |
574 | // Wait for above to finish |
575 | THECONVEYOR->wait_for_idle(); | |
576 | THEROBOT->pop_state(); | |
a2f1ce04 JM |
577 | return; |
578 | ||
579 | } else if(THEKERNEL->is_grbl_mode() && gcode->subcode == 2) { // G28.2 in grbl mode forces homing (triggered by $H) | |
580 | // fall through so it does homing cycle | |
6e92ab91 | 581 | |
a2f1ce04 JM |
582 | } else if(gcode->subcode == 1) { // G28.1 set pre defined position |
583 | // saves current position in absolute machine coordinates | |
08beee45 JM |
584 | THEROBOT->get_axis_position(saved_position); // Only XY are used |
585 | // Note the following is only meant to be used for recovering a saved position from config-override | |
5af383e2 | 586 | // Not a standard Gcode and not to be relied on |
08beee45 JM |
587 | if (gcode->has_letter('X')) saved_position[X_AXIS] = gcode->get_value('X'); |
588 | if (gcode->has_letter('Y')) saved_position[Y_AXIS] = gcode->get_value('Y'); | |
a2f1ce04 JM |
589 | return; |
590 | ||
591 | } else if(gcode->subcode == 3) { // G28.3 is a smoothie special it sets manual homing | |
592 | if(gcode->get_num_args() == 0) { | |
c8bac202 | 593 | THEROBOT->reset_axis_position(0, 0, 0); |
fdfa00d2 | 594 | homed.set(); |
a2f1ce04 | 595 | } else { |
586cc733 | 596 | // do a manual homing based on given coordinates, no endstops required |
fdfa00d2 JM |
597 | if(gcode->has_letter('X')){ THEROBOT->reset_axis_position(gcode->get_value('X'), X_AXIS); homed.set(X_AXIS); } |
598 | if(gcode->has_letter('Y')){ THEROBOT->reset_axis_position(gcode->get_value('Y'), Y_AXIS); homed.set(Y_AXIS); } | |
599 | if(gcode->has_letter('Z')){ THEROBOT->reset_axis_position(gcode->get_value('Z'), Z_AXIS); homed.set(Z_AXIS); } | |
e714bd32 | 600 | } |
a2f1ce04 | 601 | return; |
201bcb94 | 602 | |
a2f1ce04 | 603 | } else if(gcode->subcode == 4) { // G28.4 is a smoothie special it sets manual homing based on the actuator position (used for rotary delta) |
2441ba94 | 604 | // do a manual homing based on given coordinates, no endstops required |
fdfa00d2 JM |
605 | ActuatorCoordinates ac{NAN, NAN, NAN}; |
606 | if(gcode->has_letter('X')){ ac[0] = gcode->get_value('X'); homed.set(X_AXIS); } | |
607 | if(gcode->has_letter('Y')){ ac[1] = gcode->get_value('Y'); homed.set(Y_AXIS); } | |
608 | if(gcode->has_letter('Z')){ ac[2] = gcode->get_value('Z'); homed.set(Z_AXIS); } | |
c8bac202 | 609 | THEROBOT->reset_actuator_position(ac); |
a2f1ce04 | 610 | return; |
47bbe224 | 611 | |
fdfa00d2 JM |
612 | } else if(gcode->subcode == 5) { // G28.5 is a smoothie special it clears the homed flag for the specified axis, or all if not specifed |
613 | if(gcode->get_num_args() == 0) { | |
614 | homed.reset(); | |
615 | } else { | |
616 | if(gcode->has_letter('X')) homed.reset(X_AXIS); | |
617 | if(gcode->has_letter('Y')) homed.reset(Y_AXIS); | |
618 | if(gcode->has_letter('Z')) homed.reset(Z_AXIS); | |
619 | } | |
620 | return; | |
621 | ||
622 | } else if(gcode->subcode == 6) { // G28.6 is a smoothie special it shows the homing status of each axis | |
623 | for (int i = 0; i < 3; ++i) { | |
624 | gcode->stream->printf("%c:%d ", 'X'+i, homed.test(i)); | |
625 | } | |
626 | gcode->add_nl= true; | |
627 | return; | |
628 | ||
a2f1ce04 JM |
629 | } else if(THEKERNEL->is_grbl_mode()) { |
630 | gcode->stream->printf("error:Unsupported command\n"); | |
631 | return; | |
632 | } | |
e714bd32 | 633 | |
a2f1ce04 | 634 | // G28 is received, we have homing to do |
3b948656 | 635 | |
a2f1ce04 | 636 | // First wait for the queue to be empty |
04782655 | 637 | THECONVEYOR->wait_for_idle(); |
7484e84a | 638 | |
3811aa80 JM |
639 | // deltas always home Z axis only, which moves all three actuators |
640 | bool home_in_z = this->is_delta || this->is_rdelta; | |
6ded1a99 | 641 | |
95bb7f04 | 642 | // figure out which axis to home |
ceeb9155 JM |
643 | bitset<3> haxis; |
644 | haxis.reset(); | |
645 | ||
a35788ee | 646 | if(!home_in_z) { // ie not a delta |
c8bac202 | 647 | bool axis_speced = ( gcode->has_letter('X') || gcode->has_letter('Y') || gcode->has_letter('Z') ); |
6ded1a99 JM |
648 | // only enable homing if the endstop is defined, |
649 | for ( int c = X_AXIS; c <= Z_AXIS; c++ ) { | |
650 | if (this->pins[c + (this->home_direction[c] ? 0 : 3)].connected() && (!axis_speced || gcode->has_letter(c + 'X')) ) { | |
ceeb9155 | 651 | haxis.set(c); |
8cc5173e JM |
652 | // now reset axis to 0 as we do not know what state we are in |
653 | THEROBOT->reset_axis_position(0, c); | |
6ded1a99 | 654 | } |
e714bd32 | 655 | } |
6ded1a99 | 656 | |
c8bac202 | 657 | } else { |
a35788ee | 658 | // Only Z axis homes (even though all actuators move this is handled by arm solution) |
ceeb9155 | 659 | haxis.set(Z_AXIS); |
1c658603 JM |
660 | // we also set the kinematics to a known good position, this is necessary for a rotary delta, but doesn't hurt for linear delta |
661 | THEROBOT->reset_axis_position(0, 0, 0); | |
a2f1ce04 | 662 | } |
e714bd32 | 663 | |
a2f1ce04 JM |
664 | // do the actual homing |
665 | if(homing_order != 0) { | |
666 | // if an order has been specified do it in the specified order | |
667 | // homing order is 0b00ccbbaa where aa is 0,1,2 to specify the first axis, bb is the second and cc is the third | |
668 | // eg 0b00100001 would be Y X Z, 0b00100100 would be X Y Z | |
669 | for (uint8_t m = homing_order; m != 0; m >>= 2) { | |
c8bac202 | 670 | int a= (m & 0x03); // axis to home |
ceeb9155 JM |
671 | if(haxis[a]) { // if axis is selected to home |
672 | std::bitset<3> bs; | |
673 | bs.set(a); | |
674 | home(bs); | |
07186543 | 675 | } |
a2f1ce04 JM |
676 | // check if on_halt (eg kill) |
677 | if(THEKERNEL->is_halted()) break; | |
e714bd32 | 678 | } |
c8bac202 JM |
679 | |
680 | } else if(is_corexy) { | |
681 | // corexy must home each axis individually | |
c8bac202 | 682 | for (int a = X_AXIS; a <= Z_AXIS; ++a) { |
ceeb9155 JM |
683 | if(haxis[a]) { |
684 | std::bitset<3> bs; | |
685 | bs.set(a); | |
686 | home(bs); | |
c8bac202 JM |
687 | } |
688 | } | |
798295c1 | 689 | |
a2f1ce04 | 690 | } else { |
c8bac202 | 691 | // they could all home at the same time |
ceeb9155 | 692 | home(haxis); |
a2f1ce04 | 693 | } |
e714bd32 | 694 | |
a2f1ce04 JM |
695 | // check if on_halt (eg kill) |
696 | if(THEKERNEL->is_halted()) { | |
697 | if(!THEKERNEL->is_grbl_mode()) { | |
698 | THEKERNEL->streams->printf("Homing cycle aborted by kill\n"); | |
699 | } | |
fdfa00d2 | 700 | homed.reset(); |
a2f1ce04 JM |
701 | return; |
702 | } | |
e714bd32 | 703 | |
a35788ee | 704 | if(home_in_z) { // deltas only |
a2f1ce04 JM |
705 | // Here's where we would have been if the endstops were perfectly trimmed |
706 | // NOTE on a rotary delta home_offset is actuator position in degrees when homed and | |
707 | // home_offset is the theta offset for each actuator, so M206 is used to set theta offset for each actuator in degrees | |
8fe38353 | 708 | // FIXME not sure this will work with compensation transforms on. |
a2f1ce04 JM |
709 | float ideal_position[3] = { |
710 | this->homing_position[X_AXIS] + this->home_offset[X_AXIS], | |
711 | this->homing_position[Y_AXIS] + this->home_offset[Y_AXIS], | |
712 | this->homing_position[Z_AXIS] + this->home_offset[Z_AXIS] | |
713 | }; | |
7552475b | 714 | |
3811aa80 | 715 | bool has_endstop_trim = this->is_delta; |
a2f1ce04 JM |
716 | if (has_endstop_trim) { |
717 | ActuatorCoordinates ideal_actuator_position; | |
c8bac202 | 718 | THEROBOT->arm_solution->cartesian_to_actuator(ideal_position, ideal_actuator_position); |
42bbc035 | 719 | |
a2f1ce04 JM |
720 | // We are actually not at the ideal position, but a trim away |
721 | ActuatorCoordinates real_actuator_position = { | |
722 | ideal_actuator_position[X_AXIS] - this->trim_mm[X_AXIS], | |
723 | ideal_actuator_position[Y_AXIS] - this->trim_mm[Y_AXIS], | |
724 | ideal_actuator_position[Z_AXIS] - this->trim_mm[Z_AXIS] | |
725 | }; | |
726 | ||
727 | float real_position[3]; | |
c8bac202 | 728 | THEROBOT->arm_solution->actuator_to_cartesian(real_actuator_position, real_position); |
a2f1ce04 | 729 | // Reset the actuator positions to correspond our real position |
c8bac202 | 730 | THEROBOT->reset_axis_position(real_position[0], real_position[1], real_position[2]); |
42bbc035 | 731 | |
e714bd32 | 732 | } else { |
a2f1ce04 JM |
733 | // without endstop trim, real_position == ideal_position |
734 | if(is_rdelta) { | |
735 | // with a rotary delta we set the actuators angle then use the FK to calculate the resulting cartesian coordinates | |
93f20a8c | 736 | ActuatorCoordinates real_actuator_position = {ideal_position[0], ideal_position[1], ideal_position[2]}; |
c8bac202 | 737 | THEROBOT->reset_actuator_position(real_actuator_position); |
a2f1ce04 JM |
738 | |
739 | } else { | |
740 | // Reset the actuator positions to correspond our real position | |
c8bac202 | 741 | THEROBOT->reset_axis_position(ideal_position[0], ideal_position[1], ideal_position[2]); |
3ffe27fb | 742 | } |
e714bd32 | 743 | } |
3c947f85 | 744 | |
a2f1ce04 JM |
745 | } else { |
746 | // Zero the ax(i/e)s position, add in the home offset | |
fd2341bc JM |
747 | // NOTE that if compensation is active the Z will be set based on where XY are, so make sure XY are homed first then Z |
748 | // so XY are at a known consistent position. (especially true if using a proximity probe) | |
a2f1ce04 | 749 | for ( int c = X_AXIS; c <= Z_AXIS; c++ ) { |
04782655 | 750 | if (haxis[c]) { // if we requested this axis to home |
c8bac202 | 751 | THEROBOT->reset_axis_position(this->homing_position[c] + this->home_offset[c], c); |
a2f1ce04 | 752 | } |
c339d634 | 753 | } |
e714bd32 | 754 | } |
81f02e89 | 755 | |
a2f1ce04 JM |
756 | // on some systems where 0,0 is bed center it is nice to have home goto 0,0 after homing |
757 | // default is off for cartesian on for deltas | |
758 | if(!is_delta) { | |
759 | // NOTE a rotary delta usually has optical or hall-effect endstops so it is safe to go past them a little bit | |
1d323f9a | 760 | if(this->move_to_origin_after_home) move_to_origin(haxis); |
a2f1ce04 | 761 | // if limit switches are enabled we must back off endstop after setting home |
98e30679 | 762 | back_off_home(haxis); |
a2f1ce04 JM |
763 | |
764 | } else if(this->move_to_origin_after_home || this->limit_enable[X_AXIS]) { | |
765 | // deltas are not left at 0,0 because of the trim settings, so move to 0,0 if requested, but we need to back off endstops first | |
766 | // also need to back off endstops if limits are enabled | |
98e30679 | 767 | back_off_home(haxis); |
1d323f9a | 768 | if(this->move_to_origin_after_home) move_to_origin(haxis); |
a2f1ce04 JM |
769 | } |
770 | } | |
771 | ||
078f76e0 JM |
772 | void Endstops::set_homing_offset(Gcode *gcode) |
773 | { | |
3c03ba1d | 774 | // Similar to M206 but sets Homing offsets based on current position |
fdfa00d2 JM |
775 | // We want to set the machine position to what we specify, but the current position of the actuators is after the compensation transform |
776 | // we do not want to reset the actuator positions but we do want to make the current position read Z0 in machine coordinates after homing. | |
777 | // TODO so what do we set the homing offset to to make that happen? | |
cb082011 | 778 | float mpos[3]; |
fdfa00d2 | 779 | THEROBOT->get_current_machine_position(mpos); |
cb082011 | 780 | |
7492a02e | 781 | if (gcode->has_letter('X')) { |
fdfa00d2 JM |
782 | if(!homed[X_AXIS]) { |
783 | gcode->stream->printf("error: Axis X must be homed before setting Homing offset\n"); | |
784 | return; | |
785 | } | |
cb082011 | 786 | home_offset[0] += (THEROBOT->to_millimeters(gcode->get_value('X')) - mpos[X_AXIS]); |
fdfa00d2 | 787 | homed.reset(X_AXIS); // force it to be homed |
078f76e0 | 788 | } |
7492a02e | 789 | if (gcode->has_letter('Y')) { |
fdfa00d2 JM |
790 | if(!homed[Y_AXIS]) { |
791 | gcode->stream->printf("error: Axis Y must be homed before setting Homing offset\n"); | |
792 | return; | |
793 | } | |
cb082011 | 794 | home_offset[1] += (THEROBOT->to_millimeters(gcode->get_value('Y')) - mpos[Y_AXIS]); |
fdfa00d2 | 795 | homed.reset(Y_AXIS); // force it to be homed |
7492a02e JM |
796 | } |
797 | if (gcode->has_letter('Z')) { | |
fdfa00d2 JM |
798 | if(!homed[Z_AXIS]) { |
799 | gcode->stream->printf("error: Axis Z must be homed before setting Homing offset\n"); | |
800 | return; | |
801 | } | |
cb082011 | 802 | home_offset[2] += (THEROBOT->to_millimeters(gcode->get_value('Z')) - mpos[Z_AXIS]); |
fdfa00d2 | 803 | homed.reset(Z_AXIS); // force it to be homed |
7492a02e JM |
804 | } |
805 | ||
81c3ecbf | 806 | gcode->stream->printf("Homing Offset: X %5.3f Y %5.3f Z %5.3f will take effect next home\n", home_offset[0], home_offset[1], home_offset[2]); |
078f76e0 JM |
807 | } |
808 | ||
a2f1ce04 JM |
809 | // Start homing sequences by response to GCode commands |
810 | void Endstops::on_gcode_received(void *argument) | |
811 | { | |
812 | Gcode *gcode = static_cast<Gcode *>(argument); | |
813 | if ( gcode->has_g && gcode->g == 28) { | |
814 | process_home_command(gcode); | |
815 | ||
8b261cdc | 816 | } else if (gcode->has_m) { |
a2f1ce04 | 817 | |
33e4cc02 JM |
818 | switch (gcode->m) { |
819 | case 119: { | |
ef7bd372 JM |
820 | for (int i = 0; i < 6; ++i) { |
821 | if(this->pins[i].connected()) | |
822 | gcode->stream->printf("%s:%d ", endstop_names[i], this->pins[i].get()); | |
823 | } | |
e714bd32 | 824 | gcode->add_nl = true; |
6e92ab91 | 825 | |
33e4cc02 JM |
826 | } |
827 | break; | |
828 | ||
829 | case 206: // M206 - set homing offset | |
0c18b666 | 830 | if(is_rdelta) return; // RotaryDeltaCalibration module will handle this |
932a3995 | 831 | |
7492a02e JM |
832 | if (gcode->has_letter('X')) home_offset[0] = gcode->get_value('X'); |
833 | if (gcode->has_letter('Y')) home_offset[1] = gcode->get_value('Y'); | |
834 | if (gcode->has_letter('Z')) home_offset[2] = gcode->get_value('Z'); | |
81c3ecbf | 835 | gcode->stream->printf("X %5.3f Y %5.3f Z %5.3f will take effect next home\n", home_offset[0], home_offset[1], home_offset[2]); |
504f0e3e | 836 | break; |
0e4bf280 | 837 | |
078f76e0 | 838 | case 306: // set homing offset based on current position |
0c18b666 JM |
839 | if(is_rdelta) return; // RotaryDeltaCalibration module will handle this |
840 | ||
078f76e0 | 841 | set_homing_offset(gcode); |
42bbc035 | 842 | break; |
33e4cc02 JM |
843 | |
844 | case 500: // save settings | |
845 | case 503: // print settings | |
932a3995 JM |
846 | if(!is_rdelta) |
847 | gcode->stream->printf(";Home offset (mm):\nM206 X%1.2f Y%1.2f Z%1.2f\n", home_offset[0], home_offset[1], home_offset[2]); | |
848 | else | |
849 | gcode->stream->printf(";Theta offset (degrees):\nM206 A%1.5f B%1.5f C%1.5f\n", home_offset[0], home_offset[1], home_offset[2]); | |
850 | ||
d0280b9d | 851 | if (this->is_delta || this->is_scara) { |
42bbc035 | 852 | gcode->stream->printf(";Trim (mm):\nM666 X%1.3f Y%1.3f Z%1.3f\n", trim_mm[0], trim_mm[1], trim_mm[2]); |
56ce2b5a | 853 | gcode->stream->printf(";Max Z\nM665 Z%1.3f\n", this->homing_position[2]); |
7a8fe6e0 | 854 | } |
e714bd32 | 855 | if(saved_position[X_AXIS] != 0 || saved_position[Y_AXIS] != 0) { |
5af383e2 | 856 | gcode->stream->printf(";predefined position:\nG28.1 X%1.4f Y%1.4f\n", saved_position[X_AXIS], saved_position[Y_AXIS]); |
e714bd32 | 857 | } |
c339d634 | 858 | break; |
47bbe224 | 859 | |
42bbc035 JM |
860 | case 665: |
861 | if (this->is_delta || this->is_scara) { // M665 - set max gamma/z height | |
862 | float gamma_max = this->homing_position[2]; | |
863 | if (gcode->has_letter('Z')) { | |
864 | this->homing_position[2] = gamma_max = gcode->get_value('Z'); | |
865 | } | |
866 | gcode->stream->printf("Max Z %8.3f ", gamma_max); | |
867 | gcode->add_nl = true; | |
ec4773e5 | 868 | } |
42bbc035 | 869 | break; |
47bbe224 | 870 | |
56ce2b5a | 871 | case 666: |
3e1f5b74 | 872 | if(this->is_delta || this->is_scara) { // M666 - set trim for each axis in mm, NB negative mm trim is down |
56ce2b5a JM |
873 | if (gcode->has_letter('X')) trim_mm[0] = gcode->get_value('X'); |
874 | if (gcode->has_letter('Y')) trim_mm[1] = gcode->get_value('Y'); | |
875 | if (gcode->has_letter('Z')) trim_mm[2] = gcode->get_value('Z'); | |
47bbe224 | 876 | |
56ce2b5a JM |
877 | // print the current trim values in mm |
878 | gcode->stream->printf("X: %5.3f Y: %5.3f Z: %5.3f\n", trim_mm[0], trim_mm[1], trim_mm[2]); | |
6e92ab91 | 879 | |
56ce2b5a | 880 | } |
e714bd32 | 881 | break; |
47bbe224 | 882 | |
64eaf21e | 883 | } |
64eaf21e | 884 | } |
64eaf21e | 885 | } |
9f6f04a5 | 886 | |
e714bd32 JM |
887 | void Endstops::on_get_public_data(void* argument) |
888 | { | |
9f6f04a5 JM |
889 | PublicDataRequest* pdr = static_cast<PublicDataRequest*>(argument); |
890 | ||
891 | if(!pdr->starts_with(endstops_checksum)) return; | |
892 | ||
893 | if(pdr->second_element_is(trim_checksum)) { | |
86fa0b93 | 894 | pdr->set_data_ptr(&this->trim_mm); |
ea5c6d92 JM |
895 | pdr->set_taken(); |
896 | ||
e714bd32 | 897 | } else if(pdr->second_element_is(home_offset_checksum)) { |
86fa0b93 | 898 | pdr->set_data_ptr(&this->home_offset); |
9f6f04a5 | 899 | pdr->set_taken(); |
e714bd32 JM |
900 | |
901 | } else if(pdr->second_element_is(saved_position_checksum)) { | |
902 | pdr->set_data_ptr(&this->saved_position); | |
903 | pdr->set_taken(); | |
07186543 JM |
904 | |
905 | } else if(pdr->second_element_is(get_homing_status_checksum)) { | |
a2f1ce04 JM |
906 | bool *homing = static_cast<bool *>(pdr->get_data_ptr()); |
907 | *homing = this->status != NOT_HOMING; | |
07186543 | 908 | pdr->set_taken(); |
9f6f04a5 JM |
909 | } |
910 | } | |
7d6fe308 | 911 | |
e714bd32 JM |
912 | void Endstops::on_set_public_data(void* argument) |
913 | { | |
7d6fe308 JM |
914 | PublicDataRequest* pdr = static_cast<PublicDataRequest*>(argument); |
915 | ||
916 | if(!pdr->starts_with(endstops_checksum)) return; | |
917 | ||
918 | if(pdr->second_element_is(trim_checksum)) { | |
e714bd32 JM |
919 | float *t = static_cast<float*>(pdr->get_data_ptr()); |
920 | this->trim_mm[0] = t[0]; | |
921 | this->trim_mm[1] = t[1]; | |
922 | this->trim_mm[2] = t[2]; | |
7d6fe308 | 923 | pdr->set_taken(); |
ea5c6d92 | 924 | |
e714bd32 JM |
925 | } else if(pdr->second_element_is(home_offset_checksum)) { |
926 | float *t = static_cast<float*>(pdr->get_data_ptr()); | |
927 | if(!isnan(t[0])) this->home_offset[0] = t[0]; | |
928 | if(!isnan(t[1])) this->home_offset[1] = t[1]; | |
929 | if(!isnan(t[2])) this->home_offset[2] = t[2]; | |
7d6fe308 JM |
930 | } |
931 | } |