-
• #2
Hi - it depends on the code you're using. For example in http://www.espruino.com/Puck.js+Door+Light we have:
var zero = Puck.mag(); var doorOpen = false; function onMag(p) { p.x -= zero.x; p.y -= zero.y; p.z -= zero.z; var s = Math.sqrt(p.x*p.x + p.y*p.y + p.z*p.z); var open = s<1000; if (open!=doorOpen) { doorOpen = open; digitalPulse(open ? LED1 : LED2, 1,1000); } } Puck.on('mag', onMag); Puck.magOn();
The magic bit there is
zero = Puck.mag();
. Just call varzero = Puck.mag();
again and it'll reset thezero
value to the current magnetometer reading. -
• #3
I noticed when testing similar code that the first reading after offsetting with
zero = Puck.mag();
givesx
,y
andz
values close to zero, as you'd expect, but second and subsequent jump to values that vary (+/-) quite a bit from zero, and actually found it better to do use a later reading as a zero/offset, making a couple of calibration passes as belowvar offset = {x:0, y:0, z:0}; var calPass = 2; function readMag(mag){ if (calPass > 0) { // calibration pass offset.x = mag.x; offset.y = mag.y; offset.z = mag.z; calPass--; } else { // provide reading mag.x -= offset.x; mag.y -= offset.y; mag.z -= offset.z; console.log(mag); } } Puck.on('mag', readMag); Puck.magOn();
I doubt it matters much, as slight changes in orientation of Puck still move values considerably, regardless, but it seems the
Puck.mag()
reading doesn't quite do the zeroing/reset I expected. -
• #4
Odd - thanks for letting me know. I guess this is potentially something that might make sense to add to the firmware, where it could take multiple readings.
Just FYI there's a vector maths class that can make these calculations a bit tidier - especially if you maybe wanted to average a few readings : http://www.espruino.com/Vec3
-
• #5
Hi - I can't seem to get the Vec3 module to load.
require("https://www.espruino.com/modules/Vec3.js"); //no errors var strength = new Vec3(xyz).sub(zero).mag(); //throws uncaught referenceerror about Vec not being defined.
Not sure what is up...
Kevin
-
• #6
var Vec3 = require("https://www.espruino.com/modules/Vec3.js");
should work
-
• #7
You list
require("https://www.espruino.com/modules/Vec3.js"); //no errors
Try dropping the .js
require("https://www.espruino.com/modules/Vec3"); //no errors
-
• #8
Why not use
var vec3 = require("Vec3"); //no errorsvar vec3 = require("Vec3"); //no errors console.log(vec3);
1v96 (c) 2017 G.Williams >function (a,b,c) {"object"==typeof a?this.set(a.x,a.y,a.z):"number"==typeof a?this.set(a,b,c):this.set(0,0,0)} =undefined >
-
• #9
Frida, ClearMemory041063 and Ollie,
Will give these ideas a try... I think I tried ClearMemory041063's suggestion of dropping the ".js" with the same result, but will try tomorrow - left my puck at work by accident :(.
Thanks
Kevin
-
• #10
To best of my knowledge dropping the extension only applies where loading the module by name only (letting the IDE find it), for an absolute path to a module (on espruino.com or otherwise) you need the whole path to the file including extension.
You're probably using IDE so Frida's example best, but assigning toVec3
rather thanvec3
is compatible with your code. It's worth pointing that out or you'll get the same error but for a different reason.
I noticed that if the Puck is started next to a magnet, the magnetometer values are "off". If I push my program back down to the Puck (after moving it away from the magnetic source, the magnetometer goes back to "normal". How, in code, do I force a reset/recalibration of the magnetometer - basically simulated the restarting of the unit?
Thanks