test_gcodewriter.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //#define CATCH_CONFIG_DISABLE
  2. #include <catch_main.hpp>
  3. #include <memory>
  4. #include "libslic3r/GCodeWriter.hpp"
  5. #include "test_data.hpp"
  6. #include <iomanip>
  7. #include <iostream>
  8. //#include "test_data.hpp" // get access to init_print, etc
  9. using namespace Slic3r;
  10. using namespace std::literals::string_literals;
  11. // can't understand what the test want to test: here we are overflowing the double capacity to break the lift logic...
  12. //so i moved m_lifted = 0; out of the test in unlift to make that pass.
  13. SCENARIO("lift() and unlift() behavior with large values of Z", "[!shouldfail]") {
  14. GIVEN("A config from a file and a single extruder.") {
  15. GCodeWriter writer{};
  16. GCodeConfig& config {writer.config};
  17. config.set_defaults();
  18. config.load(std::string{ TEST_DATA_DIR PATH_SEPARATOR } +"fff_print_tests/test_gcodewriter/config_lift_unlift.ini"s, Slic3r::ForwardCompatibilitySubstitutionRule::Disable);
  19. assert(!config.retract_lift.values.empty() && config.retract_lift.values.front() > 0);
  20. std::vector<uint16_t> extruder_ids {0};
  21. writer.set_extruders(extruder_ids);
  22. writer.set_tool(0);
  23. int lift_layer_id = 1; // not first layer, as lift is deactivated on it by default
  24. WHEN("Z is set to 9007199254740992") {
  25. double trouble_Z{ 9007199254740992 };
  26. const std::string travel_str = writer.travel_to_z(trouble_Z);
  27. REQUIRE(travel_str.size() > 0);
  28. AND_WHEN("GcodeWriter::Lift() is called") {
  29. const std::string lift = writer.lift(lift_layer_id);
  30. REQUIRE(lift.size() > 0);
  31. AND_WHEN("Z is moved post-lift to the same delta as the config Z lift") {
  32. REQUIRE(writer.travel_to_z(trouble_Z + config.retract_lift.values[0]).size() == 0);
  33. AND_WHEN("GCodeWriter::Unlift() is called") {
  34. const std::string unlift = writer.unlift();
  35. REQUIRE(unlift.size() == 0); // we're the same height so no additional move happens.
  36. THEN("GCodeWriter::Lift() emits gcode.") {
  37. const std::string lift_again = writer.lift(lift_layer_id);
  38. REQUIRE(lift_again.size() > 0);
  39. }
  40. }
  41. }
  42. }
  43. }
  44. }
  45. }
  46. SCENARIO("lift() is not ignored after unlift() at normal values of Z") {
  47. GIVEN("A config from a file and a single extruder.") {
  48. GCodeWriter writer{};
  49. GCodeConfig& config {writer.config};
  50. config.set_defaults();
  51. config.load(std::string{ TEST_DATA_DIR PATH_SEPARATOR } +"fff_print_tests/test_gcodewriter/config_lift_unlift.ini"s, Slic3r::ForwardCompatibilitySubstitutionRule::Disable);
  52. std::vector<uint16_t> extruder_ids {0};
  53. writer.set_extruders(extruder_ids);
  54. writer.set_tool(0);
  55. int lift_layer_id = 1;
  56. WHEN("Z is set to 203") {
  57. double trouble_Z{ 203 };
  58. writer.travel_to_z(trouble_Z);
  59. AND_WHEN("GcodeWriter::Lift() is called") {
  60. REQUIRE(writer.lift(lift_layer_id).size() > 0);
  61. AND_WHEN("Z is moved post-lift to the same delta as the config Z lift") {
  62. REQUIRE(writer.travel_to_z(trouble_Z + config.retract_lift.values[0]).size() == 0);
  63. AND_WHEN("GCodeWriter::Unlift() is called") {
  64. REQUIRE(writer.unlift().size() == 0); // we're the same height so no additional move happens.
  65. THEN("GCodeWriter::Lift() emits gcode.") {
  66. REQUIRE(writer.lift(lift_layer_id).size() > 0);
  67. }
  68. }
  69. }
  70. }
  71. }
  72. WHEN("Z is set to 500003") {
  73. double trouble_Z{ 500003 };
  74. writer.travel_to_z(trouble_Z);
  75. AND_WHEN("GcodeWriter::Lift() is called") {
  76. REQUIRE(writer.lift(lift_layer_id).size() > 0);
  77. AND_WHEN("Z is moved post-lift to the same delta as the config Z lift") {
  78. REQUIRE(writer.travel_to_z(trouble_Z + config.retract_lift.values[0]).size() == 0);
  79. AND_WHEN("GCodeWriter::Unlift() is called") {
  80. REQUIRE(writer.unlift().size() == 0); // we're the same height so no additional move happens.
  81. THEN("GCodeWriter::Lift() emits gcode.") {
  82. REQUIRE(writer.lift(lift_layer_id).size() > 0);
  83. }
  84. }
  85. }
  86. }
  87. }
  88. WHEN("Z is set to 10.3") {
  89. double trouble_Z{ 10.3 };
  90. writer.travel_to_z(trouble_Z);
  91. AND_WHEN("GcodeWriter::Lift() is called") {
  92. REQUIRE(writer.lift(lift_layer_id).size() > 0);
  93. AND_WHEN("Z is moved post-lift to the same delta as the config Z lift") {
  94. REQUIRE(writer.travel_to_z(trouble_Z + config.retract_lift.values[0]).size() == 0);
  95. AND_WHEN("GCodeWriter::Unlift() is called") {
  96. REQUIRE(writer.unlift().size() == 0); // we're the same height so no additional move happens.
  97. THEN("GCodeWriter::Lift() emits gcode.") {
  98. REQUIRE(writer.lift(lift_layer_id).size() > 0);
  99. }
  100. }
  101. }
  102. }
  103. }
  104. }
  105. }
  106. SCENARIO("set_speed emits values with fixed-point output.") {
  107. GIVEN("GCodeWriter instance") {
  108. GCodeWriter writer;
  109. //max in assert is 100k
  110. //WHEN("set_speed is called to set speed to 1.09321e+06") {
  111. // THEN("Output string is G1 F1093210.000") {
  112. // REQUIRE_THAT(writer.set_speed(1.09321e+06), Catch::Equals("G1 F1093210.000\n"));
  113. // }
  114. //}
  115. WHEN("set_speed is called to set speed to 9.99321e+04") {
  116. THEN("Output string is G1 F5995926") {
  117. REQUIRE_THAT(writer.set_speed(9.99321e+04), Catch::Equals("G1 F5995926\n"));
  118. }
  119. }
  120. WHEN("set_speed is called to set speed to 1") {
  121. THEN("Output string is G1 F60") {
  122. REQUIRE_THAT(writer.set_speed(1.0), Catch::Equals("G1 F60\n"));
  123. }
  124. }
  125. WHEN("set_speed is called to set speed to 203.2000022") {
  126. THEN("Output string is G1 F12192") {
  127. REQUIRE_THAT(writer.set_speed(203.2000022), Catch::Equals("G1 F12192\n"));
  128. }
  129. }
  130. WHEN("set_speed is called to set speed to 203.2000522") {
  131. THEN("Output string is G1 F12192.003") {
  132. REQUIRE_THAT(writer.set_speed(203.2000522), Catch::Equals("G1 F12192.003\n"));
  133. }
  134. }
  135. }
  136. }