observation_layer.cpp
Go to the documentation of this file.
00001 
00026 #include <mdm_library/observation_layer.h>
00027 #include <mdm_library/WorldSymbol.h>
00028 
00029 
00030 
00031 using namespace std;
00032 using namespace predicate_manager;
00033 using namespace mdm_library;
00034 
00035 
00036 
00037 ObservationLayer::
00038 ObservationLayer() :
00039     event_map_sub_ ( nh_.subscribe ( "/event_maps", 1, &ObservationLayer::eventMapCallback, this ) ),
00040     event_update_sub_ ( nh_.subscribe ( "/event_updates", 1, &ObservationLayer::eventUpdatesCallback, this ) ),
00041     observation_metadata_sub_ ( nh_.subscribe ( "observation_metadata", 1, &ObservationLayer::observationMetadataCallback, this ) ),
00042     observation_pub_ ( nh_.advertise<WorldSymbol> ( "observation", 1, true ) )
00043 {}
00044 
00045 
00046 
00047 void
00048 ObservationLayer::
00049 eventUpdatesCallback ( const EventUpdateConstPtr& msg )
00050 {
00051     bool new_observation = false;
00052 
00053     foreach ( EventUpdate::_events_type::value_type event_id, msg->events )
00054     {
00055         NrID ev_nr_id ( msg->pm_id, event_id );
00056         if ( event_nr_observer_.count ( ev_nr_id ) )
00057         {
00058             ObsValuePair value_pair = event_nr_observer_[ev_nr_id];
00059             factored_observations_[value_pair.first] = value_pair.second;
00060             new_observation = true;
00061         }
00062     }
00063 
00064     if ( new_observation )
00065     {
00066         publishJointObservation();
00067     }
00068 }
00069 
00070 
00071 
00072 void
00073 ObservationLayer::
00074 eventMapCallback ( const EventInfoMapConstPtr& msg )
00075 {
00076     foreach ( EventInfo e, msg->map )
00077     {
00078         NameID ev_name_id ( msg->pm_id, e.name );
00079         NrID ev_nr_id ( msg->pm_id, e.nr );
00080 
00081         if ( !event_nr_observer_.count ( ev_nr_id ) )
00082         {
00083             if ( event_named_local_observer_.count ( ev_name_id ) )
00084                 event_nr_observer_[ev_nr_id] = event_named_local_observer_[ev_name_id];
00085             else if ( event_named_global_observer_.count ( e.name ) )
00086                 event_nr_observer_[ev_nr_id] = event_named_global_observer_[e.name];
00087         }
00088     }
00089 }
00090 
00091 
00092 
00093 void
00094 ObservationLayer::
00095 observationMetadataCallback ( const FactoredSymbolMetadataConstPtr& msg )
00096 {
00097     if ( msg->factors.size() != factored_observations_.size() )
00098     {
00099         ROS_WARN_STREAM ( "ObservationLayer:: Metadata check failed. The number of declared observation factors "
00100                           << " (" << factored_observations_.size() << ") is different from the number of expected observation factors ("
00101                           << msg->factors.size() << ")." );
00102     }
00103 
00104     for ( size_t i = 0; i < factored_observations_deps_.size() &&
00105             i < msg->factors.size(); i++ )
00106     {
00107         uint32_t declared_values = factored_observations_deps_[i].getNumberOfValues();
00108         uint32_t expected_values = msg->factors[i].number_of_symbols;
00109         if ( declared_values != expected_values )
00110         {
00111             ROS_WARN_STREAM ( "ObservationLayer:: Metadata check failed. The number of declared observations for factor "
00112                               << i << " (" << declared_values << ") is different from the number of expected observations ("
00113                               << expected_values << ")." );
00114         }
00115     }
00116 }
00117 
00118 
00119 
00120 void
00121 ObservationLayer::
00122 addObservationFactor ( ObservationDep deps )
00123 {
00124     const ObservationDep::ObsFactorLocalDeps& local_deps = deps.getLocalDependencies();
00125     const ObservationDep::ObsFactorGlobalDeps& global_deps = deps.getGlobalDependencies();
00126 
00127     if ( local_deps.size() + global_deps.size() <= 1 )
00128     {
00129         ROS_FATAL ( "ObservationLayer:: Attempted to add an empty or unary observation factor. Observation Factors must be associated with at least two events." );
00130         ros::shutdown();
00131     }
00132 
00133     size_t new_factor_index = factored_observations_.size();
00134 
00135     factored_observations_.push_back ( 0 );
00136 
00137     foreach ( NameID ev_name_id, local_deps )
00138     {
00139         if ( event_named_local_observer_.count ( ev_name_id ) )
00140         {
00141             ROS_FATAL_STREAM ( "ObservationLayer:: Attempted to reassign dependency for event '" << ev_name_id.second << "' from PM " << ev_name_id.first );
00142             ROS_FATAL ( "ObservationLayer:: Events must be associated with a single observation factor / value" );
00143             ros::shutdown();
00144         }
00145         event_named_local_observer_[ev_name_id] = make_pair ( new_factor_index, deps.getDependencyIndex ( ev_name_id.second ) );
00146     }
00147 
00148     foreach ( string event_name, global_deps )
00149     {
00150         if ( event_named_global_observer_.count ( event_name ) )
00151         {
00152             ROS_FATAL_STREAM ( "ObservationLayer:: Attempted to reassign dependency for global event '" << event_name << "'" );
00153             ROS_FATAL ( "ObservationLayer:: Events must be associated with a single observation factor / value" );
00154             ros::shutdown();
00155         }
00156         event_named_global_observer_[event_name] = make_pair ( new_factor_index, deps.getDependencyIndex ( event_name ) );
00157     }
00158 
00159     factored_observations_deps_.push_back ( deps );
00160 }
00161 
00162 
00163 
00164 void
00165 ObservationLayer::
00166 publishJointObservation()
00167 {
00168     WorldSymbol observationinfo;
00169     uint32_t joint_observation = 0;
00170     uint32_t prod = 1;
00171     size_t N = factored_observations_deps_.size() - 1;
00172     for ( size_t i = 0; i <= N; i++ )
00173     {
00174         joint_observation += ( prod * factored_observations_[N - i] );
00175         prod *= factored_observations_deps_[N - i].getNumberOfValues();
00176     }
00177     observationinfo.world_symbol = joint_observation;
00178     observation_pub_.publish ( observationinfo );
00179 }


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