# No. 39, Sun-and-planet motion (Brown 1868; James Watt's substitute for the crank).
#
# "The spur-gear to the right, called the planet-gear, is tied to the center of the other, or
#  sun-gear, by an arm which preserves a constant distance between their centers. ... Each revolution
#  of the planet-gear, which is rigidly attached to the connecting-rod, gives two to the sun-gear,
#  which is keyed to the fly-wheel shaft."
#
# Model (cm): the engine's beam (25 long) is rocked to and fro by the one actuator, a hand-like
# velocity servo of the beam's own swing (+-0.4127 rad, 0.5 Hz: the swing the linkage allows, solved
# by batch7/gen039.py together with the beam's pivot height). The beam's end carries the connecting
# rod (30 long) on a pin; the planet-gear (20 teeth, module 0.5) is part of the rod and never turns
# on it. The arm (10, sun centre to planet centre) turns freely on the fly-wheel shaft and is pinned
# to the planet's centre: it only keeps the gears in mesh. The sun-gear (20 teeth) is keyed to the
# fly-wheel shaft and is turned only through the teeth; the fly-wheel carries the beam over its dead
# points. For each beam stroke the planet goes once round, and the sun turns 1 + 20/20 = 2 times.
number: 39
slug: sun-and-planet-motion
name: Sun-and-planet motion
brown: "Sun-and-planet motion. The spur-gear to the right, called the planet-gear, is tied to the center of the other, or sun-gear, by an arm which preserves a constant distance between their centers. This was used as a substitute for the crank in a steam engine by James Watt, after the use of the crank had been patented by another party. Each revolution of the planet-gear, which is rigidly attached to the connecting-rod, gives two to the sun-gear, which is keyed to the fly-wheel shaft."
units: cm
sim:
  timestep: 0.0002
  integrator: implicitfast
  iterations: 100
  solver: CG
  multiccd: true
  duration_s: 10
  floor: false
bodies:
  - id: beam
    shape: part
    pos: [-25, 0, 29.91720]
    density: 2500
    color: "#c9a878"
    pieces:
      - {kind: poly, points: [[-26.2, 28.81720], [0.4, 29.36720], [0.4, 30.46720], [-26.2, 31.01720]], y: [-3.4, -2.6]}
      - {kind: disc, center: [-25, 29.91720], radius: 1.6, y: [-3.4, -2.6]}
      - {kind: disc, center: [-25, 29.91720], radius: 0.4, y: [-3.6, -0.6], color: "#8f8a80"}
  - id: rod
    shape: part
    pos: [0, 0, 29.91720]
    mount: beam
    density: 2500
    color: "#b8ad97"
    pieces:
      - {kind: rod, from: [0, -2.0, 29.91720], to: [9.87305, -2.0, 1.58837], radius: 0.55}
      - {kind: disc, center: [0, 29.91720], radius: 1.0, y: [-2.6, -1.4]}
      - {kind: disc, center: [9.87305, 1.58837], radius: 2.2, y: [-2.6, -1.0]}
  - id: arm
    shape: part
    pos: [0, 0, 0]
    density: 2500
    color: "#a39d90"
    pieces:
      - {kind: disc, center: [0, 0], radius: 1.4, inner: 0.6, y: [1.2, 2.6]}
      - {kind: rod, from: [0, 1.9, 0], to: [9.87305, 1.9, 1.58837], radius: 0.5}
      - {kind: disc, center: [9.87305, 1.58837], radius: 1.2, y: [1.2, 2.6]}
      - {kind: rod, from: [9.87305, -2.8, 1.58837], to: [9.87305, 2.6, 1.58837], radius: 0.35, color: "#8f8a80"}
  - id: sun
    shape: part
    pos: [0, 0, 0]
    density: 7200
    color: "#9c958a"
    pieces:
      - {kind: disc, center: [0, 0], radius: 16, inner: 13.5, y: [4.2, 5.8]}
      - {kind: disc, center: [0, 0], radius: 2.0, inner: 0.6, y: [4.2, 5.8]}
      - {kind: rod, from: [0, 5, 0], to: [13.60000, 5, 0.00000], radius: 0.55}
      - {kind: rod, from: [0, 5, 0], to: [6.80000, 5, 11.77795], radius: 0.55}
      - {kind: rod, from: [0, 5, 0], to: [-6.80000, 5, 11.77795], radius: 0.55}
      - {kind: rod, from: [0, 5, 0], to: [-13.60000, 5, 0.00000], radius: 0.55}
      - {kind: rod, from: [0, 5, 0], to: [-6.80000, 5, -11.77795], radius: 0.55}
      - {kind: rod, from: [0, 5, 0], to: [6.80000, 5, -11.77795], radius: 0.55}
      - {kind: rod, from: [0, -1.6, 0], to: [0, 8.2, 0], radius: 0.6, color: "#8f8a80"}
  - id: frame
    shape: part
    pos: [0, 0, 0]
    fixed: true
    color: "#cfc6b4"
    pieces:
      - {kind: poly, points: [[-26.5, -24], [-23.5, -24], [-24.2, 29.91720], [-25.8, 29.91720]], y: [-1.6, -0.6]}
      - {kind: disc, center: [-25, 29.91720], radius: 1.4, y: [-1.6, -0.6]}
      - {kind: poly, points: [[-2.5, -24], [2.5, -24], [1.2, 0], [-1.2, 0]], y: [7.2, 8.2]}
      - {kind: disc, center: [0, 0], radius: 1.6, y: [7.2, 8.2]}
      - {kind: poly, points: [[-32, -25], [22, -25], [22, -24], [-32, -24]], y: [-5, 10], color: "#b9b09c"}
joints:
  - {id: j_beam, type: hinge, body: beam, axis: [0, -1, 0], damping: 0.01}
  - {id: j_rod, type: hinge, body: rod, axis: [0, -1, 0], damping: 0.001}
  - {id: j_arm, type: hinge, body: arm, axis: [0, -1, 0], damping: 0.001}
  - {id: j_sun, type: hinge, body: sun, axis: [0, -1, 0], damping: 0.02}   # the fly-wheel shaft's bearings
pins:
  - {id: crankpin, a: arm, b: rod, at: [9.87305, 0, 1.58837]}
spurs:
  - {id: sun_g, body: sun, pos: [0, 0, 0], teeth: 20, module: 0.5, width: 2, backlash: 0.02, phase: 0.154}
  - {id: planet_g, body: rod, pos: [9.87305, 0, -28.32883], teeth: 20, module: 0.5, width: 2, backlash: 0.02, phase: 0}
contact: {solref: [0.002, 1], solimp: [0.95, 0.99, 0.0005], margin: 0}
driver: {joint: j_beam, kind: velocity, rpm: 0, swing: {amplitude: 0.45, hz: 0.5}, kv: 60, max_torque: 80}
checks:
  # four strokes of the beam (2-10 s): the arm goes once round per stroke and the sun twice
  - {type: ratio, out: j_sun, in: j_arm, expect: 2.0, tol: 0.01, settle_s: 2, window_s: 8}
  # the fly-wheel keeps going round the same way through the beam's dead points (slowing in them)
  - {type: turning, joint: j_sun, direction: 1, min_mean: 5.5, slow: 0.2, min_fraction: 0.9, settle_s: 2, window_s: 8}
  - {type: monotonic, joint: j_sun, direction: 1, max_back: 0.01, settle_s: 2, window_s: 8}
  - {type: penetration, max: 0.001, settle_s: 0.5}
camera: {target: [-5, 0, -9], position: [-5, -190, -7], fov: 30}
display:
  output: j_sun
  labels: {j_beam: Beam, j_arm: Arm (planet's centre), j_sun: Sun-gear and fly-wheel, j_rod: Connecting rod}
  proof: {label: Planet's teeth out of contact, sub: "the beam still rocks and the arm swings, but nothing turns the sun-gear"}
