Fix wrong z when zhop is diabled.

This commit is contained in:
SoftFever 2024-10-06 19:16:15 +08:00
parent 6f3c701a32
commit 608e6375e6
3 changed files with 11 additions and 14 deletions

View file

@ -729,7 +729,6 @@ static std::vector<Vec2d> get_path_of_change_filament(const Print& print)
gcode += gcodegen.writer().unlift(); // Make sure there is no z-hop (in most cases, there isn't). gcode += gcodegen.writer().unlift(); // Make sure there is no z-hop (in most cases, there isn't).
double current_z = gcodegen.writer().get_position().z(); double current_z = gcodegen.writer().get_position().z();
gcode += gcodegen.writer().travel_to_z(current_z);
if (z == -1.) // in case no specific z was provided, print at current_z pos if (z == -1.) // in case no specific z was provided, print at current_z pos
@ -6006,14 +6005,15 @@ std::string GCode::travel_to(const Point& point, ExtrusionRole role, std::string
if (m_spiral_vase) { if (m_spiral_vase) {
// No lazy z lift for spiral vase mode // No lazy z lift for spiral vase mode
for (size_t i = 1; i < travel.size(); ++i) { for (size_t i = 1; i < travel.size(); ++i) {
gcode += m_writer.travel_to_xy(this->point_to_gcode(travel.points[i]), comment + " travel_to_xy"); gcode += m_writer.travel_to_xy(this->point_to_gcode(travel.points[i]), comment);
} }
} else { } else {
if (travel.size() == 2) { if (travel.size() == 2) {
// No extra movements emitted by avoid_crossing_perimeters, simply move to the end point with z change // No extra movements emitted by avoid_crossing_perimeters, simply move to the end point with z change
const auto& dest2d = this->point_to_gcode(travel.points.back()); const auto& dest2d = this->point_to_gcode(travel.points.back());
Vec3d dest3d(dest2d(0), dest2d(1), z == DBL_MAX ? m_writer.get_position().z() : z); Vec3d dest3d(dest2d(0), dest2d(1), z == DBL_MAX ? m_writer.get_position().z() : z);
gcode += m_writer.travel_to_xyz(dest3d, comment + " travel_to_xyz"); gcode += m_writer.travel_to_xyz(dest3d, comment, m_need_change_layer_lift_z);
m_need_change_layer_lift_z = false;
} else { } else {
// Extra movements emitted by avoid_crossing_perimeters, lift the z to normal height at the beginning, then apply the z // Extra movements emitted by avoid_crossing_perimeters, lift the z to normal height at the beginning, then apply the z
// ratio at the last point // ratio at the last point
@ -6022,25 +6022,22 @@ std::string GCode::travel_to(const Point& point, ExtrusionRole role, std::string
// Lift to normal z at beginning // Lift to normal z at beginning
Vec2d dest2d = this->point_to_gcode(travel.points[i]); Vec2d dest2d = this->point_to_gcode(travel.points[i]);
Vec3d dest3d(dest2d(0), dest2d(1), m_writer.get_position().z()); Vec3d dest3d(dest2d(0), dest2d(1), m_writer.get_position().z());
gcode += m_writer.travel_to_xyz(dest3d, comment + " travel_to_xyz"); gcode += m_writer.travel_to_xyz(dest3d, comment, m_need_change_layer_lift_z);
m_need_change_layer_lift_z = false;
} else if (z != DBL_MAX && i == travel.size() - 1) { } else if (z != DBL_MAX && i == travel.size() - 1) {
// Apply z_ratio for the very last point // Apply z_ratio for the very last point
Vec2d dest2d = this->point_to_gcode(travel.points[i]); Vec2d dest2d = this->point_to_gcode(travel.points[i]);
Vec3d dest3d(dest2d(0), dest2d(1), z); Vec3d dest3d(dest2d(0), dest2d(1), z);
gcode += m_writer.travel_to_xyz(dest3d, comment + " travel_to_xyz"); gcode += m_writer.travel_to_xyz(dest3d, comment);
} else { } else {
// For all points in between, no z change // For all points in between, no z change
gcode += m_writer.travel_to_xy(this->point_to_gcode(travel.points[i]), comment + " travel_to_xy"); gcode += m_writer.travel_to_xy(this->point_to_gcode(travel.points[i]), comment);
} }
} }
} }
} }
this->set_last_pos(travel.points.back()); this->set_last_pos(travel.points.back());
} }
if (m_need_change_layer_lift_z) {
gcode += m_writer._travel_to_z(m_writer.get_position().z(), comment + " travel_to_z");
m_need_change_layer_lift_z = false;
}
return gcode; return gcode;
} }

View file

@ -440,7 +440,7 @@ std::string GCodeWriter::travel_to_xy(const Vec2d &point, const std::string &com
return w.string(); return w.string();
} }
std::string GCodeWriter::travel_to_xyz(const Vec3d &point, const std::string &comment) std::string GCodeWriter::travel_to_xyz(const Vec3d &point, const std::string &comment, bool force_z)
{ {
// FIXME: This function was not being used when travel_speed_z was separated (bd6badf). // FIXME: This function was not being used when travel_speed_z was separated (bd6badf).
// Calculation of feedrate was not updated accordingly. If you want to use // Calculation of feedrate was not updated accordingly. If you want to use
@ -526,7 +526,7 @@ std::string GCodeWriter::travel_to_xyz(const Vec3d &point, const std::string &co
this->set_current_position_clear(true); this->set_current_position_clear(true);
return slop_move + xy_z_move; return slop_move + xy_z_move;
} }
else if (!this->will_move_z(point(2))) { else if (!force_z && !this->will_move_z(point(2))) {
double nominal_z = m_pos(2) - m_lifted; double nominal_z = m_pos(2) - m_lifted;
m_lifted -= (point(2) - nominal_z); m_lifted -= (point(2) - nominal_z);
// In case that z_hop == layer_height we could end up with almost zero in_m_lifted // In case that z_hop == layer_height we could end up with almost zero in_m_lifted

View file

@ -69,7 +69,7 @@ public:
// SoftFever NOTE: the returned speed is mm/minute // SoftFever NOTE: the returned speed is mm/minute
double get_current_speed() const { return m_current_speed;} double get_current_speed() const { return m_current_speed;}
std::string travel_to_xy(const Vec2d &point, const std::string &comment = std::string()); std::string travel_to_xy(const Vec2d &point, const std::string &comment = std::string());
std::string travel_to_xyz(const Vec3d &point, const std::string &comment = std::string()); std::string travel_to_xyz(const Vec3d &point, const std::string &comment = std::string(), bool force_z = false);
std::string travel_to_z(double z, const std::string &comment = std::string()); std::string travel_to_z(double z, const std::string &comment = std::string());
bool will_move_z(double z) const; bool will_move_z(double z) const;
std::string extrude_to_xy(const Vec2d &point, double dE, const std::string &comment = std::string(), bool force_no_extrusion = false); std::string extrude_to_xy(const Vec2d &point, double dE, const std::string &comment = std::string(), bool force_no_extrusion = false);
@ -119,7 +119,6 @@ public:
// Returns whether this flavor supports separate print and travel acceleration. // Returns whether this flavor supports separate print and travel acceleration.
static bool supports_separate_travel_acceleration(GCodeFlavor flavor); static bool supports_separate_travel_acceleration(GCodeFlavor flavor);
std::string _travel_to_z(double z, const std::string &comment);
private: private:
// Extruders are sorted by their ID, so that binary search is possible. // Extruders are sorted by their ID, so that binary search is possible.
std::vector<Extruder> m_extruders; std::vector<Extruder> m_extruders;
@ -173,6 +172,7 @@ public:
Print Print
}; };
std::string _travel_to_z(double z, const std::string &comment);
std::string _spiral_travel_to_z(double z, const Vec2d &ij_offset, const std::string &comment); std::string _spiral_travel_to_z(double z, const Vec2d &ij_offset, const std::string &comment);
std::string _retract(double length, double restart_extra, const std::string &comment); std::string _retract(double length, double restart_extra, const std::string &comment);
std::string set_acceleration_internal(Acceleration type, unsigned int acceleration); std::string set_acceleration_internal(Acceleration type, unsigned int acceleration);