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
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045 #include "Robots/LoBot/tti/LoTTIEstimator.H"
00046 #include "Robots/LoBot/config/LoConfigHelpers.H"
00047
00048 #include "Robots/LoBot/util/LoGL.H"
00049 #include "Robots/LoBot/util/LoString.H"
00050 #include "Robots/LoBot/util/LoMath.H"
00051 #include "Robots/LoBot/misc/LoExcept.H"
00052 #include "Robots/LoBot/misc/singleton.hh"
00053 #include "Robots/LoBot/util/range.hh"
00054 #include "Robots/LoBot/util/triple.hh"
00055
00056
00057 #ifdef INVT_HAVE_LIBGLU
00058 #include <GL/glu.h>
00059 #endif
00060
00061 #ifdef INVT_HAVE_LIBGL
00062 #include <GL/gl.h>
00063 #endif
00064
00065
00066 #include <iomanip>
00067 #include <sstream>
00068 #include <numeric>
00069 #include <algorithm>
00070 #include <functional>
00071 #include <iterator>
00072
00073
00074
00075 namespace lobot {
00076
00077
00078
00079 namespace {
00080
00081
00082 template<typename T>
00083 static inline T conf(const std::string& key, T default_value)
00084 {
00085 return get_conf<T>("tti_estimator", key, default_value) ;
00086 }
00087
00088
00089 template<typename T>
00090 static inline range<T> conf(const std::string& key, const range<T>& defval)
00091 {
00092 return get_conf<T>("tti_estimator", key, defval) ;
00093 }
00094
00095
00096
00097
00098
00099 enum RenderMode {
00100 RENDER_OFF,
00101 RENDER_BELIEF,
00102 RENDER_TTI,
00103 RENDER_DISTANCE,
00104 } ;
00105
00106
00107
00108 class TTIParams : public singleton<TTIParams> {
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136 int m_sder_filter_size ;
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152 float m_rising_threshold, m_falling_threshold ;
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176 float m_confidence_threshold ;
00177
00178
00179
00180
00181 range<float> m_distance_range ;
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205 RenderMode m_render_mode ;
00206
00207
00208
00209
00210
00211 bool m_render_labels ;
00212
00213
00214 TTIParams() ;
00215
00216
00217 friend class singleton<TTIParams> ;
00218
00219 public:
00220
00221
00222 static int sder_filter_size() {
00223 return instance().m_sder_filter_size ;
00224 }
00225 static float rising_threshold() {
00226 return instance().m_rising_threshold ;
00227 }
00228 static float falling_threshold() {
00229 return instance().m_falling_threshold ;
00230 }
00231 static float confidence_threshold() {
00232 return instance().m_confidence_threshold ;
00233 }
00234 static const range<float>& distance_range() {
00235 return instance().m_distance_range ;
00236 }
00237 static RenderMode render_mode() {return instance().m_render_mode ;}
00238 static bool render_labels() {return instance().m_render_labels ;}
00239
00240 } ;
00241
00242
00243 TTIParams::TTIParams()
00244 : m_sder_filter_size(clamp(conf("sder_filter_size", 5), 1, 100)),
00245 m_rising_threshold(clamp(conf("rising_threshold", 700.0f),
00246 200.0f, 1000.0f)),
00247 m_falling_threshold(clamp(conf("falling_threshold", 100.0f),
00248 10.0f, 250.0f)),
00249 m_confidence_threshold(clamp(conf("confidence_threshold", 0.3f),
00250 0.01f, 0.9f)),
00251 m_distance_range(clamp(conf("distance_range", make_range(50.0f, 5000.0f)),
00252 make_range(10.0f, 10000.0f))),
00253 m_render_mode(RENDER_OFF),
00254 m_render_labels(conf("render_labels", false))
00255 {
00256 const std::string render_mode =
00257 downstring(conf<std::string>("render_mode", "off")) ;
00258 if (render_mode == "bel")
00259 m_render_mode = RENDER_BELIEF ;
00260 else if (render_mode == "tti")
00261 m_render_mode = RENDER_TTI ;
00262 else if (render_mode == "dis")
00263 m_render_mode = RENDER_DISTANCE ;
00264 }
00265
00266
00267 typedef TTIParams Params ;
00268
00269 }
00270
00271
00272
00273 SensorModel& TTIEstimator::looming_sensor_model()
00274 {
00275 static SensorModel S("looming") ;
00276 return S ;
00277 }
00278
00279 SensorModel& TTIEstimator::blanking_sensor_model()
00280 {
00281 static SensorModel S("blanking") ;
00282 return S ;
00283 }
00284
00285
00286
00287
00288 TTIEstimator::TTIEstimator(const LocustModel* L)
00289 : Drawable(L->name(), L->geometry()),
00290 m_locust(L),
00291 m_tti(-1), m_confidence(0),
00292 m_direction(cos(L->direction()), sin(L->direction())),
00293 m_sder(Params::sder_filter_size()),
00294 m_distance(-1),
00295 m_sensor_model(& looming_sensor_model())
00296 {
00297 m_lgmd[0] = m_lgmd[1] = 0 ;
00298 m_fder[0] = m_fder[1] = 0 ;
00299
00300 const int N = m_sensor_model->column_size() ;
00301 m_belief.reserve(N) ;
00302 std::fill_n(std::back_inserter(m_belief), N, 1.0f/N) ;
00303
00304 RenderMode render_mode = Params::render_mode() ;
00305 if (render_mode != RENDER_OFF)
00306 {
00307 if (render_mode == RENDER_TTI || render_mode == RENDER_DISTANCE) {
00308 m_actual.resize(m_geometry.width) ;
00309 m_predicted.resize(m_geometry.width) ;
00310 }
00311 (const_cast<LocustModel*>(L))->add_hook(
00312 RenderHook(render_hook, reinterpret_cast<unsigned long>(this))) ;
00313 }
00314 }
00315
00316
00317
00318
00319 TTIEstimator::LGMDPhase TTIEstimator::lgmd_phase() const
00320 {
00321 if (m_sensor_model == & looming_sensor_model())
00322 return LOOMING ;
00323 if (m_sensor_model == & blanking_sensor_model())
00324 return BLANKING ;
00325 throw misc_error(LOGIC_ERROR) ;
00326 }
00327
00328
00329
00330 void TTIEstimator::sensor_model(const SensorModel* S)
00331 {
00332 viz_lock() ;
00333 m_sensor_model = S ;
00334 viz_unlock() ;
00335 }
00336
00337
00338
00339 void TTIEstimator::copy_lgmd()
00340 {
00341
00342 m_lgmd[1] = m_lgmd[0] ;
00343 m_lgmd[0] = m_locust->get_lgmd() ;
00344
00345
00346 m_fder[1] = m_fder[0] ;
00347 m_fder[0] = m_lgmd[0] - m_lgmd[1] ;
00348
00349
00350 m_sder.add(m_fder[0] - m_fder[1]) ;
00351
00352
00353
00354 viz_lock() ;
00355 switch (Params::render_mode())
00356 {
00357 case RENDER_TTI:
00358 m_actual.pop_front() ;
00359 m_actual.push_back(m_locust->tti()) ;
00360 break ;
00361 case RENDER_DISTANCE:
00362 m_actual.pop_front() ;
00363 m_actual.push_back(m_locust->distance()) ;
00364 break ;
00365 default:
00366 break ;
00367 }
00368 viz_unlock() ;
00369 }
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381 class add_uniform_distribution {
00382 float uniform_probability ;
00383 public:
00384 add_uniform_distribution(float uniform_probability) ;
00385 float operator()(float p) const {
00386 return (p + uniform_probability)/2 ;
00387 }
00388 } ;
00389
00390 add_uniform_distribution::add_uniform_distribution(float p)
00391 : uniform_probability(p)
00392 {}
00393
00394
00395
00396
00397
00398
00399
00400 void TTIEstimator::update()
00401 {
00402
00403
00404 switch (lgmd_phase())
00405 {
00406 case LOOMING:
00407 if (m_lgmd[0] > Params::rising_threshold() && m_sder.value() < 0)
00408 sensor_model(& blanking_sensor_model()) ;
00409 break ;
00410 case BLANKING:
00411 if (m_lgmd[0] < Params::falling_threshold())
00412 sensor_model(& looming_sensor_model()) ;
00413 break ;
00414 }
00415
00416
00417
00418
00419 Belief tmp(m_belief.size(), 0.0f) ;
00420 std::transform(m_belief.begin(), m_belief.end(),
00421 m_sensor_model->column_vector(m_lgmd[0]).begin(),
00422 tmp.begin(), std::multiplies<float>()) ;
00423
00424
00425
00426
00427 float normalizer = std::accumulate(tmp.begin(), tmp.end(), 0.0f) ;
00428 if (normalizer <= 0)
00429 std::fill(tmp.begin(), tmp.end(), 1.0f/tmp.size()) ;
00430 else
00431 std::transform(tmp.begin(), tmp.end(), tmp.begin(),
00432 std::bind2nd(std::multiplies<float>(), 1/normalizer)) ;
00433
00434
00435
00436
00437
00438
00439
00440
00441 viz_lock() ;
00442 std::transform(tmp.begin(), tmp.end(), m_belief.begin(),
00443 add_uniform_distribution(1.0f/tmp.size())) ;
00444
00445 Belief::const_iterator max = std::max_element(m_belief.begin(),
00446 m_belief.end()) ;
00447 const float min = m_sensor_model->row_min() ;
00448 const float step = m_sensor_model->row_step();
00449 m_tti = min + (max - m_belief.begin() + 1) * step ;
00450 m_confidence = *max ;
00451 viz_unlock() ;
00452 }
00453
00454
00455
00456
00457
00458 void TTIEstimator::compute_distance(const Vector& velocity)
00459 {
00460 float speed = magnitude(dot(m_direction, velocity) * m_direction) ;
00461 float distance = -1 ;
00462 float time = -1 ;
00463 if (m_confidence >= Params::confidence_threshold())
00464 {
00465 time = m_tti ;
00466 if (! is_zero(speed))
00467 distance = clamp(speed * time * 1000, Params::distance_range()) ;
00468 }
00469
00470 viz_lock() ;
00471 m_distance = distance ;
00472 switch (Params::render_mode())
00473 {
00474 case RENDER_TTI:
00475 m_predicted.pop_front() ;
00476 m_predicted.push_back(time) ;
00477 break ;
00478 case RENDER_DISTANCE:
00479 m_predicted.pop_front() ;
00480 m_predicted.push_back(distance) ;
00481 break ;
00482 default:
00483 break ;
00484 }
00485 viz_unlock() ;
00486 }
00487
00488
00489
00490 #ifdef INVT_HAVE_LIBGLU
00491
00492
00493 static std::string phase_label(int lgmd_phase)
00494 {
00495 std::ostringstream str ;
00496 switch (lgmd_phase)
00497 {
00498 case 0:
00499 str << "Loom" ;
00500 break ;
00501 case 1:
00502 str << "Blank" ;
00503 break ;
00504 default:
00505 str << "???" ;
00506 break ;
00507 }
00508 return str.str() ;
00509 }
00510
00511
00512 static std::string tti_label(float tti)
00513 {
00514 using namespace std ;
00515
00516 std::ostringstream str ;
00517 if (tti < 0)
00518 str << "???" ;
00519 else
00520 str << fixed << setprecision(1) << tti << 's' ;
00521 return str.str() ;
00522 }
00523
00524
00525
00526 static std::string tti_label(float tti, float confidence)
00527 {
00528 using namespace std ;
00529
00530 std::ostringstream str ;
00531 str << fixed << setprecision(1) << tti << "s ("
00532 << fixed << setprecision(1) << (confidence * 100) << "%)" ;
00533 return str.str() ;
00534 }
00535
00536
00537
00538 static std::string distance_label(int distance)
00539 {
00540 std::ostringstream str ;
00541 if (distance < 0)
00542 str << "???" ;
00543 else
00544 str << distance << " mm" ;
00545 return str.str() ;
00546 }
00547
00548
00549 static std::string error_label(float actual, float predicted)
00550 {
00551 std::ostringstream str ;
00552 str << "Err: " ;
00553 if (actual < 0 || predicted < 0)
00554 str << '?' ;
00555 else
00556 str << round(100 * (predicted - actual)/actual) << '%' ;
00557 return str.str() ;
00558 }
00559
00560
00561
00562
00563 void TTIEstimator::render_hook(unsigned long client_data)
00564 {
00565 TTIEstimator* me = reinterpret_cast<TTIEstimator*>(client_data) ;
00566 switch (Params::render_mode())
00567 {
00568 case RENDER_BELIEF:
00569 me->render_belief() ;
00570 break ;
00571 case RENDER_TTI:
00572 me->render_tti() ;
00573 break ;
00574 case RENDER_DISTANCE:
00575 me->render_distance() ;
00576 break ;
00577 default:
00578 break ;
00579 }
00580 }
00581
00582
00583
00584 void TTIEstimator::render_belief()
00585 {
00586
00587
00588 viz_lock() ;
00589 Belief B = m_belief ;
00590 const SensorModel* S = m_sensor_model ;
00591 LGMDPhase P = lgmd_phase() ;
00592 int D = round(m_distance) ;
00593 viz_unlock() ;
00594
00595
00596
00597 setup_view_volume(S->row_min(), S->row_max(), 0, 1) ;
00598
00599
00600 glColor3f(1, 0, 0) ;
00601 glBegin(GL_LINE_STRIP) ;
00602 glVertex2f(S->row_min(), 0) ;
00603 float x = S->row_min() + S->row_step() ;
00604 for (unsigned int i = 0; i < B.size(); ++i, x += S->row_step())
00605 glVertex2f(x, B[i]);
00606 glVertex2f(S->row_max(), 0) ;
00607 glEnd() ;
00608 restore_view_volume() ;
00609
00610
00611
00612 if (Params::render_labels())
00613 {
00614 Belief::const_iterator max = std::max_element(B.begin(), B.end()) ;
00615 float tti = S->row_min() + (max - B.begin() + 1) * S->row_step() ;
00616 text_view_volume() ;
00617 glColor3f(0, 1, 1) ;
00618 draw_label(3, 24, phase_label(P).c_str()) ;
00619 draw_label(3, 40, tti_label(tti, *max).c_str()) ;
00620 draw_label(3, 56, distance_label(D).c_str()) ;
00621 restore_view_volume() ;
00622 }
00623 }
00624
00625
00626 typedef std::deque<float> History ;
00627
00628 static void draw_history(const History& history)
00629 {
00630 const int N = history.size() ;
00631 History::const_iterator y = history.begin() ;
00632 glBegin(GL_LINE_STRIP) ;
00633 for (int x = 0; x < N; ++x, ++y)
00634 glVertex2f(x, *y) ;
00635 glEnd() ;
00636 }
00637
00638 static void draw_history(const History& actual, const History& predicted)
00639 {
00640 glPushAttrib(GL_COLOR_BUFFER_BIT) ;
00641 glColor3f(0.0f, 0.15f, 0.85f) ;
00642 draw_history(actual) ;
00643
00644 glColor3f(1, 0, 0) ;
00645 draw_history(predicted) ;
00646 glPopAttrib() ;
00647 }
00648
00649
00650
00651
00652 void TTIEstimator::render_tti()
00653 {
00654
00655 viz_lock() ;
00656 const float max_probability =
00657 *(std::max_element(m_belief.begin(), m_belief.end())) ;
00658 History actual = m_actual ;
00659 History predicted = m_predicted ;
00660 viz_unlock() ;
00661
00662
00663
00664 setup_view_volume(0, m_geometry.width, 0, 60) ;
00665
00666
00667 draw_history(actual, predicted) ;
00668 restore_view_volume() ;
00669
00670
00671 if (Params::render_labels())
00672 {
00673 float A = actual.back() ;
00674 float P = predicted.back() ;
00675 text_view_volume() ;
00676 glColor3f(0, 1, 1) ;
00677 draw_label(3, 24, tti_label(A).c_str()) ;
00678 draw_label(3, 40, tti_label(P, max_probability).c_str()) ;
00679 draw_label(3, 56, error_label(A, P).c_str()) ;
00680 restore_view_volume() ;
00681 }
00682 }
00683
00684
00685
00686
00687
00688 void TTIEstimator::render_distance()
00689 {
00690
00691 viz_lock() ;
00692 History actual = m_actual ;
00693 History predicted = m_predicted ;
00694 viz_unlock() ;
00695
00696
00697
00698
00699 setup_view_volume(0, m_geometry.width, 0, Params::distance_range().max()) ;
00700
00701
00702 draw_history(actual, predicted) ;
00703 restore_view_volume() ;
00704
00705
00706 if (Params::render_labels())
00707 {
00708 float A = actual.back() ;
00709 float P = predicted.back() ;
00710 text_view_volume() ;
00711 glColor3f(0, 1, 1) ;
00712 draw_label(3, 24, distance_label(round(A)).c_str()) ;
00713 draw_label(3, 40, distance_label(round(P)).c_str()) ;
00714 draw_label(3, 56, error_label(A, P).c_str()) ;
00715 restore_view_volume() ;
00716 }
00717 }
00718
00719 #endif
00720
00721
00722
00723 TTIEstimator::~TTIEstimator(){}
00724
00725
00726
00727 }
00728
00729
00730
00731
00732