test_gcodewriter.cpp 6.9 KB

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