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/control/LoOpenPath.H"
00046 #include "Robots/LoBot/control/LoSpinArbiter.H"
00047
00048 #include "Robots/LoBot/LoApp.H"
00049 #include "Robots/LoBot/ui/LoLaserViz.H"
00050 #include "Robots/LoBot/config/LoConfigHelpers.H"
00051 #include "Robots/LoBot/thread/LoUpdateLock.H"
00052
00053 #include "Robots/LoBot/misc/LoExcept.H"
00054 #include "Robots/LoBot/misc/LoRegistry.H"
00055 #include "Robots/LoBot/misc/singleton.hh"
00056
00057 #include "Robots/LoBot/util/LoGL.H"
00058 #include "Robots/LoBot/util/LoMath.H"
00059 #include "Robots/LoBot/util/range.hh"
00060
00061
00062 #ifdef INVT_HAVE_LIBGLU
00063 #include <GL/glu.h>
00064 #endif
00065
00066 #ifdef INVT_HAVE_LIBGL
00067 #include <GL/gl.h>
00068 #endif
00069
00070
00071 #include <algorithm>
00072 #include <map>
00073 #include <functional>
00074 #include <utility>
00075
00076
00077 #include <math.h>
00078
00079
00080
00081 namespace lobot {
00082
00083
00084
00085 namespace {
00086
00087
00088 template<typename T>
00089 inline T conf(const std::string& key, const T& default_value)
00090 {
00091 return get_conf<T>(LOBE_OPEN_PATH, key, default_value) ;
00092 }
00093
00094
00095 template<typename T>
00096 inline range<T> conf(const std::string& key, const range<T>& default_value)
00097 {
00098 return get_conf<T>(LOBE_OPEN_PATH, key, default_value) ;
00099 }
00100
00101
00102
00103 class Params : public singleton<Params> {
00104
00105
00106 float m_path_width ;
00107
00108
00109
00110 float m_min_path_length ;
00111
00112
00113
00114
00115 int m_alpha ;
00116
00117
00118
00119
00120
00121
00122 int m_fov ;
00123
00124
00125
00126 int m_step ;
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151 range<int> m_dead_zone ;
00152
00153
00154
00155
00156
00157
00158
00159
00160 bool m_spin_style_steering ;
00161
00162
00163
00164
00165
00166
00167
00168 int m_update_delay ;
00169
00170
00171
00172 typedef Drawable::Geometry Geom ;
00173 Geom m_geometry ;
00174
00175
00176 Params() ;
00177
00178
00179 friend class singleton<Params> ;
00180
00181 public:
00182
00183
00184 static float path_width() {return instance().m_path_width ;}
00185 static float min_path_length() {return instance().m_min_path_length ;}
00186 static int alpha() {return instance().m_alpha ;}
00187 static int fov() {return instance().m_fov ;}
00188 static int step() {return instance().m_step ;}
00189 static range<int> dead_zone() {return instance().m_dead_zone ;}
00190 static bool spin_style_steering(){return instance().m_spin_style_steering;}
00191 static int update_delay() {return instance().m_update_delay ;}
00192 static Geom geometry() {return instance().m_geometry ;}
00193
00194 } ;
00195
00196
00197 Params::Params()
00198 : m_path_width(clamp(conf("path_width", 175.0f), 150.0f, 500.0f)),
00199 m_min_path_length(clamp(conf("min_path_length", 250.0f),
00200 100.0f, 2500.0f)),
00201 m_alpha(round(atan(m_path_width/2/m_min_path_length))),
00202 m_fov(clamp(conf("fov", 90), 0, 180)),
00203 m_step(clamp(conf("step", 10), 1, 45)),
00204 m_dead_zone(conf("dead_zone", range<int>(-20, 20))),
00205 m_spin_style_steering(conf("spin_style_steering", false)),
00206 m_update_delay(clamp(conf("update_delay", 600), 250, 10000)),
00207 m_geometry(conf<std::string>("geometry", "0 0 10 10"))
00208 {}
00209
00210 }
00211
00212
00213
00214 OpenPath::OpenPath()
00215 : base(Params::update_delay(), LOBE_OPEN_PATH, Params::geometry())
00216 {
00217 start(LOBE_OPEN_PATH) ;
00218 }
00219
00220 void OpenPath::pre_run()
00221 {
00222 if (! App::lrf())
00223 throw behavior_error(LASER_RANGE_FINDER_MISSING) ;
00224 if (! App::robot())
00225 throw behavior_error(MOTOR_SYSTEM_MISSING) ;
00226
00227 Drawable* v = App::laser_viz() ;
00228 if (v)
00229 v->add_hook(RenderHook(render_paths,
00230 reinterpret_cast<unsigned long>(this))) ;
00231 }
00232
00233 OpenPath::PathInfo::PathInfo(float length, float width)
00234 : m_info(length, width, length * width)
00235 {
00236 if (length < 0 || width < 0)
00237 m_info.third = -1 ;
00238 }
00239
00240
00241
00242 void OpenPath::action()
00243 {
00244
00245
00246 UpdateLock::begin_read() ;
00247 LRFData lrf(App::lrf()) ;
00248 UpdateLock::end_read() ;
00249
00250
00251
00252
00253 Paths paths ;
00254
00255
00256 int fov = std::max(lrf.min_angle() + Params::alpha(), -Params::fov()) ;
00257 for (int angle = 0; angle >= fov; angle -= Params::step())
00258 {
00259 PathInfo p = open_path(angle, lrf) ;
00260 if (p.area() > 0)
00261 paths.insert(std::make_pair(angle, p)) ;
00262 }
00263
00264 fov = std::min(lrf.max_angle() - Params::alpha(), Params::fov()) ;
00265 for (int angle = Params::step(); angle <= fov; angle += Params::step())
00266 {
00267 PathInfo p = open_path(angle, lrf) ;
00268 if (p.area() > 0)
00269 paths.insert(std::make_pair(angle, p)) ;
00270 }
00271 viz_lock() ;
00272 m_paths = paths ;
00273 viz_unlock() ;
00274
00275
00276
00277 Paths::const_iterator max =
00278 std::max_element(paths.begin(), paths.end(), map_value_compare(paths)) ;
00279
00280
00281
00282
00283 if (! Params::dead_zone().in(max->first))
00284 {
00285 if (Params::spin_style_steering())
00286 SpinArbiter::instance().vote(base::name,
00287 new SpinArbiter::Vote(max->first)) ;
00288 else
00289 {
00290 const int T = TurnArbiter::turn_max() ;
00291 TurnArbiter::Vote* V = new TurnArbiter::Vote(
00292 turn_vote_centered_at(clamp(max->first, -T, T))) ;
00293
00294
00295
00296
00297
00298
00299 viz_lock() ;
00300 m_vote = *V ;
00301 viz_unlock() ;
00302
00303 TurnArbiter::instance().vote(base::name, V) ;
00304 }
00305 }
00306 }
00307
00308
00309
00310 OpenPath::PathInfo OpenPath::open_path(int theta, const LRFData& lrf) const
00311 {
00312 const float w = Params::path_width()/2 ;
00313
00314 float L = lrf.max_distance() * 2 ;
00315 for (int x = theta - Params::alpha(); x <= theta + Params::alpha(); ++x)
00316 {
00317 int D = lrf[x] ;
00318 if (D < 0)
00319 continue ;
00320 if (D * sin(abs(theta - x)) <= w) {
00321 float d = D * cos(abs(theta - x)) ;
00322 if (d < L)
00323 L = d ;
00324 }
00325 }
00326
00327 if (L > lrf.max_distance() || L < Params::min_path_length())
00328 return PathInfo(-1, -1) ;
00329 return PathInfo(L, Params::path_width()) ;
00330 }
00331
00332
00333 #ifdef INVT_HAVE_LIBGL
00334
00335 void OpenPath::render_me()
00336 {
00337
00338
00339 viz_lock() ;
00340 TurnArbiter::Vote V = m_vote ;
00341 viz_unlock() ;
00342
00343
00344 unit_view_volume() ;
00345 glBegin(GL_LINES) ;
00346 for (TurnArbiter::Vote::iterator it = V.begin(); it; ++it)
00347 {
00348 float vote = it.value() ;
00349 float direction = it.direction() ;
00350 if (vote < 0)
00351 {
00352 glColor3f(1, 0, 0) ;
00353 glVertex2i(0, 0) ;
00354 glVertex2f(-vote * cos(direction), -vote * sin(direction)) ;
00355 }
00356 else if (vote > 0)
00357 {
00358 glColor3f(0, 1, 0) ;
00359 glVertex2i(0, 0) ;
00360 glVertex2f(vote * cos(direction), vote * sin(direction)) ;
00361 }
00362 }
00363 glEnd() ;
00364
00365
00366
00367 restore_view_volume() ;
00368 text_view_volume() ;
00369 glColor3f(0, 1, 1) ;
00370 draw_label(3, 12, "Open Path") ;
00371 restore_view_volume() ;
00372 }
00373
00374 void OpenPath::render_paths(unsigned long client_data)
00375 {
00376 (reinterpret_cast<OpenPath*>(client_data))->render_paths() ;
00377 }
00378
00379 void OpenPath::render_paths()
00380 {
00381
00382
00383 viz_lock() ;
00384 Paths P = m_paths ;
00385 viz_unlock() ;
00386
00387
00388 glPushMatrix() ;
00389 glRotatef(get_conf("laser_viz", "lrf_direction", 90.0f), 0, 0, 1) ;
00390 glBegin(GL_LINES) ;
00391 glColor3f(1, 1, 1) ;
00392 for (Paths::const_iterator it = P.begin(); it != P.end(); ++it)
00393 {
00394 glVertex2i(0, 0) ;
00395 glVertex2f(it->second.length() * cos(it->first),
00396 it->second.length() * sin(it->first));
00397 }
00398 glEnd() ;
00399 glPopMatrix() ;
00400 }
00401
00402 #endif
00403
00404
00405
00406 OpenPath::~OpenPath(){}
00407
00408
00409
00410 }
00411
00412
00413
00414
00415