this post has been too long coming. it’s rough, but I’d rather get it out now lest it stay in unfinished blog post purgatory for ever.

The month of march was nothing but code. Why? Bayleyw and me implemented **FOC** – Field Oriented Control on the inverter controller.

The source is available here.

As a review, here’s what the setup looks like:

see the prius inverter insides [here

](/gen-2-prius-inverter-insides/) see how I made that silly custom motor housing here

All the code goes on to the STM Nucleo. We’re using the mbed IDE (*~*IN THE CLOUD*~*).

## STEP 1: Find some app notes.

best practice for implementing unfamiliar motor control schemes with math that is above your head? Find an application note. People at IC companies are paid to make how-to guides for implementing specific things on their product. this Microchip app note explained the FOC situation pretty well. this one is specific to induction motors, but was helpful for figuring out the SVM block.

## STEP 2: Debug every block in the loop and make sure it does what it’s supposed to do

which is difficult when you’re still not sure how any of this really works. I printed data over serial and plotted it in excel to figure out what’s what

there was a fair amount of this:

a lot of this:

one of these: (figuring out the six-step SVM table)

oh yeah, and this one where I goofed up the clarke transform. That transform works on the pythagorean identity: sin^2(x) + cos^2(x) = 1. The 1 is the DC value that d and q are born from. At this stage in the transforms, both current and angle are expressed as pairs of orthogonal sinusoidal quantities.

If instead you accidentally swap some trig terms so your expression looks like sin(x)*cos(x) + sin(x)*cos(x), then you get d and q outputs that are twice the rotor frequency. by the double angle identity, 2*sin(x)*cos(x) = sin(2x).

Geometrically, this is like rotating the rotor angle reference frame in the opposite direction of the rotating current vectors. Viewed from the backwards turning rotor angle frame, the currents look be oscillating at twice the rotor speed.

I spent a couple days trying to align current sensor reads with the middle of the phase correct PWM.

aww yes.