controller_timed_pomdp.cpp
Go to the documentation of this file.
00001 
00025 #include <madp/QAV.h>
00026 #include <madp/PerseusPOMDPPlanner.h>
00027 #include <madp/JointBelief.h>
00028 #include <madp/JointBeliefSparse.h>
00029 #include <madp/NullPlanner.h>
00030 
00031 #include <mdm_library/controller_timed_pomdp.h>
00032 
00033 
00034 
00035 using namespace std;
00036 using namespace mdm_library;
00037 
00038 
00039 
00040 ControllerTimedPOMDP::
00041 ControllerTimedPOMDP ( string const& problem_file,
00042                        string const& value_function_file,
00043                        const CONTROLLER_STATUS initial_status ) :
00044     ControllerPOMDP ( problem_file, initial_status ),
00045     o_ ( 0 )
00046 {
00047     if ( !nh_.hasParam ( "decision_period" ) )
00048     {
00049         ROS_ERROR ( "Decision period was not specified. Synchronous MDP controllers require the 'decision_period' parameter." );
00050         abort();
00051     }
00052 
00053     try
00054     {
00055         timer_ = nh_.createTimer ( 0, &ControllerTimedPOMDP::timerCallback, this, true, false );
00056         scheduleTimer();
00057 
00058         bool isSparse;
00059         if ( !nh_.getParam ( "is_sparse", isSparse ) )
00060         {
00061             isSparse = false;
00062         }
00063 
00064 
00065         boost::shared_ptr<PlanningUnitDecPOMDPDiscrete> np ( new NullPlanner ( loader_->GetDecPOMDP().get() ) );
00066         Q_ = boost::shared_ptr<QAV<PerseusPOMDPPlanner> >
00067              ( new QAV<PerseusPOMDPPlanner> ( np,
00068                                               value_function_file ) );
00069 
00070         if ( isSparse )
00071             belief_ = boost::shared_ptr<JointBeliefSparse>
00072                       ( new JointBeliefSparse ( loader_->GetDecPOMDP()->GetNrStates() ) );
00073         else
00074             belief_ = boost::shared_ptr<JointBelief>
00075                       ( new JointBelief ( loader_->GetDecPOMDP()->GetNrStates() ) );
00076         if ( initial_status == STARTED )
00077         {
00078             startController();
00079         }
00080     }
00081     catch ( E& e )
00082     {
00083         e.Print();
00084         abort();
00085     }
00086 }
00087 
00088 
00089 
00090 void
00091 ControllerTimedPOMDP::
00092 observationCallback ( const WorldSymbolConstPtr& msg )
00093 {
00094     o_ = msg->world_symbol;
00095 }
00096 
00097 
00098 
00099 void
00100 ControllerTimedPOMDP::
00101 scheduleTimer()
00102 {
00103     double dp;
00104     nh_.getParam ( "decision_period", dp );
00105 
00106     double timeToWait = dp - fmod ( ros::Time::now().toSec(), dp );
00107     ros::Duration d ( timeToWait );
00108 
00109     timer_.stop();
00110     timer_.setPeriod ( d );
00111     timer_.start();
00112 }
00113 
00114 
00115 
00116 void
00117 ControllerTimedPOMDP::
00118 timerCallback ( const ros::TimerEvent& timerEvent )
00119 {
00120     step();
00121     scheduleTimer();
00122 }
00123 
00124 
00125 
00126 void
00127 ControllerTimedPOMDP::
00128 step()
00129 {
00130     act ( o_ );
00131 }
00132 
00133 
00134 
00135 void
00136 ControllerTimedPOMDP::
00137 startController ()
00138 {
00139     if ( belief_ == 0 )
00140     {
00141         ROS_WARN ( "ControllerTimedPOMDP:: Attempted to start controller, but the belief state hasn't been initialized." );
00142         return;
00143     }
00144     if ( ISD_ != 0 )
00145     {
00146         belief_->Set ( ISD_->ToVectorOfDoubles() );
00147     }
00148     else
00149     {
00150         belief_->Set ( * ( loader_->GetDecPOMDP()->GetISD() ) );
00151     }
00152     setStatus ( STARTED );
00153     resetDecisionEpisode();
00154 }


mdm_library
Author(s): Joao Messias
autogenerated on Wed Aug 26 2015 12:28:41