Q&A-1Design a forward-kinematics interface for a robot service.
Answer
The 90-second version
I'd state my assumptions first: the model is a URDF, units are SI (metres, radians), frames follow REP-103, and the service is stateless compute. It does not hold robot state. Then the interface: the request carries a model identifier (a content hash of the URDF, not a version string), a named base frame, a named target frame, a joint configuration as name→value pairs, and the timestamp the configuration was sampled. The response is a pose, position plus orientation, with explicit convention metadata: quaternion order, units, and the frame the pose is expressed in.
Why it behaves that way
Forward kinematics walks the chain from base link to target link and composes one transform per joint. A revolute joint contributes a rotation about its axis by the joint angle; a prismatic joint contributes a translation along its axis; a fixed joint contributes a constant transform. The composition is T_base_target = T_base_link1 · T_link1_link2 · … · T_linkN_target. Matrix multiplication does not commute, so the order of the chain is the whole computation: permuting joint order in the request must not change the result, because the chain order comes from the model, not the request.
Mimic joints are the trap. A gripper finger's value is derived from a source joint via a multiplier and offset. You must resolve mimics before composing, or you compute a pose the real robot cannot reach. If a client sends a mimic joint explicitly, that conflicts with the derived value: reject it rather than silently pick one.
Caching
The cache key is (model_hash, base, target, joint_configuration). The timestamp is deliberately excluded: it records when state was sampled, not the geometry. Two requests with identical joint values and different timestamps produce the identical pose, so keying on the timestamp makes every request a miss. The timestamp still matters: it drives a staleness check. If the sample is older than your freshness budget, return the pose but flag it, or reject, depending on whether the caller is planning or controlling.
Worked trace
Two-link planar arm, both revolute about z, each link 1.0 m, q1 = 0, q2 = π/2.
T = Rz(0) · Trans(1,0,0) · Rz(π/2) · Trans(1,0,0)
Rz(0) = I
Trans(1,0,0) → (1, 0, 0)
Rz(π/2) rotates the next offset: Trans(1,0,0) → (0, 1, 0)
position = (1,0,0) + (0,1,0) = (1, 1, 0)
orientation = Rz(π/2) → quaternion (x,y,z,w) = (0, 0, 0.7071, 0.7071)Trade-offs
| Decision | Alternative | When it flips |
|---|---|---|
| Return pose + convention metadata | Return a 4×4 matrix | Matrix is unambiguous but 12 numbers; pose matches geometry_msgs/Pose and is what callers want |
| Reject out-of-limit configs | Clamp to limits | Reject for a reporting service; clamp only inside a controller that must keep moving |
| Cache the pose | Recompute every call | FK is a handful of matrix multiplies (microseconds); cache only pays off for long chains or high request rates, and adds invalidation cost |
| Quaternion order (x,y,z,w) | (w,x,y,z) | ROS uses xyzw; some libraries use wxyz: state it in the response so callers never guess |
Failure modes
- Missing joint: never default to zero; zero is a real pose and silently wrong. Detect by validating every model joint name is present, and return the missing name.
- Model-version mismatch: client built a config for URDF v1, server holds v2, joint names differ. Detect by echoing the model hash in the response and rejecting on mismatch.
- Unknown frame: base or target not in the model. Reject with the frame name.
- Stale timestamp: geometry is correct, state is old. Flag it; the caller decides.
Tests: known poses, transform composition, alternate bases, joint-order permutations, unit handling, missing joints, model-hash changes.
Curated: · Written: · Reviewed:
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.