-
Notifications
You must be signed in to change notification settings - Fork 2
/
lookAtPositions.js
74 lines (54 loc) · 1.79 KB
/
lookAtPositions.js
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
const Prompt = require('prompt');
const Cylon = require('cylon');
const util = require('util');
const EventEmitter = require('events')
// Getting input stuff
Prompt.start();
// Cylon Stuff
Cylon.robot({
name: 'servoBot',
connections: {
arduino: { adaptor: 'firmata', port: '/dev/cu.usbmodem1411' } // /dev/cu.usbmodem1411 /dev/cu.usbmodem1451
},
devices: {
servo3: { driver: 'servo', pin: 3 },
servo13: { driver: 'servo', pin: 13 }
},
work: function(my) {
my.servo3.angle(90);
my.servo13.angle(45);
my.emitter = new MyEmitter();
my.emitter.on('pitch', function(pitch) {
console.log(' Moving servo 13 to', pitch);
my.servo13.angle(my.servo13.safeAngle(( (180/Math.PI) * pitch )));
});
my.emitter.on('yaw', function(yaw) {
console.log(' Moving servo 3 to', yaw);
my.servo3.angle(my.servo3.safeAngle(( (180/Math.PI) * yaw )));
});
my.emitter.on('newPosition', (x,y,z) => {
my.emitter.emit('pitch', pitch(x, y, z));
my.emitter.emit('yaw', yaw(x, y));
});
Cylon.devices = my;
console.log("Servo Motors are active");
Prompt.get(['x', 'y', 'z'], function (err, result) {
if(err) {return onErr(err); }
console.log('Command-line input received:');
console.log(' X:', x);
console.log(' Y:', y);
console.log(' Z:', z);
Cylon.robots.servoBot.emitter.emit('newPosition', x, y, z);
});
}
}).start();
function yaw(x, y) {
return Math.atan(y/x);
}
function pitch(x, y, z) {
return Math.atan((Math.sqrt(x*x + y*y) / z));
}
function MyEmitter() {
EventEmitter.call(this);
}
util.inherits(MyEmitter, EventEmitter);