Skip to content

Commit

Permalink
Updated speed/flow/layer count calculation to match mainsail/fluidd. …
Browse files Browse the repository at this point in the history
…Fixed issue printing start of a print.
  • Loading branch information
ballaswag committed Apr 26, 2024
1 parent 8b88d6a commit 9c52e8a
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 138 deletions.
9 changes: 6 additions & 3 deletions src/guppyscreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ lv_theme_t GuppyScreen::th_new;
lv_obj_t *GuppyScreen::screen_saver = NULL;
#endif

KWebSocketClient GuppyScreen::ws(NULL);

std::mutex GuppyScreen::lv_lock;

GuppyScreen::GuppyScreen()
: ws(NULL)
, spoolman_panel(ws, lv_lock)
: spoolman_panel(ws, lv_lock)
, main_panel(ws, lv_lock, spoolman_panel)
, init_panel(main_panel, main_panel.get_tune_panel().get_bedmesh_panel(), lv_lock)
{
ws.register_notify_update(State::get_instance());
main_panel.create_panel();
}

Expand Down Expand Up @@ -149,6 +149,8 @@ GuppyScreen *GuppyScreen::init(std::function<void()> hal_init) {
}
#endif // OS_ANDROID

ws.register_notify_update(State::get_instance());

GuppyScreen *gs = GuppyScreen::get();
auto printers = conf->get_json("/printers");
if (!printers.empty()) {
Expand Down Expand Up @@ -207,6 +209,7 @@ std::mutex &GuppyScreen::get_lock() {
}

void GuppyScreen::connect_ws(const std::string &url) {
init_panel.set_message(LV_SYMBOL_WARNING " Waiting for printer to initialize...");
ws.connect(url.c_str(),
[this]() { init_panel.connected(ws); },
[this]() { init_panel.disconnected(ws); });
Expand Down
2 changes: 1 addition & 1 deletion src/guppyscreen.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class GuppyScreen {
static lv_obj_t *screen_saver;
#endif
static std::mutex lv_lock;
static KWebSocketClient ws;

KWebSocketClient ws;
SpoolmanPanel spoolman_panel;
MainPanel main_panel;
InitPanel init_panel;
Expand Down
13 changes: 9 additions & 4 deletions src/init_panel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ InitPanel::~InitPanel() {

void InitPanel::connected(KWebSocketClient &ws) {
spdlog::debug("init panel connected");
ws.send_jsonrpc("printer.objects.list",
[this, &ws](json& d) {
State *state = State::get_instance();
state->reset();
State *state = State::get_instance();
state->reset();

ws.send_jsonrpc("printer.objects.list", [this, &ws](json& d) {
State *state = State::get_instance();
state->set_data("printer_objs", d, "/result");

ws.send_jsonrpc("server.files.roots",
Expand Down Expand Up @@ -130,3 +131,7 @@ void InitPanel::disconnected(KWebSocketClient &ws) {
lv_obj_clear_flag(cont, LV_OBJ_FLAG_HIDDEN);
lv_obj_move_foreground(cont);
}

void InitPanel::set_message(const char *message) {
lv_label_set_text(label, message);
}
1 change: 1 addition & 0 deletions src/init_panel.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class InitPanel {

void connected(KWebSocketClient &ws);
void disconnected(KWebSocketClient &ws);
void set_message(const char *message);

private:
lv_obj_t *cont;
Expand Down
2 changes: 1 addition & 1 deletion src/print_panel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ void PrintPanel::subscribe() {

if (d.contains("result")) {
for (auto f : d["result"]) {
root.add_path(KUtils::split(f["path"], '/'), f["path"], f["modified"].template get<uint32_t>());
root.add_path(KUtils::split(f["path"], '/'), f["path"], f["modified"].template get<uint32_t>());
}
}
Tree *dir = root.find_path(KUtils::split(cur_path, '/'));
Expand Down
160 changes: 70 additions & 90 deletions src/print_status_panel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,9 @@ PrintStatusPanel::PrintStatusPanel(KWebSocketClient &websocket_client,
, elapsed(detail_cont, &clock_img, 100, "0s")
, time_left(detail_cont, &hourglass, 100, "...")
, estimated_time_s(0)
, flow_ts(std::time(nullptr))
, last_filament_used(0.0)
, filament_diameter(1.75) // XXX: check config
, flow(0.0)
, extruder_target(-1)
, heater_bed_target(-1)
, cur_layer(-1)
, total_layer(-1)
{
lv_obj_move_background(status_cont);
lv_obj_clear_flag(status_cont, LV_OBJ_FLAG_SCROLLABLE);
Expand Down Expand Up @@ -173,13 +168,10 @@ void PrintStatusPanel::reset() {
elapsed.update_label("0s");
time_left.update_label("...");
estimated_time_s = 0;
flow_ts = std::time(nullptr);
last_filament_used = 0.0;

auto v = State::get_instance()
->get_data("/printer_state/configfile/config/extruder/filament_diameter"_json_pointer);
filament_diameter = v.is_null() ? 1.750 : std::stod(v.template get<std::string>());
flow = 0.0;
extruder_target = -1;
heater_bed_target = -1;

Expand Down Expand Up @@ -263,6 +255,12 @@ void PrintStatusPanel::populate() {
lv_label_set_text(progress_label, fmt::format("{}%", new_value).c_str());
mini_print_status.update_progress(new_value);
}

v = s->get_data(
"/printer_state/gcode_move/homing_origin/2"_json_pointer);
if (!v.is_null()) {
z_offset.update_label(fmt::format("{:.5} mm", v.template get<double>()).c_str());
}
}

void PrintStatusPanel::handle_metadata(const std::string &gcode_file, json &j) {
Expand All @@ -281,6 +279,8 @@ void PrintStatusPanel::handle_metadata(const std::string &gcode_file, json &j) {
}
}

current_file = j["/result"_json_pointer];

auto width_scale = (double)lv_disp_get_physical_hor_res(NULL) / 800.0;
auto thumb_detail = KUtils::get_thumbnail(gcode_file, j, width_scale);
std::string fullpath = thumb_detail.first;
Expand Down Expand Up @@ -350,17 +350,10 @@ void PrintStatusPanel::consume(json &j) {
}

// speed
auto speed = j["/params/0/gcode_move/speed"_json_pointer];
auto speed_factor = State::get_instance()
->get_data("/printer_state/gcode_move/speed_factor"_json_pointer);

if (!speed.is_null() && !speed_factor.is_null()) {
double s = speed.template get<double>();
double sf = speed_factor.template get<double>();
int req_speed = static_cast<int>((s / 60.0 * sf));

spdlog::trace("calucated speed {}, {}, {}", speed, speed_factor, req_speed);
print_speed.update_label((std::to_string(req_speed) + " mm/s").c_str());
auto speed = j["/params/0/motion_report/live_velocity"_json_pointer];
if (!speed.is_null()) {
int s = static_cast<int>(speed.template get<double>());
print_speed.update_label((std::to_string(s) + " mm/s").c_str());
}

// zoffset
Expand Down Expand Up @@ -406,10 +399,10 @@ void PrintStatusPanel::consume(json &j) {
mini_print_status.update_progress(new_value);
}

// flow
v = j["/params/0/print_stats/filament_used"_json_pointer];
v = j["/params/0/motion_report/live_extruder_velocity"_json_pointer];
if (!v.is_null()) {
update_flow_rate(v.template get<double>());
double flow = pi() / 4 * std::pow(filament_diameter, 2) * v.template get<double>();
flow_rate.update_label(fmt::format("{:.1f} mm3/s", flow > 0.0 ? flow : 0.0).c_str());
}

v = j["/params/0/pause_resume/is_paused"_json_pointer];
Expand All @@ -433,9 +426,7 @@ void PrintStatusPanel::consume(json &j) {

// layers
v = j["/params/0/print_stats/info"_json_pointer];
if (!v.is_null()) {
update_layers(v);
}
update_layers(v);
}

void PrintStatusPanel::handle_callback(lv_event_t *event) {
Expand Down Expand Up @@ -473,83 +464,72 @@ void PrintStatusPanel::update_time_progress(uint32_t time_passed) {
}

elapsed.update_label(KUtils::eta_string(time_passed).c_str());

// auto speed_factor = State::get_instance()
// ->get_data(json::json_pointer("/status/gcode_move/speed_factor"));

}

void PrintStatusPanel::update_flow_rate(double filament_used) {
/*
const extruderPosition = parseFloat(filament_used)
const filament_diameter = this.$store.getters['printer/getPrinterSettings']('extruder.filament_diameter') || 1.75
const timeDelta = (Date.now() - this.flow.timestamp) / 1000
if (timeDelta >= 2) {
if (
this.flow.lastExtruderPosition &&
this.flow.lastExtruderPosition < extruderPosition &&
this.flow.timestamp
) {
// console.log('getting flow', filament_diameter, timeDelta)
const filamentDiff = extruderPosition - this.flow.lastExtruderPosition
const filamentCrossSection = Math.pow(filament_diameter / 2, 2) * Math.PI
this.flow.value = filamentCrossSection * filamentDiff / timeDelta
if (this.flow.max < this.flow.value) this.flow.max = this.flow.value
}
void PrintStatusPanel::update_layers(json &info) {
layers.update_label(fmt::format("{} / {}", current_layer(info), max_layer(info)).c_str());
}

this.flow.lastExtruderPosition = extruderPosition
this.flow.timestamp = Date.now()
}
*/
auto delta = std::time(nullptr) - flow_ts;
spdlog::trace("got filament_used {}, last_filament_used {}, t_delta {}",
filament_used, last_filament_used, delta);
if (delta > 1) {
if (last_filament_used
&& (last_filament_used < filament_used)) {
spdlog::trace("updating flow last {} cur {}, t_delta {}",
last_filament_used, filament_used, delta);

auto filament_delta = filament_used - last_filament_used;
auto filament_xsection = std::pow((filament_diameter / 2), 2) * pi();
flow = filament_xsection * filament_delta / delta;

spdlog::trace("caculated flow {}", flow);
#ifdef GUPPY_SMALL_SCREEN
flow_rate.update_label(fmt::format("{:.1f} mm3/s", flow).c_str());
#else
flow_rate.update_label(fmt::format("{:.2f} mm3/s", flow).c_str());
#endif
int PrintStatusPanel::max_layer(json &info) {
if (!info.is_null()) {
auto v = info["/total_layer"_json_pointer];
if (!v.is_null()) {
return v.template get<int>();
}

last_filament_used = filament_used;
flow_ts = std::time(nullptr);
}
}

void PrintStatusPanel::update_layers(json &info) {
spdlog::debug("layers {}", info.dump());
auto v = info["/current_layer"_json_pointer];
int new_cur_layer = cur_layer;
int new_total_layer = total_layer;
if (!v.is_null()) {
new_cur_layer = v.template get<int>();
if (!current_file.is_null()) {
auto v = current_file["/layer_count"_json_pointer];
if (!v.is_null()) {
return v.template get<int>();
} else {
auto first_layer_height = current_file["/first_layer_height"_json_pointer];
auto layer_height = current_file["/layer_height"_json_pointer];
auto object_height = current_file["/object_height"_json_pointer];

if (!first_layer_height.is_null() && !layer_height.is_null() && !object_height.is_null()) {
auto layer = static_cast<int>(std::ceil((object_height.template get<double>() - first_layer_height.template get<double>()) / layer_height.template get<double>() + 1));
return layer > 0 ? layer : 0;
}
}
}
return 0;
}

v = info["/total_layer"_json_pointer];
if (!v.is_null()) {
new_total_layer = v.template get<int>();
int PrintStatusPanel::current_layer(json &info) {
if (!info.is_null()) {
auto v = info["/current_layer"_json_pointer];
if (!v.is_null()) {
return v.template get<int>();
}
}

if (new_total_layer != total_layer || new_cur_layer != cur_layer) {
total_layer = new_total_layer;
cur_layer = new_cur_layer;
if (!current_file.is_null()) {
State *s = State::get_instance();
auto pd = s->get_data("/printer_state/print_stats/print_duration"_json_pointer);
auto zpos = s->get_data("/printer_state/gcode_move/gcode_position/2"_json_pointer);

auto first_layer_height = current_file["/first_layer_height"_json_pointer];
auto layer_height = current_file["/layer_height"_json_pointer];

if (!pd.is_null()
&& pd.template get<int>() > 0
&& !zpos.is_null()
&& !first_layer_height.is_null()
&& !layer_height.is_null()) {
auto layer = static_cast<int>(std::ceil((zpos.template get<double>() - first_layer_height.template get<double>()) / layer_height.template get<double>() + 1));
auto total = max_layer(info);
if (layer > total) {
return total;
}

layers.update_label(fmt::format("{} / {}", cur_layer, total_layer).c_str());
if (layer > 0) {
return layer;
}
}
}

return 0;
}

FineTunePanel &PrintStatusPanel::get_finetune_panel() {
Expand Down
7 changes: 4 additions & 3 deletions src/print_status_panel.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ class PrintStatusPanel : public NotifyConsumer {
void update_time_progress(uint32_t time_passed);
void update_flow_rate(double filament_used);
void update_layers(json &info);

int max_layer(json &info);
int current_layer(json &info);

FineTunePanel &get_finetune_panel();

private:
Expand Down Expand Up @@ -81,8 +83,7 @@ class PrintStatusPanel : public NotifyConsumer {
double flow;
int extruder_target;
int heater_bed_target;
int cur_layer;
int total_layer;
json current_file;

std::map<std::string, int> fan_speeds;
};
Expand Down
2 changes: 0 additions & 2 deletions src/state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,13 @@ void State::reset() {

void State::set_data(const std::string &key, json &j, const std::string &json_path) {
std::lock_guard<std::mutex> guard(lock);
// spdlog::debug("setting state {} : {}", key, j.dump());
auto patch = j[json::json_pointer(json_path)];
if (!patch.is_null()) {
// spdlog::debug("setting data key {}\nbefore: {}\npatch: {}", key, data.contains(key) ? data[key].dump() : "nil"
// ,patch.dump());
data[key].merge_patch(patch);
// spdlog::debug("after: {}", data.contains(key) ? data[key].dump() : "nil");
}

}

json &State::get_data() {
Expand Down
Loading

0 comments on commit 9c52e8a

Please sign in to comment.