Squashed commit of the following:
[clinton/Smoothieware.git] / src / modules / robot / Robot.cpp
CommitLineData
df27a6a3 1/*
aab6cbba 2 This file is part of Smoothie (http://smoothieware.org/). The motion control part is heavily based on Grbl (https://github.com/simen/grbl) with additions from Sungeun K. Jeon (https://github.com/chamnit/grbl)
4cff3ded
AW
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/>.
4cff3ded
AW
6*/
7
8#include "libs/Module.h"
9#include "libs/Kernel.h"
5673fe39 10
c3df978d
JM
11#include "mbed.h" // for us_ticker_read()
12
5673fe39 13#include <math.h>
4cff3ded
AW
14#include <string>
15using std::string;
5673fe39 16
4cff3ded 17#include "Planner.h"
3fceb8eb 18#include "Conveyor.h"
4cff3ded 19#include "Robot.h"
5673fe39
MM
20#include "nuts_bolts.h"
21#include "Pin.h"
22#include "StepperMotor.h"
23#include "Gcode.h"
5647f709 24#include "PublicDataRequest.h"
928467c0 25#include "PublicData.h"
4cff3ded
AW
26#include "arm_solutions/BaseSolution.h"
27#include "arm_solutions/CartesianSolution.h"
c41d6d95 28#include "arm_solutions/RotatableCartesianSolution.h"
2a06c415 29#include "arm_solutions/LinearDeltaSolution.h"
c52b8675 30#include "arm_solutions/RotatableDeltaSolution.h"
bdaaa75d 31#include "arm_solutions/HBotSolution.h"
fff1e42d 32#include "arm_solutions/CoreXZSolution.h"
1217e470 33#include "arm_solutions/MorganSCARASolution.h"
61134a65 34#include "StepTicker.h"
7af0714f
JM
35#include "checksumm.h"
36#include "utils.h"
8d54c34c 37#include "ConfigValue.h"
5966b7d0 38#include "libs/StreamOutput.h"
dd0a7cfa 39#include "StreamOutputPool.h"
928467c0 40#include "ExtruderPublicAccess.h"
38bf9a1c 41
78d0e16a
MM
42#define default_seek_rate_checksum CHECKSUM("default_seek_rate")
43#define default_feed_rate_checksum CHECKSUM("default_feed_rate")
44#define mm_per_line_segment_checksum CHECKSUM("mm_per_line_segment")
45#define delta_segments_per_second_checksum CHECKSUM("delta_segments_per_second")
46#define mm_per_arc_segment_checksum CHECKSUM("mm_per_arc_segment")
47#define arc_correction_checksum CHECKSUM("arc_correction")
48#define x_axis_max_speed_checksum CHECKSUM("x_axis_max_speed")
49#define y_axis_max_speed_checksum CHECKSUM("y_axis_max_speed")
50#define z_axis_max_speed_checksum CHECKSUM("z_axis_max_speed")
43424972
JM
51
52// arm solutions
78d0e16a
MM
53#define arm_solution_checksum CHECKSUM("arm_solution")
54#define cartesian_checksum CHECKSUM("cartesian")
55#define rotatable_cartesian_checksum CHECKSUM("rotatable_cartesian")
56#define rostock_checksum CHECKSUM("rostock")
2a06c415 57#define linear_delta_checksum CHECKSUM("linear_delta")
c52b8675 58#define rotatable_delta_checksum CHECKSUM("rotatable_delta")
78d0e16a
MM
59#define delta_checksum CHECKSUM("delta")
60#define hbot_checksum CHECKSUM("hbot")
61#define corexy_checksum CHECKSUM("corexy")
fff1e42d 62#define corexz_checksum CHECKSUM("corexz")
78d0e16a 63#define kossel_checksum CHECKSUM("kossel")
1217e470 64#define morgan_checksum CHECKSUM("morgan")
78d0e16a 65
78d0e16a
MM
66// new-style actuator stuff
67#define actuator_checksum CHEKCSUM("actuator")
68
69#define step_pin_checksum CHECKSUM("step_pin")
70#define dir_pin_checksum CHEKCSUM("dir_pin")
71#define en_pin_checksum CHECKSUM("en_pin")
72
73#define steps_per_mm_checksum CHECKSUM("steps_per_mm")
df6a30f2 74#define max_rate_checksum CHECKSUM("max_rate")
78d0e16a
MM
75
76#define alpha_checksum CHECKSUM("alpha")
77#define beta_checksum CHECKSUM("beta")
78#define gamma_checksum CHECKSUM("gamma")
79
38bf9a1c
JM
80#define NEXT_ACTION_DEFAULT 0
81#define NEXT_ACTION_DWELL 1
82#define NEXT_ACTION_GO_HOME 2
83
84#define MOTION_MODE_SEEK 0 // G0
85#define MOTION_MODE_LINEAR 1 // G1
86#define MOTION_MODE_CW_ARC 2 // G2
87#define MOTION_MODE_CCW_ARC 3 // G3
88#define MOTION_MODE_CANCEL 4 // G80
89
90#define PATH_CONTROL_MODE_EXACT_PATH 0
91#define PATH_CONTROL_MODE_EXACT_STOP 1
92#define PATH_CONTROL_MODE_CONTINOUS 2
93
94#define PROGRAM_FLOW_RUNNING 0
95#define PROGRAM_FLOW_PAUSED 1
96#define PROGRAM_FLOW_COMPLETED 2
97
98#define SPINDLE_DIRECTION_CW 0
99#define SPINDLE_DIRECTION_CCW 1
100
5fa0c173
PA
101#define ARC_ANGULAR_TRAVEL_EPSILON 5E-7 // Float (radians)
102
edac9072
AW
103// The Robot converts GCodes into actual movements, and then adds them to the Planner, which passes them to the Conveyor so they can be added to the queue
104// It takes care of cutting arcs into segments, same thing for line that are too long
105
4710532a
JM
106Robot::Robot()
107{
a1b7e9f0 108 this->inch_mode = false;
0e8b102e 109 this->absolute_mode = true;
df27a6a3 110 this->motion_mode = MOTION_MODE_SEEK;
4cff3ded 111 this->select_plane(X_AXIS, Y_AXIS, Z_AXIS);
df27a6a3 112 clear_vector(this->last_milestone);
3632a517 113 clear_vector(this->transformed_last_milestone);
0b804a41 114 this->arm_solution = NULL;
da947c62 115 seconds_per_minute = 60.0F;
fae93525 116 this->clearToolOffset();
3632a517 117 this->compensationTransform= nullptr;
807b9b57
JM
118 this->wcs_offsets.fill(wcs_t(0.0F,0.0F,0.0F));
119 this->g92_offset= wcs_t(0.0F,0.0F,0.0F);
4cff3ded
AW
120}
121
122//Called when the module has just been loaded
4710532a
JM
123void Robot::on_module_loaded()
124{
4cff3ded
AW
125 this->register_for_event(ON_GCODE_RECEIVED);
126
127 // Configuration
807b9b57 128 this->load_config();
da24d6ae
AW
129}
130
807b9b57
JM
131#define ACTUATOR_CHECKSUMS(X) { \
132 CHECKSUM(X "_step_pin"), \
133 CHECKSUM(X "_dir_pin"), \
134 CHECKSUM(X "_en_pin"), \
135 CHECKSUM(X "_steps_per_mm"), \
136 CHECKSUM(X "_max_rate") \
137}
5984acdf 138
807b9b57
JM
139void Robot::load_config()
140{
edac9072
AW
141 // Arm solutions are used to convert positions in millimeters into position in steps for each stepper motor.
142 // While for a cartesian arm solution, this is a simple multiplication, in other, less simple cases, there is some serious math to be done.
143 // To make adding those solution easier, they have their own, separate object.
5984acdf 144 // Here we read the config to find out which arm solution to use
0b804a41 145 if (this->arm_solution) delete this->arm_solution;
eda9facc 146 int solution_checksum = get_checksum(THEKERNEL->config->value(arm_solution_checksum)->by_default("cartesian")->as_string());
d149c730 147 // Note checksums are not const expressions when in debug mode, so don't use switch
98761c28 148 if(solution_checksum == hbot_checksum || solution_checksum == corexy_checksum) {
314ab8f7 149 this->arm_solution = new HBotSolution(THEKERNEL->config);
bdaaa75d 150
fff1e42d
JJ
151 } else if(solution_checksum == corexz_checksum) {
152 this->arm_solution = new CoreXZSolution(THEKERNEL->config);
153
2a06c415
JM
154 } else if(solution_checksum == rostock_checksum || solution_checksum == kossel_checksum || solution_checksum == delta_checksum || solution_checksum == linear_delta_checksum) {
155 this->arm_solution = new LinearDeltaSolution(THEKERNEL->config);
73a4e3c0 156
4710532a 157 } else if(solution_checksum == rotatable_cartesian_checksum) {
314ab8f7 158 this->arm_solution = new RotatableCartesianSolution(THEKERNEL->config);
b73a756d 159
c52b8675
DP
160 } else if(solution_checksum == rotatable_delta_checksum) {
161 this->arm_solution = new RotatableDeltaSolution(THEKERNEL->config);
162
1217e470
QH
163 } else if(solution_checksum == morgan_checksum) {
164 this->arm_solution = new MorganSCARASolution(THEKERNEL->config);
165
4710532a 166 } else if(solution_checksum == cartesian_checksum) {
314ab8f7 167 this->arm_solution = new CartesianSolution(THEKERNEL->config);
73a4e3c0 168
4710532a 169 } else {
314ab8f7 170 this->arm_solution = new CartesianSolution(THEKERNEL->config);
d149c730 171 }
73a4e3c0 172
6b661ab3
DP
173 this->feed_rate = THEKERNEL->config->value(default_feed_rate_checksum )->by_default( 100.0F)->as_number();
174 this->seek_rate = THEKERNEL->config->value(default_seek_rate_checksum )->by_default( 100.0F)->as_number();
175 this->mm_per_line_segment = THEKERNEL->config->value(mm_per_line_segment_checksum )->by_default( 0.0F)->as_number();
176 this->delta_segments_per_second = THEKERNEL->config->value(delta_segments_per_second_checksum )->by_default(0.0f )->as_number();
177 this->mm_per_arc_segment = THEKERNEL->config->value(mm_per_arc_segment_checksum )->by_default( 0.5f)->as_number();
178 this->arc_correction = THEKERNEL->config->value(arc_correction_checksum )->by_default( 5 )->as_number();
78d0e16a 179
6b661ab3
DP
180 this->max_speeds[X_AXIS] = THEKERNEL->config->value(x_axis_max_speed_checksum )->by_default(60000.0F)->as_number() / 60.0F;
181 this->max_speeds[Y_AXIS] = THEKERNEL->config->value(y_axis_max_speed_checksum )->by_default(60000.0F)->as_number() / 60.0F;
182 this->max_speeds[Z_AXIS] = THEKERNEL->config->value(z_axis_max_speed_checksum )->by_default( 300.0F)->as_number() / 60.0F;
feb204be 183
78d0e16a 184 // Make our 3 StepperMotors
807b9b57
JM
185 uint16_t const checksums[][5] = {
186 ACTUATOR_CHECKSUMS("alpha"),
187 ACTUATOR_CHECKSUMS("beta"),
188 ACTUATOR_CHECKSUMS("gamma"),
189#if MAX_ROBOT_ACTUATORS > 3
190 ACTUATOR_CHECKSUMS("delta"),
191 ACTUATOR_CHECKSUMS("epsilon"),
192 ACTUATOR_CHECKSUMS("zeta")
193#endif
194 };
195 constexpr size_t actuator_checksum_count = sizeof(checksums)/sizeof(checksums[0]);
196 static_assert(actuator_checksum_count >= k_max_actuators, "Robot checksum array too small for k_max_actuators");
197
198 size_t motor_count = std::min(this->arm_solution->get_actuator_count(), k_max_actuators);
199 for (size_t a = 0; a < motor_count; a++) {
200 Pin pins[3]; //step, dir, enable
201 for (size_t i = 0; i < 3; i++) {
202 pins[i].from_string(THEKERNEL->config->value(checksums[a][i])->by_default("nc")->as_string())->as_output();
203 }
204 actuators[a]= new StepperMotor(pins[0], pins[1], pins[2]);
78d0e16a 205
807b9b57
JM
206 actuators[a]->change_steps_per_mm(THEKERNEL->config->value(checksums[a][3])->by_default(a==2 ? 2560.0F : 80.0F)->as_number());
207 actuators[a]->set_max_rate(THEKERNEL->config->value(checksums[a][4])->by_default(30000.0F)->as_number());
208 }
a84f0186 209
dd0a7cfa 210 check_max_actuator_speeds(); // check the configs are sane
df6a30f2 211
975469ad
MM
212 // initialise actuator positions to current cartesian position (X0 Y0 Z0)
213 // so the first move can be correct if homing is not performed
807b9b57 214 ActuatorCoordinates actuator_pos;
975469ad 215 arm_solution->cartesian_to_actuator(last_milestone, actuator_pos);
807b9b57 216 for (size_t i = 0; i < actuators.size(); i++)
975469ad 217 actuators[i]->change_last_milestone(actuator_pos[i]);
5966b7d0
AT
218
219 //this->clearToolOffset();
4cff3ded
AW
220}
221
212caccd
JM
222void Robot::push_state()
223{
224 bool am= this->absolute_mode;
225 bool im= this->inch_mode;
226 saved_state_t s(this->feed_rate, this->seek_rate, am, im);
227 state_stack.push(s);
228}
229
230void Robot::pop_state()
231{
232 if(!state_stack.empty()) {
233 auto s= state_stack.top();
234 state_stack.pop();
235 this->feed_rate= std::get<0>(s);
236 this->seek_rate= std::get<1>(s);
237 this->absolute_mode= std::get<2>(s);
238 this->inch_mode= std::get<3>(s);
239 }
240}
241
dd0a7cfa
JM
242// this does a sanity check that actuator speeds do not exceed steps rate capability
243// we will override the actuator max_rate if the combination of max_rate and steps/sec exceeds base_stepping_frequency
244void Robot::check_max_actuator_speeds()
245{
807b9b57
JM
246 for (size_t i = 0; i < actuators.size(); i++) {
247 float step_freq = actuators[i]->get_max_rate() * actuators[i]->get_steps_per_mm();
248 if (step_freq > THEKERNEL->base_stepping_frequency) {
249 actuators[i]->set_max_rate(floorf(THEKERNEL->base_stepping_frequency / actuators[i]->get_steps_per_mm()));
250 THEKERNEL->streams->printf("WARNING: actuator %c rate exceeds base_stepping_frequency * alpha_steps_per_mm: %f, setting to %f\n", 'A'+i, step_freq, actuators[i]->max_rate);
251 }
dd0a7cfa
JM
252 }
253}
254
4cff3ded 255//A GCode has been received
edac9072 256//See if the current Gcode line has some orders for us
4710532a
JM
257void Robot::on_gcode_received(void *argument)
258{
259 Gcode *gcode = static_cast<Gcode *>(argument);
6bc4a00a 260
23c90ba6 261 this->motion_mode = -1;
4cff3ded 262
4710532a
JM
263 //G-letter Gcodes are mostly what the Robot module is interrested in, other modules also catch the gcode event and do stuff accordingly
264 if( gcode->has_g) {
265 switch( gcode->g ) {
6e92ab91
JM
266 case 0: this->motion_mode = MOTION_MODE_SEEK; break;
267 case 1: this->motion_mode = MOTION_MODE_LINEAR; break;
268 case 2: this->motion_mode = MOTION_MODE_CW_ARC; break;
269 case 3: this->motion_mode = MOTION_MODE_CCW_ARC; break;
c3df978d
JM
270 case 4: {
271 uint32_t delay_ms= 0;
272 if (gcode->has_letter('P')) {
273 delay_ms= gcode->get_int('P');
274 }
275 if (gcode->has_letter('S')) {
276 delay_ms += gcode->get_int('S') * 1000;
277 }
278 if (delay_ms > 0){
c3df978d
JM
279 // drain queue
280 THEKERNEL->conveyor->wait_for_empty_queue();
281 // wait for specified time
6ac0b51c 282 uint32_t start= us_ticker_read(); // mbed call
c3df978d
JM
283 while ((us_ticker_read() - start) < delay_ms*1000) {
284 THEKERNEL->call_event(ON_IDLE, this);
285 }
286 }
adba2978 287 }
6b661ab3 288 break;
807b9b57
JM
289
290 case 10: // G10 L2 Pn Xn Yn Zn set WCS
291 // TODO implement G10 L20
292 if(gcode->has_letter('L') && gcode->get_int('L') == 2 && gcode->has_letter('P')) {
293 size_t n= gcode->get_uint('P');
294 if(n == 0) n= current_wcs; // set current coordinate system
295 else --n;
296 if(n < k_max_wcs) {
297 float x, y, z;
298 std::tie(x, y, z)= wcs_offsets[n];
299 if(gcode->has_letter('X')) x= this->to_millimeters(gcode->get_value('X'));
300 if(gcode->has_letter('Y')) y= this->to_millimeters(gcode->get_value('Y'));
301 if(gcode->has_letter('Z')) z= this->to_millimeters(gcode->get_value('Z'));
302 wcs_offsets[n]= wcs_t(x, y, z);
303 }
304 }
305 break;
306
6e92ab91
JM
307 case 17: this->select_plane(X_AXIS, Y_AXIS, Z_AXIS); break;
308 case 18: this->select_plane(X_AXIS, Z_AXIS, Y_AXIS); break;
309 case 19: this->select_plane(Y_AXIS, Z_AXIS, X_AXIS); break;
310 case 20: this->inch_mode = true; break;
311 case 21: this->inch_mode = false; break;
807b9b57
JM
312
313 case 54: case 55: case 56: case 57: case 58: case 59:
314 // select WCS 0-8: G54..G59, G59.1, G59.2, G59.3
315 current_wcs= gcode->g - 54;
316 if(gcode->g == 59 && gcode->subcode > 0) {
317 current_wcs += gcode->subcode;
318 if(current_wcs >= k_max_wcs) current_wcs= k_max_wcs-1;
319 }
320 break;
321
6e92ab91
JM
322 case 90: this->absolute_mode = true; break;
323 case 91: this->absolute_mode = false; break;
807b9b57 324
0b804a41 325 case 92: {
807b9b57
JM
326 if(gcode->subcode == 1 || gcode->subcode == 2 || gcode->get_num_args() == 0) {
327 g92_offset= wcs_t(0,0,0);
cef9acea 328
4710532a 329 } else {
807b9b57
JM
330 float x, y, z;
331 std::tie(x, y, z)= g92_offset;
332 if(gcode->has_letter('X')) x= this->to_millimeters(gcode->get_value('X'));
333 if(gcode->has_letter('Y')) y= this->to_millimeters(gcode->get_value('Y'));
334 if(gcode->has_letter('Z')) z= this->to_millimeters(gcode->get_value('Z'));
335 g92_offset= wcs_t(x, y, z);
6bc4a00a 336 }
78d0e16a 337 return;
4710532a
JM
338 }
339 }
340 } else if( gcode->has_m) {
341 switch( gcode->m ) {
807b9b57
JM
342 case 2: // M2 end of program
343 current_wcs= 0;
344 absolute_mode= true;
345 motion_mode = MOTION_MODE_LINEAR; // feed
346 break;
347
0fb5b438 348 case 92: // M92 - set steps per mm
0fb5b438 349 if (gcode->has_letter('X'))
78d0e16a 350 actuators[0]->change_steps_per_mm(this->to_millimeters(gcode->get_value('X')));
0fb5b438 351 if (gcode->has_letter('Y'))
78d0e16a 352 actuators[1]->change_steps_per_mm(this->to_millimeters(gcode->get_value('Y')));
0fb5b438 353 if (gcode->has_letter('Z'))
78d0e16a 354 actuators[2]->change_steps_per_mm(this->to_millimeters(gcode->get_value('Z')));
7369629d
MM
355 if (gcode->has_letter('F'))
356 seconds_per_minute = gcode->get_value('F');
78d0e16a
MM
357
358 gcode->stream->printf("X:%g Y:%g Z:%g F:%g ", actuators[0]->steps_per_mm, actuators[1]->steps_per_mm, actuators[2]->steps_per_mm, seconds_per_minute);
0fb5b438 359 gcode->add_nl = true;
dd0a7cfa 360 check_max_actuator_speeds();
0fb5b438 361 return;
562db364 362
4710532a 363 case 114: {
58c32991 364 char buf[64];
807b9b57
JM
365 int n= 0;
366 if(gcode->subcode == 0) { // M114 print WCS
367 n = snprintf(buf, sizeof(buf), "C: X:%1.3f Y:%1.3f Z:%1.3f",
368 from_millimeters(this->last_milestone[0]),
369 from_millimeters(this->last_milestone[1]),
370 from_millimeters(this->last_milestone[2]));
371
372 }else if(gcode->subcode == 1) { // M114.1 print Machine coordinate system
373 // TODO figure this out
374 n = snprintf(buf, sizeof(buf), "X:%1.3f Y:%1.3f Z:%1.3f",
4710532a
JM
375 from_millimeters(this->last_milestone[0]),
376 from_millimeters(this->last_milestone[1]),
807b9b57
JM
377 from_millimeters(this->last_milestone[2]));
378
379 }else if(gcode->subcode == 2) { // M114.2 print realtime actuator position
380 n = snprintf(buf, sizeof(buf), "A:%1.3f B:%1.3f C:%1.3f",
58c32991
JM
381 actuators[X_AXIS]->get_current_position(),
382 actuators[Y_AXIS]->get_current_position(),
383 actuators[Z_AXIS]->get_current_position() );
807b9b57
JM
384 }
385 if(n > 0)
386 gcode->txt_after_ok.append(buf, n);
4710532a
JM
387 }
388 return;
33e4cc02 389
212caccd
JM
390 case 120: // push state
391 push_state();
392 break;
562db364
JM
393
394 case 121: // pop state
212caccd 395 pop_state();
562db364
JM
396 break;
397
83488642
JM
398 case 203: // M203 Set maximum feedrates in mm/sec
399 if (gcode->has_letter('X'))
4710532a 400 this->max_speeds[X_AXIS] = gcode->get_value('X');
83488642 401 if (gcode->has_letter('Y'))
4710532a 402 this->max_speeds[Y_AXIS] = gcode->get_value('Y');
83488642 403 if (gcode->has_letter('Z'))
4710532a 404 this->max_speeds[Z_AXIS] = gcode->get_value('Z');
807b9b57
JM
405 for (size_t i = 0; i < 3 && i < actuators.size(); i++) {
406 if (gcode->has_letter('A' + i))
407 actuators[i]->set_max_rate(gcode->get_value('A' + i));
408 }
dd0a7cfa
JM
409 check_max_actuator_speeds();
410
928467c0 411 if(gcode->get_num_args() == 0) {
807b9b57
JM
412 gcode->stream->printf("X:%g Y:%g Z:%g",
413 this->max_speeds[X_AXIS], this->max_speeds[Y_AXIS], this->max_speeds[Z_AXIS]);
414 for (size_t i = 0; i < 3 && i < actuators.size(); i++) {
415 gcode->stream->printf(" %c : %g", 'A' + i, actuators[i]->get_max_rate()); //xxx
416 }
928467c0
JM
417 gcode->add_nl = true;
418 }
83488642
JM
419 break;
420
c5fe1787 421 case 204: // M204 Snnn - set acceleration to nnn, Znnn sets z acceleration
4710532a 422 if (gcode->has_letter('S')) {
4710532a 423 float acc = gcode->get_value('S'); // mm/s^2
d4ee6ee2 424 // enforce minimum
da947c62
MM
425 if (acc < 1.0F)
426 acc = 1.0F;
4710532a 427 THEKERNEL->planner->acceleration = acc;
d4ee6ee2 428 }
c5fe1787 429 if (gcode->has_letter('Z')) {
c5fe1787
JM
430 float acc = gcode->get_value('Z'); // mm/s^2
431 // enforce positive
432 if (acc < 0.0F)
433 acc = 0.0F;
434 THEKERNEL->planner->z_acceleration = acc;
435 }
d4ee6ee2
JM
436 break;
437
9502f9d5 438 case 205: // M205 Xnnn - set junction deviation, Z - set Z junction deviation, Snnn - Set minimum planner speed, Ynnn - set minimum step rate
4710532a
JM
439 if (gcode->has_letter('X')) {
440 float jd = gcode->get_value('X');
d4ee6ee2 441 // enforce minimum
8b69c90d
JM
442 if (jd < 0.0F)
443 jd = 0.0F;
4710532a 444 THEKERNEL->planner->junction_deviation = jd;
d4ee6ee2 445 }
107df03f
JM
446 if (gcode->has_letter('Z')) {
447 float jd = gcode->get_value('Z');
448 // enforce minimum, -1 disables it and uses regular junction deviation
449 if (jd < -1.0F)
450 jd = -1.0F;
451 THEKERNEL->planner->z_junction_deviation = jd;
452 }
4710532a
JM
453 if (gcode->has_letter('S')) {
454 float mps = gcode->get_value('S');
8b69c90d
JM
455 // enforce minimum
456 if (mps < 0.0F)
457 mps = 0.0F;
4710532a 458 THEKERNEL->planner->minimum_planner_speed = mps;
8b69c90d 459 }
9502f9d5 460 if (gcode->has_letter('Y')) {
807b9b57 461 actuators[0]->default_minimum_actuator_rate = gcode->get_value('Y');
9502f9d5 462 }
d4ee6ee2 463 break;
98761c28 464
7369629d 465 case 220: // M220 - speed override percentage
4710532a 466 if (gcode->has_letter('S')) {
1ad23cd3 467 float factor = gcode->get_value('S');
98761c28 468 // enforce minimum 10% speed
da947c62
MM
469 if (factor < 10.0F)
470 factor = 10.0F;
471 // enforce maximum 10x speed
472 if (factor > 1000.0F)
473 factor = 1000.0F;
474
475 seconds_per_minute = 6000.0F / factor;
adba2978 476 }else{
9ef9f45b 477 gcode->stream->printf("Speed factor at %6.2f %%\n", 6000.0F / seconds_per_minute);
7369629d 478 }
b4f56013 479 break;
ec4773e5 480
494dc541 481 case 400: // wait until all moves are done up to this point
314ab8f7 482 THEKERNEL->conveyor->wait_for_empty_queue();
494dc541
JM
483 break;
484
33e4cc02 485 case 500: // M500 saves some volatile settings to config override file
b7cd847e 486 case 503: { // M503 just prints the settings
78d0e16a 487 gcode->stream->printf(";Steps per unit:\nM92 X%1.5f Y%1.5f Z%1.5f\n", actuators[0]->steps_per_mm, actuators[1]->steps_per_mm, actuators[2]->steps_per_mm);
c5fe1787 488 gcode->stream->printf(";Acceleration mm/sec^2:\nM204 S%1.5f Z%1.5f\n", THEKERNEL->planner->acceleration, THEKERNEL->planner->z_acceleration);
c9cc5e06 489 gcode->stream->printf(";X- Junction Deviation, Z- Z junction deviation, S - Minimum Planner speed mm/sec:\nM205 X%1.5f Z%1.5f S%1.5f\n", THEKERNEL->planner->junction_deviation, THEKERNEL->planner->z_junction_deviation, THEKERNEL->planner->minimum_planner_speed);
807b9b57
JM
490 gcode->stream->printf(";Max feedrates in mm/sec, XYZ cartesian, ABC actuator:\nM203 X%1.5f Y%1.5f Z%1.5f",
491 this->max_speeds[X_AXIS], this->max_speeds[Y_AXIS], this->max_speeds[Z_AXIS]);
492 for (size_t i=0; i < 3 && i < actuators.size(); i++){
493 gcode->stream->printf(" %c%1.5f", 'A' + i, actuators[i]->get_max_rate());
494 }
495 gcode->stream->printf("\n");
b7cd847e
JM
496
497 // get or save any arm solution specific optional values
498 BaseSolution::arm_options_t options;
499 if(arm_solution->get_optional(options) && !options.empty()) {
500 gcode->stream->printf(";Optional arm solution specific settings:\nM665");
4710532a 501 for(auto &i : options) {
b7cd847e
JM
502 gcode->stream->printf(" %c%1.4f", i.first, i.second);
503 }
504 gcode->stream->printf("\n");
505 }
6e92ab91 506
807b9b57
JM
507 // save wcs_offsets and current_wcs
508 // TODO this may need to be done whenever they change to be compliant
509 gcode->stream->printf(";WCS settings\n");
510 gcode->stream->printf("G5%c", std::min(current_wcs, (uint8_t)(5 + '4')));
511 if(current_wcs >= 6) {
512 gcode->stream->printf(".%c\n", '1' + (current_wcs-5));
513 }else{
514 gcode->stream->printf("\n");
515 }
516 int n= 1;
517 for(auto &i : wcs_offsets) {
518 if(i != wcs_t(0,0,0)) {
519 float x, y, z;
520 std::tie(x, y, z) = i;
521 gcode->stream->printf("G10 L2 P%d X%f Y%f Z%f\n", n, x, y, z);
522 }
523 ++n;
524 }
b7cd847e 525 }
807b9b57 526 break;
33e4cc02 527
b7cd847e 528 case 665: { // M665 set optional arm solution variables based on arm solution.
ebc75fc6
JM
529 // the parameter args could be any letter each arm solution only accepts certain ones
530 BaseSolution::arm_options_t options= gcode->get_args();
531 options.erase('S'); // don't include the S
532 options.erase('U'); // don't include the U
533 if(options.size() > 0) {
534 // set the specified options
535 arm_solution->set_optional(options);
536 }
537 options.clear();
b7cd847e 538 if(arm_solution->get_optional(options)) {
ebc75fc6 539 // foreach optional value
4710532a 540 for(auto &i : options) {
b7cd847e
JM
541 // print all current values of supported options
542 gcode->stream->printf("%c: %8.4f ", i.first, i.second);
5523c05d 543 gcode->add_nl = true;
ec4773e5
JM
544 }
545 }
ec4773e5 546
4a839bea 547 if(gcode->has_letter('S')) { // set delta segments per second, not saved by M500
4710532a 548 this->delta_segments_per_second = gcode->get_value('S');
4a839bea
JM
549 gcode->stream->printf("Delta segments set to %8.4f segs/sec\n", this->delta_segments_per_second);
550
551 }else if(gcode->has_letter('U')) { // or set mm_per_line_segment, not saved by M500
552 this->mm_per_line_segment = gcode->get_value('U');
553 this->delta_segments_per_second = 0;
554 gcode->stream->printf("mm per line segment set to %8.4f\n", this->mm_per_line_segment);
ec29d378 555 }
4a839bea 556
ec4773e5 557 break;
b7cd847e 558 }
6989211c 559 }
494dc541
JM
560 }
561
c83887ea
MM
562 if( this->motion_mode < 0)
563 return;
6bc4a00a 564
4710532a 565 //Get parameters
1ad23cd3 566 float target[3], offset[3];
c2885de8 567 clear_vector(offset);
6bc4a00a 568
2ba859c9 569 memcpy(target, this->last_milestone, sizeof(target)); //default to last target
6bc4a00a 570
4710532a
JM
571 for(char letter = 'I'; letter <= 'K'; letter++) {
572 if( gcode->has_letter(letter) ) {
573 offset[letter - 'I'] = this->to_millimeters(gcode->get_value(letter));
c2885de8
JM
574 }
575 }
4710532a
JM
576 for(char letter = 'X'; letter <= 'Z'; letter++) {
577 if( gcode->has_letter(letter) ) {
c7689006 578 target[letter - 'X'] = this->to_millimeters(gcode->get_value(letter)) + (this->absolute_mode ? this->toolOffset[letter - 'X'] : target[letter - 'X']);
c2885de8
JM
579 }
580 }
6bc4a00a 581
4710532a 582 if( gcode->has_letter('F') ) {
7369629d 583 if( this->motion_mode == MOTION_MODE_SEEK )
da947c62 584 this->seek_rate = this->to_millimeters( gcode->get_value('F') );
7369629d 585 else
da947c62 586 this->feed_rate = this->to_millimeters( gcode->get_value('F') );
7369629d 587 }
6bc4a00a 588
4cff3ded 589 //Perform any physical actions
fae93525
JM
590 switch(this->motion_mode) {
591 case MOTION_MODE_CANCEL: break;
592 case MOTION_MODE_SEEK : this->append_line(gcode, target, this->seek_rate / seconds_per_minute ); break;
593 case MOTION_MODE_LINEAR: this->append_line(gcode, target, this->feed_rate / seconds_per_minute ); break;
594 case MOTION_MODE_CW_ARC:
595 case MOTION_MODE_CCW_ARC: this->compute_arc(gcode, offset, target ); break;
4cff3ded 596 }
13e4a3f9 597
fae93525 598 // last_milestone was set to target in append_milestone, no need to do it again
4cff3ded 599
edac9072
AW
600}
601
5984acdf 602// We received a new gcode, and one of the functions
edac9072
AW
603// determined the distance for that given gcode. So now we can attach this gcode to the right block
604// and continue
4710532a
JM
605void Robot::distance_in_gcode_is_known(Gcode *gcode)
606{
edac9072 607 //If the queue is empty, execute immediatly, otherwise attach to the last added block
e0ee24ed 608 THEKERNEL->conveyor->append_gcode(gcode);
edac9072
AW
609}
610
cef9acea
JM
611// reset the position for all axis (used in homing for delta as last_milestone may be bogus)
612void Robot::reset_axis_position(float x, float y, float z)
613{
614 this->last_milestone[X_AXIS] = x;
615 this->last_milestone[Y_AXIS] = y;
616 this->last_milestone[Z_AXIS] = z;
3632a517
JM
617 this->transformed_last_milestone[X_AXIS] = x;
618 this->transformed_last_milestone[Y_AXIS] = y;
619 this->transformed_last_milestone[Z_AXIS] = z;
cef9acea 620
807b9b57 621 ActuatorCoordinates actuator_pos;
cef9acea 622 arm_solution->cartesian_to_actuator(this->last_milestone, actuator_pos);
807b9b57 623 for (size_t i = 0; i < actuators.size(); i++)
cef9acea
JM
624 actuators[i]->change_last_milestone(actuator_pos[i]);
625}
626
807b9b57 627// Reset the position for an axis (used in homing)
4710532a
JM
628void Robot::reset_axis_position(float position, int axis)
629{
2ba859c9 630 this->last_milestone[axis] = position;
3632a517 631 this->transformed_last_milestone[axis] = position;
29c28822 632
807b9b57 633 ActuatorCoordinates actuator_pos;
cef9acea 634 arm_solution->cartesian_to_actuator(this->last_milestone, actuator_pos);
29c28822 635
807b9b57 636 for (size_t i = 0; i < actuators.size(); i++)
29c28822 637 actuators[i]->change_last_milestone(actuator_pos[i]);
4cff3ded
AW
638}
639
728477c4 640// Use FK to find out where actuator is and reset lastmilestone to match
728477c4
JM
641void Robot::reset_position_from_current_actuator_position()
642{
807b9b57
JM
643 ActuatorCoordinates actuator_pos;
644 for (size_t i = 0; i < actuators.size(); i++) {
645 actuator_pos[i] = actuators[i]->get_current_position();
646 }
58c32991 647 arm_solution->actuator_to_cartesian(actuator_pos, this->last_milestone);
4befe777 648 memcpy(this->transformed_last_milestone, this->last_milestone, sizeof(this->transformed_last_milestone));
cf91d4f3
JM
649
650 // now reset actuator correctly, NOTE this may lose a little precision
651 arm_solution->cartesian_to_actuator(this->last_milestone, actuator_pos);
807b9b57 652 for (size_t i = 0; i < actuators.size(); i++)
cf91d4f3 653 actuators[i]->change_last_milestone(actuator_pos[i]);
728477c4 654}
edac9072 655
4cff3ded 656// Convert target from millimeters to steps, and append this to the planner
d2adef5e 657void Robot::append_milestone(Gcode *gcode, float target[], float rate_mm_s)
df6a30f2 658{
1ad23cd3 659 float deltas[3];
df6a30f2 660 float unit_vec[3];
807b9b57 661 ActuatorCoordinates actuator_pos;
3632a517 662 float transformed_target[3]; // adjust target for bed compensation
df6a30f2
MM
663 float millimeters_of_travel;
664
3632a517
JM
665 // unity transform by default
666 memcpy(transformed_target, target, sizeof(transformed_target));
5e45206a 667
3632a517
JM
668 // check function pointer and call if set to transform the target to compensate for bed
669 if(compensationTransform) {
670 // some compensation strategies can transform XYZ, some just change Z
671 compensationTransform(transformed_target);
33742399 672 }
ff7e9858 673
807b9b57
JM
674 // apply wcs offsets and g92 offset
675 transformed_target[0] += (std::get<0>(wcs_offsets[current_wcs]) + std::get<0>(g92_offset));
676 transformed_target[1] += (std::get<1>(wcs_offsets[current_wcs]) + std::get<1>(g92_offset));
677 transformed_target[2] += (std::get<2>(wcs_offsets[current_wcs]) + std::get<2>(g92_offset));
678
3632a517
JM
679 // find distance moved by each axis, use transformed target from last_transformed_target
680 for (int axis = X_AXIS; axis <= Z_AXIS; axis++){
681 deltas[axis] = transformed_target[axis] - transformed_last_milestone[axis];
682 }
683 // store last transformed
684 memcpy(this->transformed_last_milestone, transformed_target, sizeof(this->transformed_last_milestone));
aab6cbba 685
edac9072 686 // Compute how long this move moves, so we can attach it to the block for later use
869acfb8 687 millimeters_of_travel = sqrtf( powf( deltas[X_AXIS], 2 ) + powf( deltas[Y_AXIS], 2 ) + powf( deltas[Z_AXIS], 2 ) );
df6a30f2
MM
688
689 // find distance unit vector
690 for (int i = 0; i < 3; i++)
691 unit_vec[i] = deltas[i] / millimeters_of_travel;
692
693 // Do not move faster than the configured cartesian limits
4710532a
JM
694 for (int axis = X_AXIS; axis <= Z_AXIS; axis++) {
695 if ( max_speeds[axis] > 0 ) {
da947c62 696 float axis_speed = fabs(unit_vec[axis] * rate_mm_s);
df6a30f2
MM
697
698 if (axis_speed > max_speeds[axis])
da947c62 699 rate_mm_s *= ( max_speeds[axis] / axis_speed );
7b470506
AW
700 }
701 }
4cff3ded 702
5e45206a 703 // find actuator position given cartesian position, use actual adjusted target
3632a517 704 arm_solution->cartesian_to_actuator( transformed_target, actuator_pos );
df6a30f2 705
928467c0 706 float isecs= rate_mm_s / millimeters_of_travel;
df6a30f2 707 // check per-actuator speed limits
807b9b57 708 for (size_t actuator = 0; actuator < actuators.size(); actuator++) {
928467c0
JM
709 float actuator_rate = fabsf(actuator_pos[actuator] - actuators[actuator]->last_milestone_mm) * isecs;
710 if (actuator_rate > actuators[actuator]->get_max_rate()){
3494f3d0 711 rate_mm_s *= (actuators[actuator]->get_max_rate() / actuator_rate);
4caeff22 712 isecs= rate_mm_s / millimeters_of_travel;
928467c0
JM
713 }
714 }
715
edac9072 716 // Append the block to the planner
da947c62 717 THEKERNEL->planner->append_block( actuator_pos, rate_mm_s, millimeters_of_travel, unit_vec );
4cff3ded 718
5e45206a 719 // Update the last_milestone to the current target for the next time we use last_milestone, use the requested target not the adjusted one
c2885de8 720 memcpy(this->last_milestone, target, sizeof(this->last_milestone)); // this->last_milestone[] = target[];
4cff3ded
AW
721
722}
723
edac9072 724// Append a move to the queue ( cutting it into segments if needed )
4710532a
JM
725void Robot::append_line(Gcode *gcode, float target[], float rate_mm_s )
726{
edac9072 727 // Find out the distance for this gcode
a9d299ab 728 // NOTE we need to do sqrt here as this setting of millimeters_of_travel is used by extruder and other modules even if there is no XYZ move
3b4b05b8 729 gcode->millimeters_of_travel = sqrtf(powf( target[X_AXIS] - this->last_milestone[X_AXIS], 2 ) + powf( target[Y_AXIS] - this->last_milestone[Y_AXIS], 2 ) + powf( target[Z_AXIS] - this->last_milestone[Z_AXIS], 2 ));
4cff3ded 730
3b4b05b8
JM
731 // We ignore non- XYZ moves ( for example, extruder moves are not XYZ moves )
732 if( gcode->millimeters_of_travel < 0.00001F ) {
95b4885b
JM
733 return;
734 }
436a2cd1 735
edac9072 736 // Mark the gcode as having a known distance
5dcb2ff3 737 this->distance_in_gcode_is_known( gcode );
436a2cd1 738
d2adef5e
JM
739 // if we have volumetric limits enabled we calculate the volume for this move and limit the rate if it exceeds the stated limit
740 // Note we need to be using volumetric extrusion for this to work as Ennn is in mm³ not mm
741 // We also check we are not exceeding the E max_speed for the current extruder
742 // We ask Extruder to do all the work, but as Extruder won't even see this gcode until after it has been planned
743 // we need to ask it now passing in the relevant data.
744 // NOTE we need to do this before we segment the line (for deltas)
745 if(gcode->has_letter('E')) {
746 float data[2];
747 data[0]= gcode->get_value('E'); // E target (maybe absolute or relative)
748 data[1]= rate_mm_s / gcode->millimeters_of_travel; // inverted seconds for the move
749 if(PublicData::set_value(extruder_checksum, target_checksum, data)) {
750 rate_mm_s *= data[1];
751 //THEKERNEL->streams->printf("Extruder has changed the rate by %f to %f\n", data[1], rate_mm_s);
752 }
753 }
754
4a0c8e14
JM
755 // We cut the line into smaller segments. This is not usefull in a cartesian robot, but necessary for robots with rotational axes.
756 // In cartesian robot, a high "mm_per_line_segment" setting will prevent waste.
3b4b05b8
JM
757 // In delta robots either mm_per_line_segment can be used OR delta_segments_per_second
758 // The latter is more efficient and avoids splitting fast long lines into very small segments, like initial z move to 0, it is what Johanns Marlin delta port does
4a0c8e14 759 uint16_t segments;
5984acdf 760
c2885de8 761 if(this->delta_segments_per_second > 1.0F) {
4a0c8e14
JM
762 // enabled if set to something > 1, it is set to 0.0 by default
763 // segment based on current speed and requested segments per second
764 // the faster the travel speed the fewer segments needed
765 // NOTE rate is mm/sec and we take into account any speed override
da947c62 766 float seconds = gcode->millimeters_of_travel / rate_mm_s;
9502f9d5 767 segments = max(1.0F, ceilf(this->delta_segments_per_second * seconds));
4a0c8e14 768 // TODO if we are only moving in Z on a delta we don't really need to segment at all
5984acdf 769
4710532a
JM
770 } else {
771 if(this->mm_per_line_segment == 0.0F) {
772 segments = 1; // don't split it up
773 } else {
9502f9d5 774 segments = ceilf( gcode->millimeters_of_travel / this->mm_per_line_segment);
4a0c8e14
JM
775 }
776 }
5984acdf 777
4710532a 778 if (segments > 1) {
2ba859c9
MM
779 // A vector to keep track of the endpoint of each segment
780 float segment_delta[3];
781 float segment_end[3];
782
783 // How far do we move each segment?
9fff6045 784 for (int i = X_AXIS; i <= Z_AXIS; i++)
2ba859c9 785 segment_delta[i] = (target[i] - last_milestone[i]) / segments;
4cff3ded 786
c8e0fb15
MM
787 // segment 0 is already done - it's the end point of the previous move so we start at segment 1
788 // We always add another point after this loop so we stop at segments-1, ie i < segments
4710532a 789 for (int i = 1; i < segments; i++) {
73706276 790 if(THEKERNEL->is_halted()) return; // don't queue any more segments
4710532a 791 for(int axis = X_AXIS; axis <= Z_AXIS; axis++ )
2ba859c9
MM
792 segment_end[axis] = last_milestone[axis] + segment_delta[axis];
793
794 // Append the end of this segment to the queue
928467c0 795 this->append_milestone(gcode, segment_end, rate_mm_s);
2ba859c9 796 }
4cff3ded 797 }
5984acdf
MM
798
799 // Append the end of this full move to the queue
928467c0 800 this->append_milestone(gcode, target, rate_mm_s);
2134bcf2
MM
801
802 // if adding these blocks didn't start executing, do that now
803 THEKERNEL->conveyor->ensure_running();
4cff3ded
AW
804}
805
4cff3ded 806
edac9072 807// Append an arc to the queue ( cutting it into segments as needed )
4710532a
JM
808void Robot::append_arc(Gcode *gcode, float target[], float offset[], float radius, bool is_clockwise )
809{
aab6cbba 810
edac9072 811 // Scary math
2ba859c9
MM
812 float center_axis0 = this->last_milestone[this->plane_axis_0] + offset[this->plane_axis_0];
813 float center_axis1 = this->last_milestone[this->plane_axis_1] + offset[this->plane_axis_1];
814 float linear_travel = target[this->plane_axis_2] - this->last_milestone[this->plane_axis_2];
1ad23cd3
MM
815 float r_axis0 = -offset[this->plane_axis_0]; // Radius vector from center to current location
816 float r_axis1 = -offset[this->plane_axis_1];
817 float rt_axis0 = target[this->plane_axis_0] - center_axis0;
818 float rt_axis1 = target[this->plane_axis_1] - center_axis1;
aab6cbba 819
51871fb8 820 // Patch from GRBL Firmware - Christoph Baumann 04072015
aab6cbba 821 // CCW angle between position and target from circle center. Only one atan2() trig computation required.
5fa0c173
PA
822 float angular_travel = atan2(r_axis0*rt_axis1-r_axis1*rt_axis0, r_axis0*rt_axis0+r_axis1*rt_axis1);
823 if (is_clockwise) { // Correct atan2 output per direction
824 if (angular_travel >= -ARC_ANGULAR_TRAVEL_EPSILON) { angular_travel -= 2*M_PI; }
825 } else {
826 if (angular_travel <= ARC_ANGULAR_TRAVEL_EPSILON) { angular_travel += 2*M_PI; }
4710532a 827 }
aab6cbba 828
edac9072 829 // Find the distance for this gcode
4710532a 830 gcode->millimeters_of_travel = hypotf(angular_travel * radius, fabs(linear_travel));
436a2cd1 831
edac9072 832 // We don't care about non-XYZ moves ( for example the extruder produces some of those )
3b4b05b8 833 if( gcode->millimeters_of_travel < 0.00001F ) {
4710532a
JM
834 return;
835 }
5dcb2ff3 836
edac9072 837 // Mark the gcode as having a known distance
d149c730 838 this->distance_in_gcode_is_known( gcode );
5984acdf
MM
839
840 // Figure out how many segments for this gcode
c8f4ee77 841 uint16_t segments = floorf(gcode->millimeters_of_travel / this->mm_per_arc_segment);
aab6cbba 842
4710532a
JM
843 float theta_per_segment = angular_travel / segments;
844 float linear_per_segment = linear_travel / segments;
aab6cbba
AW
845
846 /* Vector rotation by transformation matrix: r is the original vector, r_T is the rotated vector,
847 and phi is the angle of rotation. Based on the solution approach by Jens Geisler.
848 r_T = [cos(phi) -sin(phi);
849 sin(phi) cos(phi] * r ;
850 For arc generation, the center of the circle is the axis of rotation and the radius vector is
851 defined from the circle center to the initial position. Each line segment is formed by successive
852 vector rotations. This requires only two cos() and sin() computations to form the rotation
853 matrix for the duration of the entire arc. Error may accumulate from numerical round-off, since
1ad23cd3 854 all float numbers are single precision on the Arduino. (True float precision will not have
aab6cbba
AW
855 round off issues for CNC applications.) Single precision error can accumulate to be greater than
856 tool precision in some cases. Therefore, arc path correction is implemented.
857
858 Small angle approximation may be used to reduce computation overhead further. This approximation
859 holds for everything, but very small circles and large mm_per_arc_segment values. In other words,
860 theta_per_segment would need to be greater than 0.1 rad and N_ARC_CORRECTION would need to be large
861 to cause an appreciable drift error. N_ARC_CORRECTION~=25 is more than small enough to correct for
862 numerical drift error. N_ARC_CORRECTION may be on the order a hundred(s) before error becomes an
863 issue for CNC machines with the single precision Arduino calculations.
864 This approximation also allows mc_arc to immediately insert a line segment into the planner
865 without the initial overhead of computing cos() or sin(). By the time the arc needs to be applied
866 a correction, the planner should have caught up to the lag caused by the initial mc_arc overhead.
867 This is important when there are successive arc motions.
868 */
869 // Vector rotation matrix values
4710532a 870 float cos_T = 1 - 0.5F * theta_per_segment * theta_per_segment; // Small angle approximation
1ad23cd3 871 float sin_T = theta_per_segment;
aab6cbba 872
1ad23cd3
MM
873 float arc_target[3];
874 float sin_Ti;
875 float cos_Ti;
876 float r_axisi;
aab6cbba
AW
877 uint16_t i;
878 int8_t count = 0;
879
880 // Initialize the linear axis
2ba859c9 881 arc_target[this->plane_axis_2] = this->last_milestone[this->plane_axis_2];
aab6cbba 882
4710532a 883 for (i = 1; i < segments; i++) { // Increment (segments-1)
73706276 884 if(THEKERNEL->is_halted()) return; // don't queue any more segments
aab6cbba 885
b66fb830 886 if (count < this->arc_correction ) {
4710532a
JM
887 // Apply vector rotation matrix
888 r_axisi = r_axis0 * sin_T + r_axis1 * cos_T;
889 r_axis0 = r_axis0 * cos_T - r_axis1 * sin_T;
890 r_axis1 = r_axisi;
891 count++;
aab6cbba 892 } else {
4710532a
JM
893 // Arc correction to radius vector. Computed only every N_ARC_CORRECTION increments.
894 // Compute exact location by applying transformation matrix from initial radius vector(=-offset).
895 cos_Ti = cosf(i * theta_per_segment);
896 sin_Ti = sinf(i * theta_per_segment);
897 r_axis0 = -offset[this->plane_axis_0] * cos_Ti + offset[this->plane_axis_1] * sin_Ti;
898 r_axis1 = -offset[this->plane_axis_0] * sin_Ti - offset[this->plane_axis_1] * cos_Ti;
899 count = 0;
aab6cbba
AW
900 }
901
902 // Update arc_target location
903 arc_target[this->plane_axis_0] = center_axis0 + r_axis0;
904 arc_target[this->plane_axis_1] = center_axis1 + r_axis1;
905 arc_target[this->plane_axis_2] += linear_per_segment;
edac9072
AW
906
907 // Append this segment to the queue
928467c0 908 this->append_milestone(gcode, arc_target, this->feed_rate / seconds_per_minute);
aab6cbba
AW
909
910 }
edac9072 911
aab6cbba 912 // Ensure last segment arrives at target location.
928467c0 913 this->append_milestone(gcode, target, this->feed_rate / seconds_per_minute);
aab6cbba
AW
914}
915
edac9072 916// Do the math for an arc and add it to the queue
4710532a
JM
917void Robot::compute_arc(Gcode *gcode, float offset[], float target[])
918{
aab6cbba
AW
919
920 // Find the radius
13addf09 921 float radius = hypotf(offset[this->plane_axis_0], offset[this->plane_axis_1]);
aab6cbba
AW
922
923 // Set clockwise/counter-clockwise sign for mc_arc computations
924 bool is_clockwise = false;
4710532a
JM
925 if( this->motion_mode == MOTION_MODE_CW_ARC ) {
926 is_clockwise = true;
927 }
aab6cbba
AW
928
929 // Append arc
436a2cd1 930 this->append_arc(gcode, target, offset, radius, is_clockwise );
aab6cbba
AW
931
932}
933
934
4710532a
JM
935float Robot::theta(float x, float y)
936{
937 float t = atanf(x / fabs(y));
938 if (y > 0) {
939 return(t);
940 } else {
941 if (t > 0) {
942 return(M_PI - t);
943 } else {
944 return(-M_PI - t);
945 }
946 }
4cff3ded
AW
947}
948
4710532a
JM
949void Robot::select_plane(uint8_t axis_0, uint8_t axis_1, uint8_t axis_2)
950{
4cff3ded
AW
951 this->plane_axis_0 = axis_0;
952 this->plane_axis_1 = axis_1;
953 this->plane_axis_2 = axis_2;
954}
955
fae93525 956void Robot::clearToolOffset()
4710532a 957{
fae93525
JM
958 memset(this->toolOffset, 0, sizeof(this->toolOffset));
959}
960
961void Robot::setToolOffset(const float offset[3])
962{
fae93525 963 memcpy(this->toolOffset, offset, sizeof(this->toolOffset));
5966b7d0
AT
964}
965