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" |
201bcb94 AW |
12 | #include "Endstops.h" |
13 | #include "libs/nuts_bolts.h" | |
750277f8 | 14 | #include "libs/Pin.h" |
670fa10b | 15 | #include "libs/StepperMotor.h" |
201bcb94 | 16 | #include "wait_api.h" // mbed.h lib |
61134a65 JM |
17 | #include "Robot.h" |
18 | #include "Stepper.h" | |
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" |
201bcb94 | 25 | |
33e4cc02 JM |
26 | #define ALPHA_AXIS 0 |
27 | #define BETA_AXIS 1 | |
28 | #define GAMMA_AXIS 2 | |
9a993543 JM |
29 | #define X_AXIS 0 |
30 | #define Y_AXIS 1 | |
31 | #define Z_AXIS 2 | |
33e4cc02 JM |
32 | |
33 | #define NOT_HOMING 0 | |
34 | #define MOVING_TO_ORIGIN_FAST 1 | |
35 | #define MOVING_BACK 2 | |
36 | #define MOVING_TO_ORIGIN_SLOW 3 | |
37 | ||
38 | #define endstops_module_enable_checksum CHECKSUM("endstops_enable") | |
39 | #define corexy_homing_checksum CHECKSUM("corexy_homing") | |
40 | #define delta_homing_checksum CHECKSUM("delta_homing") | |
41 | ||
42 | #define alpha_min_endstop_checksum CHECKSUM("alpha_min_endstop") | |
43 | #define beta_min_endstop_checksum CHECKSUM("beta_min_endstop") | |
44 | #define gamma_min_endstop_checksum CHECKSUM("gamma_min_endstop") | |
45 | ||
46 | #define alpha_max_endstop_checksum CHECKSUM("alpha_max_endstop") | |
47 | #define beta_max_endstop_checksum CHECKSUM("beta_max_endstop") | |
48 | #define gamma_max_endstop_checksum CHECKSUM("gamma_max_endstop") | |
49 | ||
50 | #define alpha_trim_checksum CHECKSUM("alpha_trim") | |
51 | #define beta_trim_checksum CHECKSUM("beta_trim") | |
52 | #define gamma_trim_checksum CHECKSUM("gamma_trim") | |
53 | ||
54 | // these values are in steps and should be deprecated | |
55 | #define alpha_fast_homing_rate_checksum CHECKSUM("alpha_fast_homing_rate") | |
56 | #define beta_fast_homing_rate_checksum CHECKSUM("beta_fast_homing_rate") | |
57 | #define gamma_fast_homing_rate_checksum CHECKSUM("gamma_fast_homing_rate") | |
58 | ||
59 | #define alpha_slow_homing_rate_checksum CHECKSUM("alpha_slow_homing_rate") | |
60 | #define beta_slow_homing_rate_checksum CHECKSUM("beta_slow_homing_rate") | |
61 | #define gamma_slow_homing_rate_checksum CHECKSUM("gamma_slow_homing_rate") | |
62 | ||
63 | #define alpha_homing_retract_checksum CHECKSUM("alpha_homing_retract") | |
64 | #define beta_homing_retract_checksum CHECKSUM("beta_homing_retract") | |
65 | #define gamma_homing_retract_checksum CHECKSUM("gamma_homing_retract") | |
66 | #define endstop_debounce_count_checksum CHECKSUM("endstop_debounce_count") | |
67 | ||
68 | // same as above but in user friendly mm/s and mm | |
69 | #define alpha_fast_homing_rate_mm_checksum CHECKSUM("alpha_fast_homing_rate_mm_s") | |
70 | #define beta_fast_homing_rate_mm_checksum CHECKSUM("beta_fast_homing_rate_mm_s") | |
71 | #define gamma_fast_homing_rate_mm_checksum CHECKSUM("gamma_fast_homing_rate_mm_s") | |
72 | ||
73 | #define alpha_slow_homing_rate_mm_checksum CHECKSUM("alpha_slow_homing_rate_mm_s") | |
74 | #define beta_slow_homing_rate_mm_checksum CHECKSUM("beta_slow_homing_rate_mm_s") | |
75 | #define gamma_slow_homing_rate_mm_checksum CHECKSUM("gamma_slow_homing_rate_mm_s") | |
76 | ||
77 | #define alpha_homing_retract_mm_checksum CHECKSUM("alpha_homing_retract_mm") | |
78 | #define beta_homing_retract_mm_checksum CHECKSUM("beta_homing_retract_mm") | |
79 | #define gamma_homing_retract_mm_checksum CHECKSUM("gamma_homing_retract_mm") | |
80 | ||
81 | #define endstop_debounce_count_checksum CHECKSUM("endstop_debounce_count") | |
82 | ||
83 | #define alpha_homing_direction_checksum CHECKSUM("alpha_homing_direction") | |
84 | #define beta_homing_direction_checksum CHECKSUM("beta_homing_direction") | |
85 | #define gamma_homing_direction_checksum CHECKSUM("gamma_homing_direction") | |
86 | #define home_to_max_checksum CHECKSUM("home_to_max") | |
87 | #define home_to_min_checksum CHECKSUM("home_to_min") | |
88 | #define alpha_min_checksum CHECKSUM("alpha_min") | |
89 | #define beta_min_checksum CHECKSUM("beta_min") | |
90 | #define gamma_min_checksum CHECKSUM("gamma_min") | |
91 | ||
92 | #define alpha_max_checksum CHECKSUM("alpha_max") | |
93 | #define beta_max_checksum CHECKSUM("beta_max") | |
94 | #define gamma_max_checksum CHECKSUM("gamma_max") | |
95 | ||
96 | #define alpha_steps_per_mm_checksum CHECKSUM("alpha_steps_per_mm") | |
97 | #define beta_steps_per_mm_checksum CHECKSUM("beta_steps_per_mm") | |
98 | #define gamma_steps_per_mm_checksum CHECKSUM("gamma_steps_per_mm") | |
99 | ||
100 | Endstops::Endstops() | |
101 | { | |
201bcb94 | 102 | this->status = NOT_HOMING; |
33e4cc02 | 103 | home_offset[0] = home_offset[1] = home_offset[2] = 0.0F; |
201bcb94 AW |
104 | } |
105 | ||
33e4cc02 JM |
106 | void Endstops::on_module_loaded() |
107 | { | |
7dee696d | 108 | // Do not do anything if not enabled |
314ab8f7 | 109 | if ( THEKERNEL->config->value( endstops_module_enable_checksum )->by_default(true)->as_bool() == false ) { |
33e4cc02 JM |
110 | return; |
111 | } | |
7d62445b | 112 | |
476dcb96 | 113 | register_for_event(ON_CONFIG_RELOAD); |
201bcb94 AW |
114 | this->register_for_event(ON_GCODE_RECEIVED); |
115 | ||
116 | // Take StepperMotor objects from Robot and keep them here | |
314ab8f7 MM |
117 | this->steppers[0] = THEKERNEL->robot->alpha_stepper_motor; |
118 | this->steppers[1] = THEKERNEL->robot->beta_stepper_motor; | |
119 | this->steppers[2] = THEKERNEL->robot->gamma_stepper_motor; | |
64eaf21e | 120 | THEKERNEL->slow_ticker->attach( THEKERNEL->stepper->acceleration_ticks_per_second , this, &Endstops::acceleration_tick ); |
201bcb94 | 121 | |
750277f8 AW |
122 | // Settings |
123 | this->on_config_reload(this); | |
201bcb94 AW |
124 | } |
125 | ||
750277f8 | 126 | // Get config |
33e4cc02 JM |
127 | void Endstops::on_config_reload(void *argument) |
128 | { | |
314ab8f7 MM |
129 | this->pins[0].from_string( THEKERNEL->config->value(alpha_min_endstop_checksum )->by_default("nc" )->as_string())->as_input(); |
130 | this->pins[1].from_string( THEKERNEL->config->value(beta_min_endstop_checksum )->by_default("nc" )->as_string())->as_input(); | |
131 | this->pins[2].from_string( THEKERNEL->config->value(gamma_min_endstop_checksum )->by_default("nc" )->as_string())->as_input(); | |
132 | this->pins[3].from_string( THEKERNEL->config->value(alpha_max_endstop_checksum )->by_default("nc" )->as_string())->as_input(); | |
133 | this->pins[4].from_string( THEKERNEL->config->value(beta_max_endstop_checksum )->by_default("nc" )->as_string())->as_input(); | |
134 | this->pins[5].from_string( THEKERNEL->config->value(gamma_max_endstop_checksum )->by_default("nc" )->as_string())->as_input(); | |
5de98d7c JM |
135 | |
136 | // we need to know steps per mm for M206, also use them for all settings | |
314ab8f7 MM |
137 | this->steps_per_mm[0] = THEKERNEL->config->value(alpha_steps_per_mm_checksum )->as_number(); |
138 | this->steps_per_mm[1] = THEKERNEL->config->value(beta_steps_per_mm_checksum )->as_number(); | |
139 | this->steps_per_mm[2] = THEKERNEL->config->value(gamma_steps_per_mm_checksum )->as_number(); | |
140 | ||
64eaf21e | 141 | //These are the old ones in steps still here for backwards compatibility |
314ab8f7 MM |
142 | this->fast_rates[0] = THEKERNEL->config->value(alpha_fast_homing_rate_checksum )->by_default(4000 )->as_number(); |
143 | this->fast_rates[1] = THEKERNEL->config->value(beta_fast_homing_rate_checksum )->by_default(4000 )->as_number(); | |
144 | this->fast_rates[2] = THEKERNEL->config->value(gamma_fast_homing_rate_checksum )->by_default(6400 )->as_number(); | |
145 | this->slow_rates[0] = THEKERNEL->config->value(alpha_slow_homing_rate_checksum )->by_default(2000 )->as_number(); | |
146 | this->slow_rates[1] = THEKERNEL->config->value(beta_slow_homing_rate_checksum )->by_default(2000 )->as_number(); | |
147 | this->slow_rates[2] = THEKERNEL->config->value(gamma_slow_homing_rate_checksum )->by_default(3200 )->as_number(); | |
148 | this->retract_steps[0] = THEKERNEL->config->value(alpha_homing_retract_checksum )->by_default(400 )->as_number(); | |
149 | this->retract_steps[1] = THEKERNEL->config->value(beta_homing_retract_checksum )->by_default(400 )->as_number(); | |
150 | this->retract_steps[2] = THEKERNEL->config->value(gamma_homing_retract_checksum )->by_default(1600 )->as_number(); | |
5de98d7c JM |
151 | |
152 | // 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 | |
314ab8f7 MM |
153 | this->fast_rates[0] = THEKERNEL->config->value(alpha_fast_homing_rate_mm_checksum )->by_default(this->fast_rates[0] / steps_per_mm[0])->as_number() * steps_per_mm[0]; |
154 | this->fast_rates[1] = THEKERNEL->config->value(beta_fast_homing_rate_mm_checksum )->by_default(this->fast_rates[1] / steps_per_mm[1])->as_number() * steps_per_mm[1]; | |
155 | this->fast_rates[2] = THEKERNEL->config->value(gamma_fast_homing_rate_mm_checksum )->by_default(this->fast_rates[2] / steps_per_mm[2])->as_number() * steps_per_mm[2]; | |
156 | this->slow_rates[0] = THEKERNEL->config->value(alpha_slow_homing_rate_mm_checksum )->by_default(this->slow_rates[0] / steps_per_mm[0])->as_number() * steps_per_mm[0]; | |
157 | this->slow_rates[1] = THEKERNEL->config->value(beta_slow_homing_rate_mm_checksum )->by_default(this->slow_rates[1] / steps_per_mm[1])->as_number() * steps_per_mm[1]; | |
158 | this->slow_rates[2] = THEKERNEL->config->value(gamma_slow_homing_rate_mm_checksum )->by_default(this->slow_rates[2] / steps_per_mm[2])->as_number() * steps_per_mm[2]; | |
159 | this->retract_steps[0] = THEKERNEL->config->value(alpha_homing_retract_mm_checksum )->by_default(this->retract_steps[0] / steps_per_mm[0])->as_number() * steps_per_mm[0]; | |
160 | this->retract_steps[1] = THEKERNEL->config->value(beta_homing_retract_mm_checksum )->by_default(this->retract_steps[1] / steps_per_mm[1])->as_number() * steps_per_mm[1]; | |
161 | this->retract_steps[2] = THEKERNEL->config->value(gamma_homing_retract_mm_checksum )->by_default(this->retract_steps[2] / steps_per_mm[2])->as_number() * steps_per_mm[2]; | |
5de98d7c | 162 | |
314ab8f7 | 163 | this->debounce_count = THEKERNEL->config->value(endstop_debounce_count_checksum )->by_default(0)->as_number(); |
5de98d7c JM |
164 | |
165 | ||
409ff5b3 | 166 | // get homing direction and convert to boolean where true is home to min, and false is home to max |
314ab8f7 | 167 | int home_dir = get_checksum(THEKERNEL->config->value(alpha_homing_direction_checksum)->by_default("home_to_min")->as_string()); |
eb09fdbf | 168 | this->home_direction[0] = home_dir != home_to_max_checksum; |
47bbe224 | 169 | |
314ab8f7 | 170 | home_dir = get_checksum(THEKERNEL->config->value(beta_homing_direction_checksum)->by_default("home_to_min")->as_string()); |
eb09fdbf | 171 | this->home_direction[1] = home_dir != home_to_max_checksum; |
47bbe224 | 172 | |
314ab8f7 | 173 | home_dir = get_checksum(THEKERNEL->config->value(gamma_homing_direction_checksum)->by_default("home_to_min")->as_string()); |
eb09fdbf | 174 | this->home_direction[2] = home_dir != home_to_max_checksum; |
47bbe224 | 175 | |
314ab8f7 MM |
176 | 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(); |
177 | 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();; | |
178 | 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 | 179 | |
314ab8f7 MM |
180 | this->is_corexy = THEKERNEL->config->value(corexy_homing_checksum)->by_default(false)->as_bool(); |
181 | this->is_delta = THEKERNEL->config->value(delta_homing_checksum)->by_default(false)->as_bool(); | |
e678365e JM |
182 | |
183 | // endstop trim used by deltas to do soft adjusting, in mm, convert to steps, and negate depending on homing direction | |
184 | // eg on a delta homing to max, a negative trim value will move the carriage down, and a positive will move it up | |
33e4cc02 JM |
185 | int dirx = (this->home_direction[0] ? 1 : -1); |
186 | int diry = (this->home_direction[1] ? 1 : -1); | |
187 | int dirz = (this->home_direction[2] ? 1 : -1); | |
314ab8f7 MM |
188 | this->trim[0] = THEKERNEL->config->value(alpha_trim_checksum )->by_default(0 )->as_number() * steps_per_mm[0] * dirx; |
189 | this->trim[1] = THEKERNEL->config->value(beta_trim_checksum )->by_default(0 )->as_number() * steps_per_mm[1] * diry; | |
190 | this->trim[2] = THEKERNEL->config->value(gamma_trim_checksum )->by_default(0 )->as_number() * steps_per_mm[2] * dirz; | |
a0e0d592 BG |
191 | } |
192 | ||
33e4cc02 JM |
193 | void Endstops::wait_for_homed(char axes_to_move) |
194 | { | |
a0e0d592 | 195 | bool running = true; |
33e4cc02 JM |
196 | unsigned int debounce[3] = {0, 0, 0}; |
197 | while (running) { | |
a0e0d592 | 198 | running = false; |
314ab8f7 | 199 | THEKERNEL->call_event(ON_IDLE); |
33e4cc02 JM |
200 | for ( char c = 'X'; c <= 'Z'; c++ ) { |
201 | if ( ( axes_to_move >> ( c - 'X' ) ) & 1 ) { | |
202 | if ( this->pins[c - 'X' + (this->home_direction[c - 'X'] ? 0 : 3)].get() ) { | |
203 | if ( debounce[c - 'X'] < debounce_count ) { | |
a0e0d592 BG |
204 | debounce[c - 'X'] ++; |
205 | running = true; | |
33e4cc02 JM |
206 | } else if ( this->steppers[c - 'X']->moving ) { |
207 | this->steppers[c - 'X']->move(0, 0); | |
a0e0d592 | 208 | } |
33e4cc02 | 209 | } else { |
a0e0d592 BG |
210 | // The endstop was not hit yet |
211 | running = true; | |
212 | debounce[c - 'X'] = 0; | |
323cca60 | 213 | } |
a0e0d592 BG |
214 | } |
215 | } | |
216 | } | |
750277f8 | 217 | } |
201bcb94 | 218 | |
f29b0272 | 219 | // this homing works for cartesian and delta printers, not for HBots/CoreXY |
33e4cc02 JM |
220 | void Endstops::do_homing(char axes_to_move) |
221 | { | |
f29b0272 JM |
222 | // Start moving the axes to the origin |
223 | this->status = MOVING_TO_ORIGIN_FAST; | |
33e4cc02 JM |
224 | for ( char c = 'X'; c <= 'Z'; c++ ) { |
225 | if ( ( axes_to_move >> ( c - 'X' ) ) & 1 ) { | |
64eaf21e JM |
226 | this->feed_rate[c - 'X']= this->fast_rates[c - 'X']; |
227 | this->steppers[c - 'X']->set_speed(0); | |
33e4cc02 | 228 | this->steppers[c - 'X']->move(this->home_direction[c - 'X'], 10000000); |
f29b0272 JM |
229 | } |
230 | } | |
231 | ||
232 | // Wait for all axes to have homed | |
233 | this->wait_for_homed(axes_to_move); | |
234 | ||
235 | // Move back a small distance | |
236 | this->status = MOVING_BACK; | |
237 | bool inverted_dir; | |
33e4cc02 JM |
238 | for ( char c = 'X'; c <= 'Z'; c++ ) { |
239 | if ( ( axes_to_move >> ( c - 'X' ) ) & 1 ) { | |
f29b0272 | 240 | inverted_dir = !this->home_direction[c - 'X']; |
64eaf21e JM |
241 | this->feed_rate[c - 'X']= this->slow_rates[c - 'X']; |
242 | this->steppers[c - 'X']->set_speed(0); | |
33e4cc02 | 243 | this->steppers[c - 'X']->move(inverted_dir, this->retract_steps[c - 'X']); |
f29b0272 JM |
244 | } |
245 | } | |
246 | ||
33e4cc02 JM |
247 | // Wait for moves to be done |
248 | for ( char c = 'X'; c <= 'Z'; c++ ) { | |
249 | if ( ( axes_to_move >> ( c - 'X' ) ) & 1 ) { | |
250 | while ( this->steppers[c - 'X']->moving ) { | |
314ab8f7 | 251 | THEKERNEL->call_event(ON_IDLE); |
f29b0272 JM |
252 | } |
253 | } | |
254 | } | |
255 | ||
256 | // Start moving the axes to the origin slowly | |
257 | this->status = MOVING_TO_ORIGIN_SLOW; | |
33e4cc02 JM |
258 | for ( char c = 'X'; c <= 'Z'; c++ ) { |
259 | if ( ( axes_to_move >> ( c - 'X' ) ) & 1 ) { | |
64eaf21e JM |
260 | this->feed_rate[c - 'X']= this->slow_rates[c - 'X']; |
261 | this->steppers[c - 'X']->set_speed(0); | |
33e4cc02 | 262 | this->steppers[c - 'X']->move(this->home_direction[c - 'X'], 10000000); |
f29b0272 JM |
263 | } |
264 | } | |
265 | ||
266 | // Wait for all axes to have homed | |
267 | this->wait_for_homed(axes_to_move); | |
268 | ||
33e4cc02 | 269 | if (this->is_delta) { |
959ab59c JM |
270 | // move for soft trim |
271 | this->status = MOVING_BACK; | |
33e4cc02 JM |
272 | for ( char c = 'X'; c <= 'Z'; c++ ) { |
273 | if ( this->trim[c - 'X'] != 0 && ( axes_to_move >> ( c - 'X' ) ) & 1 ) { | |
959ab59c JM |
274 | inverted_dir = !this->home_direction[c - 'X']; |
275 | // move up or down depending on sign of trim | |
33e4cc02 | 276 | if (this->trim[c - 'X'] < 0) inverted_dir = !inverted_dir; |
64eaf21e JM |
277 | this->feed_rate[c - 'X']= this->slow_rates[c - 'X']; |
278 | this->steppers[c - 'X']->set_speed(0); | |
9ef9c12a | 279 | this->steppers[c - 'X']->move(inverted_dir, abs(this->trim[c - 'X'])); |
959ab59c | 280 | } |
f29b0272 | 281 | } |
f29b0272 | 282 | |
959ab59c | 283 | // Wait for moves to be done |
33e4cc02 JM |
284 | for ( char c = 'X'; c <= 'Z'; c++ ) { |
285 | if ( ( axes_to_move >> ( c - 'X' ) ) & 1 ) { | |
314ab8f7 | 286 | //THEKERNEL->streams->printf("axis %c \r\n", c ); |
33e4cc02 | 287 | while ( this->steppers[c - 'X']->moving ) { |
314ab8f7 | 288 | THEKERNEL->call_event(ON_IDLE); |
959ab59c | 289 | } |
f29b0272 JM |
290 | } |
291 | } | |
292 | } | |
293 | ||
294 | // Homing is done | |
295 | this->status = NOT_HOMING; | |
296 | } | |
297 | ||
33e4cc02 JM |
298 | void Endstops::wait_for_homed_corexy(int axis) |
299 | { | |
3db88866 | 300 | bool running = true; |
33e4cc02 JM |
301 | unsigned int debounce[3] = {0, 0, 0}; |
302 | while (running) { | |
3db88866 | 303 | running = false; |
314ab8f7 | 304 | THEKERNEL->call_event(ON_IDLE); |
33e4cc02 JM |
305 | if ( this->pins[axis + (this->home_direction[axis] ? 0 : 3)].get() ) { |
306 | if ( debounce[axis] < debounce_count ) { | |
3db88866 JM |
307 | debounce[axis] ++; |
308 | running = true; | |
309 | } else { | |
310 | // turn both off if running | |
33e4cc02 JM |
311 | if (this->steppers[X_AXIS]->moving) this->steppers[X_AXIS]->move(0, 0); |
312 | if (this->steppers[Y_AXIS]->moving) this->steppers[Y_AXIS]->move(0, 0); | |
3db88866 | 313 | } |
33e4cc02 | 314 | } else { |
3db88866 JM |
315 | // The endstop was not hit yet |
316 | running = true; | |
317 | debounce[axis] = 0; | |
318 | } | |
319 | } | |
320 | } | |
321 | ||
1ad23cd3 | 322 | void Endstops::corexy_home(int home_axis, bool dirx, bool diry, float fast_rate, float slow_rate, unsigned int retract_steps) |
33e4cc02 JM |
323 | { |
324 | this->status = MOVING_TO_ORIGIN_FAST; | |
64eaf21e JM |
325 | this->feed_rate[X_AXIS]= fast_rate; |
326 | this->steppers[X_AXIS]->set_speed(0); | |
33e4cc02 | 327 | this->steppers[X_AXIS]->move(dirx, 10000000); |
64eaf21e JM |
328 | this->feed_rate[Y_AXIS]= fast_rate; |
329 | this->steppers[Y_AXIS]->set_speed(0); | |
33e4cc02 | 330 | this->steppers[Y_AXIS]->move(diry, 10000000); |
f29b0272 | 331 | |
33e4cc02 JM |
332 | // wait for primary axis |
333 | this->wait_for_homed_corexy(home_axis); | |
f29b0272 | 334 | |
33e4cc02 JM |
335 | // Move back a small distance |
336 | this->status = MOVING_BACK; | |
64eaf21e JM |
337 | this->feed_rate[X_AXIS]= slow_rate; |
338 | this->steppers[X_AXIS]->set_speed(0); | |
33e4cc02 | 339 | this->steppers[X_AXIS]->move(!dirx, retract_steps); |
64eaf21e JM |
340 | this->feed_rate[Y_AXIS]= slow_rate; |
341 | this->steppers[Y_AXIS]->set_speed(0); | |
33e4cc02 JM |
342 | this->steppers[Y_AXIS]->move(!diry, retract_steps); |
343 | ||
344 | // wait until done | |
9a993543 | 345 | while ( this->steppers[X_AXIS]->moving || this->steppers[Y_AXIS]->moving) { |
314ab8f7 | 346 | THEKERNEL->call_event(ON_IDLE); |
f29b0272 JM |
347 | } |
348 | ||
33e4cc02 JM |
349 | // Start moving the axes to the origin slowly |
350 | this->status = MOVING_TO_ORIGIN_SLOW; | |
64eaf21e JM |
351 | this->feed_rate[X_AXIS]= slow_rate; |
352 | this->steppers[X_AXIS]->set_speed(0); | |
33e4cc02 | 353 | this->steppers[X_AXIS]->move(dirx, 10000000); |
64eaf21e JM |
354 | this->feed_rate[Y_AXIS]= slow_rate; |
355 | this->steppers[Y_AXIS]->set_speed(0); | |
33e4cc02 | 356 | this->steppers[Y_AXIS]->move(diry, 10000000); |
f29b0272 | 357 | |
33e4cc02 JM |
358 | // wait for primary axis |
359 | this->wait_for_homed_corexy(home_axis); | |
360 | } | |
f29b0272 | 361 | |
33e4cc02 JM |
362 | // this homing works for HBots/CoreXY |
363 | void Endstops::do_homing_corexy(char axes_to_move) | |
364 | { | |
03dffc07 | 365 | // TODO should really make order configurable, and select whether to allow XY to home at the same time, diagonally |
174d9961 | 366 | // To move XY at the same time only one motor needs to turn, determine which motor and which direction based on min or max directions |
03dffc07 | 367 | // allow to move until an endstop triggers, then stop that motor. Speed up when moving diagonally to match X or Y speed |
174d9961 | 368 | // continue moving in the direction not yet triggered (which means two motors turning) until endstop hit |
174d9961 JM |
369 | |
370 | if((axes_to_move & 0x03) == 0x03) { // both X and Y need Homing | |
371 | // determine which motor to turn and which way | |
372 | bool dirx= this->home_direction[X_AXIS]; | |
373 | bool diry= this->home_direction[Y_AXIS]; | |
374 | int motor; | |
375 | bool dir; | |
376 | if(dirx && diry) { // min/min | |
377 | motor= X_AXIS; | |
378 | dir= true; | |
379 | }else if(dirx && !diry) { // min/max | |
380 | motor= Y_AXIS; | |
381 | dir= true; | |
382 | }else if(!dirx && diry) { // max/min | |
383 | motor= Y_AXIS; | |
384 | dir= false; | |
385 | }else if(!dirx && !diry) { // max/max | |
386 | motor= X_AXIS; | |
387 | dir= false; | |
388 | } | |
389 | ||
390 | // then move both X and Y until one hits the endstop | |
391 | this->status = MOVING_TO_ORIGIN_FAST; | |
64eaf21e JM |
392 | this->feed_rate[motor]= this->fast_rates[motor]*1.4142; |
393 | this->steppers[motor]->set_speed(0); // need to allow for more ground covered when moving diagonally | |
174d9961 JM |
394 | this->steppers[motor]->move(dir, 10000000); |
395 | // wait until either X or Y hits the endstop | |
396 | bool running= true; | |
397 | while (running) { | |
314ab8f7 | 398 | THEKERNEL->call_event(ON_IDLE); |
174d9961 JM |
399 | for(int m=X_AXIS;m<=Y_AXIS;m++) { |
400 | if(this->pins[m + (this->home_direction[m] ? 0 : 3)].get()) { | |
401 | // turn off motor | |
402 | if(this->steppers[motor]->moving) this->steppers[motor]->move(0, 0); | |
403 | running= false; | |
404 | break; | |
405 | } | |
406 | } | |
407 | } | |
408 | } | |
33e4cc02 | 409 | |
174d9961 | 410 | // move individual axis |
33e4cc02 | 411 | if (axes_to_move & 0x01) { // Home X, which means both X and Y in same direction |
413dd90c JM |
412 | bool dir= this->home_direction[X_AXIS]; |
413 | corexy_home(X_AXIS, dir, dir, this->fast_rates[X_AXIS], this->slow_rates[X_AXIS], this->retract_steps[X_AXIS]); | |
f29b0272 JM |
414 | } |
415 | ||
65191d91 JM |
416 | if (axes_to_move & 0x02) { // Home Y, which means both X and Y in different directions |
417 | bool dir= this->home_direction[Y_AXIS]; | |
418 | corexy_home(Y_AXIS, dir, !dir, this->fast_rates[Y_AXIS], this->slow_rates[Y_AXIS], this->retract_steps[Y_AXIS]); | |
419 | } | |
420 | ||
33e4cc02 | 421 | if (axes_to_move & 0x04) { // move Z |
f29b0272 JM |
422 | do_homing(0x04); // just home normally for Z |
423 | } | |
424 | ||
425 | // Homing is done | |
426 | this->status = NOT_HOMING; | |
427 | } | |
428 | ||
201bcb94 | 429 | // Start homing sequences by response to GCode commands |
33e4cc02 | 430 | void Endstops::on_gcode_received(void *argument) |
c339d634 | 431 | { |
33e4cc02 JM |
432 | Gcode *gcode = static_cast<Gcode *>(argument); |
433 | if ( gcode->has_g) { | |
434 | if ( gcode->g == 28 ) { | |
74b6303c | 435 | gcode->mark_as_taken(); |
df27a6a3 | 436 | // G28 is received, we have homing to do |
201bcb94 AW |
437 | |
438 | // First wait for the queue to be empty | |
314ab8f7 | 439 | THEKERNEL->conveyor->wait_for_empty_queue(); |
201bcb94 AW |
440 | |
441 | // Do we move select axes or all of them | |
7dee696d | 442 | char axes_to_move = 0; |
959ab59c | 443 | // only enable homing if the endstop is defined, deltas always home all axis |
33e4cc02 | 444 | bool home_all = this->is_delta || !( gcode->has_letter('X') || gcode->has_letter('Y') || gcode->has_letter('Z') ); |
47bbe224 | 445 | |
33e4cc02 JM |
446 | for ( char c = 'X'; c <= 'Z'; c++ ) { |
447 | if ( (home_all || gcode->has_letter(c)) && this->pins[c - 'X' + (this->home_direction[c - 'X'] ? 0 : 3)].connected() ) { | |
448 | axes_to_move += ( 1 << (c - 'X' ) ); | |
449 | } | |
df27a6a3 | 450 | } |
3b948656 | 451 | |
f6c04440 | 452 | // Enable the motors |
314ab8f7 | 453 | THEKERNEL->stepper->turn_enable_pins_on(); |
f6c04440 | 454 | |
f29b0272 | 455 | // do the actual homing |
33e4cc02 | 456 | if (is_corexy) |
f29b0272 JM |
457 | do_homing_corexy(axes_to_move); |
458 | else | |
459 | do_homing(axes_to_move); | |
3ffe27fb | 460 | |
33e4cc02 JM |
461 | // Zero the ax(i/e)s position, add in the home offset |
462 | for ( int c = 0; c <= 2; c++ ) { | |
463 | if ( (axes_to_move >> c) & 1 ) { | |
314ab8f7 | 464 | THEKERNEL->robot->reset_axis_position(this->homing_position[c] + this->home_offset[c], c); |
3ffe27fb AV |
465 | } |
466 | } | |
c339d634 | 467 | } |
33e4cc02 JM |
468 | } else if (gcode->has_m) { |
469 | switch (gcode->m) { | |
470 | case 119: { | |
471 | ||
472 | int px = this->home_direction[0] ? 0 : 3; | |
473 | int py = this->home_direction[1] ? 1 : 4; | |
474 | int pz = this->home_direction[2] ? 2 : 5; | |
475 | const char *mx = this->home_direction[0] ? "min" : "max"; | |
476 | const char *my = this->home_direction[1] ? "min" : "max"; | |
477 | const char *mz = this->home_direction[2] ? "min" : "max"; | |
478 | ||
bd96f4d7 JM |
479 | gcode->stream->printf("X %s:%d Y %s:%d Z %s:%d", mx, this->pins[px].get(), my, this->pins[py].get(), mz, this->pins[pz].get()); |
480 | gcode->add_nl= true; | |
33e4cc02 JM |
481 | gcode->mark_as_taken(); |
482 | } | |
483 | break; | |
484 | ||
485 | case 206: // M206 - set homing offset | |
486 | if (gcode->has_letter('X')) home_offset[0] = gcode->get_value('X'); | |
487 | if (gcode->has_letter('Y')) home_offset[1] = gcode->get_value('Y'); | |
488 | if (gcode->has_letter('Z')) home_offset[2] = gcode->get_value('Z'); | |
489 | gcode->stream->printf("X %5.3f Y %5.3f Z %5.3f\n", home_offset[0], home_offset[1], home_offset[2]); | |
490 | gcode->mark_as_taken(); | |
491 | break; | |
492 | ||
493 | case 500: // save settings | |
494 | case 503: // print settings | |
495 | 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]); | |
496 | if (is_delta) { | |
1ad23cd3 | 497 | float mm[3]; |
33e4cc02 JM |
498 | trim2mm(mm); |
499 | gcode->stream->printf(";Trim (mm):\nM666 X%1.2f Y%1.2f Z%1.2f\n", mm[0], mm[1], mm[2]); | |
500 | gcode->stream->printf(";Max Z\nM665 Z%1.2f\n", this->homing_position[2]); | |
7a8fe6e0 | 501 | } |
33e4cc02 | 502 | gcode->mark_as_taken(); |
c339d634 | 503 | break; |
47bbe224 | 504 | |
ec4773e5 | 505 | case 665: { // M665 - set max gamma/z height |
33e4cc02 | 506 | gcode->mark_as_taken(); |
1ad23cd3 | 507 | float gamma_max = this->homing_position[2]; |
33e4cc02 JM |
508 | if (gcode->has_letter('Z')) { |
509 | this->homing_position[2] = gamma_max = gcode->get_value('Z'); | |
ec4773e5 | 510 | } |
33e4cc02 JM |
511 | gcode->stream->printf("Max Z %8.3f ", gamma_max); |
512 | gcode->add_nl = true; | |
513 | } | |
514 | break; | |
ec4773e5 | 515 | |
47bbe224 | 516 | |
9ef9c12a | 517 | case 666: { // M666 - set trim for each axis in mm, NB negative mm and positive steps trim is down |
1ad23cd3 | 518 | float mm[3]; |
33e4cc02 | 519 | trim2mm(mm); |
47bbe224 | 520 | |
33e4cc02 JM |
521 | if (gcode->has_letter('X')) mm[0] = gcode->get_value('X'); |
522 | if (gcode->has_letter('Y')) mm[1] = gcode->get_value('Y'); | |
523 | if (gcode->has_letter('Z')) mm[2] = gcode->get_value('Z'); | |
47bbe224 | 524 | |
33e4cc02 JM |
525 | int dirx = (this->home_direction[0] ? 1 : -1); |
526 | int diry = (this->home_direction[1] ? 1 : -1); | |
527 | int dirz = (this->home_direction[2] ? 1 : -1); | |
528 | trim[0] = lround(mm[0] * steps_per_mm[0]) * dirx; // convert back to steps | |
529 | trim[1] = lround(mm[1] * steps_per_mm[1]) * diry; | |
530 | trim[2] = lround(mm[2] * steps_per_mm[2]) * dirz; | |
531 | ||
532 | // print the current trim values in mm and steps | |
533 | gcode->stream->printf("X %5.3f (%d) Y %5.3f (%d) Z %5.3f (%d)\n", mm[0], trim[0], mm[1], trim[1], mm[2], trim[2]); | |
534 | gcode->mark_as_taken(); | |
535 | } | |
536 | break; | |
47bbe224 | 537 | |
d4ee6ee2 JM |
538 | // NOTE this is to test accuracy of lead screws etc. |
539 | case 910: { // M910 - move specific number of raw steps | |
540 | int x= 0, y=0 , z= 0, f= 200*16; | |
541 | if (gcode->has_letter('F')) f = gcode->get_value('F'); | |
542 | if (gcode->has_letter('X')) { | |
543 | x = gcode->get_value('X'); | |
544 | this->steppers[X_AXIS]->set_speed(f); | |
545 | this->steppers[X_AXIS]->move(x<0, abs(x)); | |
546 | } | |
547 | if (gcode->has_letter('Y')) { | |
548 | y = gcode->get_value('Y'); | |
549 | this->steppers[Y_AXIS]->set_speed(f); | |
550 | this->steppers[Y_AXIS]->move(y<0, abs(y)); | |
551 | } | |
552 | if (gcode->has_letter('Z')) { | |
553 | z = gcode->get_value('Z'); | |
554 | this->steppers[Z_AXIS]->set_speed(f); | |
555 | this->steppers[Z_AXIS]->move(z<0, abs(z)); | |
556 | } | |
557 | gcode->stream->printf("Moved X %d Y %d Z %d F %d steps\n", x, y, z, f); | |
558 | gcode->mark_as_taken(); | |
559 | break; | |
560 | } | |
201bcb94 AW |
561 | } |
562 | } | |
563 | } | |
564 | ||
1ad23cd3 | 565 | void Endstops::trim2mm(float *mm) |
33e4cc02 JM |
566 | { |
567 | int dirx = (this->home_direction[0] ? 1 : -1); | |
568 | int diry = (this->home_direction[1] ? 1 : -1); | |
569 | int dirz = (this->home_direction[2] ? 1 : -1); | |
570 | ||
571 | mm[0] = this->trim[0] / this->steps_per_mm[0] * dirx; // convert to mm | |
572 | mm[1] = this->trim[1] / this->steps_per_mm[1] * diry; | |
573 | mm[2] = this->trim[2] / this->steps_per_mm[2] * dirz; | |
574 | } | |
575 | ||
64eaf21e JM |
576 | #define max(a,b) (((a) > (b)) ? (a) : (b)) |
577 | // Called periodically to change the speed to match acceleration | |
578 | uint32_t Endstops::acceleration_tick(uint32_t dummy) | |
579 | { | |
580 | if(this->status == NOT_HOMING) return(0); // nothing to do | |
581 | ||
582 | // foreach stepper that is moving | |
583 | for ( int c = X_AXIS; c <= Z_AXIS; c++ ) { | |
584 | if( !this->steppers[c]->moving ) continue; | |
585 | ||
586 | uint32_t current_rate = this->steppers[c]->steps_per_second; | |
587 | uint32_t target_rate = int(floor(this->feed_rate[c])); | |
588 | ||
589 | if( current_rate < target_rate ){ | |
590 | uint32_t rate_increase = int(floor((THEKERNEL->planner->acceleration/THEKERNEL->stepper->acceleration_ticks_per_second)*this->steps_per_mm[c])); | |
591 | current_rate = min( target_rate, current_rate + rate_increase ); | |
592 | } | |
593 | if( current_rate > target_rate ){ current_rate = target_rate; } | |
594 | ||
595 | // steps per second | |
596 | this->steppers[c]->set_speed(max(current_rate, THEKERNEL->stepper->minimum_steps_per_second)); | |
597 | } | |
598 | ||
599 | return 0; | |
600 | } |