"Otto DIY" smooth movements adaptation for Espruino

Posted on
  • Did anyone ever tried to replicate such smooth robot movements on Espruino? https://youtu.be/Of2TmVZtMCo

    I learned a little bit about servo class, digitalPulse and analogWrite, but I am struggling on sequencing multiple moves one after the other. Javascript isn't quite optimal for such sequences.
    Did anyone implement Promises for servo control?

  • I guess you might find what I did for the laser cutter helpful: http://forum.espruino.com/conversations/­821/#26363

    It was for steppers, not servos - but the same should apply for more than one servo:

    function moveTo(p, speed, callback) {
      var dx = x.pos - p[0];
      var dy = y.pos - p[1];
      var d = Math.sqrt(dx*dx+dy*dy);
      var time = d*speed;
      x.move(p[0],time, undefined);
      y.move(p[1],time, callback);
    }
    

    You can just call move on multiple servo instances, having the same time period for all of them - and then callback when one has finished (they will all finish at the same time). Note the code above is trying to keep a constant speed, not movement time.

    Getting that to work as a promise, with a set time perios should be something like:

    function moveTo(p, time) {
     return new Promise(function(resolve){
      x.move(p[0],time, undefined);
      y.move(p[1],time, resolve);
     }
    }
    
    moveTo([0,0],1000).then(x=>{
      moveTo([1,1],1000)
    })...etc
    
    
  • Thanks Gordon I'll give it a try!

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

"Otto DIY" smooth movements adaptation for Espruino

Posted by Avatar for Jean-Philippe_Rey @Jean-Philippe_Rey

Actions