{
  "configById": {
    "Image!wrist": {
      "imageMode": {
        "imageTopic": "/camera/wrist"
      },
      "foxglovePanelTitle": "Wrist camera"
    },
    "Image!ext1": {
      "imageMode": {
        "imageTopic": "/camera/exterior_1"
      },
      "foxglovePanelTitle": "Exterior 1"
    },
    "Image!ext2": {
      "imageMode": {
        "imageTopic": "/camera/exterior_2"
      },
      "foxglovePanelTitle": "Exterior 2"
    },
    "Plot!gripper": {
      "foxglovePanelTitle": "Gripper (0 open, 1 closed)",
      "paths": [
        {
          "value": "/robot/gripper.position",
          "enabled": true,
          "label": "position"
        },
        {
          "value": "/robot/gripper.commanded_position",
          "enabled": true,
          "label": "commanded"
        }
      ],
      "showLegend": true,
      "isSynced": true,
      "minYValue": 0,
      "maxYValue": 1
    },
    "Plot!torque": {
      "foxglovePanelTitle": "Joint torque (Nm)",
      "paths": [
        {
          "value": "/robot/joint_states.torque_measured[0]",
          "enabled": true,
          "label": "J1"
        },
        {
          "value": "/robot/joint_states.torque_measured[1]",
          "enabled": true,
          "label": "J2"
        },
        {
          "value": "/robot/joint_states.torque_measured[2]",
          "enabled": true,
          "label": "J3"
        },
        {
          "value": "/robot/joint_states.torque_measured[3]",
          "enabled": true,
          "label": "J4"
        },
        {
          "value": "/robot/joint_states.torque_measured[4]",
          "enabled": true,
          "label": "J5"
        },
        {
          "value": "/robot/joint_states.torque_measured[5]",
          "enabled": true,
          "label": "J6"
        },
        {
          "value": "/robot/joint_states.torque_measured[6]",
          "enabled": true,
          "label": "J7"
        }
      ],
      "showLegend": true,
      "isSynced": true
    },
    "RawMessages!task": {
      "topicPath": "/task/instruction",
      "foxglovePanelTitle": "Task"
    }
  },
  "layout": {
    "direction": "column",
    "splitPercentage": 60,
    "first": {
      "direction": "row",
      "first": "Image!wrist",
      "second": {
        "direction": "row",
        "first": "Image!ext1",
        "second": "Image!ext2"
      },
      "splitPercentage": 33.3
    },
    "second": {
      "direction": "row",
      "splitPercentage": 80,
      "first": {
        "direction": "row",
        "first": "Plot!gripper",
        "second": "Plot!torque"
      },
      "second": "RawMessages!task"
    }
  },
  "globalVariables": {},
  "userNodes": {
    "droid-torque-check": {
      "name": "torque_check",
      "sourceCode": "// DROID contact-spike check. Publishes one message per /robot/joint_states sample.\n// A joint is over its threshold when its torque differs from the median of the previous 7 samples\n// by more than max(8 Nm, 18 x the median absolute step over the previous 30 steps).\n// Only joints over their threshold are listed in \"over\"; spike is true on the 2nd and later consecutive over-threshold samples.\nimport { Input } from \"./types\";\n\nexport const inputs = [\"/robot/joint_states\"];\nexport const output = \"/studio_script/torque_check\";\n\ntype OverJoint = { joint: number; deviationNm: number; thresholdNm: number; spike: boolean };\ntype Output = {\n  over: OverJoint[];\n  anySpike: boolean;\n  maxDeviationNm: number;\n  maxDeviationJoint: number;\n};\n\nconst BASE_WINDOW = 7;\nconst NOISE_STEPS = 30;\nconst MIN_THRESHOLD_NM = 8;\nconst NOISE_FACTOR = 18;\n\nlet lastT = -Infinity;\nlet history: number[][] = [];\nlet prevOver: boolean[] = [];\n\nfunction median(xs: number[]): number {\n  const s = [...xs].sort((a, b) => a - b);\n  const m = Math.floor(s.length / 2);\n  return s.length % 2 === 1 ? s[m] : (s[m - 1] + s[m]) / 2;\n}\n\nexport default function script(event: Input<\"/robot/joint_states\">): Output {\n  const t = event.receiveTime.sec + event.receiveTime.nsec / 1e9;\n  if (t < lastT) {\n    history = [];\n    prevOver = [];\n  }\n  lastT = t;\n  const tau: number[] = (event.message as any).torque_measured;\n  const out: Output = { over: [], anySpike: false, maxDeviationNm: 0, maxDeviationJoint: 0 };\n  for (let j = 0; j < tau.length; j++) {\n    const col = history.map((h) => h[j]);\n    const base = col.length > 0 ? median(col.slice(-BASE_WINDOW)) : tau[j];\n    const dev = Math.abs(tau[j] - base);\n    const steps: number[] = [];\n    for (let i = 1; i < col.length; i++) steps.push(Math.abs(col[i] - col[i - 1]));\n    const noise = steps.length >= 5 ? median(steps) : 0;\n    const thr = Math.max(MIN_THRESHOLD_NM, NOISE_FACTOR * noise);\n    const over = dev > thr;\n    const spike = over && prevOver[j] === true;\n    if (over) out.over.push({ joint: j + 1, deviationNm: Math.round(dev * 100) / 100, thresholdNm: Math.round(thr * 10) / 10, spike });\n    if (spike) out.anySpike = true;\n    if (dev > out.maxDeviationNm) { out.maxDeviationNm = Math.round(dev * 100) / 100; out.maxDeviationJoint = j + 1; }\n    prevOver[j] = over;\n  }\n  history.push(tau);\n  if (history.length > NOISE_STEPS + 1) history.shift();\n  return out;\n}\n"
    },
    "droid-gripper-check": {
      "name": "gripper_check",
      "sourceCode": "// DROID gripper check. Publishes one message per /robot/gripper sample (0 = open, 1 = closed).\n// attempt: the gripper is above 0.1. closure: the gripper is above 0.3 (one attempt can hold several closures).\n// marker is set on exactly one sample per occurrence:\n//   attempt_start  set on the sample that first crosses 0.3 in an attempt; attemptStart gives the real start (first sample above 0.1)\n//   grasp_miss     first sample of a closure at 0.95 or more, with no hold of 0.5 s earlier in that closure\n//   object_drop    same, but after a hold of at least 0.5 s earlier in that closure\n//   attempt_end    first sample back at 0.1 or below after an attempt that reached 0.3\nimport { Input } from \"./types\";\n\nexport const inputs = [\"/robot/gripper\"];\nexport const output = \"/studio_script/gripper_check\";\n\ntype Time = { sec: number; nsec: number };\ntype Output = {\n  position: number;\n  marker: string;\n  attemptStart: Time;\n  attemptPeak: number;\n  longestHoldInClosureS: number;\n  meanHoldPosition: number;\n};\n\nconst OPEN = 0.1, CLOSING = 0.3, HELD_MAX = 0.9, EMPTY = 0.95, STEADY = 0.01, HOLD_MIN_S = 0.5;\n\nlet lastT = -Infinity;\nlet prevPos = 0;\nlet inAttempt = false, attemptReachedClosure = false, attemptStart: Time = { sec: 0, nsec: 0 }, attemptPeak = 0;\nlet inClosure = false, closureDone = false, closureCount = 0;\nlet holdStartT = -1, holdSum = 0, holdN = 0;\nlet longestHold = 0, longestHoldMean = 0;\n\nfunction reset() {\n  prevPos = 0; inAttempt = false; attemptReachedClosure = false; attemptPeak = 0;\n  inClosure = false; closureDone = false; closureCount = 0;\n  holdStartT = -1; holdSum = 0; holdN = 0; longestHold = 0; longestHoldMean = 0;\n}\n\nexport default function script(event: Input<\"/robot/gripper\">): Output {\n  const t = event.receiveTime.sec + event.receiveTime.nsec / 1e9;\n  if (t < lastT) reset();\n  lastT = t;\n  const p: number = (event.message as any).position;\n  let marker = \"\";\n\n  if (p > OPEN && !inAttempt) {\n    inAttempt = true; attemptReachedClosure = false; attemptPeak = 0;\n    attemptStart = { sec: event.receiveTime.sec, nsec: event.receiveTime.nsec };\n  }\n  if (inAttempt) attemptPeak = Math.max(attemptPeak, p);\n\n  const startingClosure = p > CLOSING && !inClosure;\n  if (startingClosure) {\n    inClosure = true; closureDone = false; closureCount += 1;\n    holdStartT = -1; holdSum = 0; holdN = 0; longestHold = 0; longestHoldMean = 0;\n    if (!attemptReachedClosure) { attemptReachedClosure = true; marker = \"attempt_start\"; }\n  }\n\n  let holding = false, holdS = 0; // internal only\n  if (inClosure && p > CLOSING) {\n    const delta = startingClosure ? 0 : Math.abs(p - prevPos);\n    holding = p < HELD_MAX && delta < STEADY;\n    if (holding) {\n      if (holdStartT < 0) { holdStartT = t; holdSum = 0; holdN = 0; }\n      holdSum += p; holdN += 1; holdS = t - holdStartT;\n      if (holdS > longestHold) { longestHold = holdS; longestHoldMean = holdSum / holdN; }\n    } else {\n      holdStartT = -1;\n    }\n    if (p >= EMPTY && !closureDone) {\n      closureDone = true;\n      marker = longestHold >= HOLD_MIN_S ? \"object_drop\" : \"grasp_miss\";\n    }\n  }\n  if (inClosure && p <= CLOSING) { inClosure = false; holdStartT = -1; }\n  if (inAttempt && p <= OPEN) {\n    if (attemptReachedClosure && marker === \"\") marker = \"attempt_end\";\n    inAttempt = false;\n  }\n  prevPos = p;\n\n  return {\n    position: p, marker, attemptStart,\n    attemptPeak: Math.round(attemptPeak * 10000) / 10000,\n    longestHoldInClosureS: Math.round(longestHold * 100) / 100,\n    meanHoldPosition: Math.round(longestHoldMean * 1000) / 1000,\n  };\n}\n"
    },
    "droid-motion-check": {
      "name": "motion_check",
      "sourceCode": "// DROID motion check. Publishes one message per /robot/end_effector sample.\n// speedMps uses the change from the previous sample. A repeated timestamp with no movement counts as still.\n// stillS is how long the arm has been below 0.01 m/s; stillStart is when that stretch began.\nimport { Input } from \"./types\";\n\nexport const inputs = [\"/robot/end_effector\"];\nexport const output = \"/studio_script/motion_check\";\n\ntype Time = { sec: number; nsec: number };\ntype Output = { speedMps: number; still: boolean; stillS: number; stillStart: Time };\n\nconst STILL_SPEED = 0.01;\n\nlet lastT = -Infinity;\nlet prev: number[] = [];\nlet prevSpeed = 0;\nlet stillStartT = -1;\nlet stillStart: Time = { sec: 0, nsec: 0 };\n\nexport default function script(event: Input<\"/robot/end_effector\">): Output {\n  const t = event.receiveTime.sec + event.receiveTime.nsec / 1e9;\n  const m = event.message as any;\n  const pos = [m.x, m.y, m.z];\n  if (t < lastT) { prev = []; prevSpeed = 0; stillStartT = -1; }\n  let speed = 0;\n  if (prev.length === 3) {\n    const step = Math.hypot(pos[0] - prev[0], pos[1] - prev[1], pos[2] - prev[2]);\n    const dt = t - lastT;\n    speed = dt > 0 ? step / dt : step > 0 ? prevSpeed : 0;\n  }\n  lastT = t; prev = pos; prevSpeed = speed;\n  const still = speed < STILL_SPEED;\n  if (still && stillStartT < 0) { stillStartT = t; stillStart = { sec: event.receiveTime.sec, nsec: event.receiveTime.nsec }; }\n  if (!still) stillStartT = -1;\n  return {\n    speedMps: Math.round(speed * 100000) / 100000,\n    still,\n    stillS: still ? Math.round((t - stillStartT) * 100) / 100 : 0,\n    stillStart: still ? stillStart : { sec: 0, nsec: 0 },\n  };\n}\n"
    }
  },
  "playbackConfig": {
    "speed": 1
  }
}
