Hello.
I'm working on a project where I want to sample some sensors periodically and one of them is a KY-037 (which is basically an electret microphone).
For now I'd want to continuously sample and transmit the data to a PC via USB cable (so, using "Serial" methods).
I'm using a 50kHz sampling frequency to sample from the KY037 and I know that it's a lot if I want to transmit all this data in real time, so I began some tests to see if it's possible.
I'm using TEENSY3.6 by the way.
The current test (which I'll post the code below) uses 2 buffers to storage the audio samples, transmitting one while the other one is being filled and so on. I'd like to check if I could transmit faster than the time it takes for the other buffer to be full (meaning I could keep doing this and wouldn't lose any samples). I'm also reading data from a BME280 sensor and transmitting along with every audio buffer transmission.
I did some measurements with a Tektronix DPO2012B Scope and it mostly works fine, but every few seconds something weird occurs. Normally each transmission takes a short amount of time, less than it takes to fill a buffer (say, for instance, about 849us each transmission) but then, every few seconds (around 7 to 12s) there's one transmission that takes over 80ms! Sometimes over 100ms! And this is where things don't work as intended, because in this time the next buffer fills up and I begin losing samples until the transmission is finished.
I'll attach the code below. I tried to add as much comments and perhaps a better explanation of the problem in the code itself. I apologize if some comments seems obvious, I just wanted to make it as simple to understand as I could.
I'll also attach some Scope shots I took.
Print 1 shows the normal behaviour: Each yellow (CH1) pulse is a sample (analogRead()) and each blue (CH2) pulse is buffer1 being transmitted. This print shows that the sampling continues as the buffer is being transmitted (because the transmission is faster then the time it takes to fill the second buffer).
Print 2 shows the abnormal behaviour. The blue pulse takes way longer (844.9us normally and then 86.8ms when this happens). The sampling continues until the next buffer is full and then stops until the end of this transmission.
These prints where taken with the following test parameters: buffer size = 128 samples, Fs = 50 kHz. But the same kind of problem occurs with bigger buffer size and/or lower sampling frequency.
By reading https://www.pjrc.com/teensy/td_serial.html#txbuffer I've suspected this has something to do with the way data is transmitted using Serial methods (buffered first, then transmitted later), but these tests were made adding Serial.send_now() or Serial.flush() and the behaviour is the same.
Can someone please help me understand what's happening and how I can avoid this?
Any help is appreciated. Thanks in Advance.
I'm working on a project where I want to sample some sensors periodically and one of them is a KY-037 (which is basically an electret microphone).
For now I'd want to continuously sample and transmit the data to a PC via USB cable (so, using "Serial" methods).
I'm using a 50kHz sampling frequency to sample from the KY037 and I know that it's a lot if I want to transmit all this data in real time, so I began some tests to see if it's possible.
I'm using TEENSY3.6 by the way.
The current test (which I'll post the code below) uses 2 buffers to storage the audio samples, transmitting one while the other one is being filled and so on. I'd like to check if I could transmit faster than the time it takes for the other buffer to be full (meaning I could keep doing this and wouldn't lose any samples). I'm also reading data from a BME280 sensor and transmitting along with every audio buffer transmission.
I did some measurements with a Tektronix DPO2012B Scope and it mostly works fine, but every few seconds something weird occurs. Normally each transmission takes a short amount of time, less than it takes to fill a buffer (say, for instance, about 849us each transmission) but then, every few seconds (around 7 to 12s) there's one transmission that takes over 80ms! Sometimes over 100ms! And this is where things don't work as intended, because in this time the next buffer fills up and I begin losing samples until the transmission is finished.
I'll attach the code below. I tried to add as much comments and perhaps a better explanation of the problem in the code itself. I apologize if some comments seems obvious, I just wanted to make it as simple to understand as I could.
I'll also attach some Scope shots I took.
Print 1 shows the normal behaviour: Each yellow (CH1) pulse is a sample (analogRead()) and each blue (CH2) pulse is buffer1 being transmitted. This print shows that the sampling continues as the buffer is being transmitted (because the transmission is faster then the time it takes to fill the second buffer).
Print 2 shows the abnormal behaviour. The blue pulse takes way longer (844.9us normally and then 86.8ms when this happens). The sampling continues until the next buffer is full and then stops until the end of this transmission.
These prints where taken with the following test parameters: buffer size = 128 samples, Fs = 50 kHz. But the same kind of problem occurs with bigger buffer size and/or lower sampling frequency.
By reading https://www.pjrc.com/teensy/td_serial.html#txbuffer I've suspected this has something to do with the way data is transmitted using Serial methods (buffered first, then transmitted later), but these tests were made adding Serial.send_now() or Serial.flush() and the behaviour is the same.
Can someone please help me understand what's happening and how I can avoid this?
Any help is appreciated. Thanks in Advance.