• Alright, so I'm trying it with actual values from the accelerometer, but that really slows it down. For example:

    function getMPU() {
        I2C1.setup({scl:B6,sda:B7, bitrate: 100000});
    
        var MPU6050 = require("MPU6050");
        console.log(MPU6050);
    
        var mpu = MPU6050.connect(I2C1);
    
        return mpu
    }
    
    function connectToWifi(wifiName, options) {
        var resolve, reject
        var promise = new Promise(function(res, rej) {resolve = res; reject = rej})
    
        var wifi = require("EspruinoWiFi");
    
        wifi.connect(wifiName, options, function(err) {
            if (err) reject(err);
            resolve();
        });
    
        return promise
    }
    
    function getIP() {
        const wifi = require("EspruinoWiFi");
        wifi.getIP(function(err, info) {
            if (err) {
                console.log('Wifi connection failed, trying again in a sec...')
                setTimeout(getIP, 1000)
            }
            console.log('IP:', info.ip)
        });
    }
    
    connectToWifi("starlancer", { password : "Next stop: Mars." })
    .then(function() {
        getIP();
    
        var server = require('ws').createServer(function() {});
    
        server.listen(80);
        server.on("websocket", function(ws) {
            ws.on('message', function(msg) {
                print("[WS] "+JSON.stringify(msg));
            });
            const mpu = getMPU();
            setInterval(function() {
                let result = {
                    acceleration: mpu.getAcceleration(), // returns an [x,y,z] array with raw accl. data
                    gravity: mpu.getGravity(),  // returns acceleration array in G's
                    rotation: mpu.getRotation(), // returns an [x,y,z] array with raw gyro data
                    degreesPerSecond: mpu.getDegreesPerSecond(), // returns gyro array in degrees/s
                }
                result = JSON.stringify(result)
                ws.send(result);
            }, 16.666);
        });
    
    });
    
    function onInit() {
        console.log('Espruino started!');
    }
    

    And the browser just does something like

        let mpuData = {...defaults};
        let ws = new WebSocket("ws://192.168.43.247/my_websocket", "protocolOne");
        ws.addEventListener('message', ({data}) => {
            mpuData = parseEspruinoJson(data)
        });
    
        // ... use mpuData in requestAnimationFrame loop ...
    

    The rendering in my animation loop is slow now that I'm sending actual MPU data.

    Also after a short while I start to see some out-of-memory output until it crashes:

     _____                 _
    |   __|___ ___ ___ _ _|_|___ ___
    |   __|_ -| . |  _| | | |   | . |
    |_____|___|  _|_| |___|_|_|_|___|
              |_| http://espruino.com
     1v91 Copyright 2016 G.Williams
    >
    =undefined
    IP: 192.168.43.247
    {
      "connect": function (a,c) {return new b(a,c)}
     }
    [WS] "Hello to Espruino!"
    ERROR: Out of Memory!
    Execution Interrupted
    Execution Interrupted
    Execution Interrupted
    Execution Interrupted
    Execution Interrupted
    Execution Interrupted
    Execution Interrupted
    Uncaught InternalError: Timeout on I2C Read Receive
     at line 1 col 66
    ...is.i2c.readFrom(this.addr,6);a=c[0]<<8|c[1];var b=c[2]<<8|c[...
                                  ^
    in function "readSXYZ" called from line 1 col 17
    this.readSXYZ(59)
                    ^
    in function "getAcceleration" called from line 2 col 51
    ...ation: mpu.getAcceleration(), // returns an [x,y,z] array wi...
                                  ^
    in function called from system
    

    It seems like trying to read MPU and send over socket every 16.666ms is too much. Is that right?

    What might be the best way to do this to get values as fast as possible and not crash it? Maybe I need to be able to somehow detect how many network request and/or MPU requests are queued, and not request faster than is possible, somehow?

About

Avatar for trusktr @trusktr started