Emit program stop events for LinuxCNC stops

This commit is contained in:
CNC Local
2026-05-22 05:21:48 +08:00
parent bd04af8d8e
commit db4a175944
10 changed files with 136 additions and 0 deletions

View File

@@ -192,6 +192,12 @@ void CanonEventSink::program_end(int line) {
emit(base_event(CNC_SIM_EVENT_PROGRAM_END, line));
}
void CanonEventSink::program_stop(int line, int kind) {
CncSimEvent event = base_event(CNC_SIM_EVENT_PROGRAM_END, line);
event.reserved = kind;
emit(event);
}
void CanonEventSink::emit_raw(CncSimEvent event) {
if (event.version == 0) {
event.version = 1;

View File

@@ -32,6 +32,7 @@ public:
void straight_probe(int line, const CncSimPose &end);
void dwell(double seconds, int line);
void program_end(int line);
void program_stop(int line, int kind);
void emit_raw(CncSimEvent event);
const CncSimPose &position() const;

View File

@@ -471,6 +471,9 @@ void MIST_ON() {
}
void PALLET_SHUTTLE() {
if (active_sink) {
active_sink->program_stop(event_line(), 3);
}
}
void TURN_PROBE_OFF() {
@@ -499,6 +502,9 @@ bool GET_BLOCK_DELETE(void) {
}
void OPTIONAL_PROGRAM_STOP() {
if (active_sink) {
active_sink->program_stop(event_line(), 2);
}
}
void SET_OPTIONAL_PROGRAM_STOP(bool) {
@@ -509,6 +515,9 @@ bool GET_OPTIONAL_PROGRAM_STOP() {
}
void PROGRAM_STOP() {
if (active_sink) {
active_sink->program_stop(event_line(), 1);
}
}
void SET_MOTION_OUTPUT_BIT(int) {

View File

@@ -351,6 +351,16 @@ int SmokeGcodeParser::parse(const char *program, size_t program_len, std::string
if (stop_if_callback_aborted(sink_, error)) {
return -1;
}
} else if (m == 0) {
sink_.program_stop(line_number, 1);
if (stop_if_callback_aborted(sink_, error)) {
return -1;
}
} else if (m == 1) {
sink_.program_stop(line_number, 2);
if (stop_if_callback_aborted(sink_, error)) {
return -1;
}
} else if (m == 6) {
sink_.change_tool(line_number);
if (stop_if_callback_aborted(sink_, error)) {
@@ -371,6 +381,11 @@ int SmokeGcodeParser::parse(const char *program, size_t program_len, std::string
if (stop_if_callback_aborted(sink_, error)) {
return -1;
}
} else if (m == 60) {
sink_.program_stop(line_number, 3);
if (stop_if_callback_aborted(sink_, error)) {
return -1;
}
} else if (m == 2 || m == 30) {
sink_.program_end(line_number);
if (stop_if_callback_aborted(sink_, error)) {

View File

@@ -78,6 +78,10 @@ int main() {
bool saw_spindle_stop = false;
bool saw_comment_line = false;
bool saw_tool_number_line = false;
bool saw_program_stop = false;
bool saw_optional_stop = false;
bool saw_pallet_stop = false;
bool saw_final_end = false;
for (const auto &event : events) {
saw_tool = saw_tool || event.type == CNC_SIM_EVENT_TOOL_CHANGE;
saw_rapid = saw_rapid || event.type == CNC_SIM_EVENT_RAPID;
@@ -171,6 +175,38 @@ int main() {
}
ok &= expect(saw_tool_number_line, "expected LinuxCNC M61 tool-number event line");
const char stop_program[] =
"G21 G90 G17\n"
"M0\n"
"M1\n"
"M60\n"
"M30\n";
events.clear();
ok &= expect(cnc_sim_parse_program(sim, stop_program, sizeof(stop_program) - 1) == 0,
cnc_sim_last_error(sim));
for (const auto &event : events) {
saw_program_stop = saw_program_stop ||
(event.type == CNC_SIM_EVENT_PROGRAM_END &&
event.line == 2 &&
event.reserved == 1);
saw_optional_stop = saw_optional_stop ||
(event.type == CNC_SIM_EVENT_PROGRAM_END &&
event.line == 3 &&
event.reserved == 2);
saw_pallet_stop = saw_pallet_stop ||
(event.type == CNC_SIM_EVENT_PROGRAM_END &&
event.line == 4 &&
event.reserved == 3);
saw_final_end = saw_final_end ||
(event.type == CNC_SIM_EVENT_PROGRAM_END &&
event.line == 5 &&
event.reserved == 0);
}
ok &= expect(saw_program_stop, "expected LinuxCNC M0 program stop event");
ok &= expect(saw_optional_stop, "expected LinuxCNC M1 optional stop event");
ok &= expect(saw_pallet_stop, "expected LinuxCNC M60 pallet shuttle event");
ok &= expect(saw_final_end, "expected LinuxCNC M30 final program end event");
const char comment_program[] =
"G21 G90 G17\n"
"(operator note)\n"

View File

@@ -70,6 +70,9 @@ int main() {
bool saw_spindle_cw = false;
bool saw_spindle_ccw = false;
bool saw_spindle_stop = false;
bool saw_program_stop = false;
bool saw_optional_stop = false;
bool saw_pallet_stop = false;
bool saw_incremental_move = false;
bool saw_r_arc_center = false;
for (const auto &event : events) {
@@ -162,6 +165,32 @@ int main() {
ok &= expect(saw_spindle_ccw, "expected M4 counterclockwise spindle state");
ok &= expect(saw_spindle_stop, "expected M5 stopped spindle state");
const char stop_program[] =
"G21\n"
"M0\n"
"M1\n"
"M60\n";
events.clear();
ok &= expect(cnc_sim_parse_program(sim, stop_program, sizeof(stop_program) - 1) == 0,
cnc_sim_last_error(sim));
for (const auto &event : events) {
saw_program_stop = saw_program_stop ||
(event.type == CNC_SIM_EVENT_PROGRAM_END &&
event.line == 2 &&
event.reserved == 1);
saw_optional_stop = saw_optional_stop ||
(event.type == CNC_SIM_EVENT_PROGRAM_END &&
event.line == 3 &&
event.reserved == 2);
saw_pallet_stop = saw_pallet_stop ||
(event.type == CNC_SIM_EVENT_PROGRAM_END &&
event.line == 4 &&
event.reserved == 3);
}
ok &= expect(saw_program_stop, "expected M0 program stop event");
ok &= expect(saw_optional_stop, "expected M1 optional stop event");
ok &= expect(saw_pallet_stop, "expected M60 pallet shuttle event");
const char linuxcnc_config[] = "{\"backend\":\"linuxcnc-rs274\"}";
config_rc = cnc_sim_load_config_json(sim, linuxcnc_config, sizeof(linuxcnc_config) - 1);
rc = cnc_sim_parse_program(sim, program, sizeof(program) - 1);

View File

@@ -51,6 +51,12 @@ int main() {
SET_XY_ROTATION(15.0);
cnc_sim_linuxcnc_set_current_line(20);
COMMENT("bridge comment");
cnc_sim_linuxcnc_set_current_line(21);
PROGRAM_STOP();
cnc_sim_linuxcnc_set_current_line(22);
OPTIONAL_PROGRAM_STOP();
cnc_sim_linuxcnc_set_current_line(23);
PALLET_SHUTTLE();
PROGRAM_END();
bool ok = true;
@@ -71,6 +77,9 @@ int main() {
bool saw_g92 = false;
bool saw_rotation = false;
bool saw_comment = false;
bool saw_program_stop = false;
bool saw_optional_stop = false;
bool saw_pallet_stop = false;
bool saw_end = false;
for (const auto &event : events) {
saw_tool = saw_tool || (event.type == CNC_SIM_EVENT_TOOL_CHANGE && event.tool == 7);
@@ -113,6 +122,15 @@ int main() {
event.feed == 15.0);
saw_comment = saw_comment || (event.type == CNC_SIM_EVENT_COMMENT &&
event.line == 20);
saw_program_stop = saw_program_stop || (event.type == CNC_SIM_EVENT_PROGRAM_END &&
event.line == 21 &&
event.reserved == 1);
saw_optional_stop = saw_optional_stop || (event.type == CNC_SIM_EVENT_PROGRAM_END &&
event.line == 22 &&
event.reserved == 2);
saw_pallet_stop = saw_pallet_stop || (event.type == CNC_SIM_EVENT_PROGRAM_END &&
event.line == 23 &&
event.reserved == 3);
saw_end = saw_end || event.type == CNC_SIM_EVENT_PROGRAM_END;
}
@@ -129,6 +147,9 @@ int main() {
ok &= expect(saw_g92, "expected G92 offset event");
ok &= expect(saw_rotation, "expected XY rotation event");
ok &= expect(saw_comment, "expected comment event line");
ok &= expect(saw_program_stop, "expected program stop event");
ok &= expect(saw_optional_stop, "expected optional stop event");
ok &= expect(saw_pallet_stop, "expected pallet shuttle stop event");
ok &= expect(saw_end, "expected program end");
cnc_sim_linuxcnc_set_canon_sink(nullptr);