-
Notifications
You must be signed in to change notification settings - Fork 0
/
irisTest.js
222 lines (144 loc) · 3.79 KB
/
irisTest.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
var SP = require("serialport");
SerialPort = SP.SerialPort;
var serialPort = new SerialPort("/dev/tty.usbserial-A6031W7I", {
baudrate: 9600
}, false);
var rgbPort = new SerialPort("/dev/tty.usbmodemfa131", {
baudrate: 9600
}, false);
rgbPort.open(function (error) {
if ( error ) {
console.log('failed to open: ' + error);
} else {
console.log('open rgb');
}
});
var debug = true;
serialPort.open(function (error) {
if ( error ) {
console.log('failed to open: ' + error);
} else {
console.log('open uArm');
var header = [0xFF, 0xAA];
serialPort.on('data', function(data) {
console.log('data received: ', data.toString());
});
serialPort.on('close', function(data) {
console.log('CLOSED: ' + data);
});
var rgbOutBlack = [
0,0,0,
0x00
];
var speed = .2;//.75;
var x = 0;
var y = 0;
var wrist = 0;
var dWrist = -1;
var wristMax = 23;
var wristMin = -36;
var armRadius = 120; //(mm)
var toPolar = function(x, y)
{
y += armRadius;
var theta1 = Math.atan2(y, x);
if (x == 0 && y == armRadius)
{
theta1 = Math.PI/2;
}
var offsetSin = Math.sin(Math.PI/2 - theta1);
var offsetCos = Math.cos(Math.PI/2 - theta1);
var offsetX = armRadius * offsetSin;
var offsetY = armRadius * offsetCos;
if (debug)
{
console.log('offsetX', offsetX);
console.log('offsetY', offsetY);
}
x -= offsetX;
y -= offsetY;
if (debug)
{
console.log('debug x', x);
console.log('debug y', y);
}
var r = Math.sqrt(x*x + y*y);
var theta = Math.atan2(y, x);
if (x == 0 && y == 0)
{
theta = Math.PI/2;
}
return {
theta: theta,
r: r
}
};
var step = function()
{
wrist += dWrist
if (wrist >= wristMax)
{
dWrist = -1;
}
else if (wrist <= wristMin)
{
dWrist = +1;
}
// var polarX = toPolar( Math.cos(x)*r1, Math.sin(y)*r1 );
// var polarY = toPolar( Math.cos(y)*r2 + Math.cos(y)*Math.cos(x)*r1, 120 + Math.sin(y)*r2 + Math.sin(y)*Math.cos(x)*r1 );
var rotation = 0; //Math.floor(-x*.28) + Math.floor(w*.28/2);//Math.floor(0 + Math.sin(x)*r);//Math.floor(180 * Math.random() - 90);
var stretch = 80;//-x + 50 + w;//Math.floor(210 * Math.random());
var height = 100; //Math.floor((-y + 5 + h) + (x/2)%2 * 6 - 3 ) ;
var wrist_rotation = wrist;//Math.floor(180 * Math.random() - 90);
var grip = 0;
//+ ' ' + 0xFF + '' + 0xAA
//var out = rotation + ' ' + stretch + ' ' + height + ' ' + wrist_rotation + ' ' + grip + '\n';
if (debug)
{
// console.log('polarY.theta', polarY.theta);
console.log('rotation:', rotation);
console.log(' stretch:', stretch);
console.log(' height:', height);
console.log(' wrist:', wrist);
}
var r = 255;// /80 //Math.floor(Math.random()*255);
var g = 255;// /35 // Math.floor(Math.random()*255);
var b = 255;// /50 // Math.floor(Math.random()*255);
var rgbOut = [
r, g, b,
0x00
];
var out = [
(rotation & 0xFF00)>>8, (rotation & 0x00FF),
(stretch & 0xFF00)>>8, (stretch & 0x00FF),
(height & 0xFF00)>>8, (height & 0x00FF),
(wrist_rotation & 0xFF00)>>8, (wrist_rotation & 0x00FF),
0x00
];
var buff = new Buffer(header.concat(out));
serialPort.write(buff, function(err, results) {
if (err)
{ console.log('err ' + err); }
var rgbBuff = new Buffer(header.concat(rgbOut));
rgbPort.write(rgbBuff, function(err, results) {
// if (debug)
// { console.log('rgb:', r, g, b); }
if (err) { console.log('err ' + err); }
});
/*
setTimeout(function(){
rgbPort.write(new Buffer(header.concat(rgbOutBlack)), function(err, results) {
if (err) { console.log('err ' + err); }
});
}, 5);
*/
});
};
setTimeout(function(){
setInterval(function(){
step();
}, 30);
}, 2000);
}
});
process.stdin.resume();