360 Β° iOS device rotation detection using gyro

I want my application to detect device rotation when the device is held horizontally. I could use the readings from the compass, but I believe that the yaw values ​​from the gyro would be more accurate. Given yaw rate, which algorithm should be optimal for 360 Β° rotation (clockwise or counterclockwise). And it should be full 360 Β°, and not just rotate the phone 180 Β° in one direction and back 180 Β° in the opposite direction.

+4
source share
2 answers

Here's an idea suggesting that you can get a reading at short intervals, and that yaw can be zeroed at a specific starting point. This is different from another answer that detects full circles from a continuously adapted starting point.

In this approach, we continue to compare the current jerk with the previous yaw, asking if the control point passed 180 degrees or PI . Initially, the checkpoint flag cp_pi is NO , and transmitting it in any direction switches its state. Note that yaw changes sign in two places, at zero point and again on PI to -PI .

Assuming your object has two properties that are persistent between the tick detector, BOOL cp_pi; and float prev_yaw; , we think d_yaw less than PI for crossing 0 and greater than PI for crossing at the opposite end of your circle. When crossing the opposite end, we switch cp_pi . When cp_pi is YES at the intersection 0, we are guaranteed to go through the full circle, since otherwise cp_pi would be switched to NO :

 -(void)tick { float yaw = [self zeroedYaw]; if ((fabs(yaw) == PI) || (yaw == 0.0f)) return; if (yaw * prev_yaw < 0) { float d_yaw = fabs(yaw - prev_yaw); if (d_yaw > PI) { cp_pi = ! cp_pi; } else if (cp_pi) { // fire detection event } } prev_yaw = yaw; } 

Please note that to make our life easier, we completely skip the detector function if yaw sits directly on one of the control points.

+2
source

You must use CoreMotion to get a rotation around the vertical axis. You need to add a rotation event delta. Each time more than the minimum value, in a different direction than the previous one, you reset the starting point. Then, when you reach either plus or minus 360 degrees from this starting point, you have a turn.

+3
source

All Articles