Hinged Counterweight Simulation

This notebook:

  • Instantiates a default hinged counterweight trebuchet and projectile

  • Solves the multi-phase motion (in sling & sliding over ground, in sling, ballistic)

  • Reports key timing and range metrics

  • Generates an animation of the launch

Tip: For clearer visuals you can adjust skip (frame thinning) and delay (ms between frames) in the animation cell.

[1]:
from pytrebuchet.simulation import Simulation, SimulationPhases
from pytrebuchet.differential_equations.sling_phase import SlingPhases
from pytrebuchet.trebuchet import HingedCounterweightTrebuchet

1. Setup & Solve

We create a default HingedCounterweightTrebuchet and run the Simulation.solve() method to solve the differential equations of the trebuchet and projectile.

Key reported metrics:

  • Ground separation time: when the projectile separates from the ground, marking the end of the ‘sliding’ sling phase.

  • Sling release time: when projectile exits the sling, marking the end of the ‘sling’ phases and the start of the ‘ballistic’ phase.

  • Ground impact time: end of ballistic phase.

  • Horizontal range: total distance traveled before impact.

[2]:
trebuchet = HingedCounterweightTrebuchet.default()
simulation = Simulation(trebuchet)

simulation.solve()

print(
    f"Ground separation time: {
        simulation.get_phase_end_time(
            sim_phase=SimulationPhases.SLING,
            sling_phase=SlingPhases.SLIDING_OVER_GROUND,
        ):.4f} s"
)
print(
    f"Sling release time: {
        simulation.get_phase_end_time(
            sim_phase=SimulationPhases.SLING, sling_phase=SlingPhases.UNCONSTRAINED
        ):.4f} s"
)
print(
    f"Ground impact time: {
        simulation.get_phase_end_time(sim_phase=SimulationPhases.BALLISTIC):.4f} s"
)
print(f"Horizontal range: {simulation.distance_traveled:.2f} m")
Ground separation time: 0.6523 s
Sling release time: 1.5703 s
Ground impact time: 6.6074 s
Horizontal range: 88.51 m

3. Animation

Below we animate the launch sequence.

Adjust parameters in animate_launch(simulation, skip=10, delay=50) for smoother or faster playback.

[3]:
from matplotlib import rc
from pytrebuchet.plotting import animate_launch
from IPython.display import HTML

# Configure matplotlib to render animations as interactive JavaScript
rc("animation", html="jshtml")

ani = animate_launch(simulation, skip=10, delay=50, show=False)
HTML(ani.to_jshtml())
[3]: