I have an application that is talking to a hardware device over serial. This device is sending a json object every 30ms. This json object is the devices “state” its a motion controller.
Basically the messages look like this:
{"sr":{"line":2524,"posx":1.000,"posy":21.000,"posz":20.000,"posa":11.459,"feed":0.000,"vel":0.000,"unit":1,"coor":1,"dist":0,"frmo":0,"momo":0,"stat":2}}
I get these 1x every 30ms. I have to parse them. Then “draw” them onto the JavaFX gui.
Here is how I am parsing:
Platform.runLater(new Runnable() {
public void run() {
//We are now back in the EventThread and can update the GUI
try {
JsonRootNode json = JDOM.parse(l);
xAxisVal.setText(json.getNode("sr").getNode("posx").getText());
yAxisVal.setText(json.getNode("sr").getNode("posy").getText());
zAxisVal.setText(json.getNode("sr").getNode("posz").getText());
aAxisVal.setText(json.getNode("sr").getNode("posa").getText());
drawLine();
} catch (argo.saj.InvalidSyntaxException ex) {
//Json line invalid.
}
}
});
And here is the draw code I am using:
public void drawLine() {
xl.setX(Float.parseFloat(xAxisVal.getText()) + 400);
y1.setY(Float.parseFloat(yAxisVal.getText()) + 400);
LineTo tmpL = new LineTo((Float.parseFloat(xAxisVal.getText()) * 2) + 400, (Float.parseFloat(yAxisVal.getText()) * 2) + 400);
path.getElements().add(tmpL);
}
So basically I am creating a runnable object every 30ms then parsing and drawing. Is this the best way to do this? You can see a video of it in action:
http://www.youtube.com/watch?v=dhBB3QcmHOg&feature=youtu.be
But it seems like its “jerky” and pretty resource hungry. I am hoping for someone to give me suggestions on how to optimize this code? Perhaps point out something I am missing?
As an FYI the motion controller board we are making is called TinyG. Its opensource hardware.
More info is here:
http://www.synthetos.com/wiki/index.php?title=Projects:TinyG
Firmware here:
https://github.com/synthetos/TinyG
Thanks!
You seems to perform too much calculations on event queue which blocks graphic rendering while your data is being parsed. You should make calculations in separate thread and call
Platform.runLater()only for relevant ui code: