diff --git a/README.zh-CN.md b/README.zh-CN.md index 32521dc..8af1724 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -59,6 +59,7 @@ The first useful target is: - 已有五轴 RTCP 第一版几何内核,可根据编程刀尖点、A/B/C 姿态和刀长计算枢轴/主轴点补偿位置。 - 已有坐标系事件回归语料,覆盖 `G10 L2`、`G10 L20` 的 G5X/XY 旋转事件和 `G92.1` 清零事件。 - 已有探针事件回归语料,LinuxCNC `G38.3` 会输出独立 `probe` 事件,不再混作普通直线进给。 +- 已有主轴方向状态回归语料,`M3/M4/M5` 会通过 `set-spindle.reserved` 输出 `1=顺时针`、`2=逆时针`、`0=停止`。 - LinuxCNC Canon bridge 会把当前解释行号补到没有自带 `lineno` 的回调事件上,方便 Web 端诊断和源码高亮。 - 下一步是继续替换源码级链接中残留的 Python、INI/HAL、文件系统和动态加载依赖,并把 RTCP 内核接入 canon 事件流和机型配置。 @@ -78,6 +79,7 @@ The first useful target is: - LinuxCNC RS274 源文件语法探针和对象编译探针 - LinuxCNC RS274 源码级链接 smoke - 五轴 RTCP 几何内核 smoke +- `M3` / `M4` / `M5` 主轴方向事件回归 - `G10 L2` / `G10 L20` / `G92.1` 坐标系事件回归 - `G38.3` 探针事件回归 diff --git a/core/src/canon_event_sink.cpp b/core/src/canon_event_sink.cpp index 40ad5a3..d7743e7 100644 --- a/core/src/canon_event_sink.cpp +++ b/core/src/canon_event_sink.cpp @@ -8,6 +8,7 @@ void CanonEventSink::reset() { unit_scale_ = 1.0; feed_ = 0.0; spindle_ = 0.0; + spindle_direction_ = 0; selected_tool_ = 0; callback_status_ = 0; kinematics_type_ = 1; @@ -114,6 +115,23 @@ void CanonEventSink::set_spindle_speed(double spindle, int line) { spindle_ = spindle; CncSimEvent event = base_event(CNC_SIM_EVENT_SET_SPINDLE, line); event.spindle = spindle_; + event.reserved = spindle_direction_; + emit(event); +} + +void CanonEventSink::start_spindle(int direction, int line) { + spindle_direction_ = direction < 0 ? 2 : 1; + CncSimEvent event = base_event(CNC_SIM_EVENT_SET_SPINDLE, line); + event.spindle = spindle_; + event.reserved = spindle_direction_; + emit(event); +} + +void CanonEventSink::stop_spindle(int line) { + spindle_direction_ = 0; + CncSimEvent event = base_event(CNC_SIM_EVENT_SET_SPINDLE, line); + event.spindle = 0.0; + event.reserved = spindle_direction_; emit(event); } @@ -201,6 +219,10 @@ double CanonEventSink::spindle_speed() const { return spindle_; } +int CanonEventSink::spindle_direction() const { + return spindle_direction_; +} + int CanonEventSink::selected_tool() const { return selected_tool_; } diff --git a/core/src/canon_event_sink.h b/core/src/canon_event_sink.h index 659a777..66accf7 100644 --- a/core/src/canon_event_sink.h +++ b/core/src/canon_event_sink.h @@ -22,6 +22,8 @@ public: void select_plane(int plane, int line); void set_feed_rate(double feed, int line); void set_spindle_speed(double spindle, int line); + void start_spindle(int direction, int line); + void stop_spindle(int line); void select_tool(int tool); void change_tool(int line); void straight_traverse(int line, const CncSimPose &end); @@ -37,6 +39,7 @@ public: double unit_scale() const; double feed_rate() const; double spindle_speed() const; + int spindle_direction() const; int selected_tool() const; int kinematics_type() const; bool rtcp_enabled() const; @@ -55,6 +58,7 @@ private: double unit_scale_ = 1.0; double feed_ = 0.0; double spindle_ = 0.0; + int spindle_direction_ = 0; int selected_tool_ = 0; bool rtcp_enabled_ = false; double default_rtcp_tool_length_ = 0.0; diff --git a/core/src/linuxcnc_canon_bridge.cpp b/core/src/linuxcnc_canon_bridge.cpp index d730905..8cb33df 100644 --- a/core/src/linuxcnc_canon_bridge.cpp +++ b/core/src/linuxcnc_canon_bridge.cpp @@ -355,14 +355,20 @@ void SPINDLE_RETRACT_TRAVERSE() { } void START_SPINDLE_CLOCKWISE(int, int) { + if (active_sink) { + active_sink->start_spindle(1, event_line()); + } } void START_SPINDLE_COUNTERCLOCKWISE(int, int) { + if (active_sink) { + active_sink->start_spindle(-1, event_line()); + } } void STOP_SPINDLE_TURNING(int) { if (active_sink) { - active_sink->set_spindle_speed(0.0, 0); + active_sink->stop_spindle(event_line()); } } @@ -621,7 +627,16 @@ double GET_EXTERNAL_SPEED(int) { } CANON_DIRECTION GET_EXTERNAL_SPINDLE(int) { - return active_sink && active_sink->spindle_speed() != 0.0 ? CANON_CLOCKWISE : CANON_STOPPED; + if (!active_sink) { + return CANON_STOPPED; + } + if (active_sink->spindle_direction() == 1) { + return CANON_CLOCKWISE; + } + if (active_sink->spindle_direction() == 2) { + return CANON_COUNTERCLOCKWISE; + } + return CANON_STOPPED; } double GET_EXTERNAL_TOOL_LENGTH_XOFFSET() { return 0.0; } diff --git a/core/src/smoke_gcode_parser.cpp b/core/src/smoke_gcode_parser.cpp index 791f208..7993f5c 100644 --- a/core/src/smoke_gcode_parser.cpp +++ b/core/src/smoke_gcode_parser.cpp @@ -336,8 +336,18 @@ int SmokeGcodeParser::parse(const char *program, size_t program_len, std::string bool program_end = false; for (int m : m_codes) { - if (m == 3 || m == 4 || m == 5) { - sink_.set_spindle_speed(m == 5 ? 0.0 : sink_.spindle_speed(), line_number); + if (m == 3) { + sink_.start_spindle(1, line_number); + if (stop_if_callback_aborted(sink_, error)) { + return -1; + } + } else if (m == 4) { + sink_.start_spindle(-1, line_number); + if (stop_if_callback_aborted(sink_, error)) { + return -1; + } + } else if (m == 5) { + sink_.stop_spindle(line_number); if (stop_if_callback_aborted(sink_, error)) { return -1; } diff --git a/core/tests/cnc_sim_api_linuxcnc_rs274_smoke.cpp b/core/tests/cnc_sim_api_linuxcnc_rs274_smoke.cpp index 41c4165..1ddf53a 100644 --- a/core/tests/cnc_sim_api_linuxcnc_rs274_smoke.cpp +++ b/core/tests/cnc_sim_api_linuxcnc_rs274_smoke.cpp @@ -73,6 +73,9 @@ int main() { bool saw_l20_end_line = false; bool saw_probe = false; bool saw_probe_as_feed = false; + bool saw_spindle_cw = false; + bool saw_spindle_ccw = false; + bool saw_spindle_stop = 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; @@ -121,6 +124,36 @@ int main() { ok &= expect(saw_g49, "expected LinuxCNC G49 RTCP off state"); ok &= expect(saw_g434, "expected LinuxCNC G43.4 RTCP on state with H code"); + const char spindle_program[] = + "G21 G90 G17\n" + "S1200 M3\n" + "M4\n" + "M5\n" + "M30\n"; + events.clear(); + ok &= expect(cnc_sim_parse_program(sim, spindle_program, sizeof(spindle_program) - 1) == 0, + cnc_sim_last_error(sim)); + for (const auto &event : events) { + saw_spindle_cw = saw_spindle_cw || + (event.type == CNC_SIM_EVENT_SET_SPINDLE && + event.line == 2 && + event.spindle == 1200.0 && + event.reserved == 1); + saw_spindle_ccw = saw_spindle_ccw || + (event.type == CNC_SIM_EVENT_SET_SPINDLE && + event.line == 3 && + event.spindle == 1200.0 && + event.reserved == 2); + saw_spindle_stop = saw_spindle_stop || + (event.type == CNC_SIM_EVENT_SET_SPINDLE && + event.line == 4 && + event.spindle == 0.0 && + event.reserved == 0); + } + ok &= expect(saw_spindle_cw, "expected LinuxCNC M3 clockwise spindle state"); + ok &= expect(saw_spindle_ccw, "expected LinuxCNC M4 counterclockwise spindle state"); + ok &= expect(saw_spindle_stop, "expected LinuxCNC M5 stopped spindle state"); + const char probe_program[] = "G21 G90 G17\n" "G0 X0 Y0 Z5\n" diff --git a/core/tests/cnc_sim_api_smoke.cpp b/core/tests/cnc_sim_api_smoke.cpp index a81d3d1..256179c 100644 --- a/core/tests/cnc_sim_api_smoke.cpp +++ b/core/tests/cnc_sim_api_smoke.cpp @@ -67,6 +67,9 @@ int main() { bool saw_m430 = false; bool saw_g49 = false; bool saw_g434 = false; + bool saw_spindle_cw = false; + bool saw_spindle_ccw = false; + bool saw_spindle_stop = false; bool saw_incremental_move = false; bool saw_r_arc_center = false; for (const auto &event : events) { @@ -106,6 +109,11 @@ int main() { event.feed == 1.0 && event.tool == 7 && event.dwell_seconds == 125.0); + saw_spindle_cw = saw_spindle_cw || + (event.type == CNC_SIM_EVENT_SET_SPINDLE && + event.line == 6 && + event.spindle == 8000.0 && + event.reserved == 1); saw_incremental_move = saw_incremental_move || (event.type == CNC_SIM_EVENT_LINEAR_FEED && event.start.x == 20.0 && event.end.x == 25.0); @@ -127,9 +135,33 @@ int main() { ok &= expect(saw_m430, "expected M430 five-axis BC kinematics switch"); ok &= expect(saw_g49, "expected G49 RTCP off state"); ok &= expect(saw_g434, "expected G43.4 RTCP on state with H code"); + ok &= expect(saw_spindle_cw, "expected M3 clockwise spindle state"); ok &= expect(saw_incremental_move, "expected G91 incremental move"); ok &= expect(saw_r_arc_center, "expected R arc center calculation"); + const char spindle_program[] = + "G21\n" + "S1200 M3\n" + "M4\n" + "M5\n"; + events.clear(); + ok &= expect(cnc_sim_parse_program(sim, spindle_program, sizeof(spindle_program) - 1) == 0, + cnc_sim_last_error(sim)); + for (const auto &event : events) { + saw_spindle_ccw = saw_spindle_ccw || + (event.type == CNC_SIM_EVENT_SET_SPINDLE && + event.line == 3 && + event.spindle == 1200.0 && + event.reserved == 2); + saw_spindle_stop = saw_spindle_stop || + (event.type == CNC_SIM_EVENT_SET_SPINDLE && + event.line == 4 && + event.spindle == 0.0 && + event.reserved == 0); + } + ok &= expect(saw_spindle_ccw, "expected M4 counterclockwise spindle state"); + ok &= expect(saw_spindle_stop, "expected M5 stopped spindle state"); + 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); diff --git a/core/tests/linuxcnc_canon_bridge_smoke.cpp b/core/tests/linuxcnc_canon_bridge_smoke.cpp index aceede8..ccf16f9 100644 --- a/core/tests/linuxcnc_canon_bridge_smoke.cpp +++ b/core/tests/linuxcnc_canon_bridge_smoke.cpp @@ -34,6 +34,10 @@ int main() { USE_LENGTH_UNITS(CANON_UNITS_MM); SELECT_PLANE(CANON_PLANE::XY); SET_FEED_RATE(500.0); + SET_SPINDLE_SPEED(0, 1200.0); + START_SPINDLE_CLOCKWISE(0, 0); + START_SPINDLE_COUNTERCLOCKWISE(0, 0); + STOP_SPINDLE_TURNING(0); SELECT_TOOL(7); CHANGE_TOOL(); STRAIGHT_TRAVERSE(10, 0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0); @@ -53,6 +57,9 @@ int main() { bool saw_tool = false; bool saw_rapid = false; bool saw_feed = false; + bool saw_spindle_cw = false; + bool saw_spindle_ccw = false; + bool saw_spindle_stop = false; bool saw_arc = false; bool saw_probe = false; bool saw_g5x = false; @@ -61,6 +68,15 @@ int main() { bool saw_end = false; for (const auto &event : events) { saw_tool = saw_tool || (event.type == CNC_SIM_EVENT_TOOL_CHANGE && event.tool == 7); + saw_spindle_cw = saw_spindle_cw || (event.type == CNC_SIM_EVENT_SET_SPINDLE && + event.spindle == 1200.0 && + event.reserved == 1); + saw_spindle_ccw = saw_spindle_ccw || (event.type == CNC_SIM_EVENT_SET_SPINDLE && + event.spindle == 1200.0 && + event.reserved == 2); + saw_spindle_stop = saw_spindle_stop || (event.type == CNC_SIM_EVENT_SET_SPINDLE && + event.spindle == 0.0 && + event.reserved == 0); saw_rapid = saw_rapid || (event.type == CNC_SIM_EVENT_RAPID && event.end.z == 5.0); saw_feed = saw_feed || (event.type == CNC_SIM_EVENT_LINEAR_FEED && event.end.x == 10.0); saw_arc = saw_arc || (event.type == CNC_SIM_EVENT_ARC_FEED && @@ -90,6 +106,9 @@ int main() { } ok &= expect(saw_tool, "expected tool change"); + ok &= expect(saw_spindle_cw, "expected clockwise spindle state"); + ok &= expect(saw_spindle_ccw, "expected counterclockwise spindle state"); + ok &= expect(saw_spindle_stop, "expected stopped spindle state"); ok &= expect(saw_rapid, "expected rapid move"); ok &= expect(saw_feed, "expected linear feed"); ok &= expect(saw_arc, "expected arc feed"); diff --git a/test-linuxcnc-rs274-native.sh b/test-linuxcnc-rs274-native.sh index d97a65c..7663283 100755 --- a/test-linuxcnc-rs274-native.sh +++ b/test-linuxcnc-rs274-native.sh @@ -37,6 +37,7 @@ cp "$base_var_file" "$var_file" CNC_SIM_RS274_VAR="$var_file" "$build_dir/linuxcnc_rs274_dump" tests/gcode/linuxcnc_basic_motion.ngc >/tmp/cnc_sim_linuxcnc_basic_motion.json CNC_SIM_RS274_VAR="$var_file" "$build_dir/linuxcnc_rs274_dump" tests/gcode/basic_mill.ngc >/tmp/cnc_sim_linuxcnc_basic_mill.json CNC_SIM_RS274_VAR="$var_file" "$build_dir/linuxcnc_rs274_dump" tests/gcode/linuxcnc_rtcp_controls.ngc >/tmp/cnc_sim_linuxcnc_rtcp_controls.json +CNC_SIM_RS274_VAR="$var_file" "$build_dir/linuxcnc_rs274_dump" tests/gcode/linuxcnc_spindle_direction.ngc >/tmp/cnc_sim_linuxcnc_spindle_direction.json CNC_SIM_RS274_VAR="$var_file" "$build_dir/linuxcnc_rs274_dump" tests/gcode/linuxcnc_canned_cycle.ngc >/tmp/cnc_sim_linuxcnc_canned_cycle.json CNC_SIM_RS274_VAR="$var_file" "$build_dir/linuxcnc_rs274_dump" tests/gcode/linuxcnc_probe_no_error.ngc >/tmp/cnc_sim_linuxcnc_probe_no_error.json cp "$base_var_file" "$var_file" @@ -70,6 +71,19 @@ rtcp = [event for event in controls if event["type"] == "rtcp-state"] if [(event["line"], event["tool"], event["feed"]) for event in rtcp] != [(3, 0, 0), (4, 7, 1)]: raise SystemExit("unexpected RTCP state sequence") +spindle = json.loads(Path("/tmp/cnc_sim_linuxcnc_spindle_direction.json").read_text()) +spindle_states = [ + (event["line"], event["spindle"], event["reserved"]) + for event in spindle + if event["type"] == "set-spindle" +] +if (2, 1200, 1) not in spindle_states: + raise SystemExit("missing M3 clockwise spindle state") +if (3, 1200, 2) not in spindle_states: + raise SystemExit("missing M4 counterclockwise spindle state") +if (4, 0, 0) not in spindle_states: + raise SystemExit("missing M5 stopped spindle state") + cycle = json.loads(Path("/tmp/cnc_sim_linuxcnc_canned_cycle.json").read_text()) motions = [event for event in cycle if event["type"] in {"rapid", "linear-feed"}] expected_cycle = [ @@ -141,6 +155,7 @@ echo "linuxcnc rs274 native smoke passed" echo "dumped /tmp/cnc_sim_linuxcnc_basic_motion.json" echo "dumped /tmp/cnc_sim_linuxcnc_basic_mill.json" echo "dumped /tmp/cnc_sim_linuxcnc_rtcp_controls.json" +echo "dumped /tmp/cnc_sim_linuxcnc_spindle_direction.json" echo "dumped /tmp/cnc_sim_linuxcnc_canned_cycle.json" echo "dumped /tmp/cnc_sim_linuxcnc_probe_no_error.json" echo "dumped /tmp/cnc_sim_linuxcnc_coordinate_offsets.json" diff --git a/tests/gcode/linuxcnc_spindle_direction.ngc b/tests/gcode/linuxcnc_spindle_direction.ngc new file mode 100644 index 0000000..05ed39b --- /dev/null +++ b/tests/gcode/linuxcnc_spindle_direction.ngc @@ -0,0 +1,5 @@ +G21 G90 G17 +S1200 M3 +M4 +M5 +M30