relvelo.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <models/velocity/relvelo.h>
00026
00027 #include <utils/system/console_colors.h>
00028 #include <utils/time/time.h>
00029
00030 #include <cmath>
00031 #include <cstdlib>
00032
00033 using namespace std;
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 VelocityFromRelative::VelocityFromRelative(RelativePositionModel* model,
00045 unsigned int max_history_length,
00046 unsigned int calc_interval)
00047 {
00048 this->relative_pos_model = model;
00049 this->max_history_length = max_history_length;
00050 this->calc_interval = calc_interval;
00051
00052
00053
00054 robot_rel_vel_x = robot_rel_vel_y = 0.f;
00055
00056 velocity_x = velocity_y = 0.f;
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079 avg_vx_sum = 0.f;
00080 avg_vx_num = 0;
00081
00082 avg_vy_sum = 0.f;
00083 avg_vy_num = 0;
00084
00085 ball_history.clear();
00086
00087 }
00088
00089
00090
00091 VelocityFromRelative::~VelocityFromRelative()
00092 {
00093 }
00094
00095
00096 void
00097 VelocityFromRelative::setPanTilt(float pan, float tilt)
00098 {
00099 }
00100
00101
00102 void
00103 VelocityFromRelative::setRobotPosition(float x, float y, float ori, timeval t)
00104 {
00105 }
00106
00107
00108 void
00109 VelocityFromRelative::setRobotVelocity(float rel_vel_x, float rel_vel_y, timeval t)
00110 {
00111 robot_rel_vel_x = rel_vel_x;
00112 robot_rel_vel_y = rel_vel_y;
00113 robot_rel_vel_t.tv_sec = t.tv_sec;
00114 robot_rel_vel_t.tv_usec = t.tv_usec;
00115 }
00116
00117 void
00118 VelocityFromRelative::setTime(timeval t)
00119 {
00120 now.tv_sec = t.tv_sec;
00121 now.tv_usec = t.tv_usec;
00122 }
00123
00124
00125 void
00126 VelocityFromRelative::setTimeNow()
00127 {
00128 gettimeofday(&now, NULL);
00129 }
00130
00131
00132 void
00133 VelocityFromRelative::getTime(long int *sec, long int *usec)
00134 {
00135 *sec = now.tv_sec;
00136 *usec = now.tv_usec;
00137 }
00138
00139
00140 void
00141 VelocityFromRelative::getVelocity(float *vel_x, float *vel_y)
00142 {
00143 if (vel_x != NULL) {
00144 *vel_x = velocity_x;
00145 }
00146 if (vel_y != NULL) {
00147 *vel_y = velocity_y;
00148 }
00149 }
00150
00151
00152 float
00153 VelocityFromRelative::getVelocityX()
00154 {
00155 return velocity_x;
00156 }
00157
00158
00159 float
00160 VelocityFromRelative::getVelocityY()
00161 {
00162 return velocity_y;
00163 }
00164
00165
00166 void
00167 VelocityFromRelative::calc()
00168 {
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202 cur_ball_x = relative_pos_model->get_x();
00203 cur_ball_y = relative_pos_model->get_y();
00204 cur_ball_dist = relative_pos_model->get_distance();
00205
00206 if ( isnan(cur_ball_x) || isinf(cur_ball_x) ||
00207 isnan(cur_ball_y) || isinf(cur_ball_y) ||
00208 isnan(cur_ball_dist) || isinf(cur_ball_dist) ) {
00209
00210 return;
00211 }
00212
00213
00214
00215
00216 if (last_available) {
00217 proj_time_diff_sec = fawkes::time_diff_sec(now, last_time);
00218 proj_x = last_x + velocity_x * proj_time_diff_sec;
00219 proj_y = last_y + velocity_y * proj_time_diff_sec;
00220 last_proj_error_x = cur_ball_x - proj_x;
00221 last_proj_error_y = cur_ball_y - proj_y;
00222 last_available = false;
00223 } else {
00224 last_proj_error_x = cur_ball_x;
00225 last_proj_error_y = cur_ball_x;
00226 }
00227
00228
00229
00230 vel_postime_t *vpt = (vel_postime_t *)malloc(sizeof(vel_postime_t));;
00231 vpt->x = cur_ball_x;
00232 vpt->y = cur_ball_y;
00233 vpt->t.tv_sec = now.tv_sec;
00234 vpt->t.tv_usec = now.tv_usec;
00235 ball_history.push_front( vpt );
00236
00237
00238 if (ball_history.size() >= 2) {
00239
00240
00241
00242
00243
00244
00245 if ( fawkes::time_diff_sec(robot_rel_vel_t, vel_last_time) != 0 ) {
00246
00247
00248 vel_last_time.tv_sec = robot_rel_vel_t.tv_sec;
00249 vel_last_time.tv_usec = robot_rel_vel_t.tv_usec;
00250
00251 f_diff_sec = HUGE;
00252 float time_diff;
00253 vel_postime_t *young = NULL;
00254 vel_postime_t *old = NULL;
00255 unsigned int step = 0;
00256 for (bh_it = ball_history.begin(); bh_it != ball_history.end(); ++bh_it) {
00257
00258
00259 time_diff = fawkes::time_diff_sec((*bh_it)->t, robot_rel_vel_t);
00260 if ( (time_diff > 0) && (time_diff < f_diff_sec) ) {
00261 f_diff_sec = time_diff;
00262 young = (*bh_it);
00263 } else {
00264
00265 if (step != calc_interval) {
00266 ++step;
00267 } else {
00268
00269 old = *bh_it;
00270 ++bh_it;
00271 break;
00272 }
00273 }
00274 }
00275
00276 if ((young != NULL) && (old != NULL)) {
00277
00278
00279 diff_x = young->x - old->x;
00280 diff_y = young->y - old->y;
00281
00282 f_diff_sec = fawkes::time_diff_sec(young->t, old->t);
00283
00284 velocity_x = diff_x / f_diff_sec;
00285 velocity_y = diff_y / f_diff_sec;
00286
00287
00288
00289 velocity_x += robot_rel_vel_x;
00290 velocity_y += robot_rel_vel_y;
00291
00292 velocity_x -= last_proj_error_x * proj_time_diff_sec;
00293 velocity_y -= last_proj_error_y * proj_time_diff_sec;
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324 last_x = cur_ball_x;
00325 last_y = cur_ball_y;
00326 last_time.tv_sec = now.tv_sec;
00327 last_time.tv_usec = now.tv_usec;
00328 last_available = true;
00329
00330
00331
00332
00333
00334
00335
00336
00337 if (bh_it != ball_history.end()) {
00338 ball_history.erase(bh_it, ball_history.end());
00339 }
00340 } else {
00341
00342 velocity_x = 0.f;
00343 velocity_y = 0.f;
00344 }
00345 } else {
00346
00347 if (fawkes::time_diff_sec(now, vel_last_time) > 2) {
00348
00349 velocity_x = 0.f;
00350 velocity_y = 0.f;
00351 }
00352 }
00353 } else {
00354
00355 velocity_x = 0.f;
00356 velocity_y = 0.f;
00357 }
00358
00359 if (ball_history.size() > max_history_length) {
00360 bh_it = ball_history.begin();
00361 for (unsigned int i = 0; i < max_history_length; ++i) {
00362 ++bh_it;
00363 }
00364 ball_history.erase(bh_it, ball_history.end());
00365 }
00366
00367 }
00368
00369
00370 void
00371 VelocityFromRelative::reset()
00372 {
00373
00374
00375
00376
00377
00378 avg_vx_sum = 0.f;
00379 avg_vx_num = 0;
00380 avg_vy_sum = 0.f;
00381 avg_vy_num = 0;
00382 velocity_x = 0.f;
00383 velocity_y = 0.f;
00384 ball_history.clear();
00385 }
00386
00387
00388 const char *
00389 VelocityFromRelative::getName() const
00390 {
00391 return "VelocityModel::VelocityFromRelative";
00392 }
00393
00394
00395 coordsys_type_t
00396 VelocityFromRelative::getCoordinateSystem()
00397 {
00398 return COORDSYS_ROBOT_CART;
00399 }
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449