-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKinectGrabber.h
143 lines (119 loc) · 3.78 KB
/
KinectGrabber.h
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
#ifndef KinectGrabber_h
#define KinectGrabber_h
#define KINECT_HORIZONTAL_APERTURE DEG2RAD(57)
#define KINECT_DEPTH_STD_ERROR 0.01
#define KINECT_MAX_DEPTH 10 // meters
#include <mrpt/synch/CThreadSafeVariable.h>
#include <mrpt/hwdrivers/CKinect.h>
using namespace mrpt;
using namespace mrpt::hwdrivers;
using namespace mrpt::opengl;
using namespace mrpt::system;
using namespace std;
struct TThreadParam
{
TThreadParam() : quit(false), pushed_key(0) { }
volatile bool quit;
volatile int pushed_key;
mrpt::synch::CThreadSafeVariable<CObservation3DRangeScanPtr> new_obs; // RGB+D (+3D points)
};
void thread_grabbing(TThreadParam &p)
{
try
{
CKinect kinect;
const std::string cfgFile = "kinect_calib.cfg";
if (mrpt::system::fileExists(cfgFile))
{
cout << "Loading calibration from: "<< cfgFile << endl;
kinect.loadConfig(mrpt::utils::CConfigFile(cfgFile), "KINECT");
}
else cerr << "Warning: Calibration file ["<< cfgFile <<"] not found -> Using default params.\n";
// Open:
cout << "Calling CKinect::initialize()...";
kinect.initialize();
cout << "OK\n";
bool there_is_obs=true, hard_error=false;
while (!hard_error && !p.quit)
{
// Grab new observation from the camera:
CObservation3DRangeScanPtr obs = CObservation3DRangeScan::Create(); // Smart pointers to observations
CObservationIMUPtr obs_imu = CObservationIMU::Create();
kinect.getNextObservation(*obs,*obs_imu,there_is_obs,hard_error);
if (!hard_error && there_is_obs)
{
p.new_obs.set(obs);
}
if (p.pushed_key!=0)
{
switch (p.pushed_key)
{
case 27:
p.quit = true;
break;
}
// Clear pushed key flag:
p.pushed_key = 0;
}
}
}
catch(std::exception &e)
{
cout << "Exception in Kinect thread: " << e.what() << endl;
p.quit = true;
}
}
class KinectGrabber
{
public:
KinectGrabber(void);
bool init();
void grab(CObservation2DRangeScanPtr the_scan);
private:
TThreadParam thrPar;
mrpt::system::TThreadHandle thHandle;//= mrpt::system::createThreadRef(thread_grabbing ,thrPar);
mrpt::opengl::CFrustumPtr gl_frustum;// = mrpt::opengl::CFrustum::Create(0.2f, 5.0f, 90.0f, 5.0f, 2.0f, true, true );
CObservation3DRangeScanPtr last_obs;
};
KinectGrabber::KinectGrabber(void)
{
thHandle = mrpt::system::createThreadRef(thread_grabbing ,thrPar);
gl_frustum = mrpt::opengl::CFrustum::Create(0.2f, 5.0f, 90.0f, 5.0f, 2.0f, true, true );
}
bool KinectGrabber::init()
{
cout << "Waiting for sensor initialization...\n";
do {
CObservation3DRangeScanPtr possiblyNewObs = thrPar.new_obs.get();
if (possiblyNewObs && possiblyNewObs->timestamp!=INVALID_TIMESTAMP)
return true;
else mrpt::system::sleep(10);
} while (!thrPar.quit);
// Check error condition:
if (thrPar.quit) return false;
}
void KinectGrabber::grab(CObservation2DRangeScanPtr the_scan)
{
CObservation3DRangeScanPtr possiblyNewObs = thrPar.new_obs.get();
//CObservation2DRangeScanPtr the_scan = CObservation2DRangeScan::Create();
if (possiblyNewObs && possiblyNewObs->timestamp!=INVALID_TIMESTAMP &&
(!last_obs || possiblyNewObs->timestamp!=last_obs->timestamp))
{
// It IS a new observation:
last_obs = possiblyNewObs;
// Convert ranges to an equivalent 2D "fake laser" scan:
if (last_obs->hasRangeImage)
{
// Convert to scan:
//the_scan = CObservation2DRangeScan::Create();
const float vert_FOV = DEG2RAD(gl_frustum->getVertFOV());
last_obs->convertTo2DScan(*the_scan, "KINECT_2D_SCAN", .5f*vert_FOV, .5f*vert_FOV );
the_scan->sensorLabel = "LASER_SIM";
the_scan->sensorPose.setFromValues(0.0,0,0.0);
the_scan->maxRange = KINECT_MAX_DEPTH;
the_scan->aperture = KINECT_HORIZONTAL_APERTURE;
the_scan->stdError = KINECT_DEPTH_STD_ERROR;
}
}
}
#endif