Avatar for trusktr

trusktr

Member since Apr 2017 • Last active Jun 2017
  • 10 conversations
  • 44 comments

Most recent activity

  • in Puck.js
    Avatar for trusktr

    Hmm, WebRTC sounds like it would be a great way to, for example, stream sensor values to a client (f.e. accelerometer from Espruino to Chrome tab).

    Do you guys have WebRTC running in Espruino?

  • in General
    Avatar for trusktr

    It also seems like up until the this crash happens, the network responses take longer and longer until the crash. I'm keeping a running average, f.e.

        let ws = new WebSocket("ws://192.168.43.247/my_websoc­ket", "protocolOne");
        let lastTime = performance.now()
        let timesCount = 0
        let totalTime = 0
        let averageTime = 0
        ws.addEventListener('message', ({data}) => {
            timesCount++
    
            const thisTime = performance.now()
            const elapsed = thisTime - lastTime
    
            totalTime += elapsed
            averageTime = totalTime / timesCount
    
            lastTime = thisTime
    
            mpuData = parseEspruinoJson(data)
        });
        ws.addEventListener('open', () => {
            ws.send("Hello to Espruino!");
        })
    
        setInterval(() => console.log('avg time:', averageTime), 2000)
    

    and at first the average is about 38 ms for the time between responses, and by the time it crashes the running average is around 50 ms. It increases steadily the whole time.

  • in General
    Avatar for trusktr

    I'm not sure if this has anything to do with it, but I'm getting a bunch of errors in the Espruino console:

     _____                 _
    |   __|___ ___ ___ _ _|_|___ ___
    |   __|_ -| . |  _| | | |   | . |
    |_____|___|  _|_| |___|_|_|_|___|
              |_| 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!"
    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(67)
                    ^
    in function "getRotation" called from line 1 col 36
    ...is;return this.getRotation().map(function(c){retur­n c/a.gyro...
                                  ^
    in function "getDegreesPerSecond" called from line 5 col 51
    ...n: mpu.getDegreesPerSecond(), // returns gyro array in degre...
                                  ^
    in function called from system
    ERROR: Out of Memory!
    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 1 col 40
    ...eturn this.getAcceleration().map(function(c){r­eturn c/a.acc_...
                                  ^
    in function "getGravity" called from line 3 col 46
    ...celeration: mpu.getGravity(),  // returns acceleration array...
                                  ^
    in function called from system
    Execution Interrupted
    Execution Interrupted
    Execution Interrupted
    Execution Interrupted
    Execution Interrupted
    Execution Interrupted
    ERROR: Ctrl-C while processing interval - removing it.
    Execution Interrupted during event processing.
    at line 1 col 66
    ...is.i2c.readFrom(this.addr,6);a=c[0]<<­8|c[1];var b=in function "getRotation" called from line 1 col 36
    ...is;return this.getRotation().in function "getDegrees" called fromin function called from system
    > 
    
  • in General
    Avatar for trusktr

    Sometimes I receive the following error in Chrome when it is receiving websocket messages directly from Espruino WiFi:

    WebSocket connection to 'ws://192.168.43.247/my_websocket' failed: Could not decode a text frame as UTF-8.
    

    I'm using the ws module for Espruino. Maybe there's something in that module that causes the websocket response to be invalid?

    What I'm trying to do in practice is read values from the MPU6050 module, and send them through the websocket connection. Something like:

        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.getGravity(),  // returns acceleration array in G's
                    rotation: mpu.getDegreesPerSecond(), // returns gyro array in degrees/s
                }
                result = JSON.stringify(result)
                ws.send(result);
            }, 16.666);
        });
    

    Where getMPU() just return an MPU instance as per the MPU6050 docs, connected over I2C.

    So I have that setInterval loop that just continuously sends values over the websocket. The client code in Chrome does the following:

        let mpuData = {
            acceleration: [0,0,0],
            rotation: [0,0,0],
        }
    
        let ws = new WebSocket("ws://192.168.43.247/my_websoc­ket", "protocolOne");
        ws.addEventListener('message', ({data}) => {
            mpuData = parseEspruinoJson(data)
        });
        ws.addEventListener('open', () => {
            ws.send("Hello to Espruino!");
        })
    
        setInterval(() => console.log('data:', mpuData), 2000)
    
        // ... requestAnimationFrame loop uses mpuData for graphics ...
    

    Eventually, after a minute or two, I might get that error, and the connection breaks.

  • in General
    Avatar for trusktr

    Is this normal?

    I have been playing with the MPU6050, but I've noticed that the mpu.getRotation() values, etc, fluctuate a lot even if the accelerometer is completely still. Is this expected?

    Maybe the getRotation() values represent a small delta, so they fluctuate from vibrations and/or imprecision?

    I haven't figured out how to normalize the value from the four functions in the doc into yaw/pitch/roll, maybe that will be more stable?

    (I'm completely new to this stuff, so I have no idea, yet!)

  • in Interfacing
    Avatar for trusktr

    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_websoc­ket", "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?

Actions