Skip to content

Commit

Permalink
Merge branch 'send-data-in-intervals'
Browse files Browse the repository at this point in the history
  • Loading branch information
stsdc committed Aug 29, 2018
2 parents dcc09db + f467c76 commit 7b01b41
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 23 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
24 changes: 12 additions & 12 deletions client/src/js/core/keyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down

0 comments on commit 7b01b41

Please sign in to comment.