I want my app to be able to detect a device rotation while the device is held horizontally. I could use readings from a compass but I believe yaw values from the gyroscope would be more accurate. Given yaw readings, what would be the best algorithm to determine a 360° rotation (either clockwise or counter-clockwise). And it has to be a full 360°, not just turning the phone 180° in one direction and back 180° in the opposite direction.
Share
Here is an idea assuming that you can obtain the readout in short intervals, and that the yaw can be zeroed at a specific start point. This is different from the other answer, which detects full circles from a continuously adapted start point.
In this approach, we keep comparing the current yaw to the previous yaw, asking whether a checkpoint at 180 degrees, or
PIhas been passed. Initially, the checkpoint flagcp_piisNO, and passing it in either direction toggles its state. Note that yaw changes its sign in two places, at the zero point and again atPIto-PI.Assuming your object has two properties that are persistent between
ticks of the detector,BOOL cp_pi;andfloat prev_yaw;, we consider thatd_yawis less thanPIfor crossing 0 and larger thanPIfor crossing at the opposite end of your circle. When crossing the opposite end, we togglecp_pi. Whencp_piisYESwhile crossing 0, we are guaranteed to have passed a full circle – since otherwise,cp_piwould have been toggled back toNO:Note that in order to make our life easier, we skip the detector function entirely if
yawis sitting right on one of the checkpoints.