-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
278 lines (238 loc) · 7.51 KB
/
index.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
PWM's
test servo:
zero 1485 - 1492
base zero 1465 1456(no sing) 1451
shoulder limit 630 - 2170
elbow limit 954 - 2319
wrist limit 500 - 2500 ?
gripper limit 743 - 2233
Pot Readings
base 0-213 0-255
shoulder 22-209 0-121
elbow 120 -255 0-190
wrist 25 - 234 34-214
wrist rotate 20-230 23-225
gripper 0-255
*/
var serialport = require('serialport');
var SerialPort = serialport.SerialPort;
var keypress = require('keypress')
, tty = require('tty');
//var WebSocketServer = require('ws').Server;
function Joint (pot, servo,minByte,maxByte, minPWM, maxPWM){
this.pot = pot;
this.servo = servo;
this.minByte = minByte || 0;
this.maxByte = maxByte || 255;
this.minPWM = minPWM || 500;
this.maxPWM = maxPWM || 2500;
this.mapRange = function (curByte){
curByte = Math.max(curByte,this.minByte);
curByte = Math.min(curByte,this.maxByte);
return this.minPWM + (curByte-this.minByte) * ((this.maxPWM-this.minPWM)/(this.maxByte-this.minByte));
};
}
//function (curByte){
// curByte = Math.max(curByte,this.minByte);
// curByte = Math.min(curByte,this.maxByte);
// return this.minPWM + (curByte-this.minByte) * ((this.maxPWM-this.minPWM)/(this.maxByte-this.minByte));
//(function(minByte,maxByte,minPWM,maxPWM){
// var memo = {};
//
// function f(n){
// var value;
//
// if(n in memo){
// value = memo[n];
// } else {
// value = minPWM + (n-minByte) * ((maxPWM-minPWM)/(maxByte-minByte));
// memo[n] = value;
// }
//
// return value;
// }
//
// return f;
//})();
//var base = new Joint("VH",0);
//var shoulder = new Joint("VG",1,0,139,1990,650);
//var elbow = new Joint("VF",2,0,228,500,2500);
//var wrist = new Joint("VE",3,0,195,500,2500);
//var wrist_rotate = new Joint("VD",4,0,255,500,2500);
//var gripper = new Joint("VC",5,0,255,868,2500);
var base = new Joint("VH",0);
var shoulder = new Joint("VG",1,0,121,2000,500);
var elbow = new Joint("VF",2,0,190,1000,2500);
var wrist = new Joint("VE",3,34,214,500,2500);
var wrist_rotate = new Joint("VD",4,23,225,500,2500);
var gripper = new Joint("VC",5,0,255,868,2500);
var joints = [base,shoulder,elbow,wrist,wrist_rotate];
//var joints = [base,shoulder,elbow,wrist];
var potsString = joints.reduce(function(prev,cur){return prev + cur.pot + " "},"") + " \r";
console.log("potsString : " + potsString);
var clockID;
var zeroPWM = 1500;
var curPot = 0;
var oldContPotVal = 1500;
var moveRight = false;
var moveLeft = false;
var commandString = "";
var toSend = 1459;
var coeffSpinny = 8;
var blocked = false;
var ssc32u = new SerialPort("/dev/cu.usbserial-A5044F99", {
baudrate: 9600
});
var logPorts = function(){
console.log('Logging ports: ')
serialport.list(function(err,ports){
ports.forEach(function(singlePort){
console.log(singlePort.comName);
})
});
};
var showPortOpen = function() {
ssc32u.on('data', receiveSerialData);
ssc32u.on('close', showPortClose);
ssc32u.on('error', showError);
ssc32u.write("#0P1459");
// clockID = setInterval(function(){
// if(!blocked)
// {
// ssc32u.write("VH VG VF \r", function(){
// ssc32u.drain(function(){
// blocked = false;
// })
// });
// blocked = true;
// }
//
// },1000); try testing this out with higher write speed
clockID = setInterval(function(){ssc32u.write(potsString)}, 70);
}
var receiveSerialData = function(buff) {
for(var i = 0; i < buff.length; i++){
var val = buff.readUInt8(i);
// broadcast(JSON.stringify({servo: i, reading: val}));
// console.log("Pot : " + pots[curPot] + ", Reading : " + val);
console.log(joints[curPot].pot + " : " + val);
// console.log("PWM for" + joints[curPot].pot + " = " + joints[curPot].mapRange(val));
if(joints[curPot] !== null){
if(joints[curPot].servo == 0){
toSend = val-oldContPotVal;
oldContPotVal = val;
var toSend = Math.round(1459 + (toSend * coeffSpinny));
toSend = Math.min(toSend,2500);
toSend = Math.max(toSend,500);
commandString += "#0P" + toSend;
//moveContServo(val);
} else{
commandString += "#" + joints[curPot].servo + "P" + ~~joints[curPot].mapRange(val);
//moveServo(joints[curPot].servo, joints[curPot].mapRange(val));
}
}
curPot++;
if(curPot == joints.length){
//console.log(commandString);
ssc32u.write(commandString + "\r")
curPot = 0;
commandString = "";
}
}
};
var showPortClose = function() {
//console.log('port closed.');
clearInterval(clockID);
};
var showError = function(error) {
//console.log('Serial port error: ' + error);
};
var moveServo = function(sevoNum, pwm) {
if(typeof pwm !== 'number')
return;
data = Math.min(pwm,2500);
data = Math.max(pwm,500);
//console.log("sending to serial: " + pwm);
if(!!ssc32u){
// console.log("current PWM: " + pwm);
ssc32u.write("#" + sevoNum + "P" + pwm + "\r");
}
};
var moveContServo = function(curContPotVal){
var deltaPot = curContPotVal-oldContPotVal;
oldContPotVal = curContPotVal;
//console.log("deltaPot : " + deltaPot);
var toSend = 1459 + (deltaPot * coeffSpinny);
//console.log('coeffSpinny = ' + coeffSpinny);
toSend = Math.min(toSend,2500);
toSend = Math.max(toSend,500);
//console.log('toSend: ' + toSend);
if(moveLeft){toSend = 1400;}
if(moveRight){toSend = 1540;}
ssc32u.write("#0P" + toSend + "\r");
}
ssc32u.on('open', showPortOpen);
// make `process.stdin` begin emitting "keypress" events
keypress(process.stdin);
// listen for the "keypress" event
process.stdin.on('keypress', function (ch, key) {
//console.log('got "keypress"', key);
if (key && key.name === "up") {
zeroPWM++;
coeffSpinny++;
} else if(key && key.name === "down"){
zeroPWM--;
moveLeft = false;
moveRight = false;
coeffSpinny--;
} else if(key && key.name === "left"){
moveLeft = true;
moveRight = false;
}else if(key && key.name === "right"){
moveLeft = false;
moveRight = true;
}
if (key && key.ctrl && key.name == 'c') {
process.exit();
}
//process.stdout.write( ch );
});
if (typeof process.stdin.setRawMode == 'function') {
process.stdin.setRawMode(true);
} else {
tty.setRawMode(true);
}
process.stdin.resume();
//
// var SERVER_PORT = 8081;
// var wss = new WebSocketServer({port: SERVER_PORT});
// var connections = new Array;
//
// var broadcast = function (data) {
// for (myConnection in connections) { // iterate over the array of connections
// connections[myConnection].send(data); // send the data to each connection
// }
// }
//
//
// var handleConnection = function(client) {
// ////console.log("New Connection"); // you have a new client
// connections.push(client); // add this client to the connections array
//
// client.on('message', handleMessage); // when a client sends a message,
//
// client.on('close', function() { // when a client closes its connection
// //console.log("connection closed");// print it out
// ssc32u.write("#0P1500S500T1500\r");
// var position = connections.indexOf(client); // get the client's position in the array
// connections.splice(position, 1); // and delete it from the array
// });
// }
//
// var handleMessage = function(data) {
// //console.log('handling message from client');
// readPot();
// };
//
// wss.on('connection', handleConnection);