gwoptics rolling graph of accelerometer
in
Contributed Library Questions
•
1 year ago
I am trying to create a live graph of accelerometer readings using the gwoptics library. The problem is I am not sure the best way pass the serial readings to the gwoptics eq class. The only way I could get it to work, is to read the serial string inside the eq class definition. However this seems very sloppy, ideally I would like to read the serial string in draw(), split it up and pass each axis variable to a respective eq class, I just can't get it to work. I modified the example code provided with the library. The the "working" code is below. I am pretty new to programming and processing so any help at all is appreciated, thanks.
- import processing.serial.*;
- import org.gwoptics.graphics.graph2D.Graph2D;
- import org.gwoptics.graphics.graph2D.traces.ILine2DEquation;
- import org.gwoptics.graphics.graph2D.traces.RollingLine2DTrace;
- float inByteX;
- float inByteY;
- float inByteZ;
- String xAxis;
- String yAxis;
- String zAxis;
- String accelReadings;
- String splitReadings[];
- Serial myPort;
- RollingLine2DTrace r,r2,r3;
- Graph2D g;
- void setup(){
- size(600,300);
- println(Serial.list());
- myPort = new Serial(this, Serial.list()[0], 57600);
- r = new RollingLine2DTrace(new eq(),25,.025f);
- r.setTraceColour(0, 255, 0);
- g = new Graph2D(this, 400, 200, false);
- g.setYAxisMax(600);
- g.addTrace(r);
- g.position.y = 50;
- g.position.x = 100;
- g.setYAxisTickSpacing(100);
- g.setXAxisMax(5f);
- }
- void draw(){
- print(xAxis);
- print(",");
- print(yAxis);
- print(",");
- println(zAxis);
- background(255);
- g.draw();
- }
- class eq implements ILine2DEquation{
- public double computePoint(double x,int pos) {
- accelReadings = myPort.readStringUntil('\n');
- splitReadings = splitTokens(accelReadings, ",");
- xAxis = splitReadings[0];
- yAxis = splitReadings[1];
- zAxis = splitReadings[2];
- inByteX = float(xAxis);
- return inByteX;
- }
- }
2