0
Offline
Newbie
Karma: 0
Posts: 35
Arduino rocks
|
 |
« on: February 24, 2013, 03:21:13 am » |
I am currently working on reading ADC's, performing calculations on these values, and outputting the result from that calculation through a DAC.
However, if I design a continuous time transferfunction/calculation and want to implement that in the arduino code, I need a fixed sample/run-time. If my loop-time is faster or slower than the sampletime specified in the translation from continuous time to discrete time, I have a mismatch between my designed transferfunction and the actual digital implementation of it.
Is there an option of forcing a wait until a timer reaches the set sample time (of say 100us) before the loop is run again?
|
|
|
|
|
Logged
|
|
|
|
|
East Anglia (UK)
Online
Edison Member
Karma: 48
Posts: 1413
May all of your blinks be without delay
|
 |
« Reply #1 on: February 24, 2013, 03:33:09 am » |
Yes Have a look at the BlinkWithoutDelay example in the IDE
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 35
Arduino rocks
|
 |
« Reply #2 on: February 24, 2013, 04:06:09 am » |
Ahh ok, so if the loop is completed, I check if I may rerun it or I need to wait. I presume that this "timer" is run in the background which doesn't influence my computational power/time?! If that's true, my next question would be; What way is best/fastest to implement a discrete-time transferfunction/controller? Do I store the required values from the past or can I use some sort of discrete time variable which remembers the last say 10 values? In a different topic someone mentioned; such a formula can be calculated quite well with an Arduino.
float x1 = ((((((7.716 * z - 37.57) * z + 73.23) * z - 71.39) * z + 34.81) * z - 6.785) * z - 0.001332) * z - 1.96e-008
This trick brings it down to 14 "simple" float operations instead of using power
Main problem might be that the Arduino only supports 32bit IEEE754 floats with ~7 significant digits.
- I really feel like a noob, but since speed/performance is key in my current project, I need to start of well instead of fiddling around in crappy code I would initially write.
|
|
|
|
« Last Edit: February 24, 2013, 04:18:06 am by SuperR »
|
Logged
|
|
|
|
|
East Anglia (UK)
Online
Edison Member
Karma: 48
Posts: 1413
May all of your blinks be without delay
|
 |
« Reply #3 on: February 24, 2013, 04:40:19 am » |
It does not run in the background but it does not block other code such as reading sensors or buttons, unlike the delay() function. So, in your case start the timer, read values from the ADC, which can be quite slow, then, when the timer reaches its limit process the input. You mention a period of 100uS in your original post.
You will need to consider the use of micros() in your timer rather than millis() or even using one of the Arduino hardware timers. There are other members here better placed than me to give you advice in that area.
The check to see whether the 'delay' time has passed is carried out each time loop() or another user defined control structure, such as while(), is executed.
I cannot help you explicitly with your other questions but it seems to me that an array would be a good way to hold a series of values for later access within the program.
I think that you do need to write some code to get you started, however "crappy" it is to start with. Speed of operation, structure of code etc can often be improved but if there is no initial code then there is nothing to improve.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 35
Arduino rocks
|
 |
« Reply #4 on: February 24, 2013, 04:51:22 am » |
Thank you for your reply. I was aware of the micros() command after reading the example. Concerning the code, this couple days are to get the info I need to start building the code. Some of the hardware is not available yet so this research is to get a head-start when I finish the hardware. But, you are right, as soon as I get the HW done, I shall write my, to be expected crappy, code. Maybe the speed of the due is such high that I don't even need speed-enhancement. Updates WILL come  , once..
|
|
|
|
|
Logged
|
|
|
|
|
UK
Offline
Edison Member
Karma: 44
Posts: 2224
What a host of balls she had seen: gaity, the brass buttons...
|
 |
« Reply #5 on: February 24, 2013, 05:56:52 am » |
A word of warning:
On the Aduino (and pretty much all microcontrollers) floating point operations are slow.
If you can use fixed point maths, in say Q15 format or similar, your calculations will be considerably faster and more efficient.
At the risk of being accused of proselytizing and willy-waving, I would also add that the Arduino is probably not the best platform for this kind of application, and something like a dsPIC based system would be more suited to your needs - although that would be harder to work with.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 35
Arduino rocks
|
 |
« Reply #6 on: February 24, 2013, 06:31:21 am » |
If the Arduino environment won't work properly, I will try and get one of the dSpace pc's up and running.
I was hoping to built the entire thing from ground up with a DIY ADC and DAC. That would lead to the situation in which I would be 100% in control of everything and have the knowhow of every single component.
I will spend some time with the ADC IC's. If I can't get it up and running to my likings, I will bin the project and fiddle around with the dSpace setup.
|
|
|
|
|
Logged
|
|
|
|
|
UK
Offline
Edison Member
Karma: 44
Posts: 2224
What a host of balls she had seen: gaity, the brass buttons...
|
 |
« Reply #7 on: February 24, 2013, 06:45:38 am » |
If the Arduino environment won't work properly, I will try and get one of the dSpace pc's up and running.
I was hoping to built the entire thing from ground up with a DIY ADC and DAC. That would lead to the situation in which I would be 100% in control of everything and have the knowhow of every single component.
I will spend some time with the ADC IC's. If I can't get it up and running to my likings, I will bin the project and fiddle around with the dSpace setup.
It may work properly, it may not, it all depends on exactly what you are doing - the number of samples, the required sample times, the complexity of the maths, etc.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 35
Arduino rocks
|
 |
« Reply #8 on: February 24, 2013, 06:49:58 am » |
Only time will tell  As promised, I'll try and keep the topic updated. Tonight I will try and design a pcb for the ADC. Since I don't have the logic level converters available yet, I will use a Duemillanove or Uno for initial testing.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 138
Posts: 19067
I don't think you connected the grounds, Dave.
|
 |
« Reply #9 on: February 24, 2013, 07:19:12 am » |
Is there an option of forcing a wait until a timer reaches the set sample time (of say 100us) before the loop is run again? note also that 100us is pretty much the time taken by analogRead which, like "delay()", is blocking.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Netherlands
Offline
Tesla Member
Karma: 90
Posts: 9401
In theory there is no difference between theory and practice, however in practice there are many...
|
 |
« Reply #10 on: February 24, 2013, 09:15:03 am » |
You can do ADC in asynchronous mode, start conversion and an interrupt reads value when done. discussed here - http://arduino.cc/forum/index.php/topic,125766.msg946608.html#msg946608 -
|
|
|
|
|
Logged
|
|
|
|
|
Rome, Italy
Offline
Sr. Member
Karma: 20
Posts: 442
|
 |
« Reply #11 on: February 24, 2013, 11:00:23 am » |
Is there an option of forcing a wait until a timer reaches the set sample time (of say 100us) before the loop is run again? note also that 100us is pretty much the time taken by analogRead which, like "delay()", is blocking. Can you confirm that the timing for an available() call is much faster? In a simple loop, with timing based on micros(), it gives me 4us, which I understand is the minimum interval micros() can detect. Would it then be possible to call available() until the value returned is equal to the sample size, e.g. while (serial.available() < sample_size) { // do something else } // now get the sample data
|
|
|
|
|
Logged
|
|
|
|
|
UK
Offline
Edison Member
Karma: 44
Posts: 2224
What a host of balls she had seen: gaity, the brass buttons...
|
 |
« Reply #12 on: February 24, 2013, 11:50:06 am » |
Serial.available() just returns the number of bytes in the receive buffer: return (unsigned int)(SERIAL_BUFFER_SIZE + _rx_buffer->head - _rx_buffer->tail) % SERIAL_BUFFER_SIZE;
|
|
|
|
|
Logged
|
|
|
|
|
Rome, Italy
Offline
Sr. Member
Karma: 20
Posts: 442
|
 |
« Reply #13 on: February 24, 2013, 12:22:38 pm » |
Serial.available() just returns the number of bytes in the receive buffer:
Exactly. The idea was, instead of waiting for a sample time, to check the availability of sample data of known size.
|
|
|
|
|
Logged
|
|
|
|
|
|