Skip to content

Commit dd1421f

Browse files
committed
Add property to set vertex cost update rate. Defaults to 10 Hz
1 parent ed3d289 commit dd1421f

2 files changed

Lines changed: 57 additions & 5 deletions

File tree

rviz_mesh_tools_plugins/include/rviz_mesh_tools_plugins/MeshDisplay.hpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,11 @@ private Q_SLOTS:
339339
*/
340340
void updateCullingMode();
341341

342+
/**
343+
* @brief Updates the vertex cost update application frequency
344+
*/
345+
void updateVertexCostUpdateFrequency();
346+
342347
private:
343348
/**
344349
* @brief RViz callback on initialize
@@ -455,7 +460,8 @@ private Q_SLOTS:
455460

456461
/// Cache for batching cost updates
457462
ThreadSafeQueue<mesh_msgs::msg::MeshVertexCostsSparseStamped::ConstSharedPtr> m_costsUpdateCache;
458-
float m_timeSinceLastUpdateApply;
463+
std::chrono::nanoseconds m_timeSinceLastUpdateApply;
464+
std::chrono::nanoseconds m_invUpdateFreq;
459465

460466

461467
/// Counter for the received messages
@@ -517,6 +523,9 @@ private Q_SLOTS:
517523
rviz_common::properties::QosProfileProperty* m_vertexCostUpdateTopicQos;
518524
rclcpp::QoS m_vertexCostUpdateQos;
519525

526+
/// Property to set the update processing frequency
527+
rviz_common::properties::IntProperty* m_vertexCostsRefreshRate;
528+
520529
/// Property to select different types of vertex cost maps to be shown
521530
rviz_common::properties::EnumProperty* m_selectVertexCostMap;
522531

rviz_mesh_tools_plugins/src/MeshDisplay.cpp

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ namespace rviz_mesh_tools_plugins
104104
MeshDisplay::MeshDisplay()
105105
: rviz_common::Display()
106106
, m_ignoreMsgs(false)
107-
, m_timeSinceLastUpdateApply(0.0)
107+
, m_timeSinceLastUpdateApply(0)
108+
, m_invUpdateFreq(std::chrono::nanoseconds(long(1e9)) / 10)
108109
, m_meshQos(rclcpp::QoS(1).transient_local())
109110
, m_vertexColorsQos(rclcpp::QoS(5))
110111
, m_vertexCostsQos(rclcpp::QoS(5))
@@ -200,6 +201,15 @@ MeshDisplay::MeshDisplay()
200201
);
201202
m_vertexCostUpdateTopicQos = new rviz_common::properties::QosProfileProperty(m_vertexCostUpdateTopic, m_vertexCostUpdateQos);
202203

204+
m_vertexCostsRefreshRate = new rviz_common::properties::IntProperty(
205+
"Visualization Update Rate", 10,
206+
"Sets the frequency (Hz) with which vertex cost updates are applied to the visual.",
207+
m_displayType,
208+
SLOT(updateVertexCostUpdateFrequency()),
209+
this,
210+
1 // Min value: This is 1 since we want at least one update per second
211+
);
212+
203213
m_selectVertexCostMap = new rviz_common::properties::EnumProperty("Vertex Costs Type", "-- None --",
204214
"Select the type of vertex cost map to be displayed. New types "
205215
"will appear here when a new message arrives.",
@@ -320,16 +330,18 @@ void MeshDisplay::onInitialize()
320330

321331
void MeshDisplay::update(float wall_dt, float ros_dt)
322332
{
333+
// NOTE: In rolling the update function uses std::chrono::nanoseconds instead of float
323334
(void) ros_dt;
324335

325-
if (m_timeSinceLastUpdateApply >= 1.0e9)
336+
// Determine current configured update freq
337+
if (m_timeSinceLastUpdateApply >= m_invUpdateFreq)
326338
{
327339
this->applyCachedCostUpdates();
328-
m_timeSinceLastUpdateApply = 0.0;
340+
m_timeSinceLastUpdateApply = std::chrono::nanoseconds(0);
329341
}
330342
else
331343
{
332-
m_timeSinceLastUpdateApply += wall_dt;
344+
m_timeSinceLastUpdateApply += std::chrono::nanoseconds(long(wall_dt));
333345
}
334346

335347
this->transformMesh();
@@ -634,6 +646,8 @@ void MeshDisplay::updateDisplayType()
634646
m_textureServiceName->hide();
635647
m_costColorType->hide();
636648
m_vertexCostsTopic->hide();
649+
m_vertexCostUpdateTopic->hide();
650+
m_vertexCostsRefreshRate->hide();
637651
m_selectVertexCostMap->hide();
638652
m_costUseCustomLimits->hide();
639653
m_costLowerLimit->hide();
@@ -669,11 +683,13 @@ void MeshDisplay::updateDisplayType()
669683
if (!m_ignoreMsgs)
670684
{
671685
m_vertexCostsTopic->show();
686+
m_vertexCostUpdateTopic->show();
672687
if (!m_costsMsgCache) // checks whether we are not subscribed yet, avoids resetting the subscription when something else changes in the display type
673688
{
674689
updateVertexCostsSubscription();
675690
}
676691
}
692+
m_vertexCostsRefreshRate->show();
677693
m_selectVertexCostMap->show();
678694
m_costUseCustomLimits->show();
679695
if (m_costUseCustomLimits->getBool())
@@ -944,6 +960,33 @@ void MeshDisplay::updateCullingMode()
944960
emit signalCullingModeChanged(static_cast<Ogre::CullingMode>(m_cullingMode->getOptionInt()));
945961
}
946962

963+
void MeshDisplay::updateVertexCostUpdateFrequency()
964+
{
965+
const int freq = m_vertexCostsRefreshRate->getInt();
966+
967+
// IntProperty::getInt returns 0 if invalid data was entered
968+
// We set the min value to 1 so a 0 return is always an error
969+
if (0 == freq)
970+
{
971+
RCLCPP_ERROR(
972+
rclcpp::get_logger("rviz_mesh_tools_plugins"),
973+
"[MeshDisplay::updateVertexCostUpdateFrequency] Invalid update frequency entered!"
974+
);
975+
m_vertexCostsRefreshRate->setInt(1);
976+
m_invUpdateFreq = std::chrono::seconds(1);
977+
return;
978+
}
979+
980+
using namespace std::chrono_literals;
981+
m_invUpdateFreq = std::chrono::nanoseconds(1s) / freq;
982+
983+
RCLCPP_DEBUG(
984+
rclcpp::get_logger("rviz_mesh_tools_plugins"),
985+
"[MeshDisplay::updateVertexCostUpdateFrequency] Update frequency set to %i, delta t is %lu nanoseconds",
986+
freq, m_invUpdateFreq.count()
987+
);
988+
}
989+
947990
// =====================================================================================================================
948991
// Data loading
949992
void MeshDisplay::initialServiceCall()

0 commit comments

Comments
 (0)