Online Optimal Impedance Planning for Legged Robots