Bayley Wang / Mbed 2 deprecated foc-ed_in_the_bot_compact

Dependencies:   FastPWM3 mbed

Revision:
32:b31423041c4e
Parent:
31:ebe42589ab9d
Child:
36:cac9785c91cb
--- a/Optimize/Optimize.cpp	Thu Nov 10 14:43:25 2016 +0000
+++ b/Optimize/Optimize.cpp	Sat Nov 12 05:26:15 2016 +0000
@@ -4,17 +4,26 @@
 #include "Optimize.h"
 #include "optimize_constants.h"
 
-void get_ipm_dq(float tau, float *d, float *q) {
+void get_mtpa_dq(float tau, float *d, float *q) {
     float tau2 = tau * tau;
     float alpha = val_alpha(tau, tau2);
-    float u = fastsqrtf(cA * tau2 / alpha + cB * alpha);
-    float v = fastsqrtf(cC * tau2 / alpha + cD * alpha + cE * tau / u);
+    float u = sqrtf(cA * tau2 / alpha + cB * alpha);
+    float v = sqrtf(cC * tau2 / alpha + cD * alpha + cE * tau / u);
     *q = cU * u + cV * v;
     *d = (-3 * *q * POLE_PAIRS * FLUX_LINKAGE + 2 * tau) / (3 * *q * Lx * POLE_PAIRS);
 }
 
+void get_mtpf_dq(float tau, float w, float umax, float *d, float *q) {
+    float x = umax / w;
+    x = x * x;
+    x *= cH;
+    *d  = cF - cG - sqrtf(cH * cH + 0.5f * x);
+    float y = cI - sqrtf(cI * cI + 0.5f * x);
+    *q = x + y * y;
+}
+
 float val_alpha(float tau, float tau2) {
-    float tau23 = fastcubertf(tau2);
+    float tau23 = acbrt(tau2);
     if (tau < TAU0) {
         return tau23 * K1_13 * (c0 + c2 * tau2 + c4 * tau2 * tau2);
     }
@@ -24,10 +33,12 @@
 
 }
 
-float fastsqrtf(float x) {
-    return sqrtf(x);
-}
+float acbrt(float x0) {
+    union { int ix; float x; };
 
-float fastcubertf(float x) {
-    return powf(x, 1 / 3.f);
+    x = x0;                      // x can be viewed as int.
+    ix = 0x2a5137a0 + ix / 3;        // Initial guess.  
+    x = 0.33333333f*(2.0f*x + x0 / (x * x));  // Newton step.
+
+    return x;
 }
\ No newline at end of file