Fixed program checking and executing report

This commit is contained in:
2023-04-28 18:42:18 +03:00
parent 0b8f5a6950
commit b361b6b2fe
7 changed files with 159 additions and 48 deletions
+53 -14
View File
@@ -1,6 +1,6 @@
#include <iostream>
#include <sstream>
#include "machine.h"
#include "grbl_machine.h"
#include "string_utils.h"
static bool starts_with(const std::string& line, const std::string& prefix) {
@@ -19,15 +19,6 @@ void grbl::machine::connect() {
pipe->open(*this);
}
void grbl::machine::run_program(const grbl::program& pgm) {
std::cout << "running program with " << pgm.number_of_instructions() << " instructions" << std::endl;
running_program = pgm;
state = grbl_machine_state::run_program;
executed_instructions = 0;
continue_program();
}
void grbl::machine::on_connected(grbl::transport *transport) {
std::cout << "grbl machine connected" << std::endl;
// telnet handshake so that we get the banner. banner won't be coming otherwise
@@ -40,7 +31,6 @@ void grbl::machine::on_disconnected(grbl::transport *transport) {
std::cout << "grbl machine disconnected" << std::endl;
}
grbl::realtime_status_report grbl::parse_status_report(std::string line, grbl::realtime_status_report& result) {
// grbl::realtime_status_report result;
@@ -76,6 +66,7 @@ grbl::realtime_status_report grbl::parse_status_report(std::string line, grbl::r
return result;
}
grbl::machine_status grbl::status_from_string(const std::string& status) {
if (status == "Idle") return machine_status::idle;
if (status == "Run") return machine_status::run;
@@ -90,6 +81,25 @@ grbl::machine_status grbl::status_from_string(const std::string& status) {
return machine_status::unknown;
}
void grbl::machine::run_program(const grbl::program& pgm) {
std::cout << "running program (" << pgm.filename << ") with " << pgm.number_of_instructions() << " instructions" << std::endl;
running_program = pgm;
state = grbl_machine_state::run_program;
executed_instructions = 0;
continue_program();
}
void grbl::machine::check_program(const grbl::program& pgm) {
std::cout << "checking program (" << pgm.filename << ") with " << pgm.number_of_instructions() << " instructions" << std::endl;
running_program = pgm;
entered_check_mode = false;
executed_instructions = 0;
state = grbl_machine_state::check_program;
pipe->send("$C");
}
std::string grbl::status_to_string(const grbl::machine_status& status) {
switch (status) {
case machine_status::idle:
@@ -150,18 +160,31 @@ void grbl::machine::on_line_received(std::string line, grbl::transport *transpor
std::cout << ">> " << line << std::endl;
if (starts_with(line, "ok")) {
// on_ok();
if (state == grbl_machine_state::run_program) {
if (state == grbl_machine_state::run_program || state == grbl_machine_state::check_program) {
if (!entered_check_mode) {
entered_check_mode = true;
}
continue_program();
} else if (state == grbl_machine_state::idle && entered_check_mode) {
entered_check_mode = false;
}
} else if (starts_with(line, "error")) {
size_t error = std::stoi(line.substr(6));
if (state == grbl_machine_state::run_program) {
listener->on_run_completed(false, executed_instructions - 1, error);
state = grbl_machine_state::idle;
} else if (state == grbl_machine_state::check_program) {
listener->on_check_completed(false, executed_instructions - 1, error);
state = grbl_machine_state::idle;
pipe->send("$C"); // exit check mode
}
// on_error(error);
} else {
// we have a push message
if (starts_with(line, "Grbl")) {
listener->on_banner(line);
reset_machine_state();
} else if (starts_with(line, "<")) {
last_report = parse_status_report(line, last_report);
listener->on_realtime_status_report(last_report);
@@ -185,6 +208,7 @@ void grbl::machine::on_line_received(std::string line, grbl::transport *transpor
}
void grbl::machine::continue_program() {
bool program_ended = false;
if (executed_instructions < running_program.number_of_instructions()) {
instruction to_send;
do {
@@ -194,9 +218,19 @@ void grbl::machine::continue_program() {
if (to_send.type == instruction_type::gcode) {
pipe->send(to_send.command);
} else {
state = grbl_machine_state::idle;
program_ended = true;
}
} else {
program_ended = true;
}
if (program_ended) {
if (state == grbl_machine_state::check_program) {
pipe->send("$C");
listener->on_check_completed(true, 0, 0);
} else if (state == grbl_machine_state::run_program) {
listener->on_run_completed(true, 0, 0);
}
state = grbl_machine_state::idle;
}
}
@@ -265,6 +299,11 @@ void grbl::machine::request_feed_hold() {
pipe->send_single_char_command(0x82);
}
void grbl::machine::reset_machine_state() {
state = grbl_machine_state::idle;
executed_instructions = false;
}
bool grbl::jog_state::no_jogging() const {
return !(up_pressed || down_pressed || left_pressed || right_pressed || z_up_pressed || z_down_pressed);
}