Bayley Wang / Mbed 2 deprecated foc-ed_in_the_bot_compact

Dependencies:   FastPWM3 mbed

Committer:
bwang
Date:
Sun Nov 27 09:10:09 2016 +0000
Revision:
36:cac9785c91cb
Parent:
32:b31423041c4e
Child:
37:ba7ebf4f8a78
working gokart, stubs for maximum torque per amp based on torque, converting to mpta based on total phase current

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bwang 31:ebe42589ab9d 1 #include <math.h>
bwang 31:ebe42589ab9d 2
bwang 31:ebe42589ab9d 3 #include "config_motor.h"
bwang 31:ebe42589ab9d 4 #include "Optimize.h"
bwang 31:ebe42589ab9d 5 #include "optimize_constants.h"
bwang 31:ebe42589ab9d 6
bwang 32:b31423041c4e 7 void get_mtpa_dq(float tau, float *d, float *q) {
bwang 31:ebe42589ab9d 8 float tau2 = tau * tau;
bwang 31:ebe42589ab9d 9 float alpha = val_alpha(tau, tau2);
bwang 32:b31423041c4e 10 float u = sqrtf(cA * tau2 / alpha + cB * alpha);
bwang 32:b31423041c4e 11 float v = sqrtf(cC * tau2 / alpha + cD * alpha + cE * tau / u);
bwang 31:ebe42589ab9d 12 *q = cU * u + cV * v;
bwang 31:ebe42589ab9d 13 *d = (-3 * *q * POLE_PAIRS * FLUX_LINKAGE + 2 * tau) / (3 * *q * Lx * POLE_PAIRS);
bwang 31:ebe42589ab9d 14 }
bwang 31:ebe42589ab9d 15
bwang 32:b31423041c4e 16 void get_mtpf_dq(float tau, float w, float umax, float *d, float *q) {
bwang 32:b31423041c4e 17 float x = umax / w;
bwang 32:b31423041c4e 18 x = x * x;
bwang 32:b31423041c4e 19 x *= cH;
bwang 32:b31423041c4e 20 *d = cF - cG - sqrtf(cH * cH + 0.5f * x);
bwang 32:b31423041c4e 21 float y = cI - sqrtf(cI * cI + 0.5f * x);
bwang 32:b31423041c4e 22 *q = x + y * y;
bwang 32:b31423041c4e 23 }
bwang 32:b31423041c4e 24
bwang 31:ebe42589ab9d 25 float val_alpha(float tau, float tau2) {
bwang 32:b31423041c4e 26 float tau23 = acbrt(tau2);
bwang 31:ebe42589ab9d 27 if (tau < TAU0) {
bwang 31:ebe42589ab9d 28 return tau23 * K1_13 * (c0 + c2 * tau2 + c4 * tau2 * tau2);
bwang 31:ebe42589ab9d 29 }
bwang 31:ebe42589ab9d 30 else {
bwang 31:ebe42589ab9d 31 return s0 + s1 * (tau - TAU0);
bwang 31:ebe42589ab9d 32 }
bwang 31:ebe42589ab9d 33 }
bwang 31:ebe42589ab9d 34
bwang 32:b31423041c4e 35 float acbrt(float x0) {
bwang 32:b31423041c4e 36 union { int ix; float x; };
bwang 31:ebe42589ab9d 37
bwang 32:b31423041c4e 38 x = x0; // x can be viewed as int.
bwang 32:b31423041c4e 39 ix = 0x2a5137a0 + ix / 3; // Initial guess.
bwang 32:b31423041c4e 40 x = 0.33333333f*(2.0f*x + x0 / (x * x)); // Newton step.
bwang 32:b31423041c4e 41
bwang 32:b31423041c4e 42 return x;
bwang 31:ebe42589ab9d 43 }