Read data at 2KHz sample rate, how to?

General programming chat and advice for beginners

4 posts
by Killertechno » Thu Apr 25, 2013 2:47 pm
Hi, I don't know if it's the correct place to post, if not I apologize....

I need to intarface to an SPI device (vibration sensor) that uses SPI interface, it sends out data at 2000 Hz, every time I have to read 6 Bytes.
I need to collect data ro make a real-time monitor, data would be displayed on a graph where I can zoom... but this is a second-time focus.

Now, hoe can I get data?
2 KHz sample rate is very low rate for a microcontroller (I always used microcontrollers), but for Raspberry I'm a bit confused....
I can read through SPI channel using C (or Phyton) program, but could I really reach a reading every 500 us?
Is there some SPI library more suitable for this than other library?
Is it possible to make a software interrupt to be called every 500 us?
Or would be better make an external (hardware, maybe through serial interface) buffer and collect data every second?
Thanks.
Posts: 37
Joined: Wed Jan 02, 2013 8:28 am
by joan » Thu Apr 25, 2013 3:25 pm
6 bytes at 2000Hz is well within the Pi's capabilities. It shouldn't present any problems for the SPI interface.
User avatar
Posts: 5984
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK
by Killertechno » Thu Apr 25, 2013 3:43 pm
I'm testing SPI libraries (I suppose GPIO libraries in C language, I don't remember), I can reach 3 KHz of sample rate.
In high level language (Visual Basic) I can make a software timer interrupt that generates a call, could I make something similar in C/Phyton?

I have to synchronize data collecting, I can't use delay functions inside while statement (data processing takes variable time), so I was thinking to use time libraries to check sysytem time.
Now, Raspberry timing would be enough stable or launching other scripts may stop/delay my data acquisition?
Posts: 37
Joined: Wed Jan 02, 2013 8:28 am
by gordon@drogon.net » Thu Apr 25, 2013 4:23 pm
Killertechno wrote:Hi, I don't know if it's the correct place to post, if not I apologize....

I need to intarface to an SPI device (vibration sensor) that uses SPI interface, it sends out data at 2000 Hz, every time I have to read 6 Bytes.
I need to collect data ro make a real-time monitor, data would be displayed on a graph where I can zoom... but this is a second-time focus.

Now, hoe can I get data?
2 KHz sample rate is very low rate for a microcontroller (I always used microcontrollers), but for Raspberry I'm a bit confused....
I can read through SPI channel using C (or Phyton) program, but could I really reach a reading every 500 us?
Is there some SPI library more suitable for this than other library?
Is it possible to make a software interrupt to be called every 500 us?
Or would be better make an external (hardware, maybe through serial interface) buffer and collect data every second?
Thanks.


Using the current kernel driver, you can perform just over 8000 SPI transactions/sec. So it's going to be fast enough for your sampling.

The trick is going to be the sample accuracy, but in the 500uS range it's not going to be that bad, but there will be a little jitter. You run your program as root, engage a real-time priority and off you go - start the clock, read a sample, then wait for the next clock time.

That will (should) work very well. You could feed an external interrupt into the Pi and have it routed through to your program - maybe the sensor your using can trigger an interrupt, but then you might still need to wait until the next sample time....

wiringPi has some simplified SPI access handling, but there's no real magic to it. I'd suggest you have a look at some of th eexisating wiringPi code - e.g. the Gertboard code to see how it operates.

-Gordon
--
Gordons projects: https://projects.drogon.net/
User avatar
Posts: 1537
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK