all right, if anyone needs more or less the same hack for servo.move, I have the following code ( not yet tested but worked flawlessly within a browser ;p )
var interval, currentPos;
var offs = 1, mul = 1;
var options = {};
//var options = { range: 3};
if (options && options.range) {
mul = options.range;
offs = 1.5-(mul/2);
}
// helpers
// replacement for E.clip when not in Espruino env
var clip = function(val, min, max){
if(val < min) val = min;
if(val > max) val = max;
return val;
};
// the good-ol' Ar map() !
var map = function(x, in_min, in_max, out_min, out_max){
//return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
var mapped = (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
return mapped.toFixed(3);
}
var move = function(pos, time, callback) {
if (time===undefined) time = 1000; // T: if no time is set as duration for the move, set 1 sec
var amt = 0;
if (currentPos===undefined) currentPos = pos; // T: if no currentPos is defined, set it to the goal position
if (interval) // T: if already existing interval, cancel it
clearInterval(interval);
var initial = currentPos; // T: set initial position for the move
interval = setInterval(function() { // T: schedule re-calls
if (amt>1) { // T: move fully executed ( & maybe a little farther ? )
clearInterval(interval); // T: no more re-calls
interval = undefined; // T: reset interval
amt = 1; // T: set move to fully executed
if (callback) callback(); // T: move ended callback
}
currentPos = pos*amt + initial*(1-amt); // T: calculate currentPos from initialPos, pos & current amount ( steps already took )
//digitalPulse(pin, 1, offs+E.clip(currentPos,0,1)*mul); // T: function digitalPulse(pin, value_highOrLow, time_duration), clip to min & max
//digitalPulse(pin, 1, offs+clip(currentPos,0,1)*mul); // T: function digitalPulse(pin, value_highOrLow, time_duration), clip to min & max
console.log('HIGH pulse width: ' + ( offs+clip(currentPos,0,1)*mul ) );
console.log('amt: ' + amt);
// IDEA: clip/map to 0.03..0.115 ?
console.log('hacky analogWrite value:' + map(amt, 0, 1, 0.03, 0.15) );
amt += 1000.0 / (20*time); // T: next step
}, 20);
};
// test-drive ..
move(1, 500, function(){
console.log('movement done !');
})
I'll test the above as soon as I can, although not that efficient, it should hopefully do the trick :)
On the "web side of things", still no success using the attached code ( "kinda" works .. sometimes .. for some values .. so actually more random that working ;p ), and the tests I could run using Audacity to generate tones were no that concluant :/ ..
nb: on a quick try, I also didn't get any signal using the sound card from the pin driven by Espruino "analogWrite" command, even driving it repeatedly ( maybe it needs some amplification ? )
ps: I'm doing stuff on a macbookpro 2K11 ( don't know yet if it means some specific stuff on audio input/output ( .. ) )
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
all right, if anyone needs more or less the same hack for servo.move, I have the following code ( not yet tested but worked flawlessly within a browser ;p )
I'll test the above as soon as I can, although not that efficient, it should hopefully do the trick :)
On the "web side of things", still no success using the attached code ( "kinda" works .. sometimes .. for some values .. so actually more random that working ;p ), and the tests I could run using Audacity to generate tones were no that concluant :/ ..
nb: on a quick try, I also didn't get any signal using the sound card from the pin driven by Espruino "analogWrite" command, even driving it repeatedly ( maybe it needs some amplification ? )
ps: I'm doing stuff on a macbookpro 2K11 ( don't know yet if it means some specific stuff on audio input/output ( .. ) )
1 Attachment