Frequency response and voltage support are vital ancillary services for power grids. In this paper, we design and experimentally validate a real-time control framework for battery energy storage systems (BESSs) to provide ancillary services to power grids. The objective of the control system is to utilize the full capability of the BESSs to provide ancillary services. We take the voltage-dependent capability curve of the DC-AC converter and the security requirements of BESSs as constraints of the control system. The initial power set-points are obtained based on the droop control approach. To guarantee the feasibility of the power set-points with respect to both the converter capability and BESS security constraints, the final power set-points calculation is formulated as a nonconvex optimization problem. A convex and computationally efficient reformulation of the original control problem is then proposed. We prove that the proposed convex optimization gives the global optimal solution to the original nonconvex problem. We improve the computational performance of this algorithm by discretizing the feasible region of the optimization model. We achieve a 100 ms update time of the controller setpoint computation in the experimental validation of the utility-scale 720 kVA / 560 kWh BESS on the EPFL campus.