nav_grid_display.cpp
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2020, Locus Robotics
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
37 
38 #include <OgreSharedPtr.h>
39 #include <OgreVector3.h>
40 
41 #include <ros/ros.h>
42 #include <algorithm>
43 #include <memory>
44 #include <string>
45 #include <vector>
46 
47 #include <rviz/ogre_helpers/grid.h>
48 #include <rviz/display_context.h>
49 
50 
51 namespace robot_nav_rviz_plugins
52 {
53 NavGridDisplay::NavGridDisplay(const std::string& data_type, bool include_ignore_property)
54  : Display()
55  , panel_display_(nullptr)
56  , palette_loader_("robot_nav_rviz_plugins", "robot_nav_rviz_plugins::NavGridPalette")
57 {
59 
60  topic_property_ = new rviz::RosTopicProperty("Topic", "",
61  QString::fromStdString(data_type), QString::fromStdString(data_type + std::string(" topic to subscribe to.")),
62  this, SLOT(updateTopic()));
63 
64  alpha_property_ = new rviz::FloatProperty("Alpha", 0.7,
65  "Amount of transparency to apply to the map.",
66  this, SLOT(updateAlpha()));
69 
70  color_scheme_property_ = new rviz::EnumProperty("Color Scheme", "map", "How to color the occupancy values.",
71  this, SLOT(updatePalette()));
72 
73  draw_behind_property_ = new rviz::BoolProperty("Draw Behind", false,
74  "Rendering option, controls whether or not the map is always"
75  " drawn behind everything else.",
76  this, SLOT(updateAlpha()));
77 
78  resolution_property_ = new rviz::FloatProperty("Resolution", 0, "Resolution of the map. (not editable)", this);
80 
81  width_property_ = new rviz::IntProperty("Width", 0, "Width of the map, in cells. (not editable)", this);
83 
84  height_property_ = new rviz::IntProperty("Height", 0, "Height of the map, in cells. (not editable)", this);
86 
87  unreliable_property_ = new rviz::BoolProperty("Unreliable", false,
88  "Prefer UDP topic transport",
89  this,
90  SLOT(updateTopic()));
91 
92  if (include_ignore_property)
93  {
94  ignore_type_property_ = new rviz::EnumProperty("Ignore Value Type", "None", "Way to exclude certain value(s)", this,
95  SLOT(updateIgnoreType()));
96  ignore_type_property_->addOption("None", static_cast<int>(IgnoreType::NONE));
97  ignore_type_property_->addOption("Value", static_cast<int>(IgnoreType::VALUE));
98  ignore_type_property_->addOption("Limit", static_cast<int>(IgnoreType::LIMIT));
99 
100  ignore_property_ = new rviz::FloatProperty("Ignore Value", -1.0, "Value to not include in the min/max",
101  this, SLOT(updateIgnore()));
103  }
104 }
105 
107 {
108  unsubscribe();
109  clear();
110 }
111 
112 /**********************************************************************************
113  * Overrides from Display
114  **********************************************************************************/
116 {
117  // Lazy initialization of the panel_display_ to ensure we have the non-null pointers to the ogre objects
118  if (panel_display_) return;
119 
120  panel_display_ = std::make_shared<OgrePanel>(panel_data_, *scene_manager_, *scene_node_);
121 
122  for (auto plugin_name : palette_loader_.getDeclaredClasses())
123  {
124  auto palette = palette_loader_.createInstance(plugin_name);
125 
126  std::string name = palette->getName();
127  color_scheme_property_->addOption(name.c_str(), static_cast<int>(color_scheme_names_.size()));
128  color_scheme_names_.push_back(name);
129  panel_display_->addPalette(*palette);
130  }
131  // Set the current values
132  updatePalette();
133 }
134 
136 {
137  subscribe();
138 }
139 
141 {
142  unsubscribe();
143  clear();
144 }
145 
147 {
148  Display::reset();
149 
150  // Force resubscription so that the map will be re-sent
151  updateTopic();
152 }
153 
154 void NavGridDisplay::setTopic(const QString &topic, const QString &datatype)
155 {
156  topic_property_->setString(topic);
157 }
158 
159 void NavGridDisplay::update(float wall_dt, float ros_dt)
160 {
161  // periodically make sure the map is in the right spot
162  transformMap();
163 }
164 
166 {
167  // retransform if frame changed
168  transformMap();
169 }
170 
171 /**********************************************************************************
172  * Custom Events triggered by properties
173  **********************************************************************************/
175 {
177 }
178 
180 {
181  unsubscribe();
182  clear();
183  subscribe();
184 }
185 
187 {
188  int palette_index = color_scheme_property_->getOptionInt();
189  panel_display_->setPalette(color_scheme_names_[palette_index]);
190  updateAlpha();
191 }
192 
194 {
195  if (updated_bounds.isEmpty())
196  {
197  return;
198  }
199 
201  // Only process the info if the info has changed
202  if (info != cached_info_)
203  {
204  if (!validateFloats(info))
205  {
206  setStatus(rviz::StatusProperty::Error, "Map", "Message contained invalid floating point values (nans or infs)");
207  return;
208  }
209 
210  if (info.width * info.height == 0)
211  {
212  std::stringstream ss;
213  ss << "Map is zero-sized (" << info.width << "x" << info.height << ")";
214  setStatus(rviz::StatusProperty::Error, "Map", QString::fromStdString(ss.str()));
215  return;
216  }
217 
218  // If the data has changed size or resolution, update the panel display's info
219  if (info.resolution != cached_info_.resolution ||
220  info.width != cached_info_.width || info.height != cached_info_.height)
221  {
222  panel_display_->updateInfo(info);
226  }
227  cached_info_ = info;
228  }
229 
230  setStatus(rviz::StatusProperty::Ok, "Message", "Map received");
231 
232  panel_display_->updateData(updated_bounds);
233 
234  updatePalette();
235 
236  transformMap();
237 
238  setStatus(rviz::StatusProperty::Ok, "Map", "Map OK");
240 }
241 
242 
244 {
245  switch (getIgnoreType())
246  {
247  case IgnoreType::VALUE:
248  case IgnoreType::LIMIT:
250  break;
251  default:
253  }
254  updateIgnore();
255 }
256 
257 
258 /**********************************************************************************
259  * Other Methods
260  **********************************************************************************/
261 
263 {
264  // Make sure the topic is enabled and non-empty.
265  // Also sets status if there is a problem
266 
267  if (!isEnabled())
268  {
269  return;
270  }
271 
272  if (!topic_property_->getTopic().isEmpty())
273  {
274  try
275  {
277  setStatus(rviz::StatusProperty::Ok, "Topic", "OK");
278  }
279  catch (ros::Exception& e)
280  {
281  setStatus(rviz::StatusProperty::Error, "Topic", QString("Error subscribing: ") + e.what());
282  }
283  }
284 }
285 
287 {
288  onUnsubscribe();
289 }
290 
292 {
293  setStatus(rviz::StatusProperty::Warn, "Message", "No map received");
294  panel_display_->clear();
295 }
296 
298 {
299  if (panel_display_->transformMap(*context_->getFrameManager()))
300  {
301  setStatus(rviz::StatusProperty::Ok, "Transform", "Transform OK");
302  }
303  else
304  {
305  std::string frame = panel_data_.getFrameId();
307  "No transform from [" + QString::fromStdString(frame) + "] to [" + fixed_frame_ + "]");
308  }
309 }
310 } // namespace robot_nav_rviz_plugins
nav_grid::NavGridInfo::height
unsigned int height
robot_nav_rviz_plugins::NavGridDisplay::color_scheme_property_
rviz::EnumProperty * color_scheme_property_
Definition: nav_grid_display.h:188
rviz::EnumProperty::getOptionInt
virtual int getOptionInt()
rviz::Display::isEnabled
bool isEnabled() const
rviz::RosTopicProperty
GenericBounds< unsigned int >::isEmpty
bool isEmpty() const
validate_floats.h
rviz::DisplayContext::queueRender
virtual void queueRender()=0
robot_nav_rviz_plugins
Several reusable pieces for displaying polygons.
Definition: nav_grid_display.h:60
robot_nav_rviz_plugins::NavGridDisplay::palette_loader_
pluginlib::ClassLoader< NavGridPalette > palette_loader_
Definition: nav_grid_display.h:196
rviz::StatusProperty::Error
Error
robot_nav_rviz_plugins::validateFloats
bool validateFloats(const nav_grid::NavGridInfo &info)
Definition: validate_floats.h:49
rviz::Property::show
void show()
rviz::FloatProperty::setValue
bool setValue(const QVariant &new_value) override
ros.h
robot_nav_rviz_plugins::NavGridDisplay::onUnsubscribe
virtual void onUnsubscribe()
Actual unsubscription logic, called by unsubscribe.
Definition: nav_grid_display.h:158
robot_nav_rviz_plugins::NavGridDisplay::unsubscribe
void unsubscribe()
Called to trigger unsubscribing.
Definition: nav_grid_display.cpp:286
nav_grid::NavGrid::getInfo
NavGridInfo getInfo() const
rviz::BoolProperty
rviz::FloatProperty::setMax
void setMax(float max)
rviz::EditableEnumProperty::setString
virtual void setString(const QString &str)
robot_nav_rviz_plugins::NavGridDisplay::update
void update(float wall_dt, float ros_dt) override
Definition: nav_grid_display.cpp:159
robot_nav_rviz_plugins::NavGridDisplay::mapUpdated
void mapUpdated(const nav_core2::UIntBounds &updated_bounds)
Custom signal emitted when new map data is received.
robot_nav_rviz_plugins::NavGridDisplay::alpha_property_
rviz::FloatProperty * alpha_property_
Definition: nav_grid_display.h:185
ros::Exception
rviz::Display::fixed_frame_
QString fixed_frame_
robot_nav_rviz_plugins::NavGridDisplay::updateTopic
void updateTopic()
Definition: nav_grid_display.cpp:179
robot_nav_rviz_plugins::NavGridDisplay::onEnable
void onEnable() override
Definition: nav_grid_display.cpp:135
robot_nav_rviz_plugins::NavGridDisplay::onInitialize
void onInitialize() override
Definition: nav_grid_display.cpp:115
rviz::Property::getValue
virtual QVariant getValue() const
robot_nav_rviz_plugins::NavGridDisplay::width_property_
rviz::IntProperty * width_property_
Definition: nav_grid_display.h:180
robot_nav_rviz_plugins::NavGridDisplay::color_scheme_names_
std::vector< std::string > color_scheme_names_
Definition: nav_grid_display.h:195
rviz::FloatProperty::setMin
void setMin(float min)
robot_nav_rviz_plugins::NavGridDisplay::onSubscribe
virtual void onSubscribe(const std::string &topic)
Actual subscription logic, called by subscribe.
Definition: nav_grid_display.h:146
robot_nav_rviz_plugins::NavGridDisplay::IgnoreType::VALUE
@ VALUE
robot_nav_rviz_plugins::NavGridDisplay::NavGridDisplay
NavGridDisplay(const std::string &data_type, bool include_ignore_property=false)
Constructor for the display.
Definition: nav_grid_display.cpp:53
robot_nav_rviz_plugins::NavGridDisplay::updatePalette
void updatePalette()
Definition: nav_grid_display.cpp:186
rviz::EnumProperty
rviz::FloatProperty
robot_nav_rviz_plugins::NavGridDisplay::cached_info_
nav_grid::NavGridInfo cached_info_
Definition: nav_grid_display.h:176
rviz::IntProperty::setValue
bool setValue(const QVariant &new_value) override
rviz::Property::hide
void hide()
rviz::Display::setStatus
virtual void setStatus(StatusProperty::Level level, const QString &name, const QString &text)
robot_nav_rviz_plugins::NavGridDisplay::~NavGridDisplay
virtual ~NavGridDisplay()
Definition: nav_grid_display.cpp:106
nav_grid_display.h
robot_nav_rviz_plugins::NavGridDisplay::updateIgnoreType
virtual void updateIgnoreType()
Definition: nav_grid_display.cpp:243
robot_nav_rviz_plugins::NavGridDisplay::updateAlpha
void updateAlpha()
Definition: nav_grid_display.cpp:174
robot_nav_rviz_plugins::NavGridDisplay::panel_data_
nav_grid::VectorNavGrid< unsigned char > panel_data_
Definition: nav_grid_display.h:175
rviz::FloatProperty::getFloat
virtual float getFloat() const
rviz::EnumProperty::addOption
virtual void addOption(const QString &option, int value=0)
rviz::Display::scene_node_
Ogre::SceneNode * scene_node_
rviz::RosTopicProperty::getTopic
QString getTopic() const
rviz::Property::connect
std::enable_if<!QtPrivate::FunctionPointer< Func >::IsPointerToMemberFunction, QMetaObject::Connection >::type connect(const QObject *context, Func &&slot, Qt::ConnectionType type=Qt::AutoConnection)
rviz::StatusProperty::Ok
Ok
robot_nav_rviz_plugins::NavGridDisplay::unreliable_property_
rviz::BoolProperty * unreliable_property_
Definition: nav_grid_display.h:186
rviz::StatusProperty::Warn
Warn
robot_nav_rviz_plugins::NavGridDisplay::ignore_property_
rviz::FloatProperty * ignore_property_
Definition: nav_grid_display.h:192
robot_nav_rviz_plugins::NavGridDisplay::subscribe
void subscribe()
Called to trigger subscription, handles empty topics setting status.
Definition: nav_grid_display.cpp:262
rviz::Display::scene_manager_
Ogre::SceneManager * scene_manager_
rviz::DisplayContext::getFrameManager
virtual FrameManager * getFrameManager() const=0
robot_nav_rviz_plugins::NavGridDisplay::panel_display_
OgrePanel::Ptr panel_display_
Definition: nav_grid_display.h:172
rviz::RosTopicProperty::getTopicStd
std::string getTopicStd() const
nav_grid::NavGrid::getFrameId
std::string getFrameId() const
nav_grid::NavGridInfo
robot_nav_rviz_plugins::NavGridDisplay::showMap
void showMap(const nav_core2::UIntBounds &updated_bounds)
Definition: nav_grid_display.cpp:193
robot_nav_rviz_plugins::NavGridDisplay::transformMap
void transformMap()
Put the map in its proper place.
Definition: nav_grid_display.cpp:297
robot_nav_rviz_plugins::NavGridDisplay::draw_behind_property_
rviz::BoolProperty * draw_behind_property_
Definition: nav_grid_display.h:187
nav_grid::NavGridInfo::resolution
double resolution
rviz::Display::context_
DisplayContext * context_
robot_nav_rviz_plugins::NavGridDisplay::clear
void clear()
Clear the data and remove the objects from the screen.
Definition: nav_grid_display.cpp:291
robot_nav_rviz_plugins::NavGridDisplay::IgnoreType::LIMIT
@ LIMIT
rviz::Property::setReadOnly
virtual void setReadOnly(bool read_only)
robot_nav_rviz_plugins::NavGridDisplay::IgnoreType::NONE
@ NONE
nav_grid::NavGridInfo::width
unsigned int width
robot_nav_rviz_plugins::NavGridDisplay::reset
void reset() override
Definition: nav_grid_display.cpp:146
robot_nav_rviz_plugins::NavGridDisplay::getIgnoreType
IgnoreType getIgnoreType() const
Definition: nav_grid_display.h:121
robot_nav_rviz_plugins::NavGridDisplay::height_property_
rviz::IntProperty * height_property_
Definition: nav_grid_display.h:181
robot_nav_rviz_plugins::NavGridDisplay::onDisable
void onDisable() override
Definition: nav_grid_display.cpp:140
robot_nav_rviz_plugins::NavGridDisplay::topic_property_
rviz::RosTopicProperty * topic_property_
Definition: nav_grid_display.h:184
robot_nav_rviz_plugins::NavGridDisplay::fixedFrameChanged
void fixedFrameChanged() override
Definition: nav_grid_display.cpp:165
robot_nav_rviz_plugins::NavGridDisplay::resolution_property_
rviz::FloatProperty * resolution_property_
Definition: nav_grid_display.h:179
robot_nav_rviz_plugins::NavGridDisplay::updateIgnore
virtual void updateIgnore()
Definition: nav_grid_display.h:114
grid.h
nav_core2::UIntBounds
robot_nav_rviz_plugins::NavGridDisplay::ignore_type_property_
rviz::EnumProperty * ignore_type_property_
Definition: nav_grid_display.h:191
robot_nav_rviz_plugins::NavGridDisplay::setTopic
void setTopic(const QString &topic, const QString &datatype) override
Definition: nav_grid_display.cpp:154
rviz::IntProperty
display_context.h


robot_nav_rviz_plugins
Author(s):
autogenerated on Sun May 18 2025 02:47:50