  Defects in solid state materials provide an ideal, robust platform for
quantum sensing. To deliver maximum sensitivity, a large ensemble of
non-interacting defects hosting coherent quantum states are required. Control
of such an ensemble is challenging due to the spatial variation in both the
defect energy levels and in any control field across a macroscopic sample. In
this work we experimentally demonstrate that we can overcome these challenges
using Floquet theory and optimal control optimization methods to efficiently
and coherently control a large defect ensemble, suitable for sensing. We apply
our methods experimentally to a spin ensemble of up to 4 $\times$ 10$^9$
nitrogen vacancy (NV) centers in diamond. By considering the physics of the
system and explicitly including the hyperfine interaction in the optimization,
we design shaped microwave control pulses that can outperform conventional
($\pi$-) pulses when applied to sensing of temperature or magnetic field, with
a potential sensitivity improvement between 11 and 78\%. Through dynamical
modelling of the behaviour of the ensemble, we shed light on the physical
behaviour of the ensemble system and propose new routes for further
improvement.
