Control and safety of fully actuated and underactuated nonlinear systems: from adaptation to robustness to optimality