-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.pde
181 lines (169 loc) · 3.97 KB
/
controller.pde
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
import SimpleOpenNI.*;
import processing.opengl.*;
import processing.serial.*;
SimpleOpenNI kinect;
Serial myPort;
// NITE
XnVSessionManager sessionManager;
XnVPointControl pointControl;
// Font for text on screen
PFont font;
String Ctext;
String dir;
// Variables for Hand Detection
boolean handsTrackFlag = false;
PVector screenHandVec = new PVector();
PVector handVec = new PVector();
int t = 0;
PVector centerVec = new PVector();
PVector screenCenterVec = new PVector();
PVector v = new PVector();
boolean automated=true;
boolean serial = true;
int Val01, Val02,value;
float temp01, temp02, temp03, temp04;
void setup() {
// Simple-openni object
kinect = new SimpleOpenNI(this);
kinect.setMirror(true);
// enable depthMap generation, hands + gestures
kinect.enableDepth();
kinect.enableGesture();
kinect.enableHands();
// setup NITE
sessionManager = kinect.createSessionManager("Wave", "RaiseHand");
// Setup NITE.s Hand Point Control
pointControl = new XnVPointControl();
pointControl.RegisterPointCreate(this);
pointControl.RegisterPointDestroy(this);
pointControl.RegisterPointUpdate(this);
// Add it to the session
sessionManager.AddListener(pointControl);
// Set the sketch size to match the depth map
size(kinect.depthWidth(), kinect.depthHeight());
smooth();
// Initialize Font
Ctext="Kinect is live";
dir = "";
//Initialize Serial Communication
if (serial) {
String portName = Serial.list()[0]; // This gets the first port
myPort = new Serial(this, portName, 9600);
}
}
void draw() {
background(0);
PVector centerL = new PVector(width/2, height/2);
// Update Kinect data
kinect.update();
// update NITE
kinect.update(sessionManager);
// draw depthImageMap
image(kinect.depthImage(), 0, 0);
//displacement between the centre and the hand in 2D
v.x = screenHandVec.x-centerL.x;
v.y = screenHandVec.y-centerL.y;
if (handsTrackFlag) {
drawHand();
drawArrow(v, centerL);
controlCar();
}
textDisplay();
if (serial) {
sendSerialData();
}
}
void controlCar() {
temp01 = screenHandVec.x-width/2;
temp02 = height/2- screenHandVec.y;
temp03 = int(map(temp01, -width/2, width/2, 0, 255));
if (temp03 < 100) {
Val01 = 1;
}
if (temp03 >150) {
Val01 = 2;
}
if ((temp03 > 100) && (temp03 < 150)) {
Val01 = 0;
}
temp04= int(map(temp02, -height/2, height/2, -255, 250));
if ((temp04 > 0) && (temp04 > 50)) {
Val02 = 1;
}
else if ((temp04 < 0) && (temp04 < -50)) {
Val02 = 2;
}
else {
Val02 = 0;
}
}
// Draw the hand on screen
void drawHand() {
stroke(255, 0, 0);
pushStyle();
strokeWeight(6);
kinect.convertRealWorldToProjective(handVec, screenHandVec);
point(screenHandVec.x, screenHandVec.y);
popStyle();
}
void drawArrow(PVector v, PVector loc) {
pushMatrix();
float arrowsize = 4;
translate(loc.x, loc.y);
stroke(255, 0, 0);
strokeWeight(2);
rotate(v.heading2D());
float len = v.mag();
line(0, 0, len, 0);
line(len, 0, len-arrowsize, +arrowsize/2);
line(len, 0, len-arrowsize, -arrowsize/2);
popMatrix();
}
void textDisplay() {
fill(255,0,0);
text(Ctext, 10, kinect.depthHeight()-10);
text("Direction: "+dir, 10, kinect.depthHeight()-50);
if ((Val02 == 1) && (Val01 == 0))
{
dir ="Forward";
}
else if ((Val02 == 2) && (Val01 == 0))
{
dir="Reverse";
}
else if ((Val02 == 0) && (Val01 == 1))
{
dir="Left";
}
else if ((Val02 == 0) && (Val01 == 2))
{
dir="Right";
}
else if((Val01 == 0) && (Val02 == 0))
{
dir="Stop";
}
}
void sendSerialData() {
// Serial Communcation
myPort.write('S');
myPort.write(Val01);
myPort.write(Val02);
}
// XnVPointControl callbacks
void onPointCreate(XnVHandPointContext pContext) {
println("onPointCreate:");
handsTrackFlag = true;
handVec.set(pContext.getPtPosition().getX(),
pContext.getPtPosition().getY(),
pContext.getPtPosition().getZ());
}
void onPointDestroy(int nID) {
println("PointDestroy: " + nID);
handsTrackFlag = false;
}
void onPointUpdate(XnVHandPointContext pContext) {
handVec.set(pContext.getPtPosition().getX(),
pContext.getPtPosition().getY(),
pContext.getPtPosition().getZ());
}