# No. 148 (Brown 1868): "Continuous circular motion of the spur-gears produces alternate circular motion of the crank attached to the larger gear."
#
# Model (cm): a pinion (12 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. It gears with the larger gear (36 spaces, three times its size), which has teeth on a sector of its rim only and carries the crank.
# While the pinion's teeth are in, they turn the larger gear and its crank forward 50 degrees; when the last tooth quits, the spring (the larger gear's joint spring, 0.5 N m/rad, damped) swings the crank back to its stop,
# where the first tooth of the next turn finds the same tooth space. Generator: opencode-runs/batch10/gen148.py.
number: 148
slug: spur-gears-alternate-crank
name: Spur gears giving alternate motion to a crank
brown: "Continuous circular motion of the spur-gears produces alternate circular motion of the crank attached to the larger gear."
units: cm
sim:
  timestep: 0.0002
  integrator: implicitfast
  iterations: 100
  solver: PGS
  multiccd: true
  duration_s: 9.0
  floor: false
  gravity: 0
bodies:
  - id: pinion
    shape: part
    pos: [0, 0, 0]
    density: 2700
    friction: 0.1
    color: "#d9cfb6"
    pieces:
      - {kind: disc, center: [0, 0], radius: 3.95, inner: 0.6, y: [-0.8, 0.8]}
      - {kind: poly, points: [[3.1476, 2.6086], [1.8252, 3.658], [3.0584, 4.4156], [3.6053, 3.9816]], y: [-0.8, 0.8], collide: 1}
      - {kind: poly, points: [[4.0302, 0.6853], [3.4097, 2.2553], [4.8564, 2.2949], [5.1131, 1.6455]], y: [-0.8, 0.8], collide: 1}
      - {kind: poly, points: [[3.8329, -1.4216], [4.0805, 0.2483], [5.3532, -0.4408], [5.2508, -1.1315]], y: [-0.8, 0.8], collide: 1}
      - {kind: poly, points: [[2.6086, -3.1476], [3.658, -1.8252], [4.4156, -3.0584], [3.9816, -3.6053]], y: [-0.8, 0.8], collide: 1}
      - {kind: poly, points: [[0.6853, -4.0302], [2.2553, -3.4097], [2.2949, -4.8564], [1.6455, -5.1131]], y: [-0.8, 0.8], collide: 1}
  - id: big
    shape: part
    pos: [19.23, 0, 0]
    density: 2700
    friction: 0.1
    color: "#d9cfb6"
    pieces:
      - {kind: disc, center: [19.23, 0], radius: 13.55, inner: 9.0, y: [-0.8, 0.8]}
      - {kind: disc, center: [19.23, 0], radius: 2.2, inner: 0.6, y: [-1.0, 1.0]}
      - {kind: rod, from: [20.9852, 0, 0.9589], to: [27.3038, 0, 4.4107], radius: 0.5, collide: 0}
      - {kind: rod, from: [19.2772, 0, 1.9994], to: [19.4471, 0, 9.1974], radius: 0.5, collide: 0}
      - {kind: rod, from: [17.522, 0, 1.0406], to: [11.3733, 0, 4.7867], radius: 0.5, collide: 0}
      - {kind: rod, from: [17.4748, 0, -0.9589], to: [11.1562, 0, -4.4107], radius: 0.5, collide: 0}
      - {kind: rod, from: [19.1828, 0, -1.9994], to: [19.0129, 0, -9.1974], radius: 0.5, collide: 0}
      - {kind: rod, from: [20.938, 0, -1.0406], to: [27.0867, 0, -4.7867], radius: 0.5, collide: 0}
      - {kind: poly, points: [[16.5254, 13.3551], [14.8947, 12.9181], [15.0209, 14.3599], [15.6953, 14.5406]], y: [-0.8, 0.8], collide: 2}
      - {kind: poly, points: [[14.2474, 12.6825], [12.7174, 11.9691], [12.5912, 13.4108], [13.224, 13.7059]], y: [-0.8, 0.8], collide: 2}
      - {kind: poly, points: [[12.1208, 11.6246], [10.7379, 10.6563], [10.3633, 12.0543], [10.9353, 12.4547]], y: [-0.8, 0.8], collide: 2}
      - {kind: poly, points: [[10.2102, 10.2135], [9.0165, 9.0198], [8.4048, 10.3315], [8.8985, 10.8252]], y: [-0.8, 0.8], collide: 2}
      - {kind: poly, points: [[8.5737, 8.4921], [7.6054, 7.1092], [6.7753, 8.2947], [7.1757, 8.8667]], y: [-0.8, 0.8], collide: 2}
      - {kind: poly, points: [[7.2609, 6.5126], [6.5475, 4.9826], [5.5241, 6.006], [5.8192, 6.6388]], y: [-0.8, 0.8], collide: 2}
      - {kind: poly, points: [[6.3119, 4.3353], [5.8749, 2.7046], [4.6894, 3.5347], [4.8701, 4.2091]], y: [-0.8, 0.8], collide: 2}
      - {kind: poly, points: [[5.7553, 2.0262], [5.6082, 0.3444], [4.2965, 0.9561], [4.3574, 1.6516]], y: [-0.8, 0.8], collide: 2}
      - {kind: poly, points: [[5.6082, -0.3444], [5.7553, -2.0262], [4.3574, -1.6516], [4.2965, -0.9561]], y: [-0.8, 0.8], collide: 2}
      - {kind: poly, points: [[5.8749, -2.7046], [6.3119, -4.3353], [4.8701, -4.2091], [4.6894, -3.5347]], y: [-0.8, 0.8], collide: 2}
      - {kind: poly, points: [[6.5475, -4.9826], [7.2609, -6.5126], [5.8192, -6.6388], [5.5241, -6.006]], y: [-0.8, 0.8], collide: 2}
      - {kind: poly, points: [[7.6054, -7.1092], [8.5737, -8.4921], [7.1757, -8.8667], [6.7753, -8.2947]], y: [-0.8, 0.8], collide: 2}
      - {kind: poly, points: [[18.53, 0.0], [19.93, 0.0], [19.3984, 3.076], [18.0197, 2.8329]], y: [-1.7, -0.9], collide: 0}
      - {kind: rod, from: [19.23, -1.3, 0], to: [17.3199, -1.3, 10.8329], radius: 0.75, collide: 0, color: "#a89a7e"}   # the crank
      - {kind: rod, from: [17.3199, -1.3, 10.8329], to: [17.3199, -3.0, 10.8329], radius: 0.9, collide: 0, color: "#8f8a80"}   # its pin
  - 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"}   # the pinion's shaft
      - {kind: rod, from: [19.23, -0.9, 0], to: [19.23, 2.2, 0], radius: 0.7, color: "#8f8a80"}   # the larger gear's shaft
      - {kind: poly, points: [[-1.2, -15.5], [1.2, -15.5], [0.9, 0], [-0.9, 0]], y: [1.2, 2.2]}   # the standards, behind
      - {kind: poly, points: [[18.03, -15.5], [20.43, -15.5], [20.13, 0], [18.33, 0]], y: [1.2, 2.2]}
      - {kind: poly, points: [[-8, -16.5], [27.23, -16.5], [27.23, -15.5], [-8, -15.5]], y: [-3, 3], color: "#b9b09c"}
joints:
  - {id: j_p, type: hinge, body: pinion, axis: [0, 1, 0], damping: 0.002}   # the one driven joint; + : clockwise
  - {id: j_g, type: hinge, body: big, axis: [0, 1, 0], range: [-1.4727, 0.0], stiffness: 0.5, springref: 0, damping: 0.03}   # the larger gear; the spring brings it to the stop at 0
contact: {solref: [0.002, 1.0], solimp: [0.95, 0.99, 0.0005], margin: 0}
driver: {joint: j_p, kind: velocity, rpm: 30, kv: 1.0, max_torque: 5}
checks:
  - {type: follow, out: j_g, in: j_p, expect: -0.3333, tol: 0.01, min_r2: 0.995, settle_s: 1.9, window_s: 0.5}   # while the teeth are in, the crank turns as the gear
  - {type: follow, out: j_g, in: j_p, expect: -0.3333, tol: 0.01, min_r2: 0.995, settle_s: 4.0, window_s: 0.5}
  - {type: position, joint: j_g, min: -1.1727, max: 0.05, settle_s: 0.0, window_s: 8.5}
camera: {target: [9.615, 0, -2], position: [9.615, -78.0, -2], fov: 34}
display:
  output: j_g
  labels: {j_p: "Pinion (driver)", j_g: "Larger gear and crank"}
  proof: {label: "Teeth out of contact", sub: "the pinion turns but the larger gear stands"}

