Skip to content

About

In this repository you can find Browser-Based simulator for "Four-Bar Linkage Mechanism" which is written in Haskell. There is also a YouTube link for its simulation at the end of repo description.

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

2 stars

Watchers

0 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

Repository files navigation

Haskell Four-Bar Linkage Simulator

A browser-backed desktop-style GUI application written in Haskell with Threepenny-GUI for visualizing, classifying, and animating the planar four-bar linkage mechanism used in mechanism design and mechanical engineering courses.


Project Overview

This version replaces the previous Gloss GUI with a Threepenny-GUI + HTML/CSS/SVG interface. The pure Haskell mechanism solver remains in src/FourBar.hs, while app/Main.hs now builds a responsive browser-rendered GUI. The application runs a local GUI server and displays the interface in a web browser at http://127.0.0.1:8023.

Reference model: University of Illinois Dynamics Reference — Four-Bar Linkages.

Main GUI Features

  • Threepenny GUI: uses the browser as the display layer while the application logic stays in Haskell.
  • Local font support: loads resources/fonts/Inter-Regular.ttf through CSS @font-face.
  • Responsive 16:9 layout: the full app is drawn inside a proportional 16:9 shell.
  • Minimum width: the app shell uses a 650px minimum width and shows a warning below that size.
  • Left pane: occupies 20% of the application width and contains four horizontal sliders.
  • Link length sliders: tune g, a, b, and f from 5 cm to 40 cm.
  • SVG visualization: the four-bar mechanism is redrawn as scalable browser SVG.
  • Animation: a Play/Pause button starts and stops input-link motion.
  • Angle display: live input angle α and output angle β.
  • Mechanism classification: Grashof/non-Grashof, valid/invalid assembly, and crank/rocker state.
  • Keyboard controls: Space toggles Play/Pause, F flips the assembly branch, and R resets the mechanism.

Mechanism Design Theory

Planar Mechanisms and Mobility

A planar mechanism is a system of rigid bodies connected by joints so that the bodies move in a plane. For a planar linkage with n links, j1 lower pairs such as pin or slider joints, and j2 higher pairs, the Kutzbach-Grübler mobility equation is commonly written as:

$$ M = 3(n - 1) - 2j_1 - j_2. $$

For a four-bar linkage, there are four links and four pin joints:

$$ M = 3(4 - 1) - 2(4) = 9 - 8 = 1. $$

Therefore, an ideal planar four-bar linkage has exactly one degree of freedom. Once the input angle α is chosen, the other moving link positions are determined by the loop-closure constraints, except for the assembly branch.

Four-Bar Linkage Model

Symbol Name Description
gGround linkFixed distance between pivots A and B.
aInput linkDriven link from A to moving pivot C; its angle is α.
bOutput linkLink from fixed pivot B to moving pivot D; its angle is β.
fFloating link / couplerConnects moving pivots C and D.

The fixed pivots are placed at:

$$ A = (0,0), \qquad B = (g,0). $$

The input point C is determined by:

$$ C = (a\cos\alpha,\; a\sin\alpha). $$

The output point D must satisfy:

$$ \|D - C\| = f, \qquad \|D - B\| = b. $$

The simulator solves this by circle intersection. A real assembly exists for the current input angle when:

$$ |f - b| \le d \le f + b, $$

where d = ||B - C||. The two possible circle intersections correspond to the two assembly branches; pressing F flips between them.


Grashof and Validity Indices

Let the four link lengths be sorted as:

$$ s \le p \le q \le l, $$

where s is the shortest link and l is the longest link.

Grashof Index

$$ G = s + l - p - q. $$

  • G < 0: Grashof mechanism.
  • G = 0: change-point mechanism.
  • G > 0: non-Grashof mechanism.

Validity Index

$$ V = l - s - p - q. $$

A physically assemblable four-bar must satisfy V <= 0. If V > 0, the longest link is longer than the sum of the other three links, so the mechanism cannot close.


Full Linkage Model State Classification

The simulator computes:

$$ T_1 = g + f - b - a, \qquad T_2 = b + g - f - a, \qquad T_3 = f + b - g - a. $$

The signs of T1, T2, and T3 determine whether the input and output are cranks, rockers, 0-rockers, or π-rockers. The GUI displays both the detailed state and the collapsed basic state:

  • crank - crank
  • crank - rocker
  • rocker - rocker
  • rocker - crank

Repository Structure

fourbar-linkage-threepenny/
├── app/
│   └── Main.hs
├── src/
│   └── FourBar.hs
├── resources/
│   └── fonts/
│       └── Inter-Regular.ttf
├── fourbar-linkage-threepenny.cabal
├── README.md
├── LICENSE
└── .gitignore

src/FourBar.hs

This module contains the pure mechanism mathematics and classification logic. It is intentionally separated from the GUI so that future versions can reuse the solver in tests, command-line tools, notebooks, or web backends.

  • Linkage: stores g, a, b, and f.
  • Pose: stores the coordinates of pivots A, B, C, and D, plus α and β.
  • excesses: computes T1, T2, and T3.
  • grashofIndex: computes G = s + l - p - q.
  • validityIndex: computes V = l - s - p - q.
  • classifyMotion: returns detailed labels such as crank, rocker, 0-rocker, and π-rocker.
  • solvePose: solves the four-bar geometry using circle intersections.
  • nearestValidAlpha: finds a nearby valid angle after parameter changes.

app/Main.hs

This module now contains the Threepenny GUI and event loop. It builds DOM controls for the sliders and buttons, renders the mechanism as responsive SVG, loads the local Inter font through CSS, and updates the application state from Haskell.

  • setup: builds the page, sliders, readouts, buttons, and SVG container.
  • renderWorld: synchronizes the browser GUI with the Haskell World state.
  • mechanismSvg: generates the current linkage drawing as SVG.
  • animationLoop: advances the mechanism while Play is active.
  • updateSliderValue: applies slider changes to the pure linkage model.

How to Install Haskell and Run the App

The recommended installation route is GHCup, which installs ghc, cabal, and optional development tools.

Official Haskell download page: https://www.haskell.org/downloads/
Official GHCup installation page: https://www.haskell.org/ghcup/install/

Windows

Set-ExecutionPolicy Bypass -Scope Process -Force;[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; try { & ([ScriptBlock]::Create((Invoke-WebRequest https://www.haskell.org/ghcup/sh/bootstrap-haskell.ps1 -UseBasicParsing))) -Interactive -DisableCurl } catch { Write-Error $_ }

After installation, close and reopen PowerShell, then verify:

ghc --version
cabal --version

Run the simulator:

git clone https://github.com/mohammadijoo/Four-Bar-Mechanism-Haskell.git
cd Four-Bar-Mechanism-Haskell
cabal update
cabal run fourbar-linkage-threepenny

Then open http://127.0.0.1:8023 if the browser does not open automatically.

macOS / Linux

curl --proto '=https' --tlsv1.2 -sSf https://get-ghcup.haskell.org | sh

Restart the terminal, then verify:

ghc --version
cabal --version

Run the simulator:

git clone https://github.com/mohammadijoo/Four-Bar-Mechanism-Haskell.git
cd Four-Bar-Mechanism-Haskell
cabal update
cabal run fourbar-linkage-threepenny

Controls

ControlAction
Drag g sliderChange ground link length.
Drag a sliderChange input link length.
Drag b sliderChange output link length.
Drag f sliderChange floating/coupler link length.
Click Play / PauseStart or stop the animation.
Click Flip branchSwitch between the two possible assembly branches.
Click ResetRestore the default linkage.
SpaceToggle Play/Pause.
FFlip between assembly branches.
RReset the mechanism.

Troubleshooting

Browser does not open automatically

Threepenny starts a local GUI server. If the browser is not launched automatically, open:

http://127.0.0.1:8023

Port 8023 is already in use

Close the older running instance, or change jsPort = Just 8023 in app/Main.hs to another port such as 8024.

Local Inter font is not visible

Make sure the file exists exactly here:

resources/fonts/Inter-Regular.ttf

Also make sure you run cabal run from the project root, because the static file server is configured with jsStatic = Just ".".

Gloss / GLUT errors are gone

This project no longer depends on gloss, OpenGL, or GLUT. The old unknown GLUT entry glutInit Windows problem does not apply to this Threepenny version.

Responsive window behavior

The application shell uses CSS aspect-ratio: 16 / 9. The shell grows to the largest 16:9 rectangle that fits inside the browser viewport. It uses a 650px minimum width. On a viewport narrower than that, the app may horizontally clip and a warning is displayed.


Suggested Future Improvements

  • Add a coupler point P and trace its curve.
  • Add export of linkage motion data as CSV.
  • Add velocity and acceleration analysis using differentiated loop-closure equations.
  • Add force transmission angle and mechanical advantage plots.
  • Add automated tests for the classification table.
  • Add an Electron wrapper for a packaged desktop executable.

Simulation video

Below is a link to the simulation video on YouTube.

Four Bar Linkage Mechanism in Haskell

License

This project is released under the MIT License. See LICENSE for details.

About

In this repository you can find Browser-Based simulator for "Four-Bar Linkage Mechanism" which is written in Haskell. There is also a YouTube link for its simulation at the end of repo description.

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages