Go to the documentation of this file.00001
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
00026
00027
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
00046 bool rt_loop_not_making_timing;
00047 double halt_rt_loop_frequency;
00048 double rt_loop_frequency;
00049 };
00050
00051
00052 class ControllerStatus {
00053 public:
00054 ControllerStatus(){}
00055 virtual ~ControllerStatus(){}
00056 virtual bool isHealthy(){return true;}
00057 };
00058 typedef boost::shared_ptr<ControllerStatus> ControllerStatusPtr;
00059
00060 class OpenController {
00061 public:
00062 OpenController();
00063 virtual ~OpenController();
00064 virtual ControllerStatusPtr updateJoints(struct timespec*) = 0;
00065 virtual ControllerStatusPtr recoverController() = 0;
00066 virtual void finalizeHW() = 0;
00067
00068 static bool initRT();
00069
00070 void setAllowUnprogrammedP(bool val) {
00071 allow_unprogrammed_p_ = val;
00072 }
00073
00074 void setStatsPublishP(bool val) {
00075 stats_publish_p_ = val;
00076 }
00077
00078 void setRobotXMLFile(std::string val) {
00079 robot_xml_file_ = val;
00080 }
00081
00082 bool setDryRun(bool _dryrun) {
00083 dryrunp_ = _dryrun;
00084 return _dryrun;
00085 }
00086
00087 void start();
00088
00089 int waitThreadJoin() {
00090 return 0;
00091 }
00092
00093
00094 virtual void quitRequest();
00095
00096 bool resetMotorsService(std_srvs::Empty::Request &req,
00097 std_srvs::Empty::Response &resp);
00098 bool haltMotorsService(std_srvs::Empty::Request &req,
00099 std_srvs::Empty::Response &resp);
00100 bool publishTraceService(std_srvs::Empty::Request &req,
00101 std_srvs::Empty::Response &resp);
00102
00103
00104 void initialize();
00105 void loadRobotDescription();
00106 void startMain();
00107 void finalize();
00108
00109 void diagnosticLoop();
00110
00111 int setupPidFile ();
00112 void cleanupPidFile ();
00113 void parseArguments(int argc, char** argv);
00114 void Usage(std::string msg = "");
00115 protected:
00116 int lock_fd(int fd);
00117 virtual void initializeROS(ros::NodeHandle& node) = 0;
00118 virtual void initializeCM() = 0;
00119 virtual void initializeHW() = 0;
00120
00121 void publishDiagnostics();
00122 double now();
00123 void timespecInc(struct timespec &tick, int nsec);
00124 double publishJitter(double start);
00125
00126 std::string piddir_;
00127 std::string pidfile_;
00128 bool dryrunp_;
00129 bool not_sleep_clock_;
00130 bool allow_unprogrammed_p_;
00131 bool stats_publish_p_;
00132 bool g_reset_motors_;
00133 bool g_quit_;;
00134 bool g_halt_requested_;
00135 bool g_publish_trace_requested_;
00136 std::string robot_xml_file_;
00137 double min_acceptable_rt_loop_frequency_;
00138 double period_;
00139 std::string g_robot_desc_;
00140 pr2_hardware_interface::HardwareInterface* hw_;
00141 boost::shared_ptr<pr2_controller_manager::ControllerManager> cm_;
00142 boost::shared_ptr<realtime_tools::RealtimePublisher<std_msgs::Float64> > rtpublisher_;
00143 boost::shared_ptr<realtime_tools::RealtimePublisher<diagnostic_msgs::DiagnosticArray> > publisher_;
00144 Stat g_stats_;
00145
00146 ros::ServiceServer reset_service_;
00147 ros::ServiceServer halt_service_;
00148 ros::ServiceServer publishTrace_service_;
00149
00150 private:
00151 };
00152
00153 class RTLoopHistory {
00154 public:
00155 RTLoopHistory(unsigned length, double default_value) :
00156 index_(0),
00157 length_(length),
00158 history_(new double[length]) {
00159 for (unsigned i=0; i<length_; ++i)
00160 history_[i] = default_value;
00161 }
00162
00163 ~RTLoopHistory() {
00164 delete[] history_;
00165 history_ = NULL;
00166 }
00167
00168 void sample(double value) {
00169 index_ = (index_+1) % length_;
00170 history_[index_] = value;
00171 }
00172
00173 double average() const {
00174 double sum(0.0);
00175 for (unsigned i=0; i<length_; ++i)
00176 sum+=history_[i];
00177 return sum / double(length_);
00178 }
00179
00180 protected:
00181 unsigned index_;
00182 unsigned length_;
00183 double *history_;
00184 };
00185
00186 class Finalizer {
00187 protected:
00188 OpenController* controller;
00189 public:
00190 Finalizer(OpenController* controller_): controller(controller_) { };
00191 virtual ~Finalizer();
00192 };
00193
00194 }
00195 #endif