Reputation: 16978
I have an Arduino that reads values from a sensor every sample period T (in this example, T = 10ms approximately). After every read, I want to do a bunch of computations on the sensor values. Before designing an algorithm, I want to get a realistic feel for what my effective computational budget is. Here is the code outline:
void loop() {
//read sensor measurements into variables
//do computations
delay(SAMPLE_PERIOD);
}
What I want to do is insert some sort of test that could realistically assess how many multiplications, adds, and loads I could afford to run in the time period before the next loop starts. My first idea was to insert something like:
for (i = 0; i < N; i++) {
//load operation
//multiply operation
//add operation
//store operation
//or some mix of the above
}
Where I would increase N to see how many operations I could run per sample period. I would use a counter/timer to see if this took longer than T.
What do you think?
For reference, the microcontroller board is the Teensy 3.1: https://www.pjrc.com/teensy/teensy31.html#specs
Upvotes: 1
Views: 197
Reputation: 8449
The slower mcu on that datasheet runs at 48 MHz, which means you will have 480 000 cycles to play with in 10 ms. It is difficult to see how you could use that up.
But the bigger problem with your plan is the delay idea. While the delay loop is running, the processor can't be doing anything productive.
A better approach is to use an interrupt on a timer. Not only will there be reduced jitter, but it will be much easier to do.
Another factor to consider is how the sensor values are to be collected. If the sensors require a trigger followed by a waiting period, then it may be a good idea to trigger the sensors at the end of the ISR. That way, the values will be ready to use when the ISR runs the next time.
Upvotes: 1
Reputation: 3807
Alternatively, with some testbench code:
This will give you a good estimate of how long it will take to do one calculation.
10ms might NOT be long enough, if your calc. is too long!
Upvotes: 2