Loading...
Processing Forum
Recent Topics
All Forums
Screen name:
belimawr
belimawr's Profile
1
Posts
2
Responses
0
Followers
Activity Trend
Last 30 days
Last 30 days
Date Interval
From Date :
To Date :
Go
Loading Chart...
Posts
Responses
PM
Show:
All
Discussions
Questions
Expanded view
List view
Private Message
Speed of Serial, it's too slow
[5 Replies]
10-Jun-2013 10:35 AM
Forum:
Contributed Library Questions
Hi,
I'm doing a simple sketch to receive some bytes from an Arduino, covert them into floats to print and generate a graph.
Everything is working, but very slow! I'd say that the printing rate is about 2Hz but the Arduino is sending data at about 200Hz!
I'm not loosing any packet (it looks to have a huge buffer), however I need to print it in real time.
Here is my code:
import org.gwoptics.graphics.graph2D.Graph2D;
import org.gwoptics.graphics.graph2D.traces.ILine2DEquation;
import org.gwoptics.graphics.graph2D.traces.RollingLine2DTrace;
import java.util.ArrayList;
import java.util.Stack;
import processing.serial.*;
class GraphStackWrapper implements ILine2DEquation
{
Stack<Float> points;
float last;
public GraphStackWrapper()
{
points = new Stack<Float>();
last = 0.0f;
}
public double computePoint(double x,int pos)
{
if(!points.empty())
{
last = points.pop();
}
return (double) last;
}
public void addValue(float f)
{
points.push(f);
}
}
Serial my_serial;
int count;
byte[] byteBuffer;
GraphStackWrapper tmp0, tmp1;
GraphStackWrapper accel0x, accel0y, accel0z;
GraphStackWrapper accel1x, accel1y, accel1z;
RollingLine2DTrace Ltemp0, Lacc0X;
Graph2D g;
void setup()
{
size(800,300);
/* Initialize all graph wrappers */
tmp0 = new GraphStackWrapper();
tmp1 = new GraphStackWrapper();
accel0x = new GraphStackWrapper();
accel0y = new GraphStackWrapper();
accel0z = new GraphStackWrapper();
accel1x = new GraphStackWrapper();
accel1y = new GraphStackWrapper();
accel1z = new GraphStackWrapper();
Ltemp0 = new RollingLine2DTrace(tmp0 , 200, 0.1f);
Ltemp0.setTraceColour(0, 0, 0);
Lacc0X = new RollingLine2DTrace(accel0x , 200, 0.1f);
Lacc0X.setTraceColour(255, 0, 0);
g = new Graph2D(this, 400, 200, false);
g.setYAxisMax(50);
g.addTrace(Ltemp0);
g.addTrace(Lacc0X);
g.position.y = 50;
g.position.x = 100;
g.setYAxisTickSpacing(20);
g.setXAxisMax(5f);
g.setYAxisMin(-100.0f);
g.setXAxisLabel("Time");
g.setYAxisLabel("Sensors");
String portName = Serial.list()[0];
println(portName);
byteBuffer = new byte[38];
my_serial = new Serial(this, portName, 9600);
count = 1;
/* wait for a beginning */
while(my_serial.read() != 0x26)
print('.');
println("Setup Done!");
print("[26]");
}
void draw()
{
background(200);
/* Grab data */
if(my_serial.available() > 0)
{
byte inByte = (byte) my_serial.read();
if(count > 0)
byteBuffer[count - 1] = inByte;
// print('[');
// print(hex(inByte, 2));
// print(']');
count ++;
/* If it's finished, process it */
if(count == 0x27)
{
print('\n');
print('[');
print(hex(byteBuffer[1], 2));
print(']');
print('[');
print(hex(byteBuffer[0], 2));
print(']');
print('\t');
for(int i = 0; i < 9; i++)
{
float f = get4bytesFloat(byteBuffer, 2+i*4);
print(nf(f, 2, 3));
print('\t');
if(i==0)
{
tmp0.addValue(f);
}
if(i==2)
{
accel0x.addValue(f*30.0f);
}
}
count = 0;
println('\n');
}
}
/* print a graph */
g.draw();
}
float get4bytesFloat(byte[] data, int offset)
{
String hexint=hex(data[offset+3])+hex(data[offset+2])+hex(data[offset+1])+hex(data[offset]);
return Float.intBitsToFloat(unhex(hexint));
}
Does anyone have any idea about how I can speed up this code?
«Prev
Next »
Moderate user : belimawr
Forum