naoqi_driver.hpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2015 Aldebaran
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  *
00016 */
00017 
00018 
00019 #ifndef NAOQI_DRIVER_HPP
00020 #define NAOQI_DRIVER_HPP
00021 
00022 #include <vector>
00023 #include <queue>
00024 
00025 /*
00026 * BOOST
00027 */
00028 #include <boost/property_tree/ptree.hpp>
00029 #include <boost/thread/thread.hpp>
00030 #include <boost/thread/mutex.hpp>
00031 #include <boost/scoped_ptr.hpp>
00032 
00033 /*
00034 * ALDEB
00035 */
00036 #include <qi/session.hpp>
00037 
00038 /*
00039 * PUBLIC INTERFACE
00040 */
00041 #include <naoqi_driver/converter/converter.hpp>
00042 #include <naoqi_driver/publisher/publisher.hpp>
00043 #include <naoqi_driver/subscriber/subscriber.hpp>
00044 #include <naoqi_driver/service/service.hpp>
00045 #include <naoqi_driver/recorder/recorder.hpp>
00046 #include <naoqi_driver/event/event.hpp>
00047 #include <naoqi_driver/recorder/globalrecorder.hpp>
00048 
00049 namespace tf2_ros
00050 {
00051   class Buffer;
00052 }
00053 
00054 namespace naoqi
00055 {
00056 
00057 namespace recorder
00058 {
00059   class GlobalRecorder;
00060 }
00065 class Driver
00066 {
00067 public:
00072   Driver( qi::SessionPtr session, const std::string& prefix );
00073 
00078   ~Driver();
00079 
00080   void init();
00081 
00082   void startRosLoop();
00083   void stopRosLoop();
00087   std::string minidump(const std::string& prefix);
00088   std::string minidumpConverters(const std::string& prefix, const std::vector<std::string>& names);
00089 
00090   void setBufferDuration(float duration);
00091   float getBufferDuration();
00092 
00097   void registerConverter( converter::Converter& conv );
00098 
00104   void registerPublisher( const std::string& conv_name, publisher::Publisher& pub);
00105 
00111   void registerRecorder(const std::string& conv_name, recorder::Recorder& rec, float frequency);
00112 
00116   void registerConverter(converter::Converter conv, publisher::Publisher pub, recorder::Recorder rec );
00117 
00121   void registerPublisher(converter::Converter conv, publisher::Publisher pub );
00122 
00126   void registerRecorder(converter::Converter conv, recorder::Recorder rec );
00127 
00131   bool registerMemoryConverter(const std::string& key, float frequency, const dataType::DataType& type );
00132 
00136   bool registerEventConverter(const std::string& key, const dataType::DataType& type);
00137 
00138 
00142   std::vector<std::string> getAvailableConverters();
00143 
00147   std::vector<std::string> getSubscribedPublishers() const;
00148 
00149   std::string _whoIsYourDaddy()
00150   {
00151     return "A sugar bear";
00152   }
00153 
00161   void registerSubscriber( subscriber::Subscriber sub );
00162 
00170   void registerService( service::Service srv );
00175   std::string getMasterURI() const;
00176 
00182   void setMasterURINet( const std::string& uri, const std::string& network_interface );
00183 
00188   void setMasterURI( const std::string& uri );
00189 
00193   void startPublishing();
00194 
00198   void stopPublishing();
00199 
00203   void startRecording();
00204 
00208   void startRecordingConverters(const std::vector<std::string>& names);
00209 
00213   std::string stopRecording();
00214 
00215   void startLogging();
00216 
00217   void stopLogging();
00218 
00222   void addMemoryConverters(std::string filepath);
00223 
00224   void parseJsonFile(std::string filepath, boost::property_tree::ptree& pt);
00225 
00226   void stopService();
00227 
00228   std::vector<std::string> getFilesList();
00229 
00230   void removeAllFiles();
00231 
00232   void removeFiles(std::vector<std::string> files);
00233 
00234 private:
00235   qi::SessionPtr sessionPtr_;
00236 
00237   const robot::Robot& robot_;
00238 
00239   bool publish_enabled_;
00240   bool record_enabled_;
00241   bool log_enabled_;
00242   bool keep_looping;
00243   bool has_stereo;
00244 
00245   const size_t freq_;
00246   boost::thread publisherThread_;
00247   //ros::Rate r_;
00248 
00249   boost::shared_ptr<recorder::GlobalRecorder> recorder_;
00250 
00251   /* boot config */
00252   boost::property_tree::ptree boot_config_;
00253   void loadBootConfig();
00254 
00255   void registerDefaultConverter();
00256   void registerDefaultSubscriber();
00257   void registerDefaultServices();
00258   void insertEventConverter(const std::string& key, event::Event event);
00259 
00260   template <typename T1, typename T2, typename T3>
00261   void _registerMemoryConverter( const std::string& key, float frequency ) {
00262     boost::shared_ptr<T1> mfp = boost::make_shared<T1>( key );
00263     boost::shared_ptr<T2> mfr = boost::make_shared<T2>( key );
00264     boost::shared_ptr<T3> mfc = boost::make_shared<T3>( key , frequency, sessionPtr_, key );
00265     mfc->registerCallback( message_actions::PUBLISH, boost::bind(&T1::publish, mfp, _1) );
00266     mfc->registerCallback( message_actions::RECORD, boost::bind(&T2::write, mfr, _1) );
00267     mfc->registerCallback( message_actions::LOG, boost::bind(&T2::bufferize, mfr, _1) );
00268     registerConverter( mfc, mfp, mfr );
00269   }
00270 
00271   void rosLoop();
00272 
00273   boost::scoped_ptr<ros::NodeHandle> nhPtr_;
00274   boost::mutex mutex_reinit_;
00275   boost::mutex mutex_conv_queue_;
00276   boost::mutex mutex_record_;
00277 
00278   std::vector< converter::Converter > converters_;
00279   std::map< std::string, publisher::Publisher > pub_map_;
00280   std::map< std::string, recorder::Recorder > rec_map_;
00281   std::map< std::string, event::Event > event_map_;
00282   typedef std::map< std::string, publisher::Publisher>::const_iterator PubConstIter;
00283   typedef std::map< std::string, publisher::Publisher>::iterator PubIter;
00284   typedef std::map< std::string, recorder::Recorder>::const_iterator RecConstIter;
00285   typedef std::map< std::string, recorder::Recorder>::iterator RecIter;
00286   typedef std::map< std::string, event::Event>::const_iterator EventConstIter;
00287   typedef std::map< std::string, event::Event>::iterator EventIter;
00288 
00289   std::vector< subscriber::Subscriber > subscribers_;
00290   std::vector< service::Service > services_;
00291 
00292   float buffer_duration_;
00293 
00295   struct ScheduledConverter {
00296     ScheduledConverter(const ros::Time& schedule, size_t conv_index) :
00297        schedule_(schedule), conv_index_(conv_index)
00298     {
00299     }
00300 
00301     bool operator < (const ScheduledConverter& sp_in) const {
00302       return schedule_ > sp_in.schedule_;
00303     }
00305     ros::Time schedule_;
00307     size_t conv_index_;
00308   };
00309 
00311   std::priority_queue<ScheduledConverter> conv_queue_;
00312 
00316   boost::shared_ptr<tf2_ros::Buffer> tf2_buffer_;
00317 };
00318 
00319 } // naoqi
00320 
00321 #endif


naoqi_driver
Author(s): Karsten Knese
autogenerated on Tue Jul 9 2019 03:21:56