Fun with gyro

Posted on
  • Here's a fun way to visualize live gyro data for the z axis - especially fun to roll the puck along a desk! Also fascinating to swing it around fast and film it in slow motion, shows a really nice fast response.

    Gyro is quite power hungry so you can tap the puck to toggle it on and off.

    let callbackCount = 0;
    Puck.on('accel', function(a) {
      callbackCount++;
      if(a.gyro.z > 0) {
       analogWrite(LED1, a.gyro.z / 32764);
       analogWrite(LED2, 0);
      } else {
       analogWrite(LED1, 0);
       analogWrite(LED2, -a.gyro.z / 32764);
      }
    });
    
    let gyroOn = false;
    function toggleGyro() {
      if(gyroOn) {
        Puck.accelOff();
        digitalWrite([LED1, LED2], 0);
      } else {
        Puck.accelOn(104);
        // Set max range to 2000 degrees per sec, 104 Hz
        Puck.accelWr(0x11, 76);
      }
      gyroOn = !gyroOn;
    }
    
    setWatch(toggleGyro, BTN, {edge:"rising", debounce:50, repeat:true});
    digitalWrite([LED1, LED2], 0);
    
    /*
    let lastCount = 0;
    setInterval(() => {
      let change = callbackCount - lastCount;
      print((change / 5).toFixed(1) + ' callbacks / sec');
      lastCount = callbackCount;
    }, 5000);
    */
    

    @Gordon - I did notice the callback seems to be called frequently (about 280 times / sec), regardless of the rate passed in to accelOn() - bug?

  • Nice - thanks!

    I believe the callback is called whenever the IRQ line is asserted by the accelerometer/gyro - normally accelOn would then configure the accelerometer for the correct interval (which seems to work ok?) but if you configure the accelerometer for something different with accelWr then that may have an effect.

    When I run your code with the commented out bit uncommented it reports 104 callbacks/sec, so I guess maybe you'd configured the accelerometer some other way while developing? Probably if you just pull the battery and re-add it then run your code again, it'll go back to 104 samples/sec

  • Ah yes, works as expected after a power cycle. I thought I'd only been setting that 0x11 register for full scale modes as shown in the code but it's possible I poked something else at some point.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Fun with gyro

Posted by Avatar for SimonT @SimonT

Actions