Digging up an old thread here, but I kind of got it working enough for what I want.
I did what was suggested an took four readings at N,S,E,W, averaged them and deducted them from what Puck.mag() gave me back, then I ran it through the function I had before and it gave me accurate enough readings for the headings (0 for North, 180 for South, -90 for West and 90 for East). They aren't super accurate, but I'm guessing that's because of the nearby magnetic fields etc.
My code is below:
// Be sure to take your own readings for your own magnetic environment!
const avg = { "x": -967, "y": 514, "z": 1762 };
Puck.magOn();
Puck.on('mag', function(xyz){
xyz.x -= avg.x;
xyz.y -= avg.y;
xyz.z -= avg.z;
console.log((Math.atan2(xyz.y, xyz.x) * 180) / Math.PI);
});
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
Digging up an old thread here, but I kind of got it working enough for what I want.
I did what was suggested an took four readings at N,S,E,W, averaged them and deducted them from what
Puck.mag()
gave me back, then I ran it through the function I had before and it gave me accurate enough readings for the headings (0 for North, 180 for South, -90 for West and 90 for East). They aren't super accurate, but I'm guessing that's because of the nearby magnetic fields etc.My code is below: