For a constant $a,$ let $f(x)=ax\sin x+x+\frac{\pi}{2}.$ Find the range of $a$ such that $\int_{0}^{\pi}\{f'(x)\}^{2}\ dx \geq f\left(\frac{\pi}{2}\right).$
1. Given the function \( f(x) = ax \sin x + x + \frac{\pi}{2} \), we first find its derivative:
   \[
   f'(x) = \frac{d}{dx} \left( ax \sin x + x + \frac{\pi}{2} \right) = a \sin x + ax \cos x + 1
   \]

2. We need to evaluate the integral:
   \[
   \int_{0}^{\pi} \left( f'(x) \right)^2 \, dx = \int_{0}^{\pi} \left( a \sin x + ax \cos x + 1 \right)^2 \, dx
   \]

3. Expanding the integrand:
   \[
   \left( a \sin x + ax \cos x + 1 \right)^2 = a^2 \sin^2 x + 2a^2 x \sin x \cos x + a^2 x^2 \cos^2 x + 2a \sin x + 2ax \cos x + 1
   \]

4. We now integrate each term separately:
   \[
   \int_{0}^{\pi} a^2 \sin^2 x \, dx + \int_{0}^{\pi} 2a^2 x \sin x \cos x \, dx + \int_{0}^{\pi} a^2 x^2 \cos^2 x \, dx + \int_{0}^{\pi} 2a \sin x \, dx + \int_{0}^{\pi} 2ax \cos x \, dx + \int_{0}^{\pi} 1 \, dx
   \]

5. Using standard integrals and symmetry properties:
   \[
   \int_{0}^{\pi} \sin^2 x \, dx = \frac{\pi}{2}, \quad \int_{0}^{\pi} x \sin x \cos x \, dx = 0, \quad \int_{0}^{\pi} x^2 \cos^2 x \, dx = \frac{\pi^3}{6}
   \]
   \[
   \int_{0}^{\pi} \sin x \, dx = 0, \quad \int_{0}^{\pi} x \cos x \, dx = 0, \quad \int_{0}^{\pi} 1 \, dx = \pi
   \]

6. Substituting these results:
   \[
   \int_{0}^{\pi} \left( f'(x) \right)^2 \, dx = a^2 \frac{\pi}{2} + a^2 \frac{\pi^3}{6} + \pi
   \]

7. We need to compare this with \( f\left( \frac{\pi}{2} \right) \):
   \[
   f\left( \frac{\pi}{2} \right) = a \frac{\pi}{2} \sin \frac{\pi}{2} + \frac{\pi}{2} + \frac{\pi}{2} = a \frac{\pi}{2} + \pi
   \]

8. Setting up the inequality:
   \[
   a^2 \frac{\pi}{2} + a^2 \frac{\pi^3}{6} + \pi \geq a \frac{\pi}{2} + \pi
   \]

9. Simplifying the inequality:
   \[
   a^2 \frac{\pi}{2} + a^2 \frac{\pi^3}{6} \geq a \frac{\pi}{2}
   \]
   \[
   a^2 \left( \frac{\pi}{2} + \frac{\pi^3}{6} \right) \geq a \frac{\pi}{2}
   \]

10. Factoring out \( a \):
    \[
    a \left( a \left( \frac{\pi}{2} + \frac{\pi^3}{6} \right) - \frac{\pi}{2} \right) \geq 0
    \]

11. Solving the quadratic inequality:
    \[
    a \left( a \left( \frac{\pi}{2} + \frac{\pi^3}{6} \right) - \frac{\pi}{2} \right) \geq 0
    \]
    \[
    a \left( a \left( \frac{\pi (3 + 2\pi^2)}{6} \right) - \frac{\pi}{2} \right) \geq 0
    \]
    \[
    a \left( a \left( \frac{\pi (3 + 2\pi^2)}{6} \right) - \frac{\pi}{2} \right) \geq 0
    \]

12. Solving for \( a \):
    \[
    a \left( a \left( \frac{3 + 2\pi^2}{6} \right) - \frac{1}{2} \right) \geq 0
    \]
    \[
    a \left( a \left( \frac{3 + 2\pi^2}{6} \right) - \frac{1}{2} \right) \geq 0
    \]

13. The roots of the quadratic equation are:
    \[
    a = 0 \quad \text{or} \quad a = \frac{3}{3 + 2\pi^2}
    \]

14. The solution to the inequality is:
    \[
    -\infty < a \leq 0 \quad \text{or} \quad \frac{6}{3 + 2\pi^2} \leq a < \infty
    \]

The final answer is \( \boxed{ -\infty < a \leq 0 \cup \frac{6}{3 + 2\pi^2} \leq a < \infty } \).