| 1 | /* |
| 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. |
| 5 | You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>. |
| 6 | */ |
| 7 | |
| 8 | #include "libs/Module.h" |
| 9 | #include "libs/Kernel.h" |
| 10 | #include "modules/communication/utils/Gcode.h" |
| 11 | #include "modules/robot/Conveyor.h" |
| 12 | #include "Endstops.h" |
| 13 | #include "libs/nuts_bolts.h" |
| 14 | #include "libs/Pin.h" |
| 15 | #include "libs/StepperMotor.h" |
| 16 | #include "wait_api.h" // mbed.h lib |
| 17 | #include "Robot.h" |
| 18 | #include "Stepper.h" |
| 19 | #include "Config.h" |
| 20 | #include "SlowTicker.h" |
| 21 | #include "Planner.h" |
| 22 | #include "checksumm.h" |
| 23 | #include "utils.h" |
| 24 | #include "ConfigValue.h" |
| 25 | #include "libs/StreamOutput.h" |
| 26 | #include "PublicDataRequest.h" |
| 27 | #include "EndstopsPublicAccess.h" |
| 28 | #include "StreamOutputPool.h" |
| 29 | #include "Pauser.h" |
| 30 | |
| 31 | #define ALPHA_AXIS 0 |
| 32 | #define BETA_AXIS 1 |
| 33 | #define GAMMA_AXIS 2 |
| 34 | #define X_AXIS 0 |
| 35 | #define Y_AXIS 1 |
| 36 | #define Z_AXIS 2 |
| 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_limit_enable_checksum CHECKSUM("alpha_limit_enable") |
| 97 | #define beta_limit_enable_checksum CHECKSUM("beta_limit_enable") |
| 98 | #define gamma_limit_enable_checksum CHECKSUM("gamma_limit_enable") |
| 99 | |
| 100 | #define STEPPER THEKERNEL->robot->actuators |
| 101 | #define STEPS_PER_MM(a) (STEPPER[a]->get_steps_per_mm()) |
| 102 | |
| 103 | // Homing States |
| 104 | enum{ |
| 105 | NOT_HOMING, |
| 106 | MOVING_TO_ORIGIN_FAST, |
| 107 | MOVING_BACK, |
| 108 | MOVING_TO_ORIGIN_SLOW, |
| 109 | BACK_OFF_HOME, |
| 110 | LIMIT_TRIGGERED |
| 111 | }; |
| 112 | |
| 113 | Endstops::Endstops() |
| 114 | { |
| 115 | this->status = NOT_HOMING; |
| 116 | home_offset[0] = home_offset[1] = home_offset[2] = 0.0F; |
| 117 | } |
| 118 | |
| 119 | void Endstops::on_module_loaded() |
| 120 | { |
| 121 | // Do not do anything if not enabled |
| 122 | if ( THEKERNEL->config->value( endstops_module_enable_checksum )->by_default(true)->as_bool() == false ) { |
| 123 | delete this; |
| 124 | return; |
| 125 | } |
| 126 | |
| 127 | register_for_event(ON_GCODE_RECEIVED); |
| 128 | register_for_event(ON_GET_PUBLIC_DATA); |
| 129 | register_for_event(ON_SET_PUBLIC_DATA); |
| 130 | |
| 131 | THEKERNEL->slow_ticker->attach( THEKERNEL->stepper->get_acceleration_ticks_per_second() , this, &Endstops::acceleration_tick ); |
| 132 | |
| 133 | // Settings |
| 134 | this->on_config_reload(this); |
| 135 | } |
| 136 | |
| 137 | // Get config |
| 138 | void Endstops::on_config_reload(void *argument) |
| 139 | { |
| 140 | this->pins[0].from_string( THEKERNEL->config->value(alpha_min_endstop_checksum )->by_default("nc" )->as_string())->as_input(); |
| 141 | this->pins[1].from_string( THEKERNEL->config->value(beta_min_endstop_checksum )->by_default("nc" )->as_string())->as_input(); |
| 142 | this->pins[2].from_string( THEKERNEL->config->value(gamma_min_endstop_checksum )->by_default("nc" )->as_string())->as_input(); |
| 143 | this->pins[3].from_string( THEKERNEL->config->value(alpha_max_endstop_checksum )->by_default("nc" )->as_string())->as_input(); |
| 144 | this->pins[4].from_string( THEKERNEL->config->value(beta_max_endstop_checksum )->by_default("nc" )->as_string())->as_input(); |
| 145 | this->pins[5].from_string( THEKERNEL->config->value(gamma_max_endstop_checksum )->by_default("nc" )->as_string())->as_input(); |
| 146 | |
| 147 | // These are the old ones in steps still here for backwards compatibility |
| 148 | this->fast_rates[0] = THEKERNEL->config->value(alpha_fast_homing_rate_checksum )->by_default(4000 )->as_number() / STEPS_PER_MM(0); |
| 149 | this->fast_rates[1] = THEKERNEL->config->value(beta_fast_homing_rate_checksum )->by_default(4000 )->as_number() / STEPS_PER_MM(1); |
| 150 | this->fast_rates[2] = THEKERNEL->config->value(gamma_fast_homing_rate_checksum )->by_default(6400 )->as_number() / STEPS_PER_MM(2); |
| 151 | this->slow_rates[0] = THEKERNEL->config->value(alpha_slow_homing_rate_checksum )->by_default(2000 )->as_number() / STEPS_PER_MM(0); |
| 152 | this->slow_rates[1] = THEKERNEL->config->value(beta_slow_homing_rate_checksum )->by_default(2000 )->as_number() / STEPS_PER_MM(1); |
| 153 | this->slow_rates[2] = THEKERNEL->config->value(gamma_slow_homing_rate_checksum )->by_default(3200 )->as_number() / STEPS_PER_MM(2); |
| 154 | this->retract_mm[0] = THEKERNEL->config->value(alpha_homing_retract_checksum )->by_default(400 )->as_number() / STEPS_PER_MM(0); |
| 155 | this->retract_mm[1] = THEKERNEL->config->value(beta_homing_retract_checksum )->by_default(400 )->as_number() / STEPS_PER_MM(1); |
| 156 | this->retract_mm[2] = THEKERNEL->config->value(gamma_homing_retract_checksum )->by_default(1600 )->as_number() / STEPS_PER_MM(2); |
| 157 | |
| 158 | // 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 |
| 159 | this->fast_rates[0] = THEKERNEL->config->value(alpha_fast_homing_rate_mm_checksum )->by_default(this->fast_rates[0])->as_number(); |
| 160 | this->fast_rates[1] = THEKERNEL->config->value(beta_fast_homing_rate_mm_checksum )->by_default(this->fast_rates[1])->as_number(); |
| 161 | this->fast_rates[2] = THEKERNEL->config->value(gamma_fast_homing_rate_mm_checksum )->by_default(this->fast_rates[2])->as_number(); |
| 162 | this->slow_rates[0] = THEKERNEL->config->value(alpha_slow_homing_rate_mm_checksum )->by_default(this->slow_rates[0])->as_number(); |
| 163 | this->slow_rates[1] = THEKERNEL->config->value(beta_slow_homing_rate_mm_checksum )->by_default(this->slow_rates[1])->as_number(); |
| 164 | this->slow_rates[2] = THEKERNEL->config->value(gamma_slow_homing_rate_mm_checksum )->by_default(this->slow_rates[2])->as_number(); |
| 165 | this->retract_mm[0] = THEKERNEL->config->value(alpha_homing_retract_mm_checksum )->by_default(this->retract_mm[0])->as_number(); |
| 166 | this->retract_mm[1] = THEKERNEL->config->value(beta_homing_retract_mm_checksum )->by_default(this->retract_mm[1])->as_number(); |
| 167 | this->retract_mm[2] = THEKERNEL->config->value(gamma_homing_retract_mm_checksum )->by_default(this->retract_mm[2])->as_number(); |
| 168 | |
| 169 | this->debounce_count = THEKERNEL->config->value(endstop_debounce_count_checksum )->by_default(100)->as_number(); |
| 170 | |
| 171 | |
| 172 | // get homing direction and convert to boolean where true is home to min, and false is home to max |
| 173 | int home_dir = get_checksum(THEKERNEL->config->value(alpha_homing_direction_checksum)->by_default("home_to_min")->as_string()); |
| 174 | this->home_direction[0] = home_dir != home_to_max_checksum; |
| 175 | |
| 176 | home_dir = get_checksum(THEKERNEL->config->value(beta_homing_direction_checksum)->by_default("home_to_min")->as_string()); |
| 177 | this->home_direction[1] = home_dir != home_to_max_checksum; |
| 178 | |
| 179 | home_dir = get_checksum(THEKERNEL->config->value(gamma_homing_direction_checksum)->by_default("home_to_min")->as_string()); |
| 180 | this->home_direction[2] = home_dir != home_to_max_checksum; |
| 181 | |
| 182 | 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(); |
| 183 | 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();; |
| 184 | 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();; |
| 185 | |
| 186 | this->is_corexy = THEKERNEL->config->value(corexy_homing_checksum)->by_default(false)->as_bool(); |
| 187 | this->is_delta = THEKERNEL->config->value(delta_homing_checksum)->by_default(false)->as_bool(); |
| 188 | |
| 189 | // endstop trim used by deltas to do soft adjusting |
| 190 | // on a delta homing to max, a negative trim value will move the carriage down, and a positive will move it up |
| 191 | this->trim_mm[0] = THEKERNEL->config->value(alpha_trim_checksum )->by_default(0 )->as_number(); |
| 192 | this->trim_mm[1] = THEKERNEL->config->value(beta_trim_checksum )->by_default(0 )->as_number(); |
| 193 | this->trim_mm[2] = THEKERNEL->config->value(gamma_trim_checksum )->by_default(0 )->as_number(); |
| 194 | |
| 195 | // limits enabled |
| 196 | this->limit_enable[X_AXIS]= THEKERNEL->config->value(alpha_limit_enable_checksum)->by_default(false)->as_bool(); |
| 197 | this->limit_enable[Y_AXIS]= THEKERNEL->config->value(beta_limit_enable_checksum)->by_default(false)->as_bool(); |
| 198 | this->limit_enable[Z_AXIS]= THEKERNEL->config->value(gamma_limit_enable_checksum)->by_default(false)->as_bool(); |
| 199 | |
| 200 | if(this->limit_enable[X_AXIS] || this->limit_enable[Y_AXIS] || this->limit_enable[Z_AXIS]){ |
| 201 | register_for_event(ON_IDLE); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | static const char *endstop_names[]= {"MIN_X", "MIN_Y", "MIN_Z", "MAX_X", "MAX_Y", "MAX_Z"}; |
| 206 | |
| 207 | void Endstops::on_idle(void *argument) |
| 208 | { |
| 209 | if(this->status != NOT_HOMING) return; // don't check while homing or if a LIMIT was triggered |
| 210 | |
| 211 | for( int c = X_AXIS; c <= Z_AXIS; c++ ) { |
| 212 | if(this->limit_enable[c] && STEPPER[c]->is_moving()) { |
| 213 | std::array<int, 2> minmax{{0, 3}}; |
| 214 | // check min and max endstops |
| 215 | for (int i : minmax) { |
| 216 | int n= c+i; |
| 217 | uint8_t debounce= 0; |
| 218 | while(this->pins[n].get()) { |
| 219 | if ( ++debounce >= debounce_count ) { |
| 220 | // endstop triggered |
| 221 | THEKERNEL->pauser->take(); |
| 222 | THEKERNEL->streams->printf("Limit switch %s was hit - reset required\n", endstop_names[n]); |
| 223 | this->status= LIMIT_TRIGGERED; |
| 224 | return; |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | // if limit switches are enabled, then we must move off of the endstop otherwise we won't be able to move |
| 233 | void Endstops::back_off_home() |
| 234 | { |
| 235 | this->status = BACK_OFF_HOME; |
| 236 | for( int c = X_AXIS; c <= Z_AXIS; c++ ) { |
| 237 | if(this->limit_enable[c]) { |
| 238 | // Move off of the endstop using a regular relative move |
| 239 | char buf[32]; |
| 240 | snprintf(buf, sizeof(buf), "G0 %c%1.4f F%1.4f", c+'X', this->retract_mm[c]*(this->home_direction[c]?1:-1), this->slow_rates[c]*60.0F); |
| 241 | Gcode gc(buf, &(StreamOutput::NullStream)); |
| 242 | bool oldmode= THEKERNEL->robot->absolute_mode; |
| 243 | THEKERNEL->robot->absolute_mode= false; // needs to be relative mode |
| 244 | THEKERNEL->robot->on_gcode_received(&gc); // send to robot directly |
| 245 | THEKERNEL->robot->absolute_mode= oldmode; // restore mode |
| 246 | } |
| 247 | } |
| 248 | // Wait for above to finish |
| 249 | THEKERNEL->conveyor->wait_for_empty_queue(); |
| 250 | this->status = NOT_HOMING; |
| 251 | } |
| 252 | |
| 253 | void Endstops::wait_for_homed(char axes_to_move) |
| 254 | { |
| 255 | bool running = true; |
| 256 | unsigned int debounce[3] = {0, 0, 0}; |
| 257 | while (running) { |
| 258 | running = false; |
| 259 | THEKERNEL->call_event(ON_IDLE); |
| 260 | for ( int c = X_AXIS; c <= Z_AXIS; c++ ) { |
| 261 | if ( ( axes_to_move >> c ) & 1 ) { |
| 262 | if ( this->pins[c + (this->home_direction[c] ? 0 : 3)].get() ) { |
| 263 | if ( debounce[c] < debounce_count ) { |
| 264 | debounce[c]++; |
| 265 | running = true; |
| 266 | } else if ( STEPPER[c]->is_moving() ) { |
| 267 | STEPPER[c]->move(0, 0); |
| 268 | } |
| 269 | } else { |
| 270 | // The endstop was not hit yet |
| 271 | running = true; |
| 272 | debounce[c] = 0; |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | // this homing works for cartesian and delta printers, not for HBots/CoreXY |
| 280 | void Endstops::do_homing(char axes_to_move) |
| 281 | { |
| 282 | // Start moving the axes to the origin |
| 283 | this->status = MOVING_TO_ORIGIN_FAST; |
| 284 | for ( int c = X_AXIS; c <= Z_AXIS; c++ ) { |
| 285 | if ( ( axes_to_move >> c) & 1 ) { |
| 286 | this->feed_rate[c]= this->fast_rates[c]; |
| 287 | STEPPER[c]->set_speed(0); |
| 288 | STEPPER[c]->move(this->home_direction[c], 10000000); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | // Wait for all axes to have homed |
| 293 | this->wait_for_homed(axes_to_move); |
| 294 | |
| 295 | // Move back a small distance |
| 296 | this->status = MOVING_BACK; |
| 297 | bool inverted_dir; |
| 298 | for ( int c = X_AXIS; c <= Z_AXIS; c++ ) { |
| 299 | if ( ( axes_to_move >> c ) & 1 ) { |
| 300 | inverted_dir = !this->home_direction[c]; |
| 301 | this->feed_rate[c]= this->slow_rates[c]; |
| 302 | STEPPER[c]->set_speed(0); |
| 303 | STEPPER[c]->move(inverted_dir, this->retract_mm[c]*STEPS_PER_MM(c)); |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | // Wait for moves to be done |
| 308 | for ( int c = X_AXIS; c <= Z_AXIS; c++ ) { |
| 309 | if ( ( axes_to_move >> c ) & 1 ) { |
| 310 | while ( STEPPER[c]->is_moving() ) { |
| 311 | THEKERNEL->call_event(ON_IDLE); |
| 312 | } |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | // Start moving the axes to the origin slowly |
| 317 | this->status = MOVING_TO_ORIGIN_SLOW; |
| 318 | for ( int c = X_AXIS; c <= Z_AXIS; c++ ) { |
| 319 | if ( ( axes_to_move >> c ) & 1 ) { |
| 320 | this->feed_rate[c]= this->slow_rates[c]; |
| 321 | STEPPER[c]->set_speed(0); |
| 322 | STEPPER[c]->move(this->home_direction[c], 10000000); |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | // Wait for all axes to have homed |
| 327 | this->wait_for_homed(axes_to_move); |
| 328 | |
| 329 | if (this->is_delta) { |
| 330 | // move for soft trim |
| 331 | this->status = MOVING_BACK; |
| 332 | for ( int c = X_AXIS; c <= Z_AXIS; c++ ) { |
| 333 | if ( this->trim_mm[c] != 0.0F && ( axes_to_move >> c ) & 1 ) { |
| 334 | inverted_dir = this->home_direction[c]; |
| 335 | // move up or down depending on sign of trim, -ive is down away from home |
| 336 | if (this->trim_mm[c] < 0) inverted_dir = !inverted_dir; |
| 337 | this->feed_rate[c]= this->slow_rates[c]; |
| 338 | STEPPER[c]->set_speed(0); |
| 339 | STEPPER[c]->move(inverted_dir, abs(round(this->trim_mm[c]*STEPS_PER_MM(c)))); |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | // Wait for moves to be done |
| 344 | for ( int c = X_AXIS; c <= Z_AXIS; c++ ) { |
| 345 | if ( ( axes_to_move >> c ) & 1 ) { |
| 346 | //THEKERNEL->streams->printf("axis %c \r\n", c ); |
| 347 | while ( STEPPER[c]->is_moving() ) { |
| 348 | THEKERNEL->call_event(ON_IDLE); |
| 349 | } |
| 350 | } |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | // Homing is done |
| 355 | this->status = NOT_HOMING; |
| 356 | } |
| 357 | |
| 358 | void Endstops::wait_for_homed_corexy(int axis) |
| 359 | { |
| 360 | bool running = true; |
| 361 | unsigned int debounce[3] = {0, 0, 0}; |
| 362 | while (running) { |
| 363 | running = false; |
| 364 | THEKERNEL->call_event(ON_IDLE); |
| 365 | if ( this->pins[axis + (this->home_direction[axis] ? 0 : 3)].get() ) { |
| 366 | if ( debounce[axis] < debounce_count ) { |
| 367 | debounce[axis] ++; |
| 368 | running = true; |
| 369 | } else { |
| 370 | // turn both off if running |
| 371 | if (STEPPER[X_AXIS]->is_moving()) STEPPER[X_AXIS]->move(0, 0); |
| 372 | if (STEPPER[Y_AXIS]->is_moving()) STEPPER[Y_AXIS]->move(0, 0); |
| 373 | } |
| 374 | } else { |
| 375 | // The endstop was not hit yet |
| 376 | running = true; |
| 377 | debounce[axis] = 0; |
| 378 | } |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | void Endstops::corexy_home(int home_axis, bool dirx, bool diry, float fast_rate, float slow_rate, unsigned int retract_steps) |
| 383 | { |
| 384 | this->status = MOVING_TO_ORIGIN_FAST; |
| 385 | this->feed_rate[X_AXIS]= fast_rate; |
| 386 | STEPPER[X_AXIS]->set_speed(0); |
| 387 | STEPPER[X_AXIS]->move(dirx, 10000000); |
| 388 | this->feed_rate[Y_AXIS]= fast_rate; |
| 389 | STEPPER[Y_AXIS]->set_speed(0); |
| 390 | STEPPER[Y_AXIS]->move(diry, 10000000); |
| 391 | |
| 392 | // wait for primary axis |
| 393 | this->wait_for_homed_corexy(home_axis); |
| 394 | |
| 395 | // Move back a small distance |
| 396 | this->status = MOVING_BACK; |
| 397 | this->feed_rate[X_AXIS]= slow_rate; |
| 398 | STEPPER[X_AXIS]->set_speed(0); |
| 399 | STEPPER[X_AXIS]->move(!dirx, retract_steps); |
| 400 | this->feed_rate[Y_AXIS]= slow_rate; |
| 401 | STEPPER[Y_AXIS]->set_speed(0); |
| 402 | STEPPER[Y_AXIS]->move(!diry, retract_steps); |
| 403 | |
| 404 | // wait until done |
| 405 | while ( STEPPER[X_AXIS]->is_moving() || STEPPER[Y_AXIS]->is_moving()) { |
| 406 | THEKERNEL->call_event(ON_IDLE); |
| 407 | } |
| 408 | |
| 409 | // Start moving the axes to the origin slowly |
| 410 | this->status = MOVING_TO_ORIGIN_SLOW; |
| 411 | this->feed_rate[X_AXIS]= slow_rate; |
| 412 | STEPPER[X_AXIS]->set_speed(0); |
| 413 | STEPPER[X_AXIS]->move(dirx, 10000000); |
| 414 | this->feed_rate[Y_AXIS]= slow_rate; |
| 415 | STEPPER[Y_AXIS]->set_speed(0); |
| 416 | STEPPER[Y_AXIS]->move(diry, 10000000); |
| 417 | |
| 418 | // wait for primary axis |
| 419 | this->wait_for_homed_corexy(home_axis); |
| 420 | } |
| 421 | |
| 422 | // this homing works for HBots/CoreXY |
| 423 | void Endstops::do_homing_corexy(char axes_to_move) |
| 424 | { |
| 425 | // TODO should really make order configurable, and select whether to allow XY to home at the same time, diagonally |
| 426 | // 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 |
| 427 | // allow to move until an endstop triggers, then stop that motor. Speed up when moving diagonally to match X or Y speed |
| 428 | // continue moving in the direction not yet triggered (which means two motors turning) until endstop hit |
| 429 | |
| 430 | if((axes_to_move & 0x03) == 0x03) { // both X and Y need Homing |
| 431 | // determine which motor to turn and which way |
| 432 | bool dirx= this->home_direction[X_AXIS]; |
| 433 | bool diry= this->home_direction[Y_AXIS]; |
| 434 | int motor; |
| 435 | bool dir; |
| 436 | if(dirx && diry) { // min/min |
| 437 | motor= X_AXIS; |
| 438 | dir= true; |
| 439 | }else if(dirx && !diry) { // min/max |
| 440 | motor= Y_AXIS; |
| 441 | dir= true; |
| 442 | }else if(!dirx && diry) { // max/min |
| 443 | motor= Y_AXIS; |
| 444 | dir= false; |
| 445 | }else if(!dirx && !diry) { // max/max |
| 446 | motor= X_AXIS; |
| 447 | dir= false; |
| 448 | } |
| 449 | |
| 450 | // then move both X and Y until one hits the endstop |
| 451 | this->status = MOVING_TO_ORIGIN_FAST; |
| 452 | this->feed_rate[motor]= this->fast_rates[motor]*1.4142; |
| 453 | STEPPER[motor]->set_speed(0); // need to allow for more ground covered when moving diagonally |
| 454 | STEPPER[motor]->move(dir, 10000000); |
| 455 | // wait until either X or Y hits the endstop |
| 456 | bool running= true; |
| 457 | while (running) { |
| 458 | THEKERNEL->call_event(ON_IDLE); |
| 459 | for(int m=X_AXIS;m<=Y_AXIS;m++) { |
| 460 | if(this->pins[m + (this->home_direction[m] ? 0 : 3)].get()) { |
| 461 | // turn off motor |
| 462 | if(STEPPER[motor]->is_moving()) STEPPER[motor]->move(0, 0); |
| 463 | running= false; |
| 464 | break; |
| 465 | } |
| 466 | } |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | // move individual axis |
| 471 | if (axes_to_move & 0x01) { // Home X, which means both X and Y in same direction |
| 472 | bool dir= this->home_direction[X_AXIS]; |
| 473 | corexy_home(X_AXIS, dir, dir, this->fast_rates[X_AXIS], this->slow_rates[X_AXIS], this->retract_mm[X_AXIS]*STEPS_PER_MM(X_AXIS)); |
| 474 | } |
| 475 | |
| 476 | if (axes_to_move & 0x02) { // Home Y, which means both X and Y in different directions |
| 477 | bool dir= this->home_direction[Y_AXIS]; |
| 478 | corexy_home(Y_AXIS, dir, !dir, this->fast_rates[Y_AXIS], this->slow_rates[Y_AXIS], this->retract_mm[Y_AXIS]*STEPS_PER_MM(Y_AXIS)); |
| 479 | } |
| 480 | |
| 481 | if (axes_to_move & 0x04) { // move Z |
| 482 | do_homing(0x04); // just home normally for Z |
| 483 | } |
| 484 | |
| 485 | // Homing is done |
| 486 | this->status = NOT_HOMING; |
| 487 | } |
| 488 | |
| 489 | // Start homing sequences by response to GCode commands |
| 490 | void Endstops::on_gcode_received(void *argument) |
| 491 | { |
| 492 | Gcode *gcode = static_cast<Gcode *>(argument); |
| 493 | if ( gcode->has_g) { |
| 494 | if ( gcode->g == 28 ) { |
| 495 | gcode->mark_as_taken(); |
| 496 | // G28 is received, we have homing to do |
| 497 | |
| 498 | // First wait for the queue to be empty |
| 499 | THEKERNEL->conveyor->wait_for_empty_queue(); |
| 500 | |
| 501 | // Do we move select axes or all of them |
| 502 | char axes_to_move = 0; |
| 503 | // only enable homing if the endstop is defined, deltas always home all axis |
| 504 | bool home_all = this->is_delta || !( gcode->has_letter('X') || gcode->has_letter('Y') || gcode->has_letter('Z') ); |
| 505 | |
| 506 | for ( int c = X_AXIS; c <= Z_AXIS; c++ ) { |
| 507 | if ( (home_all || gcode->has_letter(c+'X')) && this->pins[c + (this->home_direction[c] ? 0 : 3)].connected() ) { |
| 508 | axes_to_move += ( 1 << c ); |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | // Enable the motors |
| 513 | THEKERNEL->stepper->turn_enable_pins_on(); |
| 514 | |
| 515 | // do the actual homing |
| 516 | if (is_corexy) |
| 517 | do_homing_corexy(axes_to_move); |
| 518 | else |
| 519 | do_homing(axes_to_move); |
| 520 | |
| 521 | // Zero the ax(i/e)s position, add in the home offset |
| 522 | for ( int c = X_AXIS; c <= Z_AXIS; c++ ) { |
| 523 | if ( (axes_to_move >> c) & 1 ) { |
| 524 | THEKERNEL->robot->reset_axis_position(this->homing_position[c] + this->home_offset[c], c); |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | // if limit switches are enabled we must back off endstop after setting home |
| 529 | back_off_home(); |
| 530 | } |
| 531 | } else if (gcode->has_m) { |
| 532 | switch (gcode->m) { |
| 533 | case 119: { |
| 534 | |
| 535 | int px = this->home_direction[0] ? 0 : 3; |
| 536 | int py = this->home_direction[1] ? 1 : 4; |
| 537 | int pz = this->home_direction[2] ? 2 : 5; |
| 538 | const char *mx = this->home_direction[0] ? "min" : "max"; |
| 539 | const char *my = this->home_direction[1] ? "min" : "max"; |
| 540 | const char *mz = this->home_direction[2] ? "min" : "max"; |
| 541 | |
| 542 | 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()); |
| 543 | gcode->add_nl= true; |
| 544 | gcode->mark_as_taken(); |
| 545 | } |
| 546 | break; |
| 547 | |
| 548 | case 206: // M206 - set homing offset |
| 549 | if (gcode->has_letter('X')) home_offset[0] = gcode->get_value('X'); |
| 550 | if (gcode->has_letter('Y')) home_offset[1] = gcode->get_value('Y'); |
| 551 | if (gcode->has_letter('Z')) home_offset[2] = gcode->get_value('Z'); |
| 552 | gcode->stream->printf("X %5.3f Y %5.3f Z %5.3f\n", home_offset[0], home_offset[1], home_offset[2]); |
| 553 | gcode->mark_as_taken(); |
| 554 | break; |
| 555 | |
| 556 | case 500: // save settings |
| 557 | case 503: // print settings |
| 558 | 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]); |
| 559 | if (is_delta) { |
| 560 | 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]); |
| 561 | gcode->stream->printf(";Max Z\nM665 Z%1.3f\n", this->homing_position[2]); |
| 562 | } |
| 563 | gcode->mark_as_taken(); |
| 564 | break; |
| 565 | |
| 566 | case 665: { // M665 - set max gamma/z height |
| 567 | gcode->mark_as_taken(); |
| 568 | float gamma_max = this->homing_position[2]; |
| 569 | if (gcode->has_letter('Z')) { |
| 570 | this->homing_position[2] = gamma_max = gcode->get_value('Z'); |
| 571 | } |
| 572 | gcode->stream->printf("Max Z %8.3f ", gamma_max); |
| 573 | gcode->add_nl = true; |
| 574 | } |
| 575 | break; |
| 576 | |
| 577 | |
| 578 | case 666: |
| 579 | if(this->is_delta) { // M666 - set trim for each axis in mm, NB negative mm trim is down |
| 580 | if (gcode->has_letter('X')) trim_mm[0] = gcode->get_value('X'); |
| 581 | if (gcode->has_letter('Y')) trim_mm[1] = gcode->get_value('Y'); |
| 582 | if (gcode->has_letter('Z')) trim_mm[2] = gcode->get_value('Z'); |
| 583 | |
| 584 | // print the current trim values in mm |
| 585 | gcode->stream->printf("X: %5.3f Y: %5.3f Z: %5.3f\n", trim_mm[0], trim_mm[1], trim_mm[2]); |
| 586 | gcode->mark_as_taken(); |
| 587 | } |
| 588 | break; |
| 589 | |
| 590 | // NOTE this is to test accuracy of lead screws etc. |
| 591 | case 910: { // M910 - move specific number of raw steps |
| 592 | // Enable the motors |
| 593 | THEKERNEL->stepper->turn_enable_pins_on(); |
| 594 | |
| 595 | int x= 0, y=0 , z= 0, f= 200*16; |
| 596 | if (gcode->has_letter('F')) f = gcode->get_value('F'); |
| 597 | if (gcode->has_letter('X')) { |
| 598 | x = gcode->get_value('X'); |
| 599 | STEPPER[X_AXIS]->set_speed(f); |
| 600 | STEPPER[X_AXIS]->move(x<0, abs(x)); |
| 601 | } |
| 602 | if (gcode->has_letter('Y')) { |
| 603 | y = gcode->get_value('Y'); |
| 604 | STEPPER[Y_AXIS]->set_speed(f); |
| 605 | STEPPER[Y_AXIS]->move(y<0, abs(y)); |
| 606 | } |
| 607 | if (gcode->has_letter('Z')) { |
| 608 | z = gcode->get_value('Z'); |
| 609 | STEPPER[Z_AXIS]->set_speed(f); |
| 610 | STEPPER[Z_AXIS]->move(z<0, abs(z)); |
| 611 | } |
| 612 | gcode->stream->printf("Moved X %d Y %d Z %d F %d steps\n", x, y, z, f); |
| 613 | gcode->mark_as_taken(); |
| 614 | break; |
| 615 | } |
| 616 | } |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | #define max(a,b) (((a) > (b)) ? (a) : (b)) |
| 621 | // Called periodically to change the speed to match acceleration |
| 622 | uint32_t Endstops::acceleration_tick(uint32_t dummy) |
| 623 | { |
| 624 | if(this->status == NOT_HOMING) return(0); // nothing to do |
| 625 | |
| 626 | // foreach stepper that is moving |
| 627 | for ( int c = X_AXIS; c <= Z_AXIS; c++ ) { |
| 628 | if( !STEPPER[c]->is_moving() ) continue; |
| 629 | |
| 630 | uint32_t current_rate = STEPPER[c]->get_steps_per_second(); |
| 631 | uint32_t target_rate = int(floor(this->feed_rate[c]*STEPS_PER_MM(c))); |
| 632 | |
| 633 | if( current_rate < target_rate ){ |
| 634 | uint32_t rate_increase = int(floor((THEKERNEL->planner->get_acceleration()/THEKERNEL->stepper->get_acceleration_ticks_per_second())*STEPS_PER_MM(c))); |
| 635 | current_rate = min( target_rate, current_rate + rate_increase ); |
| 636 | } |
| 637 | if( current_rate > target_rate ){ current_rate = target_rate; } |
| 638 | |
| 639 | // steps per second |
| 640 | STEPPER[c]->set_speed(max(current_rate, THEKERNEL->stepper->get_minimum_steps_per_second())); |
| 641 | } |
| 642 | |
| 643 | return 0; |
| 644 | } |
| 645 | |
| 646 | void Endstops::on_get_public_data(void* argument){ |
| 647 | PublicDataRequest* pdr = static_cast<PublicDataRequest*>(argument); |
| 648 | |
| 649 | if(!pdr->starts_with(endstops_checksum)) return; |
| 650 | |
| 651 | if(pdr->second_element_is(trim_checksum)) { |
| 652 | pdr->set_data_ptr(&this->trim_mm); |
| 653 | pdr->set_taken(); |
| 654 | |
| 655 | }else if(pdr->second_element_is(home_offset_checksum)) { |
| 656 | pdr->set_data_ptr(&this->home_offset); |
| 657 | pdr->set_taken(); |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | void Endstops::on_set_public_data(void* argument){ |
| 662 | PublicDataRequest* pdr = static_cast<PublicDataRequest*>(argument); |
| 663 | |
| 664 | if(!pdr->starts_with(endstops_checksum)) return; |
| 665 | |
| 666 | if(pdr->second_element_is(trim_checksum)) { |
| 667 | float *t= static_cast<float*>(pdr->get_data_ptr()); |
| 668 | this->trim_mm[0]= t[0]; |
| 669 | this->trim_mm[1]= t[1]; |
| 670 | this->trim_mm[2]= t[2]; |
| 671 | pdr->set_taken(); |
| 672 | |
| 673 | }else if(pdr->second_element_is(home_offset_checksum)) { |
| 674 | float *t= static_cast<float*>(pdr->get_data_ptr()); |
| 675 | if(!isnan(t[0])) this->home_offset[0]= t[0]; |
| 676 | if(!isnan(t[1])) this->home_offset[1]= t[1]; |
| 677 | if(!isnan(t[2])) this->home_offset[2]= t[2]; |
| 678 | } |
| 679 | } |