0

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

2 Answers 2

0

The reported distances of a mouse device depend on the speed at which you move the mouse. This is normally desired if a mouse is used for what it is made for but in your case this is a problem. I'd try to switch this feature of in your mouse settings. Maybe this link can help you. https://askubuntu.com/questions/698961/disable-mouse-acceleration-in-ubuntu-15-10

3
  • just tried your suggestion, but nothing has changed. Are you sure this could be the problem? If so I'll investigate more into it Commented Apr 18, 2017 at 9:50
  • This is at least one of your problems. There may be more of course which I do not see yet. I am wondering for example how you want to handle a rotation of the car.
    – mipa
    Commented Apr 18, 2017 at 12:19
  • the rotation is handled by a digital compass, not too hard; after disabling acceleration the situation doesn't change... I'll dig into this solution in the meantime Commented Apr 18, 2017 at 13:20
0

If you're reading this post you may want to know that yes, the problem was with acceleration, and even when "disabled" something still doesn't work out, I solved this problem by connecting a PS2 mouse to an Arduino, and with JSSC I ask it the delta X and Y since last check (take a look here http://www.instructables.com/id/Optical-Mouse-Odometer-for-Arduino-Robot/)

Not the answer you're looking for? Browse other questions tagged or ask your own question.