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