2023-04-27 23:15:06 +03:00
|
|
|
#pragma once
|
|
|
|
|
|
2023-05-04 14:15:33 +03:00
|
|
|
#include <map>
|
|
|
|
|
#include <unordered_map>
|
2023-04-27 23:15:06 +03:00
|
|
|
#include "grbl_communication.h"
|
|
|
|
|
#include "grbl.h"
|
|
|
|
|
|
|
|
|
|
namespace grbl {
|
|
|
|
|
|
2023-04-28 14:50:58 +03:00
|
|
|
enum class machine_status {
|
|
|
|
|
idle,
|
|
|
|
|
run,
|
|
|
|
|
hold,
|
|
|
|
|
jog,
|
|
|
|
|
alarm,
|
|
|
|
|
door,
|
|
|
|
|
check,
|
|
|
|
|
home,
|
|
|
|
|
sleep,
|
|
|
|
|
tool,
|
|
|
|
|
unknown
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
machine_status status_from_string(const std::string& status);
|
|
|
|
|
std::string status_to_string(const machine_status& status);
|
|
|
|
|
std::string alarm_to_string(int alarm);
|
2023-04-28 18:54:49 +03:00
|
|
|
std::string error_to_string(size_t error);
|
2023-05-04 14:15:33 +03:00
|
|
|
std::string setting_description(int number);
|
2023-04-28 14:50:58 +03:00
|
|
|
|
|
|
|
|
struct realtime_status_report {
|
|
|
|
|
machine_status status;
|
|
|
|
|
std::string sub_status;
|
|
|
|
|
float work_pos[3] = {0};
|
|
|
|
|
float machine_pos[3] = {0};
|
|
|
|
|
size_t buffers_free = 0;
|
|
|
|
|
size_t rx_chars_free = 0;
|
|
|
|
|
size_t line_number = 0;
|
|
|
|
|
float feed_rate = 0;
|
|
|
|
|
float programmed_rpm = 0;
|
|
|
|
|
float actual_rpm = 0;
|
|
|
|
|
std::string signals;
|
|
|
|
|
float axis_offsets[3] = {0};
|
|
|
|
|
std::string coordinate_system;
|
|
|
|
|
std::string overrides;
|
|
|
|
|
std::string accessory_status;
|
|
|
|
|
bool mpg = false;
|
|
|
|
|
bool homing_complete = false;
|
|
|
|
|
std::string scaled_axis;
|
|
|
|
|
bool tool_length_reference_offset_set = false;
|
|
|
|
|
std::string firmware;
|
|
|
|
|
};
|
|
|
|
|
|
2023-04-28 16:08:05 +03:00
|
|
|
inline bool operator==(const realtime_status_report& a, const realtime_status_report& b) {
|
2023-04-28 18:42:18 +03:00
|
|
|
return a.status == b.status &&
|
|
|
|
|
a.sub_status == b.sub_status &&
|
|
|
|
|
a.machine_pos[0] == b.machine_pos[0] &&
|
|
|
|
|
a.machine_pos[1] == b.machine_pos[1] &&
|
|
|
|
|
a.machine_pos[2] == b.machine_pos[2] &&
|
|
|
|
|
a.work_pos[0] == b.work_pos[0] &&
|
|
|
|
|
a.work_pos[1] == b.work_pos[1] &&
|
|
|
|
|
a.work_pos[2] == b.work_pos[2];
|
2023-04-28 16:08:05 +03:00
|
|
|
}
|
|
|
|
|
|
2023-04-28 14:50:58 +03:00
|
|
|
realtime_status_report parse_status_report(std::string line, grbl::realtime_status_report& result);
|
2023-04-27 23:15:06 +03:00
|
|
|
|
|
|
|
|
enum class grbl_machine_state {
|
2023-05-04 14:15:33 +03:00
|
|
|
disconnected,
|
2023-04-27 23:15:06 +03:00
|
|
|
init,
|
|
|
|
|
run_program,
|
2023-04-28 18:42:18 +03:00
|
|
|
check_program,
|
2023-04-27 23:15:06 +03:00
|
|
|
idle,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct jog_state {
|
|
|
|
|
bool left_pressed = false;
|
|
|
|
|
bool right_pressed = false;
|
|
|
|
|
bool up_pressed = false;
|
|
|
|
|
bool down_pressed = false;
|
|
|
|
|
bool z_up_pressed = false;
|
|
|
|
|
bool z_down_pressed = false;
|
|
|
|
|
bool speed_fast_pressed = false;
|
|
|
|
|
bool speed_slow_pressed = false;
|
|
|
|
|
|
2023-04-28 14:50:58 +03:00
|
|
|
[[nodiscard]] bool no_jogging() const;
|
2023-04-27 23:15:06 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static bool operator==(const jog_state& a, const jog_state& b) {
|
|
|
|
|
if (a.left_pressed != b.left_pressed ||
|
|
|
|
|
a.right_pressed != b.right_pressed ||
|
|
|
|
|
a.up_pressed != b.up_pressed ||
|
|
|
|
|
a.down_pressed != b.down_pressed ||
|
|
|
|
|
a.z_up_pressed != b.z_up_pressed ||
|
|
|
|
|
a.z_down_pressed != b.z_down_pressed ||
|
|
|
|
|
a.speed_fast_pressed != b.speed_fast_pressed ||
|
|
|
|
|
a.speed_slow_pressed != b.speed_slow_pressed) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool operator!=(const jog_state& a, const jog_state& b) {
|
|
|
|
|
return !(a == b);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-28 14:50:58 +03:00
|
|
|
struct machine_listener {
|
|
|
|
|
virtual void on_connected() = 0;
|
|
|
|
|
virtual void on_disconnected() = 0;
|
|
|
|
|
virtual void on_realtime_status_report(realtime_status_report) = 0;
|
|
|
|
|
virtual void on_banner(std::string line) = 0;
|
|
|
|
|
virtual void on_message(std::string message) = 0;
|
|
|
|
|
virtual void on_alarm(int alarm) = 0;
|
2023-05-04 14:15:33 +03:00
|
|
|
virtual void on_init_completed() = 0;
|
2023-04-28 18:54:49 +03:00
|
|
|
virtual void on_run_completed(bool success, size_t failed_index, size_t error) = 0;
|
|
|
|
|
virtual void on_check_completed(bool success, size_t failed_index, size_t error) = 0;
|
2023-05-04 15:57:34 +03:00
|
|
|
virtual void on_settings_reloaded() = 0;
|
|
|
|
|
virtual void on_parameters_reloaded() = 0;
|
2023-04-28 14:50:58 +03:00
|
|
|
};
|
|
|
|
|
|
2023-05-04 14:15:33 +03:00
|
|
|
|
|
|
|
|
enum class init_stage {
|
|
|
|
|
start,
|
|
|
|
|
set_work_pos,
|
|
|
|
|
fetch_settings,
|
|
|
|
|
fetch_parameters
|
|
|
|
|
};
|
|
|
|
|
|
2023-05-04 21:30:22 +03:00
|
|
|
enum class check_stage {
|
|
|
|
|
start,
|
|
|
|
|
enable_check_mode,
|
|
|
|
|
run_program,
|
|
|
|
|
disable_check_mode
|
|
|
|
|
};
|
|
|
|
|
|
2023-05-04 21:48:55 +03:00
|
|
|
enum class run_stage {
|
|
|
|
|
start,
|
|
|
|
|
run_program,
|
|
|
|
|
};
|
|
|
|
|
|
2023-05-04 14:15:33 +03:00
|
|
|
struct machine;
|
|
|
|
|
|
|
|
|
|
struct machine_state {
|
|
|
|
|
virtual void on_connected(machine *m) = 0;
|
|
|
|
|
virtual void on_disconnected(machine *m) = 0;
|
|
|
|
|
virtual void on_enter(machine *m) = 0;
|
|
|
|
|
virtual void on_exit(machine *m) = 0;
|
|
|
|
|
virtual void on_line_received(std::string line) = 0;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct machine_state_connect : public machine_state {
|
|
|
|
|
void on_connected(machine *m) override;
|
|
|
|
|
void on_disconnected(machine *m) override;
|
|
|
|
|
void on_enter(machine *m) override;
|
|
|
|
|
void on_exit(machine *m) override;
|
|
|
|
|
void on_line_received(std::string line) override;
|
|
|
|
|
machine *cnc;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct machine_state_init : public machine_state {
|
|
|
|
|
void on_connected(machine *m) override;
|
|
|
|
|
void on_disconnected(machine *m) override;
|
|
|
|
|
void on_enter(machine *m) override;
|
|
|
|
|
void on_exit(machine *m) override;
|
|
|
|
|
void on_line_received(std::string line) override;
|
|
|
|
|
|
|
|
|
|
init_stage init_state = init_stage::start;
|
|
|
|
|
void move_to_next_init_stage();
|
|
|
|
|
|
|
|
|
|
machine* cnc;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct machine_state_idle : public machine_state {
|
|
|
|
|
void on_connected(machine *m) override;
|
|
|
|
|
void on_disconnected(machine *m) override;
|
|
|
|
|
void on_enter(machine *m) override;
|
|
|
|
|
void on_exit(machine *m) override;
|
|
|
|
|
void on_line_received(std::string line) override;
|
|
|
|
|
machine *cnc;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct machine_state_check_program : public machine_state {
|
|
|
|
|
void on_connected(machine *m) override;
|
|
|
|
|
void on_disconnected(machine *m) override;
|
|
|
|
|
void on_enter(machine *m) override;
|
|
|
|
|
void on_exit(machine *m) override;
|
|
|
|
|
void on_line_received(std::string line) override;
|
2023-05-04 21:30:22 +03:00
|
|
|
|
|
|
|
|
bool continue_program();
|
|
|
|
|
void move_to_next_check_stage();
|
2023-05-04 21:48:55 +03:00
|
|
|
|
|
|
|
|
check_stage stage = check_stage::start;
|
|
|
|
|
machine *cnc;
|
2023-05-04 21:30:22 +03:00
|
|
|
bool check_failed;
|
|
|
|
|
size_t check_error;
|
2023-05-04 14:15:33 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct machine_state_run_program : public machine_state {
|
|
|
|
|
void on_connected(machine *m) override;
|
|
|
|
|
void on_disconnected(machine *m) override;
|
|
|
|
|
void on_enter(machine *m) override;
|
|
|
|
|
void on_exit(machine *m) override;
|
|
|
|
|
void on_line_received(std::string line) override;
|
2023-05-04 21:48:55 +03:00
|
|
|
|
|
|
|
|
bool continue_program();
|
|
|
|
|
void move_to_next_run_stage();
|
|
|
|
|
|
|
|
|
|
machine* cnc;
|
|
|
|
|
run_stage stage = run_stage::start;
|
|
|
|
|
bool run_failed;
|
|
|
|
|
size_t run_error;
|
2023-05-04 14:15:33 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct settings_cmp {
|
|
|
|
|
bool operator()(const std::string& a, const std::string& b) const {
|
|
|
|
|
return std::stoi(a.substr(1)) < std::stoi(b.substr(1));
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct parameters_cmp {
|
|
|
|
|
bool operator()(const std::string& a, const std::string& b) const {
|
|
|
|
|
return a < b;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2023-04-27 23:15:06 +03:00
|
|
|
struct machine : public transport_callbacks {
|
|
|
|
|
machine();
|
|
|
|
|
~machine();
|
|
|
|
|
|
2023-04-28 14:50:58 +03:00
|
|
|
void set_listener(grbl::machine_listener *listener);
|
2023-04-27 23:15:06 +03:00
|
|
|
void connect();
|
2023-05-05 11:33:00 +03:00
|
|
|
void run_program(const grbl::program& pgm, std::string work_offset);
|
2023-04-28 18:42:18 +03:00
|
|
|
void check_program(const grbl::program& pgm);
|
2023-04-27 23:15:06 +03:00
|
|
|
void request_jog(jog_state jog) const;
|
|
|
|
|
void cancel_jog() const;
|
|
|
|
|
|
2023-04-28 18:54:49 +03:00
|
|
|
[[nodiscard]] realtime_status_report get_status() const { return last_report; };
|
2023-05-04 14:15:33 +03:00
|
|
|
const std::map<std::string, std::string, settings_cmp>& get_settings() const;
|
|
|
|
|
const std::map<std::string, std::string, parameters_cmp>& get_parameters() const;
|
2023-04-28 14:50:58 +03:00
|
|
|
|
|
|
|
|
void request_unlock();
|
|
|
|
|
void request_home();
|
|
|
|
|
void request_reset();
|
|
|
|
|
void request_cycle_start();
|
|
|
|
|
void request_feed_hold();
|
|
|
|
|
|
2023-05-04 15:57:34 +03:00
|
|
|
// 0 = G54, 1 = G55, etc
|
|
|
|
|
void zero_offset(int which);
|
|
|
|
|
|
|
|
|
|
// 0 = G54, 1 = G55, etc axis 0=X, 1=Y, 2=Z
|
|
|
|
|
void zero_offset_axis(int offset_index, int axis);
|
2023-05-04 14:15:33 +03:00
|
|
|
|
2023-04-28 14:50:58 +03:00
|
|
|
protected:
|
2023-04-27 23:15:06 +03:00
|
|
|
void on_connected(transport *transport) override;
|
|
|
|
|
void on_disconnected(transport *transport) override;
|
|
|
|
|
void on_line_received(std::string line, transport *transport) override;
|
|
|
|
|
|
2023-05-04 14:15:33 +03:00
|
|
|
void switch_to_state(grbl_machine_state new_state);
|
|
|
|
|
|
|
|
|
|
friend class machine_state_connect;
|
|
|
|
|
friend class machine_state_init;
|
|
|
|
|
friend class machine_state_idle;
|
|
|
|
|
friend class machine_state_check_program;
|
|
|
|
|
friend class machine_state_run_program;
|
|
|
|
|
|
|
|
|
|
std::map<grbl_machine_state, machine_state *> states;
|
|
|
|
|
std::map<std::string, std::string, settings_cmp> settings;
|
|
|
|
|
std::map<std::string, std::string, parameters_cmp> parameters;
|
|
|
|
|
|
2023-05-04 15:57:34 +03:00
|
|
|
volatile size_t awaiting_responses = 0;
|
2023-04-28 14:50:58 +03:00
|
|
|
realtime_status_report last_report{};
|
2023-04-28 16:08:05 +03:00
|
|
|
machine_listener *listener = nullptr;
|
2023-04-27 23:15:06 +03:00
|
|
|
transport *pipe = nullptr;
|
2023-05-04 14:15:33 +03:00
|
|
|
grbl_machine_state state = grbl_machine_state::disconnected;
|
2023-04-27 23:15:06 +03:00
|
|
|
program running_program;
|
|
|
|
|
size_t executed_instructions = 0;
|
2023-04-28 18:42:18 +03:00
|
|
|
void reset_machine_state();
|
2023-04-27 23:15:06 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|