planner.cpp

Fri, 17 Nov 2017 10:13:31 +0100

author
mdd
date
Fri, 17 Nov 2017 10:13:31 +0100
changeset 3
770b218a4931
parent 1
b584642d4f58
permissions
-rw-r--r--

proper configuration, homing and planner optimization

0
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1 /*
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
2 planner.c - buffers movement commands and manages the acceleration profile plan
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
3 Part of Grbl
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
4
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
5 Copyright (c) 2009-2011 Simen Svale Skogsrud
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
6
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
7 Grbl is free software: you can redistribute it and/or modify
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
8 it under the terms of the GNU General Public License as published by
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
9 the Free Software Foundation, either version 3 of the License, or
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
10 (at your option) any later version.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
11
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
12 Grbl is distributed in the hope that it will be useful,
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
15 GNU General Public License for more details.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
16
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
17 You should have received a copy of the GNU General Public License
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
18 along with Grbl. If not, see <http://www.gnu.org/licenses/>.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
19 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
20
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
21 /* The ring buffer implementation gleaned from the wiring_serial library by David A. Mellis. */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
22
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
23 /*
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
24 Reasoning behind the mathematics in this module (in the key of 'Mathematica'):
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
25
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
26 s == speed, a == acceleration, t == time, d == distance
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
27
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
28 Basic definitions:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
29
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
30 Speed[s_, a_, t_] := s + (a*t)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
31 Travel[s_, a_, t_] := Integrate[Speed[s, a, t], t]
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
32
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
33 Distance to reach a specific speed with a constant acceleration:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
34
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
35 Solve[{Speed[s, a, t] == m, Travel[s, a, t] == d}, d, t]
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
36 d -> (m^2 - s^2)/(2 a) --> estimate_acceleration_distance()
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
37
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
38 Speed after a given distance of travel with constant acceleration:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
39
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
40 Solve[{Speed[s, a, t] == m, Travel[s, a, t] == d}, m, t]
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
41 m -> Sqrt[2 a d + s^2]
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
42
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
43 DestinationSpeed[s_, a_, d_] := Sqrt[2 a d + s^2]
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
44
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
45 When to start braking (di) to reach a specified destionation speed (s2) after accelerating
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
46 from initial speed s1 without ever stopping at a plateau:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
47
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
48 Solve[{DestinationSpeed[s1, a, di] == DestinationSpeed[s2, a, d - di]}, di]
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
49 di -> (2 a d - s1^2 + s2^2)/(4 a) --> intersection_distance()
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
50
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
51 IntersectionDistance[s1_, s2_, a_, d_] := (2 a d - s1^2 + s2^2)/(4 a)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
52 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
53
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
54 #include "Marlin.h"
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
55 #include "planner.h"
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
56 #include "stepper.h"
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
57 #include "temperature.h"
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
58 #include "ultralcd.h"
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
59 #include "language.h"
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
60 #include "led.h"
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
61
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
62 //===========================================================================
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
63 //=============================public variables ============================
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
64 //===========================================================================
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
65
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
66 unsigned long minsegmenttime;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
67 float max_feedrate[4]; // set the max speeds
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
68 float axis_steps_per_unit[4];
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
69 unsigned long max_acceleration_units_per_sq_second[4]; // Use M201 to override by software
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
70 float minimumfeedrate;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
71 float acceleration; // Normal acceleration mm/s^2 THIS IS THE DEFAULT ACCELERATION for all moves. M204 SXXXX
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
72 float retract_acceleration; // mm/s^2 filament pull-pack and push-forward while standing still in the other axis M204 TXXXX
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
73 float max_xy_jerk; //speed than can be stopped at once, if i understand correctly.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
74 float max_z_jerk;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
75 float max_e_jerk;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
76 float mintravelfeedrate;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
77 unsigned long axis_steps_per_sqr_second[NUM_AXIS];
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
78
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
79 // The current position of the tool in absolute steps
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
80 long position[4]; //rescaled from extern when axis_steps_per_unit are changed by gcode
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
81 static float previous_speed[4]; // Speed of previous path line segment
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
82 static float previous_nominal_speed; // Nominal speed of previous path line segment
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
83
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
84 extern volatile int extrudemultiply; // Sets extrude multiply factor (in percent)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
85
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
86 #ifdef AUTOTEMP
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
87 float autotemp_max=250;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
88 float autotemp_min=210;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
89 float autotemp_factor=0.1;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
90 bool autotemp_enabled=false;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
91 #endif
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
92
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
93 //===========================================================================
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
94 //=================semi-private variables, used in inline functions =====
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
95 //===========================================================================
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
96 block_t block_buffer[BLOCK_BUFFER_SIZE]; // A ring buffer for motion instfructions
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
97 volatile unsigned char block_buffer_head; // Index of the next block to be pushed
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
98 volatile unsigned char block_buffer_tail; // Index of the block to process now
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
99
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
100 //===========================================================================
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
101 //=============================private variables ============================
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
102 //===========================================================================
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
103 #ifdef PREVENT_DANGEROUS_EXTRUDE
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
104 bool allow_cold_extrude=false;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
105 #endif
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
106 #ifdef XY_FREQUENCY_LIMIT
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
107 // Used for the frequency limit
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
108 static unsigned char old_direction_bits = 0; // Old direction bits. Used for speed calculations
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
109 static long x_segment_time[3]={0,0,0}; // Segment times (in us). Used for speed calculations
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
110 static long y_segment_time[3]={0,0,0};
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
111 #endif
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
112
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
113 // Returns the index of the next block in the ring buffer
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
114 // NOTE: Removed modulo (%) operator, which uses an expensive divide and multiplication.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
115 static int8_t next_block_index(int8_t block_index) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
116 block_index++;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
117 if (block_index == BLOCK_BUFFER_SIZE) { block_index = 0; }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
118 return(block_index);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
119 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
120
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
121
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
122 // Returns the index of the previous block in the ring buffer
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
123 static int8_t prev_block_index(int8_t block_index) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
124 if (block_index == 0) { block_index = BLOCK_BUFFER_SIZE; }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
125 block_index--;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
126 return(block_index);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
127 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
128
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
129 //===========================================================================
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
130 //=============================functions ============================
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
131 //===========================================================================
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
132
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
133 // Calculates the distance (not time) it takes to accelerate from initial_rate to target_rate using the
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
134 // given acceleration:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
135 FORCE_INLINE float estimate_acceleration_distance(float initial_rate, float target_rate, float acceleration)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
136 {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
137 if (acceleration!=0) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
138 return((target_rate*target_rate-initial_rate*initial_rate)/
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
139 (2.0*acceleration));
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
140 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
141 else {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
142 return 0.0; // acceleration was 0, set acceleration distance to 0
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
143 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
144 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
145
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
146 // This function gives you the point at which you must start braking (at the rate of -acceleration) if
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
147 // you started at speed initial_rate and accelerated until this point and want to end at the final_rate after
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
148 // a total travel of distance. This can be used to compute the intersection point between acceleration and
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
149 // deceleration in the cases where the trapezoid has no plateau (i.e. never reaches maximum speed)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
150
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
151 FORCE_INLINE float intersection_distance(float initial_rate, float final_rate, float acceleration, float distance)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
152 {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
153 if (acceleration!=0) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
154 return((2.0*acceleration*distance-initial_rate*initial_rate+final_rate*final_rate)/
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
155 (4.0*acceleration) );
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
156 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
157 else {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
158 return 0.0; // acceleration was 0, set intersection distance to 0
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
159 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
160 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
161
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
162 // Calculates trapezoid parameters so that the entry- and exit-speed is compensated by the provided factors.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
163
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
164 void calculate_trapezoid_for_block(block_t *block, float entry_factor, float exit_factor) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
165 unsigned long initial_rate = ceil(block->nominal_rate*entry_factor); // (step/min)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
166 unsigned long final_rate = ceil(block->nominal_rate*exit_factor); // (step/min)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
167
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
168 // Limit minimal step rate (Otherwise the timer will overflow.)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
169 if(initial_rate <120) {initial_rate=120; }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
170 if(final_rate < 120) {final_rate=120; }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
171
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
172 long acceleration = block->acceleration_st;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
173 int32_t accelerate_steps =
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
174 ceil(estimate_acceleration_distance(block->initial_rate, block->nominal_rate, acceleration));
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
175 int32_t decelerate_steps =
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
176 floor(estimate_acceleration_distance(block->nominal_rate, block->final_rate, -acceleration));
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
177
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
178 // Calculate the size of Plateau of Nominal Rate.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
179 int32_t plateau_steps = block->step_event_count-accelerate_steps-decelerate_steps;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
180
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
181 // Is the Plateau of Nominal Rate smaller than nothing? That means no cruising, and we will
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
182 // have to use intersection_distance() to calculate when to abort acceleration and start braking
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
183 // in order to reach the final_rate exactly at the end of this block.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
184 if (plateau_steps < 0) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
185 accelerate_steps = ceil(
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
186 intersection_distance(block->initial_rate, block->final_rate, acceleration, block->step_event_count));
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
187 accelerate_steps = max(accelerate_steps,0); // Check limits due to numerical round-off
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
188 accelerate_steps = min(accelerate_steps,block->step_event_count);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
189 plateau_steps = 0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
190 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
191
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
192 #ifdef ADVANCE
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
193 volatile long initial_advance = block->advance*entry_factor*entry_factor;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
194 volatile long final_advance = block->advance*exit_factor*exit_factor;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
195 #endif // ADVANCE
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
196
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
197 // block->accelerate_until = accelerate_steps;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
198 // block->decelerate_after = accelerate_steps+plateau_steps;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
199 CRITICAL_SECTION_START; // Fill variables used by the stepper in a critical section
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
200 if(block->busy == false) { // Don't update variables if block is busy.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
201 block->accelerate_until = accelerate_steps;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
202 block->decelerate_after = accelerate_steps+plateau_steps;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
203 block->initial_rate = initial_rate;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
204 block->final_rate = final_rate;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
205 #ifdef ADVANCE
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
206 block->initial_advance = initial_advance;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
207 block->final_advance = final_advance;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
208 #endif //ADVANCE
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
209 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
210 CRITICAL_SECTION_END;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
211 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
212
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
213 // Calculates the maximum allowable speed at this point when you must be able to reach target_velocity using the
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
214 // acceleration within the allotted distance.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
215 FORCE_INLINE float max_allowable_speed(float acceleration, float target_velocity, float distance) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
216 return sqrt(target_velocity*target_velocity-2*acceleration*distance);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
217 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
218
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
219 // "Junction jerk" in this context is the immediate change in speed at the junction of two blocks.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
220 // This method will calculate the junction jerk as the euclidean distance between the nominal
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
221 // velocities of the respective blocks.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
222 //inline float junction_jerk(block_t *before, block_t *after) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
223 // return sqrt(
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
224 // pow((before->speed_x-after->speed_x), 2)+pow((before->speed_y-after->speed_y), 2));
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
225 //}
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
226
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
227
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
228 // The kernel called by planner_recalculate() when scanning the plan from last to first entry.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
229 void planner_reverse_pass_kernel(block_t *previous, block_t *current, block_t *next) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
230 if(!current) { return; }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
231
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
232 if (next) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
233 // If entry speed is already at the maximum entry speed, no need to recheck. Block is cruising.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
234 // If not, block in state of acceleration or deceleration. Reset entry speed to maximum and
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
235 // check for maximum allowable speed reductions to ensure maximum possible planned speed.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
236 if (current->entry_speed != current->max_entry_speed) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
237
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
238 // If nominal length true, max junction speed is guaranteed to be reached. Only compute
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
239 // for max allowable speed if block is decelerating and nominal length is false.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
240 if ((!current->nominal_length_flag) && (current->max_entry_speed > next->entry_speed)) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
241 current->entry_speed = min( current->max_entry_speed,
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
242 max_allowable_speed(-current->acceleration,next->entry_speed,current->millimeters));
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
243 } else {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
244 current->entry_speed = current->max_entry_speed;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
245 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
246 current->recalculate_flag = true;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
247
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
248 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
249 } // Skip last block. Already initialized and set for recalculation.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
250 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
251
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
252 // planner_recalculate() needs to go over the current plan twice. Once in reverse and once forward. This
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
253 // implements the reverse pass.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
254 void planner_reverse_pass() {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
255 uint8_t block_index = block_buffer_head;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
256 if(((block_buffer_head-block_buffer_tail + BLOCK_BUFFER_SIZE) & (BLOCK_BUFFER_SIZE - 1)) > 3) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
257 block_index = (block_buffer_head - 3) & (BLOCK_BUFFER_SIZE - 1);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
258 block_t *block[3] = { NULL, NULL, NULL };
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
259 while(block_index != block_buffer_tail) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
260 block_index = prev_block_index(block_index);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
261 block[2]= block[1];
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
262 block[1]= block[0];
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
263 block[0] = &block_buffer[block_index];
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
264 planner_reverse_pass_kernel(block[0], block[1], block[2]);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
265 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
266 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
267 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
268
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
269 // The kernel called by planner_recalculate() when scanning the plan from first to last entry.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
270 void planner_forward_pass_kernel(block_t *previous, block_t *current, block_t *next) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
271 if(!previous) { return; }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
272
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
273 // If the previous block is an acceleration block, but it is not long enough to complete the
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
274 // full speed change within the block, we need to adjust the entry speed accordingly. Entry
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
275 // speeds have already been reset, maximized, and reverse planned by reverse planner.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
276 // If nominal length is true, max junction speed is guaranteed to be reached. No need to recheck.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
277 if (!previous->nominal_length_flag) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
278 if (previous->entry_speed < current->entry_speed) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
279 double entry_speed = min( current->entry_speed,
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
280 max_allowable_speed(-previous->acceleration,previous->entry_speed,previous->millimeters) );
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
281
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
282 // Check for junction speed change
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
283 if (current->entry_speed != entry_speed) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
284 current->entry_speed = entry_speed;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
285 current->recalculate_flag = true;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
286 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
287 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
288 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
289 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
290
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
291 // planner_recalculate() needs to go over the current plan twice. Once in reverse and once forward. This
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
292 // implements the forward pass.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
293 void planner_forward_pass() {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
294 uint8_t block_index = block_buffer_tail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
295 block_t *block[3] = { NULL, NULL, NULL };
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
296
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
297 while(block_index != block_buffer_head) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
298 block[0] = block[1];
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
299 block[1] = block[2];
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
300 block[2] = &block_buffer[block_index];
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
301 planner_forward_pass_kernel(block[0],block[1],block[2]);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
302 block_index = next_block_index(block_index);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
303 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
304 planner_forward_pass_kernel(block[1], block[2], NULL);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
305 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
306
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
307 // Recalculates the trapezoid speed profiles for all blocks in the plan according to the
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
308 // entry_factor for each junction. Must be called by planner_recalculate() after
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
309 // updating the blocks.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
310 void planner_recalculate_trapezoids() {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
311 int8_t block_index = block_buffer_tail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
312 block_t *current;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
313 block_t *next = NULL;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
314
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
315 while(block_index != block_buffer_head) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
316 current = next;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
317 next = &block_buffer[block_index];
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
318 if (current) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
319 // Recalculate if current block entry or exit junction speed has changed.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
320 if (current->recalculate_flag || next->recalculate_flag) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
321 // NOTE: Entry and exit factors always > 0 by all previous logic operations.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
322 calculate_trapezoid_for_block(current, current->entry_speed/current->nominal_speed,
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
323 next->entry_speed/current->nominal_speed);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
324 current->recalculate_flag = false; // Reset current only to ensure next trapezoid is computed
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
325 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
326 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
327 block_index = next_block_index( block_index );
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
328 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
329 // Last/newest block in buffer. Exit speed is set with MINIMUM_PLANNER_SPEED. Always recalculated.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
330 if(next != NULL) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
331 calculate_trapezoid_for_block(next, next->entry_speed/next->nominal_speed,
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
332 MINIMUM_PLANNER_SPEED/next->nominal_speed);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
333 next->recalculate_flag = false;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
334 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
335 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
336
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
337 // Recalculates the motion plan according to the following algorithm:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
338 //
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
339 // 1. Go over every block in reverse order and calculate a junction speed reduction (i.e. block_t.entry_factor)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
340 // so that:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
341 // a. The junction jerk is within the set limit
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
342 // b. No speed reduction within one block requires faster deceleration than the one, true constant
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
343 // acceleration.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
344 // 2. Go over every block in chronological order and dial down junction speed reduction values if
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
345 // a. The speed increase within one block would require faster accelleration than the one, true
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
346 // constant acceleration.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
347 //
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
348 // When these stages are complete all blocks have an entry_factor that will allow all speed changes to
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
349 // be performed using only the one, true constant acceleration, and where no junction jerk is jerkier than
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
350 // the set limit. Finally it will:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
351 //
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
352 // 3. Recalculate trapezoids for all blocks.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
353
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
354 void planner_recalculate() {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
355 planner_reverse_pass();
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
356 planner_forward_pass();
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
357 planner_recalculate_trapezoids();
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
358 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
359
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
360 void plan_init() {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
361 block_buffer_head = 0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
362 block_buffer_tail = 0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
363 memset(position, 0, sizeof(position)); // clear position
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
364 previous_speed[0] = 0.0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
365 previous_speed[1] = 0.0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
366 previous_speed[2] = 0.0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
367 previous_speed[3] = 0.0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
368 previous_nominal_speed = 0.0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
369 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
370
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
371
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
372
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
373
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
374 #ifdef AUTOTEMP
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
375 void getHighESpeed()
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
376 {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
377 static float oldt=0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
378 if(!autotemp_enabled){
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
379 return;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
380 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
381 if(degTargetHotend0()+2<autotemp_min) { //probably temperature set to zero.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
382 return; //do nothing
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
383 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
384
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
385 float high=0.0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
386 uint8_t block_index = block_buffer_tail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
387
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
388 while(block_index != block_buffer_head) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
389 if((block_buffer[block_index].steps_x != 0) ||
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
390 (block_buffer[block_index].steps_y != 0) ||
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
391 (block_buffer[block_index].steps_z != 0)) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
392 float se=(float(block_buffer[block_index].steps_e)/float(block_buffer[block_index].step_event_count))*block_buffer[block_index].nominal_speed;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
393 //se; mm/sec;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
394 if(se>high)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
395 {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
396 high=se;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
397 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
398 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
399 block_index = (block_index+1) & (BLOCK_BUFFER_SIZE - 1);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
400 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
401
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
402 float g=autotemp_min+high*autotemp_factor;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
403 float t=g;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
404 if(t<autotemp_min)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
405 t=autotemp_min;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
406 if(t>autotemp_max)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
407 t=autotemp_max;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
408 if(oldt>t)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
409 {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
410 t=AUTOTEMP_OLDWEIGHT*oldt+(1-AUTOTEMP_OLDWEIGHT)*t;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
411 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
412 oldt=t;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
413 setTargetHotend0(t);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
414 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
415 #endif
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
416
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
417 void check_axes_activity() {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
418 unsigned char x_active = 0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
419 unsigned char y_active = 0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
420 unsigned char z_active = 0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
421 unsigned char e_active = 0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
422 unsigned char fan_speed = 0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
423 unsigned char tail_fan_speed = 0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
424 block_t *block;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
425
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
426 if(block_buffer_tail != block_buffer_head) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
427 uint8_t block_index = block_buffer_tail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
428 tail_fan_speed = block_buffer[block_index].fan_speed;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
429 while(block_index != block_buffer_head) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
430 block = &block_buffer[block_index];
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
431 if(block->steps_x != 0) x_active++;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
432 if(block->steps_y != 0) y_active++;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
433 if(block->steps_z != 0) z_active++;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
434 if(block->steps_e != 0) e_active++;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
435 if(block->fan_speed != 0) fan_speed++;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
436 block_index = (block_index+1) & (BLOCK_BUFFER_SIZE - 1);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
437 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
438 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
439 else {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
440 #if FAN_PIN > -1
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
441 if (FanSpeed != 0) analogWrite(FAN_PIN,FanSpeed); // If buffer is empty use current fan speed
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
442 #endif
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
443 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
444 if((DISABLE_X) && (x_active == 0)) disable_x();
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
445 if((DISABLE_Y) && (y_active == 0)) disable_y();
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
446 if((DISABLE_Z) && (z_active == 0)) disable_z();
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
447 if((DISABLE_E) && (e_active == 0)) { disable_e0();disable_e1();disable_e2(); }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
448 #if FAN_PIN > -1
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
449 if((FanSpeed == 0) && (fan_speed ==0)) analogWrite(FAN_PIN, 0);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
450 #endif
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
451 if (FanSpeed != 0 && tail_fan_speed !=0) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
452 analogWrite(FAN_PIN,tail_fan_speed);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
453 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
454 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
455
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
456
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
457 float junction_deviation = 0.1;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
458 // Add a new linear movement to the buffer. steps_x, _y and _z is the absolute position in
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
459 // mm. Microseconds specify how many microseconds the move should take to perform. To aid acceleration
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
460 // calculation the caller must also provide the physical length of the line in millimeters.
1
b584642d4f58 several modifications to support laser enable - still needs cleanup
mbayer
parents: 0
diff changeset
461 void plan_buffer_line(const float &x, const float &y, const float &z, const float &e, float feed_rate, const uint8_t &extruder, bool laser_on)
0
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
462 {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
463 // Calculate the buffer head after we push this byte
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
464 int next_buffer_head = next_block_index(block_buffer_head);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
465
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
466 // If the buffer is full: good! That means we are well ahead of the robot.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
467 // Rest here until there is room in the buffer.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
468 while(block_buffer_tail == next_buffer_head) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
469 manage_heater();
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
470 manage_inactivity(1);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
471 LCD_STATUS;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
472 LED_STATUS;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
473 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
474
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
475 // The target position of the tool in absolute steps
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
476 // Calculate target position in absolute steps
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
477 //this should be done after the wait, because otherwise a M92 code within the gcode disrupts this calculation somehow
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
478 long target[4];
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
479 target[X_AXIS] = lround(x*axis_steps_per_unit[X_AXIS]);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
480 target[Y_AXIS] = lround(y*axis_steps_per_unit[Y_AXIS]);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
481 target[Z_AXIS] = lround(z*axis_steps_per_unit[Z_AXIS]);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
482 target[E_AXIS] = lround(e*axis_steps_per_unit[E_AXIS]);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
483
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
484 #ifdef PREVENT_DANGEROUS_EXTRUDE
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
485 if(target[E_AXIS]!=position[E_AXIS])
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
486 if(degHotend(active_extruder)<EXTRUDE_MINTEMP && !allow_cold_extrude)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
487 {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
488 position[E_AXIS]=target[E_AXIS]; //behave as if the move really took place, but ignore E part
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
489 SERIAL_ECHO_START;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
490 SERIAL_ECHOLNPGM(MSG_ERR_COLD_EXTRUDE_STOP);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
491 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
492 if(labs(target[E_AXIS]-position[E_AXIS])>axis_steps_per_unit[E_AXIS]*EXTRUDE_MAXLENGTH)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
493 {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
494 position[E_AXIS]=target[E_AXIS]; //behave as if the move really took place, but ignore E part
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
495 SERIAL_ECHO_START;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
496 SERIAL_ECHOLNPGM(MSG_ERR_LONG_EXTRUDE_STOP);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
497 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
498 #endif
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
499
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
500 // Prepare to set up new block
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
501 block_t *block = &block_buffer[block_buffer_head];
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
502
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
503 // Mark block as not busy (Not executed by the stepper interrupt)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
504 block->busy = false;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
505
1
b584642d4f58 several modifications to support laser enable - still needs cleanup
mbayer
parents: 0
diff changeset
506 // set the laser output status
b584642d4f58 several modifications to support laser enable - still needs cleanup
mbayer
parents: 0
diff changeset
507 block->laser_on = laser_on;
b584642d4f58 several modifications to support laser enable - still needs cleanup
mbayer
parents: 0
diff changeset
508
0
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
509 // Number of steps for each axis
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
510 block->steps_x = labs(target[X_AXIS]-position[X_AXIS]);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
511 block->steps_y = labs(target[Y_AXIS]-position[Y_AXIS]);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
512 block->steps_z = labs(target[Z_AXIS]-position[Z_AXIS]);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
513 block->steps_e = labs(target[E_AXIS]-position[E_AXIS]);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
514 block->steps_e *= extrudemultiply;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
515 block->steps_e /= 100;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
516 block->step_event_count = max(block->steps_x, max(block->steps_y, max(block->steps_z, block->steps_e)));
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
517
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
518 // Bail if this is a zero-length block
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
519 if (block->step_event_count <= dropsegments) { return; };
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
520
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
521 block->fan_speed = FanSpeed;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
522
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
523 // Compute direction bits for this block
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
524 block->direction_bits = 0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
525 if (target[X_AXIS] < position[X_AXIS]) { block->direction_bits |= (1<<X_AXIS); }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
526 if (target[Y_AXIS] < position[Y_AXIS]) { block->direction_bits |= (1<<Y_AXIS); }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
527 if (target[Z_AXIS] < position[Z_AXIS]) { block->direction_bits |= (1<<Z_AXIS); }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
528 if (target[E_AXIS] < position[E_AXIS]) { block->direction_bits |= (1<<E_AXIS); }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
529
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
530 block->active_extruder = extruder;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
531
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
532 //enable active axes
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
533 if(block->steps_x != 0) enable_x();
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
534 if(block->steps_y != 0) enable_y();
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
535 #ifndef Z_LATE_ENABLE
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
536 if(block->steps_z != 0) enable_z();
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
537 #endif
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
538
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
539 // Enable all
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
540 // N571 disables real E drive! (ie. on laser operations)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
541 if (!n571_enabled) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
542 if(block->steps_e != 0) { enable_e0();enable_e1();enable_e2(); }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
543 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
544
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
545 if (block->steps_e == 0) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
546 if(feed_rate<mintravelfeedrate) feed_rate=mintravelfeedrate;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
547 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
548 else {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
549 if(feed_rate<minimumfeedrate) feed_rate=minimumfeedrate;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
550 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
551
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
552 float delta_mm[4];
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
553 delta_mm[X_AXIS] = (target[X_AXIS]-position[X_AXIS])/axis_steps_per_unit[X_AXIS];
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
554 delta_mm[Y_AXIS] = (target[Y_AXIS]-position[Y_AXIS])/axis_steps_per_unit[Y_AXIS];
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
555 delta_mm[Z_AXIS] = (target[Z_AXIS]-position[Z_AXIS])/axis_steps_per_unit[Z_AXIS];
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
556 delta_mm[E_AXIS] = ((target[E_AXIS]-position[E_AXIS])/axis_steps_per_unit[E_AXIS])*extrudemultiply/100.0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
557 // if ( block->steps_x <=dropsegments && block->steps_y <=dropsegments && block->steps_z <=dropsegments ) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
558 // block->millimeters = abs(delta_mm[E_AXIS]);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
559 // } else {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
560 // block->millimeters = sqrt(square(delta_mm[X_AXIS]) + square(delta_mm[Y_AXIS]) + square(delta_mm[Z_AXIS]));
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
561 // }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
562
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
563 // TODO - JMG - SORT OUT RETRACTS WHEN e IS NOT ALONE
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
564 block->millimeters = sqrt(square(delta_mm[X_AXIS]) + square(delta_mm[Y_AXIS]) +
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
565 square(delta_mm[Z_AXIS]) + square(delta_mm[E_AXIS]));
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
566 float inverse_millimeters = 1.0/block->millimeters; // Inverse millimeters to remove multiple divides
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
567
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
568 // Calculate speed in mm/second for each axis. No divide by zero due to previous checks.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
569 float inverse_second = feed_rate * inverse_millimeters;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
570
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
571 int moves_queued=(block_buffer_head-block_buffer_tail + BLOCK_BUFFER_SIZE) & (BLOCK_BUFFER_SIZE - 1);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
572
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
573 // slow down when de buffer starts to empty, rather than wait at the corner for a buffer refill
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
574 #ifdef OLD_SLOWDOWN
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
575 if(moves_queued < (BLOCK_BUFFER_SIZE * 0.5) && moves_queued > 1) feed_rate = feed_rate*moves_queued / (BLOCK_BUFFER_SIZE * 0.5);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
576 #endif
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
577
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
578 #ifdef SLOWDOWN
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
579 // segment time im micro seconds
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
580 unsigned long segment_time = lround(1000000.0/inverse_second);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
581 if ((moves_queued > 1) && (moves_queued < (BLOCK_BUFFER_SIZE * 0.5))) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
582 if (segment_time < minsegmenttime) { // buffer is draining, add extra time. The amount of time added increases if the buffer is still emptied more.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
583 inverse_second=1000000.0/(segment_time+lround(2*(minsegmenttime-segment_time)/moves_queued));
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
584 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
585 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
586 #endif
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
587 // END OF SLOW DOWN SECTION
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
588
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
589
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
590 block->nominal_speed = block->millimeters * inverse_second; // (mm/sec) Always > 0
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
591 block->nominal_rate = ceil(block->step_event_count * inverse_second); // (step/sec) Always > 0
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
592
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
593 // Calculate and limit speed in mm/sec for each axis
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
594 float current_speed[4];
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
595 float speed_factor = 1.0; //factor <=1 do decrease speed
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
596 for(int i=0; i < 4; i++) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
597 current_speed[i] = delta_mm[i] * inverse_second;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
598 if(fabs(current_speed[i]) > max_feedrate[i])
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
599 speed_factor = min(speed_factor, max_feedrate[i] / fabs(current_speed[i]));
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
600 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
601
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
602 // Max segement time in us.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
603 #ifdef XY_FREQUENCY_LIMIT
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
604 #define MAX_FREQ_TIME (1000000.0/XY_FREQUENCY_LIMIT)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
605
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
606 // Check and limit the xy direction change frequency
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
607 unsigned char direction_change = block->direction_bits ^ old_direction_bits;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
608 old_direction_bits = block->direction_bits;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
609
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
610 if((direction_change & (1<<X_AXIS)) == 0) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
611 x_segment_time[0] += segment_time;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
612 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
613 else {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
614 x_segment_time[2] = x_segment_time[1];
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
615 x_segment_time[1] = x_segment_time[0];
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
616 x_segment_time[0] = segment_time;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
617 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
618 if((direction_change & (1<<Y_AXIS)) == 0) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
619 y_segment_time[0] += segment_time;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
620 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
621 else {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
622 y_segment_time[2] = y_segment_time[1];
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
623 y_segment_time[1] = y_segment_time[0];
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
624 y_segment_time[0] = segment_time;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
625 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
626 long max_x_segment_time = max(x_segment_time[0], max(x_segment_time[1], x_segment_time[2]));
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
627 long max_y_segment_time = max(y_segment_time[0], max(y_segment_time[1], y_segment_time[2]));
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
628 long min_xy_segment_time =min(max_x_segment_time, max_y_segment_time);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
629 if(min_xy_segment_time < MAX_FREQ_TIME) speed_factor = min(speed_factor, speed_factor * (float)min_xy_segment_time / (float)MAX_FREQ_TIME);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
630 #endif
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
631
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
632 // Correct the speed
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
633 if( speed_factor < 1.0) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
634 for(unsigned char i=0; i < 4; i++) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
635 current_speed[i] *= speed_factor;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
636 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
637 block->nominal_speed *= speed_factor;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
638 block->nominal_rate *= speed_factor;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
639 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
640
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
641 // Compute and limit the acceleration rate for the trapezoid generator.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
642 float steps_per_mm = block->step_event_count/block->millimeters;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
643 if(block->steps_x == 0 && block->steps_y == 0 && block->steps_z == 0) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
644 block->acceleration_st = ceil(retract_acceleration * steps_per_mm); // convert to: acceleration steps/sec^2
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
645 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
646 else {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
647 block->acceleration_st = ceil(acceleration * steps_per_mm); // convert to: acceleration steps/sec^2
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
648 // Limit acceleration per axis
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
649 if(((float)block->acceleration_st * (float)block->steps_x / (float)block->step_event_count) > axis_steps_per_sqr_second[X_AXIS])
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
650 block->acceleration_st = axis_steps_per_sqr_second[X_AXIS];
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
651 if(((float)block->acceleration_st * (float)block->steps_y / (float)block->step_event_count) > axis_steps_per_sqr_second[Y_AXIS])
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
652 block->acceleration_st = axis_steps_per_sqr_second[Y_AXIS];
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
653 if(((float)block->acceleration_st * (float)block->steps_e / (float)block->step_event_count) > axis_steps_per_sqr_second[E_AXIS])
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
654 block->acceleration_st = axis_steps_per_sqr_second[E_AXIS];
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
655 if(((float)block->acceleration_st * (float)block->steps_z / (float)block->step_event_count ) > axis_steps_per_sqr_second[Z_AXIS])
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
656 block->acceleration_st = axis_steps_per_sqr_second[Z_AXIS];
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
657 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
658 block->acceleration = block->acceleration_st / steps_per_mm;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
659 block->acceleration_rate = (long)((float)block->acceleration_st * 8.388608);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
660
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
661 #if 0 // Use old jerk for now
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
662 // Compute path unit vector
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
663 double unit_vec[3];
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
664
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
665 unit_vec[X_AXIS] = delta_mm[X_AXIS]*inverse_millimeters;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
666 unit_vec[Y_AXIS] = delta_mm[Y_AXIS]*inverse_millimeters;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
667 unit_vec[Z_AXIS] = delta_mm[Z_AXIS]*inverse_millimeters;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
668
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
669 // Compute maximum allowable entry speed at junction by centripetal acceleration approximation.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
670 // Let a circle be tangent to both previous and current path line segments, where the junction
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
671 // deviation is defined as the distance from the junction to the closest edge of the circle,
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
672 // colinear with the circle center. The circular segment joining the two paths represents the
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
673 // path of centripetal acceleration. Solve for max velocity based on max acceleration about the
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
674 // radius of the circle, defined indirectly by junction deviation. This may be also viewed as
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
675 // path width or max_jerk in the previous grbl version. This approach does not actually deviate
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
676 // from path, but used as a robust way to compute cornering speeds, as it takes into account the
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
677 // nonlinearities of both the junction angle and junction velocity.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
678 double vmax_junction = MINIMUM_PLANNER_SPEED; // Set default max junction speed
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
679
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
680 // Skip first block or when previous_nominal_speed is used as a flag for homing and offset cycles.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
681 if ((block_buffer_head != block_buffer_tail) && (previous_nominal_speed > 0.0)) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
682 // Compute cosine of angle between previous and current path. (prev_unit_vec is negative)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
683 // NOTE: Max junction velocity is computed without sin() or acos() by trig half angle identity.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
684 double cos_theta = - previous_unit_vec[X_AXIS] * unit_vec[X_AXIS]
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
685 - previous_unit_vec[Y_AXIS] * unit_vec[Y_AXIS]
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
686 - previous_unit_vec[Z_AXIS] * unit_vec[Z_AXIS] ;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
687
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
688 // Skip and use default max junction speed for 0 degree acute junction.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
689 if (cos_theta < 0.95) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
690 vmax_junction = min(previous_nominal_speed,block->nominal_speed);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
691 // Skip and avoid divide by zero for straight junctions at 180 degrees. Limit to min() of nominal speeds.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
692 if (cos_theta > -0.95) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
693 // Compute maximum junction velocity based on maximum acceleration and junction deviation
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
694 double sin_theta_d2 = sqrt(0.5*(1.0-cos_theta)); // Trig half angle identity. Always positive.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
695 vmax_junction = min(vmax_junction,
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
696 sqrt(block->acceleration * junction_deviation * sin_theta_d2/(1.0-sin_theta_d2)) );
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
697 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
698 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
699 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
700 #endif
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
701 // Start with a safe speed
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
702 float vmax_junction = max_xy_jerk/2;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
703 if(fabs(current_speed[Z_AXIS]) > max_z_jerk/2)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
704 vmax_junction = max_z_jerk/2;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
705 vmax_junction = min(vmax_junction, block->nominal_speed);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
706 if(fabs(current_speed[E_AXIS]) > max_e_jerk/2)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
707 vmax_junction = min(vmax_junction, max_e_jerk/2);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
708
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
709 if ((moves_queued > 1) && (previous_nominal_speed > 0.0001)) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
710 float jerk = sqrt(pow((current_speed[X_AXIS]-previous_speed[X_AXIS]), 2)+pow((current_speed[Y_AXIS]-previous_speed[Y_AXIS]), 2));
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
711 if((fabs(previous_speed[X_AXIS]) > 0.0001) || (fabs(previous_speed[Y_AXIS]) > 0.0001)) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
712 vmax_junction = block->nominal_speed;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
713 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
714 if (jerk > max_xy_jerk) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
715 vmax_junction *= (max_xy_jerk/jerk);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
716 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
717 if(fabs(current_speed[Z_AXIS] - previous_speed[Z_AXIS]) > max_z_jerk) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
718 vmax_junction *= (max_z_jerk/fabs(current_speed[Z_AXIS] - previous_speed[Z_AXIS]));
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
719 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
720 if(fabs(current_speed[E_AXIS] - previous_speed[E_AXIS]) > max_e_jerk) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
721 vmax_junction *= (max_e_jerk/fabs(current_speed[E_AXIS] - previous_speed[E_AXIS]));
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
722 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
723 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
724 block->max_entry_speed = vmax_junction;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
725
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
726 // Initialize block entry speed. Compute based on deceleration to user-defined MINIMUM_PLANNER_SPEED.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
727 double v_allowable = max_allowable_speed(-block->acceleration,MINIMUM_PLANNER_SPEED,block->millimeters);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
728 block->entry_speed = min(vmax_junction, v_allowable);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
729
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
730 // Initialize planner efficiency flags
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
731 // Set flag if block will always reach maximum junction speed regardless of entry/exit speeds.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
732 // If a block can de/ac-celerate from nominal speed to zero within the length of the block, then
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
733 // the current block and next block junction speeds are guaranteed to always be at their maximum
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
734 // junction speeds in deceleration and acceleration, respectively. This is due to how the current
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
735 // block nominal speed limits both the current and next maximum junction speeds. Hence, in both
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
736 // the reverse and forward planners, the corresponding block junction speed will always be at the
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
737 // the maximum junction speed and may always be ignored for any speed reduction checks.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
738 if (block->nominal_speed <= v_allowable) { block->nominal_length_flag = true; }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
739 else { block->nominal_length_flag = false; }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
740 block->recalculate_flag = true; // Always calculate trapezoid for new block
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
741
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
742 // Update previous path unit_vector and nominal speed
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
743 memcpy(previous_speed, current_speed, sizeof(previous_speed)); // previous_speed[] = current_speed[]
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
744 previous_nominal_speed = block->nominal_speed;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
745
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
746
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
747 #ifdef ADVANCE
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
748 // Calculate advance rate
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
749 if((block->steps_e == 0) || (block->steps_x == 0 && block->steps_y == 0 && block->steps_z == 0)) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
750 block->advance_rate = 0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
751 block->advance = 0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
752 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
753 else {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
754 long acc_dist = estimate_acceleration_distance(0, block->nominal_rate, block->acceleration_st);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
755 float advance = (STEPS_PER_CUBIC_MM_E * advance_k) *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
756 (current_speed[E_AXIS] * current_speed[E_AXIS] * EXTRUTION_AREA * EXTRUTION_AREA)*256;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
757 block->advance = advance;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
758 if(acc_dist == 0) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
759 block->advance_rate = 0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
760 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
761 else {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
762 block->advance_rate = advance / (float)acc_dist;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
763 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
764 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
765 /*
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
766 SERIAL_ECHO_START;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
767 SERIAL_ECHOPGM("advance :");
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
768 SERIAL_ECHO(block->advance/256.0);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
769 SERIAL_ECHOPGM("advance rate :");
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
770 SERIAL_ECHOLN(block->advance_rate/256.0);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
771 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
772 #endif // ADVANCE
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
773
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
774 calculate_trapezoid_for_block(block, block->entry_speed/block->nominal_speed,
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
775 MINIMUM_PLANNER_SPEED/block->nominal_speed);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
776
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
777 // Move buffer head
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
778 block_buffer_head = next_buffer_head;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
779
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
780 // Update position
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
781 memcpy(position, target, sizeof(target)); // position[] = target[]
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
782
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
783 planner_recalculate();
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
784
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
785 st_wake_up();
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
786 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
787
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
788 void plan_set_position(const float &x, const float &y, const float &z, const float &e)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
789 {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
790 position[X_AXIS] = lround(x*axis_steps_per_unit[X_AXIS]);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
791 position[Y_AXIS] = lround(y*axis_steps_per_unit[Y_AXIS]);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
792 position[Z_AXIS] = lround(z*axis_steps_per_unit[Z_AXIS]);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
793 position[E_AXIS] = lround(e*axis_steps_per_unit[E_AXIS]);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
794 st_set_position(position[X_AXIS], position[Y_AXIS], position[Z_AXIS], position[E_AXIS]);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
795 previous_nominal_speed = 0.0; // Resets planner junction speeds. Assumes start from rest.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
796 previous_speed[0] = 0.0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
797 previous_speed[1] = 0.0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
798 previous_speed[2] = 0.0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
799 previous_speed[3] = 0.0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
800 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
801
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
802 void plan_set_e_position(const float &e)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
803 {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
804 position[E_AXIS] = lround(e*axis_steps_per_unit[E_AXIS]);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
805 st_set_e_position(position[E_AXIS]);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
806 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
807
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
808 uint8_t movesplanned()
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
809 {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
810 return (block_buffer_head-block_buffer_tail + BLOCK_BUFFER_SIZE) & (BLOCK_BUFFER_SIZE - 1);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
811 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
812
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
813 void allow_cold_extrudes(bool allow)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
814 {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
815 #ifdef PREVENT_DANGEROUS_EXTRUDE
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
816 allow_cold_extrude=allow;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
817 #endif
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
818 }

mercurial