Closed-form inverse kinematics of a planar two-link arm
Source:R/inverse-kinematics.R
ik_planar2.RdFor link lengths \(l_1, l_2\) and target \((x, y)\), \(c_2 = (x^2 + y^2 - l_1^2 - l_2^2) / (2 l_1 l_2)\). When \(|c_2| \le 1\) the solutions are \(q_2 = \pm\arccos c_2\) and \(q_1 = \mathrm{atan2}(y, x) - \mathrm{atan2}(l_2 \sin q_2, l_1 + l_2 \cos q_2)\). At the workspace boundary (\(|c_2| = 1\)) the two solutions coincide and one row is returned. Unreachable targets return a matrix with zero rows. At the origin with \(l_1 = l_2\) every \(q_1\) is a solution; the function returns \(q_1 = 0\).