Expand LinuxCNC smoke coverage

This commit is contained in:
cnc
2026-05-22 15:11:28 +08:00
parent 44e024e49c
commit 0ff0058417
17 changed files with 3507 additions and 70 deletions

View File

@@ -439,7 +439,19 @@ void USE_NO_SPINDLE_FORCE() {
void SET_TOOL_TABLE_ENTRY(int, int, const EmcPose &, double, double, double, int) {
}
void USE_TOOL_LENGTH_OFFSET(const EmcPose &) {
void USE_TOOL_LENGTH_OFFSET(const EmcPose &offset) {
if (!active_sink) {
return;
}
CncSimEvent event{};
event.version = 1;
event.type = CNC_SIM_EVENT_COMMENT;
event.line = event_line();
event.reserved = 430;
event.start = make_pose(offset.tran.x, offset.tran.y, offset.tran.z,
offset.a, offset.b, offset.c,
offset.u, offset.v, offset.w);
active_sink->emit_raw(event);
}
void CHANGE_TOOL_NUMBER(int number) {

View File

@@ -15,6 +15,8 @@
#include <string>
#include <vector>
#include <cstdlib>
#include <fstream>
#include <unistd.h>
int _task = 0;
char _parameter_file_name[LINELEN];
@@ -88,6 +90,92 @@ std::string interp_error(InterpBase *interp, int status, const char *stage) {
return result;
}
bool file_mode_enabled() {
return std::getenv("CNC_SIM_RS274_FILE_MODE") != nullptr;
}
bool write_temp_program(const char *program,
size_t program_len,
std::string *path,
std::string *error) {
char tmpl[] = "/tmp/cnc_sim_api_XXXXXX.ngc";
const int fd = mkstemps(tmpl, 4);
if (fd < 0) {
if (error) {
*error = "failed to create temporary ngc file";
}
return false;
}
bool ok = true;
size_t written = 0;
while (written < program_len) {
const ssize_t rc = write(fd, program + written, program_len - written);
if (rc <= 0) {
ok = false;
break;
}
written += static_cast<size_t>(rc);
}
if (close(fd) != 0) {
ok = false;
}
if (!ok) {
unlink(tmpl);
if (error) {
*error = "failed to write temporary ngc file";
}
return false;
}
*path = tmpl;
return true;
}
int execute_file_mode(InterpBase *interp,
CanonEventSink &sink,
const std::string &path,
std::string *error) {
int status = interp->open(path.c_str());
if (status != INTERP_OK) {
if (error) {
*error = interp_error(interp, status, "open");
}
return -1;
}
bool program_done = false;
while (!program_done) {
status = interp->read();
if (status == INTERP_EXIT || status == INTERP_ENDFILE) {
break;
}
if (!normal_read_status(status)) {
if (error) {
*error = interp_error(interp, status, "read");
}
interp->close();
return -1;
}
cnc_sim_linuxcnc_set_current_line(interp->line());
status = interp->execute();
if (!normal_execute_status(status, &program_done)) {
if (error) {
*error = interp_error(interp, status, "execute");
}
interp->close();
return -1;
}
if (stop_if_callback_aborted(sink, error)) {
interp->close();
return -1;
}
}
interp->close();
return 0;
}
} // namespace
int parse_linuxcnc_rs274_backend(CanonEventSink &sink,
@@ -119,57 +207,82 @@ int parse_linuxcnc_rs274_backend(CanonEventSink &sink,
return -1;
}
std::string source(program, program + program_len);
std::istringstream input(source);
std::string line;
bool program_done = false;
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) {
emit_simulator_gcode_control_action(sink, action, line_number);
if (stop_if_callback_aborted(sink, error)) {
interp->exit();
delete interp;
cnc_sim_linuxcnc_set_canon_sink(nullptr);
return -1;
if (file_mode_enabled()) {
std::string temp_path;
if (!write_temp_program(program, program_len, &temp_path, error)) {
interp->exit();
delete interp;
cnc_sim_linuxcnc_set_canon_sink(nullptr);
return -1;
}
const int rc = execute_file_mode(interp, sink, temp_path, error);
unlink(temp_path.c_str());
if (rc != 0) {
interp->exit();
delete interp;
cnc_sim_linuxcnc_set_canon_sink(nullptr);
return -1;
}
} else {
std::string source(program, program + program_len);
std::istringstream input(source);
std::string line;
bool program_done = false;
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)) {
bool simulator_only_line = true;
for (const auto &action : control_actions) {
emit_simulator_gcode_control_action(sink, action, line_number);
if (action.kind == SimulatorGcodeControlKind::RtcpState &&
!action.rtcp_enabled) {
simulator_only_line = false;
}
if (stop_if_callback_aborted(sink, error)) {
interp->exit();
delete interp;
cnc_sim_linuxcnc_set_canon_sink(nullptr);
return -1;
}
}
if (simulator_only_line) {
continue;
}
}
continue;
}
status = interp->read(line.c_str());
if (!normal_read_status(status)) {
if (error) {
*error = interp_error(interp, status, "read");
status = interp->read(line.c_str());
if (!normal_read_status(status)) {
if (error) {
*error = interp_error(interp, status, "read");
}
interp->exit();
delete interp;
cnc_sim_linuxcnc_set_canon_sink(nullptr);
return -1;
}
if (status == INTERP_EXIT || status == INTERP_ENDFILE) {
break;
}
interp->exit();
delete interp;
cnc_sim_linuxcnc_set_canon_sink(nullptr);
return -1;
}
if (status == INTERP_EXIT || status == INTERP_ENDFILE) {
break;
}
status = interp->execute();
if (!normal_execute_status(status, &program_done)) {
if (error) {
*error = interp_error(interp, status, "execute");
status = interp->execute();
if (!normal_execute_status(status, &program_done)) {
if (error) {
*error = interp_error(interp, status, "execute");
}
interp->exit();
delete interp;
cnc_sim_linuxcnc_set_canon_sink(nullptr);
return -1;
}
if (stop_if_callback_aborted(sink, error)) {
interp->exit();
delete interp;
cnc_sim_linuxcnc_set_canon_sink(nullptr);
return -1;
}
interp->exit();
delete interp;
cnc_sim_linuxcnc_set_canon_sink(nullptr);
return -1;
}
if (stop_if_callback_aborted(sink, error)) {
interp->exit();
delete interp;
cnc_sim_linuxcnc_set_canon_sink(nullptr);
return -1;
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -14,5 +14,16 @@ public:
private:
CanonEventSink &sink_;
bool absolute_ = true;
int canned_cycle_ = 0;
bool canned_return_to_initial_ = false;
bool canned_has_r_ = false;
bool canned_has_z_ = false;
bool canned_has_p_ = false;
bool canned_has_q_ = false;
bool canned_has_d_ = false;
double canned_r_ = 0.0;
double canned_z_ = 0.0;
double canned_p_ = 0.0;
double canned_q_ = 0.0;
double canned_d_ = 1.0;
};

View File

@@ -94,6 +94,8 @@ int main() {
bool saw_g61_mode = false;
bool saw_g611_mode = false;
bool saw_g64_mode = false;
bool saw_tool_length = false;
bool saw_tool_length_clear = 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;
@@ -316,6 +318,29 @@ int main() {
ok &= expect(saw_g611_mode, "expected LinuxCNC G61.1 exact stop mode");
ok &= expect(saw_g64_mode, "expected LinuxCNC G64 continuous mode");
const char tool_length_program[] =
"G21 G90 G17\n"
"G43 H1\n"
"G49\n"
"M30\n";
events.clear();
ok &= expect(cnc_sim_parse_program(sim, tool_length_program, sizeof(tool_length_program) - 1) == 0,
cnc_sim_last_error(sim));
for (const auto &event : events) {
saw_tool_length = saw_tool_length ||
(event.type == CNC_SIM_EVENT_COMMENT &&
event.line == 2 &&
event.reserved == 430 &&
event.start.z == 0.0);
saw_tool_length_clear = saw_tool_length_clear ||
(event.type == CNC_SIM_EVENT_COMMENT &&
event.line == 3 &&
event.reserved == 430 &&
event.start.z == 0.0);
}
ok &= expect(saw_tool_length, "expected LinuxCNC G43 H1 tool length event");
ok &= expect(saw_tool_length_clear, "expected LinuxCNC G49 tool length clear event");
const char comment_program[] =
"G21 G90 G17\n"
"(operator note)\n"

File diff suppressed because it is too large Load Diff

View File

@@ -49,6 +49,10 @@ int main() {
SET_G5X_OFFSET(1, 10.0, 20.0, 30.0, 1.0, 2.0, 3.0, 0.0, 0.0, 0.0);
SET_G92_OFFSET(1.0, 2.0, 3.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
SET_XY_ROTATION(15.0);
EmcPose tool_offset{};
tool_offset.tran.z = 12.5;
cnc_sim_linuxcnc_set_current_line(19);
USE_TOOL_LENGTH_OFFSET(tool_offset);
cnc_sim_linuxcnc_set_current_line(20);
COMMENT("bridge comment");
cnc_sim_linuxcnc_set_current_line(24);
@@ -83,6 +87,7 @@ int main() {
bool saw_g5x = false;
bool saw_g92 = false;
bool saw_rotation = false;
bool saw_tool_length = false;
bool saw_comment = false;
bool saw_mist_on = false;
bool saw_flood_on = false;
@@ -131,6 +136,10 @@ int main() {
event.start.z == 3.0);
saw_rotation = saw_rotation || (event.type == CNC_SIM_EVENT_SET_XY_ROTATION &&
event.feed == 15.0);
saw_tool_length = saw_tool_length || (event.type == CNC_SIM_EVENT_COMMENT &&
event.line == 19 &&
event.reserved == 430 &&
event.start.z == 12.5);
saw_comment = saw_comment || (event.type == CNC_SIM_EVENT_COMMENT &&
event.line == 20);
saw_mist_on = saw_mist_on || (event.type == CNC_SIM_EVENT_COMMENT &&
@@ -169,6 +178,7 @@ int main() {
ok &= expect(saw_g5x, "expected G5X offset event");
ok &= expect(saw_g92, "expected G92 offset event");
ok &= expect(saw_rotation, "expected XY rotation event");
ok &= expect(saw_tool_length, "expected tool length offset event");
ok &= expect(saw_comment, "expected comment event line");
ok &= expect(saw_mist_on, "expected mist on state");
ok &= expect(saw_flood_on, "expected flood on state");

View File

@@ -136,14 +136,21 @@ bool execute_line(InterpBase *interp,
cnc_sim_linuxcnc_set_current_line(line_number);
std::vector<SimulatorGcodeControlAction> control_actions;
if (parse_simulator_gcode_control_line(line, &control_actions)) {
bool simulator_only_line = true;
for (const auto &action : control_actions) {
emit_simulator_gcode_control_action(sink, action, line_number);
if (action.kind == SimulatorGcodeControlKind::RtcpState &&
!action.rtcp_enabled) {
simulator_only_line = false;
}
if (sink.callback_aborted()) {
std::cerr << "event callback aborted parsing\n";
return false;
}
}
return true;
if (simulator_only_line) {
return true;
}
}
if (trace_enabled()) {
@@ -183,6 +190,54 @@ bool execute_line(InterpBase *interp,
return true;
}
bool execute_open_file(InterpBase *interp, const char *path) {
if (trace_enabled()) {
std::cerr << "rs274:open " << path << '\n';
}
int rc = interp->open(path);
if (rc != 0) {
char error[1024]{};
interp->error_text(rc, error, sizeof(error));
std::cerr << "open failed: " << error << '\n';
return false;
}
bool ok = true;
bool program_done = false;
while (!program_done) {
rc = interp->read();
if (rc == INTERP_ENDFILE || rc == INTERP_EXIT) {
break;
}
if (rc != 0) {
char error[1024]{};
interp->error_text(rc, error, sizeof(error));
std::cerr << "read failed: " << error << '\n';
ok = false;
break;
}
cnc_sim_linuxcnc_set_current_line(interp->line());
if (trace_enabled()) {
std::cerr << "rs274:execute file line " << interp->line() << '\n';
}
rc = interp->execute();
if (rc == INTERP_EXIT || rc == INTERP_ENDFILE) {
program_done = true;
} else if (rc == INTERP_EXECUTE_FINISH || rc == INTERP_OK) {
continue;
} else {
char error[1024]{};
interp->error_text(rc, error, sizeof(error));
std::cerr << "execute failed: " << error << '\n';
ok = false;
break;
}
}
interp->close();
return ok;
}
void init_minimal_tooldata() {
tool_mmap_creator(nullptr, 0);
tooldata_reset();
@@ -202,7 +257,10 @@ void init_minimal_tooldata() {
} // namespace
int main(int argc, char **argv) {
const std::string program = read_all(argc > 1 ? argv[1] : "-");
const bool use_file_mode = std::getenv("CNC_SIM_RS274_FILE_MODE") != nullptr &&
argc > 1 &&
std::string(argv[1]) != "-";
const std::string program = use_file_mode ? std::string{} : read_all(argc > 1 ? argv[1] : "-");
if (const char *parameter_file = std::getenv("CNC_SIM_RS274_VAR")) {
SET_PARAMETER_FILE_NAME(parameter_file);
}
@@ -230,16 +288,20 @@ int main(int argc, char **argv) {
std::cerr << "rs274:init done\n";
}
std::istringstream input(program);
std::string line;
bool ok = true;
bool program_done = false;
int line_number = 0;
while (!program_done && std::getline(input, line)) {
++line_number;
if (!execute_line(interp, sink, line, line_number, &program_done)) {
ok = false;
break;
if (use_file_mode) {
ok = execute_open_file(interp, argv[1]);
} else {
std::istringstream input(program);
std::string line;
bool program_done = false;
int line_number = 0;
while (!program_done && std::getline(input, line)) {
++line_number;
if (!execute_line(interp, sink, line, line_number, &program_done)) {
ok = false;
break;
}
}
}
std::cout << "\n]\n";