/
robotarm-automation.js
177 lines (165 loc) · 4.39 KB
/
robotarm-automation.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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
var _ = require('lodash');
var five = require('johnny-five');
var RobotArm = require('../');
// JohnnyFive
// https://github.com/rwaldron/johnny-five/wiki/Board#parameters
var board = new five.Board({
repl: true
});
var createRobotArm = function() {
return new RobotArm({
axis: {
pivot: new five.Servo({
pin: 3,
center: true,
range: [10, 170]
}),
stand: new five.Servo({
pin: 5,
center: true,
range: [10, 170]
}),
shoulder: new five.Servo({
pin: 6,
center: true,
range: [10, 170]
}),
elbow: new five.Servo({
pin: 9,
center: true,
range: [10, 170]
}),
wrist: new five.Servo({
pin: 10,
center: true,
range: [0, 180]
}),
claw: new five.Servo({
pin: 11,
center: true,
range: [135, 175]
})
}
});
};
board.on('ready', function() {
var robotarm = createRobotArm();
// Move each servo to the center point.
var center = function() {
var args = Array.prototype.slice.call(arguments);
_.each(robotarm.axis, function(axis) {
axis.center.apply(axis, args);
});
};
// Move each servo to a specific position.
var to = function() {
var args = Array.prototype.slice.call(arguments);
_.each(robotarm.axis, function(axis) {
axis.to.apply(axis, args);
});
};
this.repl.inject({
robotarm: robotarm,
axis: robotarm.axis,
center: center,
to: to
});
{ // Toggle play/stop
var button = new five.Button({
pin: 2,
isPullup: true
});
button.on('down', function(value) {
if (robotarm.isRunning()) {
robotarm.stop();
center(1000); // Go back to center in 1000ms
} else {
robotarm.play();
}
});
}
var motion = {
openClaw: {
claw: 'min',
},
closeClaw: {
claw: 'max'
},
center: {
pivot: 'center',
stand: 'center',
shoulder: 'center',
elbow: 'center',
wrist: 'center'
},
turnRightDown: {
pivot: 50.00,
stand: 151.50,
shoulder: 58.00,
elbow: 96.00,
wrist: 90.00
},
turnLeftDown: {
pivot: 122.00,
stand: 151.50,
shoulder: 58.00,
elbow: 96.00,
wrist: 90.00
}
};
var go = function(m, delay) {
_.each(m, function(value, axis) {
if ( ! _.isNumber(value)) {
var method = value;
robotarm.axis[axis][method](delay);
return;
}
robotarm.axis[axis].to(value, delay);
});
};
robotarm
// turn right down and open the claw
.then(function(next) {
go(motion.turnRightDown, 2000);
go(motion.openClaw, 2000);
setTimeout(next, 2500);
})
// close the claw
.then(function(next) {
go(motion.closeClaw, 1500);
setTimeout(next, 2000);
})
// center all except the claw
.then(function(next) {
go(motion.center, 1500);
setTimeout(next, 2000);
})
// turn left down
.then(function(next) {
go(motion.turnLeftDown, 2000);
setTimeout(next, 2500);
})
// open the claw
.then(function(next) {
go(motion.openClaw, 1500);
setTimeout(next, 2000);
})
// close the claw
.then(function(next) {
go(motion.closeClaw, 1500);
setTimeout(next, 2000);
})
// center all except the claw
.then(function(next) {
go(motion.center, 1500);
setTimeout(next, 2000);
})
// turn right down
.then(function(next) {
go(motion.turnRightDown, 2000);
setTimeout(next, 2500);
});
robotarm.play({
loop: true
});
});