2

I'm currently programming an autonomous robot in java and here is my problem;

I want to know the orientation of my robot at all time, considering there is only 4 possible orientations:

Up / Down / Left / Right

I define each of these positions with the following numbers:

Up and Down = 0 , Left = 1 , Right = -1

So say my robot is currently oriented "0" (say up), and I turn 90 degrees on the right then the value should change to 1. If I rotate 90 degrees on the right again it will change back to 0.

So we have the following sequence: 0 , -1 , 0 , 1 (we can start at any point).

Basically my robot will encounter different types of obstacles. Everytime he meets one he will either turn right or left. I want him to always try to turn in the "up" direction. if the robot turns while being on a "0" position it doesn't matter because it doesn't affect the Y position (if we consider a X-Y axis). But if he is on a 1 or -1 position then he will have to turn 90° * 1 or 90° * (-1).

I'm struggling with finding how to change properly my CURRENT value depending on how many "quadrants" on the right (or left) the robot is rotating. I thought of exponents of (-1) but I couldn't figure out how.

Can you please help me with that?

Thank you!

P.S. I know I could just check the value with if conditions but there are lots of different classes and I use many times the method to rotate, so it needs to be as short and simple as possible.

4
  • why not give up and down separate values? Commented Nov 29, 2016 at 17:32
  • 1
    Like @Gurwinder I would suggest using "N,S,E,W" instead of range -1 to 1. Commented Nov 29, 2016 at 17:34
  • It is because of how the whole thing works and basically these values will be used as multipliers for other methods. @Gurwinder Commented Nov 29, 2016 at 17:34
  • 1
    Please provide some example code in the question how you're using it and where exactly you face the issue. Commented Nov 29, 2016 at 17:38

2 Answers 2

2

Your problem seems a good candidate to experiment with sinus or cosinus functions.

If you use your problem as a quadrant, you can use the proprieties of the sinus like:

sin(0) = 0 
sin(90) = 1 
sin(180) = 0 
sin(270) = -1

In that case, the 0 and 180 degrees are 0 (N and S) and E - W are 1 and -1 respectively.

Can that help you ?

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks I'm going to try it out!
@FredV if an answer worked for you, remember that you can accept it to say thanks!
0

You could use some enum to do what you need:

enum Orientation {
    NORTH, EAST, SOUTH, WEST;

    // clockwise
    public Orientation turnRight() {
        Orientation[] values = values();
        int ordinal = this.ordinal();
        if (ordinal == values.length - 1) {
            return NORTH;
        }
        return values[ordinal+ 1];
    }

    // counter-clockwise
    public Orientation turnLeft() {
        Orientation[] values = values();
        int ordinal = this.ordinal();
        if (ordinal == 1) {
            return WEST;
        }
        return values[ordinal - 1];
    }
}

(Live demo).


If you need the numeric values, just add a field (and a constructor) to the enum:

enum Orientation {
    NORTH(0), EAST(1), SOUTH(0), WEST(-1);

    private final int foo;

    private Orientation(final int foo) {
        this.foo = foo;
    }

    // getter for "foo"

then Orientation.NORTH.turnLeft().turnLeft().turnLeft().getFoo();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.