SimpleFOClibrary  2.1
CurrentSense.cpp
Go to the documentation of this file.
1 #include "CurrentSense.h"
2 
3 
4 // get current magnitude
5 // - absolute - if no electrical_angle provided
6 // - signed - if angle provided
7 float CurrentSense::getDCCurrent(float motor_electrical_angle){
8  // read current phase currents
10  // currnet sign - if motor angle not provided the magnitude is always positive
11  float sign = 1;
12 
13  // calculate clarke transform
14  float i_alpha, i_beta;
15  if(!current.c){
16  // if only two measured currents
17  i_alpha = current.a;
18  i_beta = _1_SQRT3 * current.a + _2_SQRT3 * current.b;
19  }else{
20  i_alpha = 2*(current.a - (current.b - current.c))/3.0;
21  i_beta = _2_SQRT3 *( current.b - current.c );
22  }
23 
24  // if motor angle provided function returns signed value of the current
25  // determine the sign of the current
26  // sign(atan2(current.q, current.d)) is the same as c.q > 0 ? 1 : -1
27  if(motor_electrical_angle)
28  sign = (i_beta * _cos(motor_electrical_angle) - i_alpha*_sin(motor_electrical_angle)) > 0 ? 1 : -1;
29  // return current magnitude
30  return sign*_sqrt(i_alpha*i_alpha + i_beta*i_beta);
31 }
32 
33 // function used with the foc algorihtm
34 // calculating DQ currents from phase currents
35 // - function calculating park and clarke transform of the phase currents
36 // - using getPhaseCurrents internally
38  // read current phase currents
39  PhaseCurrent_s current = getPhaseCurrents();
40 
41  // calculate clarke transform
42  float i_alpha, i_beta;
43  if(!current.c){
44  // if only two measured currents
45  i_alpha = current.a;
46  i_beta = _1_SQRT3 * current.a + _2_SQRT3 * current.b;
47  }else{
48  i_alpha = 0.6666667*(current.a - (current.b - current.c));
49  i_beta = _2_SQRT3 *( current.b - current.c );
50  }
51 
52  // calculate park transform
53  float ct = _cos(angle_el);
54  float st = _sin(angle_el);
55  DQCurrent_s return_current;
56  return_current.d = i_alpha * ct + i_beta * st;
57  return_current.q = i_beta * ct - i_alpha * st;
58  return return_current;
59 }
_sin
float _sin(float a)
Definition: foc_utils.cpp:14
CurrentSense::getPhaseCurrents
virtual PhaseCurrent_s getPhaseCurrents()=0
_sqrt
#define _sqrt(a)
Definition: foc_utils.h:10
_2_SQRT3
#define _2_SQRT3
Definition: foc_utils.h:14
DQCurrent_s
Definition: foc_utils.h:34
CurrentSense::getFOCCurrents
DQCurrent_s getFOCCurrents(float angle_el)
Definition: CurrentSense.cpp:37
_1_SQRT3
#define _1_SQRT3
Definition: foc_utils.h:16
DQCurrent_s::q
float q
Definition: foc_utils.h:36
DQCurrent_s::d
float d
Definition: foc_utils.h:35
CurrentSense::getDCCurrent
virtual float getDCCurrent(float angle_el=0)
Definition: CurrentSense.cpp:7
PhaseCurrent_s
Definition: foc_utils.h:40
PhaseCurrent_s::b
float b
Definition: foc_utils.h:42
_cos
float _cos(float a)
Definition: foc_utils.cpp:39
PhaseCurrent_s::a
float a
Definition: foc_utils.h:41
CurrentSense.h
PhaseCurrent_s::c
float c
Definition: foc_utils.h:43