forked from slic3r/Slic3r
/
Extruder.cpp
206 lines (179 loc) · 5.61 KB
/
Extruder.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include "Extruder.hpp"
namespace Slic3r {
Extruder::Extruder(unsigned int id, GCodeConfig *config)
: id(id),
config(config),
time_used(0.0)
{
reset();
// cache values that are going to be called often
if (config->use_volumetric_e) {
this->e_per_mm3 = this->extrusion_multiplier();
} else {
this->e_per_mm3 = this->extrusion_multiplier()
* (4 / ((this->filament_diameter() * this->filament_diameter()) * PI));
}
this->retract_speed_mm_min = this->retract_speed() * 60;
}
void
Extruder::reset()
{
this->E = 0;
this->absolute_E = 0;
this->retracted = 0;
this->restart_extra = 0;
}
double
Extruder::extrude(double dE)
{
// in case of relative E distances we always reset to 0 before any output
if (this->config->use_relative_e_distances)
this->E = 0;
this->E += dE;
this->absolute_E += dE;
return dE;
}
/* This method makes sure the extruder is retracted by the specified amount
of filament and returns the amount of filament retracted.
If the extruder is already retracted by the same or a greater amount,
this method is a no-op.
The restart_extra argument sets the extra length to be used for
unretraction. If we're actually performing a retraction, any restart_extra
value supplied will overwrite the previous one if any. */
double
Extruder::retract(double length, double restart_extra)
{
// in case of relative E distances we always reset to 0 before any output
if (this->config->use_relative_e_distances)
this->E = 0;
double to_retract = length - this->retracted;
if (to_retract > 0) {
this->E -= to_retract;
this->absolute_E -= to_retract;
this->retracted += to_retract;
this->restart_extra = restart_extra;
return to_retract;
} else {
return 0;
}
}
double
Extruder::unretract()
{
double dE = this->retracted + this->restart_extra;
this->extrude(dE);
this->retracted = 0;
this->restart_extra = 0;
return dE;
}
double
Extruder::e_per_mm(double mm3_per_mm) const
{
return mm3_per_mm * this->e_per_mm3;
}
double
Extruder::extruded_volume() const
{
if (this->config->use_volumetric_e) {
// Any current amount of retraction should not affect used filament, since
// it represents empty volume in the nozzle. We add it back to E.
return this->absolute_E + this->retracted;
}
return this->used_filament() * (this->filament_diameter() * this->filament_diameter()) * PI/4;
}
double
Extruder::used_filament() const
{
if (this->config->use_volumetric_e) {
return this->extruded_volume() / (this->filament_diameter() * this->filament_diameter() * PI/4);
}
// Any current amount of retraction should not affect used filament, since
// it represents empty volume in the nozzle. We add it back to E.
return this->absolute_E + this->retracted;
}
double
Extruder::filament_diameter() const
{
return this->config->filament_diameter.get_at(this->id);
}
double
Extruder::filament_density() const
{
return this->config->filament_density.get_at(this->id);
}
double
Extruder::filament_cost() const
{
return this->config->filament_cost.get_at(this->id);
}
double
Extruder::extrusion_multiplier() const
{
return this->config->extrusion_multiplier.get_at(this->id);
}
double
Extruder::retract_length() const
{
return this->config->retract_length.get_at(this->id);
}
double
Extruder::retract_lift() const
{
return this->config->retract_lift.get_at(this->id);
}
int
Extruder::retract_speed() const
{
return this->config->retract_speed.get_at(this->id);
}
double
Extruder::retract_restart_extra() const
{
return this->config->retract_restart_extra.get_at(this->id);
}
double
Extruder::retract_length_toolchange() const
{
return this->config->retract_length_toolchange.get_at(this->id);
}
double
Extruder::retract_restart_extra_toolchange() const
{
return this->config->retract_restart_extra_toolchange.get_at(this->id);
}
// Wildly optimistic acceleration "bell" curve modeling.
// Adds an estimate of how long the move with a given accel
// takes, in seconds, to the extruder's total time count.
// It is assumed that the movement is smooth and uniform.
void
Extruder::add_extrusion_time(double length, double v, double acceleration)
{
// for half of the move, there are 2 zones, where the speed is increasing/decreasing and
// where the speed is constant.
// Since the slowdown is assumed to be uniform, calculate the average velocity for half of the
// expected displacement.
// final velocity v = a*t => a * (dx / 0.5v) => v^2 = 2*a*dx
// v_avg = 0.5v => 2*v_avg = v
// d_x = v_avg*t => t = d_x / v_avg
acceleration = (acceleration == 0.0 ? 4000.0 : acceleration); // Set a default accel to use for print time in case it's 0 somehow.
auto half_length = length / 2.0;
auto t_init = v / acceleration; // time to final velocity
auto dx_init = (0.5*v*t_init); // Initial displacement for the time to get to final velocity
auto t = 0.0;
if (half_length >= dx_init) {
half_length -= (0.5*v*t_init);
t += t_init;
t += (half_length / v); // rest of time is at constant speed.
} else {
// If too much displacement for the expected final velocity, we don't hit the max, so reduce
// the average velocity to fit the displacement we actually are looking for.
t += std::sqrt(std::abs(length) * 2.0 * acceleration) / acceleration;
}
this->time_used += 2.0*t; // cut in half before, so double to get full time spent.
}
double
Extruder::extrusion_time() const
{
return this->time_used;
}
}