I'm currently writing a program to track the height of the head of a 3d printer using a servo motor and a camera mounted to it. I made a sketch in Solidworks just to visualize better the shape of the system and this is what I did.
The camera "eye" is placed at the point "C", always pointing to the head "H" with the servo "S" where the rotation center is, and "B" the printer "bed".
I know the position of the printer head at each moment (BH length) , and the x and y offsets (SB, SC length respectively). What I would like to do is to compute the angle θ of the servo motor to always point to the head.
From this website I got to this formula $\theta = \arccos\left(\frac{x^2+y^2-h^2-a^2}{2\left(xy+ha\right)}\right)$ where $x$ and $y$ are the offsets, $h$ the printed head height, and $a$ the distance between the camera and the head, and $a $is unknown. So I'm wondering how to find $\theta$ if $a$ is not known neither. Thanks!
Assuming $a=|CH|$, using the Pythagorean theorem, $$|SH|^2=a^2+y^2=x^2+h^2\implies a^2=x^2-y^2+h^2. $$
$$\therefore\; \theta=\cos^{-1}\left(\frac{x^2+y^2-a^2-h^2}{2(xy+ah)}\right)=\cos^{-1}\left(\frac{y^2-h^2}{xy+h\sqrt{x^2-y^2+h^2}}\right)$$