Go to the documentation of this file.00001
00025 #include <mdm_library/common_defs.h>
00026 #include <mdm_library/state_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 StateLayer::
00038 StateLayer() :
00039 pred_map_sub_ ( nh_.subscribe ( "/predicate_maps", 1, &StateLayer::predicateMapCallback, this ) ),
00040 pred_update_sub_ ( nh_.subscribe ( "/predicate_updates", 1, &StateLayer::predicateUpdatesCallback, this ) ),
00041 state_pub_ ( nh_.advertise<WorldSymbol> ( "state", 1, true ) )
00042 {}
00043
00044
00045
00046 void
00047 StateLayer::
00048 predicateUpdatesCallback ( const PredicateUpdateConstPtr& msg )
00049 {
00050 if ( nr_value_map_.empty() )
00051 {
00052 ROS_WARN ( "Predicate names table is empty. Trying to re-subscribe to predicate updates and maps." );
00053 pred_update_sub_.shutdown();
00054 pred_update_sub_ = nh_.subscribe ( "/predicate_updates", 1, &StateLayer::predicateUpdatesCallback, this );
00055 pred_map_sub_.shutdown();
00056 pred_map_sub_ = nh_.subscribe ( "/predicate_maps", 1, &StateLayer::predicateMapCallback, this );
00057
00058 return;
00059 }
00060 SFIDSetPtr sf_to_update ( new set<uint32_t> );
00061
00062 boost::unordered_map<NrID, boost::shared_ptr<bool> >::const_iterator value_it;
00063 boost::unordered_map<NrID, SFIDSetPtr >::const_iterator observer_it;
00064 foreach ( PredicateUpdate::_true_predicates_type::value_type pred_id, msg->true_predicates )
00065 {
00066 NrID nr_id ( msg->pm_id, pred_id );
00067 value_it = nr_value_map_.find ( nr_id );
00068 if ( value_it != nr_value_map_.end() )
00069 {
00070 * ( value_it->second ) = true;
00071 if ( nr_pred_observer_[nr_id] != 0 && !nr_pred_observer_[nr_id]->empty() )
00072 {
00073 sf_to_update->insert ( nr_pred_observer_[nr_id]->begin(), nr_pred_observer_[nr_id]->end() );
00074 }
00075 }
00076 }
00077
00078 foreach ( PredicateUpdate::_falling_predicates_type::value_type pred_id, msg->falling_predicates )
00079 {
00080 NrID nr_id ( msg->pm_id, pred_id );
00081 value_it = nr_value_map_.find ( nr_id );
00082 if ( value_it != nr_value_map_.end() )
00083 {
00084 * ( value_it->second ) = false;
00085 if ( nr_pred_observer_[nr_id] != 0 && !nr_pred_observer_[nr_id]->empty() )
00086 {
00087 sf_to_update->insert ( nr_pred_observer_[nr_id]->begin(), nr_pred_observer_[nr_id]->end() );
00088 }
00089 }
00090 }
00091 foreach ( uint32_t sf, *sf_to_update )
00092 {
00093 updateStateInfo ( sf );
00094 }
00095 if ( !sf_to_update->empty() )
00096 {
00097 publishJointState();
00098 }
00099 }
00100
00101
00102
00103 std::string
00104 StateLayer::
00105 factorString ( unsigned k )
00106 {
00107 std::stringstream s1;
00108 std::string s2;
00109
00110 if ( factored_state_deps_[k].size() == 1 )
00111 {
00113 foreach ( NameID nid, factored_state_deps_[k] )
00114 {
00115 if ( named_value_map_.count ( nid ) )
00116 {
00117 uint32_t pm_id = nid.first;
00118 string pred_name = nid.second;
00119 s1 << "PM_Id:" << pm_id << "," << pred_name << "=" << * ( named_value_map_[nid] );
00120 }
00121 }
00122 }
00123 else
00124 {
00126 foreach ( NameID nid, factored_state_deps_[k] )
00127 {
00128 uint32_t pm_id = nid.first;
00129 string pred_name = nid.second;
00130 if ( named_value_map_.count ( nid ) && * ( named_value_map_[nid] ) == true )
00131 {
00132 s1 << "PM_Id:" << pm_id << "," << pred_name;
00133 }
00134 }
00135 }
00136 s1 >> s2;
00137 return s2;
00138 }
00139
00140
00141
00142 void
00143 StateLayer::
00144 predicateMapCallback ( const PredicateInfoMapConstPtr& msg )
00145 {
00146 foreach ( PredicateInfo p, msg->map )
00147 {
00148 NameID name_id ( msg->pm_id, p.name );
00149 NrID nr_id ( msg->pm_id, p.nr );
00150
00151 boost::unordered_map<NameID, boost::shared_ptr<bool> >::const_iterator it;
00152 it = named_value_map_.find ( name_id );
00153 if ( it != named_value_map_.end() )
00154 {
00155 nr_value_map_[nr_id] = it->second;
00156 }
00157 nr_pred_observer_[nr_id] = named_pred_observer_[name_id];
00158 }
00159 }
00160
00161
00162
00163 void
00164 StateLayer::
00165 addStateFactor ( const StateDep::SFDeps& deps )
00166 {
00167 factored_state_deps_.push_back ( deps );
00168
00169 foreach ( NameID name_id, deps )
00170 {
00171 if ( !named_pred_observer_.count ( name_id ) )
00172 {
00173 named_pred_observer_[name_id] = ( SFIDSetPtr ) ( new set<uint32_t> );
00174 named_value_map_[name_id] = boost::shared_ptr<bool> ( new bool ( false ) );
00175 }
00176 named_pred_observer_[name_id]->insert ( factored_state_.size() );
00177 }
00178
00179 factored_state_.push_back ( 0 );
00180 }
00181
00182
00183
00184 void
00185 StateLayer::
00186 addStateFactor ( const StateDep& deps )
00187 {
00188 addStateFactor ( deps.getDependencies() );
00189 }
00190
00191
00192
00193 void
00194 StateLayer::
00195 publishJointState()
00196 {
00197 WorldSymbol ws;
00198 uint32_t joint_state = 0;
00199 uint32_t prod = 1;
00200 size_t N = factored_state_deps_.size() - 1;
00201 for ( size_t i = 0; i <= N; i++ )
00202 {
00203 ROS_DEBUG_STREAM ( "StateLayer:: Factor " << N - i << ": " << factored_state_[N - i]
00204 << " (" << factorString ( N - i ) << ")" );
00205 joint_state += ( prod * factored_state_[N - i] );
00206 if ( factored_state_deps_[N - i].size() > 1 )
00207 {
00208 prod *= factored_state_deps_[N - i].size();
00209 }
00210 else
00211 {
00212 prod *= 2;
00213 }
00214 }
00215 ws.world_symbol = joint_state;
00216 state_pub_.publish ( ws );
00217 }
00218
00219
00220
00221 void
00222 StateLayer::
00223 updateStateInfo ( const uint32_t& factor )
00224 {
00225 size_t i = 0, val = factored_state_deps_[factor].size();
00226 bool any = false;
00227 foreach ( NameID name_id, factored_state_deps_[factor] )
00228 {
00229 uint32_t pm_id = name_id.first;
00230 string pred_name = name_id.second;
00231
00232 boost::unordered_map<NameID, boost::shared_ptr<bool> >::const_iterator it;
00233 it = named_value_map_.find ( name_id );
00234 if ( it == named_value_map_.end() )
00235 {
00236 ROS_WARN_STREAM ( "StateLayer:: Predicate " << pred_name << " from PM " << pm_id << " is not known to this agent." );
00237 ROS_WARN_STREAM ( "StateLayer:: Skipping the update of State Factor " << factor );
00238 return;
00239 }
00240
00241 if ( * ( it->second ) == true )
00242 {
00243 if ( any )
00244 {
00245 ROS_WARN_STREAM ( "StateLayer:: Multiple dependencies active for state factor " << factor << " -- "
00246 << name_id.first << "(" << name_id.second << "); "
00247 << factored_state_deps_[factor][val].first << "(" << factored_state_deps_[factor][val].second << "); " );
00248 ROS_WARN_STREAM ( "StateLayer:: Multi-valued state factors should depend on mutually exclusive predicates." );
00249 }
00250 val = i;
00251 any = true;
00252 }
00253 i++;
00254 }
00255
00256 if ( factored_state_deps_[factor].size() == 1 )
00257 {
00258
00259 factored_state_[factor] = ( uint32_t ) any;
00260 }
00261 else
00262 {
00263
00264 if ( val < factored_state_deps_[factor].size() )
00265 {
00266 factored_state_[factor] = val;
00267 }
00268 else
00269 {
00270 ROS_WARN_STREAM ( "StateLayer:: No dependencies active for n-ary state factor " << factor );
00271 }
00272 }
00273 }