Propagate LinuxCNC canon event line numbers

This commit is contained in:
CNC Local
2026-05-22 04:48:37 +08:00
parent 55d9fff9f0
commit bf7192cc07
7 changed files with 42 additions and 15 deletions

View File

@@ -14,6 +14,7 @@
namespace {
CanonEventSink *active_sink = nullptr;
int active_line = 0;
std::string parameter_file_name = "rs274ngc.var";
CncSimPose make_pose(double x, double y, double z,
@@ -81,17 +82,26 @@ void trace_call(const char *name) {
}
}
int event_line(int lineno = 0) {
return lineno > 0 ? lineno : active_line;
}
} // namespace
void cnc_sim_linuxcnc_set_canon_sink(CanonEventSink *sink) {
trace_call("cnc_sim_linuxcnc_set_canon_sink");
active_sink = sink;
active_line = 0;
}
CanonEventSink *cnc_sim_linuxcnc_get_canon_sink() {
return active_sink;
}
void cnc_sim_linuxcnc_set_current_line(int line) {
active_line = line > 0 ? line : 0;
}
void INIT_CANON() {
trace_call("INIT_CANON");
if (active_sink) {
@@ -102,28 +112,28 @@ void INIT_CANON() {
void USE_LENGTH_UNITS(CANON_UNITS units) {
trace_call("USE_LENGTH_UNITS");
if (active_sink) {
active_sink->use_length_units(units_to_scale(units), 0);
active_sink->use_length_units(units_to_scale(units), event_line());
}
}
void SELECT_PLANE(CANON_PLANE plane) {
trace_call("SELECT_PLANE");
if (active_sink) {
active_sink->select_plane(plane_to_g_code(plane), 0);
active_sink->select_plane(plane_to_g_code(plane), event_line());
}
}
void SET_FEED_RATE(double rate) {
trace_call("SET_FEED_RATE");
if (active_sink) {
active_sink->set_feed_rate(rate, 0);
active_sink->set_feed_rate(rate, event_line());
}
}
void SET_SPINDLE_SPEED(int, double speed) {
trace_call("SET_SPINDLE_SPEED");
if (active_sink) {
active_sink->set_spindle_speed(speed, 0);
active_sink->set_spindle_speed(speed, event_line());
}
}
@@ -137,7 +147,7 @@ void SELECT_TOOL(int tool) {
void CHANGE_TOOL() {
trace_call("CHANGE_TOOL");
if (active_sink) {
active_sink->change_tool(0);
active_sink->change_tool(event_line());
}
}
@@ -147,7 +157,7 @@ void STRAIGHT_TRAVERSE(int lineno,
double u, double v, double w) {
trace_call("STRAIGHT_TRAVERSE");
if (active_sink) {
active_sink->straight_traverse(lineno, make_pose(x, y, z, a, b, c, u, v, w));
active_sink->straight_traverse(event_line(lineno), make_pose(x, y, z, a, b, c, u, v, w));
}
}
@@ -157,7 +167,7 @@ void STRAIGHT_FEED(int lineno,
double u, double v, double w) {
trace_call("STRAIGHT_FEED");
if (active_sink) {
active_sink->straight_feed(lineno, make_pose(x, y, z, a, b, c, u, v, w));
active_sink->straight_feed(event_line(lineno), make_pose(x, y, z, a, b, c, u, v, w));
}
}
@@ -205,20 +215,20 @@ void ARC_FEED(int lineno,
end.u = u;
end.v = v;
end.w = w;
active_sink->arc_feed(lineno, end, center, rotation);
active_sink->arc_feed(event_line(lineno), end, center, rotation);
}
void DWELL(double seconds) {
trace_call("DWELL");
if (active_sink) {
active_sink->dwell(seconds, 0);
active_sink->dwell(seconds, event_line());
}
}
void PROGRAM_END() {
trace_call("PROGRAM_END");
if (active_sink) {
active_sink->program_end(0);
active_sink->program_end(event_line());
}
}
@@ -232,7 +242,7 @@ void SET_G5X_OFFSET(int index,
double u, double v, double w) {
trace_call("SET_G5X_OFFSET");
if (active_sink) {
active_sink->set_g5x_offset(index, make_pose(x, y, z, a, b, c, u, v, w), 0);
active_sink->set_g5x_offset(index, make_pose(x, y, z, a, b, c, u, v, w), event_line());
}
}
@@ -241,14 +251,14 @@ void SET_G92_OFFSET(double x, double y, double z,
double u, double v, double w) {
trace_call("SET_G92_OFFSET");
if (active_sink) {
active_sink->set_g92_offset(make_pose(x, y, z, a, b, c, u, v, w), 0);
active_sink->set_g92_offset(make_pose(x, y, z, a, b, c, u, v, w), event_line());
}
}
void SET_XY_ROTATION(double angle) {
trace_call("SET_XY_ROTATION");
if (active_sink) {
active_sink->set_xy_rotation(angle, 0);
active_sink->set_xy_rotation(angle, event_line());
}
}
@@ -257,7 +267,7 @@ void CANON_UPDATE_END_POINT(double x, double y, double z,
double u, double v, double w) {
trace_call("CANON_UPDATE_END_POINT");
if (active_sink) {
active_sink->straight_traverse(0, make_pose(x, y, z, a, b, c, u, v, w));
active_sink->straight_traverse(event_line(), make_pose(x, y, z, a, b, c, u, v, w));
}
}

View File

@@ -4,4 +4,4 @@ class CanonEventSink;
void cnc_sim_linuxcnc_set_canon_sink(CanonEventSink *sink);
CanonEventSink *cnc_sim_linuxcnc_get_canon_sink();
void cnc_sim_linuxcnc_set_current_line(int line);

View File

@@ -126,6 +126,7 @@ int parse_linuxcnc_rs274_backend(CanonEventSink &sink,
int line_number = 0;
while (!program_done && std::getline(input, line)) {
++line_number;
cnc_sim_linuxcnc_set_current_line(line_number);
std::vector<SimulatorGcodeControlAction> control_actions;
if (parse_simulator_gcode_control_line(line, &control_actions)) {
for (const auto &action : control_actions) {
@@ -172,6 +173,7 @@ int parse_linuxcnc_rs274_backend(CanonEventSink &sink,
}
}
cnc_sim_linuxcnc_set_current_line(0);
interp->exit();
delete interp;
cnc_sim_linuxcnc_set_canon_sink(nullptr);

View File

@@ -68,6 +68,7 @@ int main() {
bool saw_g5x_offset = false;
bool saw_xy_rotation = false;
bool saw_g92_clear = false;
bool saw_coordinate_end_line = 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;
@@ -128,22 +129,29 @@ int main() {
for (const auto &event : events) {
saw_g5x_offset = saw_g5x_offset ||
(event.type == CNC_SIM_EVENT_SET_G5X_OFFSET &&
event.line == 2 &&
event.tool == 1 &&
event.start.x == 12.5 &&
event.start.y == -3.0 &&
event.start.z == 4.0);
saw_xy_rotation = saw_xy_rotation ||
(event.type == CNC_SIM_EVENT_SET_XY_ROTATION &&
event.line == 2 &&
event.feed == 30.0);
saw_g92_clear = saw_g92_clear ||
(event.type == CNC_SIM_EVENT_SET_G92_OFFSET &&
event.line == 4 &&
event.start.x == 0.0 &&
event.start.y == 0.0 &&
event.start.z == 0.0);
saw_coordinate_end_line = saw_coordinate_end_line ||
(event.type == CNC_SIM_EVENT_PROGRAM_END &&
event.line == 5);
}
ok &= expect(saw_g5x_offset, "expected LinuxCNC G10 L2 G5X offset event");
ok &= expect(saw_xy_rotation, "expected LinuxCNC G10 L2 XY rotation event");
ok &= expect(saw_g92_clear, "expected LinuxCNC G92.1 clear offset event");
ok &= expect(saw_coordinate_end_line, "expected LinuxCNC coordinate program end line number");
cnc_sim_destroy(sim);

View File

@@ -133,6 +133,7 @@ bool execute_line(InterpBase *interp,
const std::string &line,
int line_number,
bool *program_done) {
cnc_sim_linuxcnc_set_current_line(line_number);
std::vector<SimulatorGcodeControlAction> control_actions;
if (parse_simulator_gcode_control_line(line, &control_actions)) {
for (const auto &action : control_actions) {