So the spikes occur at random?
Educated guess, this seems like a hardware issue, not software. Something is shooting through the gate and not clear what. Please trace the signal back to the MCU, I suppose, you will see the spurious signal occurring on the gates but not on the driver MCU PWM control input.
Also, I don’t think this noise is due to the spikes. Please comment out the
motor.monitor();
This consumes a lot of resources and on a smaller MCUs this will create a low-frequency humming due to holding the motor while the software monitor is working.
F4 should be probably good enough but you need to find another way to control the motor speed, Serial monitor holds / blocks the closed loop and this is probably the humming you hear.
Extreme case, you may want to introduce a “gap” and read the serial monitor only every tenth of a second or so, this will create a very low frequency infrasound not hearable by human ears.
Put this inside the code to read every 1/10’th of a second:
In setup() :
unsigned long prev = millis();
unsigned long current = millis();
unsigned long threshold = 100; // Occurs every 100 milliseconds
In loop () :
current = millis();
if ((current - prev) > threshold) {
prev = current;
motor.monitor();
}
So you got two problems to solve.
Last but not least, you set the PWM frequency to 10khz, this will blow the ears of young people and dogs and cats (any small animal), they can hear 10k really easy, go for at least 16k, this will create a little more heat loss inside the IGBT but your ears will be better off.
Cheers,
Valentine