test_gcodewriter.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #include <catch2/catch.hpp>
  2. #include <memory>
  3. #include "libslic3r/GCode/GCodeWriter.hpp"
  4. using namespace Slic3r;
  5. //note: erased by prusa, maybe it's wrong now?
  6. SCENARIO("lift() is not ignored after unlift() at normal values of Z", "[GCodeWriter]") {
  7. GIVEN("A config from a file and a single extruder.") {
  8. GCodeWriter writer;
  9. GCodeConfig &config = writer.config;
  10. config.load(std::string(TEST_DATA_DIR) + "/fff_print_tests/test_gcodewriter/config_lift_unlift.ini", ForwardCompatibilitySubstitutionRule::Disable);
  11. std::vector<uint16_t> extruder_ids {0};
  12. writer.set_extruders(extruder_ids);
  13. writer.set_tool(0);
  14. WHEN("Z is set to 203") {
  15. double trouble_Z = 203;
  16. writer.travel_to_z(trouble_Z);
  17. AND_WHEN("GcodeWriter::Lift() is called") {
  18. REQUIRE(writer.lift().size() > 0);
  19. AND_WHEN("Z is moved post-lift to the same delta as the config Z lift") {
  20. REQUIRE(writer.travel_to_z(trouble_Z + config.retract_lift.values[0]).size() == 0);
  21. AND_WHEN("GCodeWriter::Unlift() is called") {
  22. REQUIRE(writer.unlift().size() == 0); // we're the same height so no additional move happens.
  23. THEN("GCodeWriter::Lift() emits gcode.") {
  24. REQUIRE(writer.lift().size() > 0);
  25. }
  26. }
  27. }
  28. }
  29. }
  30. WHEN("Z is set to 500003") {
  31. double trouble_Z = 500003;
  32. writer.travel_to_z(trouble_Z);
  33. AND_WHEN("GcodeWriter::Lift() is called") {
  34. REQUIRE(writer.lift().size() > 0);
  35. AND_WHEN("Z is moved post-lift to the same delta as the config Z lift") {
  36. REQUIRE(writer.travel_to_z(trouble_Z + config.retract_lift.values[0]).size() == 0);
  37. AND_WHEN("GCodeWriter::Unlift() is called") {
  38. REQUIRE(writer.unlift().size() == 0); // we're the same height so no additional move happens.
  39. THEN("GCodeWriter::Lift() emits gcode.") {
  40. REQUIRE(writer.lift().size() > 0);
  41. }
  42. }
  43. }
  44. }
  45. }
  46. WHEN("Z is set to 10.3") {
  47. double trouble_Z = 10.3;
  48. writer.travel_to_z(trouble_Z);
  49. AND_WHEN("GcodeWriter::Lift() is called") {
  50. REQUIRE(writer.lift().size() > 0);
  51. AND_WHEN("Z is moved post-lift to the same delta as the config Z lift") {
  52. REQUIRE(writer.travel_to_z(trouble_Z + config.retract_lift.values[0]).size() == 0);
  53. AND_WHEN("GCodeWriter::Unlift() is called") {
  54. REQUIRE(writer.unlift().size() == 0); // we're the same height so no additional move happens.
  55. THEN("GCodeWriter::Lift() emits gcode.") {
  56. REQUIRE(writer.lift().size() > 0);
  57. }
  58. }
  59. }
  60. }
  61. }
  62. // The test above will fail for trouble_Z == 9007199254740992, where trouble_Z + 1.5 will be rounded to trouble_Z + 2.0 due to double mantisa overflow.
  63. }
  64. }
  65. SCENARIO("set_speed emits values with floating-point output, 8 significant digits.", "[GCodeWriter]") {
  66. GIVEN("GCodeWriter instance") {
  67. GCodeWriter writer;
  68. WHEN("set_speed is called to set speed to 12345.678") {
  69. THEN("Output string is G1 12345.678") {
  70. REQUIRE_THAT(writer.set_speed(12345.678), Catch::Equals("G1 F12345.678\n"));
  71. }
  72. }
  73. WHEN("set_speed is called to set speed to 1") {
  74. THEN("Output string is G1 F1") {
  75. REQUIRE_THAT(writer.set_speed(1.0), Catch::Equals("G1 F1\n"));
  76. }
  77. }
  78. WHEN("set_speed is called to set speed to 203.200022") {
  79. THEN("Output string is G1 F203.2") {
  80. REQUIRE_THAT(writer.set_speed(203.200022), Catch::Equals("G1 F203.2\n"));
  81. }
  82. }
  83. WHEN("set_speed is called to set speed to 12345.200522") {
  84. THEN("Output string is G1 F12345.201") {
  85. REQUIRE_THAT(writer.set_speed(12345.200522), Catch::Equals("G1 F12345.201\n"));
  86. }
  87. }
  88. }
  89. }
  90. SCENARIO("set_fan rescales based on fan_percentage.", "[GCode][GCodeWriter]") {
  91. GIVEN("GCodeWriter instance with comments off and RepRap flavor") {
  92. GCodeWriter writer;
  93. writer.config.gcode_comments.value = false;
  94. writer.config.gcode_flavor.value = gcfRepRap;
  95. WHEN("set_fan is called to set speed to 100\% with fan_percentage = true") {
  96. writer.config.fan_percentage.value = true;
  97. THEN("Fan value is set to 100.") {
  98. REQUIRE_THAT(writer.set_fan(100, true), Catch::Equals("M106 S100\n"));
  99. }
  100. AND_WHEN("Fan value is set to 93\%") {
  101. THEN("Output string is 'M106 S93'") {
  102. REQUIRE_THAT(writer.set_fan(93, true), Catch::Equals("M106 S93\n"));
  103. }
  104. }
  105. AND_WHEN("Fan value is set to 21\%") {
  106. THEN("Output string is 'M106 S21'") {
  107. REQUIRE_THAT(writer.set_fan(21, true), Catch::Equals("M106 S21\n"));
  108. }
  109. }
  110. }
  111. WHEN("set_fan is called to set speed to 100\% with fan_percentage = false") {
  112. writer.config.fan_percentage.value = false;
  113. THEN("Output string is 'M106 S255'") {
  114. REQUIRE_THAT(writer.set_fan(100, true), Catch::Equals("M106 S255\n"));
  115. }
  116. AND_WHEN("Fan value is set to 93\%") {
  117. THEN("Output string is 'M106 S237'") {
  118. REQUIRE_THAT(writer.set_fan(93, true), Catch::Equals("M106 S237.15\n"));
  119. }
  120. }
  121. AND_WHEN("Fan value is set to 21\%") {
  122. THEN("Output string is 'M106 S54'") {
  123. REQUIRE_THAT(writer.set_fan(21, true), Catch::Equals("M106 S53.55\n"));
  124. }
  125. }
  126. }
  127. }
  128. }
  129. SCENARIO("set_fan saves state.", "[GCode][GCodeWriter]") {
  130. GIVEN("GCodeWriter instance with comments off and RepRap flavor") {
  131. GCodeWriter writer;
  132. writer.config.gcode_comments.value = false;
  133. writer.config.gcode_flavor.value = gcfRepRap;
  134. writer.config.fan_percentage.value = true;
  135. WHEN("set_fan is called to set speed to 100\%, saving") {
  136. THEN("Fan gcode is emitted.") {
  137. CHECK_THAT(writer.set_fan(100, false), Catch::Equals("M106 S100\n"));
  138. }
  139. }
  140. AND_WHEN("Another call is made to set_fan for 100\%") {
  141. THEN("No fan output gcode is emitted.") {
  142. REQUIRE_THAT(writer.set_fan(100, false), Catch::Equals(""));
  143. }
  144. }
  145. AND_WHEN("Another call is made to set_fan for 90\%") {
  146. THEN("Fan gcode is emitted.") {
  147. REQUIRE_THAT(writer.set_fan(90, false), Catch::Equals("M106 S90\n"));
  148. }
  149. }
  150. }
  151. }