Hi
Intention:
Realtime plot an ECG Signal from Arduino’s ADC (sent from Arduino–>RaspberryPi–>LAN–>ClientPC–>Processing).
Problem:
The plotting in processing somehow lags/delays behind realtime.
In depth:
I use a EGC signal generator to feed an Arduino ADC. The data is sent
to the serial output every 2 milliseconds, thats the arduino code for that:
void setup() {
Serial.begin(38400);
}
void loop() {
Serial.println(analogRead(0));
delay(2);
}
That should give about ~500 sps/s. I am not sure if my serial speed of 38400baud is enough for that but on the RaspberryPi I am receiving
7046 values per second, that is roughly 2 milliseconds and I think this is not the cause of the lag.
Then, on the raspberryPi I relay the pakets from /dev/ttyUSB0 to the ClientPc over LAN
raspberryPi;
socat /dev/ttyUSB0,raw,echo=0 tcp:ClientPcIP:8888
Arriving at the ClientPc I still get about 7040 lines of data per second which is again ~2ms.
So I rule out any bottlenecks over the LAN…
But then when I plot the data with this code:
import processing.net.*;
int SERVERPORT = 8888;
Server myServer;
String myString;
float inByte = 0;
float yPos = 1;
int xPos = 1;
float yPos2 = 1;
int xPos2 = 1;
void setup() {
size(800, 300);
myServer = new Server(this, SERVERPORT);
background(255);
}
void draw() {
Client thisClient = myServer.available();
if (thisClient != null) {
if (thisClient.available() > 0) {
myString = thisClient.readStringUntil('\n');
if (myString != null) {
myString = trim(myString);
inByte = float(myString);
yPos = map(inByte, 0, 1024, 0, height);
//point(xPos,yPos);
xPos++;
line(xPos2, yPos2, xPos, yPos);
if(xPos >= width){
xPos = 0;
background(255);
}
xPos2 = xPos;
yPos2 = yPos;
}
}
}
}
the plot runs from left to right very slow.
I recorded a video to show what I mean. I first show how ArduinoIDE Serial Plotter plots in realtime, then I start sending the data over lan to processing and you can see how slow it runs there:
I then wrote a simple code to count the time that it needs to receive 1000 pakets (data lines) in processing:
import processing.net.*;
int SERVERPORT = 8888;
Server myServer;
String myString;
float inByte = 0;
float yPos = 1;
int xPos = 1;
float yPos2 = 1;
int xPos2 = 1;
int counter = 0;
int startTime = 0;
int endTime = 0;
void setup() {
size(800, 300);
myServer = new Server(this, SERVERPORT);
background(255);
}
void draw() {
if(counter == 0){
startTime = millis();
}
Client thisClient = myServer.available();
if (thisClient != null) {
if (thisClient.available() > 0) {
myString = thisClient.readStringUntil('\n');
println(counter + "-->" + myString);
counter++;
if(counter == 1000){
endTime = millis();
println((endTime-startTime)/1000);
println("EXIT OK");
exit();
}
}
}
}
It takes 16 seconds for 1000 data lines which gives about 16ms per sample. This is 8 times more than expected of 2ms.
Does someone have an idea what I am doing wrong? Is this maybe something buffering related in processing? I never had anything to do with buffering…