Adaptive Feedforward Cancellation

A while back I read this blog post by Ben Katz about this technique, it’s used to adaptively cancel out components of an error signal that are sinusoidal in nature. He uses it to cancel out harmonics in the phase currents, but you can use it to deal with anything that is repeatable and can be mapped into a fixed coordinate frame (like the dq coordinate frame).

Earlier this week I was toying with using it to cancel out speed variations in velocity mode. I got this result with my 50pp stepper motor running at 0.1rad/sec in closed loop mode. This is MUCH better than without, and it’s pretty cool to watch it drive the velocity error towards a flat line.

In this implementation I used several harmonics of the electrical angle, and used velocity_setpoint - measured_velocity as the error signal. This does potentially allow the velocity pid controller to interfere, but it seems like so long as AFC is either much faster or much slower than the PID controller that it’s not an issue. I tried using this to generate an anticogging map, but didn’t spend a lot of time on it. That would be conceptually similar to doing the fourier transform on the velocity signal for one frequency bin and correcting that before moving on to the next bin.

The code to do this is pretty straightforward, and the blog post “pseudocode” is almost valid C, but here’s a link to my implementation. It uses the simplefoc _sincos function, if you’re using the G4 cordic driver it automatically uses that too.

I don’t have the usage in the sfoc motor class uploaded anywhere right now, but it’s a simple modification. If there was a feed forward term for voltage you could implement this without modification to the library.

Anyhow, I thought this was cool and wanted to share it on the forum so it didn’t just get lost to time in the discord.