-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
111 lines (99 loc) · 2.35 KB
/
Copy pathtest.js
File metadata and controls
111 lines (99 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
var controller = require('http').createServer(handler);
var io = require('socket.io').listen(controller);
var fs = require('fs');
var five = require('johnny-five');
var board = new five.Board()
controller.listen(8080);
function handler (req, res) {
fs.readFile(__dirname + '/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
}
);
console.log('Connection');
}
//set board to ready state to start transfer of data
board.on('ready', function () {
// Motor
var motorGroupRight_1 = new five.Motor({
pins: {
pwm: 3,
dir: 2,
cdir: 4
}
});
var motorGroupRight_2 = new five.Motor({
pins: {
pwm: 9,
dir: 8,
cdir: 10
}
});
function motorDrive(speed, direction)
{
switch(direction)
{
case 'forward':
motorGroupRight_1.forward(speed);
motorGroupRight_2.forward(speed);
console.log('forward_motorDrive');
break;
case 'backward':
motorGroupRight.reverse(170);
console.log('reverse_motorDrive');
break;
// default:
// {
// //stops all motors
// motorGroupRight.stop();
// console.log('stop');
// }
}
}
io.on('connection', function(socket)
{
socket.on('mag', function (data)
{
var turnAMP = data.Results[0];
var speedGamma = data.Results[1];
var pSpeed = speedGamma * -1 * 2.83;
// console.log('gamma' + data.Results[1]);
// console.log('beta' + data.Results[0]);
if(!(turnAMP > 5 || turnAMP < -5))
{
if(speedGamma > 0)
{
motorDrive(pSpeed, 'forward');
}
else if(speedGamma < 0)
{
motorDrive((pSpeed * -1), 'backward');
}
}
else if(turnAMP > 5)
{
var right = pSpeed - (turnAMP * 3.64);
rightTurn(pSpeed, right);
}
else if(turnAMP < -5)
{
var left = pSpeed + (turnAMP * 3.64);
leftTurn(pSpeed, left);
}
else
{
motorDrive(pSpeed, 'forward');
}
});
socket.on('autoDrive', function (data)
{
motorDrive(255, 'forward');
console.log('forward_func');
});
});
})