forked from ashelly/grbl
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathprobe.c
252 lines (201 loc) · 6.03 KB
/
probe.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
probe.c - code pertaining to probing methods
Part of Grbl
Copyright (c) 2014 Sungeun K. Jeon, Adam Shelly
Grbl 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.
Grbl 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.
You should have received a copy of the GNU General Public License
along with Grbl. If not, see <http://www.gnu.org/licenses/>.
*/
#include "system.h"
#include "probe.h"
#include "counters.h"
#include "protocol.h"
#include "stepper.h"
#include "planner.h"
#include "settings.h"
#include "limits.h"
#include "gcode.h"
#include "report.h"
#include "motion_control.h"
#define PROBE_LINE_NUMBER (LINENUMBER_SPECIAL)
struct probe_state probe;
// Idx, Mask, PIN - Note PIN is not the
// pin of the microcontroller, but Port In.
static struct {
uint8_t idx;
uint8_t mask;
volatile uint8_t *ddr;
// Named in_port rather that pin for clarity
volatile uint8_t *in_port;
volatile uint8_t *port;
} sensor_map[] = {
{
.idx = MAG_SENSOR,
.mask = MAGAZINE_ALIGNMENT_MASK,
.ddr = &MAGAZINE_ALIGNMENT_DDR,
.in_port = &MAGAZINE_ALIGNMENT_PIN,
.port = &MAGAZINE_ALIGNMENT_PORT
},
{
.idx = KEY_SENSOR_FRONT,
.mask = (1 << Z_LIMIT_BIT),
.ddr = &LIMIT_DDR,
.in_port = &LIMIT_PIN,
.port = &LIMIT_PORT
},
{
.idx = KEY_SENSOR_BACK,
.mask = (1 << Z_LIMIT_BIT),
.ddr = &LIMIT_DDR,
.in_port = &LIMIT_PIN,
.port = &LIMIT_PORT
}
};
void set_active_probe(enum e_sensor sensor)
{
probe.active_sensor = sensor;
}
// Probe pin initialization routine.
void probe_init()
{
uint8_t len = sizeof(sensor_map) / sizeof(sensor_map[0]);
uint8_t idx;
for (idx = 0; idx < len; idx++) {
*sensor_map[idx].ddr &= ~(sensor_map[idx].mask);
*sensor_map[idx].port |= sensor_map[idx].mask;
}
probe.active_sensor = E_SENSOR_TYPES;
probe.probe_reached = 0;
probe.isprobing = 0;
}
void probe_check()
{
if (probe_get_active_sensor_state()) {
// Stop looking for probe
probe.isprobing = 0;
// TODO: Report the position where the probe was found
// using report_probe_parameters(). This should possibly be
// reported in in probe_loop or probe_move_to_sensor instead of here.
}
// Check for ESTOP
if (ESTOP_PIN & ESTOP_MASK) {
sys.alarm |= ALARM_ESTOP;
SYS_EXEC |= (EXEC_FEED_HOLD | EXEC_ALARM | EXEC_CRIT_EVENT);
}
}
bool probe_loop()
{
// Start stepper
st_prep_buffer();
st_wake_up();
SYS_EXEC |= EXEC_CYCLE_START;
// Stay in this loop until alarm or until
// the probe is found. probe_check() is called
// from the stepper ISR to check if the active
// probe is found.
while (probe.isprobing) {
// Check for user reset and allow
// protocol_execute_runtime to run in this loop
protocol_execute_runtime();
if(sys.abort)
return false;
if (SYS_EXEC & EXEC_RESET) {
protocol_execute_runtime();
return false;
}
if (ESTOP_PIN & ESTOP_MASK) {
sys.alarm |= ALARM_ESTOP;
SYS_EXEC |= (EXEC_FEED_HOLD | EXEC_ALARM | EXEC_CRIT_EVENT);
protocol_execute_runtime();
return false;
}
// Check if we never reach probe.
if ((SYS_EXEC & EXEC_CYCLE_STOP) ) {
sys.alarm |= ALARM_PROBE_FAIL;
SYS_EXEC |= EXEC_CRIT_EVENT;
protocol_execute_runtime();
return false;
}
}
return true;
}
// This function adds support for gcode G38.2
void probe_move_to_sensor(float * target, float feed_rate, uint8_t invert_feed_rate,
linenumber_t line_number, enum e_sensor sensor)
{
// Set the active probe
probe.active_sensor = sensor;
if (sys.state != STATE_CYCLE)
protocol_auto_cycle_start();
// Finish all queued commands
protocol_buffer_synchronize();
// Return if system reset has been issued
if (sys.abort)
return;
// Move in a line to the target
mc_line(target, feed_rate, invert_feed_rate, line_number);
// TODO: If the probe is already activated, we should look in
// the oppostie direction that is specified.
if (sensor == MAG_SENSOR)
probe.carousel_probe_state = PROBE_ACTIVE;
// Tell the system we are probing
probe.isprobing = 1;
sys.state = STATE_PROBING;
uint8_t probe_fail;
probe_fail = !probe_loop();
if (!probe_fail)
memcpy(sys.probe_position, sys.position, sizeof(float) * N_AXIS);
if (sensor == MAG_SENSOR) {
probe_fail = (probe.carousel_probe_state == PROBE_ACTIVE);
if (probe_fail)
memcpy(sys.probe_position, sys.position, sizeof(float) * N_AXIS);
}
protocol_execute_runtime();
if (sys.abort)
return;
// Prep the new target based on the positon that the probe triggered
uint8_t idx;
for (idx = 0; idx < N_AXIS; ++idx) {
target[idx] = (float)sys.probe_position[idx] / settings.steps_per_mm[idx];
}
protocol_execute_runtime();
// Force kill steppers and reset
// step segment buffer
st_reset();
plan_reset();
plan_sync_position();
SYS_EXEC |= EXEC_CYCLE_START;
// Complete pull-off action
protocol_buffer_synchronize();
// Did not complete. Alarm state set by mc_alarm
if (sys.abort)
return;
gc_sync_position();
sys.state = STATE_IDLE;
st_go_idle();
request_eol_report();
protocol_execute_runtime();
report_probe_parameters(probe_fail);
}
// This function monitors the carousel magazine alignment probe
// which is used to move to a specified magazine.
void probe_carousel_monitor()
{
uint8_t probe_on = probe_get_carousel_state();
if (probe.carousel_probe_state == PROBE_ACTIVE && probe_on) {
probe.carousel_probe_state = PROBE_OFF;
memcpy(sys.probe_position, sys.position, sizeof(float) * N_AXIS);
SYS_EXEC |= EXEC_FEED_HOLD;
}
if (ESTOP_PIN & ESTOP_MASK) {
sys.alarm |= ALARM_ESTOP;
SYS_EXEC |= (EXEC_FEED_HOLD | EXEC_ALARM | EXEC_CRIT_EVENT);
}
}