#include #include #include #include #include #include #include "Point.h" #include "gcode.h" #include "json.hpp" using namespace std; void usage(char *name) { cout << "Usage:" << endl; cout << name << " [file] ..." << endl; } bool is_gcode(const string& line) { bool result = true; if (line.empty()) { result = false; } if (0 == line.find(';')) { result = false; } return result; } string& remove_comments(string& str) { size_t pos; pos = str.find(';'); if (string::npos != pos) { str.erase(pos); } return str; } vector tokenize_line(string line) { vector result; size_t pos = 0; while (!line.empty()) { pos = line.find(' '); if (string::npos == pos) { pos = line.size(); } // Create a token result.push_back(line.substr(0, pos)); // Erase the token line.erase(0, pos); // Erase the space line.erase(0, 1); } return result; } void init_map(map& gcode_map) { // Case insensitive gcode_map["G1"] = g1; gcode_map["g1"] = g1; gcode_map["G4"] = g4; gcode_map["g4"] = g4; gcode_map["G21"] = gcode_dummy; gcode_map["g21"] = gcode_dummy; gcode_map["G28"] = g28; gcode_map["g28"] = g28; gcode_map["G90"] = gcode_dummy; gcode_map["g90"] = gcode_dummy; gcode_map["G92"] = g92; gcode_map["g92"] = g92; gcode_map["M82"] = gcode_dummy; gcode_map["m82"] = gcode_dummy; gcode_map["M84"] = gcode_dummy; gcode_map["m84"] = gcode_dummy; gcode_map["M104"] = gcode_dummy; gcode_map["m104"] = gcode_dummy; gcode_map["M106"] = gcode_dummy; gcode_map["m106"] = gcode_dummy; gcode_map["M107"] = gcode_dummy; gcode_map["m107"] = gcode_dummy; gcode_map["M109"] = gcode_dummy; gcode_map["m109"] = gcode_dummy; gcode_map["M117"] = gcode_dummy; gcode_map["m117"] = gcode_dummy; gcode_map["M140"] = gcode_dummy; gcode_map["m140"] = gcode_dummy; gcode_map["M190"] = gcode_dummy; gcode_map["m190"] = gcode_dummy; } void print_time(double sec) { int hours; int min; hours = sec / 3600; sec -= hours * 3600; min = sec / 60; sec -= min * 60; printf("%02d:%02d:%02.0f", hours, min, sec); } void init_calc(TimeCalc& calc) { ifstream file("config.json"); nlohmann::json conf; vector conf_keys = {"x", "y", "z"}; try { file >> conf; } catch (const exception& e) { cout << e.what() << endl; return; } file.close(); for (int i = 0; i < conf_keys.size(); ++i) { string key = conf_keys[i]; if (conf.contains(key) && conf[key].is_object()) { if (conf[key].contains("acceleration") && conf[key]["acceleration"].is_number()) { calc.set_max_acceleration(i, conf[key]["acceleration"]); } if (conf[key].contains("velocity") && conf[key]["velocity"].is_number()) { calc.set_max_velocity(i, conf[key]["velocity"]); } if (conf[key].contains("jerk") && conf[key]["jerk"].is_number()) { calc.set_max_jerk(i, conf[key]["jerk"]); } } } if (conf.contains("extruders") && conf["extruders"].is_array()) { for (int i = 0; i < conf["extruders"].size(); ++i) { if (conf["extruders"][i].contains("acceleration") && conf["extruders"][i]["acceleration"].is_number()) { calc.set_max_acceleration(3 + i, conf["extruders"][i]["acceleration"]); } 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"]); } } } } int main(int argc, char **argv) { ifstream file; string line; vector filenames(&(argv[1]), &(argv[argc])); vector line_tokens; map gcode_map; TimeCalc calc; double speed; double total_time; int hours; int minutes; if (argc < 2) { usage(argv[0]); return -1; } init_map(gcode_map); for (string& fname : filenames) { calc.reset(); init_calc(calc); speed = 0; file.open(fname); while (!file.eof()) { getline(file, line); if (!is_gcode(remove_comments(line))) { continue; } line_tokens = tokenize_line(line); if (0 != gcode_map.count(line_tokens[0])) { gcode_map[line_tokens[0]](line_tokens, calc, speed); } else { cout << "Unknown Gcode '" << line_tokens[0] << "'" << endl; } } file.close(); if (filenames.size() > 1) { cout << "--- " << fname << " ---" << endl; } calc.flush(); total_time = calc.get_time(); cout << "Filament - " << calc.get_extruder_length(0) << "mm" << endl; cout << "Estimation - "; print_time(total_time); cout << endl; cout << "Adding 10% - "; print_time(total_time * 1.1); cout << endl; cout << "Removing 10% - "; print_time(total_time * 0.9); cout << endl; } return 0; }