MAV'RIC
/home/travis/build/lis-epfl/MAVRIC_Library/communication/periodic_telemetry.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 periodic_telemetry.hpp
00034  *
00035  * \author MAV'RIC Team
00036  * \author Julien Lecoeur
00037  *
00038  * \brief Periodic telemetry
00039  *
00040  ******************************************************************************/
00041 
00042 
00043 #ifndef PERIODIC_TELEMETRY_HPP_
00044 #define PERIODIC_TELEMETRY_HPP_
00045 
00046 #include <cstdbool>
00047 
00048 #include "communication/mavlink_stream.hpp"
00049 #include "communication/mavlink_message_handler.hpp"
00050 #include "runtime/scheduler.hpp"
00051 
00052 class Periodic_telemetry
00053 {
00054 public:
00055 
00059     struct conf_t
00060     {
00061         Scheduler::conf_t  scheduler_config;   
00062     };
00063 
00064 
00070     static inline conf_t default_config(void);
00071 
00072 
00082     template<typename T>
00083     struct function
00084     {
00085         typedef void (*type_t)(const T*, const Mavlink_stream*, mavlink_message_t*);
00086     };
00087 
00088 
00096     Periodic_telemetry(Mavlink_stream& mavlink_stream, Mavlink_message_handler& handler, conf_t config = default_config());
00097 
00098 
00102     bool update(void);
00103 
00104 
00120     template<typename T>
00121     bool add(   uint32_t                        task_id,
00122                 uint32_t                        repeat_period,
00123                 typename function<T>::type_t    telemetry_function,
00124                 T*                              telemetry_module,
00125                 Scheduler_task::priority_t      priority    = Scheduler_task::PRIORITY_NORMAL,
00126                 Scheduler_task::timing_mode_t   timing_mode = Scheduler_task::PERIODIC_RELATIVE,
00127                 Scheduler_task::run_mode_t      run_mode    = Scheduler_task::RUN_REGULAR);
00128 
00129 
00135     bool sort(void);
00136 
00137 
00145     static void toggle_telemetry_stream(Periodic_telemetry* scheduler, uint32_t sysid, const mavlink_message_t* msg);
00146 
00147 
00148 protected:
00149 
00153     struct telemetry_entry_t
00154     {
00155         function<void>::type_t  function;           
00156         void*                   module;             
00157         Mavlink_stream*         mavlink_stream;     
00158     };
00159 
00166     virtual uint32_t max_count(void) = 0;
00167 
00168 
00175     virtual Scheduler& scheduler(void) = 0;
00176 
00177 
00184     virtual telemetry_entry_t* list(void) = 0;
00185 
00186 
00187 private:
00188 
00189     Mavlink_stream&             mavlink_stream_;                    
00190     uint32_t                    count_;                             
00191 
00199     static bool send_message(telemetry_entry_t* telemetry_entry);
00200 };
00201 
00202 
00203 template<uint32_t N = 10>
00204 class Periodic_telemetry_T: public Periodic_telemetry
00205 {
00206 public:
00207 
00214     Periodic_telemetry_T(Mavlink_stream& mavlink_stream, Mavlink_message_handler& handler, conf_t config):
00215         Periodic_telemetry(mavlink_stream, handler, config),
00216         scheduler_(config.scheduler_config)
00217     {};
00218 
00219 protected:
00220 
00226     uint32_t max_count(void)
00227     {
00228         return N;
00229     }
00230 
00237     Scheduler& scheduler(void)
00238     {
00239         return scheduler_;
00240     }
00241 
00248     telemetry_entry_t* list(void)
00249     {
00250         return list_;
00251     }
00252 
00253 private:
00254     Scheduler_T<N>     scheduler_;          
00255     telemetry_entry_t    list_[N];            
00256 };
00257 
00258 
00259 
00260 Periodic_telemetry::conf_t Periodic_telemetry::default_config(void)
00261 {
00262     Periodic_telemetry::conf_t conf  = {};
00263 
00264     conf.scheduler_config.schedule_strategy = Scheduler::ROUND_ROBIN;
00265     conf.scheduler_config.debug             = false;
00266 
00267     return conf;
00268 };
00269 
00270 #include "periodic_telemetry.hxx"
00271 
00272 #endif /* PERIODIC_TELEMETRY_HPP_ */
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines