forked from grblHAL/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wall_plotter.c
325 lines (253 loc) · 10.5 KB
/
wall_plotter.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
wall_plotter.c - wall plotter kinematics implementation
Part of grblHAL
Code lifted from Grbl_Esp32 pull request by user @ https://github.com/rognlien
Original code here: https://github.com/jasonwebb/grbl-mega-wall-plotter
Note: homing is not implemented!
Bits also pulled from: https://github.com/ldocull/MaslowDue
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 "grbl.h"
#if WALL_PLOTTER
#include <math.h>
#include <string.h>
#include "hal.h"
#include "settings.h"
#include "planner.h"
#include "kinematics.h"
#define A_MOTOR X_AXIS // Must be X_AXIS
#define B_MOTOR Y_AXIS // Must be Y_AXIS
#define MAX_SEG_LENGTH_MM 2.0f
typedef struct {
int32_t width;
float width_mm;
float width_pow;
int32_t height;
int32_t width_2;
int32_t height_2;
int32_t spindlezero[2];
float spindlezero_mm[2];
} machine_t;
typedef struct {
float a;
float b;
} coord_t;
static bool jog_cancel = false;
static machine_t machine = {0};
static on_report_options_ptr on_report_options;
// Returns machine position in mm converted from system position steps.
// TODO: perhaps change to double precision here - float calculation results in errors of a couple of micrometers.
static float *wp_convert_array_steps_to_mpos (float *position, int32_t *steps)
{
coord_t len;
len.a = (float)steps[A_MOTOR] / settings.axis[A_MOTOR].steps_per_mm;
len.b = (float)steps[B_MOTOR] / settings.axis[B_MOTOR].steps_per_mm;
position[X_AXIS] = (machine.width_pow + len.a * len.a - len.b * len.b) / (2.0f * machine.width_mm);
len.a = machine.width_mm - position[X_AXIS];
position[Y_AXIS] = sqrtf(len.b * len.b - len.a * len.a );
position[Z_AXIS] = steps[Z_AXIS] / settings.axis[Z_AXIS].steps_per_mm;
return position;
}
// Returns machine position in mm converted from system position steps.
// TODO: perhaps change to double precision here - float calculation results in errors of a couple of micrometers.
static float *transform_to_cartesian (float *target, float *position)
{
coord_t len;
len.a = position[A_MOTOR];
len.b = position[B_MOTOR];
target[X_AXIS] = (machine.width_pow + len.a * len.a - len.b * len.b) / (2.0f * machine.width_mm);
len.a = machine.width_mm - target[X_AXIS];
target[Y_AXIS] = sqrtf(len.b * len.b - len.a * len.a );
target[Z_AXIS] = position[Z_AXIS];
return target;
}
// Wall plotter calculation only. Returns x or y-axis "steps" based on wall plotter motor steps.
// A length = sqrt( X^2 + Y^2 )
// B length = sqrt( (MACHINE_WIDTH - X)^2 + Y^2 )
inline static float wp_convert_to_a_motor_steps (float *target)
{
return sqrtf(target[A_MOTOR] * target[A_MOTOR] + target[B_MOTOR] * target[B_MOTOR]);
}
inline static float wp_convert_to_b_motor_steps (float *target)
{
float xpos = machine.width_mm - target[A_MOTOR];
return sqrtf(xpos * xpos + target[B_MOTOR] * target[B_MOTOR]);
}
// Transform absolute position from cartesian coordinate system to wall plotter coordinate system
static float *transform_from_cartesian (float *target, float *position)
{
uint_fast8_t idx = N_AXIS - 1;
do {
target[idx] = position[idx];
} while(--idx > Y_AXIS);
target[A_MOTOR] = wp_convert_to_a_motor_steps(position);
target[B_MOTOR] = wp_convert_to_b_motor_steps(position);
return target;
}
static inline float get_distance (float *p0, float *p1)
{
uint_fast8_t idx = Z_AXIS;
float distance = 0.0f;
do {
idx--;
distance += (p0[idx] - p1[idx]) * (p0[idx] - p1[idx]);
} while(idx);
return sqrtf(distance);
}
// Wall plotter is circular in motion, so long lines must be divided up
static float *wp_segment_line (float *target, float *position, plan_line_data_t *pl_data, bool init)
{
static uint_fast16_t iterations;
static bool segmented;
static coord_data_t delta, segment_target, final_target, cpos;
// static plan_line_data_t plan;
uint_fast8_t idx = N_AXIS;
if(init) {
jog_cancel = false;
memcpy(final_target.values, target, sizeof(final_target));
transform_to_cartesian(segment_target.values, position);
delta.x = target[X_AXIS] - segment_target.x;
delta.y = target[Y_AXIS] - segment_target.y;
delta.z = target[Z_AXIS] - segment_target.z;
float distance = sqrtf(delta.x * delta.x + delta.y * delta.y);
if((segmented = !pl_data->condition.rapid_motion && distance > MAX_SEG_LENGTH_MM && !(delta.x == 0.0f && delta.y == 0.0f))) {
idx = N_AXIS;
iterations = (uint_fast16_t)ceilf(distance / MAX_SEG_LENGTH_MM);
do {
--idx;
delta.values[idx] = delta.values[idx] / (float)iterations;
} while(idx);
} else {
iterations = 1;
memcpy(&segment_target, &final_target, sizeof(coord_data_t));
}
iterations++; // return at least one iteration
} else {
iterations--;
if(segmented && iterations > 1) {
do {
idx--;
segment_target.values[idx] += delta.values[idx];
} while(idx);
} else
memcpy(&segment_target, &final_target, sizeof(coord_data_t));
transform_from_cartesian(cpos.values, segment_target.values);
}
return iterations == 0 || jog_cancel ? NULL : cpos.values;
}
static uint_fast8_t wp_limits_get_axis_mask (uint_fast8_t idx)
{
return ((idx == A_MOTOR) || (idx == B_MOTOR)) ? (bit(X_AXIS) | bit(Y_AXIS)) : bit(idx);
}
static void wp_limits_set_target_pos (uint_fast8_t idx) // fn name?
{
float xy[2];
int32_t axis_position;
xy[X_AXIS] = sys.position[X_AXIS] / settings.axis[X_AXIS].steps_per_mm;
xy[Y_AXIS] = sys.position[Y_AXIS] / settings.axis[Y_AXIS].steps_per_mm;
switch(idx) {
case X_AXIS:
axis_position = wp_convert_to_b_motor_steps(xy);
sys.position[A_MOTOR] = axis_position;
sys.position[B_MOTOR] = -axis_position;
break;
case Y_AXIS:
sys.position[A_MOTOR] = sys.position[B_MOTOR] = wp_convert_to_a_motor_steps(xy);
break;
default:
sys.position[idx] = 0;
break;
}
}
// Set machine positions for homed limit switches. Don't update non-homed axes.
// NOTE: settings.max_travel[] is stored as a negative value.
static void wp_limits_set_machine_positions (axes_signals_t cycle)
{
float xy[2];
uint_fast8_t idx = N_AXIS;
xy[X_AXIS] = sys.position[X_AXIS] / settings.axis[X_AXIS].steps_per_mm;
xy[Y_AXIS] = sys.position[Y_AXIS] / settings.axis[Y_AXIS].steps_per_mm;
if(settings.homing.flags.force_set_origin) {
if (cycle.mask & bit(--idx)) do {
switch(--idx) {
case X_AXIS:
sys.position[A_MOTOR] = wp_convert_to_b_motor_steps(xy);
sys.position[B_MOTOR] = - sys.position[A_MOTOR];
break;
case Y_AXIS:
sys.position[A_MOTOR] = wp_convert_to_a_motor_steps(xy);
sys.position[B_MOTOR] = sys.position[A_MOTOR];
break;
default:
sys.position[idx] = 0;
break;
}
} while (idx);
} else do {
if (cycle.mask & bit(--idx)) {
int32_t off_axis_position;
int32_t set_axis_position = bit_istrue(settings.homing.dir_mask.value, bit(idx))
? lroundf((settings.axis[idx].max_travel + settings.homing.pulloff) * settings.axis[idx].steps_per_mm)
: lroundf(-settings.homing.pulloff * settings.axis[idx].steps_per_mm);
switch(idx) {
case X_AXIS:
off_axis_position = wp_convert_to_b_motor_steps(xy);
sys.position[A_MOTOR] = set_axis_position + off_axis_position;
sys.position[B_MOTOR] = set_axis_position - off_axis_position;
break;
case Y_AXIS:
off_axis_position = wp_convert_to_a_motor_steps(xy);
sys.position[A_MOTOR] = off_axis_position + set_axis_position;
sys.position[B_MOTOR] = off_axis_position - set_axis_position;
break;
default:
sys.position[idx] = set_axis_position;
break;
}
}
} while(idx);
}
static void cancel_jog (sys_state_t state)
{
jog_cancel = true;
}
static void report_options (bool newopt)
{
on_report_options(newopt);
if(!newopt)
hal.stream.write("[KINEMATICS:WallPlotter v2.00]" ASCII_EOL);
}
// Initialize API pointers for Wall Plotter kinematics
void wall_plotter_init (void)
{
machine.width_mm = -settings.axis[A_MOTOR].max_travel;
machine.width = (int32_t)(machine.width_mm * settings.axis[A_MOTOR].steps_per_mm);
machine.width_2 = machine.width >> 1;
machine.width_pow = machine.width_mm * machine.width_mm;
machine.height = (int32_t)((float)settings.axis[B_MOTOR].max_travel * settings.axis[B_MOTOR].steps_per_mm);
machine.height_2 = machine.height >> 1;
machine.spindlezero[A_MOTOR] = 0; // machine.width_2;
machine.spindlezero[B_MOTOR] = 0; // machine.height_2;
machine.spindlezero_mm[A_MOTOR] = (float)machine.spindlezero[A_MOTOR] / settings.axis[A_MOTOR].steps_per_mm;
machine.spindlezero_mm[B_MOTOR] = (float)machine.spindlezero[B_MOTOR] / settings.axis[B_MOTOR].steps_per_mm;
sys.position[B_MOTOR] = machine.width;
kinematics.limits_set_target_pos = wp_limits_set_target_pos;
kinematics.limits_get_axis_mask = wp_limits_get_axis_mask;
kinematics.limits_set_machine_positions = wp_limits_set_machine_positions;
kinematics.transform_from_cartesian = transform_from_cartesian;
kinematics.transform_steps_to_cartesian = wp_convert_array_steps_to_mpos;
kinematics.segment_line = wp_segment_line;
grbl.on_jog_cancel = cancel_jog;
on_report_options = grbl.on_report_options;
grbl.on_report_options = report_options;
}
#endif