Merge pull request #182 from wolfmanjm/add/johann-kossel-arm-solution
[clinton/Smoothieware.git] / src / modules / robot / Robot.cpp
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) with additions from Sungeun K. Jeon (https://github.com/chamnit/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 <string>
11 using std::string;
12 #include <math.h>
13 #include "Planner.h"
14 #include "Conveyor.h"
15 #include "Robot.h"
16 #include "libs/nuts_bolts.h"
17 #include "libs/Pin.h"
18 #include "libs/StepperMotor.h"
19 #include "../communication/utils/Gcode.h"
20 #include "PublicDataRequest.h"
21 #include "arm_solutions/BaseSolution.h"
22 #include "arm_solutions/CartesianSolution.h"
23 #include "arm_solutions/RotatableCartesianSolution.h"
24 #include "arm_solutions/RostockSolution.h"
25 #include "arm_solutions/JohannKosselSolution.h"
26 #include "arm_solutions/HBotSolution.h"
27
28 // 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
29 // It takes care of cutting arcs into segments, same thing for line that are too long
30
31 Robot::Robot(){
32 this->inch_mode = false;
33 this->absolute_mode = true;
34 this->motion_mode = MOTION_MODE_SEEK;
35 this->select_plane(X_AXIS, Y_AXIS, Z_AXIS);
36 clear_vector(this->current_position);
37 clear_vector(this->last_milestone);
38 this->arm_solution = NULL;
39 seconds_per_minute = 60.0;
40 }
41
42 //Called when the module has just been loaded
43 void Robot::on_module_loaded() {
44 register_for_event(ON_CONFIG_RELOAD);
45 this->register_for_event(ON_GCODE_RECEIVED);
46 this->register_for_event(ON_GET_PUBLIC_DATA);
47 this->register_for_event(ON_SET_PUBLIC_DATA);
48
49 // Configuration
50 this->on_config_reload(this);
51
52 // Make our 3 StepperMotors
53 this->alpha_stepper_motor = this->kernel->step_ticker->add_stepper_motor( new StepperMotor(&alpha_step_pin,&alpha_dir_pin,&alpha_en_pin) );
54 this->beta_stepper_motor = this->kernel->step_ticker->add_stepper_motor( new StepperMotor(&beta_step_pin, &beta_dir_pin, &beta_en_pin ) );
55 this->gamma_stepper_motor = this->kernel->step_ticker->add_stepper_motor( new StepperMotor(&gamma_step_pin,&gamma_dir_pin,&gamma_en_pin) );
56
57 }
58
59 void Robot::on_config_reload(void* argument){
60
61 // Arm solutions are used to convert positions in millimeters into position in steps for each stepper motor.
62 // While for a cartesian arm solution, this is a simple multiplication, in other, less simple cases, there is some serious math to be done.
63 // To make adding those solution easier, they have their own, separate object.
64 // Here we read the config to find out which arm solution to use
65 if (this->arm_solution) delete this->arm_solution;
66 int solution_checksum = get_checksum(this->kernel->config->value(arm_solution_checksum)->by_default("cartesian")->as_string());
67 // Note checksums are not const expressions when in debug mode, so don't use switch
68 if(solution_checksum == hbot_checksum) {
69 this->arm_solution = new HBotSolution(this->kernel->config);
70
71 }else if(solution_checksum == rostock_checksum) {
72 this->arm_solution = new RostockSolution(this->kernel->config);
73
74 }else if(solution_checksum == kossel_checksum) {
75 this->arm_solution = new JohannKosselSolution(this->kernel->config);
76
77 }else if(solution_checksum == delta_checksum) {
78 // place holder for now
79 this->arm_solution = new RostockSolution(this->kernel->config);
80
81 }else if(solution_checksum == rotatable_cartesian_checksum) {
82 this->arm_solution = new RotatableCartesianSolution(this->kernel->config);
83
84 }else if(solution_checksum == cartesian_checksum) {
85 this->arm_solution = new CartesianSolution(this->kernel->config);
86
87 }else{
88 this->arm_solution = new CartesianSolution(this->kernel->config);
89 }
90
91
92 this->feed_rate = this->kernel->config->value(default_feed_rate_checksum )->by_default(100 )->as_number() / 60;
93 this->seek_rate = this->kernel->config->value(default_seek_rate_checksum )->by_default(100 )->as_number() / 60;
94 this->mm_per_line_segment = this->kernel->config->value(mm_per_line_segment_checksum )->by_default(0.0 )->as_number();
95 this->delta_segments_per_second = this->kernel->config->value(delta_segments_per_second_checksum )->by_default(0.0 )->as_number();
96 this->mm_per_arc_segment = this->kernel->config->value(mm_per_arc_segment_checksum )->by_default(0.5 )->as_number();
97 this->arc_correction = this->kernel->config->value(arc_correction_checksum )->by_default(5 )->as_number();
98 this->max_speeds[X_AXIS] = this->kernel->config->value(x_axis_max_speed_checksum )->by_default(60000 )->as_number();
99 this->max_speeds[Y_AXIS] = this->kernel->config->value(y_axis_max_speed_checksum )->by_default(60000 )->as_number();
100 this->max_speeds[Z_AXIS] = this->kernel->config->value(z_axis_max_speed_checksum )->by_default(300 )->as_number();
101 this->alpha_step_pin.from_string( this->kernel->config->value(alpha_step_pin_checksum )->by_default("2.0" )->as_string())->as_output();
102 this->alpha_dir_pin.from_string( this->kernel->config->value(alpha_dir_pin_checksum )->by_default("0.5" )->as_string())->as_output();
103 this->alpha_en_pin.from_string( this->kernel->config->value(alpha_en_pin_checksum )->by_default("0.4" )->as_string())->as_output();
104 this->beta_step_pin.from_string( this->kernel->config->value(beta_step_pin_checksum )->by_default("2.1" )->as_string())->as_output();
105 this->gamma_step_pin.from_string( this->kernel->config->value(gamma_step_pin_checksum )->by_default("2.2" )->as_string())->as_output();
106 this->gamma_dir_pin.from_string( this->kernel->config->value(gamma_dir_pin_checksum )->by_default("0.20" )->as_string())->as_output();
107 this->gamma_en_pin.from_string( this->kernel->config->value(gamma_en_pin_checksum )->by_default("0.19" )->as_string())->as_output();
108 this->beta_dir_pin.from_string( this->kernel->config->value(beta_dir_pin_checksum )->by_default("0.11" )->as_string())->as_output();
109 this->beta_en_pin.from_string( this->kernel->config->value(beta_en_pin_checksum )->by_default("0.10" )->as_string())->as_output();
110
111 }
112
113 void Robot::on_get_public_data(void* argument){
114 PublicDataRequest* pdr = static_cast<PublicDataRequest*>(argument);
115
116 if(!pdr->starts_with(robot_checksum)) return;
117
118 if(pdr->second_element_is(speed_override_percent_checksum)) {
119 static double return_data= 100*60/seconds_per_minute;
120 pdr->set_data_ptr(&return_data);
121 pdr->set_taken();
122
123 }else if(pdr->second_element_is(current_position_checksum)) {
124 static double return_data[3];
125 return_data[0]= from_millimeters(this->current_position[0]);
126 return_data[1]= from_millimeters(this->current_position[1]);
127 return_data[2]= from_millimeters(this->current_position[2]);
128
129 pdr->set_data_ptr(&return_data);
130 pdr->set_taken();
131 }
132 }
133
134 void Robot::on_set_public_data(void* argument){
135 PublicDataRequest* pdr = static_cast<PublicDataRequest*>(argument);
136
137 if(!pdr->starts_with(robot_checksum)) return;
138
139 if(pdr->second_element_is(speed_override_percent_checksum)) {
140 double t= *static_cast<double*>(pdr->get_data_ptr());
141 seconds_per_minute= t * 0.6;
142 pdr->set_taken();
143 }
144 }
145
146 //A GCode has been received
147 //See if the current Gcode line has some orders for us
148 void Robot::on_gcode_received(void * argument){
149 Gcode* gcode = static_cast<Gcode*>(argument);
150
151 //Temp variables, constant properties are stored in the object
152 uint8_t next_action = NEXT_ACTION_DEFAULT;
153 this->motion_mode = -1;
154
155 //G-letter Gcodes are mostly what the Robot module is interrested in, other modules also catch the gcode event and do stuff accordingly
156 if( gcode->has_g){
157 switch( gcode->g ){
158 case 0: this->motion_mode = MOTION_MODE_SEEK; gcode->mark_as_taken(); break;
159 case 1: this->motion_mode = MOTION_MODE_LINEAR; gcode->mark_as_taken(); break;
160 case 2: this->motion_mode = MOTION_MODE_CW_ARC; gcode->mark_as_taken(); break;
161 case 3: this->motion_mode = MOTION_MODE_CCW_ARC; gcode->mark_as_taken(); break;
162 case 17: this->select_plane(X_AXIS, Y_AXIS, Z_AXIS); gcode->mark_as_taken(); break;
163 case 18: this->select_plane(X_AXIS, Z_AXIS, Y_AXIS); gcode->mark_as_taken(); break;
164 case 19: this->select_plane(Y_AXIS, Z_AXIS, X_AXIS); gcode->mark_as_taken(); break;
165 case 20: this->inch_mode = true; gcode->mark_as_taken(); break;
166 case 21: this->inch_mode = false; gcode->mark_as_taken(); break;
167 case 90: this->absolute_mode = true; gcode->mark_as_taken(); break;
168 case 91: this->absolute_mode = false; gcode->mark_as_taken(); break;
169 case 92: {
170 if(gcode->get_num_args() == 0){
171 clear_vector(this->last_milestone);
172 }else{
173 for (char letter = 'X'; letter <= 'Z'; letter++){
174 if ( gcode->has_letter(letter) )
175 this->last_milestone[letter-'X'] = this->to_millimeters(gcode->get_value(letter));
176 }
177 }
178 memcpy(this->current_position, this->last_milestone, sizeof(double)*3); // current_position[] = last_milestone[];
179 this->arm_solution->millimeters_to_steps(this->current_position, this->kernel->planner->position);
180 gcode->mark_as_taken();
181 return; // TODO: Wait until queue empty
182 }
183 }
184 }else if( gcode->has_m){
185 switch( gcode->m ){
186 case 92: // M92 - set steps per mm
187 double steps[3];
188 this->arm_solution->get_steps_per_millimeter(steps);
189 if (gcode->has_letter('X'))
190 steps[0] = this->to_millimeters(gcode->get_value('X'));
191 if (gcode->has_letter('Y'))
192 steps[1] = this->to_millimeters(gcode->get_value('Y'));
193 if (gcode->has_letter('Z'))
194 steps[2] = this->to_millimeters(gcode->get_value('Z'));
195 if (gcode->has_letter('F'))
196 seconds_per_minute = gcode->get_value('F');
197 this->arm_solution->set_steps_per_millimeter(steps);
198 // update current position in steps
199 this->arm_solution->millimeters_to_steps(this->current_position, this->kernel->planner->position);
200 gcode->stream->printf("X:%g Y:%g Z:%g F:%g ", steps[0], steps[1], steps[2], seconds_per_minute);
201 gcode->add_nl = true;
202 gcode->mark_as_taken();
203 return;
204 case 114: gcode->stream->printf("C: X:%1.3f Y:%1.3f Z:%1.3f ",
205 from_millimeters(this->current_position[0]),
206 from_millimeters(this->current_position[1]),
207 from_millimeters(this->current_position[2]));
208 gcode->add_nl = true;
209 gcode->mark_as_taken();
210 return;
211 case 204: // M204 Snnn - set acceleration to nnn, NB only Snnn is currently supported
212 gcode->mark_as_taken();
213 if (gcode->has_letter('S'))
214 {
215 double acc= gcode->get_value('S');
216 // enforce minimum
217 if (acc < 1.0)
218 acc = 1.0;
219 this->kernel->planner->acceleration= acc;
220 }
221 break;
222
223 case 220: // M220 - speed override percentage
224 gcode->mark_as_taken();
225 if (gcode->has_letter('S'))
226 {
227 double factor = gcode->get_value('S');
228 // enforce minimum 1% speed
229 if (factor < 1.0)
230 factor = 1.0;
231 seconds_per_minute = factor * 0.6;
232 }
233 break;
234 }
235 }
236 if( this->motion_mode < 0)
237 return;
238
239 //Get parameters
240 double target[3], offset[3];
241 clear_vector(target); clear_vector(offset);
242
243 memcpy(target, this->current_position, sizeof(target)); //default to last target
244
245 for(char letter = 'I'; letter <= 'K'; letter++){ if( gcode->has_letter(letter) ){ offset[letter-'I'] = this->to_millimeters(gcode->get_value(letter)); } }
246 for(char letter = 'X'; letter <= 'Z'; letter++){ if( gcode->has_letter(letter) ){ target[letter-'X'] = this->to_millimeters(gcode->get_value(letter)) + ( this->absolute_mode ? 0 : target[letter-'X']); } }
247
248 if( gcode->has_letter('F') )
249 {
250 if( this->motion_mode == MOTION_MODE_SEEK )
251 this->seek_rate = this->to_millimeters( gcode->get_value('F') ) / 60.0;
252 else
253 this->feed_rate = this->to_millimeters( gcode->get_value('F') ) / 60.0;
254 }
255
256 //Perform any physical actions
257 switch( next_action ){
258 case NEXT_ACTION_DEFAULT:
259 switch(this->motion_mode){
260 case MOTION_MODE_CANCEL: break;
261 case MOTION_MODE_SEEK : this->append_line(gcode, target, this->seek_rate ); break;
262 case MOTION_MODE_LINEAR: this->append_line(gcode, target, this->feed_rate ); break;
263 case MOTION_MODE_CW_ARC: case MOTION_MODE_CCW_ARC: this->compute_arc(gcode, offset, target ); break;
264 }
265 break;
266 }
267
268 // As far as the parser is concerned, the position is now == target. In reality the
269 // motion control system might still be processing the action and the real tool position
270 // in any intermediate location.
271 memcpy(this->current_position, target, sizeof(double)*3); // this->position[] = target[];
272
273 }
274
275 // We received a new gcode, and one of the functions
276 // determined the distance for that given gcode. So now we can attach this gcode to the right block
277 // and continue
278 void Robot::distance_in_gcode_is_known(Gcode* gcode){
279
280 //If the queue is empty, execute immediatly, otherwise attach to the last added block
281 if( this->kernel->conveyor->queue.size() == 0 ){
282 this->kernel->call_event(ON_GCODE_EXECUTE, gcode );
283 }else{
284 Block* block = this->kernel->conveyor->queue.get_ref( this->kernel->conveyor->queue.size() - 1 );
285 block->append_gcode(gcode);
286 }
287
288 }
289
290 // Reset the position for all axes ( used in homing and G92 stuff )
291 void Robot::reset_axis_position(double position, int axis) {
292 this->last_milestone[axis] = this->current_position[axis] = position;
293 this->arm_solution->millimeters_to_steps(this->current_position, this->kernel->planner->position);
294 }
295
296
297 // Convert target from millimeters to steps, and append this to the planner
298 void Robot::append_milestone( double target[], double rate ){
299 int steps[3]; //Holds the result of the conversion
300
301 // We use an arm solution object so exotic arm solutions can be used and neatly abstracted
302 this->arm_solution->millimeters_to_steps( target, steps );
303
304 double deltas[3];
305 for(int axis=X_AXIS;axis<=Z_AXIS;axis++){deltas[axis]=target[axis]-this->last_milestone[axis];}
306
307 // Compute how long this move moves, so we can attach it to the block for later use
308 double millimeters_of_travel = sqrt( pow( deltas[X_AXIS], 2 ) + pow( deltas[Y_AXIS], 2 ) + pow( deltas[Z_AXIS], 2 ) );
309
310 // Do not move faster than the configured limits
311 for(int axis=X_AXIS;axis<=Z_AXIS;axis++){
312 if( this->max_speeds[axis] > 0 ){
313 double axis_speed = ( fabs(deltas[axis]) / ( millimeters_of_travel / rate )) * seconds_per_minute;
314 if( axis_speed > this->max_speeds[axis] ){
315 rate = rate * ( this->max_speeds[axis] / axis_speed );
316 }
317 }
318 }
319
320 // Append the block to the planner
321 this->kernel->planner->append_block( steps, rate * seconds_per_minute, millimeters_of_travel, deltas );
322
323 // Update the last_milestone to the current target for the next time we use last_milestone
324 memcpy(this->last_milestone, target, sizeof(double)*3); // this->last_milestone[] = target[];
325
326 }
327
328 // Append a move to the queue ( cutting it into segments if needed )
329 void Robot::append_line(Gcode* gcode, double target[], double rate ){
330
331 // Find out the distance for this gcode
332 gcode->millimeters_of_travel = sqrt( pow( target[X_AXIS]-this->current_position[X_AXIS], 2 ) + pow( target[Y_AXIS]-this->current_position[Y_AXIS], 2 ) + pow( target[Z_AXIS]-this->current_position[Z_AXIS], 2 ) );
333
334 // We ignore non-moves ( for example, extruder moves are not XYZ moves )
335 if( gcode->millimeters_of_travel < 0.0001 ){ return; }
336
337 // Mark the gcode as having a known distance
338 this->distance_in_gcode_is_known( gcode );
339
340 // We cut the line into smaller segments. This is not usefull in a cartesian robot, but necessary for robots with rotational axes.
341 // In cartesian robot, a high "mm_per_line_segment" setting will prevent waste.
342 // In delta robots either mm_per_line_segment can be used OR delta_segments_per_second 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
343 uint16_t segments;
344
345 if(this->delta_segments_per_second > 1.0) {
346 // enabled if set to something > 1, it is set to 0.0 by default
347 // segment based on current speed and requested segments per second
348 // the faster the travel speed the fewer segments needed
349 // NOTE rate is mm/sec and we take into account any speed override
350 float seconds = 60.0/seconds_per_minute * gcode->millimeters_of_travel / rate;
351 segments= max(1, ceil(this->delta_segments_per_second * seconds));
352 // TODO if we are only moving in Z on a delta we don't really need to segment at all
353
354 }else{
355 if(this->mm_per_line_segment == 0.0){
356 segments= 1; // don't split it up
357 }else{
358 segments = ceil( gcode->millimeters_of_travel/ this->mm_per_line_segment);
359 }
360 }
361
362 // A vector to keep track of the endpoint of each segment
363 double temp_target[3];
364 //Initialize axes
365 memcpy( temp_target, this->current_position, sizeof(double)*3); // temp_target[] = this->current_position[];
366
367 //For each segment
368 for( int i=0; i<segments-1; i++ ){
369 for(int axis=X_AXIS; axis <= Z_AXIS; axis++ ){ temp_target[axis] += ( target[axis]-this->current_position[axis] )/segments; }
370 // Append the end of this segment to the queue
371 this->append_milestone(temp_target, rate);
372 }
373
374 // Append the end of this full move to the queue
375 this->append_milestone(target, rate);
376 }
377
378
379 // Append an arc to the queue ( cutting it into segments as needed )
380 void Robot::append_arc(Gcode* gcode, double target[], double offset[], double radius, bool is_clockwise ){
381
382 // Scary math
383 double center_axis0 = this->current_position[this->plane_axis_0] + offset[this->plane_axis_0];
384 double center_axis1 = this->current_position[this->plane_axis_1] + offset[this->plane_axis_1];
385 double linear_travel = target[this->plane_axis_2] - this->current_position[this->plane_axis_2];
386 double r_axis0 = -offset[this->plane_axis_0]; // Radius vector from center to current location
387 double r_axis1 = -offset[this->plane_axis_1];
388 double rt_axis0 = target[this->plane_axis_0] - center_axis0;
389 double rt_axis1 = target[this->plane_axis_1] - center_axis1;
390
391 // CCW angle between position and target from circle center. Only one atan2() trig computation required.
392 double angular_travel = atan2(r_axis0*rt_axis1-r_axis1*rt_axis0, r_axis0*rt_axis0+r_axis1*rt_axis1);
393 if (angular_travel < 0) { angular_travel += 2*M_PI; }
394 if (is_clockwise) { angular_travel -= 2*M_PI; }
395
396 // Find the distance for this gcode
397 gcode->millimeters_of_travel = hypot(angular_travel*radius, fabs(linear_travel));
398
399 // We don't care about non-XYZ moves ( for example the extruder produces some of those )
400 if( gcode->millimeters_of_travel < 0.0001 ){ return; }
401
402 // Mark the gcode as having a known distance
403 this->distance_in_gcode_is_known( gcode );
404
405 // Figure out how many segments for this gcode
406 uint16_t segments = floor(gcode->millimeters_of_travel/this->mm_per_arc_segment);
407
408 double theta_per_segment = angular_travel/segments;
409 double linear_per_segment = linear_travel/segments;
410
411 /* Vector rotation by transformation matrix: r is the original vector, r_T is the rotated vector,
412 and phi is the angle of rotation. Based on the solution approach by Jens Geisler.
413 r_T = [cos(phi) -sin(phi);
414 sin(phi) cos(phi] * r ;
415 For arc generation, the center of the circle is the axis of rotation and the radius vector is
416 defined from the circle center to the initial position. Each line segment is formed by successive
417 vector rotations. This requires only two cos() and sin() computations to form the rotation
418 matrix for the duration of the entire arc. Error may accumulate from numerical round-off, since
419 all double numbers are single precision on the Arduino. (True double precision will not have
420 round off issues for CNC applications.) Single precision error can accumulate to be greater than
421 tool precision in some cases. Therefore, arc path correction is implemented.
422
423 Small angle approximation may be used to reduce computation overhead further. This approximation
424 holds for everything, but very small circles and large mm_per_arc_segment values. In other words,
425 theta_per_segment would need to be greater than 0.1 rad and N_ARC_CORRECTION would need to be large
426 to cause an appreciable drift error. N_ARC_CORRECTION~=25 is more than small enough to correct for
427 numerical drift error. N_ARC_CORRECTION may be on the order a hundred(s) before error becomes an
428 issue for CNC machines with the single precision Arduino calculations.
429 This approximation also allows mc_arc to immediately insert a line segment into the planner
430 without the initial overhead of computing cos() or sin(). By the time the arc needs to be applied
431 a correction, the planner should have caught up to the lag caused by the initial mc_arc overhead.
432 This is important when there are successive arc motions.
433 */
434 // Vector rotation matrix values
435 double cos_T = 1-0.5*theta_per_segment*theta_per_segment; // Small angle approximation
436 double sin_T = theta_per_segment;
437
438 double arc_target[3];
439 double sin_Ti;
440 double cos_Ti;
441 double r_axisi;
442 uint16_t i;
443 int8_t count = 0;
444
445 // Initialize the linear axis
446 arc_target[this->plane_axis_2] = this->current_position[this->plane_axis_2];
447
448 for (i = 1; i<segments; i++) { // Increment (segments-1)
449
450 if (count < this->arc_correction ) {
451 // Apply vector rotation matrix
452 r_axisi = r_axis0*sin_T + r_axis1*cos_T;
453 r_axis0 = r_axis0*cos_T - r_axis1*sin_T;
454 r_axis1 = r_axisi;
455 count++;
456 } else {
457 // Arc correction to radius vector. Computed only every N_ARC_CORRECTION increments.
458 // Compute exact location by applying transformation matrix from initial radius vector(=-offset).
459 cos_Ti = cos(i*theta_per_segment);
460 sin_Ti = sin(i*theta_per_segment);
461 r_axis0 = -offset[this->plane_axis_0]*cos_Ti + offset[this->plane_axis_1]*sin_Ti;
462 r_axis1 = -offset[this->plane_axis_0]*sin_Ti - offset[this->plane_axis_1]*cos_Ti;
463 count = 0;
464 }
465
466 // Update arc_target location
467 arc_target[this->plane_axis_0] = center_axis0 + r_axis0;
468 arc_target[this->plane_axis_1] = center_axis1 + r_axis1;
469 arc_target[this->plane_axis_2] += linear_per_segment;
470
471 // Append this segment to the queue
472 this->append_milestone(arc_target, this->feed_rate);
473
474 }
475
476 // Ensure last segment arrives at target location.
477 this->append_milestone(target, this->feed_rate);
478 }
479
480 // Do the math for an arc and add it to the queue
481 void Robot::compute_arc(Gcode* gcode, double offset[], double target[]){
482
483 // Find the radius
484 double radius = hypot(offset[this->plane_axis_0], offset[this->plane_axis_1]);
485
486 // Set clockwise/counter-clockwise sign for mc_arc computations
487 bool is_clockwise = false;
488 if( this->motion_mode == MOTION_MODE_CW_ARC ){ is_clockwise = true; }
489
490 // Append arc
491 this->append_arc(gcode, target, offset, radius, is_clockwise );
492
493 }
494
495
496 double Robot::theta(double x, double y){
497 double t = atan(x/fabs(y));
498 if (y>0) {return(t);} else {if (t>0){return(M_PI-t);} else {return(-M_PI-t);}}
499 }
500
501 void Robot::select_plane(uint8_t axis_0, uint8_t axis_1, uint8_t axis_2){
502 this->plane_axis_0 = axis_0;
503 this->plane_axis_1 = axis_1;
504 this->plane_axis_2 = axis_2;
505 }
506
507