\[ \begin{aligned} 2L& = 2 g m \left(\sqrt{\ell^2-r^2 \cos ^2(\varphi)}+ r \sin (\varphi)\right)+I \dot{\varphi}^2\\ & + m r^2 (\dot{\varphi})^2 \cos^2(\varphi) \left(\frac{r \sin (\varphi)}{\sqrt{\ell^2-r^2 \cos ^2(\varphi)}}+1\right)^2 \end{aligned} \]

\(\)

(\(r\) is the radius from the gear center to the connector. \(\ell\) is the length of the rod connecting the piston and gear. \(g\), \(m\), \(I\) are the gravitational acceleration, piston mass, and gear moment of inertia respectively. \(\varphi\) is the angle between the horizontal axis and gear location in the positive sense.)

The equations of motion are integrated numerically with the Euler's method. So don't expect much in terms of accuracy.