Skip to content

Commit ac5bc51

Browse files
authored
Merge pull request #64 from naturerobots/feature/configurable-cost-update-refresh-rate
2 parents ed3d289 + 73f4f1d commit ac5bc51

File tree

3 files changed

+70
-23
lines changed

3 files changed

+70
-23
lines changed

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()

rviz_mesh_tools_plugins/src/MeshVisual.cpp

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,36 +1089,31 @@ bool MeshVisual::updateVertexCosts(
10891089
}
10901090

10911091
/* ManualObject::getNumSections and ManualObject::getSection are deprecated
1092-
* in the Ogre version ROS Jazzy (and future versions) uses. Since the new
1093-
* API is not available in the Ogre version used by ROS Humble we use this
1094-
* check to keep Humble support.
1092+
* in the Ogre version ROS Jazzy (and future versions) uses. The VES_COLOUR
1093+
* semantic is also new in the ROS Jazzy Ogre version. Prior versions use
1094+
* VES_DIFFUSE for colours in the ManualObject. Since the new API is not
1095+
* available in the Ogre version used by ROS Humble we use this check to keep
1096+
* Humble support.
10951097
*
10961098
* This can be removed when Humble support is dropped.
10971099
*/
10981100
#if OGRE_VERSION < ((1 << 16) | (12 << 8) | 7)
10991101
Ogre::RenderOperation* render_op = m_vertexCostsMesh->getSection(0)->getRenderOperation();
11001102
const Ogre::VertexDeclaration* v_decl = render_op->vertexData->vertexDeclaration;
1101-
1102-
// findElementBySemantic does not support VES_COLOUR yet
1103-
const Ogre::VertexElement* color_sem = nullptr;
1104-
for (const auto& elem: v_decl->getElements())
1105-
{
1106-
// VET_COLOUR is deprecated in favour of VET_UBYTE4_NORM
1107-
if (Ogre::VET_UBYTE4_NORM == elem.getType())
1108-
{
1109-
color_sem = &elem;
1110-
}
1111-
}
1103+
const Ogre::VertexElement* color_elem = v_decl->findElementBySemantic(Ogre::VES_DIFFUSE);
11121104
#else
11131105
// Get the needed Vertex Colour attribute information about the raw vertex buffer
11141106
Ogre::RenderOperation* render_op = m_vertexCostsMesh->getSections().front()->getRenderOperation();
11151107
const Ogre::VertexDeclaration* v_decl = render_op->vertexData->vertexDeclaration;
1116-
const Ogre::VertexElement* color_sem = v_decl->findElementBySemantic(Ogre::VES_COLOUR);
1108+
const Ogre::VertexElement* color_elem = v_decl->findElementBySemantic(Ogre::VES_COLOUR);
11171109
#endif
11181110

1119-
if (nullptr == color_sem)
1111+
if (nullptr == color_elem)
11201112
{
1121-
RCLCPP_ERROR(rclcpp::get_logger("rviz_mesh_tools_plugins"), "Vertex Cost Mesh has no Vertex Colour attribute!");
1113+
RCLCPP_ERROR(
1114+
rclcpp::get_logger("rviz_mesh_tools_plugins"),
1115+
"Cannot update vertex costs: Vertex Cost Mesh has no Vertex Colour element!"
1116+
);
11221117
return false;
11231118
}
11241119

@@ -1143,7 +1138,7 @@ bool MeshVisual::updateVertexCosts(
11431138
std::byte* vertex = raw + v * vbuf->getVertexSize();
11441139
// Pointer to the color
11451140
Ogre::ABGR* vcolor = nullptr;
1146-
color_sem->baseVertexPointerToElement(vertex, &vcolor);
1141+
color_elem->baseVertexPointerToElement(vertex, &vcolor);
11471142

11481143
// write vertex colors that are calculated from the cost values
11491144
float normalizedCost = (costs[i] - minCost) / range;

0 commit comments

Comments
 (0)