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" | |
11 | #include "modules/robot/Player.h" | |
12 | #include "Endstops.h" | |
13 | #include "libs/nuts_bolts.h" | |
750277f8 | 14 | #include "libs/Pin.h" |
201bcb94 AW |
15 | #include "libs/StepperMotor.h" |
16 | #include "wait_api.h" // mbed.h lib | |
17 | ||
18 | Endstops::Endstops(){ | |
19 | this->status = NOT_HOMING; | |
20 | } | |
21 | ||
22 | void Endstops::on_module_loaded() { | |
23 | this->register_for_event(ON_GCODE_RECEIVED); | |
24 | ||
25 | // Take StepperMotor objects from Robot and keep them here | |
26 | this->steppers[0] = this->kernel->robot->alpha_stepper_motor; | |
27 | this->steppers[1] = this->kernel->robot->beta_stepper_motor; | |
28 | this->steppers[2] = this->kernel->robot->gamma_stepper_motor; | |
29 | ||
750277f8 AW |
30 | // Settings |
31 | this->on_config_reload(this); | |
32 | ||
201bcb94 AW |
33 | } |
34 | ||
750277f8 AW |
35 | // Get config |
36 | void Endstops::on_config_reload(void* argument){ | |
37 | this->pins[0] = this->kernel->config->value(alpha_min_endstop_checksum )->by_default("nc" )->as_pin()->as_input(); | |
38 | this->pins[1] = this->kernel->config->value(beta_min_endstop_checksum )->by_default("nc" )->as_pin()->as_input(); | |
39 | this->pins[2] = this->kernel->config->value(gamma_min_endstop_checksum )->by_default("nc" )->as_pin()->as_input(); | |
4026a515 AW |
40 | this->fast_rates[0] = this->kernel->config->value(alpha_fast_homing_rate_checksum )->by_default("500" )->as_number(); |
41 | this->fast_rates[1] = this->kernel->config->value(beta_fast_homing_rate_checksum )->by_default("500" )->as_number(); | |
42 | this->fast_rates[2] = this->kernel->config->value(gamma_fast_homing_rate_checksum )->by_default("500" )->as_number(); | |
43 | this->slow_rates[0] = this->kernel->config->value(alpha_slow_homing_rate_checksum )->by_default("100" )->as_number(); | |
44 | this->slow_rates[1] = this->kernel->config->value(beta_slow_homing_rate_checksum )->by_default("100" )->as_number(); | |
45 | this->slow_rates[2] = this->kernel->config->value(gamma_slow_homing_rate_checksum )->by_default("100" )->as_number(); | |
46 | this->retract_steps[0] = this->kernel->config->value(alpha_homing_retract_checksum )->by_default("30" )->as_number(); | |
47 | this->retract_steps[1] = this->kernel->config->value(beta_homing_retract_checksum )->by_default("30" )->as_number(); | |
48 | this->retract_steps[2] = this->kernel->config->value(gamma_homing_retract_checksum )->by_default("30" )->as_number(); | |
750277f8 | 49 | } |
201bcb94 AW |
50 | |
51 | // Start homing sequences by response to GCode commands | |
52 | void Endstops::on_gcode_received(void* argument){ | |
53 | Gcode* gcode = static_cast<Gcode*>(argument); | |
2f7d3dba AW |
54 | if( gcode->has_g){ |
55 | if( gcode->g == 28 ){ | |
df27a6a3 | 56 | // G28 is received, we have homing to do |
201bcb94 AW |
57 | |
58 | // First wait for the queue to be empty | |
59 | while(this->kernel->player->queue.size() > 0) { wait_us(500); } | |
60 | ||
61 | // Do we move select axes or all of them | |
3b948656 AW |
62 | char axes_to_move = ( ( gcode->has_letter('X') || gcode->has_letter('Y') || gcode->has_letter('Z') ) ? 0x00 : 0xff ); |
63 | for( char c = 'X'; c <= 'Z'; c++ ){ | |
64 | if( gcode->has_letter(c) && this->pins[c - 'X']->connected() ){ axes_to_move += ( 1 << (c - 'X' ) ); } | |
df27a6a3 | 65 | } |
3b948656 | 66 | |
201bcb94 | 67 | // Start moving the axes to the origin |
df27a6a3 | 68 | this->status = MOVING_TO_ORIGIN_FAST; |
201bcb94 | 69 | for( char c = 'X'; c <= 'Z'; c++ ){ |
3b948656 | 70 | if( ( axes_to_move >> ( c - 'X' ) ) & 1 ){ |
7e30976d | 71 | this->steppers[c - 'X']->move(1,10000000); |
df27a6a3 | 72 | this->steppers[c - 'X']->set_speed(this->fast_rates[c -'X']); |
201bcb94 AW |
73 | } |
74 | } | |
75 | ||
76 | // Wait for all axes to have homed | |
750277f8 AW |
77 | bool running = true; |
78 | while(running){ | |
df27a6a3 | 79 | running = false; |
750277f8 | 80 | for( char c = 'X'; c <= 'Z'; c++ ){ |
3b948656 | 81 | if( ( axes_to_move >> ( c - 'X' ) ) & 1 ){ |
750277f8 AW |
82 | if( this->pins[c - 'X']->get() ){ |
83 | // The endstop was hit, stop moving | |
df27a6a3 MM |
84 | if( this->steppers[c - 'X']->moving ){ |
85 | this->steppers[c - 'X']->move(0,0); | |
86 | } | |
750277f8 AW |
87 | }else{ |
88 | // The endstop was not hit yet | |
89 | running = true; | |
90 | } | |
91 | } | |
92 | } | |
93 | } | |
201bcb94 AW |
94 | |
95 | ||
3b948656 AW |
96 | printf("test a\r\n"); |
97 | // Move back a small distance | |
df27a6a3 | 98 | this->status = MOVING_BACK; |
3b948656 AW |
99 | for( char c = 'X'; c <= 'Z'; c++ ){ |
100 | if( ( axes_to_move >> ( c - 'X' ) ) & 1 ){ | |
7e30976d | 101 | this->steppers[c - 'X']->move(0,this->retract_steps[c - 'X']); |
df27a6a3 | 102 | this->steppers[c - 'X']->set_speed(this->slow_rates[c - 'X']); |
3b948656 AW |
103 | } |
104 | } | |
105 | ||
106 | printf("test b\r\n"); | |
107 | // Wait for moves to be done | |
108 | for( char c = 'X'; c <= 'Z'; c++ ){ | |
df27a6a3 | 109 | if( ( axes_to_move >> ( c - 'X' ) ) & 1 ){ |
3b948656 AW |
110 | printf("axis %c \r\n", c ); |
111 | while( this->steppers[c - 'X']->moving ){ } | |
112 | } | |
113 | } | |
114 | ||
115 | printf("test c\r\n"); | |
201bcb94 | 116 | |
9b0ac510 | 117 | // Start moving the axes to the origin slowly |
df27a6a3 | 118 | this->status = MOVING_TO_ORIGIN_SLOW; |
9b0ac510 AW |
119 | for( char c = 'X'; c <= 'Z'; c++ ){ |
120 | if( ( axes_to_move >> ( c - 'X' ) ) & 1 ){ | |
7e30976d | 121 | this->steppers[c - 'X']->move(1,10000000); |
df27a6a3 | 122 | this->steppers[c - 'X']->set_speed(this->slow_rates[c -'X']); |
9b0ac510 AW |
123 | } |
124 | } | |
125 | ||
126 | // Wait for all axes to have homed | |
127 | running = true; | |
128 | while(running){ | |
df27a6a3 | 129 | running = false; |
9b0ac510 AW |
130 | for( char c = 'X'; c <= 'Z'; c++ ){ |
131 | if( ( axes_to_move >> ( c - 'X' ) ) & 1 ){ | |
132 | if( this->pins[c - 'X']->get() ){ | |
133 | // The endstop was hit, stop moving | |
df27a6a3 MM |
134 | if( this->steppers[c - 'X']->moving ){ |
135 | this->steppers[c - 'X']->move(0,0); | |
136 | } | |
9b0ac510 AW |
137 | }else{ |
138 | // The endstop was not hit yet | |
139 | running = true; | |
140 | } | |
141 | } | |
142 | } | |
143 | } | |
144 | ||
145 | ||
201bcb94 AW |
146 | // Homing is done |
147 | this->status = NOT_HOMING; | |
148 | ||
149 | } | |
150 | } | |
151 | } | |
152 |