Add LinuxCNC cutter compensation coverage

This commit is contained in:
cnc
2026-05-22 15:21:34 +08:00
parent 0ff0058417
commit 3dae570f88
5 changed files with 131 additions and 1 deletions

View File

@@ -1,5 +1,6 @@
#include "cnc_sim_api.h"
#include <cmath>
#include <iostream>
#include <vector>
@@ -25,6 +26,10 @@ bool expect(bool value, const char *message) {
return true;
}
bool near(double lhs, double rhs) {
return std::fabs(lhs - rhs) < 0.000001;
}
} // namespace
int main() {
@@ -96,6 +101,12 @@ int main() {
bool saw_g64_mode = false;
bool saw_tool_length = false;
bool saw_tool_length_clear = false;
bool saw_cutter_left_first = false;
bool saw_cutter_left_second = false;
bool saw_cutter_left_off_move = false;
bool saw_cutter_right_first = false;
bool saw_cutter_right_second = false;
bool saw_cutter_right_off_move = 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;
@@ -341,6 +352,61 @@ int main() {
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 cutter_comp_program[] =
"G21 G90 G17\n"
"G0 X0 Y0 Z0\n"
"F100\n"
"G41.1 D6\n"
"G1 X20 Y0\n"
"G1 X20 Y20\n"
"G40\n"
"G1 X0 Y0\n"
"G42.1 D6\n"
"G1 X20 Y0\n"
"G1 X20 Y-20\n"
"G40\n"
"G1 X0 Y0\n"
"M30\n";
events.clear();
ok &= expect(cnc_sim_parse_program(sim, cutter_comp_program, sizeof(cutter_comp_program) - 1) == 0,
cnc_sim_last_error(sim));
for (const auto &event : events) {
saw_cutter_left_first = saw_cutter_left_first ||
(event.type == CNC_SIM_EVENT_LINEAR_FEED &&
near(event.end.x, 17.0) &&
near(event.end.y, 3.0));
saw_cutter_left_second = saw_cutter_left_second ||
(event.type == CNC_SIM_EVENT_LINEAR_FEED &&
event.line == 7 &&
near(event.end.x, 17.0) &&
near(event.end.y, 20.0));
saw_cutter_left_off_move = saw_cutter_left_off_move ||
(event.type == CNC_SIM_EVENT_LINEAR_FEED &&
event.line == 8 &&
near(event.end.x, 0.0) &&
near(event.end.y, 0.0));
saw_cutter_right_first = saw_cutter_right_first ||
(event.type == CNC_SIM_EVENT_LINEAR_FEED &&
near(event.end.x, 17.0) &&
near(event.end.y, -3.0));
saw_cutter_right_second = saw_cutter_right_second ||
(event.type == CNC_SIM_EVENT_LINEAR_FEED &&
event.line == 12 &&
near(event.end.x, 17.0) &&
near(event.end.y, -20.0));
saw_cutter_right_off_move = saw_cutter_right_off_move ||
(event.type == CNC_SIM_EVENT_LINEAR_FEED &&
event.line == 13 &&
near(event.end.x, 0.0) &&
near(event.end.y, 0.0));
}
ok &= expect(saw_cutter_left_first, "expected LinuxCNC G41.1 compensated lead-in motion");
ok &= expect(saw_cutter_left_second, "expected LinuxCNC G41.1 compensated side motion");
ok &= expect(saw_cutter_left_off_move, "expected LinuxCNC G40 move after left compensation");
ok &= expect(saw_cutter_right_first, "expected LinuxCNC G42.1 compensated lead-in motion");
ok &= expect(saw_cutter_right_second, "expected LinuxCNC G42.1 compensated side motion");
ok &= expect(saw_cutter_right_off_move, "expected LinuxCNC G40 move after right compensation");
const char comment_program[] =
"G21 G90 G17\n"
"(operator note)\n"