Got rid of segfault when uploading VBOs.
Added go-to-zero function. Reworked how work pos is obtained.
This commit is contained in:
+46
-5
@@ -1,5 +1,7 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
#include <utility>
|
||||||
|
#include <array>
|
||||||
#include "grbl_machine.h"
|
#include "grbl_machine.h"
|
||||||
#include "string_utils.h"
|
#include "string_utils.h"
|
||||||
|
|
||||||
@@ -96,15 +98,29 @@ void grbl::machine::check_program(const grbl::program& pgm) {
|
|||||||
switch_to_state(grbl_machine_state::check_program);
|
switch_to_state(grbl_machine_state::check_program);
|
||||||
}
|
}
|
||||||
|
|
||||||
void grbl::machine::run_program(const grbl::program& pgm, std::string work_offset) {
|
void grbl::machine::set_work_offset(std::string work_offset) {
|
||||||
|
std::cout << "Setting work offsset " << work_offset << std::endl;
|
||||||
|
// if (state != grbl_machine_state::disconnected) {
|
||||||
|
pipe->send(work_offset);
|
||||||
|
// awaiting_responses++;
|
||||||
|
// while (awaiting_responses > 0);
|
||||||
|
|
||||||
|
current_work_offset = work_offset;
|
||||||
|
|
||||||
|
auto pieces = split_string(parameters[work_offset], ",");
|
||||||
|
current_work_offset_values[0] = std::stof(pieces[0]);
|
||||||
|
current_work_offset_values[1] = std::stof(pieces[1]);
|
||||||
|
current_work_offset_values[2] = std::stof(pieces[2]);
|
||||||
|
// }
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void grbl::machine::run_program(const grbl::program& pgm, const std::string& work_offset) {
|
||||||
running_program = pgm;
|
running_program = pgm;
|
||||||
std::cout << "running program (" << running_program.filename << ") with " << running_program.number_of_instructions() << " instructions"
|
std::cout << "running program (" << running_program.filename << ") with " << running_program.number_of_instructions() << " instructions"
|
||||||
<< " on work offset " << work_offset << std::endl;
|
<< " on work offset " << work_offset << std::endl;
|
||||||
|
|
||||||
// set desired offset
|
set_work_offset(work_offset);
|
||||||
while (awaiting_responses > 0);
|
|
||||||
pipe->send(work_offset);
|
|
||||||
while (awaiting_responses > 0);
|
|
||||||
|
|
||||||
switch_to_state(grbl_machine_state::run_program);
|
switch_to_state(grbl_machine_state::run_program);
|
||||||
}
|
}
|
||||||
@@ -576,6 +592,31 @@ void grbl::machine::zero_offset_axis(int offset_index, int axis) {
|
|||||||
listener->on_parameters_reloaded();
|
listener->on_parameters_reloaded();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void grbl::machine::go_to_zero(bool x, bool y, bool z) {
|
||||||
|
std::string command = "G0";
|
||||||
|
if (x) command += "X0";
|
||||||
|
if (y) command += "Y0";
|
||||||
|
if (z) command += "Z0";
|
||||||
|
|
||||||
|
while (awaiting_responses > 0);
|
||||||
|
pipe->send(command);
|
||||||
|
awaiting_responses++;
|
||||||
|
while (awaiting_responses > 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
const float *grbl::machine::get_current_work_offset_values() const {
|
||||||
|
return current_work_offset_values;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::array<float, 3> grbl::machine::get_work_pos() const {
|
||||||
|
std::array<float, 3> result = {
|
||||||
|
last_report.machine_pos[0] - current_work_offset_values[0],
|
||||||
|
last_report.machine_pos[1] - current_work_offset_values[1],
|
||||||
|
last_report.machine_pos[2] - current_work_offset_values[2],
|
||||||
|
};
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
bool grbl::jog_state::no_jogging() const {
|
bool grbl::jog_state::no_jogging() const {
|
||||||
return !(up_pressed || down_pressed || left_pressed || right_pressed || z_up_pressed || z_down_pressed);
|
return !(up_pressed || down_pressed || left_pressed || right_pressed || z_up_pressed || z_down_pressed);
|
||||||
}
|
}
|
||||||
|
|||||||
+13
-1
@@ -228,7 +228,11 @@ struct machine : public transport_callbacks {
|
|||||||
|
|
||||||
void set_listener(grbl::machine_listener *listener);
|
void set_listener(grbl::machine_listener *listener);
|
||||||
void connect();
|
void connect();
|
||||||
void run_program(const grbl::program& pgm, std::string work_offset);
|
|
||||||
|
void set_work_offset(std::string work_offset);
|
||||||
|
std::array<float, 3> get_work_pos() const;
|
||||||
|
|
||||||
|
void run_program(const grbl::program& pgm, const std::string& work_offset);
|
||||||
void check_program(const grbl::program& pgm);
|
void check_program(const grbl::program& pgm);
|
||||||
void request_jog(jog_state jog) const;
|
void request_jog(jog_state jog) const;
|
||||||
void cancel_jog() const;
|
void cancel_jog() const;
|
||||||
@@ -246,6 +250,8 @@ struct machine : public transport_callbacks {
|
|||||||
// 0 = G54, 1 = G55, etc
|
// 0 = G54, 1 = G55, etc
|
||||||
void zero_offset(int which);
|
void zero_offset(int which);
|
||||||
|
|
||||||
|
void go_to_zero(bool x, bool y, bool z);
|
||||||
|
|
||||||
// 0 = G54, 1 = G55, etc axis 0=X, 1=Y, 2=Z
|
// 0 = G54, 1 = G55, etc axis 0=X, 1=Y, 2=Z
|
||||||
void zero_offset_axis(int offset_index, int axis);
|
void zero_offset_axis(int offset_index, int axis);
|
||||||
|
|
||||||
@@ -266,6 +272,12 @@ protected:
|
|||||||
std::map<std::string, std::string, settings_cmp> settings;
|
std::map<std::string, std::string, settings_cmp> settings;
|
||||||
std::map<std::string, std::string, parameters_cmp> parameters;
|
std::map<std::string, std::string, parameters_cmp> parameters;
|
||||||
|
|
||||||
|
std::string current_work_offset;
|
||||||
|
float current_work_offset_values[3] = {0};
|
||||||
|
public:
|
||||||
|
const float *get_current_work_offset_values() const;
|
||||||
|
protected:
|
||||||
|
|
||||||
volatile size_t awaiting_responses = 0;
|
volatile size_t awaiting_responses = 0;
|
||||||
realtime_status_report last_report{};
|
realtime_status_report last_report{};
|
||||||
machine_listener *listener = nullptr;
|
machine_listener *listener = nullptr;
|
||||||
|
|||||||
@@ -73,9 +73,6 @@ public:
|
|||||||
Widget *parameters_layer;
|
Widget *parameters_layer;
|
||||||
TextBox *mpos_x_text, *mpos_y_text, *mpos_z_text;
|
TextBox *mpos_x_text, *mpos_y_text, *mpos_z_text;
|
||||||
ComboBox *cboOffset, *cboTool;
|
ComboBox *cboOffset, *cboTool;
|
||||||
float offset_x = 0;
|
|
||||||
float offset_y = 0;
|
|
||||||
float offset_z = 0;
|
|
||||||
|
|
||||||
SenderApp() : Screen(Vector2i(1024, 768), "GRBL Sender") {
|
SenderApp() : Screen(Vector2i(1024, 768), "GRBL Sender") {
|
||||||
inc_ref();
|
inc_ref();
|
||||||
@@ -165,6 +162,11 @@ public:
|
|||||||
cnc.zero_offset(cboOffset->selected_index());
|
cnc.zero_offset(cboOffset->selected_index());
|
||||||
});
|
});
|
||||||
|
|
||||||
|
auto goto_zero_btn = mpos->add<Button>("Goto 0");
|
||||||
|
goto_zero_btn->set_callback([&]() {
|
||||||
|
cnc.go_to_zero(true, true, true);
|
||||||
|
});
|
||||||
|
|
||||||
// work parameters
|
// work parameters
|
||||||
layer->add<Label>("Work parameters", "sans-bold", 20);
|
layer->add<Label>("Work parameters", "sans-bold", 20);
|
||||||
auto x = new Widget(layer);
|
auto x = new Widget(layer);
|
||||||
@@ -262,11 +264,7 @@ public:
|
|||||||
|
|
||||||
void refresh_offset() {
|
void refresh_offset() {
|
||||||
auto offset_name = "G" + std::to_string(cboOffset->selected_index() + 54);
|
auto offset_name = "G" + std::to_string(cboOffset->selected_index() + 54);
|
||||||
auto offset_values = cnc.get_parameters().at(offset_name);
|
cnc.set_work_offset(offset_name);
|
||||||
auto offset_pieces = split_string(offset_values, ",");
|
|
||||||
offset_x = std::stof(offset_pieces[0]);
|
|
||||||
offset_y = std::stof(offset_pieces[1]);
|
|
||||||
offset_z = std::stof(offset_pieces[2]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void fill_in_parameters() {
|
void fill_in_parameters() {
|
||||||
@@ -388,9 +386,10 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
void update_dro() {
|
void update_dro() {
|
||||||
mpos_x_text->set_value(std::to_string(cnc.get_status().machine_pos[0] - offset_x));
|
auto work_pos = cnc.get_work_pos();
|
||||||
mpos_y_text->set_value(std::to_string(cnc.get_status().machine_pos[1] - offset_y));
|
mpos_x_text->set_value(std::to_string(work_pos[0]));
|
||||||
mpos_z_text->set_value(std::to_string(cnc.get_status().machine_pos[2] - offset_z));
|
mpos_y_text->set_value(std::to_string(work_pos[1]));
|
||||||
|
mpos_z_text->set_value(std::to_string(work_pos[2]));
|
||||||
}
|
}
|
||||||
|
|
||||||
void on_banner(std::string line) override {
|
void on_banner(std::string line) override {
|
||||||
|
|||||||
+14
-7
@@ -91,8 +91,13 @@ void grbl::program_renderer::update(const grbl::program& pgm, const grbl::machin
|
|||||||
// build vbo and vao
|
// build vbo and vao
|
||||||
vertices_count = build_vbo(pgm);
|
vertices_count = build_vbo(pgm);
|
||||||
|
|
||||||
auto x = cnc.get_status().work_pos;
|
auto work_pos = cnc.get_status().machine_pos;
|
||||||
spindle_pos = glm::vec3(x[0], x[1], x[2]);
|
auto offsets = cnc.get_current_work_offset_values();
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
work_pos[i] -= offsets[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
spindle_pos = glm::vec3(work_pos[0], work_pos[1], work_pos[2]);
|
||||||
}
|
}
|
||||||
|
|
||||||
void grbl::program_renderer::initialize_spindle_buffers() {
|
void grbl::program_renderer::initialize_spindle_buffers() {
|
||||||
@@ -148,7 +153,7 @@ void grbl::program_renderer::initialize_spindle_buffers() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, spindle_vbo_id);
|
glBindBuffer(GL_ARRAY_BUFFER, spindle_vbo_id);
|
||||||
glBufferData(GL_ARRAY_BUFFER, sizeOfVertexInBytes * buffer_data.size(), buffer_data.data(), GL_STATIC_DRAW);
|
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * buffer_data.size(), buffer_data.data(), GL_STATIC_DRAW);
|
||||||
|
|
||||||
spindle_vertices_count = buffer_data.size() * sizeof(float) / sizeOfVertexInBytes;
|
spindle_vertices_count = buffer_data.size() * sizeof(float) / sizeOfVertexInBytes;
|
||||||
}
|
}
|
||||||
@@ -178,7 +183,8 @@ GLsizei grbl::program_renderer::build_vbo(const grbl::program& pgm) {
|
|||||||
static auto movement_re = std::regex(R"(([gG]0*1?\s+|[xXyYzZ]\s*[0-9\.\-]+))");
|
static auto movement_re = std::regex(R"(([gG]0*1?\s+|[xXyYzZ]\s*[0-9\.\-]+))");
|
||||||
bool is_tool_on = false;
|
bool is_tool_on = false;
|
||||||
|
|
||||||
std::vector<float> buffer_data;
|
|
||||||
|
buffer_data.clear();
|
||||||
|
|
||||||
glm::vec3 tool_pos;
|
glm::vec3 tool_pos;
|
||||||
min_pos = max_pos = tool_pos = glm::vec3(0);
|
min_pos = max_pos = tool_pos = glm::vec3(0);
|
||||||
@@ -270,10 +276,11 @@ GLsizei grbl::program_renderer::build_vbo(const grbl::program& pgm) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const GLsizei sizeOfVertexInBytes = 28;
|
const GLsizei sizeOfVertexInBytes = 28;
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, vbo_id);
|
|
||||||
glBufferData(GL_ARRAY_BUFFER, sizeOfVertexInBytes * buffer_data.size(), buffer_data.data(), GL_DYNAMIC_DRAW);
|
|
||||||
|
|
||||||
auto number_of_vertices = buffer_data.size() * sizeof(float) / sizeOfVertexInBytes;
|
auto number_of_vertices = buffer_data.size() * sizeof(float) / sizeOfVertexInBytes;
|
||||||
|
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, vbo_id);
|
||||||
|
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * buffer_data.size(), buffer_data.data(), GL_STATIC_DRAW);
|
||||||
|
|
||||||
return number_of_vertices;
|
return number_of_vertices;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user