Skip to content

Commit

Permalink
Grbl: Send jog commands instead of G0
Browse files Browse the repository at this point in the history
  • Loading branch information
Lyrkan committed May 15, 2024
1 parent adcffb1 commit 4fa284b
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 50 deletions.
9 changes: 4 additions & 5 deletions include/Grbl/grbl_serial.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,13 @@ typedef enum {
} GrblAxis;

typedef struct {
GrblMoveMode move_mode;
float feed_rate;
uint8_t axis_flags;
float x;
float y;
float z;
} GrblMoveCoordinates;
} GrblMoveCommand;

typedef struct {
void (*on_success)() = NULL;
Expand All @@ -76,9 +78,6 @@ bool grbl_send_message(
GrblCommandCallbacks callbacks = GrblCommandCallbacks());
bool grbl_send_init_commands();
bool grbl_send_home_command(uint8_t axis_flags, GrblCommandCallbacks callbacks = GrblCommandCallbacks());
bool grbl_send_move_command(
GrblMoveCoordinates target,
GrblMoveMode mode = GRBL_MOVE_MODE_UNDEFINED,
GrblCommandCallbacks callbacks = GrblCommandCallbacks());
bool grbl_send_move_command(GrblMoveCommand command, GrblCommandCallbacks callbacks = GrblCommandCallbacks());

#endif
7 changes: 7 additions & 0 deletions include/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ typedef enum {
SETTINGS_TYPE_BED = 0x01,
SETTINGS_TYPE_PROBES = 0x02,
SETTINGS_TYPE_OTA = 0x04,
SETTINGS_TYPE_GRBL = 0x08,
} SettingsType;

typedef struct {
Expand All @@ -37,13 +38,19 @@ typedef struct {
char password[250];
} OTASettings;

typedef struct {
float jog_speed;
} GrblSettings;

extern SemaphoreHandle_t bed_settings_mutex;
extern SemaphoreHandle_t probes_settings_mutex;
extern SemaphoreHandle_t ota_settings_mutex;
extern SemaphoreHandle_t grbl_settings_mutex;

extern BedSettings bed_settings;
extern ProbesSettings probes_settings;
extern OTASettings ota_settings;
extern GrblSettings grbl_settings;

void settings_init();
void settings_schedule_save(uint32_t settings_types);
Expand Down
71 changes: 33 additions & 38 deletions src/Grbl/grbl_serial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -343,49 +343,44 @@ bool grbl_send_home_command(uint8_t axis_flags, GrblCommandCallbacks callbacks)
return grbl_send_message(buffer, false, GRBL_ACK_HOMING_TIMEOUT_MS, callbacks);
}

bool grbl_send_move_command(GrblMoveCoordinates target, GrblMoveMode mode, GrblCommandCallbacks callbacks) {
bool grbl_send_move_command(GrblMoveCommand command, GrblCommandCallbacks callbacks) {
log_i(
"Sending a%s move command for axis %s%s%s with coordinates (%.2f, %.2f, %.2f)",
mode == GRBL_MOVE_MODE_ABSOLUTE ? "n absolute"
: (mode == GRBL_MOVE_MODE_RELATIVE ? " relative" : "n undefined"),
(target.axis_flags & GRBL_AXIS_X) != 0 ? "X" : "",
(target.axis_flags & GRBL_AXIS_Y) != 0 ? "Y" : "",
(target.axis_flags & GRBL_AXIS_Z) != 0 ? "Z" : "",
(target.axis_flags & GRBL_AXIS_X) != 0 ? target.x : 0,
(target.axis_flags & GRBL_AXIS_Y) != 0 ? target.y : 0,
(target.axis_flags & GRBL_AXIS_Z) != 0 ? target.z : 0);

if (mode == GRBL_MOVE_MODE_ABSOLUTE || mode == GRBL_MOVE_MODE_RELATIVE) {
bool switch_mode_scheduled = false;
switch (mode) {
case GRBL_MOVE_MODE_ABSOLUTE:
if (!grbl_send_message("G90")) {
if (callbacks.on_failure != NULL) {
callbacks.on_failure();
}
return false;
}
break;
case GRBL_MOVE_MODE_RELATIVE:
if (!grbl_send_message("G91")) {
if (callbacks.on_failure != NULL) {
callbacks.on_failure();
}
return false;
}
break;
}
"Sending a%s move command for axis %s%s%s with coordinates (%.2f, %.2f, %.2f) at %.2fmm/s",
command.move_mode == GRBL_MOVE_MODE_ABSOLUTE
? "n absolute"
: (command.move_mode == GRBL_MOVE_MODE_RELATIVE ? " relative" : "n undefined"),
(command.axis_flags & GRBL_AXIS_X) != 0 ? "X" : "",
(command.axis_flags & GRBL_AXIS_Y) != 0 ? "Y" : "",
(command.axis_flags & GRBL_AXIS_Z) != 0 ? "Z" : "",
(command.axis_flags & GRBL_AXIS_X) != 0 ? command.x : 0,
(command.axis_flags & GRBL_AXIS_Y) != 0 ? command.y : 0,
(command.axis_flags & GRBL_AXIS_Z) != 0 ? command.z : 0,
command.feed_rate);

char buffer[ARRAY_SIZE("$J=G21 G90 F0000.0 X000.00 Y000.00 Z000.00")] = "$J=G21\0";

// Set absolute/relative mode
switch (command.move_mode) {
case GRBL_MOVE_MODE_ABSOLUTE:
snprintf(buffer, ARRAY_SIZE(buffer), "%s G90");
break;
case GRBL_MOVE_MODE_RELATIVE:
snprintf(buffer, ARRAY_SIZE(buffer), "%s G91");
break;
}

char buffer[ARRAY_SIZE("G0 X000.00 Y000.00 Z000.00")] = "G0\0";
if ((target.axis_flags & GRBL_AXIS_X) != 0) {
snprintf(buffer, ARRAY_SIZE(buffer), "%s X%.2f", buffer, target.x);
// Set feed rate
snprintf(buffer, ARRAY_SIZE(buffer), "%s F%.1f", buffer, command.feed_rate);

// Set target
if ((command.axis_flags & GRBL_AXIS_X) != 0) {
snprintf(buffer, ARRAY_SIZE(buffer), "%s X%.2f", buffer, command.x);
}
if ((target.axis_flags & GRBL_AXIS_Y) != 0) {
snprintf(buffer, ARRAY_SIZE(buffer), "%s Y%.2f", buffer, target.y);
if ((command.axis_flags & GRBL_AXIS_Y) != 0) {
snprintf(buffer, ARRAY_SIZE(buffer), "%s Y%.2f", buffer, command.y);
}
if ((target.axis_flags & GRBL_AXIS_Z) != 0) {
snprintf(buffer, ARRAY_SIZE(buffer), "%s Z%.2f", buffer, target.z);
if ((command.axis_flags & GRBL_AXIS_Z) != 0) {
snprintf(buffer, ARRAY_SIZE(buffer), "%s Z%.2f", buffer, command.z);
}

return grbl_send_message(buffer, false, GRBL_ACK_DEFAULT_TIMEOUT_MS, callbacks);
Expand Down
22 changes: 16 additions & 6 deletions src/UI/screens/controls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "macros.h"
#include "mutex.h"
#include "queues.h"
#include "settings.h"

lv_obj_t *ui_controls_screen;

Expand Down Expand Up @@ -82,22 +83,31 @@ static void ui_controls_btnmatrix_handler(lv_event_t *e) {
grbl_send_home_command(GRBL_AXIS_Y, grbl_command_callbacks);
}
} else { // Relative move
TAKE_MUTEX(grbl_settings_mutex);

GrblMoveCommand move_command = {
.move_mode = GRBL_MOVE_MODE_RELATIVE,
.feed_rate = grbl_settings.jog_speed,
.axis_flags = 0,
};

RELEASE_MUTEX(grbl_settings_mutex);

float_t move_offset = pow10(abs((int)(btn_id - 3)) - 1) * (btn_id < 3 ? -1 : 1);
GrblMoveCoordinates move_target = {.axis_flags = 0};
if (event_target == ui_controls_laser_move_x_matrix) {
move_target.axis_flags = GRBL_AXIS_X;
move_target.x = move_offset;
move_command.axis_flags = GRBL_AXIS_X;
move_command.x = move_offset;
} else if (event_target == ui_controls_laser_move_y_matrix) {
move_target.axis_flags = GRBL_AXIS_Y;
move_target.y = move_offset;
move_command.axis_flags = GRBL_AXIS_Y;
move_command.y = move_offset;
}

grbl_command_callbacks.on_failure = []() -> void {
ui_overlay_add_flash_message(FLASH_LEVEL_DANGER, "Move command failed or timed out");
};

ui_controls_lock_grbl_controls();
grbl_send_move_command(move_target, GRBL_MOVE_MODE_RELATIVE, grbl_command_callbacks);
grbl_send_move_command(move_command, grbl_command_callbacks);
}
}
}
Expand Down
43 changes: 43 additions & 0 deletions src/UI/screens/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ static lv_obj_t *ui_settings_wifi_page;
static lv_obj_t *ui_settings_bed_page;
static lv_obj_t *ui_settings_probes_page;
static lv_obj_t *ui_settings_ota_page;
static lv_obj_t *ui_settings_grbl_page;
static lv_obj_t *ui_settings_alarm_page;
static lv_obj_t *ui_settings_interlock_page;

Expand Down Expand Up @@ -49,6 +50,8 @@ static lv_obj_t *ui_settings_probes_cooling_temp_max_value;
static lv_obj_t *ui_settings_ota_login_value;
static lv_obj_t *ui_settings_ota_password_value;

static lv_obj_t *ui_settings_grbl_jog_speed_value;

static lv_obj_t *ui_settings_alarm_enable_when_running_value;
static lv_obj_t *ui_settings_alarm_enable_when_not_idling_value;
static lv_obj_t *ui_settings_alarm_enable_when_flame_sensor_triggered_value;
Expand Down Expand Up @@ -179,6 +182,31 @@ static void ui_settings_save_ota_settings() {
RELEASE_MUTEX(ota_settings_mutex)
}

static void ui_settings_load_grbl_settings() {
// Acquire GRBL settings mutex
TAKE_MUTEX(grbl_settings_mutex)

static char jog_speed_text[10];

snprintf(jog_speed_text, ARRAY_SIZE(jog_speed_text), "%.1f", grbl_settings.jog_speed);

lv_textarea_set_text(ui_settings_grbl_jog_speed_value, jog_speed_text);

// Release GRBL settings mutex
RELEASE_MUTEX(grbl_settings_mutex)
}

static void ui_settings_save_grbl_settings() {
// Acquire GRBL settings mutex
TAKE_MUTEX(grbl_settings_mutex)

grbl_settings.jog_speed = static_cast<float_t>(atof(lv_textarea_get_text(ui_settings_grbl_jog_speed_value)));
settings_schedule_save(SETTINGS_TYPE_GRBL);

// Release GRBL settings mutex
RELEASE_MUTEX(grbl_settings_mutex)
}

static void ui_settings_wifi_buttons_handler(lv_event_t *e) {
lv_event_code_t event_code = lv_event_get_code(e);
if (event_code != LV_EVENT_CLICKED) {
Expand Down Expand Up @@ -250,6 +278,8 @@ static void ui_settings_field_value_changed_handler(lv_event_t *e) {
ui_settings_save_probes_settings();
} else if (target == ui_settings_ota_login_value || target == ui_settings_ota_password_value) {
ui_settings_save_ota_settings();
} else if (target == ui_settings_grbl_jog_speed_value) {
ui_settings_save_grbl_settings();
}
}
}
Expand Down Expand Up @@ -322,13 +352,15 @@ static void ui_settings_init_screen_content() {
char bed_page_name[] = "Bed";
char probes_page_name[] = "Probes";
char ota_page_name[] = "OTA Updates";
char grbl_page_name[] = "GRBL";
char alarm_page_name[] = "Alarm behavior";
char interlock_page_name[] = "Interlock behavior";

ui_settings_wifi_page = lv_menu_page_create(ui_settings_menu, wifi_page_name);
ui_settings_bed_page = lv_menu_page_create(ui_settings_menu, bed_page_name);
ui_settings_probes_page = lv_menu_page_create(ui_settings_menu, probes_page_name);
ui_settings_ota_page = lv_menu_page_create(ui_settings_menu, ota_page_name);
ui_settings_grbl_page = lv_menu_page_create(ui_settings_menu, grbl_page_name);
ui_settings_alarm_page = lv_menu_page_create(ui_settings_menu, alarm_page_name);
ui_settings_interlock_page = lv_menu_page_create(ui_settings_menu, interlock_page_name);

Expand All @@ -341,6 +373,8 @@ static void ui_settings_init_screen_content() {
lv_obj_clear_flag(ui_settings_probes_page, LV_OBJ_FLAG_SCROLL_ELASTIC);
lv_obj_clear_flag(ui_settings_ota_page, LV_OBJ_FLAG_SCROLL_MOMENTUM);
lv_obj_clear_flag(ui_settings_ota_page, LV_OBJ_FLAG_SCROLL_ELASTIC);
lv_obj_clear_flag(ui_settings_grbl_page, LV_OBJ_FLAG_SCROLL_MOMENTUM);
lv_obj_clear_flag(ui_settings_grbl_page, LV_OBJ_FLAG_SCROLL_ELASTIC);
lv_obj_clear_flag(ui_settings_alarm_page, LV_OBJ_FLAG_SCROLL_MOMENTUM);
lv_obj_clear_flag(ui_settings_alarm_page, LV_OBJ_FLAG_SCROLL_ELASTIC);
lv_obj_clear_flag(ui_settings_interlock_page, LV_OBJ_FLAG_SCROLL_MOMENTUM);
Expand Down Expand Up @@ -372,6 +406,11 @@ static void ui_settings_init_screen_content() {
lv_label_set_text(item_label, LV_SYMBOL_CODE_PULL_REQUEST " OTA Updates");
lv_menu_set_load_page_event(ui_settings_menu, item_cont, ui_settings_ota_page);

item_cont = lv_menu_cont_create(ui_settings_root_page);
item_label = lv_label_create(item_cont);
lv_label_set_text(item_label, LV_SYMBOL_ARROWS_TO_DOT " GRBL");
lv_menu_set_load_page_event(ui_settings_menu, item_cont, ui_settings_grbl_page);

item_cont = lv_menu_cont_create(ui_settings_root_page);
item_label = lv_label_create(item_cont);
lv_label_set_text(item_label, LV_SYMBOL_WARNING " Alarm behavior");
Expand Down Expand Up @@ -483,6 +522,9 @@ static void ui_settings_init_screen_content() {
ui_settings_ota_login_value = ui_settings_create_textarea_field(ui_settings_ota_page, "Login");
ui_settings_ota_password_value = ui_settings_create_textarea_field(ui_settings_ota_page, "Password");

// GRBL page
ui_settings_grbl_jog_speed_value = ui_settings_create_textarea_field(ui_settings_grbl_page, "Jog speed (mm/s)");

// Alarm page
ui_settings_alarm_enable_when_running_value =
ui_settings_create_checkbox_field(ui_settings_alarm_page, "Enable alarm when Grbl status is 'Running'");
Expand Down Expand Up @@ -510,6 +552,7 @@ static void ui_settings_init_screen_content() {
ui_settings_load_bed_settings();
ui_settings_load_probes_settings();
ui_settings_load_ota_settings();
ui_settings_load_grbl_settings();
settings_loaded = true;

// Force the first update
Expand Down
42 changes: 41 additions & 1 deletion src/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
static const char PREFERENCES_NAMESPACE_BED[] = "bed-settings";
static const char PREFERENCES_NAMESPACE_PROBES[] = "probes-settings";
static const char PREFERENCES_NAMESPACE_OTA[] = "ota-settings";
static const char PREFERENCES_NAMESPACE_GRBL[] = "grbl-settings";

static const char PREFERENCES_KEY_BED_SCREW_LEAD[] = "screw-lead-um";
static const char PREFERENCES_KEY_BED_MICROSTEP_MULTIPLIER[] = "microstep-mul";
Expand All @@ -27,11 +28,14 @@ static const char PREFERENCES_KEY_PROBES_COOLING_TEMP_MAX[] = "cool-temp-max";
static const char PREFERENCES_KEY_OTA_LOGIN[] = "login";
static const char PREFERENCES_KEY_OTA_PASSWORD[] = "password";

static const char PREFERENCES_KEY_GRBL_JOG_SPEED[] = "jog-speed";

static Preferences preferences;

SemaphoreHandle_t bed_settings_mutex = xSemaphoreCreateMutex();
SemaphoreHandle_t probes_settings_mutex = xSemaphoreCreateMutex();
SemaphoreHandle_t ota_settings_mutex = xSemaphoreCreateMutex();
SemaphoreHandle_t grbl_settings_mutex = xSemaphoreCreateMutex();

BedSettings bed_settings = {
.screw_lead_um = 8000,
Expand All @@ -54,6 +58,10 @@ OTASettings ota_settings = {
.password = {0},
};

GrblSettings grbl_settings = {
.jog_speed = 100.0,
};

static void settings_save_task_func(void *params) {
uint32_t settings_types;

Expand Down Expand Up @@ -116,6 +124,20 @@ static void settings_save_task_func(void *params) {
RELEASE_MUTEX(ota_settings_mutex)
}

if ((settings_types & SETTINGS_TYPE_GRBL) != 0) {
log_i("GRBL settings have changed, saving new values...");

// Acquire GRBL settings lock
TAKE_MUTEX(grbl_settings_mutex)

preferences.begin(PREFERENCES_NAMESPACE_GRBL, false);
preferences.putFloat(PREFERENCES_KEY_GRBL_JOG_SPEED, grbl_settings.jog_speed);
preferences.end();

// Release OTA settings lock
RELEASE_MUTEX(grbl_settings_mutex)
}

// Wait a little bit before the next check
vTaskDelay(pdMS_TO_TICKS(SETTINGS_UPDATE_INTERVAL));
}
Expand Down Expand Up @@ -201,9 +223,27 @@ void settings_init() {
log_d(" login: %s", ota_settings.login);
log_d(" password: %s", ota_settings.password);

// Release probes settings lock
// Release OTA settings lock
RELEASE_MUTEX(ota_settings_mutex)

// Acquire GRBL settings lock
TAKE_MUTEX(grbl_settings_mutex)
log_i("Loading GRBL settings... ");
preferences.begin(PREFERENCES_NAMESPACE_GRBL, true);

// clang-format off
grbl_settings = {
.jog_speed = preferences.getFloat(PREFERENCES_KEY_GRBL_JOG_SPEED, grbl_settings.jog_speed),
};
// clang-format on

preferences.end();

log_d(" jog speed: %.1f", grbl_settings.jog_speed);

// Release GRBL settings lock
RELEASE_MUTEX(grbl_settings_mutex)

// Start saving task
xTaskCreatePinnedToCore(
settings_save_task_func,
Expand Down

0 comments on commit 4fa284b

Please sign in to comment.