MAV'RIC
/home/travis/build/lis-epfl/MAVRIC_Library/communication/data_logging.hpp
00001 /*******************************************************************************
00002  * Copyright (c) 2009-2016, MAV'RIC Development Team
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  * 1. Redistributions of source code must retain the above copyright notice,
00009  * this list of conditions and the following disclaimer.
00010  *
00011  * 2. Redistributions in binary form must reproduce the above copyright notice,
00012  * this list of conditions and the following disclaimer in the documentation
00013  * and/or other materials provided with the distribution.
00014  *
00015  * 3. Neither the name of the copyright holder nor the names of its contributors
00016  * may be used to endorse or promote products derived from this software without
00017  * specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00020  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00021  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00022  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00023  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00024  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00025  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00026  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00027  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00028  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00029  * POSSIBILITY OF SUCH DAMAGE.
00030  ******************************************************************************/
00031 
00032 /*******************************************************************************
00033  * \file data_logging.hpp
00034  *
00035  * \author MAV'RIC Team
00036  * \author Nicolas Dousse
00037  *
00038  * \brief Performs the data logging on a file
00039  *
00040  ******************************************************************************/
00041 
00042 
00043 #ifndef DATA_LOGGING_HPP__
00044 #define DATA_LOGGING_HPP__
00045 
00046 #include "status/state.hpp"
00047 #include "hal/common/file.hpp"
00048 #include "hal/common/console.hpp"
00049 
00053 typedef struct
00054 {
00055     const double* param;                                              
00056     char param_name[MAVLINK_MSG_PARAM_SET_FIELD_PARAM_ID_LEN];  
00057     mavlink_message_type_t data_type;                           
00058     uint8_t precision;                                          
00059 } data_logging_entry_t;
00060 
00061 
00062 
00063 
00064 
00071 class Data_logging
00072 {
00073 public:
00077     typedef struct
00078     {
00079         uint16_t max_logs;                          
00080         bool debug;                                 
00081         uint32_t log_data;                          
00082         bool continuous_write;                      
00083     } conf_t;
00084 
00090     static conf_t default_config()
00091     {
00092         conf_t conf    = {};
00093 
00094         conf.max_logs               = 500;
00095         conf.debug                  = true;
00096         conf.log_data               = 0;     // 1: log data, 0: no log data
00097         conf.continuous_write       = false;
00098         return conf;
00099     };
00100 
00104     Data_logging(File& file, State& state, conf_t config = default_config());
00105 
00114     bool create_new_log_file(const char* file_name_, uint32_t sysid);
00115 
00123     bool update();
00124 
00125 
00133     bool start(void);
00134 
00135 
00143     bool stop(void);
00144 
00145 
00157     template<typename T>
00158     bool add_field(const T* val, const char* param_name);
00159 
00160 
00173     template<typename T>
00174     bool add_field(const T* val, const char* param_name, uint32_t precision);
00175 
00176 
00177 protected:
00178 
00186     virtual uint32_t max_count(void) = 0;
00187 
00188 
00196     virtual data_logging_entry_t* list() = 0;
00197 
00198 
00199 private:
00200 
00201     static const uint8_t MAX_FILENAME_LENGTH = 255;    
00202 
00208     void add_header_name(void);
00209 
00210 
00217     void write_separator(uint16_t param_num);
00218 
00219 
00225     void log_parameters(void);
00226 
00227 
00233     void seek(void);
00234 
00235 
00248     bool filename_append_extension(char* output, char* filename, uint32_t length);
00249 
00250 
00264     bool filename_append_int(char* output, char* filename, uint32_t num, uint32_t length);
00265 
00266 
00274     bool open_new_log_file(void);
00275 
00276 
00284     bool checksum_control(void);
00285 
00286     conf_t config_;                
00287 
00288     bool debug_;                                 
00289     uint32_t data_logging_count_;               
00290 
00291     char file_name_[MAX_FILENAME_LENGTH];                        
00292     char name_n_extension_[MAX_FILENAME_LENGTH];                 
00293 
00294     bool file_init_;                             
00295     bool file_opened_;                           
00296     bool sys_status_;                            
00297 
00298     uint32_t log_data_;                          
00299 
00300     uint32_t logging_time_;                      
00301 
00302     double cksum_a_;                             
00303     double cksum_b_;                             
00304 
00305     uint32_t sys_id_;                            
00306 
00307     Console<File> console_;                      
00308 
00309     State& state_;                              
00310 };
00311 
00312 
00318 template<uint32_t N>
00319 class Data_logging_T: public Data_logging
00320 {
00321 public:
00325     Data_logging_T(File& file, State& state, conf_t config = default_config()):
00326         Data_logging(file, state, config)
00327     {}
00328 
00329 protected:
00330 
00336     uint32_t max_count(void)
00337     {
00338         return N;
00339     }
00340 
00346     data_logging_entry_t* list()
00347     {
00348         return list_;
00349     }
00350 
00351 private:
00352     data_logging_entry_t list_[N];            
00353 };
00354 
00355 
00356 static inline bool task_data_logging_update(Data_logging* data_logging)
00357 {
00358     return data_logging->update();
00359 }
00360 
00361 #endif /* DATA_LOGGING_HPP__ */
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines