UVW 3 phases Brushless DC motor control
Dependencies: QEI mbed-rtos mbed
Fork of DCmotor by
fast_math.cpp
- Committer:
- kosaka
- Date:
- 2012-12-21
- Revision:
- 12:a4b17bb682eb
- Child:
- 13:791e20f1af43
File content as of revision 12:a4b17bb682eb:
#include "mbed.h" #include "fast_math.h" unsigned short sin60[DEG60+1]; // sin table from 0 to 60 deg. (max precision error is 0.003%) long _sin(unsigned short th){ // return( 65535*sin(th) ), th=rad*DEG60/(PI/3)=rad*(512*3)/PI (0<=rad<2*PI) // init_sin60(); // if( th>2.*PI ){ th -= 2*PI*(float)((int)(th/(2.*PI)));} // th_int = (unsigned short)(th/(PI/3.)*(float)DEG60+0.5); // rad to deg // sin = (float)_sin(th)/65535.; unsigned short f_minus; long x; if( th>=DEG60*3){ f_minus = 1; th -= DEG60*3;} // if th>=180deg, th = th - 180deg; else{ f_minus = 0;} // else , f_minus = on. if( th<DEG60 ){ // th<60deg? x = sin60[th]; // sin(th) }else if( th<DEG60*2 ){ // 60<=th<120deg? x = sin60[DEG60*2-th] + sin60[th-DEG60]; // sin(th)=sin(th+60)+sin(th-60)=sin(180-(th+60))+sin(th-60) because sin(th+60)=s/2+c*root(3)/2, sin(th-60)=s/2-c*root(3)/2. }else{// if( th<180 ) // 120<=th<180deg? x = sin60[DEG60*3-th]; // sin(60-(th-120))=sin(180-th) } if( f_minus==1 ){ x = -x;} return(x); } long _cos(unsigned short th){ // return( 65535*sin(th) ), th=rad*DEG60/(PI/3)=rad*(512*3)/PI (0<=rad<2*PI) th += DEG60*3/2; if( th>=DEG60*6 ){ th -= DEG60*6;} return( _sin(th) ); } void init_fast_math(){ // sin0-sin60deg; 0deg=0, 60deg=512 int i; for( i=0;i<=DEG60;i++ ){ // set sin table from 0 to 60 deg.. // sin60[i] = (unsigned short)(sin((float)i/512.*PI/3.)); sin60[i] = (unsigned short)(65535.*sinf((float)i/(float)DEG60*PI/3.)); } } #if 0 //float norm(float x[0], float x[1]){ // 2ノルムを計算 // return(sqrt(x[0]*x[0]+x[1]*x[1])); //} float sqrt2(float x){ // √xのx=1まわりのテイラー展開 √x = 1 + 1/2*(x-1) -1/4*(x-1)^2 + ... // return((1+x)*0.5); // 一次近似 return(x+(1-x*x)*0.25); // 二次近似 } #endif