Skip to content

Commit 8727908

Browse files
committed
refactoring of FOC
1 parent 1694fa3 commit 8727908

File tree

3 files changed

+47
-39
lines changed

3 files changed

+47
-39
lines changed

src/common/base_classes/CurrentSense.cpp

Lines changed: 30 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,12 @@
77
float CurrentSense::getDCCurrent(float motor_electrical_angle){
88
// read current phase currents
99
PhaseCurrent_s current = getPhaseCurrents();
10-
// currnet sign - if motor angle not provided the magnitude is always positive
11-
float sign = 1;
12-
10+
1311
// 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-
}if(!current.a){
20-
// if only two measured currents
21-
float a = -current.c - current.b;
22-
i_alpha = a;
23-
i_beta = _1_SQRT3 * a + _2_SQRT3 * current.b;
24-
}if(!current.b){
25-
// if only two measured currents
26-
float b = -current.a - current.c;
27-
i_alpha = current.a;
28-
i_beta = _1_SQRT3 * current.a + _2_SQRT3 * b;
29-
}else{
30-
// signal filtering using identity a + b + c = 0. Assumes measurement error is normally distributed.
31-
float mid = (1.f/3) * (current.a + current.b + current.c);
32-
float a = current.a - mid;
33-
float b = current.b - mid;
34-
i_alpha = a;
35-
i_beta = _1_SQRT3 * a + _2_SQRT3 * b;
36-
}
12+
ABCurrent_s ABcurrent = getABCurrents(current);
13+
14+
// current sign - if motor angle not provided the magnitude is always positive
15+
float sign = 1;
3716

3817
// if motor angle provided function returns signed value of the current
3918
// determine the sign of the current
@@ -42,20 +21,38 @@ float CurrentSense::getDCCurrent(float motor_electrical_angle){
4221
float ct;
4322
float st;
4423
_sincos(motor_electrical_angle, &st, &ct);
45-
sign = (i_beta*ct - i_alpha*st) > 0 ? 1 : -1;
24+
sign = (ABcurrent.beta*ct - ABcurrent.alpha*st) > 0 ? 1 : -1;
4625
}
4726
// return current magnitude
48-
return sign*_sqrt(i_alpha*i_alpha + i_beta*i_beta);
27+
return sign*_sqrt(ABcurrent.alpha*ABcurrent.alpha + ABcurrent.beta*ABcurrent.beta);
4928
}
5029

5130
// function used with the foc algorihtm
5231
// calculating DQ currents from phase currents
5332
// - function calculating park and clarke transform of the phase currents
54-
// - using getPhaseCurrents internally
33+
// - using getPhaseCurrents and getABCurrents internally
5534
DQCurrent_s CurrentSense::getFOCCurrents(float angle_el){
5635
// read current phase currents
5736
PhaseCurrent_s current = getPhaseCurrents();
5837

38+
// calculate clarke transform
39+
ABCurrent_s ABcurrent = getABCurrents(current);
40+
41+
// calculate park transform
42+
float ct;
43+
float st;
44+
_sincos(angle_el, &st, &ct);
45+
DQCurrent_s return_current;
46+
return_current.d = ABcurrent.alpha * ct + ABcurrent.beta * st;
47+
return_current.q = ABcurrent.beta * ct - ABcurrent.alpha * st;
48+
return return_current;
49+
}
50+
51+
// function used with the foc algorihtm
52+
// calculating Alpha Beta currents from phase currents
53+
// - function calculating Clarke transform of the phase currents
54+
ABCurrent_s CurrentSense::getABCurrents(PhaseCurrent_s current){
55+
5956
// calculate clarke transform
6057
float i_alpha, i_beta;
6158
if(!current.c){
@@ -81,14 +78,10 @@ DQCurrent_s CurrentSense::getFOCCurrents(float angle_el){
8178
i_beta = _1_SQRT3 * a + _2_SQRT3 * b;
8279
}
8380

84-
// calculate park transform
85-
float ct;
86-
float st;
87-
_sincos(angle_el, &st, &ct);
88-
DQCurrent_s return_current;
89-
return_current.d = i_alpha * ct + i_beta * st;
90-
return_current.q = i_beta * ct - i_alpha * st;
91-
return return_current;
81+
ABCurrent_s return_ABcurrent;
82+
return_ABcurrent.alpha = i_alpha;
83+
return_ABcurrent.beta = i_beta;
84+
return return_ABcurrent;
9285
}
9386

9487
/**

src/common/base_classes/CurrentSense.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class CurrentSense{
5353
virtual PhaseCurrent_s getPhaseCurrents() = 0;
5454
/**
5555
* Function reading the magnitude of the current set to the motor
56-
* It returns the abosolute or signed magnitude if possible
56+
* It returns the absolute or signed magnitude if possible
5757
* It can receive the motor electrical angle to help with calculation
5858
* This function is used with the current control (not foc)
5959
*
@@ -62,13 +62,22 @@ class CurrentSense{
6262
virtual float getDCCurrent(float angle_el = 0);
6363

6464
/**
65-
* Function used for FOC contorl, it reads the DQ currents of the motor
65+
* Function used for FOC control, it reads the DQ currents of the motor
6666
* It uses the function getPhaseCurrents internally
6767
*
6868
* @param angle_el - motor electrical angle
6969
*/
7070
DQCurrent_s getFOCCurrents(float angle_el);
7171

72+
/**
73+
* Function used for Clarke transform in FOC control
74+
* It reads the phase currents of the motor
75+
* It returns the alpha and beta currents
76+
*
77+
* @param current - phase current
78+
*/
79+
ABCurrent_s getABCurrents(PhaseCurrent_s current);
80+
7281

7382
};
7483

src/common/foc_utils.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ struct DQVoltage_s
5656
float d;
5757
float q;
5858
};
59+
// alpha beta current structure
60+
struct ABCurrent_s
61+
{
62+
float alpha;
63+
float beta;
64+
};
5965

6066

6167
/**

0 commit comments

Comments
 (0)