Detect 360 degree rotation algorithm

I successfully detect 0-360 degrees of rotation (roll) of the phone around the axis, but now I have difficult times developing an effective algorithm for detecting one full stroke. My job, but I think that is not an elegant and efficient algorithm, as I would like:

private boolean detectRoll; private boolean[] checkpointsR = new boolean[4]; private boolean fullRollTurn; public void detectingRoll() { setDetectRoll(true); checkpointsR[0] = true; for (int i = 1; i < 4; i++) { if (roll > 90 * i && roll < 90 * (i + 1) && checkpointsR[i - 1] == true) { checkpointsR[i] = true; } } if (areAllTrue(checkpointsR) && roll > 0 && roll < 45) { fullRollTurn = true; // reset rollCheckpoints for (int i = 1; i < 4; i++) { checkpointsR[i] = false; } } } public static boolean areAllTrue(boolean[] array) { for (boolean b : array) if (!b) return false; return true; } public void setDetectRoll(boolean detectRoll) { this.detectRoll = detectRoll; } 

Any help would be really appreciated.

+4
source share
1 answer

Well, your code only defines what comes from increasing roll . In the other direction, i.e. Turn as a result of falling roll ,

  if (checkpointsR[i - 1] == true) { checkpointsR[i] = true; } 

never works.

While this is easily fixed, if you have a single roll sensor input, a fixed setpoint will always have problems. The best way to visualize this is that your control points are small red dots on the circle, and turning the phone corresponds to going around ant around the edge of the circle. Suppose that when ant passes a point, it turns green, and this means that the breakpoint is set to true . If ant starts between two control points A and B , it can β€œtrick” them by scanning through B , rounding to A , and then turning again and again turning in the opposite direction. All breakpoints will be green, but ant will not complete the full circle.

The way to solve this problem is to change two things: firstly, give control points three states: unvisited , clockwise and anticlockwise . Second, run ant over the breakpoint .

Here are the new rules:

  • Each dot begins with unvisited (red dot).
  • If ant transmits a control point clockwise, set it to clockwise (a small green arrow pointing clockwise from the point)
  • If ant transmits a control point in a counterclockwise direction, set it to anticlockwise (a small green arrow indicating counterclockwise direction from a point)
  • The rotation is completed if all the control points are clockwise or all of them are anticlockwise .

This can be achieved using three control points and placed where you want, in a circle, while the first is under the original ant position. I would recommend {initial, inital + 120, initial + 240} though for the sake of symmetry.

(ant thought experiment works with two control points, but when you have two control points, the problem is that there are regions between them with a non-specific control point, which confuses the detection of which control point ant actually passed when ant goes from one area to another)

0
source

Source: https://habr.com/ru/post/1268757/


All Articles