I'm designing a small system that should be able to locate itself in the real world, it's basically an electric RC car with a PC mounted on it. This car should be able to navigate in the real world and know where it is on a map. Since it needs a good precision, GPS isn't an option (the best I can get is 4 meters, way over what I can accept) and encoding the wheels in any way is too expensive for my budget, so the workaround is to place a mouse under this car and use its feedback as the relative positioning system.
My first idea was to calculate the difference in the pixel distance between two instants (using a timer), I even tried to apply the same principle using the mouseMoved event, but the problem is still there: if I vary the speed of the mouse, the calculated distance varies too.
At this point I have no other ideas, what do you think is wrong with my approach?
public class Main extends Application {
public static void main(String args[]) {
launch(args);
}
private double cmToPixel = 1;
private int totalX;
private int totalY;
private Robot robot;
private int counter;
@Override
public void start(Stage primaryStage) throws Exception {
VBox pane = new VBox();
pane.setFillWidth(false);
pane.setMinWidth(200);
javafx.scene.control.TextArea textArea = new TextArea();
javafx.scene.control.TextArea logArea = new TextArea();
javafx.scene.control.TextArea debugArea = new TextArea();
pane.getChildren().addAll(textArea, logArea, debugArea);
Scene scene = new Scene(pane);
primaryStage.setScene(scene);
primaryStage.centerOnScreen();
robot = new Robot();
robot.mouseMove((int) (Screen.getPrimary().getBounds().getWidth() / 2), (int) (Screen.getPrimary().getBounds().getHeight() / 2));
scene.setOnMouseMoved(e -> {
double deltaX = e.getX() - Screen.getPrimary().getBounds().getWidth() / 2;
double deltaY = Screen.getPrimary().getBounds().getHeight() / 2 - e.getY() ;
totalX += deltaX;
totalY += deltaY;
textArea.appendText((totalX / cmToPixel) + " - " + (totalY / cmToPixel) + "\n");
debugArea.appendText(deltaX+" - "+deltaY+"\n");
logArea.appendText("Center: ["+(int) (Screen.getPrimary().getBounds().getWidth() / 2)+";"+(int) (Screen.getPrimary().getBounds().getHeight() / 2)+"] cursorPosition: "+e.getX()+" - "+e.getY()+"\n");
robot.mouseMove((int) (Screen.getPrimary().getBounds().getWidth() / 2), (int) (Screen.getPrimary().getBounds().getHeight() / 2));
});
primaryStage.show();
primaryStage.setFullScreen(true);
}}
if you want to reproduce my results, just mark two lines on a piece of paper and try to run the mouse between those lines at different speeds while keeping an eye on the program