-
Notifications
You must be signed in to change notification settings - Fork 5
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
CMSIS routines for sin
/cos
are not used in FOC
#67
Comments
For the sake of clearness, |
Hi @pattacini, good spot. I have just verified that the |
Yep @marcoaccame 👍🏻 |
I think I got it. I made a very simple model to investigate this issue, which is illustrated below. I compared the code generated from the same Sin block, where in the first instance I used the plain function, while in the second instance (in green) I enabled the approximation. The first block calls The interpretation is that MathWorks has enhanced the options available in certain functions to let the user decide whether to go with the approximation (i.e., CMSIS1) or not. Footnotes
|
Once ready, I'll apply the necessary fix via: |
Hi @pattacini here is the comparison of the statistics between r2023a and r2023b for the FOC control of both duration and frequency AMCBLDC (experiment_supervisor_user FOC timing test)
As we can see they are pretty similar. "Unfortunately", does not change a lot :( |
This is quite surprising. Essentially, this means that calls to At any rate, in the recent past, I saw that the LUT used by CMSIS contains 512 positions. Maybe, too many. |
Maybe we can try to create an ad-hoc test to validate this statement further. A simple application with the call to the |
Good idea 👍🏻 |
Considering the cumbersomeness required by the use of CMSIS, if we demonstrate practically that STD and CMSIS perform equally, then I'm in favor of rolling back to STD. |
A couple of relevant resources:
Unless we want to try out CORDIC with the native MATLAB implementation of SinCos, they tell us that it's probably a good option to roll back to STD. Let's do the planned tests anyway! |
I performed a quick test using the basic project on
Note This table contains the average time used to calculate the trigonometric function 1 time. Here is the code I used for the above tests: void test_init();
void test_on();
void test_off();
constexpr uint32_t N_TIMES {100};
constexpr float32_t step = (2*PI) / N_TIMES;
int main() {
test_init();
// Preparing the array of samples
std::array<float_t, N_TIMES> values = {0};
for(int i = 0; i < N_TIMES; i++)
{
values[i] = i*step;
}
__disable_irq();
for(;;)
{
test_on();
for(const auto& value : values)
{
//std::cos(value);
//arm_cos_f32(value);
//arm_sin_f32(value);
std::sin(value);
}
test_off();
}
} |
To sum up, we learned that:
Important I would switch back to STD, honestly, also to spare the safe-guard required by using CMSIS. |
Changed implemented in #75. @mfussi66, we ought to update the new arch model accordingly. |
Opened again for further investigations. |
Hi guys, Today @marcoaccame and I tried to analyze even further the measurements of sin function between The codeconstexpr uint32_t N_TIMES {1}; // or 100 in the last case
constexpr float32_t step = (2*PI) / N_TIMES; // values from 0 to 2PI
int main() {
test_init();
// Preparing the array of samples
std::array<float_t, N_TIMES> values = {0};
for(int i = 0; i < N_TIMES; i++)
{
values[i] = i*step;
}
__disable_irq();
for(;;)
{
test_on();
for(int i = 0; i < N_TIMES; i++)
{
//std::cos(value);
//arm_cos_f32(value);
std::sin(values[i]);
//arm_sin_f32(values[i]);
}
test_off();
}
} on
|
std::sin | arm_sin_f32 | optimization |
---|---|---|
3.08 | 2.90 | -O0 |
0.54 | 1.62 | balanced |
0.58 | 1.64 | fast |
0.58 | 1.17 | fast but divided by 100 |
on amcbldc
in usec (I*STEP)
std::sin | arm_sin_f32 | optimization |
---|---|---|
1.40 | 1.25 | -O0 |
1.26 | 0.58 | balanced |
1.27? | 0.59 | fast |
1.27? | 0.37 | fast but divided by 100 |
Note
when processing using the std::xxx, I excluded the arm_cortexM4lf_math.lib
from the project in order to avoid unexpected/unwanted sideffects
Doing the Math
amc2c_CPU_Ts = 1/200 = 0.005 us
amcbldc_CPU_Ts = 1/168 = 0.00595238095238095238095238095238 us
Considering the std::sin
VS the arm_sin_f32
using the balanced optimization
$\frac{1.26}{0.54} = \frac{amcbldc CPU_{Ts}}{amc2c CPU_{Ts}}$
$1.26 = \left( \frac{200}{168} \right) \cdot 0.54$
$1.26 = ~0.64$
Considering this result, we believe that the amc2c
is faster than the amcbldc
.
The purpose of this issue is not to compare AMC vs. AMCBLDC but rather STD vs. CMSIS. In light of this, the results above seem to be contradictory. How come CMSIS is far slower than STD on AMC? A couple of more questions:
|
AMC pure math ops can be faster than those of AMCBLDC but as soon as there is some access to other resources we know that AMC is slower than AMCBLDC. To a certain extent, this can explain why CMSIS on AMC is slower than STD as CMSIS probably requires several accesses to memory. |
In this sense, after the alignment at the standup, we decided to measure these timings pulling up and down pins and measuring the time elapsed with the oscilloscope. |
|
Let me try to recap what I got from a F2F alignment with @sgiraz.
|
I think that this kind of analysis went a little bit out of scope, the @sgiraz analysis allowed to understand that we are good at using the std sin/cos. |
While I was looking at the generated code of the FOC in the AMCBLDC, I spotted that the trigonometric functions for
sin
andcos
are implemented by resorting tostd
instead of CMSIS.I spent quite some time investigating this issue and, from the code generation report, it seems that the replacement library for the Cortex-M correctly detects the presence of
sin
andcos
although it doesn't apply the substitution.To check the performance gain, we can simply replace
std::sin
andstd::cos
witharm_sin_f32
andarm_cos_f32
. However, the problem still remains when it comes to code generation.There are easy alternatives that recruit proper approximations of
sin
andcos
but the reason why the link to CMSIS is not performed is unclear.Definitely, worth investigating.
The text was updated successfully, but these errors were encountered: