joint.drive: a suppressed joint stores nothing, and the reply says what was asked and why a value differs (#9742) (#9862)
* claim: #9742 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019FAWPuFoh9qxn2uS2j53QZ * joint.drive: a suppressed joint stores nothing; the reply says what was asked and why a value differs (#9742) Driving a suppressed joint answered moved: false with "H is suppressed" but wrote the values into the joint, with an undo step, so the part jumped when the joint was unsuppressed. A suppressed joint outside any motion link now stores nothing. The reply gains `requested` (the values asked for, before limits and links) and `held`: each driven value that ended up elsewhere, with why (a motion link sets it, as a screw's travel follows its turn; a limit; contact; a closed loop). A drive that moves nothing gives the first held value's why as `reason`, so driving a screw's distance alone no longer answers moved: false without one. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019FAWPuFoh9qxn2uS2j53QZ --------- Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -46,9 +46,11 @@ pub static COMMANDS: &[CommandSpec] = &[
|
||||
CommandSpec::new("joint.drive", "Drive Joints", drive).at("SOLID", "ASSEMBLE").icon("drive").params(
|
||||
"joint: id|name; value | values: [angles in deg or expressions, distances] (values not given keep theirs; a value driven drops the expression joint.create gave it; clamped to the limits; the other joints of a \
|
||||
closed loop follow, and the motion stops where the loop can no longer close → stopped_by_loop); quiet?: bool (leave out dof \
|
||||
and fixed) → values: [{name, value, unit: deg|mm, limits}] as joint.list gives them; moved: whether any occurrence moved; \
|
||||
reason: why the joint can move nothing (suppressed, its origins lost, or its chain joined to nothing grounded → unconnected: \
|
||||
the occurrence named); index?: zero-based coordinate to drive with value (the other coordinates follow); continuous?: bool (drag frame, suppress UI echo)",
|
||||
and fixed) → values: [{name, value, unit: deg|mm, limits}] as joint.list gives them; requested: the values asked for, in the same form; \
|
||||
held: [{name, requested, value, unit, why}] for each driven value that ended up elsewhere (a motion link sets it, as a screw's \
|
||||
travel follows its turn; a limit; contact; a closed loop); moved: whether any occurrence moved; \
|
||||
reason: why the joint can move nothing (suppressed: nothing is stored; its origins lost; its chain joined to nothing grounded → unconnected: \
|
||||
the occurrence named; or, when nothing moved, the first held value's why); index?: zero-based coordinate to drive with value (the other coordinates follow); continuous?: bool (drag frame, suppress UI echo)",
|
||||
),
|
||||
CommandSpec::new("joint.reach", "Reach Point", reach).icon("reach").params(
|
||||
"occurrence: id|name; point: [x,y,z] (where its point goes, world); at?: [x,y,z] (its point, in the world where it is now; default: the centre of its bodies' box); z?: [x,y,z] (the way its axis then points); axis?: [x,y,z] (that axis, in the world now; default: its own z); joints?: [ids or names] (only these move toward the point; the other joints of a closed loop they are in follow); quiet?: bool; continuous?: bool (a frame of a drag: the answer says continuous, and the app's command line doesn't echo it). Moves the values of the joints on its chain from where they are to the nearest pose, within their limits, through motion links, keeping closed loops closed as joint.drive does (contact sets are not checked) → reached; error_mm (and error_deg with z): how close it got; point: where its point is; values: [{joint, name, values}] of the joints that moved, as joint.list gives them; moved; stopped_by_limit: [{joint, value, limit: min|max}]: the limits it rests on short of the point; stopped_by_loop: {joint}: the closed loop that stopped it (its chain alone gets closer); reason: why nothing can move",
|
||||
@@ -898,32 +900,43 @@ fn drive(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
if p.get("values").is_none() && p.get("value").is_none() {
|
||||
return Err(bad(cmd, "give `value` or `values`"));
|
||||
}
|
||||
let doc = s.doc_mut();
|
||||
let Some(j) = doc.assembly.joints.iter_mut().find(|j| j.id == id) else { return Err(bad(cmd, "joint")) };
|
||||
// Values not given (after `value`'s first, or past the end of `values`) keep theirs.
|
||||
let given = match p.get("values") {
|
||||
Some(Value::Array(a)) => a.len(),
|
||||
_ => 1,
|
||||
};
|
||||
let joint = s.doc.assembly.joints.iter().find(|j| j.id == id).ok_or_else(|| bad(cmd, "joint"))?;
|
||||
if index.is_none() {
|
||||
for (i, v) in q.iter_mut().enumerate().skip(given) {
|
||||
*v = j.values.get(i).copied().unwrap_or(*v);
|
||||
*v = joint.values.get(i).copied().unwrap_or(*v);
|
||||
}
|
||||
}
|
||||
joints::clamp(j, &mut q);
|
||||
// Moved as far toward `q` as contact sets and closed loops allow. Values left out of
|
||||
// `values` are not driven: in a closed loop they follow.
|
||||
// Driving moves the joint by hand: the values driven drop their expressions (an expression
|
||||
// given here is worked out once, as contact or a loop may stop the motion short of it).
|
||||
if let Some(index) = index {
|
||||
j.exprs.set_value(index, None);
|
||||
// What was asked for, before limits and motion links have their say.
|
||||
let requested = q.clone();
|
||||
let driven: Vec<usize> = match index {
|
||||
Some(index) => vec![index],
|
||||
None => (0..given.min(kind.dofs().len())).collect(),
|
||||
};
|
||||
// A suppressed joint places nothing: a value stored now would move the part only when the
|
||||
// joint is unsuppressed, and leave an undo step for a drive whose reply says nothing moved
|
||||
// (#9742). A joint in a motion link still carries its value to the joints it drives.
|
||||
let hold = joint.suppressed && !s.doc.assembly.links.iter().any(|l| l.a == id || l.b == id);
|
||||
let before = super::motion::positions(s);
|
||||
let r = if hold {
|
||||
json!({"stopped_by_contact": null, "stopped_by_loop": null})
|
||||
} else {
|
||||
for i in 0..given.min(kind.dofs().len()) {
|
||||
let doc = s.doc_mut();
|
||||
let Some(j) = doc.assembly.joints.iter_mut().find(|j| j.id == id) else { return Err(bad(cmd, "joint")) };
|
||||
joints::clamp(j, &mut q);
|
||||
// Moved as far toward `q` as contact sets and closed loops allow. Values left out of
|
||||
// `values` are not driven: in a closed loop they follow.
|
||||
// Driving moves the joint by hand: the values driven drop their expressions (an expression
|
||||
// given here is worked out once, as contact or a loop may stop the motion short of it).
|
||||
for &i in &driven {
|
||||
j.exprs.set_value(i, None);
|
||||
}
|
||||
}
|
||||
let before = super::motion::positions(s);
|
||||
let r = if let Some(index) = index { super::motion::drive_joint_index(s, id, q, index)? } else { super::motion::drive_joint(s, id, q, given)? };
|
||||
if let Some(index) = index { super::motion::drive_joint_index(s, id, q, index)? } else { super::motion::drive_joint(s, id, q, given)? }
|
||||
};
|
||||
let sol = solution(s, &BTreeMap::new(), response_analysis(p));
|
||||
let mut out = sol_json(&sol, p);
|
||||
out["joint"] = json!(id);
|
||||
@@ -933,7 +946,20 @@ fn drive(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
}
|
||||
out["stopped_by_contact"] = r["stopped_by_contact"].clone();
|
||||
out["stopped_by_loop"] = r["stopped_by_loop"].clone();
|
||||
out["moved"] = json!(super::motion::moved(&before, &super::motion::positions(s)).next().is_some());
|
||||
let moved = super::motion::moved(&before, &super::motion::positions(s)).next().is_some();
|
||||
out["moved"] = json!(moved);
|
||||
if let Some(j) = s.doc.assembly.joints.iter().find(|j| j.id == id) {
|
||||
out["requested"] = json!(named_values(j, &requested));
|
||||
let held = if hold { Vec::new() } else { held_values(s, j, &requested, &driven, &r) };
|
||||
if let Some(first) = held.first()
|
||||
&& !moved
|
||||
{
|
||||
out["reason"] = first["why"].clone();
|
||||
}
|
||||
if !held.is_empty() {
|
||||
out["held"] = json!(held);
|
||||
}
|
||||
}
|
||||
if let Some((why, unconnected)) = cannot_move(s, &sol, id) {
|
||||
out["reason"] = json!(why);
|
||||
if let Some(o) = unconnected {
|
||||
@@ -943,6 +969,52 @@ fn drive(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// The driven values of `j` that ended up other than `requested`, each with why: a motion link
|
||||
/// sets it from another coordinate (a screw's travel follows its turn), a limit holds it, or
|
||||
/// contact or a closed loop stopped the motion short. Degrees and mm, as `values` (#9742).
|
||||
fn held_values(s: &Session, j: &Joint, requested: &[f64], driven: &[usize], r: &Value) -> Vec<Value> {
|
||||
let now = joints::joint_values(&s.doc.assembly).remove(&j.id).unwrap_or_else(|| j.values.clone());
|
||||
let names = j.kind.dofs();
|
||||
let mut out = Vec::new();
|
||||
for &i in driven {
|
||||
let (Some(&want), Some(&got)) = (requested.get(i), now.get(i)) else { continue };
|
||||
if (want - got).abs() <= 1e-9 * want.abs().max(got.abs()).max(1.0) {
|
||||
continue;
|
||||
}
|
||||
let angle = j.kind.is_angle(i);
|
||||
let shown = |v: f64| if angle { v.to_degrees() } else { v };
|
||||
let name = names.get(i).copied().unwrap_or("value");
|
||||
let why = if let Some(l) = s.doc.assembly.links.iter().find(|l| l.b == j.id && l.ib == i) {
|
||||
let coupling = match &l.coupling {
|
||||
Some(joints::Coupling::Gear { .. }) => " (gears)",
|
||||
Some(joints::Coupling::RackPinion { .. }) => " (rack and pinion)",
|
||||
Some(joints::Coupling::Screw { .. }) => " (screw)",
|
||||
Some(joints::Coupling::Cam(_)) => " (cam)",
|
||||
None => "",
|
||||
};
|
||||
let from = s.doc.assembly.joints.iter().find(|o| o.id == l.a);
|
||||
let coordinate = from.and_then(|o| o.kind.dofs().get(l.ia).copied()).unwrap_or("value");
|
||||
if l.a == j.id {
|
||||
format!("{name} follows its {coordinate} through a motion link{coupling}")
|
||||
} else {
|
||||
format!("{name} follows {}'s {coordinate} through a motion link{coupling}", from.map_or("another joint", |o| o.name.as_str()))
|
||||
}
|
||||
} else if let Some(Some((lo, hi))) = j.limits.get(i)
|
||||
&& (want < lo.min(*hi) || want > hi.max(*lo))
|
||||
{
|
||||
format!("{name} is held to its limits, {} to {} {}", shown(lo.min(*hi)), shown(hi.max(*lo)), if angle { "deg" } else { "mm" })
|
||||
} else if r["stopped_by_contact"].is_object() {
|
||||
format!("{name} stopped at contact")
|
||||
} else if r["stopped_by_loop"].is_object() {
|
||||
format!("{name} stopped where a closed loop can no longer close")
|
||||
} else {
|
||||
continue;
|
||||
};
|
||||
out.push(json!({"name": name, "requested": shown(want), "value": shown(got), "unit": if angle { "deg" } else { "mm" }, "why": why}));
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// Joints `joint.reach` takes at most in `joints`.
|
||||
const MAX_REACH_JOINTS: usize = 1000;
|
||||
|
||||
|
||||
@@ -1491,8 +1491,52 @@ fn quiet_joint_answers_and_a_drive_to_where_it_is() {
|
||||
assert!(r["dof"].is_array() && r["moved"] == false && r.get("reason").is_none(), "{r}");
|
||||
// A suppressed joint with no motion link moves nothing, and says why.
|
||||
run(&mut s, "joint.edit", json!({"joint": "Rail", "suppressed": true}));
|
||||
let (doc, undo) = (s.doc.clone(), s.undo.len());
|
||||
let r = run(&mut s, "joint.drive", json!({"joint": "Rail", "value": 8}));
|
||||
assert!(r["moved"] == false && r["reason"] == "Rail is suppressed" && r.get("unconnected").is_none(), "{r}");
|
||||
// Nor does it store the value, as it did with an undo step: unsuppressed, the part jumped to
|
||||
// it (#9742). The reply keeps what was asked apart from what the joint holds.
|
||||
assert!(std::sync::Arc::ptr_eq(&doc, &s.doc) && s.undo.len() == undo, "a drive of a suppressed joint changed the design");
|
||||
assert_eq!((&r["requested"][0]["value"], &r["values"][0]["value"]), (&json!(8.0), &json!(5.0)), "{r}");
|
||||
assert!(r.get("held").is_none(), "{r}");
|
||||
}
|
||||
|
||||
/// A screw's travel follows its turn, so a distance asked of it is not reached: the reply says
|
||||
/// what was asked and why the joint holds another value, and a drive of the distance alone,
|
||||
/// which moves nothing, says why. A limit is named the same way (#9742).
|
||||
#[test]
|
||||
fn a_drive_says_which_values_a_motion_link_or_a_limit_held() {
|
||||
let mut s = two_cubes();
|
||||
run(&mut s, "joint.create", json!({"type": "cylindrical", "a": {"face": [5, 5, 10]}, "b": {"face": [25, 5, 0]}, "name": "Cy"}));
|
||||
run(&mut s, "joint.screw", json!({"joint": "Cy", "pitch": 2}));
|
||||
let follows = json!("distance follows its angle through a motion link (screw)");
|
||||
let r = run(&mut s, "joint.drive", json!({"joint": "Cy", "values": [90, 5]}));
|
||||
assert!(r["moved"] == true && r.get("reason").is_none(), "{r}");
|
||||
assert_eq!((&r["requested"][0]["value"], &r["requested"][1]["value"]), (&json!(90.0), &json!(5.0)), "{r}");
|
||||
let held = r["held"].as_array().cloned().unwrap_or_default();
|
||||
assert_eq!(held.len(), 1, "only the distance is held: {r}");
|
||||
assert_eq!(
|
||||
(&held[0]["name"], &held[0]["requested"], &held[0]["unit"], &held[0]["why"]),
|
||||
(&json!("distance"), &json!(5.0), &json!("mm"), &follows),
|
||||
"{r}"
|
||||
);
|
||||
assert!((held[0]["value"].as_f64().unwrap_or(f64::NAN) - 0.5).abs() < 1e-9, "a quarter turn at pitch 2 travels 0.5 mm: {r}");
|
||||
// The distance alone: nothing moves, and the reply says why.
|
||||
let r = run(&mut s, "joint.drive", json!({"joint": "Cy", "index": 1, "value": 4}));
|
||||
assert_eq!((&r["moved"], &r["reason"], &r["held"][0]["requested"]), (&json!(false), &follows, &json!(4.0)), "{r}");
|
||||
|
||||
let mut s = two_cubes();
|
||||
run(&mut s, "joint.as_built", json!({"type": "slider", "a": "A", "b": "B", "name": "Rail", "limits": [[0, 5]]}));
|
||||
let r = run(&mut s, "joint.drive", json!({"joint": "Rail", "value": 20}));
|
||||
assert!(r["moved"] == true && r.get("reason").is_none(), "{r}");
|
||||
assert_eq!(
|
||||
r["held"],
|
||||
json!([{"name": "distance", "requested": 20.0, "value": 5.0, "unit": "mm", "why": "distance is held to its limits, 0 to 5 mm"}]),
|
||||
"{r}"
|
||||
);
|
||||
// Within the limits, nothing is held.
|
||||
let r = run(&mut s, "joint.drive", json!({"joint": "Rail", "value": 3}));
|
||||
assert!(r.get("held").is_none() && r["requested"][0]["value"] == 3.0, "{r}");
|
||||
}
|
||||
|
||||
/// An as-built joint takes a name and limits like any other joint.
|
||||
|
||||
Reference in New Issue
Block a user