active_zoneset_node.cpp
Go to the documentation of this file.
1 // Copyright (c) 2021 Pilz GmbH & Co. KG
2 //
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU Lesser General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public License
14 // along with this program. If not, see <https://www.gnu.org/licenses/>.
15 
16 #include <algorithm>
17 #include <stdexcept>
18 #include <string>
19 #include <vector>
20 
21 #include <boost/optional.hpp>
22 
23 #include <ros/ros.h>
24 #include <visualization_msgs/MarkerArray.h>
25 #include <std_msgs/UInt8.h>
26 
28 #include "psen_scan_v2/ZoneSetConfiguration.h"
30 
31 namespace psen_scan_v2
32 {
34 {
38  zoneset_markers_ = nh_.advertise<visualization_msgs::MarkerArray>(DEFAULT_ZONESET_MARKER_ARRAY_TOPIC, 10);
39 }
40 
41 void ActiveZonesetNode::zonesetCallback(const ZoneSetConfiguration& zoneset_config)
42 {
43  zoneset_config_ = zoneset_config;
44  updateMarkers();
45 }
46 
47 void ActiveZonesetNode::activeZonesetCallback(const std_msgs::UInt8& active_zoneset_id)
48 {
49  active_zoneset_id_ = active_zoneset_id;
50  updateMarkers();
51 };
52 
54 {
56  {
57  try
58  {
59  auto new_markers = toMarkers(activeZoneset());
60  addMarkers(new_markers);
61  }
62  catch (std::out_of_range const& e)
63  {
65  "Active zone " << static_cast<unsigned>(active_zoneset_id_->data)
66  << " of your scanner does not exist in the provided configuration!");
67  }
70  }
71 }
72 
74 {
75  return active_zoneset_id_.is_initialized() && zoneset_config_.is_initialized();
76 }
77 
79 {
80  return zoneset_config_->zonesets.at(active_zoneset_id_->data);
81 }
82 
83 // LCOV_EXCL_START
85 {
86  return this->activeZoneset();
87 }
88 // LCOV_EXCL_STOP
89 
90 void ActiveZonesetNode::addMarkers(std::vector<visualization_msgs::Marker>& new_markers)
91 {
92  for (const auto& marker : new_markers)
93  {
94  current_markers_.push_back(marker);
95  }
96 }
97 
99 {
100  for (const auto& lm : last_markers_)
101  {
102  auto has_lm_namespace = [&lm](visualization_msgs::Marker& x) { return x.ns == lm.ns; };
103  if (std::find_if(current_markers_.begin(), current_markers_.end(), has_lm_namespace) == current_markers_.end())
104  { // not in current markers -> delete it
105  auto marker = visualization_msgs::Marker();
106  marker.action = visualization_msgs::Marker::DELETE;
107  marker.ns = lm.ns;
108  marker.id = lm.id;
109  current_markers_.push_back(marker);
110  }
111  }
112  last_markers_.clear();
113 }
114 
116 {
117  auto ma = visualization_msgs::MarkerArray();
118  ma.markers = current_markers_;
120  last_markers_ = std::move(current_markers_);
121 }
122 
123 } // namespace psen_scan_v2
static const std::string DEFAULT_ACTIVE_ZONESET_TOPIC
#define ROS_ERROR_STREAM_THROTTLE(period, args)
void addMarkers(std::vector< visualization_msgs::Marker > &new_markers)
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
void zonesetCallback(const ZoneSetConfiguration &zoneset_config)
static const std::string DEFAULT_ZONECONFIGURATION_TOPIC
Contains the events needed to define and implement the scanner protocol.
void activeZonesetCallback(const std_msgs::UInt8 &zoneset_config)
void publish(const boost::shared_ptr< M > &message) const
boost::optional< ZoneSetConfiguration > zoneset_config_
Root namespace for the ROS part.
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
boost::optional< std_msgs::UInt8 > active_zoneset_id_
static const std::string DEFAULT_ZONESET_MARKER_ARRAY_TOPIC
std::vector< visualization_msgs::Marker > current_markers_
ActiveZonesetNode(ros::NodeHandle &nh)
Constructor.
std::vector< visualization_msgs::Marker > toMarkers(const ZoneSet &zoneset)
std::vector< visualization_msgs::Marker > last_markers_


psen_scan_v2
Author(s): Pilz GmbH + Co. KG
autogenerated on Sat Nov 5 2022 02:13:36