nathankellenicki / node-poweredup

A Javascript module to interface with LEGO Powered Up components.
https://nathankellenicki.github.io/node-poweredup/
MIT License
477 stars 59 forks source link

add impact count mode to tiltsensor #147

Closed Debenben closed 2 years ago

Debenben commented 2 years ago

This adds some probably rarely required functionality, feel free to decline.

My problem was that the tilt angle approaching y=90 and y=-90 seems to be inaccurate, e.g. you can see it jumping form y=85 directly to y=90. To verify one can compare to the angle using the acceleration values which I ended up using:

    hub.on("accel", (device, { x, y, z }) => {
    const X = Math.round(Math.atan2(y, z)*180/Math.PI);
    const Y = -Math.round(Math.atan2(x, Math.sqrt(y**2 + z**2))*180/Math.PI);
        console.log(`accel (X: ${X}, Y: ${Y}`);
    });
    hub.on("tilt", (device, { x, y, z }) => {
        console.log(`tilt  (X: ${x}, Y: ${y}, Z: ${z}`);
    });

My original goal was to configure the orientation like mentioned here https://lego.github.io/lego-ble-wireless-protocol-docs/#output-sub-command-tiltconfigorientation-orientation-n-a. Unfortunately the documentation seems to be outdated and I did not find any up to date one. I was able to set the impact threshold and bump holdoff which I wanted to share in case someone is looking for that or also trying to configure the orientation then it might be of some help.