• I have made some progress.

    The following code calculates the min and max value for the mag x,y,z axis using Vec3 module. Can you test and show what you get on your puck. The to do at the end of the code shows my next steps.

    To use hold down the button for 5 seconds then rotate the puck in all directions until you only see green flashes (no blue). Then press the button to end calibration. The min and max values are then logged on your debug screen.

    var Vec3 = require('Vec3');
    var vMin = new Vec3();
    var vMax = new Vec3();
    var calibrating = false;
    var count = 0;
    
    
    // call a function if button is held down for 4 seconds
    function b4Seconds(callback) {
      setWatch(function(e) {
        console.log("Button down");
        t4s = setTimeout(function() {
          callback();
        },4000);
        setWatch(function(e) {
          clearTimeout(t4s);
          console.log("Button up");
        }, BTN, { repeat: false, edge: 'falling', debounce: 50 });
      }, BTN, { repeat: true, edge: 'rising', debounce: 50 });
    }
    
    
    function calibrate () {
      console.log('calibrating...');
      calibrating = true;
      var xyz = Puck.mag();
      var sMax = 0; 
      var sMin = 0;
      count = 0;
      vMin = new Vec3(xyz);
      vMax = new Vec3(xyz);
      Puck.magOn(10);
      Puck.on('mag', function(xyz) {
        var vMag = new Vec3(xyz);
        vMin = vMin.min(vMag);
        vMax = vMax.max(vMag);
        if ((sMax !=  vMax.mag()) || (sMin != vMin.mag())) {
          sMin = vMin.mag();
          sMax = vMax.mag();
          count++;
          console.log('new min or max: ',count);
          // flash the blue LED when calibrating
          digitalWrite(LED3, count % 2 == 0);
        } else { 
        // flash the green LED when not calibrating
        digitalWrite(LED2, Math.random()>0.5);
        digitalWrite(LED3, false);
        }
      });
      //press the buton to stop calibrating
      setWatch(function(e) {
        Puck.magOff();
        calibrating = false;
        console.log("calibarion completed");
        console.log('max: ',vMax);
        console.log('min: ',vMin);
        digitalWrite(LED3, false);
        digitalWrite(LED2, false);
        // TO DO 
        // get the zero position when the end calibrate button was pressed
        // calculate the average of vMin and vMax to get the nuteral position
        // calculate the spread of vMin, vMax to scale future readings
        // use the above to find out if the puck is rotating clockwise or anti
        // use the above to find out if the puck is upside down
        // send BT HID key presses
      }, BTN, { repeat: false, edge: 'rising', debounce: 50 });
    }
    
    //calibrate by holding button down for 4 seconds
    b4Seconds(calibrate);
    
    
    

    | |_ ___ ___ _ ||___ ___
    | |_ -| . | _| | | | | . |
    |
    |_| || |_|||_|_|

         |_| espruino.com
    

    2v03 (c) 2018 G.Williams

    Button down
    calibrating...
    new min or max: 1
    new min or max: 2
    new min or max: 3
    new min or max: 4
    new min or max: 5
    new min or max: 6
    Button up
    new min or max: 7
    new min or max: 8
    new min or max: 9
    new min or max: 10
    new min or max: 11
    new min or max: 12
    new min or max: 13
    new min or max: 14
    new min or max: 15
    new min or max: 16
    new min or max: 17
    new min or max: 18
    new min or max: 19
    new min or max: 20
    new min or max: 21
    new min or max: 22
    new min or max: 23
    new min or max: 24
    new min or max: 25
    new min or max: 26
    new min or max: 27
    new min or max: 28
    new min or max: 29
    new min or max: 30
    new min or max: 31
    new min or max: 32
    new min or max: 33
    new min or max: 34
    new min or max: 35
    new min or max: 36
    new min or max: 37
    new min or max: 38
    Button down
    calibarion completed
    max: Vec3: { "x": 794, "y": 527, "z": 124 }
    min: Vec3: { "x": 104, "y": -179, "z": -857 }
    Button up

  • Sat 2019.06.29

    Hi @user101436, just happen to have a Puck lying around, desperately waiting to do something useful. ;-)

    Results of two separate tests, about 300 degrees each axis:

    new min or max:  68
    new min or max:  69
    new min or max:  70
    Button up
    Button down
    calibarion completed
    max:  Vec3: { "x": -2493, "y": 2277, "z": 493 }
    min:  Vec3: { "x": -3198, "y": 1564, "z": -1226 }
    Button up
    >
    


    new min or max:  108
    new min or max:  109
    new min or max:  110
    Button up
    Button down
    calibarion completed
    max:  Vec3: { "x": -2654, "y": 2234, "z": 100 }
    min:  Vec3: { "x": -3308, "y": 1540, "z": -1256 }
    Button up
    >
    

    After giving this a whirl, and re-reading the objective in #1, I have to agree you are on to a pretty cool idea here. I really like the idea of inverting the Puck to get yet another feature.

    Just a minute, . . . I have to get up, walk over to the telly, change the channel and turn down the volume!!

    If it hasn't been thought of yet, put the Puck to sleep to save battery.

About

Avatar for user101436 @user101436 started