Skip to content

Commit 7b01b41

Browse files
author
stsdc
committed
Merge branch 'send-data-in-intervals'
2 parents dcc09db + f467c76 commit 7b01b41

File tree

2 files changed

+37
-23
lines changed

2 files changed

+37
-23
lines changed

client/src/js/actions/index.js

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,29 +39,43 @@ const actions = {
3939
joystick: ({el, motors}) => {
4040

4141
let manager = nipplejs.create({
42-
zone: el,
43-
mode: 'static',
44-
position: {left: '50%', top: '50%'},
45-
size: el.clientHeight,
46-
// dataOnly: true
42+
zone: el,
43+
mode: 'static',
44+
position: {left: '50%', top: '50%'},
45+
size: el.clientHeight,
46+
// dataOnly: true
4747
});
48+
49+
var force = 0;
50+
var angle = "up";
51+
var interval;
4852

4953
manager.on('start', function (evt, nipple) {
50-
console.log(evt);
51-
nipple.on('move', (evt, data) => motorsThrottled(evt, data));
54+
// console.log(evt);
55+
nipple.on('move', (evt, data) => motorsThrottled(evt, data, force, angle));
56+
57+
interval = setInterval(function() {
58+
motors.set(force, angle);
59+
}, 100);
60+
61+
62+
5263
});
5364

5465
let motorsThrottled = throttle((evt, data) => {
5566
if (!data.hasOwnProperty('direction')) { return; }
56-
motors.set(treshold(data.force), convertToArrOfDirections(data.direction.angle));
57-
console.log(treshold(data.force), convertToArrOfDirections(data.direction.angle));
67+
force = treshold(data.force);
68+
angle = convertToArrOfDirections(data.direction.angle);
69+
console.log('[joystick]', treshold(data.force), convertToArrOfDirections(data.direction.angle));
5870
}, 100, { 'trailing': false });
5971

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

62-
manager.on('end', function(evt, nipple) {
74+
manager.on('end', function(evt, nipple) {
75+
clearInterval(interval);
76+
console.log("[joystick interval]", interval);
77+
6378
console.log(evt);
64-
// nipple.off('start move end dir plain');
6579
motors.stop();
6680
});
6781

client/src/js/core/keyboard.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ export const keyboard = function(motors) {
1919
e.preventRepeat();
2020
console.log("[keyboard] UP");
2121
intervalUp = setInterval(function() {
22-
if (speed <= SPEED_MAX) {
23-
24-
motors.set(speed, motors.direction.forward);
22+
if (speed < SPEED_MAX) {
2523
console.log('[keyboard]', speed);
2624
speed = speed + SPEED_STEP;
2725
}
26+
motors.set(speed, motors.direction.forward);
27+
2828
}, UPDATE_INTERVAL);
2929
}, function(e) {
3030
clearInterval(intervalUp);
@@ -37,12 +37,12 @@ export const keyboard = function(motors) {
3737
e.preventRepeat();
3838
console.log("[keyboard] DOWN");
3939
intervalDown = setInterval(function () {
40-
if (speed <= SPEED_MAX) {
41-
42-
motors.set(speed, motors.direction.backward);
40+
if (speed < SPEED_MAX) {
4341
console.log('[keyboard]', speed);
4442
speed = speed + SPEED_STEP;
4543
}
44+
motors.set(speed, motors.direction.backward);
45+
4646
}, UPDATE_INTERVAL);
4747

4848
}, function (e) {
@@ -56,12 +56,12 @@ export const keyboard = function(motors) {
5656
e.preventRepeat();
5757
console.log("[keyboard] LEFT");
5858
intervalLeft = setInterval(function () {
59-
if (speed <= SPEED_MAX) {
60-
61-
motors.set(speed, motors.direction.left);
59+
if (speed < SPEED_MAX) {
6260
console.log('[keyboard]', speed);
6361
speed = speed + SPEED_STEP;
6462
}
63+
motors.set(speed, motors.direction.left);
64+
6565
}, UPDATE_INTERVAL);
6666

6767
}, function (e) {
@@ -75,12 +75,12 @@ export const keyboard = function(motors) {
7575
e.preventRepeat();
7676
console.log("[keyboard] RIGHT");
7777
intervalRight = setInterval(function () {
78-
if (speed <= SPEED_MAX) {
79-
80-
motors.set(speed, motors.direction.right);
78+
if (speed < SPEED_MAX) {
8179
console.log('[keyboard]', speed);
8280
speed = speed + SPEED_STEP;
8381
}
82+
motors.set(speed, motors.direction.right);
83+
8484
}, UPDATE_INTERVAL);
8585

8686
}, function (e) {

0 commit comments

Comments
 (0)