1

I am working in a project using Raspberry Pi 3 B where I get data from a IR sensor(Sharp GP2Y0A21YK0F) through a ADC MPC3008 and display it in real-time using PyQtgraph library.

However, it seems that I am getting very few samples and the graph is not "smooth" as I expect.

I am using the Adafruit Python MCP3008 Library and the function mcp.read_adc(0) to get the data.

Is there a way to measure the sample rate in Python?

Thank you

Hugo Oliveira

Hugo Oliveira
  • 197
  • 2
  • 3
  • 8
  • `it seems that I am getting very few samples` what do you mean by you are by this ? , you get sample when you read the ADC channel. how often are you reading your ADC ? can you show some code ? – Sufiyan Ghori Nov 10 '16 at 07:02
  • try collecting e.g. a 100 (or a 1000?) samples and use timeit to measure how long that takes. – DisappointedByUnaccountableMod Nov 10 '16 at 11:30
  • I think you asked another question about how to get 200ksps from MCP3008 with Pi. Don't suppose you're getting anywhere near that. Pi/Linux isn't a realtime platform – DisappointedByUnaccountableMod Nov 10 '16 at 11:32
  • @barny Yes, now I know it can't be done. However, I trying to understand the characteristics of the data I am taking in order to choose a dedicated board compatible with the Raspberry. – Hugo Oliveira Nov 10 '16 at 15:37
  • @SufiyanGhori Thank you for you comment. I am counting how many points my code is taking in a second. I am not a computer scientist, that is the reason I am having a really hard time. :( Here is my code: http://stackoverflow.com/questions/40499890/how-to-obtain-the-highest-sample-rate-possible-in-raspbery-pi-using-a-adc – Hugo Oliveira Nov 10 '16 at 15:40

1 Answers1

0

I would suggest setting up some next level buffering, ideally via multiprocessing (see multiprocessing and GUI updating - Qprocess or multiprocessing?) to better get a handle on how fast you can access the data. Currently you're using a QTimer to poll with, which is only getting 3 raw reads every 50 msec... so you're REALLY limiting yourself artificially via the timer. I haven't used the MCP3008, but a quick look at some at their code seems like you'll have to set up some sample testing to try some things out, or investigate further for better documentation. The question is the behavior of the mcp.read_adc(0) method and is it blocking or non-blocking... if non-blocking, does it return stale data if there's no new data, ... etc. It would be ideal if it was blocking from a timing sense, you could just set up a loop on it and time delta each successive return to determine how fast you're able to get new samples. If it's non-blocking, you would want it to return null for no new samples, and only return the actual samples that were new if it does return something. You'll have to play around with it and see how it behaves. At any rate, once you get the secondary thread set up to just poll the mcp.read_adc(0), then you can use the update() timer to collect the latest buffer and plot it. I also don't know the implications of multi-threading / multiprocessing on the RaspPI (see general discussion here: Multiprocessing vs Threading Python) , but anything should be better than the QTimer polling.

Community
  • 1
  • 1
segFaultCoder
  • 466
  • 4
  • 8