LograbMain.C
Go to the documentation of this file.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 #include "Robots/LoBot/ui/LoMainWindow.H"
00045
00046 #include "Robots/LoBot/io/LoCompositor.H"
00047 #include "Robots/LoBot/io/LoVideoRecorder.H"
00048 #include "Robots/LoBot/io/LoVideoStream.H"
00049 #include "Robots/LoBot/io/LoFireWireBus.H"
00050
00051 #include "Robots/LoBot/config/LoConfigHelpers.H"
00052 #include "Robots/LoBot/config/LoCommonOpts.H"
00053 #include "Robots/LoBot/config/LoDefaults.H"
00054
00055 #include "Robots/LoBot/thread/LoShutdown.H"
00056 #include "Robots/LoBot/thread/LoThread.H"
00057
00058 #include "Robots/LoBot/misc/LoExcept.H"
00059 #include "Robots/LoBot/misc/LoTypes.H"
00060 #include "Robots/LoBot/util/LoSTL.H"
00061
00062
00063 #include "Image/Dims.H"
00064
00065
00066 #include "Component/ModelManager.H"
00067 #include "Component/ModelParam.H"
00068
00069
00070 #include "Util/log.H"
00071
00072
00073 #include <unistd.h>
00074
00075
00076 #include <sstream>
00077 #include <algorithm>
00078 #include <iterator>
00079 #include <list>
00080 #include <vector>
00081
00082
00083
00084
00085
00086 namespace lobot {
00087
00088 struct LoApp {
00089 LoApp() ;
00090 void parse_command_line(int argc, const char* argv[]) ;
00091 void run() ;
00092 ~LoApp() ;
00093
00094
00095 typedef Compositor<PixelType> ImageCompositor ;
00096 typedef std::vector<VideoStream*> VideoStreams ;
00097 typedef std::vector<VideoRecorder*> VideoRecorders ;
00098 typedef std::list<Drawable*> Drawables ;
00099
00100
00101 std::string config_file() const {return m_conf_option.getVal() ;}
00102
00103 private:
00104 VideoStreams m_video_streams ;
00105 VideoRecorders m_video_recorders ;
00106 ImageCompositor m_compositor ;
00107 Drawables m_drawables ;
00108 ModelManager m_model_manager ;
00109
00110
00111 OModelParam<std::string> m_conf_option ;
00112 } ;
00113
00114 }
00115
00116
00117
00118 int main(int argc, const char* argv[])
00119 {
00120 MYLOGVERB = LOG_ERR ;
00121 try
00122 {
00123 lobot::LoApp app ;
00124 app.parse_command_line(argc, argv) ;
00125 app.run() ;
00126 }
00127 catch (lobot::uhoh& e)
00128 {
00129 LERROR("%s", e.what()) ;
00130 return e.code() ;
00131 }
00132 catch (std::exception& e)
00133 {
00134 LERROR("%s", e.what()) ;
00135 return 255 ;
00136 }
00137 return 0 ;
00138 }
00139
00140
00141
00142 namespace lobot {
00143
00144
00145
00146 Dims grab_size() ;
00147 float grab_rate() ;
00148 bool recording_enabled() ;
00149 std::string recording_stem() ;
00150 bool show_ui() ;
00151
00152
00153
00154
00155 LoApp::LoApp()
00156 : m_model_manager("lograb"),
00157 m_conf_option(& OPT_ConfigFile, & m_model_manager)
00158 {
00159 Shutdown::start_listening() ;
00160 }
00161
00162
00163
00164 static void create_video_streams(const Dims& resolution, float frame_rate,
00165 LoApp::VideoStreams* V)
00166 {
00167 const int n = FireWireBus::instance().num_cameras() ;
00168 V->reserve(n) ;
00169 for (int i = 0; i < n; ++i)
00170 V->push_back(new VideoStream(i, resolution, frame_rate)) ;
00171
00172
00173
00174 FireWireBus::instance().release_camera_nodes() ;
00175 }
00176
00177
00178
00179 static void create_video_recorders(const LoApp::VideoStreams& V,
00180 const std::string& mpeg_name_root,
00181 LoApp::VideoRecorders* R)
00182 {
00183 const int n = V.size() ;
00184 R->reserve(n) ;
00185 for (int i = 0; i < n; ++i)
00186 R->push_back(new VideoRecorder(mpeg_name_root + to_string(i), V[i])) ;
00187 }
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207 void LoApp::parse_command_line(int argc, const char* argv[])
00208 {
00209 std::string default_config_file = getenv("HOME") ;
00210 default_config_file += "/" ;
00211 default_config_file += LOBOT_DEFAULT_CONFIG_FILE_NAME ;
00212 m_model_manager.setOptionValString(& OPT_ConfigFile,
00213 default_config_file.c_str()) ;
00214
00215 if (! m_model_manager.parseCommandLine(argc, argv, "", 0, 0))
00216 throw customization_error(BAD_OPTION) ;
00217 }
00218
00219
00220
00221
00222
00223
00224
00225 class ModelManagerStarter {
00226 ModelManager& mgr ;
00227 public :
00228 ModelManagerStarter(ModelManager& m) : mgr(m) {mgr.start() ;}
00229 ~ModelManagerStarter() {mgr.stop() ;}
00230 } ;
00231
00232
00233 void LoApp::run()
00234 {
00235 ModelManagerStarter M(m_model_manager) ;
00236
00237
00238 try
00239 {
00240 Configuration::load(config_file()) ;
00241
00242 }
00243 catch (customization_error& e)
00244 {
00245 LERROR("%s", e.what()) ;
00246 }
00247
00248
00249 create_video_streams(grab_size(), grab_rate(), & m_video_streams) ;
00250 connect(m_video_streams, m_compositor) ;
00251 if (recording_enabled())
00252 create_video_recorders(m_video_streams, recording_stem() + "-",
00253 & m_video_recorders) ;
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271 while (! Shutdown::signaled())
00272 {
00273 std::for_each(m_video_streams.begin(), m_video_streams.end(),
00274 std::mem_fun(& VideoStream::update)) ;
00275 std::for_each(m_video_recorders.begin(), m_video_recorders.end(),
00276 std::mem_fun(& VideoRecorder::update)) ;
00277 m_compositor.update() ;
00278
00279
00280
00281
00282
00283
00284
00285
00286 usleep(15000) ;
00287 }
00288 }
00289
00290
00291
00292 LoApp::~LoApp()
00293 {
00294 purge_container(m_drawables) ;
00295 purge_container(m_video_recorders) ;
00296 purge_container(m_video_streams) ;
00297 }
00298
00299
00300
00301 Dims grab_size()
00302 {
00303 try
00304 {
00305 Dims d ;
00306 convertFromString(video_conf<std::string>("grab_size"), d) ;
00307 return d ;
00308 }
00309 catch (std::exception&)
00310 {
00311 return LOBOT_DEFAULT_GRAB_SIZE ;
00312 }
00313 }
00314
00315 float grab_rate()
00316 {
00317 return video_conf("grab_rate", LOBOT_DEFAULT_GRAB_RATE) ;
00318 }
00319
00320 bool recording_enabled()
00321 {
00322 return recording_stem() != "" ;
00323 }
00324
00325 std::string recording_stem()
00326 {
00327 return video_conf<std::string>("record") ;
00328 }
00329
00330 bool show_ui()
00331 {
00332 return ui_conf("show_ui", true) ;
00333 }
00334
00335
00336
00337 }
00338
00339
00340
00341
00342