Compass - track number with full 360 degree rotation

Suppose a person uses this compass, and starting at 90 degrees they begin to rotate either clockwise or counterclockwise. What is the best way to count the full 360 degree revolutions they complete? Assuming they will only rotate clockwise or only counterclockwise from start to finish.

I continued to come up with solutions where, if the initial bearing, for example, is 90 degrees, I continue to check the next bearing when the sensor data changes, and if it constantly moves in one direction, I know that they rotate. And if they continue to rotate in this direction and return it 90 degrees, this is considered one rotation. My path seems very confusing and inefficient, and it's hard for me to find a better way.

In this case, I expect a few full turns.

I would appreciate any help. Thanks!

I found this answer and I am trying to compile sample code for this. If someone has already done something similar, send it!

@Override public void onSensorChanged(SensorEvent event) { switch(event.sensor.getType()) { case Sensor.TYPE_GRAVITY: { mValuesAccelerometer = lowPass(event.values.clone(), mValuesAccelerometer); break; } case Sensor.TYPE_MAGNETIC_FIELD: { mValuesMagneticField = lowPass(event.values.clone(), mValuesMagneticField); break; } } boolean success = SensorManager.getRotationMatrix( mMatrixR, mMatrixI, mValuesAccelerometer, mValuesMagneticField); if (success) { SensorManager.getOrientation(mMatrixR, mMatrixValues); float azimuth = toDegrees(mMatrixValues[0]); float pitch = toDegrees(mMatrixValues[1]); float roll = toDegrees(mMatrixValues[2]); if (azimuth < 0.0d) { //The bearing in degrees azimuth += 360.0d; } } } 
+5
source share
4 answers

If you are sure that they will only move in 1 direction in order to optimize your code, you can have breakpoints for degrees instead of constant monitoring if they are still moving in the right direction.

Here's a rough algorithm for doing this

 //You noted 90 degree as starting point // checkpoint 1 will be 180 keep it as a boolean // now you've reached 180 if the meter gets to 180 before going to next checkpoint // which is 270 then make 180 false. it means they turned back. // if they make it to 270 then wait for 0 degrees and do the same. // if they make it back to 90 like that. You got a rotation and hopefully // a bit of complexity is reduced as you're just checking for 4 checkpoints 

I currently have no code.

+1
source

Create 3 Integers

 int rotationCount=0 int currentDegrees=0 int previousDegrees=89 

not a java programmer, so I don't know how you handle the onSensorChanged event, but basically do the checking inside the while loop

 while (currentDegrees + 90 < 360) { if (currentDegrees + 90 == 0) { if (previousDegrees == 359) { rotationCount = rotationCount + 1 } } else if (currentDegrees + 90 == 359) { if (previousDegrees == 0) { rotationCount = rotationCount - 1 } } previousDegrees = currentDegrees + 90 } 

Sorry for the syntax, this is just an example of how to do this.

0
source

This is a read tracking issue that is overflowing. You need to keep track of the last reading and hope that the user does not make more than half a turn between each reading .... (due to the Nyquist theorem)

Here is the basic pseudo code.

 var totalChange = 0; var lastAzimuth = -1000; function CountTurns(az) { if (az > 180) az -= 360; // do this if your azimuth is always positive ie 0-360. if (lastAzimuth == -1000) { lastAzimuth = az; } diff = az - lastAzimuth; if (diff > 180) diff -= 360; if (diff < -180) diff += 360; lastAzimuth = az; totalChange += diff; return totalChange / 360; } 
0
source

Visualize what I say and you will surely hit your target as soon as possible. Since you do not need to think about the full 360 degrees, but you can take half of this and use the differences of the signs to your advantage.

Take a look at this figure: 360 rotation

We have a circle divided into two sides (left and right).

The left side will take a negative 180 degrees. (West side).

The right side will take a positive 180 degrees. (East side).

Current positioning will always be 0 as (North) and positive 180 as (South).

IF the compass is going positively (this means that it is going in the right direction)

Then add +1 for each revolution.

IF the compass is negative (value goes to the left).

Then subtract -1 on each revolution

IF the compass is in OR equal to 0, then this is the current position (NORTH).

IF the compass hits OR equals 90, then it (East).

IF the compass hits OR equals 180, then it (south)

IF the compass hit OR is -90, then it (West).

It turns out that whenever a person goes to the East, the counter will add +1 to 180, then it will change from positive to negative, which will subtract -1 on each revolution until it reaches 0. This will be a full rotation of 360.

0
source

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


All Articles