Track spindle direction in set-spindle events

This commit is contained in:
CNC Local
2026-05-22 05:06:00 +08:00
parent a0d78b74b2
commit 14dd0facbf
10 changed files with 161 additions and 4 deletions

View File

@@ -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` 探针事件回归

View File

@@ -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_;
}

View File

@@ -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;

View File

@@ -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; }

View File

@@ -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;
}

View File

@@ -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"

View File

@@ -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);

View File

@@ -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");

View File

@@ -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"

View File

@@ -0,0 +1,5 @@
G21 G90 G17
S1200 M3
M4
M5
M30