Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New GCodeTimeEstimator class, with basic estimation code by @lordofhy…
- Loading branch information
Showing
11 changed files
with
214 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#!/usr/bin/env perl | ||
|
||
use strict; | ||
use warnings; | ||
|
||
BEGIN { | ||
use FindBin; | ||
use lib "$FindBin::Bin/../lib"; | ||
use local::lib "$FindBin::Bin/../local-lib"; | ||
} | ||
|
||
use Slic3r; | ||
|
||
die "Usage: estimate-gcode-time.pl FILE\n" | ||
if @ARGV != 1; | ||
|
||
my $estimator = Slic3r::GCode::TimeEstimator->new; | ||
$estimator->parse_file($ARGV[0]); | ||
printf "Time: %d minutes and %d seconds\n", int($estimator->time / 60), $estimator->time % 60; | ||
|
||
__END__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
#include "GCodeTimeEstimator.hpp" | ||
#include <boost/bind.hpp> | ||
#include <cmath> | ||
|
||
namespace Slic3r { | ||
|
||
void | ||
GCodeTimeEstimator::parse(const std::string &gcode) | ||
{ | ||
GCodeReader::parse(gcode, boost::bind(&GCodeTimeEstimator::_parser, this, _1, _2)); | ||
} | ||
|
||
void | ||
GCodeTimeEstimator::parse_file(const std::string &file) | ||
{ | ||
GCodeReader::parse_file(file, boost::bind(&GCodeTimeEstimator::_parser, this, _1, _2)); | ||
} | ||
|
||
void | ||
GCodeTimeEstimator::_parser(GCodeReader&, const GCodeReader::GCodeLine &line) | ||
{ | ||
// std::cout << "[" << this->time << "] " << line.raw << std::endl; | ||
if (line.cmd == "G1") { | ||
const float dist_XY = line.dist_XY(); | ||
const float new_F = line.new_F(); | ||
|
||
if (dist_XY > 0) { | ||
//this->time += dist_XY / new_F * 60; | ||
this->time += _accelerated_move(dist_XY, new_F/60, this->acceleration); | ||
} else { | ||
//this->time += std::abs(line.dist_E()) / new_F * 60; | ||
this->time += _accelerated_move(std::abs(line.dist_E()), new_F/60, this->acceleration); | ||
} | ||
//this->time += std::abs(line.dist_Z()) / new_F * 60; | ||
this->time += _accelerated_move(std::abs(line.dist_Z()), new_F/60, this->acceleration); | ||
} else if (line.cmd == "M204" && line.has('S')) { | ||
this->acceleration = line.get_float('S'); | ||
} else if (line.cmd == "G4") { // swell | ||
if (line.has('S')) { | ||
this->time += line.get_float('S'); | ||
} else if (line.has('P')) { | ||
this->time += line.get_float('P')/1000; | ||
} | ||
} | ||
} | ||
|
||
// Wildly optimistic acceleration "bell" curve modeling. | ||
// Returns an estimate of how long the move with a given accel | ||
// takes in seconds. | ||
// It is assumed that the movement is smooth and uniform. | ||
float | ||
GCodeTimeEstimator::_accelerated_move(double length, double v, double acceleration) | ||
{ | ||
// for half of the move, there are 2 zones, where the speed is increasing/decreasing and | ||
// where the speed is constant. | ||
// Since the slowdown is assumed to be uniform, calculate the average velocity for half of the | ||
// expected displacement. | ||
// final velocity v = a*t => a * (dx / 0.5v) => v^2 = 2*a*dx | ||
// v_avg = 0.5v => 2*v_avg = v | ||
// d_x = v_avg*t => t = d_x / v_avg | ||
acceleration = (acceleration == 0.0 ? 4000.0 : acceleration); // Set a default accel to use for print time in case it's 0 somehow. | ||
auto half_length = length / 2.0; | ||
auto t_init = v / acceleration; // time to final velocity | ||
auto dx_init = (0.5*v*t_init); // Initial displacement for the time to get to final velocity | ||
auto t = 0.0; | ||
if (half_length >= dx_init) { | ||
half_length -= (0.5*v*t_init); | ||
t += t_init; | ||
t += (half_length / v); // rest of time is at constant speed. | ||
} else { | ||
// If too much displacement for the expected final velocity, we don't hit the max, so reduce | ||
// the average velocity to fit the displacement we actually are looking for. | ||
t += std::sqrt(std::abs(length) * 2.0 * acceleration) / acceleration; | ||
} | ||
return 2.0*t; // cut in half before, so double to get full time spent. | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#ifndef slic3r_GCodeTimeEstimator_hpp_ | ||
#define slic3r_GCodeTimeEstimator_hpp_ | ||
|
||
#include "libslic3r.h" | ||
#include "GCodeReader.hpp" | ||
|
||
namespace Slic3r { | ||
|
||
class GCodeTimeEstimator : public GCodeReader { | ||
public: | ||
float time = 0; // in seconds | ||
|
||
void parse(const std::string &gcode); | ||
void parse_file(const std::string &file); | ||
|
||
protected: | ||
float acceleration = 9000; | ||
void _parser(GCodeReader&, const GCodeReader::GCodeLine &line); | ||
static float _accelerated_move(double length, double v, double acceleration); | ||
}; | ||
|
||
} /* namespace Slic3r */ | ||
|
||
#endif /* slic3r_GCodeTimeEstimator_hpp_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
%module{Slic3r::XS}; | ||
|
||
%{ | ||
#include <xsinit.h> | ||
#include "libslic3r/GCodeTimeEstimator.hpp" | ||
%} | ||
|
||
%name{Slic3r::GCode::TimeEstimator} class GCodeTimeEstimator { | ||
GCodeTimeEstimator(); | ||
~GCodeTimeEstimator(); | ||
|
||
float time %get{time}; | ||
void parse(std::string gcode); | ||
void parse_file(std::string file); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters