Page 1 of 1
Phidget Basic Compass Bearing
Posted: Sun Nov 13, 2022 11:21 pm
by Neutronito
Hello,
I have a Phidget Basic 1042_0, and I was wondering if I would be able to use the library to find the compass bearing, or if I need to run the calculations myself. Using the phidget control panel on Windows, I can see the compass bearing under the Spatial tab, so I know you have the software available to do this. According to
this post, your libraries should now be able to do these calculations for me, but the example is in Python, and I can't seem to find any information on how this works for C in the API.
Any help would be greatly appreciated!
Re: Phidget Basic Compass Bearing
Posted: Mon Nov 14, 2022 10:54 am
by Patrick
Hi,
You'll need to open the spatial channel, and use the getEulerAngles() function. The compass bearing is the yaw. Make sure you use the compass calibration program first, to flash calibration for your setup into the board.
-Patrick
Re: Phidget Basic Compass Bearing
Posted: Mon Nov 14, 2022 6:43 pm
by Neutronito
Hello,
Thank you for your reply!
I can't figure out how to use the getEulerAngles function however, I've had a look at the C API documentation for the 1042 and I can't find any mention of this function. Is there any chance you could tell me how to use it, or perhaps I am not looking for the documentation in the right spot.
Thanks again.
Re: Phidget Basic Compass Bearing
Posted: Tue Nov 15, 2022 11:09 am
by jdecoux
1042 is an older Phidget that doesn't support new features such as internal heading calculation and tracking.
In this case, you will have to do some math, or migrate to an
MOT1102, with accompanying VINT hub.
Re: Phidget Basic Compass Bearing
Posted: Tue Nov 15, 2022 12:38 pm
by Patrick
Have a look at the Spatial C# example code to see how the Compass calculation is done from accelerometer and magnetometer data:
https://www.phidgets.com/downloads/phid ... ows_Ex.zip
Code: Select all
//Roll Angle - about axis 0
// tan(roll angle) = gy/gz
// Use Atan2 so we have an output os (-180 - 180) degrees
double rollAngle = Math.Atan2(gravity[1], gravity[2]);
//Pitch Angle - about axis 1
// tan(pitch angle) = -gx / (gy * sin(roll angle) * gz * cos(roll angle))
// Pitch angle range is (-90 - 90) degrees
double pitchAngle = Math.Atan(-gravity[0] / (gravity[1] * Math.Sin(rollAngle) + gravity[2] * Math.Cos(rollAngle)));
//Yaw Angle - about axis 2
// tan(yaw angle) = (mz * sin(roll) – my * cos(roll)) /
// (mx * cos(pitch) + my * sin(pitch) * sin(roll) + mz * sin(pitch) * cos(roll))
// Use Atan2 to get our range in (-180 - 180)
//
// Yaw angle == 0 degrees when axis 0 is pointing at magnetic north
double yawAngle = Math.Atan2(magField[2] * Math.Sin(rollAngle) - magField[1] * Math.Cos(rollAngle), magField[0] * Math.Cos(pitchAngle) + magField[1] * Math.Sin(pitchAngle) * Math.Sin(rollAngle) + magField[2] * Math.Sin(pitchAngle) * Math.Cos(rollAngle));
-Patrick
Re: Phidget Basic Compass Bearing
Posted: Wed Nov 16, 2022 9:09 pm
by Neutronito
Hi all,
Thank you for your replies, greatly appreciated! I was able to adapt the C# example code for C and get the compass bearing, so all is good now
.
Thank you once again for your time.