@@ -15,9 +15,9 @@ LayerManager::LayerManager(MeshMap& map, const rclcpp::Node::SharedPtr& node)
1515, loader_(" mesh_map" , " mesh_map::AbstractLayer" )
1616{}
1717
18- void LayerManager::read_configured_layers (const rclcpp::Node::SharedPtr& node )
18+ void LayerManager::read_configured_layers ()
1919{
20- const auto layer_names = node ->declare_parameter (
20+ const auto layer_names = node_ ->declare_parameter (
2121 MeshMap::MESH_MAP_NAMESPACE + " .layers" , std::vector<std::string>()
2222 );
2323 const rclcpp::ParameterType ros_param_type = rclcpp::ParameterType::PARAMETER_STRING ;
@@ -29,7 +29,7 @@ void LayerManager::read_configured_layers(const rclcpp::Node::SharedPtr& node)
2929 throw rclcpp::exceptions::InvalidParametersException (" The layer name " + layer_name + " is used more than once. Layer names must be unique!" );
3030 }
3131 // This will throws rclcpp::ParameterValue exception if mesh_map.layer_name.type is not set
32- const std::string layer_type = node ->declare_parameter <std::string>(
32+ const std::string layer_type = node_ ->declare_parameter <std::string>(
3333 MeshMap::MESH_MAP_NAMESPACE + " ." + layer_name + " .type"
3434 );
3535
@@ -42,7 +42,7 @@ void LayerManager::read_configured_layers(const rclcpp::Node::SharedPtr& node)
4242 // output warning if no layer plugins were configured
4343 if (names_.size () == 0 )
4444 {
45- RCLCPP_WARN_STREAM (node ->get_logger (), " No MeshMap layer plugins configured!"
45+ RCLCPP_WARN_STREAM (node_ ->get_logger (), " No MeshMap layer plugins configured!"
4646 << " - Use the param \" " << MeshMap::MESH_MAP_NAMESPACE << " .layers\" , which must be a list of strings with arbitrary layer names. "
4747 << " For each layer_name, also define layer_name.type with the respective type that shall be loaded via pluginlib." );
4848 }
@@ -59,7 +59,7 @@ void LayerManager::read_configured_layers(const rclcpp::Node::SharedPtr& node)
5959 {
6060 const Vertex& v = vertices_[layer_name];
6161 // If mesh_map.layer_name.inputs is not set we assume the layer needs no inputs
62- const auto dependencies = node ->declare_parameter <std::vector<std::string>>(
62+ const auto dependencies = node_ ->declare_parameter <std::vector<std::string>>(
6363 MeshMap::MESH_MAP_NAMESPACE + " ." + layer_name + " .inputs" , std::vector<std::string>()
6464 );
6565
@@ -72,7 +72,7 @@ void LayerManager::read_configured_layers(const rclcpp::Node::SharedPtr& node)
7272 }
7373
7474 // Print the dependencies
75- RCLCPP_INFO (node ->get_logger (), " Layer dependencies:" );
75+ RCLCPP_INFO (node_ ->get_logger (), " Layer dependencies:" );
7676 for (const auto & [name, vertex]: vertices_)
7777 {
7878 auto [it, end] = boost::adjacent_vertices (vertex, graph_);
@@ -90,8 +90,28 @@ void LayerManager::read_configured_layers(const rclcpp::Node::SharedPtr& node)
9090 }
9191 builder << ' ]' ;
9292
93- RCLCPP_INFO (node ->get_logger (), " %s: %s" , name.c_str (), builder.str ().c_str ());
93+ RCLCPP_INFO (node_ ->get_logger (), " %s: %s" , name.c_str (), builder.str ().c_str ());
9494 }
95+
96+ // Initialize the publishers
97+ cost_pub_ = node_->create_publisher <mesh_msgs::msg::MeshVertexCostsStamped>(" ~/vertex_costs" , rclcpp::QoS (names_.size ()).transient_local ());
98+
99+ // we dont want to publish static costs all the time.
100+ // therefore we have to check if someone newly subscribed to the topic
101+ cost_subscribe_checker_ = node_->create_wall_timer (std::chrono::milliseconds (200 ), [this ]{
102+ const size_t count = cost_pub_->get_subscription_count () + cost_pub_->get_intra_process_subscription_count ();
103+ if (count > vertex_cost_sub_count_)
104+ {
105+ RCLCPP_INFO (node_->get_logger (), " New cost layer subscriber detected. Publishing vertex costs once..." );
106+ for (const auto & [layer_name, layer_ptr]: layer_instances ())
107+ {
108+ publish_cost_layer (layer_ptr->costs (), layer_name, node_->get_clock ()->now ());
109+ }
110+ }
111+ vertex_cost_sub_count_ = count;
112+ });
113+
114+ cost_update_pub_ = node_->create_publisher <mesh_msgs::msg::MeshVertexCostsSparseStamped>(std::string (cost_pub_->get_topic_name ()) + " /updates" , rclcpp::QoS (10 ).transient_local ());
95115}
96116
97117bool LayerManager::load_layer_plugins (const rclcpp::Logger& logger)
@@ -125,7 +145,7 @@ bool LayerManager::load_layer_plugins(const rclcpp::Logger& logger)
125145 return instances_.empty () ? false : true ;
126146}
127147
128- bool LayerManager::initialize_layer_plugins (const rclcpp::Node::SharedPtr& node, const MeshMap::Ptr& map)
148+ bool LayerManager::initialize_layer_plugins (const MeshMap::Ptr& map)
129149{
130150 // Prevent layers from publishing updates while initialization is still ongoing initialization
131151 std::unique_lock lock (layer_mtx_);
@@ -142,7 +162,7 @@ bool LayerManager::initialize_layer_plugins(const rclcpp::Node::SharedPtr& node,
142162 if (nullptr == instance)
143163 {
144164 RCLCPP_WARN (
145- node ->get_logger (),
165+ node_ ->get_logger (),
146166 " Skipping initialization of layer '%s' because it failed to load" ,
147167 name.c_str ()
148168 );
@@ -156,10 +176,10 @@ bool LayerManager::initialize_layer_plugins(const rclcpp::Node::SharedPtr& node,
156176 std::placeholders::_3
157177 );
158178
159- if (!instance->initialize (name, callback, map, node ))
179+ if (!instance->initialize (name, callback, map, node_ ))
160180 {
161181 RCLCPP_ERROR (
162- node ->get_logger (),
182+ node_ ->get_logger (),
163183 " Could not initialize the layer plugin with the name \" %s\" !" ,
164184 name.c_str ()
165185 );
@@ -168,12 +188,12 @@ bool LayerManager::initialize_layer_plugins(const rclcpp::Node::SharedPtr& node,
168188
169189 if (!instance->readLayer ())
170190 {
171- RCLCPP_INFO (node ->get_logger (), " Computing layer '%s' ..." , name.c_str ());
191+ RCLCPP_INFO (node_ ->get_logger (), " Computing layer '%s' ..." , name.c_str ());
172192 instance->computeLayer ();
173193 }
174194
175195 // Publish the layer
176- map_. publishVertexCosts (instance->costs (), name, node_->get_clock ()->now ());
196+ publish_cost_layer (instance->costs (), name, node_->get_clock ()->now ());
177197 }
178198
179199 return true ;
@@ -199,14 +219,14 @@ void LayerManager::layer_changed(
199219 const auto & ptr = get_layer (name);
200220 if (nullptr == ptr)
201221 {
202- // TODO: Log error
222+ RCLCPP_ERROR (node_-> get_logger (), " LayerManager::layer_changed() - Could not get pointer to changed layer %s " , name. c_str ());
203223 return ;
204224 }
205225
206226 const auto v_it = vertices_.find (name);
207227 if (v_it == vertices_.end ())
208228 {
209- // TODO: Log error
229+ RCLCPP_ERROR (node_-> get_logger (), " LayerManager::layer_changed() - Could not find dependency graph vertex for layer %s " , name. c_str ());
210230 return ;
211231 }
212232
@@ -221,7 +241,7 @@ void LayerManager::layer_changed(
221241 c.insert (v, cm.containsKey (v)? cm[v]: ptr->defaultValue ());
222242 }
223243 rlock.unlock ();
224- map_. publishVertexCostsUpdate (c, ptr->defaultValue (), name, timestamp);
244+ publish_cost_update (c, ptr->defaultValue (), name, timestamp);
225245
226246 // Notify the map
227247 map_.layerChanged (name, changed);
@@ -242,4 +262,33 @@ void LayerManager::layer_changed(
242262 }
243263}
244264
265+ void LayerManager::publish_cost_layer (const lvr2::VertexMap<float >& costs, const std::string& name, const rclcpp::Time& timestamp)
266+ {
267+ cost_pub_->publish (
268+ mesh_msgs_conversions::toVertexCostsStamped (
269+ costs,
270+ map_.mesh ()->numVertices (),
271+ 0 ,
272+ name,
273+ map_.getGlobalFrameID (),
274+ map_.getUUID (),
275+ timestamp
276+ )
277+ );
278+ }
279+
280+ void LayerManager::publish_cost_update (const lvr2::VertexMap<float >& costs, const float default_value, const std::string& name, const rclcpp::Time& timestamp)
281+ {
282+ cost_update_pub_->publish (
283+ mesh_msgs_conversions::toVertexCostsSparseStamped (
284+ costs,
285+ default_value,
286+ name,
287+ map_.getGlobalFrameID (),
288+ map_.getUUID (),
289+ timestamp
290+ )
291+ );
292+ }
293+
245294} // namespace mesh_map;
0 commit comments