Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Foc calculation refactoring #311

Open
Candas1 opened this issue Sep 19, 2023 · 3 comments
Open

[FEATURE] Foc calculation refactoring #311

Candas1 opened this issue Sep 19, 2023 · 3 comments
Labels
enhancement New feature or request

Comments

@Candas1
Copy link
Collaborator

Candas1 commented Sep 19, 2023

Hi,

This is not adding a feature, but could make further changes easier.
There is duplicate code here and here

    float i_alpha, i_beta;
    if(!current.c){
        // if only two measured currents
        i_alpha = current.a;  
        i_beta = _1_SQRT3 * current.a + _2_SQRT3 * current.b;
    }if(!current.a){
        // if only two measured currents
        float a = -current.c - current.b;
        i_alpha = a;  
        i_beta = _1_SQRT3 * a + _2_SQRT3 * current.b;
    }if(!current.b){
        // if only two measured currents
        float b = -current.a - current.c;
        i_alpha = current.a;  
        i_beta = _1_SQRT3 * current.a + _2_SQRT3 * b;
    } else {
        // signal filtering using identity a + b + c = 0. Assumes measurement error is normally distributed.
        float mid = (1.f/3) * (current.a + current.b + current.c);
        float a = current.a - mid;
        float b = current.b - mid;
        i_alpha = a;
        i_beta = _1_SQRT3 * a + _2_SQRT3 * b;
    }

This could be made a function getAlphaBeta or getClarkTransform that uses foc currents as inputs, and returns alpha and beta.
A function getParkTransform could use the electrical angle, alpha and beta and return DQcurrents.
getDCCurrent could use the electrical angle, alpha and beta and return DCcurrent.

Eventually, foc_current torque control type in loopfoc would do:

  • getPhaseCurrents
  • getClarkTransform
  • getParkTransform
  • PI control

And dc_current torque control type in loopfoc would do:

  • getPhaseCurrents
  • getClarkTransform
  • getDCcurrent
  • PI control
@Candas1 Candas1 added the enhancement New feature or request label Sep 19, 2023
@Candas1
Copy link
Collaborator Author

Candas1 commented Sep 19, 2023

If this generates too much work (changing the examples, ....)
This alternative should have less impact:

getfoccurrents:

  • getPhaseCurrents
  • getClarkTransform
  • getParkTransform
  • return dqcurrents

getdccurrents:

  • getPhaseCurrents
  • getClarkTransform
  • calculate dc current
  • return dccurrent

and no changes in loopfoc.

@Candas1
Copy link
Collaborator Author

Candas1 commented Sep 24, 2023

I only added a getABCurrents function and it looks like that

before:
RAM: [= ] 9.2% (used 4536 bytes from 49152 bytes)
Flash: [=== ] 27.3% (used 71540 bytes from 262144 bytes)
loopfoc = 260us

RAM: [= ] 9.2% (used 4536 bytes from 49152 bytes)
Flash: [=== ] 27.3% (used 71500 bytes from 262144 bytes)
loopfoc = 260us

So no real space saving, no performance impact, but a single function to enhance in the future.
If you think it's useful I can send you the PR.

@Candas1
Copy link
Collaborator Author

Candas1 commented Sep 24, 2023

I thought a getDQCurrents function (park transform) wouldn't be useful because it's used only once.
But if you guys want to implement FOC for steppers, you could reuse such a function.
Inverse park is used for both bldc and stepper motors also, it could be a getABVoltages function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant