0

I am playing around, trying to make a little JavaFX application to visualize data received via the serial port from an arduino-based board and some sensors.

After adding some live-updating LineGraphs, I am currently trying to display the roll, pitch and yaw values received from the micro-controller, by rotating a simple box-element.

I have one thread calling a function every x ms, which stores the incoming data into an ObservableList with an changeListener and calls the controller based function to update/rotate the orientation of the box.

Since the calculation of the angles is allready done on the micro-controller, I would like to rotate the box to the received absolute orientation.

From what I've understand so far, I can't simply rotate from any previous orientation to a new absolute one, but only change the orientation relatively to the previous one.

I came up with the following idea to just subtract the last roll/pitch/yaw values from the penultimate one of the observableList.

Data dataTmp = observableList.get(observableList.size()-2);
Data dataTmp2 = observableList.get(observableList.size()-1);

newRoll = dataTmp2.getRoll() - dataTmp.getRoll();
newPitch = dataTmp2.getPitch() - dataTmp.getPitch();
newYaw = dataTmp2.getYaw() - dataTmp.getYaw();

Platform.runLater(new Runnable() {
    @Override
    public void run() {
        controller.setToPosition(newRoll, newPitch, newYaw);
    }
});

//...

This only works out to a certain extent. I still want to rotate to the absolute position received from the micro-controller.

So my question is this: Is there a way to reset the orientation of the box to e.g. 0, 0, 0 from where I could rotate to my new absolute orientation? Simply removing the box and adding a new one did not work out at all.

group.getChildren().remove(box);
box = new Box(300,50,300);
group.getChildren().add(box);

Thank you in advance for any ideas or even solutions. If you need more information or code snippets let me know.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
Canuma
  • 1
  • 1
  • 1
    The book [JavaFX 9 By Example](http://www.apress.com/9781484219607) has an example exactly on this. You can get the source code [here](https://github.com/Apress/javafx-9-by-example/raw/master/JFXBE_JPereda.zip). Anyway, you haven't posted how you set the box rotation (`controller::setToPosition`), so it is hard to help you on that. – José Pereda Jan 28 '19 at 20:41
  • 1
    Among the example seen [here](https://stackoverflow.com/q/37075261/230513), several display three orthogonal axes that transform as a `Group`. – trashgod Jan 28 '19 at 21:36

1 Answers1

1

Referring to this example, an onMouseMoved handler rotates the red Box around the x and y axes as the mouse moves. The following onKeyPressed handler restores the red Box to its original position when the Z key is pressed. Each handler uses the setAngle() method of the Rotate class.

scene.setOnKeyPressed(e -> {
    if (e.getCode() == KeyCode.Z) {
        content.rx.setAngle(0);
        content.ry.setAngle(0);
        content.rz.setAngle(0);
    }
});

Similarly, your setToPosition() implementation can invoke setAngle() to establish the new roll, pitch and yaw values.

Before:

before

After:

after

More subtly, verify that you synchronize access to any data shared between your data acquisition thread and the JavaFX application thread. This example illustrates a Task<Canvas>, while your application might instead implement a Task<Point3D>, where a Point3D holds the roll, pitch and yaw values.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045