Extruder.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #include "Extruder.hpp"
  2. #include "PrintConfig.hpp"
  3. namespace Slic3r {
  4. Tool::Tool(uint16_t id, GCodeConfig* config) :
  5. m_id(id),
  6. m_config(config)
  7. {
  8. reset();
  9. }
  10. Extruder::Extruder(uint16_t id, GCodeConfig* config) :
  11. Tool(id, config)
  12. {
  13. // set extra_toolchange to be init for when it will be new current extruder
  14. m_restart_extra_toolchange = retract_restart_extra_toolchange();
  15. // cache values that are going to be called often
  16. m_e_per_mm3 = this->extrusion_multiplier();
  17. if (!m_config->use_volumetric_e)
  18. m_e_per_mm3 /= this->filament_crossection();
  19. }
  20. Mill::Mill(uint16_t mill_id, GCodeConfig* config) :
  21. Tool(mill_id, config)
  22. {
  23. m_mill_id = mill_id;
  24. m_id = mill_id + (uint16_t)config->retract_length.values.size();
  25. }
  26. double Tool::extrude(double dE)
  27. {
  28. // in case of relative E distances we always reset to 0 before any output
  29. if (m_config->use_relative_e_distances)
  30. m_E = 0.;
  31. m_E += dE;
  32. m_absolute_E += dE;
  33. if (dE < 0.)
  34. m_retracted -= dE;
  35. return dE;
  36. }
  37. /* This method makes sure the extruder is retracted by the specified amount
  38. of filament and returns the amount of filament retracted.
  39. If the extruder is already retracted by the same or a greater amount,
  40. this method is a no-op.
  41. The restart_extra argument sets the extra length to be used for
  42. unretraction. If we're actually performing a retraction, any restart_extra
  43. value supplied will overwrite the previous one if any. */
  44. double Tool::retract(double length, double restart_extra, double restart_extra_toolchange)
  45. {
  46. // in case of relative E distances we always reset to 0 before any output
  47. if (m_config->use_relative_e_distances)
  48. m_E = 0.;
  49. double to_retract = std::max(0., length - m_retracted);
  50. if (to_retract > 0.) {
  51. m_E -= to_retract;
  52. m_absolute_E -= to_retract;
  53. m_retracted += to_retract;
  54. if(!std::isnan(restart_extra))
  55. m_restart_extra = restart_extra;
  56. }
  57. if (!std::isnan(restart_extra_toolchange))
  58. m_restart_extra_toolchange = restart_extra_toolchange;
  59. return to_retract;
  60. }
  61. double Tool::unretract()
  62. {
  63. double dE = m_retracted + m_restart_extra + m_restart_extra_toolchange;
  64. this->extrude(dE);
  65. m_retracted = 0.;
  66. m_restart_extra = 0.;
  67. if(m_restart_extra_toolchange != 0)
  68. m_restart_extra_toolchange = 0.;
  69. return dE;
  70. }
  71. // Called after a M600 or somethgin like that, so you don't have to unretract, but the absolute position won't change.
  72. void Tool::reset_retract() {
  73. m_retracted = 0.;
  74. m_restart_extra = 0.;
  75. if (m_restart_extra_toolchange != 0)
  76. m_restart_extra_toolchange = 0.;
  77. }
  78. // Used filament volume in mm^3.
  79. double Tool::extruded_volume() const
  80. {
  81. return m_config->use_volumetric_e ?
  82. m_absolute_E + m_retracted :
  83. this->used_filament() * this->filament_crossection();
  84. }
  85. // Used filament length in mm.
  86. double Tool::used_filament() const
  87. {
  88. return m_config->use_volumetric_e ?
  89. this->extruded_volume() / this->filament_crossection() :
  90. m_absolute_E + m_retracted;
  91. }
  92. double Tool::filament_diameter() const
  93. {
  94. return 0;
  95. }
  96. double Tool::filament_density() const
  97. {
  98. return 0;
  99. }
  100. double Tool::filament_cost() const
  101. {
  102. return 0;
  103. }
  104. double Tool::extrusion_multiplier() const
  105. {
  106. return 0;
  107. }
  108. // Return a "retract_before_wipe" percentage as a factor clamped to <0, 1>
  109. double Tool::retract_before_wipe() const
  110. {
  111. return 0;
  112. }
  113. double Tool::retract_length() const
  114. {
  115. return 0;
  116. }
  117. double Tool::retract_lift() const
  118. {
  119. return 0;
  120. }
  121. int Tool::retract_speed() const
  122. {
  123. return 0;
  124. }
  125. int Tool::deretract_speed() const
  126. {
  127. return 0;
  128. }
  129. double Tool::retract_restart_extra() const
  130. {
  131. return 0;
  132. }
  133. double Tool::retract_length_toolchange() const
  134. {
  135. return 0;
  136. }
  137. double Tool::retract_restart_extra_toolchange() const
  138. {
  139. return 0;
  140. }
  141. int16_t Tool::temp_offset() const
  142. {
  143. return 0;
  144. }
  145. int8_t Tool::fan_offset() const
  146. {
  147. return 0;
  148. }
  149. double Extruder::filament_diameter() const
  150. {
  151. return m_config->filament_diameter.get_at(m_id);
  152. }
  153. double Extruder::filament_density() const
  154. {
  155. return m_config->filament_density.get_at(m_id);
  156. }
  157. double Extruder::filament_cost() const
  158. {
  159. return m_config->filament_cost.get_at(m_id);
  160. }
  161. double Extruder::extrusion_multiplier() const
  162. {
  163. return m_config->extrusion_multiplier.get_at(m_id);
  164. }
  165. // Return a "retract_before_wipe" percentage as a factor clamped to <0, 1>
  166. double Extruder::retract_before_wipe() const
  167. {
  168. return std::min(1., std::max(0., m_config->retract_before_wipe.get_at(m_id) * 0.01));
  169. }
  170. double Extruder::retract_length() const
  171. {
  172. return m_config->retract_length.get_at(m_id);
  173. }
  174. double Extruder::retract_lift() const
  175. {
  176. return m_config->retract_lift.get_at(m_id);
  177. }
  178. int Extruder::retract_speed() const
  179. {
  180. return int(floor(m_config->retract_speed.get_at(m_id)+0.5));
  181. }
  182. int Extruder::deretract_speed() const
  183. {
  184. int speed = int(floor(m_config->deretract_speed.get_at(m_id)+0.5));
  185. return (speed > 0) ? speed : this->retract_speed();
  186. }
  187. double Extruder::retract_restart_extra() const
  188. {
  189. return m_config->retract_restart_extra.get_at(m_id);
  190. }
  191. double Extruder::retract_length_toolchange() const
  192. {
  193. return m_config->retract_length_toolchange.get_at(m_id);
  194. }
  195. double Extruder::retract_restart_extra_toolchange() const
  196. {
  197. return m_config->retract_restart_extra_toolchange.get_at(m_id);
  198. }
  199. int16_t Extruder::temp_offset() const
  200. {
  201. return int16_t(m_config->extruder_temperature_offset.get_at(m_id));
  202. }
  203. int8_t Extruder::fan_offset() const
  204. {
  205. return int8_t(m_config->extruder_fan_offset.get_at(m_id));
  206. }
  207. double Mill::retract_lift() const {
  208. return m_config->milling_z_lift.get_at(m_mill_id);
  209. }
  210. }