Expo orientation sensor is a javascript library for react native applications managed with expo.
The purpose of this library is to keep track of the phone orientation by providing access to the three Euler Angles:
- pitch
- roll
- yaw
(Look at Orientation)
This virtual sensor uses data form Accelerometer, Magnetometer and Gyroscope fused together by AHRS (using Madgwick algorithm) to calculate the exact orientation.
npm install expo-orientation-sensor
The usage of this package is very similar to the way you use expo-sensors, with some extra functionalities.
import { Orientation } from 'expo-orientation-sensor'
const [angles, setAngles] = useState({
yaw: 0,
pitch: 0,
roll: 0,
})
useEffect(() => {
const subscriber = Orientation.addListener(data => {
setAngles(data)
})
return () => {
subscriber.remove()
}
}, [])
This code should update state angles
every time new measurements are available.
You don't have to keep track of all updates, you can call Orientation.eulerAngles
and it will return the latest angles measured.
Keep in mind that if there are no listeners added by Orientation.addListener
, or all listeners have been removed The angles will NOT be updated.
The image below shows the pitch, roll, and yaw axis and their positive direction.
-
Yaw axis:
an axis drawn from front to back, and perpendicular to the other two axes.
A positive yawing motion moves the top of the device to the right. -
Pitch axis:
an axis running from the device's left to right, and parallel to the screen.
A positive pitching motion raises the top of the device and lowers the bottom. -
Roll axis:
an axis drawn through the body of the device from bottom to top.
A positive rolling motion lifts the left side of the device and lowers the right side.
Orientation.eulerAngles
Returns the latest angles measured.
Note: the angles returned might be outdated if no listeners are set.
Orientation.isAvailableAsync()
Returns whether this sensor is available on the device.
Should be true if Accelerometer, Magnetometer, and Gyroscope are available.
Orientation.addListener()
Subscribe for updates to the orientation.
Arguments:
- listener (function): A callback that is invoked when an orientation update is available. When invoked, the listener is provided with the current Euler angles.
Returns:
- A subscription that you can call
remove()
on when you would like to unsubscribe the listener.
Orientation.removeAllListeners()
Remove all listeners.
Orientation.setUpdateInterval()
Subscribe for updates to the orientation.
Arguments
- intervalMs (number): Desired interval in milliseconds between gyroscope updates.
Orientation.hasListener
Return a boolean indicating whether there is at least a listener.
Orientation.listenerCount
Returns how many listeners are currently subscribed.