ac-power
Reference frames, transforms, and trig for embedded processing of AC power signals.
Reference Frames
At the core of the library are data structs which represent three-phase AC vectors in different reference frames.
The crate supports 6 difference reference frames. These include 3 balanced reference frames:
- Polar - Polar representation (amplitude and angle)
- AlphaBeta - Orthogonal (alpha and beta) stationary reference frame representation
- Dq - Two axis (d and q) rotating reference frame representation
And three reference frames for unbalanced representations (supports a zero sequence component):
- Abc - Instantaneous signals
- AlphaBeta0 - Orthogonal(alpha and beta) stationary reference frame representation with zero
- Dq0 - Two axis (d and q) rotating reference frame representation with zero
Converting between reference frames invokes power theory transforms.
use ;
// create a vector in Abc reference frame
let abc = Abc ;
// convert to alpha-beta
let alpha_beta_zero = from;
Trigonometry
The library also includes a trig module, which is useful when converting between stationary and rotating reference frames.
use ;
use ;
// create a vector in Abc reference frame
let abc = Abc ;
// convert to Dq0
let = cos_sin;
let dq0 = abc.to_dq0;
There are additional functions in the trig module for rotating Sin/Cos pairs or generating Sin(Nx), Cos(Nx) pairs using Chebyshev method.
Newtypes
From the example above we see that there are some newtypes defined in this crate. Specifically, there are three defined in the trig module:
- Theta(i32) - An angle between -π and π radians
- Sin(f32) - Sin of an angle
- Cos(f32) - Cos of an angle
There are also 4 additional newtypes defined in this crate:
- Voltage(f32) - An electric voltage
- Current(f32) - An electric current
- Power(f32) - An electric power
- Impedance(f32) - An electric impedance
Meaningful type conversions automatically occur during mulitplication of different types.
use ;
let z = from;
let i = from;
let v: Voltage = i * z;
let p: Power = v * i;
The reference frames are implemented with generics, so they can be used with regular f32s as seen in the examples above, or any data-type that implements the necessary numeric traits. The 4 additional newtypes defined above all do.
use ;
// define a voltage vector in Abc reference frame
let v: = Abc ;
Power Calculations
When you create AC reference frame vectors out of Voltage and Current types, they can be multiplied by each other to return a Pq struct. This is a basic use case to calculate real and reactive powers from three-phase voltage and current data.
use ;
use ;
use assert_abs_diff_eq;
// set the magnitude of the voltage and current
let v_mag = from;
let i_mag = from;
// create voltage and current vectors in the Abc reference frame
let v = from_polar;
let i = from_polar;
// calculate P and Q
let pq = v * i;
// calculate the power factor
let pf = pq.power_factor;
// check the power factor
assert_abs_diff_eq!;
// convert v and i to alpha_beta
let v_alpha_beta = from;
let i_alpha_beta = from;
// verify the power factor is still correct
let pf = .power_factor;
assert_abs_diff_eq!;
Advanced Use Cases
Many inverter control systems that implement advanced grid controls or grid forming controls also rely on the transforms implemented in this crate. Use of this crate can not only make the application code much more readible, it can improve performance and eliminate bugs due to the extensive optimization and verification of this crate. Bellow are a few examples.
A Grid Synchronizing Phased-Locked-Loop (PLL)
Bellow is an example of a simple three-phase Phased Locked Loop implementation, a common DSP block in inverter controls and advanced power meters, to illustrate how the crate can be used to facillitate such applications.
use ;
use ;
use Voltage;
use ;
A Three-Phase Waveform Generator
Bellow is an example of a three-phase waveform generator that supports unbalanced representations as well as harmonics.
use Num;
use ;
use ;