Unified Inverse Dynamics of Modular Serial Mechanical Systems with Application to Soft Robotics