1. A system for self-tuning a PID controller for controlling an exciter and generator comprising: a power source; an exciter electrically connected to the power source; a generator that is electrically energized by the exciter; a processor that provides a PID controller and also for calculating an estimated exciter time constant and an estimated generator time constant using particle swarm optimization, wherein the calculating of the estimated exciter time constant and the estimated generator time constant comprises: performing a system disturbance by increasing a voltage reference by a predetermined percentage over a predetermined time period, initializing each particle position of the particle swarm optimization with an initial exciter time constant and generator time constant, calculating a generator voltage, performing a fitness evaluation, and obtaining and updating the best values; and repeating the calculating, fitness evaluation, and updating over a predetermined number of iterations; and wherein the processor further calculates gain values for the PID controller based on the estimated exciter time constant and estimated generator time constant and provides the gain values to the PID controller for control of the exciter field voltage.