Heightmap probing ... done?

This commit is contained in:
2023-05-09 14:30:39 +03:00
parent 368e5621d7
commit abf1b26ba2
9 changed files with 322 additions and 42 deletions
+150
View File
@@ -16,6 +16,7 @@ grbl::machine::machine() {
states[grbl_machine_state::idle] = new machine_state_idle;
states[grbl_machine_state::check_program] = new machine_state_check_program;
states[grbl_machine_state::run_program] = new machine_state_run_program;
states[grbl_machine_state::heightmap_probing] = new machine_state_heightmap_probing;
switch_to_state(grbl_machine_state::disconnected);
}
@@ -688,6 +689,12 @@ void grbl::machine::start_z_probe(float min_z, float feed_rate) {
awaiting_responses++;
}
void grbl::machine::probe_heightmap(grbl::heightmap& grid) {
std::cout << "probing heightmap" << std::endl;
dynamic_cast<machine_state_heightmap_probing *>(states[grbl_machine_state::heightmap_probing])->grid = &grid;
switch_to_state(grbl_machine_state::heightmap_probing);
}
bool grbl::jog_state::no_jogging() const {
return !(up_pressed || down_pressed || left_pressed || right_pressed || z_up_pressed || z_down_pressed);
}
@@ -1017,3 +1024,146 @@ void grbl::machine_state_run_program::on_line_received(std::string line) {
cnc->listener->on_alarm(std::stoi(line.substr(6)));
}
}
// heightmap probing
void grbl::machine_state_heightmap_probing::on_connected(grbl::machine *m) {
}
void grbl::machine_state_heightmap_probing::on_disconnected(grbl::machine *m) {
}
void grbl::machine_state_heightmap_probing::on_enter(grbl::machine *m) {
cnc = m;
stage = heightmap_probing_stage::start;
failed = false;
error = 0;
probed_locations = 0;
move_to_next_stage();
}
void grbl::machine_state_heightmap_probing::on_exit(grbl::machine *m) {
}
void grbl::machine_state_heightmap_probing::on_line_received(std::string line) {
if (starts_with(line, "ok")) {
if (cnc->awaiting_responses > 0) {
cnc->awaiting_responses--;
}
move_to_next_stage();
} else if (starts_with(line, "error")) {
if (cnc->awaiting_responses > 0) {
cnc->awaiting_responses--;
}
failed = true;
error = std::stoi(line.substr(6));
move_to_next_stage();
} else if (starts_with(line, "Grbl")) {
cnc->listener->on_banner(line);
cnc->reset_machine_state();
} else if (starts_with(line, "<")) {
cnc->last_report = parse_status_report(line, cnc->last_report);
cnc->listener->on_realtime_status_report(cnc->last_report);
} else if (starts_with(line, "[MSG:")) {
cnc->listener->on_message(line.substr(5, line.size() - 6));
} else if (starts_with(line, "ALARM:")) {
cnc->listener->on_alarm(std::stoi(line.substr(6)));
} else if (starts_with(line, "[PRB")) {
std::cout << "received PRB" << std::endl;
line = line.substr(1, line.size() - 2);
auto pieces = split_string(line, ":");
cnc->parameters[pieces[0]] = pieces[1] + ":" + pieces[2];
}
}
bool grbl::machine_state_heightmap_probing::continue_program() {
return false;
}
void grbl::machine_state_heightmap_probing::move_to_next_stage() {
switch (stage) {
case heightmap_probing_stage::start:
cnc->pipe->send("G0X0Y0Z15");
stage = heightmap_probing_stage::goto_home;
break;
case heightmap_probing_stage::goto_home:
cnc->pipe->send("G38.2 Z-65 F100");
stage = heightmap_probing_stage::initial_probe_step_back;
break;
case heightmap_probing_stage::initial_probe_step_back:
// step back a bit (1mm, relative)
cnc->pipe->send("G91 G0 Z1");
stage = heightmap_probing_stage::initial_probe_fine_seek;
break;
case heightmap_probing_stage::initial_probe_fine_seek:
// probe again but finer
cnc->pipe->send(" G38.2 Z-5 F5");
stage = heightmap_probing_stage::initial_probe;
break;
case heightmap_probing_stage::initial_probe:
std::cout << "Initial probe: " << cnc->parameters["PRB"];
std::cout << ". Setting this as new zero" << std::endl;
cnc->pipe->send("G10 L20 P1 Z0");
{
auto pieces = split_string(cnc->parameters["PRB"], ":");
auto axes = split_string(pieces[0], ",");
z_zero_in_mpos = std::stof(axes[2]);
std::cout << "Z zero in mpos: " << z_zero_in_mpos << std::endl;
grid->vertices[probed_locations].z = 0;
}
stage = heightmap_probing_stage::goto_next_location;
break;
case heightmap_probing_stage::goto_next_location: {
auto pieces = split_string(cnc->parameters["PRB"], ":");
auto axes = split_string(pieces[0], ",");
auto current_z = std::stof(axes[2]);
if (probed_locations == 0) {
z_zero_in_mpos = current_z;
std::cout << "Z zero in mpos: " << z_zero_in_mpos << std::endl;
grid->vertices[probed_locations].z = 0;
} else {
auto delta_z = current_z - z_zero_in_mpos;
std::cout << "Z[" << probed_locations << "] = " << delta_z << std::endl;
grid->vertices[probed_locations].z = delta_z;
cnc->listener->on_heightmap_probe_acquired(grid);
}
}
probed_locations++;
if (probed_locations == grid->vertices.size()) {
cnc->pipe->send("G0Z15"); // safe height
stage = heightmap_probing_stage::done;
} else {
cnc->pipe->send("G90 G0 Z1.5");
stage = heightmap_probing_stage::goto_next_location_move;
}
break;
case heightmap_probing_stage::goto_next_location_move:
cnc->pipe->send("G0 X" + std::to_string(grid->vertices[probed_locations].x) + " Y" +
std::to_string(grid->vertices[probed_locations].y));
stage = heightmap_probing_stage::goto_start_probing_at;
break;
case heightmap_probing_stage::goto_start_probing_at:
cnc->pipe->send("G0 Z0.5"); // this appears to move Z upwards instead of downwards. why?
// faking the G0 Z0.5
// cnc->pipe->send("G91 G0 Z-1"); // 1.5 - 1 = 0.5.//
stage = heightmap_probing_stage::probing;
break;
case heightmap_probing_stage::probing:
cnc->pipe->send("G38.2 Z-5 F5");
stage = heightmap_probing_stage::goto_next_location;
break;
case heightmap_probing_stage::done:
cnc->switch_to_state(grbl_machine_state::idle);
break;
default:
break;
}
}