diff --git a/README.md b/README.md index 77d8744..de16644 100644 --- a/README.md +++ b/README.md @@ -13,8 +13,8 @@ v3.11.2 2. Execute `make` ### Settings -* Use as is to get linear calculations for time -* Copy `config_example.json` to `config.json` and edit based on your printer settings +* Use without printer config to use linear moves. You should look at +10% Estimation +* Alternatively copy `config_example.json` to `config.json` and edit based on your printer settings. You should look at -15% Estimation ### Usage `./gcode_time [file] ...` @@ -22,8 +22,8 @@ v3.11.2 **file** - a file containing gcode ### Example output -`Estimation - 05:20:10` +`Estimation - 05:20:10` -`Adding 10% - 05:52:11` +`+10% - 05:52:11` -`Removing 10% - 04:48:09` +`-15% - 04:32:09` diff --git a/TimeCalc.cpp b/TimeCalc.cpp index fef2c2e..9ee23f0 100644 --- a/TimeCalc.cpp +++ b/TimeCalc.cpp @@ -15,7 +15,9 @@ #include #include "math.h" -#include +#if DEBUG_CALC +# include +#endif using namespace std; @@ -195,7 +197,7 @@ TimeCalc::PrinterMove::PrinterMove() } TimeCalc::TimeCalc() -: m_time(0.0) +: m_time(0.0), m_print_accel_max(0.0), m_travel_accel_max(0.0) { reset(); } @@ -203,6 +205,8 @@ TimeCalc::TimeCalc() void TimeCalc::reset() { m_time = 0.0; + m_print_accel_max = 0.0; + m_travel_accel_max = 0.0; fill_n(m_extruder_length, EXTRUDER_COUNT, 0.0); fill_n(m_pos, AXIS_COUNT, 0.0); @@ -222,6 +226,7 @@ void TimeCalc::add_move(Point new_pos, double speed) double vel; double accel; double prev_move_idx; + bool is_print; static int counter = 0; pos_new[0] = new_pos.x; @@ -271,6 +276,34 @@ void TimeCalc::add_move(Point new_pos, double speed) // Calculate acceleration temp.clear(); + + // Is it a print move + is_print = false; + for (int i = 0; i < EXTRUDER_COUNT; ++i) + { + if (0.0 != move_new.delta[3 + i]) + { + is_print = true; + break; + } + } + + // Add max acceleration for specific move to be considered + if (is_print) + { + if (0.0 != m_print_accel_max) + { + temp.push_back(m_print_accel_max); + } + } + else + { + if (0.0 != m_travel_accel_max) + { + temp.push_back(m_travel_accel_max); + } + } + for (int i = 0 ; i < AXIS_COUNT; ++i) { if (0.0 != move_new.unit[i] && 0.0 != m_accel_max[i]) @@ -379,6 +412,7 @@ void TimeCalc::flush() } max = vector_max(times); +#if DEBUG_CALC if (max < 0) { printf("Dist - %10.5f - %10.5f\n", move.dist, max); @@ -413,6 +447,8 @@ void TimeCalc::flush() printf("Accel [%d] - %10.5f\n", i, move.accel[i]); } } +#endif + m_time += max; } m_moves.clear(); @@ -466,3 +502,13 @@ void TimeCalc::set_max_jerk(int axis, double value) m_jerk_max[axis] = value; } } + +void TimeCalc::set_print_accel(double value) +{ + m_print_accel_max = value; +} + +void TimeCalc::set_travel_accel(double value) +{ + m_travel_accel_max = value; +} diff --git a/TimeCalc.h b/TimeCalc.h index b4b3699..96f8398 100644 --- a/TimeCalc.h +++ b/TimeCalc.h @@ -33,6 +33,9 @@ private: double m_vel_max[AXIS_COUNT]; double m_jerk_max[AXIS_COUNT]; + double m_print_accel_max; + double m_travel_accel_max; + std::vector m_moves; public: @@ -51,6 +54,9 @@ public: void set_max_acceleration(int axis, double value); void set_max_velocity(int axis, double value); void set_max_jerk(int axis, double value); + + void set_print_accel(double value); + void set_travel_accel(double value); }; #endif // TIMECALC_H_ diff --git a/config_example.json b/config_example.json index da0066a..8f1c543 100644 --- a/config_example.json +++ b/config_example.json @@ -1,28 +1,31 @@ { - "x": + "acceleration": { - "acceleration": 500, - "velocity": 500, - "jerk": 10 + "print": 500, + "travel": 1000, + "x": 500, + "y": 500, + "z": 100, + "e": [ + 1000 + ] }, - "y": + "velocity": { - "acceleration": 500, - "velocity": 500, - "jerk": 10 + "x": 500, + "y": 500, + "z": 5, + "e": [ + 50 + ] }, - "z": + "jerk": { - "acceleration": 100, - "velocity": 5, - "jerk": 0.4 - }, - "extruders": - [ - { - "acceleration": 500, - "velocity": 50, - "jerk": 5 - } - ] + "x": 10, + "y": 10, + "z": 0.3, + "e": [ + 5 + ] + } } diff --git a/main.cpp b/main.cpp index ca40348..c60d775 100644 --- a/main.cpp +++ b/main.cpp @@ -137,8 +137,14 @@ void init_calc(TimeCalc& calc) { ifstream file("config.json"); nlohmann::json conf; - vector conf_keys = {"x", "y", "z"}; + vector conf_keys_types = {"acceleration", "velocity", "jerk"}; + vector conf_keys_axes = {"x", "y", "z"}; + + if (!file.is_open()) + { + return; + } try { file >> conf; @@ -150,46 +156,74 @@ void init_calc(TimeCalc& calc) } file.close(); - for (int i = 0; i < conf_keys.size(); ++i) + for (int j = 0; j < conf_keys_types.size(); ++j) { - string key = conf_keys[i]; - if (conf.contains(key) && conf[key].is_object()) + string& key = conf_keys_types[j]; + + if (conf.contains(key) && conf["key"].is_object()) { - if (conf[key].contains("acceleration") && conf[key]["acceleration"].is_number()) + // Set 3D Axes + for (int i = 0; i < conf_keys_axes.size(); ++i) { - calc.set_max_acceleration(i, conf[key]["acceleration"]); + if (conf[key].contains(conf_keys_axes[i]) && conf[key][conf_keys_axes[i]].is_number()) + { + switch (j) + { + case 0: + calc.set_max_acceleration(i, conf[key][conf_keys_axes[i]]); + break; + + case 1: + calc.set_max_velocity(i, conf[key][conf_keys_axes[i]]); + break; + + case 2: + calc.set_max_jerk(i, conf[key][conf_keys_axes[i]]); + break; + } + } } - if (conf[key].contains("velocity") && conf[key]["velocity"].is_number()) + // Set Extruders + if (conf[key].contains("e") && conf[key]["e"].is_array()) { - calc.set_max_velocity(i, conf[key]["velocity"]); - } + for (int i = 0; i < conf[key]["e"].size(); ++i) + { + if (conf[key]["e"][i].is_number()) + { + switch (j) + { + case 0: + calc.set_max_acceleration(3 + i, conf[key]["e"][i]); + break; - if (conf[key].contains("jerk") && conf[key]["jerk"].is_number()) - { - calc.set_max_jerk(i, conf[key]["jerk"]); + case 1: + calc.set_max_velocity(3 + i, conf[key]["e"][i]); + break; + + case 2: + calc.set_max_jerk(3 + i, conf[key]["e"][i]); + break; + } + } + } } } - } - if (conf.contains("extruders") && conf["extruders"].is_array()) - { - for (int i = 0; i < conf["extruders"].size(); ++i) + // Set Print Acceleration + if (conf.contains("acceleration") && + conf["acceleration"].contains("print") && + conf["acceleration"]["print"].is_number()) { - if (conf["extruders"][i].contains("acceleration") && conf["extruders"][i]["acceleration"].is_number()) - { - calc.set_max_acceleration(3 + i, conf["extruders"][i]["acceleration"]); - } + calc.set_print_accel(conf["acceleration"]["print"]); + } - if (conf["extruders"][i].contains("velocity") && conf["extruders"][i]["velocity"].is_number()) - { - calc.set_max_velocity(3 + i, conf["extruders"][i]["velocity"]); - } - - if (conf["extruders"][i].contains("jerk") && conf["extruders"][i]["jerk"].is_number()) - { - calc.set_max_jerk(3 + i, conf["extruders"][i]["jerk"]); - } + // Set Travel Acceleration + if (conf.contains("acceleration") && + conf["acceleration"].contains("travel") && + conf["acceleration"]["travel"].is_number()) + { + calc.set_travel_accel(conf["acceleration"]["travel"]); } } } @@ -253,16 +287,16 @@ int main(int argc, char **argv) total_time = calc.get_time(); cout << "Filament - " << calc.get_extruder_length(0) << "mm" << endl; - cout << "Estimation - "; + cout << "Estimation - "; print_time(total_time); cout << endl; - cout << "Adding 10% - "; + cout << "+10% - "; print_time(total_time * 1.1); cout << endl; - cout << "Removing 10% - "; - print_time(total_time * 0.9); + cout << "-15% - "; + print_time(total_time * 0.85); cout << endl; } return 0;