Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 52 additions & 12 deletions bindings/python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2053,7 +2053,7 @@ impl IntentSession {
/// unmodified default post: formats and independently decodes them with
/// `formatter`, then runs the same checks. Returns the chunk, and the
/// decoder's position and plane after it.
#[pyo3(signature=(events_json,formatter,position,units,plane,tool,cancel=None))]
#[pyo3(signature=(events_json,formatter,position,units,plane,tool,placed,cancel=None))]
#[allow(clippy::too_many_arguments, clippy::type_complexity)]
fn consume_default<'py>(
&mut self,
Expand All @@ -2064,8 +2064,14 @@ impl IntentSession {
units: f64,
plane: Option<&str>,
tool: &str,
placed: (bool, bool, bool),
cancel: Option<PyRef<'_, PostCancellation>>,
) -> PyResult<(Bound<'py, PyBytes>, (f64, f64, f64), Option<String>)> {
) -> PyResult<(
Bound<'py, PyBytes>,
(f64, f64, f64),
Option<String>,
(bool, bool, bool),
)> {
use camlib::motion::{Event, Motion, MoveKind};
if !self.initial.is_empty() || events_json.len() > 512 * 1024 {
return Err(PyValueError::new_err(
Expand All @@ -2092,11 +2098,12 @@ impl IntentSession {
.ok_or_else(|| PyValueError::new_err("invalid decoder plane"))
})
.transpose()?;
let (blocks, decoded, end, plane) = formatter.format_decode(
let (blocks, decoded, end, plane, placed) = formatter.format_decode(
&motions,
[position.0, position.1, position.2],
units,
plane,
[placed.0, placed.1, placed.2],
)?;
let decoded: Vec<Vec<Event>> = motions
.iter()
Expand Down Expand Up @@ -2133,6 +2140,7 @@ impl IntentSession {
PyBytes::new(py, &bytes),
(end[0], end[1], end[2]),
plane.map(|i| names[i].to_string()),
(placed[0], placed[1], placed[2]),
))
}
#[pyo3(signature=(lines_json,cancel=None))]
Expand Down Expand Up @@ -2418,19 +2426,23 @@ impl DefaultMotionPost {
}
/// Blocks for each source motion and its decoded motion words (None when
/// a linear block rounds to the current position), the decoded end
/// position and plane. `units` is the decoder's active unit scale.
/// position and plane, and which axes are still where the controller
/// placed them. `units` is the decoder's active unit scale; `placed` the
/// post's `_placed` before the batch.
#[allow(clippy::type_complexity)]
fn motions(
&self,
motions_json: &str,
position: (f64, f64, f64),
units: f64,
plane: Option<&str>,
placed: (bool, bool, bool),
) -> PyResult<(
Vec<Vec<String>>,
Vec<Option<(MotionWords, Option<&'static str>)>>,
(f64, f64, f64),
Option<String>,
(bool, bool, bool),
)> {
use camlib::motion::Curve;
let names = ["xy", "xz", "yz"];
Expand All @@ -2444,8 +2456,13 @@ impl DefaultMotionPost {
.ok_or_else(|| PyValueError::new_err("invalid decoder plane"))
})
.transpose()?;
let (blocks, decoded, position, plane) =
self.format_decode(&motions, [position.0, position.1, position.2], units, plane)?;
let (blocks, decoded, position, plane, placed) = self.format_decode(
&motions,
[position.0, position.1, position.2],
units,
plane,
[placed.0, placed.1, placed.2],
)?;
let point = |p: camlib::motion::Vec3| (p[0], p[1], p[2]);
let words = decoded
.into_iter()
Expand Down Expand Up @@ -2483,25 +2500,30 @@ impl DefaultMotionPost {
words,
point(position),
plane.map(|i| names[i].to_string()),
(placed[0], placed[1], placed[2]),
))
}
}
impl DefaultMotionPost {
/// Blocks for each source motion, its independently decoded curve, feed
/// and rapid flag (None when a linear block rounds to the current
/// position), the decoded end position and plane index.
/// position), the decoded end position and plane index, and the axes
/// still where the controller placed them (`placed` on entry: the post's
/// `_placed`).
#[allow(clippy::type_complexity)]
fn format_decode(
&self,
motions: &[camlib::motion::Motion],
position: camlib::motion::Vec3,
units: f64,
plane: Option<usize>,
placed: [bool; 3],
) -> PyResult<(
Vec<Vec<String>>,
Vec<Option<(camlib::motion::Curve, Option<f64>, bool)>>,
camlib::motion::Vec3,
Option<usize>,
[bool; 3],
)> {
use camlib::motion::{Curve, MoveKind, Plane};
let names = ["xy", "xz", "yz"];
Expand All @@ -2515,6 +2537,7 @@ impl DefaultMotionPost {
// against its source, so a missing code is refused.
let mut selected = plane;
let mut position = position;
let mut placed = placed;
let declared = |code: &Option<String>, effect: &str| {
code.clone()
.ok_or_else(|| PyValueError::new_err(format!("dialect does not declare {effect}")))
Expand Down Expand Up @@ -2598,6 +2621,7 @@ impl DefaultMotionPost {
for &i in &axes {
addresses.push_str(&self.axes[i]);
values.push(e[i]);
placed[i] = false;
}
addresses.push_str(&self.centers[u]);
addresses.push_str(&self.centers[v]);
Expand All @@ -2615,13 +2639,29 @@ impl DefaultMotionPost {
&& (position[0] - end[0]).hypot(position[1] - end[1])
+ 0.5 * self.unit * 10f64.powi(-(self.decimals as i32))
<= self.output_tolerance;
for &i in &axes {
addresses.push_str(&self.axes[i]);
values.push(if vertical && i < 2 {
let value = |i: usize| {
if vertical && i < 2 {
position[i]
} else {
end[i]
});
}
};
// `GCodePost.on_motion`: an axis the controller placed that
// this move keeps gets no word (a word would command it
// through the actual work offset). A block left with no axis
// word is written whole: a bare G0 is no motion block.
let mut kept: Vec<usize> = axes
.iter()
.copied()
.filter(|&i| !(placed[i] && value(i) == position[i]))
.collect();
if kept.is_empty() {
kept = axes.clone();
}
for i in kept {
addresses.push_str(&self.axes[i]);
values.push(value(i));
placed[i] = false;
}
}
if let Some(f) = m.feed_mm_min {
Expand Down Expand Up @@ -2679,7 +2719,7 @@ impl DefaultMotionPost {
blocks.push(lines);
decoded.push(result);
}
Ok((blocks, decoded, position, plane))
Ok((blocks, decoded, position, plane, placed))
}
}

Expand Down
2 changes: 1 addition & 1 deletion docs/lnc-mill-post.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ M6
G1 G43 H7 Z3. F3000.
M3 S12000
M8
G1 X10. Y0. Z3. F1200.
G1 X10. Z3. F1200.
G2 X20. Y0. I5. J0. F1200.
...
M5
Expand Down
50 changes: 50 additions & 0 deletions docs/posts.md
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,56 @@ deviation of a posted arc
includes the spiral: the start/end radius difference of the written words is
added to the bound before it is compared with the posting tolerance.

## Axis words a straight move leaves out

A straight move (G0/G1) leaves out the word of an axis it does not move
while that axis is still where the controller placed it itself (#367):

- At the program start every axis is where the controller placed it (the
machine's start pose).
- A tool change whose declared mode moves axes to a machine position places
them again, from its change block (`M6`, or the `T` word with
`tool_change="direct_t"`/`"lathe_t"`): Z with
`tool_change_position_mode="z_home"`, every axis with `"table"`.
`"preserve_tip"` and `"preserve_spindle"` place none.
- An axis leaves that state with the first block that writes its word: a
move that changes it, an arc or helix (they always name their plane's
axes), a canned cycle, a tapping or threading block, or a block a
controller post writes itself (a FANUC `G1 G43 H.. Z..`, a Carvera
`G53 G0 Z-3`). A probing cycle leaves every axis written, since its macros
move axes their blocks need not name.

An axis word commands the axis to that work coordinate through the
controller's *actual* work offset, even at the value it already has. Where
the controller placed the axis itself, the program's nominal offset and the
real one differ by the calibration interval, so the word would move the axis
by that error; at a travel limit, past it. A mill homed at X 0 and Y 0 (its
travel minimums) facing from its home pose writes

```
G0 Z10
G0 X-24 Y-17.6 Z10
```

and not `G0 X-250 Y-200 Z10` first, which LinuxCNC refuses as beyond the soft
limit when the real offset is 0.02 mm the other way. The word is left out
only when the value the block would write equals the controller's current
position exactly (the vertical-move rule above keeps the current XY, so a
move vertical in the plan qualifies), and never in a block that turns cutter
compensation on or off. A move that would keep no axis word at all (it moves
nothing) is written whole, since a bare `G0` is no motion block. Every decoder reads an absent word as the axis's
current position, and the machine check holds an axis no block has written
exactly at its declared position ([machine coverage](machine.md)), so the
first moves from a pose on a travel limit are checked inside it. Only the
blocks before each axis is first written change; the toolpaths do not.

The default post (`GCodePost.on_motion`, and every controller post in
`examples/` built on it), the native default formatter and the Rust posts
(default, Haas NGC, Carvera) apply the rule alike. The state is
`self._placed` (one flag per work axis), kept by `PostProcessor._call` from
the blocks each hook writes; a post that writes its own straight-move blocks
can read it.

## Writing your own post

Subclass `GCodePost` and override the hooks you need. Each hook writes lines
Expand Down
2 changes: 1 addition & 1 deletion fixtures/posts/F0IMB1000.NC
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ M6
G1 G43 H7 Z3. F3000.
M3 S12000
M8
G1 X10. Y0. Z3. F1200.
G1 X10. Z3. F1200.
M9
M5
G1 G49 Z63. F3000.
Expand Down
2 changes: 1 addition & 1 deletion fixtures/posts/LNC-MILL-ARCS.NC
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ M6
G1 G43 H7 Z3. F3000.
M3 S12000
M8
G1 X10. Y0. Z3. F1200.
G1 X10. Z3. F1200.
G2 X20. Y0. I5. J0. F1200.
G18
G2 X25. Z8. K5. I0. F1200.
Expand Down
2 changes: 1 addition & 1 deletion fixtures/posts/rust/carvera-arcs.nc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ T1 M6
M3 S12000
M400
M7
G0 X6 Y13 Z14
G0 X6 Y13
G1 X6 Y13 Z8 F150
G1 X6 Y16 Z8 F600
G2 X26 Y16 I10 J0 F600
Expand Down
2 changes: 1 addition & 1 deletion fixtures/posts/rust/carvera-drill.nc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ T1 M6
M3 S6000
M400
M7
G0 X8 Y8 Z12
G0 X8 Y8
G0 X8 Y8 Z11
G1 X8 Y8 Z4 F150
G4 P0.5
Expand Down
2 changes: 1 addition & 1 deletion fixtures/posts/rust/haas-program.nc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ M6
G43 H7
M3 S12000
M8
G1 X10. Y0. Z3. F1200.
G1 X10. F1200.
M9
M5
M0
Expand Down
Loading
Loading