How can I calculate the heading (N/W/S/E) given (x,y,z) magnetometer and accelerometer data?Can I use Javascript to get the compass heading for iOS and Android?Calculate compass heading from DeviceOrientation Event APIHow can I upload files asynchronously?How can I merge properties of two JavaScript objects dynamically?How can I convert a string to boolean in JavaScript?How can I know which radio button is selected via jQuery?How can I get query string values in JavaScript?How can I display a JavaScript object?How can I select an element with multiple classes in jQuery?How can I pretty-print JSON using JavaScript?How can I refresh a page with jQuery?How does data binding work in AngularJS?
If the Dominion rule using their Jem'Hadar troops, why is their life expectancy so low?
Did I make a mistake by ccing email to boss to others?
Capacitor electron flow
Do native speakers use "ultima" and "proxima" frequently in spoken English?
When is the exact date for EOL of Ubuntu 14.04 LTS?
What is the period/term used describe Giuseppe Arcimboldo's style of painting?
How do you justify more code being written by following clean code practices?
Why does the frost depth increase when the surface temperature warms up?
Why didn’t Eve recognize the little cockroach as a living organism?
In the event of Brexit being postponed beyond the EU elections, will UK voters in EU countries be eligible to participate?
How can an organ that provides biological immortality be unable to regenerate?
Friend wants my recommendation but I don't want to give it to him
Output visual diagram of picture
Why doesn't Gödel's incompleteness theorem apply to false statements?
Are hand made posters acceptable in Academia?
Started in 1987 vs. Starting in 1987
Magnifying glass in hyperbolic space
What 1968 Moog synthesizer was used in the Movie Apollo 11?
"Oh no!" in Latin
Reason why a kingside attack is not justified
Reasons for having MCU pin-states default to pull-up/down out of reset
Taking the numerator and the denominator
Why do Radio Buttons not fill the entire outer circle?
Travelling in US for more than 90 days
How can I calculate the heading (N/W/S/E) given (x,y,z) magnetometer and accelerometer data?
Can I use Javascript to get the compass heading for iOS and Android?Calculate compass heading from DeviceOrientation Event APIHow can I upload files asynchronously?How can I merge properties of two JavaScript objects dynamically?How can I convert a string to boolean in JavaScript?How can I know which radio button is selected via jQuery?How can I get query string values in JavaScript?How can I display a JavaScript object?How can I select an element with multiple classes in jQuery?How can I pretty-print JSON using JavaScript?How can I refresh a page with jQuery?How does data binding work in AngularJS?
I am using react-native-sensor to grab the raw data from these sensors.
import magnetometer, acclerometer from 'react-native-sensors';
const subscription = accelerometer.subscribe(( x, y, z, timestamp ) =>
console.log( x, y, z, timestamp )
this.setState( accelerometer: x, y, z, timestamp )
);
const subscription = magnetometer.subscribe(( x, y, z, timestamp ) =>
console.log( x, y, z )
this.setState( magnetometer: x, y, z, timestamp )
);
Given these 6 data points, how can I get the degree and direction? What is the appropriate algorithm?
I do not understand the algorithm in this answer. This answer utilizes alpha, beta, gamma...is that the same as "x, y, z"? Why does that only use 3 data points and not 6? Why do some other answers say that accelerometer data is required (for tilt adjustment?). Why isn't there an answer that utilizes all 6 data points?
(note: the documentation has a mis-spelling of "magenetometer")
javascript geolocation core-location sensor cllocation
add a comment |
I am using react-native-sensor to grab the raw data from these sensors.
import magnetometer, acclerometer from 'react-native-sensors';
const subscription = accelerometer.subscribe(( x, y, z, timestamp ) =>
console.log( x, y, z, timestamp )
this.setState( accelerometer: x, y, z, timestamp )
);
const subscription = magnetometer.subscribe(( x, y, z, timestamp ) =>
console.log( x, y, z )
this.setState( magnetometer: x, y, z, timestamp )
);
Given these 6 data points, how can I get the degree and direction? What is the appropriate algorithm?
I do not understand the algorithm in this answer. This answer utilizes alpha, beta, gamma...is that the same as "x, y, z"? Why does that only use 3 data points and not 6? Why do some other answers say that accelerometer data is required (for tilt adjustment?). Why isn't there an answer that utilizes all 6 data points?
(note: the documentation has a mis-spelling of "magenetometer")
javascript geolocation core-location sensor cllocation
can you please show input and output examples?
– Maheer Ali
Mar 9 at 6:12
Check out stackoverflow.com/a/26275869/3408531
– TiyebM
Mar 9 at 10:40
add a comment |
I am using react-native-sensor to grab the raw data from these sensors.
import magnetometer, acclerometer from 'react-native-sensors';
const subscription = accelerometer.subscribe(( x, y, z, timestamp ) =>
console.log( x, y, z, timestamp )
this.setState( accelerometer: x, y, z, timestamp )
);
const subscription = magnetometer.subscribe(( x, y, z, timestamp ) =>
console.log( x, y, z )
this.setState( magnetometer: x, y, z, timestamp )
);
Given these 6 data points, how can I get the degree and direction? What is the appropriate algorithm?
I do not understand the algorithm in this answer. This answer utilizes alpha, beta, gamma...is that the same as "x, y, z"? Why does that only use 3 data points and not 6? Why do some other answers say that accelerometer data is required (for tilt adjustment?). Why isn't there an answer that utilizes all 6 data points?
(note: the documentation has a mis-spelling of "magenetometer")
javascript geolocation core-location sensor cllocation
I am using react-native-sensor to grab the raw data from these sensors.
import magnetometer, acclerometer from 'react-native-sensors';
const subscription = accelerometer.subscribe(( x, y, z, timestamp ) =>
console.log( x, y, z, timestamp )
this.setState( accelerometer: x, y, z, timestamp )
);
const subscription = magnetometer.subscribe(( x, y, z, timestamp ) =>
console.log( x, y, z )
this.setState( magnetometer: x, y, z, timestamp )
);
Given these 6 data points, how can I get the degree and direction? What is the appropriate algorithm?
I do not understand the algorithm in this answer. This answer utilizes alpha, beta, gamma...is that the same as "x, y, z"? Why does that only use 3 data points and not 6? Why do some other answers say that accelerometer data is required (for tilt adjustment?). Why isn't there an answer that utilizes all 6 data points?
(note: the documentation has a mis-spelling of "magenetometer")
javascript geolocation core-location sensor cllocation
javascript geolocation core-location sensor cllocation
edited Mar 7 at 0:16
TIMEX
asked Mar 7 at 0:10
TIMEXTIMEX
70.1k275651959
70.1k275651959
can you please show input and output examples?
– Maheer Ali
Mar 9 at 6:12
Check out stackoverflow.com/a/26275869/3408531
– TiyebM
Mar 9 at 10:40
add a comment |
can you please show input and output examples?
– Maheer Ali
Mar 9 at 6:12
Check out stackoverflow.com/a/26275869/3408531
– TiyebM
Mar 9 at 10:40
can you please show input and output examples?
– Maheer Ali
Mar 9 at 6:12
can you please show input and output examples?
– Maheer Ali
Mar 9 at 6:12
Check out stackoverflow.com/a/26275869/3408531
– TiyebM
Mar 9 at 10:40
Check out stackoverflow.com/a/26275869/3408531
– TiyebM
Mar 9 at 10:40
add a comment |
1 Answer
1
active
oldest
votes
Background
The magnetometer measures the Earth’s magnetic field. This information is combined with an accelerator inside the phone. The accelerator gets information regarding the phone’s position in space. It is able to pinpoint the phone’s position from solid-state sensors within the phone that can measure their tilt and movement. The information provided by these devices means that the compass app can display cardinal directions no matter which orientation the phone is in, according to the algorithmic software development company Sensor Platforms.
A similar project: compass-react-native-non-expo under MIT License to use device's built in Magnetometer sensor only, to identify direction and calculate degree of angle using the package react-native-sensors, uses 3 data points from magnetometer:
subscribe = async () =>
new Magnetometer(
updateInterval: 100
)
.then(magnetometerObservable =>
this._subscription = magnetometerObservable;
this._subscription.subscribe(sensorData =>
console.log(sensorData);
this.setState(magnetometer: this._angle(sensorData));
);
)
.catch(error =>
console.log("The sensor is not available");
);
;
_unsubscribe = () =>
this._subscription && this._subscription.stop();
this._subscription = null;
;
_angle = (magnetometer) =>
if (magnetometer)
let x, y, z = magnetometer;
if (Math.atan2(y, x) >= 0)
angle = Math.atan2(y, x) * (180 / Math.PI);
else
angle = (Math.atan2(y, x) + 2 * Math.PI) * (180 / Math.PI);
return Math.round(angle);
;
_direction = (degree) =>
if (degree >= 22.5 && degree < 67.5)
return 'NE';
else if (degree >= 67.5 && degree < 112.5)
return 'E';
else if (degree >= 112.5 && degree < 157.5)
return 'SE';
else if (degree >= 157.5 && degree < 202.5)
return 'S';
else if (degree >= 202.5 && degree < 247.5)
return 'SW';
else if (degree >= 247.5 && degree < 292.5)
return 'W';
else if (degree >= 292.5 && degree < 337.5)
return 'NW';
else
return 'N';
;
// Match the device top with pointer 0° degree. (By default 0° starts from the right of the device.)
_degree = (magnetometer) =>
return magnetometer - 90 >= 0 ? magnetometer - 90 : magnetometer + 271;
;
Another project: react-native-sensor-manager uses the 6 data points from both magnetometer and accelerometer to calculate the orientation:
float[] mGravity;
float[] mGeomagnetic;
@Override
public void onSensorChanged(SensorEvent sensorEvent)
Sensor mySensor = sensorEvent.sensor;
WritableMap map = mArguments.createMap();
if (mySensor.getType() == Sensor.TYPE_ACCELEROMETER)
mGravity = sensorEvent.values;
if (mySensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
mGeomagnetic = sensorEvent.values;
if (mGravity != null && mGeomagnetic != null)
float R[] = new float[9];
float I[] = new float[9];
boolean success = mSensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic);
if (success)
long curTime = System.currentTimeMillis();
float orientation[] = new float[3];
mSensorManager.getOrientation(R, orientation);
float heading = (float)((Math.toDegrees(orientation[0])) % 360.0f);
float pitch = (float)((Math.toDegrees(orientation[1])) % 360.0f);
float roll = (float)((Math.toDegrees(orientation[2])) % 360.0f);
if (heading < 0)
heading = 360 - (0 - heading);
if (pitch < 0)
pitch = 360 - (0 - pitch);
if (roll < 0)
roll = 360 - (0 - roll);
map.putDouble("azimuth", heading);
map.putDouble("pitch", pitch);
map.putDouble("roll", roll);
sendEvent("Orientation", map);
lastUpdate = curTime;
There are others too.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55034145%2fhow-can-i-calculate-the-heading-n-w-s-e-given-x-y-z-magnetometer-and-acceler%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Background
The magnetometer measures the Earth’s magnetic field. This information is combined with an accelerator inside the phone. The accelerator gets information regarding the phone’s position in space. It is able to pinpoint the phone’s position from solid-state sensors within the phone that can measure their tilt and movement. The information provided by these devices means that the compass app can display cardinal directions no matter which orientation the phone is in, according to the algorithmic software development company Sensor Platforms.
A similar project: compass-react-native-non-expo under MIT License to use device's built in Magnetometer sensor only, to identify direction and calculate degree of angle using the package react-native-sensors, uses 3 data points from magnetometer:
subscribe = async () =>
new Magnetometer(
updateInterval: 100
)
.then(magnetometerObservable =>
this._subscription = magnetometerObservable;
this._subscription.subscribe(sensorData =>
console.log(sensorData);
this.setState(magnetometer: this._angle(sensorData));
);
)
.catch(error =>
console.log("The sensor is not available");
);
;
_unsubscribe = () =>
this._subscription && this._subscription.stop();
this._subscription = null;
;
_angle = (magnetometer) =>
if (magnetometer)
let x, y, z = magnetometer;
if (Math.atan2(y, x) >= 0)
angle = Math.atan2(y, x) * (180 / Math.PI);
else
angle = (Math.atan2(y, x) + 2 * Math.PI) * (180 / Math.PI);
return Math.round(angle);
;
_direction = (degree) =>
if (degree >= 22.5 && degree < 67.5)
return 'NE';
else if (degree >= 67.5 && degree < 112.5)
return 'E';
else if (degree >= 112.5 && degree < 157.5)
return 'SE';
else if (degree >= 157.5 && degree < 202.5)
return 'S';
else if (degree >= 202.5 && degree < 247.5)
return 'SW';
else if (degree >= 247.5 && degree < 292.5)
return 'W';
else if (degree >= 292.5 && degree < 337.5)
return 'NW';
else
return 'N';
;
// Match the device top with pointer 0° degree. (By default 0° starts from the right of the device.)
_degree = (magnetometer) =>
return magnetometer - 90 >= 0 ? magnetometer - 90 : magnetometer + 271;
;
Another project: react-native-sensor-manager uses the 6 data points from both magnetometer and accelerometer to calculate the orientation:
float[] mGravity;
float[] mGeomagnetic;
@Override
public void onSensorChanged(SensorEvent sensorEvent)
Sensor mySensor = sensorEvent.sensor;
WritableMap map = mArguments.createMap();
if (mySensor.getType() == Sensor.TYPE_ACCELEROMETER)
mGravity = sensorEvent.values;
if (mySensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
mGeomagnetic = sensorEvent.values;
if (mGravity != null && mGeomagnetic != null)
float R[] = new float[9];
float I[] = new float[9];
boolean success = mSensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic);
if (success)
long curTime = System.currentTimeMillis();
float orientation[] = new float[3];
mSensorManager.getOrientation(R, orientation);
float heading = (float)((Math.toDegrees(orientation[0])) % 360.0f);
float pitch = (float)((Math.toDegrees(orientation[1])) % 360.0f);
float roll = (float)((Math.toDegrees(orientation[2])) % 360.0f);
if (heading < 0)
heading = 360 - (0 - heading);
if (pitch < 0)
pitch = 360 - (0 - pitch);
if (roll < 0)
roll = 360 - (0 - roll);
map.putDouble("azimuth", heading);
map.putDouble("pitch", pitch);
map.putDouble("roll", roll);
sendEvent("Orientation", map);
lastUpdate = curTime;
There are others too.
add a comment |
Background
The magnetometer measures the Earth’s magnetic field. This information is combined with an accelerator inside the phone. The accelerator gets information regarding the phone’s position in space. It is able to pinpoint the phone’s position from solid-state sensors within the phone that can measure their tilt and movement. The information provided by these devices means that the compass app can display cardinal directions no matter which orientation the phone is in, according to the algorithmic software development company Sensor Platforms.
A similar project: compass-react-native-non-expo under MIT License to use device's built in Magnetometer sensor only, to identify direction and calculate degree of angle using the package react-native-sensors, uses 3 data points from magnetometer:
subscribe = async () =>
new Magnetometer(
updateInterval: 100
)
.then(magnetometerObservable =>
this._subscription = magnetometerObservable;
this._subscription.subscribe(sensorData =>
console.log(sensorData);
this.setState(magnetometer: this._angle(sensorData));
);
)
.catch(error =>
console.log("The sensor is not available");
);
;
_unsubscribe = () =>
this._subscription && this._subscription.stop();
this._subscription = null;
;
_angle = (magnetometer) =>
if (magnetometer)
let x, y, z = magnetometer;
if (Math.atan2(y, x) >= 0)
angle = Math.atan2(y, x) * (180 / Math.PI);
else
angle = (Math.atan2(y, x) + 2 * Math.PI) * (180 / Math.PI);
return Math.round(angle);
;
_direction = (degree) =>
if (degree >= 22.5 && degree < 67.5)
return 'NE';
else if (degree >= 67.5 && degree < 112.5)
return 'E';
else if (degree >= 112.5 && degree < 157.5)
return 'SE';
else if (degree >= 157.5 && degree < 202.5)
return 'S';
else if (degree >= 202.5 && degree < 247.5)
return 'SW';
else if (degree >= 247.5 && degree < 292.5)
return 'W';
else if (degree >= 292.5 && degree < 337.5)
return 'NW';
else
return 'N';
;
// Match the device top with pointer 0° degree. (By default 0° starts from the right of the device.)
_degree = (magnetometer) =>
return magnetometer - 90 >= 0 ? magnetometer - 90 : magnetometer + 271;
;
Another project: react-native-sensor-manager uses the 6 data points from both magnetometer and accelerometer to calculate the orientation:
float[] mGravity;
float[] mGeomagnetic;
@Override
public void onSensorChanged(SensorEvent sensorEvent)
Sensor mySensor = sensorEvent.sensor;
WritableMap map = mArguments.createMap();
if (mySensor.getType() == Sensor.TYPE_ACCELEROMETER)
mGravity = sensorEvent.values;
if (mySensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
mGeomagnetic = sensorEvent.values;
if (mGravity != null && mGeomagnetic != null)
float R[] = new float[9];
float I[] = new float[9];
boolean success = mSensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic);
if (success)
long curTime = System.currentTimeMillis();
float orientation[] = new float[3];
mSensorManager.getOrientation(R, orientation);
float heading = (float)((Math.toDegrees(orientation[0])) % 360.0f);
float pitch = (float)((Math.toDegrees(orientation[1])) % 360.0f);
float roll = (float)((Math.toDegrees(orientation[2])) % 360.0f);
if (heading < 0)
heading = 360 - (0 - heading);
if (pitch < 0)
pitch = 360 - (0 - pitch);
if (roll < 0)
roll = 360 - (0 - roll);
map.putDouble("azimuth", heading);
map.putDouble("pitch", pitch);
map.putDouble("roll", roll);
sendEvent("Orientation", map);
lastUpdate = curTime;
There are others too.
add a comment |
Background
The magnetometer measures the Earth’s magnetic field. This information is combined with an accelerator inside the phone. The accelerator gets information regarding the phone’s position in space. It is able to pinpoint the phone’s position from solid-state sensors within the phone that can measure their tilt and movement. The information provided by these devices means that the compass app can display cardinal directions no matter which orientation the phone is in, according to the algorithmic software development company Sensor Platforms.
A similar project: compass-react-native-non-expo under MIT License to use device's built in Magnetometer sensor only, to identify direction and calculate degree of angle using the package react-native-sensors, uses 3 data points from magnetometer:
subscribe = async () =>
new Magnetometer(
updateInterval: 100
)
.then(magnetometerObservable =>
this._subscription = magnetometerObservable;
this._subscription.subscribe(sensorData =>
console.log(sensorData);
this.setState(magnetometer: this._angle(sensorData));
);
)
.catch(error =>
console.log("The sensor is not available");
);
;
_unsubscribe = () =>
this._subscription && this._subscription.stop();
this._subscription = null;
;
_angle = (magnetometer) =>
if (magnetometer)
let x, y, z = magnetometer;
if (Math.atan2(y, x) >= 0)
angle = Math.atan2(y, x) * (180 / Math.PI);
else
angle = (Math.atan2(y, x) + 2 * Math.PI) * (180 / Math.PI);
return Math.round(angle);
;
_direction = (degree) =>
if (degree >= 22.5 && degree < 67.5)
return 'NE';
else if (degree >= 67.5 && degree < 112.5)
return 'E';
else if (degree >= 112.5 && degree < 157.5)
return 'SE';
else if (degree >= 157.5 && degree < 202.5)
return 'S';
else if (degree >= 202.5 && degree < 247.5)
return 'SW';
else if (degree >= 247.5 && degree < 292.5)
return 'W';
else if (degree >= 292.5 && degree < 337.5)
return 'NW';
else
return 'N';
;
// Match the device top with pointer 0° degree. (By default 0° starts from the right of the device.)
_degree = (magnetometer) =>
return magnetometer - 90 >= 0 ? magnetometer - 90 : magnetometer + 271;
;
Another project: react-native-sensor-manager uses the 6 data points from both magnetometer and accelerometer to calculate the orientation:
float[] mGravity;
float[] mGeomagnetic;
@Override
public void onSensorChanged(SensorEvent sensorEvent)
Sensor mySensor = sensorEvent.sensor;
WritableMap map = mArguments.createMap();
if (mySensor.getType() == Sensor.TYPE_ACCELEROMETER)
mGravity = sensorEvent.values;
if (mySensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
mGeomagnetic = sensorEvent.values;
if (mGravity != null && mGeomagnetic != null)
float R[] = new float[9];
float I[] = new float[9];
boolean success = mSensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic);
if (success)
long curTime = System.currentTimeMillis();
float orientation[] = new float[3];
mSensorManager.getOrientation(R, orientation);
float heading = (float)((Math.toDegrees(orientation[0])) % 360.0f);
float pitch = (float)((Math.toDegrees(orientation[1])) % 360.0f);
float roll = (float)((Math.toDegrees(orientation[2])) % 360.0f);
if (heading < 0)
heading = 360 - (0 - heading);
if (pitch < 0)
pitch = 360 - (0 - pitch);
if (roll < 0)
roll = 360 - (0 - roll);
map.putDouble("azimuth", heading);
map.putDouble("pitch", pitch);
map.putDouble("roll", roll);
sendEvent("Orientation", map);
lastUpdate = curTime;
There are others too.
Background
The magnetometer measures the Earth’s magnetic field. This information is combined with an accelerator inside the phone. The accelerator gets information regarding the phone’s position in space. It is able to pinpoint the phone’s position from solid-state sensors within the phone that can measure their tilt and movement. The information provided by these devices means that the compass app can display cardinal directions no matter which orientation the phone is in, according to the algorithmic software development company Sensor Platforms.
A similar project: compass-react-native-non-expo under MIT License to use device's built in Magnetometer sensor only, to identify direction and calculate degree of angle using the package react-native-sensors, uses 3 data points from magnetometer:
subscribe = async () =>
new Magnetometer(
updateInterval: 100
)
.then(magnetometerObservable =>
this._subscription = magnetometerObservable;
this._subscription.subscribe(sensorData =>
console.log(sensorData);
this.setState(magnetometer: this._angle(sensorData));
);
)
.catch(error =>
console.log("The sensor is not available");
);
;
_unsubscribe = () =>
this._subscription && this._subscription.stop();
this._subscription = null;
;
_angle = (magnetometer) =>
if (magnetometer)
let x, y, z = magnetometer;
if (Math.atan2(y, x) >= 0)
angle = Math.atan2(y, x) * (180 / Math.PI);
else
angle = (Math.atan2(y, x) + 2 * Math.PI) * (180 / Math.PI);
return Math.round(angle);
;
_direction = (degree) =>
if (degree >= 22.5 && degree < 67.5)
return 'NE';
else if (degree >= 67.5 && degree < 112.5)
return 'E';
else if (degree >= 112.5 && degree < 157.5)
return 'SE';
else if (degree >= 157.5 && degree < 202.5)
return 'S';
else if (degree >= 202.5 && degree < 247.5)
return 'SW';
else if (degree >= 247.5 && degree < 292.5)
return 'W';
else if (degree >= 292.5 && degree < 337.5)
return 'NW';
else
return 'N';
;
// Match the device top with pointer 0° degree. (By default 0° starts from the right of the device.)
_degree = (magnetometer) =>
return magnetometer - 90 >= 0 ? magnetometer - 90 : magnetometer + 271;
;
Another project: react-native-sensor-manager uses the 6 data points from both magnetometer and accelerometer to calculate the orientation:
float[] mGravity;
float[] mGeomagnetic;
@Override
public void onSensorChanged(SensorEvent sensorEvent)
Sensor mySensor = sensorEvent.sensor;
WritableMap map = mArguments.createMap();
if (mySensor.getType() == Sensor.TYPE_ACCELEROMETER)
mGravity = sensorEvent.values;
if (mySensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
mGeomagnetic = sensorEvent.values;
if (mGravity != null && mGeomagnetic != null)
float R[] = new float[9];
float I[] = new float[9];
boolean success = mSensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic);
if (success)
long curTime = System.currentTimeMillis();
float orientation[] = new float[3];
mSensorManager.getOrientation(R, orientation);
float heading = (float)((Math.toDegrees(orientation[0])) % 360.0f);
float pitch = (float)((Math.toDegrees(orientation[1])) % 360.0f);
float roll = (float)((Math.toDegrees(orientation[2])) % 360.0f);
if (heading < 0)
heading = 360 - (0 - heading);
if (pitch < 0)
pitch = 360 - (0 - pitch);
if (roll < 0)
roll = 360 - (0 - roll);
map.putDouble("azimuth", heading);
map.putDouble("pitch", pitch);
map.putDouble("roll", roll);
sendEvent("Orientation", map);
lastUpdate = curTime;
There are others too.
edited Mar 10 at 14:02
answered Mar 10 at 13:55
TiyebMTiyebM
1,5261133
1,5261133
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55034145%2fhow-can-i-calculate-the-heading-n-w-s-e-given-x-y-z-magnetometer-and-acceler%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
can you please show input and output examples?
– Maheer Ali
Mar 9 at 6:12
Check out stackoverflow.com/a/26275869/3408531
– TiyebM
Mar 9 at 10:40