#### Equip your Genius

Welcome to the Parallax Discussion Forums, sign-up to participate.

# Park transformation for motor control

Posts: 547
The Park or d/q-transformation is useful when controlling AC motors or brushless servo motors. It transforms currents or voltages from a three phase system with stator reference into a rectangular coordinate system that is referenced to the rotor. The output can be treated as if the motor was a DC machine. After calculations (PID control) are done the values are transformed back to the three phase system using the inverse Park transformation. The results can then be output to a three phase power stage.

The calculation of the matrix coefficients and the matrix multiplication normally are very complex operations. You'd need sine tables and lots of multiplications filling multiple pages with hundrets of instructions. But not on the P2 where this can be done quite elegant and efficient with the CORDIC solver.
```{ Park transformation

Park matrix: [+cos(T) +cos(T-120°) +cos(T+120°)]
[-sin(T) -sin(T-120°) -sin(T+120°)]

PM x (Iu Iv Iw) = (Id Iq)

Id:=  Iu*cos(T) + Iv*cos(T-120°) + Iw*cos(T+120°)
Iq:= -Iu*sin(T) - Iv*sin(T-120°) - Iw*sin(T+120°)

QROTATE performs x:= D*cos(S) - Q*sin(S)
y:= D*sin(S) + Q*cos(S)

So the matrix multiplication can be carried out with 3 QROTATE operations
with Q=0, summing up the X/Y results and then scaling them by 2/3.

x1,y1:= QROTATE Iu,T
x2,y2:= QROTATE Iv,T-120°
x3,y3:= QROTATE Iw,T+120°
Id:= 2/3 * (x1 + x2 + x3)
Iq:= -2/3 * (y1 + y2 + y3)
}

parkTrafo	qrotate	actU,theta	' input: actU,V,W, theta
sub	theta,angle120
qrotate actV,theta
add	theta,angle240
qrotate actW,theta
sub	theta,angle120
getqx	actD
getqy	actQ
getqx	x
getqy	y
add	actD,x		' output: actD, actQ
add	actQ,y
getqx	x
getqy	y
add	actD,x
add	actQ,y
scas	actD,##\$2AAA	' *= +2/3
mov	actD,#0
scas	actQ,##\$D555	' *= -2/3
mov	actQ,#0
ret

angle120	long	\$5555_5555 ' 120° as unsigned long
angle240	long	\$AAAA_AAAA ' 240°
```

## Comments

• Posts: 12,640
Right on!!!
• Posts: 547
And here is the inverse:
```{ Inverse Park transformation

Park-1 matrix: [+cos(T)      -sin(T)     ]
[+cos(T-120°) -sin(T-120°)]
[+cos(T+120°) -sin(T+120°)]

IM x (Vd Vq) = (Vu Vv Vw)

Vu:= Vd*cos(T)      - Vq*sin(T)
Vv:= Vd*cos(T-120°) - Vq*sin(T-120°)
Vw:= Vd*cos(T+120°) - Vq*sin(T+120°)

So the matrix multiplication can be again carried out with 3 QROTATE
operations. But this time 2 input operands are used and only the X
result is taken.

Vu:= QROTATE Vd,Vq,T
Vv:= QROTATE Vd,Vq,T-120°
Vw:= QROTATE Vd,Vq,T+120°
}
inverseTrafo	setq	dutyQ
qrotate dutyD,theta
sub	theta,angle120
setq	dutyQ
qrotate	dutyD,theta
add	theta,angle240
setq	dutyQ
qrotate	dutyD,theta
sub	theta,angle120
getqx	dutyU
getqx	dutyV
getqx	dutyW
ret
```

I forgot to say that I use 16 bit signed format for all current and voltage values. This should be sufficient for most applications. The CORDIC even could do full 32 bit resolution, only the scaling had to use 32 bit multiplication instead of the SCAS instructions. The angle (theta) has to be left adjusted.
• Posts: 1,617
I can see you selling out to Siemens before long
• Posts: 369
I was looking for this the other day! Great timing @ManAtWork

We really need a wiki for gems like this. There are so many of these golden nuggets that get buried under the sheer volume of posts.
• Posts: 8,998
Cool! Beyond my knowledge for sure.

JRoark,
Cluso's sticky will do for the moment - https://forums.parallax.com/discussion/169542/p2-links-for-where-to-obtain-tools-sample-test-code-reference-only

• Posts: 284
High-speed HDMI and USB enabled multi-channel 3-phase AC motor controller with built-in performance logging to SD anyone?
• Posts: 12,640
What are you saying, AJL?
• Posts: 14,278
cgracey wrote: »
What are you saying, AJL?

I think what AJL is talking about, would be quite useful in motor research labs, where the display could graph real-time effects and measurements, and the USB/SD allow easy changes for testing.

There are also wireless power transfer projects, where that combination would be very nice to have.
• Posts: 1,999
edited 2020-02-06 - 03:54:56
Multi motor controller / sequencer with inbuilt touchscreen GUI to configure it all from the P2 plus some power FETs etc. Sounds good for some custom CNC or other general motor control applications needing manual configuration options and other information status display. Probably lots of fun applications there, assembly line process control, robotics etc.
• Posts: 547
JRoark wrote: »
I was looking for this the other day! Great timing @ManAtWork
We really need a wiki for gems like this. There are so many of these golden nuggets that get buried under the sheer volume of posts.

Yeah, agreed. I thought about putting this into OBEX (GIT hub). But these "nuggets" are part of a bigger puzzle which still has to be put together. So I think I'll wait until all the interfaces are clear. A piece of assembler code alone is not very useful to anyone else except as example to learn from.
• Posts: 284
jmg wrote: »
cgracey wrote: »
What are you saying, AJL?

I think what AJL is talking about, would be quite useful in motor research labs, where the display could graph real-time effects and measurements, and the USB/SD allow easy changes for testing.

There are also wireless power transfer projects, where that combination would be very nice to have.

That and probably more.
I was hinting at all the great experimentation possibilities in one board with the P2 at the heart of it, and only really needing power drive stages to support it.
• Posts: 547
It's far to early to promise anything but I thought of probably releasing my single axis servo controller project as open source or some sort of reference design. I'm not sure if it makes sense to put some of the code to the OBEX library. If I program for my own project I can hard code the pin numbers, buffer sizes and things like that. It is much more work to write a general purpose library where everything must be configurable and more robustness (range checking and error handling) is required.
• Posts: 12,640
> @ManAtWork said:
> It's far to early to promise anything but I thought of probably releasing my single axis servo controller project as open source or some sort of reference design. I'm not sure if it makes sense to put some of the code to the OBEX library. If I program for my own project I can hard code the pin numbers, buffer sizes and things like that. It is much more work to write a general purpose library where everything must be configurable and more robustness (range checking and error handling) is required.

All that extra code can make things less understandable. For learning, something simpler with maybe hard-coded values is okay because it lets the concept shine through more clearly.
• Posts: 602
In the old OBEX, there was a distinction between ready-to-use libraries and "snippets". Does this destinction exist in the Git repo?
Sign In or Register to comment.