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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445 | /**
* @file CameraMatrixCorrectorV3.cpp
*
* @author <a href="mailto:kaden@informatik.hu-berlin.de">Steffen Kaden</a>
*/
#include "CameraMatrixCorrectorV3.h"
#include <array>
#include <algorithm>
CameraMatrixCorrectorV3::CameraMatrixCorrectorV3():
theCamMatErrorFunctionV3(getDebugRequest(), getDebugDrawings(), getDebugModify(), getFieldInfo(), getCameraInfo(), getCameraInfoTop()),
gn_minimizer(1, 1.25, 0.005, /*double regularizer,*/ false)
{
getDebugParameterList().add(&getCameraMatrixOffset());
getDebugParameterList().add(&cmc_params);
bounds.lower_bound << cmc_params.lower.body_x,
cmc_params.lower.body_y,
cmc_params.lower.head_x,
cmc_params.lower.head_y,
cmc_params.lower.head_z,
cmc_params.lower.cam_x,
cmc_params.lower.cam_y,
cmc_params.lower.cam_z,
cmc_params.lower.cam_top_x,
cmc_params.lower.cam_top_y,
cmc_params.lower.cam_top_z;
bounds.upper_bound << cmc_params.upper.body_x,
cmc_params.upper.body_y,
cmc_params.upper.head_x,
cmc_params.upper.head_y,
cmc_params.upper.head_z,
cmc_params.upper.cam_x,
cmc_params.upper.cam_y,
cmc_params.upper.cam_z,
cmc_params.upper.cam_top_x,
cmc_params.upper.cam_top_y,
cmc_params.upper.cam_top_z;
if(cmc_params.use_bounded_variable){
theCamMatErrorFunctionV3.bounds = &bounds;
} else {
theCamMatErrorFunctionV3.bounds = nullptr;
}
DEBUG_REQUEST_REGISTER("CameraMatrixV3:automatic_mode","try to do automatic calibration", false);
DEBUG_REQUEST_REGISTER("CameraMatrixV3:manual:collect_calibration_data", "collect the data for calibration", false);
DEBUG_REQUEST_REGISTER("CameraMatrixV3:manual:calibrate","",false);
DEBUG_REQUEST_REGISTER("CameraMatrixV3:manual:clear_calibration_data", "clears the data used for calibration", false);
DEBUG_REQUEST_REGISTER("CameraMatrixV3:manual:reset_calibration", "set the calibration offsets of the CM to 0", false);
DEBUG_REQUEST_REGISTER("CameraMatrixV3:manual:reset_minimizer", "reset lm parameters to initial values", false);
DEBUG_REQUEST_REGISTER("CameraMatrixV3:debug:enable_CamMatErrorFunctionV3_drawings", "needed to be activated for error function drawings", true);
DEBUG_REQUEST_REGISTER("CameraMatrixV3:debug:optimizer:use_GN", "use Gauss-Newton based optimizer during calibration (default)", false);
DEBUG_REQUEST_REGISTER("CameraMatrixV3:debug:optimizer:use_LM", "use Levenberg-Marquardt based optimizer during calibration", false);
DEBUG_REQUEST_REGISTER("CameraMatrixV3:debug:optimizer:use_LM2", "use Levenberg-Marquardt based optimizer (smoothed update) during calibration", true);
DEBUG_REQUEST_REGISTER("CameraMatrixV3:debug:read_calibration_data_from_file", "", false);
DEBUG_REQUEST_REGISTER("CameraMatrixV3:debug:write_calibration_data_to_file", "", false);
theCamMatErrorFunctionV3.global_position = &cmc_params.global_pose.position;
theCamMatErrorFunctionV3.global_orientation = &cmc_params.global_pose.orientation;
read_calibration_data = written_calibration_data = false;
auto_cleared_data = auto_collected = auto_calibrated = false;
play_calibrated = play_calibrating = play_collecting = true;
last_error = 0;
minimizer = &lm2_minimizer;
// sampling coordinates
std::array<double,4> pitchs {-20, -10 , 0, 10};
std::vector<double> yaws;
for (int i = -88; i <= 88; i=i+8) {
yaws.push_back(i);
}
for(double pitch : pitchs)
{
for(double yaw : yaws) {
target_points.push_back(Vector2d(yaw, pitch));
}
// reverse the order for the next iteration
std::reverse(yaws.begin(), yaws.end());
}
current_target = target_points.begin();
readFromRepresentation();
}
CameraMatrixCorrectorV3::~CameraMatrixCorrectorV3()
{
getDebugParameterList().remove(&getCameraMatrixOffset());
getDebugParameterList().remove(&cmc_params);
}
void CameraMatrixCorrectorV3::execute()
{
if(cmc_params.check_changed()){
update_bounds();
}
DEBUG_REQUEST("CameraMatrixV3:debug:read_calibration_data_from_file",
if(!read_calibration_data) {
theCamMatErrorFunctionV3.read_calibration_data_from_file();
read_calibration_data = true;
}
);
DEBUG_REQUEST_ON_DEACTIVE("CameraMatrixV3:debug:read_calibration_data_from_file",
read_calibration_data = false;
);
DEBUG_REQUEST("CameraMatrixV3:debug:write_calibration_data_to_file",
if(!written_calibration_data) {
theCamMatErrorFunctionV3.write_calibration_data_to_file();
written_calibration_data = true;
}
);
DEBUG_REQUEST_ON_DEACTIVE("CameraMatrixV3:debug:write_calibration_data_to_file",
written_calibration_data = false;
);
DEBUG_REQUEST("CameraMatrixV3:debug:optimizer:use_GN",
minimizer = &gn_minimizer;
);
DEBUG_REQUEST("CameraMatrixV3:debug:optimizer:use_LM",
minimizer = &lm_minimizer;
);
DEBUG_REQUEST("CameraMatrixV3:debug:optimizer:use_LM2",
minimizer = &lm2_minimizer;
);
if (getSoundPlayData().soundFile == "finished.wav"
|| getSoundPlayData().soundFile == "collectingdata.wav"
|| getSoundPlayData().soundFile == "calibrating.wav")
{
getSoundPlayData().mute = true;
getSoundPlayData().soundFile = "";
}
// sit down if auto calibrated
if(auto_calibrated){
getMotionRequest().id = motion::sit;
getMotionRequest().disable_relaxed_stand = false;
} else {
getMotionRequest().id = motion::stand;
getMotionRequest().disable_relaxed_stand = true;
}
// enable debug drawings in manual and auto mode
DEBUG_REQUEST("CameraMatrixV3:debug:enable_CamMatErrorFunctionV3_drawings",
theCamMatErrorFunctionV3.plot_CalibrationData(cam_mat_offsets);
);
bool use_automatic_mode = false;
DEBUG_REQUEST("CameraMatrixV3:automatic_mode",
use_automatic_mode = true;
);
DEBUG_REQUEST_ON_DEACTIVE("CameraMatrixV3:automatic_mode",
// Note: wanted behavior because we want to save the "best" solution so far
if(!auto_calibrated){
writeToRepresentation();
getCameraMatrixOffset().saveToConfig();
}
// reset to intial
getHeadMotionRequest().id = HeadMotionRequest::hold;
current_target = target_points.begin();
auto_cleared_data = auto_collected = auto_calibrated = false;
play_calibrated = play_calibrating = play_collecting = true;
derrors.clear();
last_error = 0;
minimizer->reset();
);
DEBUG_REQUEST("CameraMatrixV3:manual:reset_minimizer",
minimizer->reset();
);
if(use_automatic_mode){
if(!auto_calibrated){
doItAutomatically();
}
} else { // manual mode
DEBUG_REQUEST("CameraMatrixV3:manual:clear_calibration_data",
theCamMatErrorFunctionV3.clear();
);
DEBUG_REQUEST("CameraMatrixV3:manual:collect_calibration_data",
collectingData();
);
DEBUG_REQUEST_ON_DEACTIVE("CameraMatrixV3:manual:collect_calibration_data",
// reset to initial
getHeadMotionRequest().id = HeadMotionRequest::hold;
current_target = target_points.begin();
);
DEBUG_REQUEST("CameraMatrixV3:manual:calibrate",
calibrate();
);
DEBUG_REQUEST_ON_DEACTIVE("CameraMatrixV3:manual:calibrate",
writeToRepresentation();
getCameraMatrixOffset().saveToConfig();
);
DEBUG_REQUEST("CameraMatrixV3:manual:reset_calibration",
reset_calibration();
minimizer->reset();
derrors.clear();
last_error = 0;
);
DEBUG_REQUEST_ON_DEACTIVE("CameraMatrixV3:manual:reset_calibration",
writeToRepresentation();
getCameraMatrixOffset().saveToConfig();
);
}
last_frame_info = getFrameInfo();
}
void CameraMatrixCorrectorV3::reset_calibration()
{
getCameraMatrixOffset().body_rot = Vector2d();
getCameraMatrixOffset().head_rot = Vector3d();
getCameraMatrixOffset().cam_rot[CameraInfo::Top] = Vector3d();
getCameraMatrixOffset().cam_rot[CameraInfo::Bottom] = Vector3d();
cam_mat_offsets = Parameter::Zero();
}
// returns true if the averaged reduction per second decreases under a heuristic value
bool CameraMatrixCorrectorV3::calibrate()
{
double dt = getFrameInfo().getTimeInSeconds()-last_frame_info.getTimeInSeconds();
Eigen::Matrix<double, 11, 1> epsilon = Eigen::Matrix<double, 11, 1>::Constant(1e-4);
Parameter offset;
bool valid;
if(cmc_params.use_bounded_variable){
Parameter bounded_cam_mat_offsets = bounds.bound(cam_mat_offsets);
valid = minimizer->minimizeOneStep(theCamMatErrorFunctionV3, bounded_cam_mat_offsets, epsilon, offset);
} else {
valid = minimizer->minimizeOneStep(theCamMatErrorFunctionV3, cam_mat_offsets, epsilon, offset);
}
if(valid) {
if(cmc_params.use_bounded_variable){
Parameter bounded_cam_mat_offsets = bounds.bound(cam_mat_offsets);
cam_mat_offsets = bounds.unbound(bounded_cam_mat_offsets + offset);
} else {
cam_mat_offsets += offset;
}
}
double de_dt = (minimizer->error - last_error)/dt; // improvement per second
if(last_error != 0){ //ignore the jump from zero to initial error value
derrors.add(de_dt);
}
last_error = minimizer->error;
PLOT("CameraMatrixV3:error", minimizer->error);
PLOT("CameraMatrixV3:de/dt", de_dt);
PLOT("CameraMatrixV3:avg_derror", derrors.getAverage());
writeToRepresentation();
return (( (derrors.getAverage() > -50) // average error decreases by less than this per second
&& derrors.isFull())
|| minimizer->step_failed()); // and we have a full history
}
// returns true, if the trajectory is starting again from beginning
bool CameraMatrixCorrectorV3::collectingData()
{
if(current_target == target_points.end())
return true;
getHeadMotionRequest().id = HeadMotionRequest::goto_angle;
getHeadMotionRequest().targetJointPosition.x = Math::fromDegrees(current_target->x);
getHeadMotionRequest().targetJointPosition.y = Math::fromDegrees(current_target->y);
getHeadMotionRequest().velocity = 20;
MODIFY("CameraMatrixV3:collecting_velocity", getHeadMotionRequest().velocity);
double dt = getFrameInfo().getTimeInSeconds()-last_frame_info.getTimeInSeconds();
double current_yaw = Math::toDegrees(getSensorJointData().position[JointData::HeadYaw]);
double current_pitch = Math::toDegrees(getSensorJointData().position[JointData::HeadPitch]);
static double last_yaw(current_yaw);
static double last_pitch(current_pitch);
double vel_yaw = (last_yaw - current_yaw)/dt;
double vel_pitch = (last_pitch - current_pitch)/dt;
last_yaw = current_yaw;
last_pitch = current_pitch;
// state transitions and triggering sampling
bool target_reached = fabs(current_yaw - current_target->x) < 3
&& fabs(current_pitch - current_target->y) < 3
&& fabs(vel_yaw) < 0.2
&& fabs(vel_pitch) < 0.2;
if(target_reached && current_target != target_points.end()){
sampling();
current_target++;<--- Prefer prefix ++/-- operators for non-primitive types. [+]Prefix ++/-- operators should be preferred for non-primitive types. Pre-increment/decrement can be more efficient than post-increment/decrement. Post-increment/decrement usually involves keeping a copy of the previous value around and adds a little extra code.
}
PLOT("CameraMatrixV3:vel_yaw",vel_yaw);
PLOT("CameraMatrixV3:vel_pitch", vel_pitch);
PLOT("CameraMatrixV3:target_reached", target_reached);
return current_target == target_points.end();
}
void CameraMatrixCorrectorV3::sampling()
{
LineGraphPercept lineGraphPercept(getLineGraphPercept());
// TODO: ignore upper image if the center is above the horizon
// HACK: ignore it if headpitch is smaller than -15
//if(head_state == look_right_up || head_state == look_left_up
// || last_head_state == look_right_up || last_head_state == look_left_up){
if(Math::toDegrees(getSensorJointData().position[JointData::HeadPitch]) < -15) {
lineGraphPercept.edgelsInImageTop.clear();
}
theCamMatErrorFunctionV3.add(CamMatErrorFunctionV3::CalibrationDataSample(getKinematicChain().theLinks[KinematicChain::Torso].M,
lineGraphPercept,
getInertialModel().orientation,
getSensorJointData().position[JointData::HeadYaw],
getSensorJointData().position[JointData::HeadPitch]
));
}
void CameraMatrixCorrectorV3::doItAutomatically()
{
if(!auto_cleared_data) {
theCamMatErrorFunctionV3.clear();
readFromRepresentation();
auto_cleared_data = true;
}
if(!auto_collected){
auto_collected = collectingData();
if(play_collecting){
getSoundPlayData().mute = false;
getSoundPlayData().soundFile = "collectingdata.wav";
play_collecting = false;
}
}
if(auto_collected && !auto_calibrated){
auto_calibrated = calibrate();
if(auto_calibrated){
derrors.clear();
minimizer->reset();
}
if(play_calibrating){
getSoundPlayData().mute = false;
getSoundPlayData().soundFile = "calibrating.wav";
play_calibrating = false;
}
}
if(auto_calibrated){
writeToRepresentation();
getCameraMatrixOffset().saveToConfig();
if(play_calibrated) {
getSoundPlayData().mute = false;
getSoundPlayData().soundFile = "finished.wav";
play_calibrated = false;
}
}
}
void CameraMatrixCorrectorV3::writeToRepresentation()
{
getCameraMatrixOffset().body_rot = Vector2d(cam_mat_offsets(0),cam_mat_offsets(1));
getCameraMatrixOffset().head_rot = Vector3d(cam_mat_offsets(2),cam_mat_offsets(3),cam_mat_offsets(4));
getCameraMatrixOffset().cam_rot[CameraInfo::Top] = Vector3d(cam_mat_offsets(5),cam_mat_offsets(6),cam_mat_offsets(7));
getCameraMatrixOffset().cam_rot[CameraInfo::Bottom] = Vector3d(cam_mat_offsets(8),cam_mat_offsets(9),cam_mat_offsets(10));
}
void CameraMatrixCorrectorV3::readFromRepresentation(){
cam_mat_offsets << getCameraMatrixOffset().body_rot.x,
getCameraMatrixOffset().body_rot.y,
getCameraMatrixOffset().head_rot.x,
getCameraMatrixOffset().head_rot.y,
getCameraMatrixOffset().head_rot.z,
getCameraMatrixOffset().cam_rot[CameraInfo::Top].x,
getCameraMatrixOffset().cam_rot[CameraInfo::Top].y,
getCameraMatrixOffset().cam_rot[CameraInfo::Top].z,
getCameraMatrixOffset().cam_rot[CameraInfo::Bottom].x,
getCameraMatrixOffset().cam_rot[CameraInfo::Bottom].y,
getCameraMatrixOffset().cam_rot[CameraInfo::Bottom].z;
}
void CameraMatrixCorrectorV3::update_bounds(){
bounds.lower_bound << cmc_params.lower.body_x,
cmc_params.lower.body_y,
cmc_params.lower.head_x,
cmc_params.lower.head_y,
cmc_params.lower.head_z,
cmc_params.lower.cam_x,
cmc_params.lower.cam_y,
cmc_params.lower.cam_z,
cmc_params.lower.cam_top_x,
cmc_params.lower.cam_top_y,
cmc_params.lower.cam_top_z;
bounds.upper_bound << cmc_params.upper.body_x,
cmc_params.upper.body_y,
cmc_params.upper.head_x,
cmc_params.upper.head_y,
cmc_params.upper.head_z,
cmc_params.upper.cam_x,
cmc_params.upper.cam_y,
cmc_params.upper.cam_z,
cmc_params.upper.cam_top_x,
cmc_params.upper.cam_top_y,
cmc_params.upper.cam_top_z;
if(cmc_params.use_bounded_variable){
theCamMatErrorFunctionV3.bounds = &bounds;
} else {
theCamMatErrorFunctionV3.bounds = nullptr;
}
}
|