-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeapDevice.cpp
126 lines (99 loc) · 2.53 KB
/
LeapDevice.cpp
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
#include "StdAfx.h"
#include "LeapDevice.h"
#pragma region LeapDevice
double MyListener::AMEND_SPEED = 1;
LeapDevice::LeapDevice(void)
{
}
LeapDevice::LeapDevice(ArmInfo *arm)
{
mListener.setArm(arm);
}
LeapDevice::~LeapDevice(void)
{
}
void LeapDevice::start()
{
mController.addListener(mListener);
}
void LeapDevice::stop()
{
mController.removeListener(mListener);
}
#pragma endregion
#pragma region MyListener
MyListener::MyListener()
{
id = 0;
}
void MyListener::setArm(ArmInfo* arm)
{
mArm = arm;
}
void MyListener::onConnect(const Controller& controller)
{
cout<<"Leap motion connected"<<endl;
}
Vector MyListener::jointPosition(const Frame& frame)
{
HandList hands = frame.hands();
for (HandList::const_iterator h = hands.begin(); h != hands.end(); h++)
{
const Hand hand = *h;
FingerList fingers = hand.fingers();
for (FingerList::const_iterator f = fingers.begin(); f != fingers.end(); f++)
{
const Finger finger = *f;
if (finger.type() == Finger::Type::TYPE_MIDDLE)
{
Vector pos = (*f).bone(Bone::Type::TYPE_METACARPAL).nextJoint() / 10; // 中指掌指关节
pos.z -= 14;
return pos;
break;
}
}
}
return Vector::zero();
}
void MyListener::onFrame(const Controller& controller)
{
const Frame frame = controller.frame();
if (frame.hands().count() == 0) return;
Vector normal, pos;
double roll;
double grabStrength;
double speed;
const Hand hand = *(frame.hands().begin());
//pos = jointPosition(frame); // 抓取器采用关节位置
pos = hand.palmPosition() / 10; // 抓取器采用掌心位置
if (pos == Vector::zero()) return;
normal = hand.palmNormal();
roll = normal.roll() + PI / 2;
grabStrength = hand.grabStrength();
speed = (hand.palmVelocity()/10).magnitude() * AMEND_SPEED;
if (speed > 45) return;
#ifdef OUTPUT
printf("Start\n");
printf("ID: %04lld\n", id++);
printf("Hand palm position: %.2f, %.2f, %.2f\n", pos.x, pos.y, pos.z);
printf("Roll: %.4lf\n", roll);
printf("Palm direction: %03d\n", (int) (roll / PI * 180));
printf("Grab strength: %.2f\n", grabStrength);
printf("Speed: %.2f\n", speed);
#endif
#ifdef RECORD
recordData(pos);
#endif
/********test*********/
//mArm->updateEndPoint(Vector(-13.767, 14.508, -6.99));
/*********************/
//bool isDisplaced = mArm->updateEndPoint(Vector(x, y, z)) + mArm->updateGrabber(roll, grabStrength);
mArm->updateSpeed(speed * AMEND_SPEED);
bool isDisplaced = mArm->updateEndPoint(pos) + mArm->updateGrabber(roll, grabStrength);
#ifdef OUTPUT
if (!isDisplaced)
printf("No displacement\n");
#endif
Sleep(10);
}
#pragma endregion