open_controllers_interface.h
Go to the documentation of this file.
00001 // -*- mode: c++ -*-
00002 #ifndef __OPEN_CONTROLLERS_INTERFACE_H__
00003 #define __OPEN_CONTROLLERS_INTERFACE_H__
00004 #include <iostream>
00005 #include "ros/ros.h"
00006 #include <std_srvs/Empty.h>
00007 #include <boost/shared_ptr.hpp>
00008 #include <boost/accumulators/accumulators.hpp>
00009 #include <boost/accumulators/statistics/stats.hpp>
00010 #include <boost/accumulators/statistics/max.hpp>
00011 #include <boost/accumulators/statistics/mean.hpp>
00012 #include <boost/thread.hpp>
00013 #include <diagnostic_updater/DiagnosticStatusWrapper.h>
00014 #include <pr2_controller_manager/controller_manager.h>
00015 #include <diagnostic_msgs/DiagnosticArray.h>
00016 
00017 #include <ros/ros.h>
00018 #include <std_srvs/Empty.h>
00019 
00020 #include <realtime_tools/realtime_publisher.h>
00021 #include <std_msgs/Float64.h>
00022 
00023 
00024 namespace OpenControllersInterface {
00025   // if groovy
00026   //typedef std::vector<pr2_mechanism_model::Transmission>::iterator TransmissionIterator;
00027   // if hydro
00028   typedef std::vector<boost::shared_ptr<pr2_mechanism_model::Transmission> >::iterator TransmissionIterator;
00029   //
00030   typedef boost::accumulators::accumulator_set<double, boost::accumulators::stats<boost::accumulators::tag::max, boost::accumulators::tag::mean> > DoubleAccumulatorSet;
00031   struct Stat {
00032     DoubleAccumulatorSet ec_acc;
00033     DoubleAccumulatorSet cm_acc;
00034     DoubleAccumulatorSet loop_acc;
00035     DoubleAccumulatorSet jitter_acc;
00036     int overruns;
00037     int recent_overruns;
00038     int last_overrun;
00039     int last_severe_overrun;
00040     unsigned long loop_count;
00041     double overrun_loop_sec;
00042     double overrun_ec;
00043     double overrun_cm;
00044     
00045     // These values are set when realtime loop does not meet performace expections
00046     bool rt_loop_not_making_timing; 
00047     double halt_rt_loop_frequency;
00048     double rt_loop_frequency;
00049   };
00050 
00051 
00052   
00053   class OpenController {
00054   public:
00055     OpenController();
00056     virtual ~OpenController();
00057     virtual bool updateJoints(struct timespec*) = 0;
00058     virtual void finalizeHW() = 0;
00059 
00060     static bool initRT();
00061     
00062     void setAllowUnprogrammedP(bool val) {
00063       allow_unprogrammed_p = val;
00064     }
00065     
00066     void setStatsPublishP(bool val) {
00067       stats_publish_p = val;
00068     }
00069     
00070     void setRobotXMLFile(std::string val) {
00071       robot_xml_file = val;
00072     }
00073 
00074     bool setDryRun(bool _dryrun) {
00075       dryrunp = _dryrun;
00076       return _dryrun;
00077     }
00078 
00079     void start();
00080 
00081     int waitThreadJoin() {
00082       return 0;
00083     }
00084 
00085     // calling haltMotorsService
00086     virtual void quitRequest();
00087 
00088     bool resetMotorsService(std_srvs::Empty::Request &req,
00089           std_srvs::Empty::Response &resp);
00090     bool haltMotorsService(std_srvs::Empty::Request &req,
00091          std_srvs::Empty::Response &resp);
00092     bool publishTraceService(std_srvs::Empty::Request &req,
00093            std_srvs::Empty::Response &resp);
00094     
00095     // virtual function you need to impelement
00096     void initialize();
00097     void startMain();
00098     void finalize();
00099 
00100     void diagnosticLoop();
00101 
00102     int setupPidFile ();
00103     void cleanupPidFile ();
00104     void parseArguments(int argc, char** argv);
00105     void Usage(std::string msg = "");
00106   protected:
00107     int lock_fd(int fd);
00108     virtual void initializeROS(ros::NodeHandle& node) = 0;
00109     virtual void initializeHW() = 0;
00110     
00111     void publishDiagnostics();
00112     double now();
00113     void timespecInc(struct timespec &tick, int nsec);
00114     double publishJitter(double start);
00115 
00116     std::string piddir;
00117     std::string pidfile;
00118     bool dryrunp;
00119     bool not_sleep_clock;
00120     bool allow_unprogrammed_p;
00121     bool stats_publish_p;
00122     bool initialized_p;
00123     bool g_reset_motors;
00124     bool g_quit;
00125     bool g_halt_requested;
00126     bool g_halt_motors;
00127     bool g_publish_trace_requested;
00128     std::string robot_xml_file;
00129     double min_acceptable_rt_loop_frequency;
00130     double period;
00131     std::string g_robot_desc;
00132     pr2_hardware_interface::HardwareInterface* hw;
00133     boost::shared_ptr<pr2_controller_manager::ControllerManager> cm;
00134     realtime_tools::RealtimePublisher<std_msgs::Float64> *rtpublisher;
00135     realtime_tools::RealtimePublisher<diagnostic_msgs::DiagnosticArray> *publisher;
00136     Stat g_stats;
00137 
00138     ros::ServiceServer reset_service;
00139     ros::ServiceServer halt_service;
00140     ros::ServiceServer publishTrace_service;
00141     
00142   private:
00143   };
00144 
00145   class RTLoopHistory {
00146   public:
00147     RTLoopHistory(unsigned length, double default_value) :
00148       index_(0), 
00149       length_(length),
00150       history_(new double[length]) {
00151       for (unsigned i=0; i<length_; ++i) 
00152         history_[i] = default_value;
00153     }
00154 
00155     ~RTLoopHistory() {
00156       delete[] history_;
00157       history_ = NULL;
00158     }
00159   
00160     void sample(double value) {
00161       index_ = (index_+1) % length_;
00162       history_[index_] = value;
00163     }
00164 
00165     double average() const {
00166       double sum(0.0);
00167       for (unsigned i=0; i<length_; ++i) 
00168         sum+=history_[i];
00169       return sum / double(length_);
00170     }
00171 
00172   protected:
00173     unsigned index_;
00174     unsigned length_;
00175     double *history_;
00176   };
00177 
00178   class Finalizer {
00179   protected:
00180     OpenController* controller;
00181   public:
00182     Finalizer(OpenController* controller_): controller(controller_) { };
00183     virtual ~Finalizer();
00184   };
00185 
00186 }
00187 #endif


open_controllers_interface
Author(s): Ryohei Ueda , Kei Okada
autogenerated on Thu Aug 27 2015 14:13:45