diff --git a/client/src/js/actions/index.js b/client/src/js/actions/index.js index 1e96935..98fa7c6 100644 --- a/client/src/js/actions/index.js +++ b/client/src/js/actions/index.js @@ -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(); }); diff --git a/client/src/js/core/keyboard.js b/client/src/js/core/keyboard.js index eed26ed..ef20988 100644 --- a/client/src/js/core/keyboard.js +++ b/client/src/js/core/keyboard.js @@ -19,12 +19,12 @@ export const keyboard = function(motors) { e.preventRepeat(); console.log("[keyboard] UP"); intervalUp = setInterval(function() { - if (speed <= SPEED_MAX) { - - motors.set(speed, motors.direction.forward); + if (speed < SPEED_MAX) { console.log('[keyboard]', speed); speed = speed + SPEED_STEP; } + motors.set(speed, motors.direction.forward); + }, UPDATE_INTERVAL); }, function(e) { clearInterval(intervalUp); @@ -37,12 +37,12 @@ export const keyboard = function(motors) { e.preventRepeat(); console.log("[keyboard] DOWN"); intervalDown = setInterval(function () { - if (speed <= SPEED_MAX) { - - motors.set(speed, motors.direction.backward); + if (speed < SPEED_MAX) { console.log('[keyboard]', speed); speed = speed + SPEED_STEP; } + motors.set(speed, motors.direction.backward); + }, UPDATE_INTERVAL); }, function (e) { @@ -56,12 +56,12 @@ export const keyboard = function(motors) { e.preventRepeat(); console.log("[keyboard] LEFT"); intervalLeft = setInterval(function () { - if (speed <= SPEED_MAX) { - - motors.set(speed, motors.direction.left); + if (speed < SPEED_MAX) { console.log('[keyboard]', speed); speed = speed + SPEED_STEP; } + motors.set(speed, motors.direction.left); + }, UPDATE_INTERVAL); }, function (e) { @@ -75,12 +75,12 @@ export const keyboard = function(motors) { e.preventRepeat(); console.log("[keyboard] RIGHT"); intervalRight = setInterval(function () { - if (speed <= SPEED_MAX) { - - motors.set(speed, motors.direction.right); + if (speed < SPEED_MAX) { console.log('[keyboard]', speed); speed = speed + SPEED_STEP; } + motors.set(speed, motors.direction.right); + }, UPDATE_INTERVAL); }, function (e) {