Skip to content

Commit

Permalink
send data in intervals via joystick
Browse files Browse the repository at this point in the history
  • Loading branch information
stsdc committed Aug 29, 2018
1 parent a1d844d commit f467c76
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions client/src/js/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,29 +39,43 @@ const actions = {
joystick: ({el, motors}) => {

let manager = nipplejs.create({
zone: el,
mode: 'static',
position: {left: '50%', top: '50%'},
size: el.clientHeight,
// dataOnly: true
zone: el,
mode: 'static',
position: {left: '50%', top: '50%'},
size: el.clientHeight,
// dataOnly: true
});

var force = 0;
var angle = "up";
var interval;

manager.on('start', function (evt, nipple) {
console.log(evt);
nipple.on('move', (evt, data) => motorsThrottled(evt, data));
// console.log(evt);
nipple.on('move', (evt, data) => motorsThrottled(evt, data, force, angle));

interval = setInterval(function() {
motors.set(force, angle);
}, 100);



});

let motorsThrottled = throttle((evt, data) => {
if (!data.hasOwnProperty('direction')) { return; }
motors.set(treshold(data.force), convertToArrOfDirections(data.direction.angle));
console.log(treshold(data.force), convertToArrOfDirections(data.direction.angle));
force = treshold(data.force);
angle = convertToArrOfDirections(data.direction.angle);
console.log('[joystick]', treshold(data.force), convertToArrOfDirections(data.direction.angle));
}, 100, { 'trailing': false });

let treshold = (force) => force >= 1 ? 100 : (force * 100).toFixed(0);

manager.on('end', function(evt, nipple) {
manager.on('end', function(evt, nipple) {
clearInterval(interval);
console.log("[joystick interval]", interval);

console.log(evt);
// nipple.off('start move end dir plain');
motors.stop();
});

Expand Down

0 comments on commit f467c76

Please sign in to comment.