Skip to content

Commit

Permalink
Add basic consumption estimator
Browse files Browse the repository at this point in the history
  • Loading branch information
wawanbreton committed Nov 10, 2024
1 parent 51e8acd commit 503c6d1
Show file tree
Hide file tree
Showing 20 changed files with 212 additions and 8 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,12 @@ set(engine_SRCS # Except main.cpp.
src/infill/SubDivCube.cpp
src/infill/GyroidInfill.cpp

src/path_export/CommunicationExporter.cpp include/path_export/CommunicationExporter.h
src/path_export/ConsoleExporter.cpp include/path_export/ConsoleExporter.h
src/path_export/ConsumptionEstimationExporter.cpp include/path_export/ConsumptionEstimationExporter.h
src/path_export/GCodeExporter.cpp include/path_export/GCodeExporter.h
src/path_export/MultiExporter.cpp include/path_export/MultiExporter.h
src/path_export/PathExporter.cpp include/path_export/PathExporter.h
src/path_export/CommunicationExporter.cpp include/path_export/CommunicationExporter.h

src/path_planning/Comb.cpp include/path_planning/Comb.h
src/path_planning/ExtruderMove.cpp include/path_planning/ExtruderMove.h
Expand Down
2 changes: 2 additions & 0 deletions include/communication/ArcusCommunication.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ class ArcusCommunication : public Communication
*/
void sendPrintTimeMaterialEstimates() const override;

void sendPrintTimeMaterialEstimates(const std::shared_ptr<ConsumptionEstimationExporter>& exporter) const override;

/*
* \brief Communicate to Arcus what our progress is.
*/
Expand Down
2 changes: 2 additions & 0 deletions include/communication/CommandLine.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ class CommandLine : public Communication
*/
void sendPrintTimeMaterialEstimates() const override;

void sendPrintTimeMaterialEstimates(const std::shared_ptr<ConsumptionEstimationExporter>& exporter) const override;

/*
* \brief Show an update of our slicing progress.
*/
Expand Down
5 changes: 4 additions & 1 deletion include/communication/Communication.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

#include "geometry/Point2LL.h"
#include "settings/types/LayerIndex.h"
#include "settings/types/Velocity.h"

namespace cura
{
Expand All @@ -15,6 +14,8 @@ enum class PrintFeatureType : unsigned char;
class Shape;
class Polygon;
class ExtruderTrain;
class ConsumptionEstimationExporter;
class Velocity;

/*
* An abstract class to provide a common interface for all methods of
Expand Down Expand Up @@ -115,6 +116,8 @@ class Communication
*/
virtual void sendPrintTimeMaterialEstimates() const = 0;

virtual void sendPrintTimeMaterialEstimates(const std::shared_ptr<ConsumptionEstimationExporter>& exporter) const = 0;

/*
* \brief Indicate that we're beginning to send g-code.
*/
Expand Down
1 change: 1 addition & 0 deletions include/path_export/CommunicationExporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class CommunicationExporter : public PathExporter
virtual void writeExtrusion(
const Point3LL& p,
const Velocity& speed,
const size_t extruder_nr,
const double extrusion_mm3_per_mm,
const coord_t line_width,
const coord_t line_thickness,
Expand Down
1 change: 1 addition & 0 deletions include/path_export/ConsoleExporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class ConsoleExporter : public PathExporter
virtual void writeExtrusion(
const Point3LL& p,
const Velocity& speed,
const size_t extruder_nr,
const double extrusion_mm3_per_mm,
const coord_t line_width,
const coord_t line_thickness,
Expand Down
52 changes: 52 additions & 0 deletions include/path_export/ConsumptionEstimationExporter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright (c) 2024 UltiMaker
// CuraEngine is released under the terms of the AGPLv3 or higher

#ifndef PATHEXPORTER_CONSUMPTIONESTIMATIONEXPORTER_H
#define PATHEXPORTER_CONSUMPTIONESTIMATIONEXPORTER_H

#include <map>
#include <optional>

#include "path_export/PathExporter.h"
#include "settings/types/Duration.h"

namespace cura
{

class ConsumptionEstimationExporter : public PathExporter
{
public:
const std::map<PrintFeatureType, Duration>& getDurations() const;

const std::map<size_t, double>& getExtrusionAmounts() const;

void writeExtrusion(
const Point3LL& p,
const Velocity& speed,
const size_t extruder_nr,
const double extrusion_mm3_per_mm,
const coord_t line_width,
const coord_t line_thickness,
const PrintFeatureType feature,
const bool update_extrusion_offset) override;

void writeTravelMove(const Point3LL& position, const Velocity& speed, const PrintFeatureType feature) override;

void writeLayerEnd(const LayerIndex& layer_index, const coord_t z, const coord_t layer_thickness) override;

void writeLayerStart(const LayerIndex& layer_index, const Point3LL& start_position) override;

private:
std::optional<double> getDistanceToLastPosition(const Point3LL& p) const;

void addDuration(const std::optional<double>& distance, const Velocity& speed, PrintFeatureType feature);

private:
std::map<PrintFeatureType, Duration> durations_;
std::map<size_t, double> extrusions_amounts_;
std::optional<Point3LL> last_position_;
};

} // namespace cura

#endif // PATHEXPORTER_CONSUMPTIONESTIMATIONEXPORTER_H
3 changes: 2 additions & 1 deletion include/path_export/GCodeExporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ class GCodeExporter : public NoCopy, public PathExporter
virtual void writeExtrusion(
const Point3LL& p,
const Velocity& speed,
const size_t extruder_nr,
double extrusion_mm3_per_mm,
const coord_t line_width,
const coord_t line_thickness,
Expand All @@ -401,7 +402,7 @@ class GCodeExporter : public NoCopy, public PathExporter

void writeExtrusion(const Point3LL& p, const Velocity& speed, double extrusion_mm3_per_mm, PrintFeatureType feature, bool update_extrusion_offset = false)
{
return writeExtrusion(p, speed, extrusion_mm3_per_mm, 0, 0, feature, update_extrusion_offset);
return writeExtrusion(p, speed, current_extruder_, extrusion_mm3_per_mm, 0, 0, feature, update_extrusion_offset);
}

/*!
Expand Down
1 change: 1 addition & 0 deletions include/path_export/MultiExporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class MultiExporter : public PathExporter
virtual void writeExtrusion(
const Point3LL& p,
const Velocity& speed,
const size_t extruder_nr,
const double extrusion_mm3_per_mm,
const coord_t line_width,
const coord_t line_thickness,
Expand Down
1 change: 1 addition & 0 deletions include/path_export/PathExporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class PathExporter
virtual void writeExtrusion(
const Point3LL& p,
const Velocity& speed,
const size_t extruder_nr,
const double extrusion_mm3_per_mm,
const coord_t line_width,
const coord_t line_thickness,
Expand Down
5 changes: 5 additions & 0 deletions src/FffGcodeWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "infill.h"
#include "path_export/CommunicationExporter.h"
#include "path_export/ConsoleExporter.h"
#include "path_export/ConsumptionEstimationExporter.h"
#include "path_export/MultiExporter.h"
#include "path_planning/LayerPlan.h"
#include "progress/Progress.h"
Expand Down Expand Up @@ -168,6 +169,8 @@ void FffGcodeWriter::writeGCode(SliceDataStorage& storage, TimeKeeper& time_keep
MultiExporter exporter;
exporter.appendExporter(std::make_shared<ConsoleExporter>());
exporter.appendExporter(std::make_shared<CommunicationExporter>(Application::getInstance().communication_));
auto consumption_estimator = std::make_shared<ConsumptionEstimationExporter>();
exporter.appendExporter(consumption_estimator);

int process_layer_starting_layer_nr = 0;
const bool has_raft = scene.current_mesh_group->settings.get<EPlatformAdhesion>("adhesion_type") == EPlatformAdhesion::RAFT;
Expand Down Expand Up @@ -210,6 +213,8 @@ void FffGcodeWriter::writeGCode(SliceDataStorage& storage, TimeKeeper& time_keep

constexpr bool force = true;
gcode.writeRetraction(storage.retraction_wipe_config_per_extruder[gcode.getExtruderNr()].retraction_config, force); // retract after finishing each meshgroup

Application::getInstance().communication_->sendPrintTimeMaterialEstimates(consumption_estimator);
}

unsigned int FffGcodeWriter::findSpiralizedLayerSeamVertexIndex(const SliceDataStorage& storage, const SliceMeshStorage& mesh, const int layer_nr, const int last_layer_nr)
Expand Down
33 changes: 32 additions & 1 deletion src/communication/ArcusCommunication.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) 2024 UltiMaker
// CuraEngine is released under the terms of the AGPLv3 or higher

#include "path_export/ConsumptionEstimationExporter.h"
#ifdef ARCUS

#include "communication/ArcusCommunication.h"
Expand Down Expand Up @@ -438,6 +439,36 @@ void ArcusCommunication::sendPrintTimeMaterialEstimates() const
spdlog::debug("Done sending print time and material estimates.");
}

void ArcusCommunication::sendPrintTimeMaterialEstimates(const std::shared_ptr<ConsumptionEstimationExporter>& exporter) const
{
spdlog::debug("Sending print time and material estimates.");
std::shared_ptr<proto::PrintTimeMaterialEstimates> message = std::make_shared<proto::PrintTimeMaterialEstimates>();

std::map<PrintFeatureType, Duration> time_estimates = exporter->getDurations();
message->set_time_infill(time_estimates[PrintFeatureType::Infill]);
message->set_time_inset_0(time_estimates[PrintFeatureType::OuterWall]);
message->set_time_inset_x(time_estimates[PrintFeatureType::InnerWall]);
message->set_time_none(time_estimates[PrintFeatureType::NoneType]);
message->set_time_retract(time_estimates[PrintFeatureType::MoveRetraction]);
message->set_time_skin(time_estimates[PrintFeatureType::Skin]);
message->set_time_skirt(time_estimates[PrintFeatureType::SkirtBrim]);
message->set_time_support(time_estimates[PrintFeatureType::Support]);
message->set_time_support_infill(time_estimates[PrintFeatureType::SupportInfill]);
message->set_time_support_interface(time_estimates[PrintFeatureType::SupportInterface]);
message->set_time_travel(time_estimates[PrintFeatureType::MoveCombing]);
message->set_time_prime_tower(time_estimates[PrintFeatureType::PrimeTower]);

for (const auto& [extruder_nr, amount] : exporter->getExtrusionAmounts())
{
proto::MaterialEstimates* material_message = message->add_materialestimates();
material_message->set_id(extruder_nr);
material_message->set_material_amount(amount);
}

private_data->socket->sendMessage(message);
spdlog::debug("Done sending print time and material estimates.");
}

void ArcusCommunication::sendProgress(double progress) const
{
const int rounded_amount = 1000 * progress;
Expand Down Expand Up @@ -540,7 +571,7 @@ void ArcusCommunication::sliceNext()
slice->compute();
FffProcessor::getInstance()->finalize();
flushGCode();
sendPrintTimeMaterialEstimates();
// sendPrintTimeMaterialEstimates();
sendFinishedSlicing();
slice.reset();
private_data->slice_count++;
Expand Down
4 changes: 4 additions & 0 deletions src/communication/CommandLine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ void CommandLine::sendPrintTimeMaterialEstimates() const
}
}

void CommandLine::sendPrintTimeMaterialEstimates(const std::shared_ptr<ConsumptionEstimationExporter>& exporter) const
{
}

void CommandLine::sendProgress(double progress) const
{
const unsigned int rounded_amount = 100 * progress;
Expand Down
1 change: 1 addition & 0 deletions src/path_export/CommunicationExporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ CommunicationExporter::CommunicationExporter(const std::shared_ptr<Communication
void CommunicationExporter::writeExtrusion(
const Point3LL& p,
const Velocity& speed,
const size_t /*extruder_nr*/,
const double /*extrusion_mm3_per_mm*/,
const coord_t line_width,
const coord_t line_thickness,
Expand Down
1 change: 1 addition & 0 deletions src/path_export/ConsoleExporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace cura
void ConsoleExporter::writeExtrusion(
const Point3LL& p,
const Velocity& speed,
const size_t extruder_nr,
const double extrusion_mm3_per_mm,
const coord_t line_width,
const coord_t line_thickness,
Expand Down
93 changes: 93 additions & 0 deletions src/path_export/ConsumptionEstimationExporter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Copyright (c) 2024 UltiMaker
// CuraEngine is released under the terms of the AGPLv3 or higher

#include "path_export/ConsumptionEstimationExporter.h"

#include <settings/types/Velocity.h>

namespace cura
{

const std::map<PrintFeatureType, Duration>& ConsumptionEstimationExporter::getDurations() const
{
return durations_;
}

const std::map<size_t, double>& ConsumptionEstimationExporter::getExtrusionAmounts() const
{
return extrusions_amounts_;
}

void ConsumptionEstimationExporter::writeExtrusion(
const Point3LL& p,
const Velocity& speed,
const size_t extruder_nr,
const double extrusion_mm3_per_mm,
const coord_t /*line_width*/,
const coord_t /*line_thickness*/,
const PrintFeatureType feature,
const bool /*update_extrusion_offset*/)
{
std::optional<double> distance = getDistanceToLastPosition(p);
addDuration(distance, speed, feature);

if (distance.has_value()) [[likely]]
{
double extrusion_amount = distance.value() * extrusion_mm3_per_mm;

if (auto iterator = extrusions_amounts_.find(extruder_nr); iterator != extrusions_amounts_.end()) [[likely]]
{
iterator->second += extrusion_amount;
}
else
{
extrusions_amounts_.insert({ extruder_nr, extrusion_amount });
}
}

last_position_ = p;
}

void ConsumptionEstimationExporter::writeTravelMove(const Point3LL& position, const Velocity& speed, const PrintFeatureType feature)
{
std::optional<double> distance = getDistanceToLastPosition(position);
addDuration(distance, speed, feature);

last_position_ = position;
}

void ConsumptionEstimationExporter::writeLayerEnd(const LayerIndex& /*layer_index*/, const coord_t /*z*/, const coord_t /*layer_thickness*/)
{
}

void ConsumptionEstimationExporter::writeLayerStart(const LayerIndex& /*layer_index*/, const Point3LL& /*start_position*/)
{
}
std::optional<double> ConsumptionEstimationExporter::getDistanceToLastPosition(const Point3LL& p) const
{
if (last_position_.has_value()) [[likely]]
{
return (p - last_position_.value()).vSizeMM();
}

return std::nullopt;
}

void ConsumptionEstimationExporter::addDuration(const std::optional<double>& distance, const Velocity& speed, PrintFeatureType feature)
{
if (distance.has_value()) [[likely]]
{
const Duration duration = distance.value() / speed;

if (auto iterator = durations_.find(feature); iterator != durations_.end())
{
iterator->second += duration;
}
else
{
durations_.insert({ feature, duration });
}
}
}

} // namespace cura
1 change: 1 addition & 0 deletions src/path_export/GCodeExporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,7 @@ void GCodeExporter::writeTravel(const Point3LL& p, const Velocity& speed)
void GCodeExporter::writeExtrusion(
const Point3LL& p,
const Velocity& speed,
const size_t extruder_nr,
double extrusion_mm3_per_mm,
const coord_t line_width,
const coord_t line_thickness,
Expand Down
3 changes: 2 additions & 1 deletion src/path_export/MultiExporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace cura
void MultiExporter::writeExtrusion(
const Point3LL& p,
const Velocity& speed,
const size_t extruder_nr,
const double extrusion_mm3_per_mm,
const coord_t line_width,
const coord_t line_thickness,
Expand All @@ -17,7 +18,7 @@ void MultiExporter::writeExtrusion(
{
for (const std::shared_ptr<PathExporter>& exporter : exporters_)
{
exporter->writeExtrusion(p, speed, extrusion_mm3_per_mm, line_width, line_thickness, feature, update_extrusion_offset);
exporter->writeExtrusion(p, speed, extruder_nr, extrusion_mm3_per_mm, line_width, line_thickness, feature, update_extrusion_offset);
}
}

Expand Down
Loading

0 comments on commit 503c6d1

Please sign in to comment.