# No. 81 (Brown 1868): "Alternate rectilinear motion is given to the rack-rod, B, by the continuous
# revolution of the mutilated spur-gear, A, the spiral spring, C, forcing the rod back to its original
# position on the teeth of the gear, A, quitting the rack."
#
# Model (cm): A (16 tooth spaces, module 0.8, No. 34's straight-flanked teeth) has teeth on 5 of them and is
# cut down to its root circle round the rest; it turns continuously, the one driven joint, 30 turns a minute.
# The rack-rod B slides in its guides under A. While A's teeth pass the rack they draw B to the left, about
# 12.6 cm; when the last tooth quits the rack the spring C, compressed by the stroke, forces B back
# until its collar comes against the stop at the first guide. There the spring's preload holds it, and A's first
# tooth comes round into the same tooth space of the rack the next turn. The spring C is modelled as B's joint
# spring (30 N/m, 3.0 cm of preload at the stop, damped), not drawn. Generator: opencode-runs/batch8/gen081.py.
number: 81
slug: mutilated-gear-rack-spring
name: Rack-rod moved to and fro by a mutilated gear and a spring
brown: "Alternate rectilinear motion is given to the rack-rod, B, by the continuous revolution of the mutilated spur-gear, A, the spiral spring, C, forcing the rod back to its original position on the teeth of the gear, A, quitting the rack."
units: cm
sim:
  timestep: 0.0002
  integrator: implicitfast
  iterations: 100
  solver: PGS
  multiccd: true
  duration_s: 10
  floor: false
bodies:
  - id: gear_a
    shape: part
    pos: [0, 0, 0]
    density: 7800
    friction: 0.1
    color: "#d9cfb6"
    pieces:
      - {kind: disc, center: [0, 0], radius: 5.55, inner: 0.6, y: [-0.8, 0.8]}
      - {kind: poly, points: [[4.1873, -3.813], [5.1252, -2.4094], [5.981, -3.5765], [5.5931, -4.157]], y: [-0.8, 0.8], collide: 1}
      - {kind: poly, points: [[5.3277, -1.9204], [5.6571, -0.2646], [6.8944, -1.0154], [6.7582, -1.7002]], y: [-0.8, 0.8], collide: 1}
      - {kind: poly, points: [[5.6571, 0.2646], [5.3277, 1.9204], [6.7582, 1.7002], [6.8944, 1.0154]], y: [-0.8, 0.8], collide: 1}
      - {kind: poly, points: [[5.1252, 2.4094], [4.1873, 3.813], [5.5931, 4.157], [5.981, 3.5765]], y: [-0.8, 0.8], collide: 1}
      - {kind: poly, points: [[3.813, 4.1873], [2.4094, 5.1252], [3.5765, 5.981], [4.157, 5.5931]], y: [-0.8, 0.8], collide: 1}
  - id: rod_b
    shape: part
    pos: [0, 0, -7.65]
    density: 7800
    friction: 0.1
    color: "#b8ad97"
    pieces:
      - {kind: poly, points: [[-1.2566, -8.1], [21.3628, -8.1], [21.3628, -7.2], [-1.2566, -7.2]], y: [-0.8, 0.8]}   # the rack
      - {kind: poly, points: [[-0.819, -7.2], [0.819, -7.2], [0.3822, -6.0], [-0.3822, -6.0]], y: [-0.8, 0.8], collide: 2}
      - {kind: poly, points: [[1.6943, -7.2], [3.3322, -7.2], [2.8955, -6.0], [2.1311, -6.0]], y: [-0.8, 0.8], collide: 2}
      - {kind: poly, points: [[4.2076, -7.2], [5.8455, -7.2], [5.4087, -6.0], [4.6443, -6.0]], y: [-0.8, 0.8], collide: 2}
      - {kind: poly, points: [[6.7209, -7.2], [8.3588, -7.2], [7.922, -6.0], [7.1576, -6.0]], y: [-0.8, 0.8], collide: 2}
      - {kind: poly, points: [[9.2341, -7.2], [10.8721, -7.2], [10.4353, -6.0], [9.6709, -6.0]], y: [-0.8, 0.8], collide: 2}
      - {kind: poly, points: [[11.7474, -7.2], [13.3853, -7.2], [12.9486, -6.0], [12.1842, -6.0]], y: [-0.8, 0.8], collide: 2}
      - {kind: poly, points: [[14.2607, -7.2], [15.8986, -7.2], [15.4618, -6.0], [14.6974, -6.0]], y: [-0.8, 0.8], collide: 2}
      - {kind: poly, points: [[16.774, -7.2], [18.4119, -7.2], [17.9751, -6.0], [17.2107, -6.0]], y: [-0.8, 0.8], collide: 2}
      - {kind: poly, points: [[19.2872, -7.2], [20.9252, -7.2], [20.4884, -6.0], [19.724, -6.0]], y: [-0.8, 0.8], collide: 2}
      - {kind: rod, from: [21.3628, 0, -7.65], to: [40.0, 0, -7.65], radius: 0.35}   # the rod
      - {kind: poly, points: [[20.2, -8.45], [21.0, -8.45], [21.0, -6.85], [20.2, -6.85]], y: [-0.8, 0.8], collide: 1}   # its collar
  - id: frame
    shape: part
    pos: [0, 0, 0]
    fixed: true
    color: "#cfc6b4"
    pieces:
      - {kind: rod, from: [0, -1.4, 0], to: [0, 2.2, 0], radius: 0.6, color: "#8f8a80"}   # A's shaft
      - {kind: poly, points: [[-1.2, -11.1], [1.2, -11.1], [0.9, 0], [-0.9, 0]], y: [1.2, 2.2]}   # its standard, behind
      - {kind: poly, points: [[21.02, -8.85], [22.8, -8.85], [22.8, -8.1], [21.02, -8.1]], y: [-0.9, 0.9], collide: 2}   # the first guide: the stop
      - {kind: poly, points: [[21.02, -7.2], [22.8, -7.2], [22.8, -6.45], [21.02, -6.45]], y: [-0.9, 0.9], collide: 2}
      - {kind: poly, points: [[21.4, -11.1], [22.4, -11.1], [22.4, -8.85], [21.4, -8.85]], y: [-0.9, 0.9]}
      - {kind: poly, points: [[25.2, -8.85], [26.0, -8.85], [26.0, -6.45], [25.2, -6.45]], y: [-0.9, -0.45]}   # the second guide, either side of the rod
      - {kind: poly, points: [[25.2, -8.85], [26.0, -8.85], [26.0, -6.45], [25.2, -6.45]], y: [0.45, 0.9]}
      - {kind: poly, points: [[25.2, -11.1], [26.0, -11.1], [26.0, -8.85], [25.2, -8.85]], y: [-0.9, 0.9]}
      - {kind: poly, points: [[-22, -12.1], [41.0, -12.1], [41.0, -11.1], [-22, -11.1]], y: [-3, 3], color: "#b9b09c"}
joints:
  - {id: j_A, type: hinge, body: gear_a, axis: [0, 1, 0], damping: 0.002}   # the one driven joint; + : clockwise
  - {id: j_b, type: slide, body: rod_b, axis: [1, 0, 0], damping: 6.0, stiffness: 30.0, springref: 3.0}   # + : to the right; the spring C
contact: {solref: [0.002, 1.0], solimp: [0.95, 0.99, 0.0005], margin: 0}
driver: {joint: j_A, kind: velocity, rpm: 30.0, kv: 5.0, max_torque: 20}
checks:
  - {type: follow, out: j_b, in: j_A, expect: -0.064, tol: 0.0013, min_r2: 0.995, settle_s: 2.45, window_s: 0.4}   # B goes as A's pitch line while the teeth are in
  - {type: position, joint: j_b, min: -17.5, max: 0.1, settle_s: 0.5, window_s: 9}
  - {type: position, joint: j_b, min: -0.1, max: 0.1, settle_s: 3.6, window_s: 0.3}   # back at the stop before the teeth come round
  - {type: penetration, max: 0.001, settle_s: 0.5}
camera: {target: [9, 0, -4], position: [9, -80, -2], fov: 34}
display:
  output: j_b
  labels: {j_A: "Mutilated gear A (driver)", j_b: "Rack-rod B"}
  proof: {label: "A's teeth out of contact", sub: "A turns but B never leaves the stop"}

