Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
10 years, 4 months ago.
Sampling large amounts of data from analogIn and sending it through serial on Nuceleo boards?
I am trying to read a CCD and send the data to a computer.. The CCD has almost 4000 elements that need to be read out and sent to the computer, and timing is an issue... At 921600 baud, the data can't get out fast enough if I sample it and try and send it one by one.. Here is my first attempt (which doesn't quite work)...
CCD sampling
case readOut_signalElements: pixelCount++; // pixelValue[pixelCount] = imageIn.read(); raspi.printf("%i\t%4.12f\r\n", pixelCount,(imageIn.read_u16() * 5.0) / 4096.0); if (pixelCount == signalElements) { pixelCount = 0; state = readOut_trailingDummy; } break;
The source code for the rest of the program is here.... http://mbed.org/users/flatcat/code/ramanSpectrometer_imagingBoard/
I would be very thankful for any suggestions! I tried storing the data in an array, but it's obviously too much... and I'm not quite experienced enough to get this working... The data will be displayed using python and matplotlib on a PC....and that part I actually have working :)
Thanks!!
3 Answers
10 years, 4 months ago.
How much faster does it need to be? It should be able to go a few times faster simply by sending the data more efficient. Fastest option would be sending it as binary data, which would require two bytes per read. Although that has downsides compared to ASCII. But also sending it just as 16-bit integer in ASCII without pixelCount (since that simply increases with one all the time, so is alot of overhead).
But you should have plenty of RAM to just put it in an array.
10 years, 4 months ago.
Two things you can do which should help:
1) Sending data to the serial port will block the program flow until the data is sent. If you use a buffered serial port, something like MODSERIAL rather than the standard serial interface then your main program can carry on running while the data is being sent.
2) printf is a very slow function to call so avoid it when speed is critical. And related to that you are sending two values, both of which can be stored in 16 bits as a text string that is going to be about 20 bytes long. Sending things as text is nice when practical, it makes things a lot easier to handle. However when speed is critical sending 20 bytes of text to transfer for 4 bytes of information is not a good idea. And from the few lines you posted it looks like one of those values is a count which you could probably manage without. If you change it so that the start of the frame sends a few bytes header that are unlikely to show up in the real data ( e.g. 0x00 0x00 ) followed by a 16 bit length ( e.g. 4096 ) and then for each pixel send the raw 16 bit number in binary. That gets your serial data sent each pixel down to 10% of the current amount. It does add some work on the receiver side to decode the data but that probably has the cpu power to keep up.
Make that sort of change and you may not even need the buffered serial port.
Thanks for the reply Andy... I tried MODSERIAL but it doesn't seem to compile on the Nucleo... I got it working using the array, and then dumping it after the read.. But I might look into your #2 point when I get around to cleaning up the code.. The other end is a python script that puts the data into a formatted file for matplotlib and numpy to plot into a graph.. You can see that here https://github.com/flatCat1597/ramanSpectrometer/blob/master/software/raspberryPi/getSpectra_test-01.py if you're interested.. What I have thrown together so far is by now means fancy.. but it's mostly for a proof of concept...
Thanks again for the info! Much appreciated!!!
posted by 19 Aug 201410 years, 4 months ago.
I would personally recommend HID
because you are sending out a formatted packet of upto 64 bytes,
and it is relatively fast, And you can easily send RAW data, or whatever you want.
Ceri
That's an interesting idea.. I'm wondering how easy that would be to read into python to plot the data using matplotlib.... It does seem like it'd be pretty quick... my other question on that would be since the "PC" is ultimately going to be a raspberryPi... how would that be recognized on the Pi? And since this seems to be a theme I am running into with the Nucleo......will the HID libraries compile on the Nucleo..
posted by 19 Aug 2014