-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
179 lines (141 loc) · 5.21 KB
/
sketch.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
// Neill Kaipo Shikada
// ATLAS Institute
// University of Colorado Boulder
// reference: https://itp.nyu.edu/physcomp/labs/labs-serial-communication/lab-serial-output-from-p5-js/
import Mediapipe from "./MediapipeHands.js";
import { scalePoints, calculateDistance } from "./functions.js";
// enable camera
const videoElement = document.getElementsByClassName('input_video')[0];
let width = 1280;
let height = 720;
let mp = new Mediapipe();
// place to put info about points on hand
let indexPoints = [];
let thumbPoints = [];
let wristPoint = [];
// angle that is exported to the arduino for the servo
let degrees;
// everything the serial controller needs
var serial;
var portName = '0COM3';
var inData;
var outByte = 0;
// --------------------------------------------------- //
// ---------------------P5 Setup---------------------- //
// --------------------------------------------------- //
// --------------------------------------------------- //
window.setup = function() {
createCanvas(windowWidth, windowHeight);
serial = new p5.SerialPort();
serial.on('data', serialEvent);
serial.on('error', serialError);
serial.on('list', printList);
serial.list();
serial.open(portName);
// color of the hand
mp.color = [255, 255, 255];
}
// --------------------------------------------------- //
// -----------------------P5 Draw--------------------- //
// --------------------------------------------------- //
// --------------------------------------------------- //
window.draw = function() {
// somehow make a way to find the maximum distance between thumb tip and index tip?
// takes the points from mediapipe, and scales them to the canvas
for (point in mp.index) {
indexPoints.push(scalePoints(mp.index[point]));
thumbPoints.push(scalePoints(mp.thumb[point]));
wristPoint.push(scalePoints(mp.wrist[point]));
}
background(50);
fill(255);
// display the data coming back from the arduino to make sure it's the same as when it was sent
text("incoming value: " + inData, 30, 50);
// Actions to do if the hand is detected
if (indexPoints[0] != null) {
// get the distance between the index and thumb
let distance = calculateDistance(indexPoints[0], thumbPoints[0]);
// scales the distance between 0 and 180 #TODO
let angle = int(map(indexPoints[0].y, 0, windowHeight, 0, 180));
// #TODO want degrees to have multiple angles for multiple motors
degrees = angle;
// make sure the angle stays within bounds (0< x <180)
console.log(degrees);
// if (angle <= 180) {
// degrees = angle;
// } else if (angle > 180){
// degrees = 180;
// } else {
// degrees = 0;
// }
}
// create an array of hand points that all have corresponding places on that array (i.e. indexPoint = [0], thumbPoint = [1], etc.)
// reference that array in the arduino code
// calculate the distance between thum tip and index tip for opening and closing the claw (servo 5)
// calculate angle of a couple hand points to control the angle of the arm (adjusts servo 2 and 3)
// calculate the direction of the hand (somehow) to control the rotation of the shoulder (servo 1)
// What controls servo 4? (the wrist)
// Array (all values scaled to 0 to 180)
// [0] = shoulder;
// [1] = arm; angle of two points on the palm
// [2] = wrist;
// [3] = claw; distance between indextip and thumbtip
// actually sends the data to the arduino
updateAngle();
// resets the hand points every time
indexPoints = [];
thumbPoints = [];
wristPoint = [];
}
// --------------------------------------------------- //
// --------------------MediaPipe---------------------- //
// --------------------------------------------------- //
// --------------------------------------------------- //
mp.runMediapipe();
const hands = new Hands({locateFile: (file) => {
return `https://cdn.jsdelivr.net/npm/@mediapipe/hands/${file}`;
}});
hands.setOptions({
maxNumHands: 1,
modelComplexity: 1,
minDetectionConfidence: 0.6,
minTrackingConfidence: 0.7
});
hands.onResults(onResults);
const camera = new Camera(videoElement, {
onFrame: async () => {
await hands.send({image: videoElement});
},
width: 1280,
height: 720
});
camera.start();
// --------------------------------------------------- //
// -----------------Serial Functions------------------ //
// --------------------------------------------------- //
// --------------------------------------------------- //
window.serialEvent = function() {
var inByte = serial.read();
inData = inByte;
}
window.serialError = function(err) {
print('Something went wrong with the serial port. ' + err);
}
window.updateAngle = function() {
outByte = degrees;
serial.write(outByte);
}
window.printList = function(portList) {
// portList is an array of serial port names
for (var i = 0; i < portList.length; i++) {
// Display the list the console:
console.log(i + portList[i]);
}
}
// --------------------------------------------------- //
// ------------------Other Functions------------------ //
// --------------------------------------------------- //
// --------------------------------------------------- //
window.setLineDash = function(list) {
drawingContext.setLineDash(list);
}