Hello all,
I have difficulties using the mathematic "modulo" function with embedded coder and F28335.
I use following software:
- Matlab R2011b (Embedded Coder version 6.1)
- CodeComposerStudio V3.3.83.20 (not possible to use another CCS version...)
In my Simulink model, I have to create a sawtooth signal (between 0 and 2*pi: it is a rotor position signal; sample time is 100µs) at a given frequency; to do this, I do an integration (iterative sum) of "2*pi*frequency*100µs", then I use the Simulink "mod" bloc function to do a 2pi-modulo. Then I apply sinus and cosinus (using Simulink "SIN" and "COS" bloc functions) to the sawtooth, and these values are used for a 3-phases motor current regulation (Park transformation). This works well in simulation; but when I generate the associated code with embedded coder, then flash it into the DSP RAM, I got the following issues:
- at low frequency the behavior is OK; at high frequency (about 400Hz) it seems there is problem with modulo function, or an interaction between modulo and sinus/cosinus: the result is that the sawtooth is not generated correctly (looking at the sawtooth it seems OK, but the current regulation becomes instable)
- I remarked that to partially solve this problem, I can use two times the 2pi-modulo function - in Simulink I do something like mod( mod(sawtooth,2pi) , 2pi ) - doing this, I don't have regulation instability anymore, but precision on the sawtooth frequency is bad (I ask for 400Hz and I get 420Hz for example) ;
I took a look at the generated code, the modulo is coded as follows:
real_T rt_modd_snf(real_T u0, real_T u1)
{
real_T y;
real_T tmp;
if (u1 == 0.0) {
y = u0;
} else if (!((!rtIsNaN(u0)) && (!rtIsInf(u0)) && ((!rtIsNaN(u1)) && (!rtIsInf
(u1))))) {
y = (rtNaN);
} else {
tmp = u0 / u1;
if (u1 <= floor(u1)) {
y = u0 - floor(tmp) * u1;
} else if (fabs(tmp - rt_roundd_snf(tmp)) <= DBL_EPSILON * fabs(tmp)) {
y = 0.0;
} else {
y = (tmp - floor(tmp)) * u1;
}
}
return y;
}
So my questions are:
- Why is the behavior different when I use two times the modulo function, when from a "mathematic point of view" applying two times a 2pi-modulo shall be exactly the same than applying one time a 2pi-modulo?
- What can I do to solve this problem? Is the math.h library involved? Is it because I use modulo and number in double format?
- I use the default CCS v3.3 configuration, shall I modify any configuration file or code generation option?
If you need any other information for this topic, don't hesitate.
Thanks for reading me
Regards,
Julien