From 5748e3d5afc22d6c50087cc701cf1113909edd47 Mon Sep 17 00:00:00 2001 From: Adrian Scripca Date: Wed, 10 May 2023 14:08:14 +0300 Subject: [PATCH] Heightmap probing is now rendered in real time. --- docs/todo.txt | 46 ++++++++++++++++++++++++---------------------- main.cpp | 43 +++++++++++++++++++++++++------------------ render.cpp | 7 ++++++- 3 files changed, 55 insertions(+), 41 deletions(-) diff --git a/docs/todo.txt b/docs/todo.txt index 5f8b77e..9f64cea 100644 --- a/docs/todo.txt +++ b/docs/todo.txt @@ -9,32 +9,33 @@ DONE - step every: 5mm? - z final safety height: Z15 DONE - from and to can be filled from current project bounding box +DONE 2. probing +DONE - machine moves at grid start position (x, y) +DONE G0X0Y0 +DONE - gets down for initial probing +DONE G38.2 Z-65 F100 +DONE - step back a bit (1mm, relative) +DONE G91 G0 Z1 +DONE - seek again with lower feed rate +DONE G38.2 Z-5 F5 +DONE - after first probe ever, this Z becomes reference 0 +DONE G10 L20 P0 Z0 +DONE - all subsequent probes will be relative to this +DONE - for each probing location: +DONE - G0 Z"clearance height" +DONE - G0 XxxYyyy (next point location) +DONE - G0 Z"start probing at" +DONE - G38.2 Z-5 F5 +DONE - store Z offset and place it in the heightmap - 2. probing - - machine moves at grid start position (x, y) - G0X0Y0 - - gets down for initial probing - G38.2 Z-65 F100 - - step back a bit (1mm, relative) - G91 G0 Z1 - - seek again with lower feed rate - G38.2 Z-5 F5 - - after first probe ever, this Z becomes reference 0 - G10 L20 P0 Z0 - - - all subsequent probes will be relative to this - - for each probing location: - - G0 Z"clearance height" - - G0 XxxYyyy (next point location) - - G0 Z"start probing at" - - G38.2 Z-5 F5 - - store Z offset and place it in the heightmap - - 3. modify loaded program with heightmap data + 3. modify loaded program with heightmap data - foreach line in program - if line contains a Z coordinate, update the Z according to the X and Y - if line does not contain a Z coordinate, add a Z coordinate according to the X and Y +Solve bug in which the probing data does not get rendered properly as it gets probed. +Bug: query coordinate systems after first z probe. + Edge finding - prerequisites - which edge to find? -X/+X/-Y/+Y @@ -50,8 +51,9 @@ Render arcs Render quadratic splines Render bezier splines Synchronize executing program with render (show executed paths with different colors) -Show a progress bar when executing programs. Add aggressive buffer execution policy and make it selectable. +Show a progress bar when executing programs. +Show a progress bar when probing. DONE - Show program extents diff --git a/main.cpp b/main.cpp index 99fa1be..62132a3 100644 --- a/main.cpp +++ b/main.cpp @@ -513,7 +513,7 @@ public: return result; } - void fill_heightmap_from_grid(const grbl::heightmap& grid) { + void fill_heightmap_controls_from_grid(const grbl::heightmap& grid) const { txt_heightmap_from_x->set_value(std::to_string(grid.from_x)); txt_heightmap_from_y->set_value(std::to_string(grid.from_y)); txt_heightmap_to_x->set_value(std::to_string(grid.to_x)); @@ -562,7 +562,7 @@ public: }, true); if (!path.empty()) { heightmap_grid = load_heightmap(path); - fill_heightmap_from_grid(heightmap_grid); + fill_heightmap_controls_from_grid(heightmap_grid); renderer.update_grid(heightmap_grid); } }); @@ -639,7 +639,7 @@ public: }); } - void fill_heightmap_from_model() { + void fill_heightmap_from_model() const { auto min = renderer.get_extents_min(); auto max = renderer.get_extents_max(); txt_heightmap_from_x->set_value(std::to_string(min.x)); @@ -895,9 +895,12 @@ public: new MessageDialog(this, MessageDialog::Type::Warning, "Probe result", ss.str()); } + volatile bool should_render_grid = false; void on_heightmap_probe_acquired(grbl::heightmap *grid) override { std::cout << "Updating grid" << std::endl; - renderer.update_grid(*grid); + // let's try to update this from the UI thread since the call is invoked from the + // grbl communicator thread. + should_render_grid = true; } void on_check_completed(bool success, size_t failed_index, size_t error) override { @@ -1065,6 +1068,12 @@ public: machine_initialized = false; } + // TODO: change this to something prettier. make a pipe/queue for in-between threads comm + if (should_render_grid) { + renderer.update_grid(heightmap_grid); + should_render_grid = false; + } + Widget::draw(ctx); } @@ -1075,23 +1084,21 @@ public: m_render_pass->resize(framebuffer_size()); m_render_pass->begin(); - if (pgm.is_loaded) { - renderer.update(pgm, cnc); - // compute mvp - glm::mat4 projection = glm::perspective(45.0f * glm::pi() / 180.0f, - (float) fb_size.x() / (float) fb_size.y(), - 0.1f, - 10000.f); + renderer.update(pgm, cnc); + // compute mvp + glm::mat4 projection = glm::perspective(45.0f * glm::pi() / 180.0f, + (float) fb_size.x() / (float) fb_size.y(), + 0.1f, + 10000.f); - glm::mat4 view = glm::translate(glm::mat4(1.0f), glm::vec3(0, 0, -cam_zoom)) * - glm::toMat4(cam_src_rotation) * - glm::translate(glm::mat4(1.0f), cam_pan - cam_target); + glm::mat4 view = glm::translate(glm::mat4(1.0f), glm::vec3(0, 0, -cam_zoom)) * + glm::toMat4(cam_src_rotation) * + glm::translate(glm::mat4(1.0f), cam_pan - cam_target); - glm::mat4 model = glm::mat4(1.0f); - glm::mat3 normal_mat = glm::inverseTranspose(glm::mat3(view * model)); + glm::mat4 model = glm::mat4(1.0f); + glm::mat3 normal_mat = glm::inverseTranspose(glm::mat3(view * model)); - renderer.render(model, view, projection, normal_mat, glm::vec2(fb_size.x(), fb_size.y())); - } + renderer.render(model, view, projection, normal_mat, glm::vec2(fb_size.x(), fb_size.y())); m_render_pass->end(); } diff --git a/render.cpp b/render.cpp index ab44f6c..5619a92 100644 --- a/render.cpp +++ b/render.cpp @@ -339,6 +339,8 @@ void grbl::program_renderer::initialize_program_buffers() { } GLsizei grbl::program_renderer::update_model_vbo(const grbl::program& pgm) { + if (!pgm.is_loaded) return 0; + static auto movement_re = std::regex(R"(([gG]0*1?\s+|[xXyYzZ]\s*[0-9\.\-]+))"); bool is_tool_on = false; @@ -428,6 +430,9 @@ void grbl::program_renderer::update_grid(const grbl::heightmap& grid) { glm::vec4 color = {0.5, 0.3, 0, 1}; + // this should only be called whenever the grid changes + // therefor it should be called each and every time we + // probe a new location. let's check that. std::vector buffer_data; for (int y = 0; y < grid.y_segments; y++) { @@ -466,7 +471,7 @@ void grbl::program_renderer::update_grid(const grbl::heightmap& grid) { heightmap_vertices_count = buffer_data.size() * sizeof(float) / size_of_vertex_in_bytes; glBindBuffer(GL_ARRAY_BUFFER, heightmap_vbo_id); - glBufferData(GL_ARRAY_BUFFER, sizeof(float) * buffer_data.size(), buffer_data.data(), GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, sizeof(float) * buffer_data.size(), buffer_data.data(), GL_DYNAMIC_DRAW); } void grbl::program_renderer::initialize_heightmap_buffers() {