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/LoForward.H"
00046
00047 #include "Robots/LoBot/LoApp.H"
00048 #include "Robots/LoBot/config/LoConfigHelpers.H"
00049 #include "Robots/LoBot/thread/LoUpdateLock.H"
00050
00051 #include "Robots/LoBot/misc/LoExcept.H"
00052 #include "Robots/LoBot/misc/LoRegistry.H"
00053 #include "Robots/LoBot/util/LoGL.H"
00054 #include "Robots/LoBot/util/LoMath.H"
00055
00056
00057 #ifdef INVT_HAVE_LIBGL
00058 #include <GL/gl.h>
00059 #endif
00060
00061
00062 #include <iomanip>
00063 #include <sstream>
00064 #include <algorithm>
00065 #include <limits>
00066
00067
00068
00069 namespace lobot {
00070
00071
00072
00073
00074 template<typename T>
00075 static inline T conf(const std::string& key, const T& default_value)
00076 {
00077 return get_conf<T>(LOBE_FORWARD, key, default_value) ;
00078 }
00079
00080
00081 template<typename T>
00082 static inline range<T>
00083 conf(const std::string& key, const range<T>& default_value)
00084 {
00085 return get_conf<T>(LOBE_FORWARD, key, default_value) ;
00086 }
00087
00088
00089
00090 Forward::Forward()
00091 : base(clamp(conf("update_delay", 1000), 100, 2500),
00092 LOBE_FORWARD, conf<std::string>("geometry", "480 0 140 140")),
00093 m_min_distance(0, -1)
00094 {
00095 start(LOBE_FORWARD) ;
00096 }
00097
00098 void Forward::pre_run()
00099 {
00100 if (Params::adaptive_mode())
00101 {
00102 if (! App::lrf())
00103 throw behavior_error(LASER_RANGE_FINDER_MISSING) ;
00104 }
00105
00106 }
00107
00108
00109
00110
00111
00112
00113 void Forward::action()
00114 {
00115
00116
00117 TurnArbiter::Vote* T = new TurnArbiter::Vote(turn_vote_centered_at(0)) ;
00118 SpeedInfo S = Params::adaptive_mode() ? adaptive_speed() : fixed_speed() ;
00119
00120
00121
00122
00123
00124
00125 viz_lock() ;
00126 m_turn_vote = *T ;
00127 m_speed_vote = *S.second ;
00128 m_min_distance = S.first ;
00129 viz_unlock() ;
00130
00131
00132 TurnArbiter::instance().vote(base::name, T) ;
00133 SpeedArbiter::instance().vote(base::name, S.second) ;
00134 }
00135
00136
00137 Forward::SpeedInfo Forward::fixed_speed() const
00138 {
00139 return SpeedInfo(LRFData::Reading(0, -1),
00140 new SpeedArbiter::Vote(Params::cruising_speed(),
00141 Params::cruising_pwm())) ;
00142 }
00143
00144
00145 Forward::SpeedInfo Forward::adaptive_speed() const
00146 {
00147
00148 UpdateLock::begin_read() ;
00149 LRFData lrf(App::lrf()) ;
00150 UpdateLock::end_read() ;
00151
00152
00153
00154
00155
00156
00157
00158 const range<int> fov = Params::fov() ;
00159 const int step = Params::averaging_block() ;
00160
00161 LRFData::Reading min(std::numeric_limits<int>::min(),
00162 std::numeric_limits<int>::max()) ;
00163 for (int angle = fov.min(); angle <= fov.max(); angle += step)
00164 {
00165 const int m = clamp(angle - step/2, lrf.min_angle(), angle - 1) ;
00166 const int M = clamp(angle + step/2, angle + 1, lrf.max_angle()) ;
00167 const int d = round(lrf.average_distance(m, M)) ;
00168 if (d <= 0)
00169 continue ;
00170 if (d < min.distance())
00171 min = LRFData::Reading(angle, d) ;
00172 }
00173
00174
00175
00176 const range<float> S = Params::speed_range() ;
00177 const range<int> P = Params::pwm_range() ;
00178 const range<int> D = Params::distance_range() ;
00179
00180 if (min.distance() == std::numeric_limits<int>::max())
00181 return SpeedInfo(LRFData::Reading(180, -1),
00182 new SpeedArbiter::Vote(S.min(), P.min())) ;
00183
00184 const float d =
00185 static_cast<float>(min.distance() - D.min())/(D.max() - D.min()) ;
00186 const float s = clamp(S.min() + d * (S.max() - S.min()),
00187 S.min(), S.max()) ;
00188 const int p = clamp(P.min() + round(d * (P.max() - P.min())),
00189 P.min(), P.max()) ;
00190
00191
00192 return SpeedInfo(min, new SpeedArbiter::Vote(s, p)) ;
00193 }
00194
00195
00196
00197 #ifdef INVT_HAVE_LIBGL
00198
00199
00200 static std::string dist(int d)
00201 {
00202 std::ostringstream label ;
00203 label << d << " mm" ;
00204 return label.str() ;
00205 }
00206
00207
00208 static std::string speed(float s)
00209 {
00210 using namespace std ;
00211
00212 std::ostringstream label ;
00213 label << setw(6) << fixed << setprecision(3) << s << " m/s" ;
00214 return label.str() ;
00215 }
00216
00217 void Forward::render_me()
00218 {
00219
00220
00221 viz_lock() ;
00222 TurnArbiter::Vote V = m_turn_vote ;
00223 SpeedArbiter::Vote S = m_speed_vote ;
00224 LRFData::Reading D = m_min_distance ;
00225 viz_unlock() ;
00226
00227 unit_view_volume() ;
00228 glBegin(GL_LINES) ;
00229
00230 for (TurnArbiter::Vote::iterator it = V.begin(); it; ++it)
00231 {
00232 float vote = it.value() ;
00233 float direction = it.direction() ;
00234 if (vote < 0)
00235 {
00236 glColor3f(1, 0, 0) ;
00237 glVertex2i(0, 0) ;
00238 glVertex2f(-vote * cos(direction), -vote * sin(direction)) ;
00239 }
00240 else if (vote > 0)
00241 {
00242 glColor3f(0, 1, 0) ;
00243 glVertex2i(0, 0) ;
00244 glVertex2f(vote * cos(direction), vote * sin(direction)) ;
00245 }
00246 }
00247
00248
00249 if (Params::adaptive_mode()) {
00250 glColor3f(1, 1, 1) ;
00251 glVertex2i(0, 0) ;
00252 glVertex2f(0.75f * cos(D.angle()), 0.75f * sin(D.angle())) ;
00253 }
00254 glEnd() ;
00255
00256
00257
00258
00259 restore_view_volume() ;
00260 text_view_volume() ;
00261 glColor3f(0, 1, 1) ;
00262 draw_label(3, 12, "Forward") ;
00263 if (Params::adaptive_mode())
00264 draw_label(3, m_geometry.height - 16, dist(D.distance()).c_str()) ;
00265 draw_label(3, m_geometry.height - 4, speed(S.speed()).c_str()) ;
00266
00267 restore_view_volume() ;
00268 }
00269
00270 #endif
00271
00272
00273
00274 Forward::~Forward(){}
00275
00276
00277
00278
00279 Forward::Params::Params()
00280 : m_cruising_speed(clamp(conf("cruising_speed", 0.5f), 0.1f, 10.0f)),
00281 m_cruising_pwm(clamp(conf("cruising_pwm", 30), 15, 100)),
00282 m_adaptive_mode(conf("adaptive_mode", false)),
00283 m_fov(clamp(conf("fov", range<int>(-60, 60)), range<int>(-90, 90))),
00284 m_averaging_block(clamp(conf("averaging_block", 10), 1, 20)),
00285 m_speed_range(clamp(conf("speed_range", range<float>(0.1f, 0.5f)),
00286 range<float>(0.05f, 0.5f))),
00287 m_pwm_range(clamp(conf("pwm_range", range<int>(25, 100)),
00288 range<int>(10, 100))),
00289 m_distance_range(clamp(conf("distance_range", range<int>(250, 2500)),
00290 range<int>(100, 5000)))
00291 {}
00292
00293
00294 Forward::Params::~Params(){}
00295
00296
00297
00298 }
00299
00300
00301
00302
00303