I came across a strange behavior in my program (arduino Due).
I have included external .c function in the sketch and it works fine (the output values are correct).
However when I try to measure the loop time, i realize that the global variables, i.e. t_now, t_prev and dt are not updating and so the printed loop time is garbage. but if I comment the eternal function, the dt is printed correctly as 3ms.
i checked the sRAM (using freeram()) and it seems 92kB free.
So the problem is that the global variables are not updating when I call the function. Can it be because of the RAM usag of the function during its execution(using several temporary variables in the function)
unsigned long t_now=0.0;
unsigned long dt=0.0;
unsigned long t_prev=0.0;
void setup() {
Serial.begin(115200);
Serial.println("begin...");
}
void loop() {
my_externl_function(1,0,-.5, &out_1, &out_2);
t_now =millis();
dt = t_now-t_prev;
Serial.println(dt);
t_prev=t_now;
}
There doesn't appear to be anything wrong with the code you've posted. Something may be happening in your_external_function or elsewhere that we cannot see, so who knows.
steven2010:
However when I try to measure the loop time, i realize that the global variables, i.e. t_now, t_prev and dt are not updating and so the printed loop time is garbage.
What makes you think they are "not updating"? What values of "garbage" are being displayed for 'dt'? Have you tried printing out the other two values to see if the garbage 'dt' is caused by a garbage t_now or a garbage t_prev?
The problem is certainly in the code you didn't show.
Its the complete code except for many header files included before the setup.
The external function contains some matrix operation and complicted but the question here is why should that function affect the global vriables in the loop?!!
I suspect that mabe the function (which is in C) defines and uses some temporary matrix variables in it (and then destroies them) and that affects the RAM
steven2010:
Its the complete code except for many header files included before the setup.
The external function contains some matrix operation and complicted but the question here is why should that function affect the global vriables in the loop?!!
I suspect that mabe the function (which is in C) defines and uses some temporary matrix variables in it (and then destroies them) and that affects the RAM
Then there is one of two possibilities.
You have identified the problem and can go fix it, as we can't help fix what we can't see.
The global variables are updating, just so fast you can't see it. How long do you think it takes to do these commands
steven2010:
The external function contains some matrix operation and complicted but the question here is why should that function affect the global vriables in the loop?!!
Array subscript errors are common.
steven2010:
I suspect that mabe the function (which is in C) defines and uses some temporary matrix variables in it (and then destroies them) and that affects the RAM
Not enough RAM and/or unchecked malloc returns can screw up a program.
The problem is not apparent in the partial code posted. We need to see full code and the code for that external c file (not just the function - I need to see the includes) in order to figure out what's going on - the problem is either somewhere else in the sketch (I know it can't be complete because there's no declaration of out_1 and out_2), or in the external function.
Does the external code use timer0? Does it make assumptions about the sizes of datatypes that are valid for some architectures, but not the due, or engage in reckless pointer math? (asking in particular because a lot of code intended for embedded environment assumes an architecture to reduce code size, memory use, or execution time)
Oh - and unsigned long is not a float. unsigned long x = 0; more accurately reflects what the line does than unsigned long x = 0.0; Assigning a float to an integer datatype like a long truncates it (everything after decimal is dropped - not rounded)
johnwasser:
What makes you think they are "not updating"? What values of "garbage" are being displayed for 'dt'? Have you tried printing out the other two values to see if the garbage 'dt' is caused by a garbage t_now or a garbage t_prev?
The problem is certainly in the code you didn't show.
i print dt and its incementing. That means t_prev is reset to 0 everytime. and I printed t_prev and it was 0.
The function has been converted to C from a matla bscript and it is too complicated to post here
steven2010:
The function has been converted to C from a matlab script and it is too complicated to post here
If the sketch is too long to be posted inline (9000 characters?) you can upload .ino, .c, .cpp, and .h files or compress them into a .zip file and upload that. Look at the "Attachments and other options" menu just below the edit box. If you are using a Quick Reply, click on the "Preview" button to get the "Attachments and other options" menu. You can upload up to 2 MB.
Something is wrong with the translation from MatLab and you have apparently not found it yet. If you want anyone to help you find the mistake you will have to show them the code.