Skip to content

Commit 62b0e1e

Browse files
author
Aaron Roller
committed
feat: configure stat and hz stat AM-777/configure_stat
1 parent 1a8ee40 commit 62b0e1e

File tree

2 files changed

+69
-33
lines changed

2 files changed

+69
-33
lines changed

include/super_lib/am_life_cycle.h

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ class AMLifeCycle
7474
void cleanup();
7575
void sendNodeUpdate();
7676
void error(std::string message, std::string error_code, bool forced = false);
77+
void configureStat(AMStat& stat, std::string name, std::string category="");
7778

7879
protected:
7980
std::string node_name_;
@@ -179,12 +180,16 @@ class AMLifeCycle
179180
/**Initialize statistics by adding to the list*/
180181
virtual void addStatistics(diagnostic_updater::DiagnosticStatusWrapper& dsw);
181182

183+
184+
[[deprecated("configureHzStat with the stat's long name in the yaml")]]
185+
AMStatReset& configureHzStats(AMStatReset& stats);
186+
182187
/** Initialize the stats that reset once per second providing the equivalent of rostopic hz to ensure frequency of
183188
* publishing. Allows for overriding values in roslaunch configurations.
184189
* Provide the target, which is the approximate value you expect to receive. The warnings and errors will be
185190
* provided with tolerance on both sides of the target.
186191
*
187-
* Configurations key use the stats short name.
192+
* Configurations key use the stats long name.
188193
*
189194
* setting a target will also set a min/max 5% warn and 10% error
190195
* no target allows for just min or just max or both.
@@ -202,8 +207,20 @@ class AMLifeCycle
202207
*
203208
*
204209
* @param stats to be configured
205-
* */
206-
AMStatReset& configureHzStats(AMStatReset& stats);
210+
* */
211+
AMStatReset& configureHzStat(AMStatReset& stats);
212+
213+
/** Standard configuration of min/max or warn/error for a stat.
214+
*
215+
* Keyed by the stat long name in the yaml for configuration.
216+
*
217+
* my_stat:
218+
* error:
219+
* max: 85
220+
* warn:
221+
* max: 70
222+
*/
223+
void configureStat(AMStat& stat);
207224

208225
/** Called periodically by a timer defaulting to 1 second.
209226
* Useful for checking health regularly, but not during

src/super_lib/am_life_cycle.cpp

Lines changed: 49 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -392,55 +392,74 @@ void AMLifeCycle::addStatistics(diagnostic_updater::DiagnosticStatusWrapper& dsw
392392
dsw.summary((uint8_t)status, "update");
393393
}
394394

395-
AMStatReset& AMLifeCycle::configureHzStats(AMStatReset& stats)
395+
void AMLifeCycle::configureStat(AMStat& stat, std::string name, std::string category)
396396
{
397397
int unassigned=UINT_MAX;
398-
int hz_target = unassigned;
399-
int hz_min_error =unassigned;
400-
int hz_min_warn=unassigned;
401-
int hz_max_warn=unassigned;
402-
int hz_max_error=unassigned;
403-
const std::string prefix = stats.getShortName();
404-
const std::string hz_prefix=prefix + "/hz/";
405-
const std::string target_key =hz_prefix + "target";
406-
const std::string error_min_key=hz_prefix + "error/min";
407-
const std::string warn_min_key =hz_prefix + "warn/min";
408-
const std::string warn_max_key =hz_prefix + "warn/max";
409-
const std::string error_max_key=hz_prefix + "error/max";
398+
int target = unassigned;
399+
int min_error =unassigned;
400+
int min_warn=unassigned;
401+
int max_warn=unassigned;
402+
int max_error=unassigned;
403+
std::string prefix = name + "/";
404+
405+
if(!category.empty())
406+
{
407+
prefix = prefix + category + "/";
408+
}
409+
410+
const std::string target_key =prefix + "target";
411+
const std::string error_min_key=prefix + "error/min";
412+
const std::string warn_min_key =prefix + "warn/min";
413+
const std::string warn_max_key =prefix + "warn/max";
414+
const std::string error_max_key=prefix + "error/max";
410415

411416
//set all if target is provided
412-
if(param<int>(target_key, hz_target, 0))
417+
if(param<int>(target_key, target, 0))
413418
{
414419
//give 5% tolerance in either direction for warning, 10% for error. Override default values as desired
415-
const int warning_offset = std::ceil(hz_target * 0.05);
420+
const int warning_offset = std::ceil(target * 0.05);
416421
const int error_offset = 2 * warning_offset;
417422
//don't go below zero because that doesn't make any sense for hz.
418-
hz_min_error=std::max(0,hz_target - error_offset);
419-
hz_min_warn=std::max(0,hz_target - warning_offset);
420-
hz_max_warn=hz_target + warning_offset;
421-
hz_max_error=hz_target + error_offset;
422-
stats.setWarnError(hz_min_error, hz_min_warn, hz_max_warn, hz_max_error);
423+
min_error=std::max(0,target - error_offset);
424+
min_warn=std::max(0,target - warning_offset);
425+
max_warn=target + warning_offset;
426+
max_error=target + error_offset;
427+
stat.setWarnError(min_error, min_warn, max_warn, max_error);
423428
}
424429
//override individual boundary configs if provided
425-
if(param<int>(error_min_key, hz_min_error, hz_min_error))
430+
if(param<int>(error_min_key, min_error, min_error))
426431
{
427-
stats.setMinError(hz_min_error);
432+
stat.setMinError(min_error);
428433
}
429-
if(param<int>(warn_min_key, hz_min_warn, hz_min_warn))
434+
if(param<int>(warn_min_key, min_warn, min_warn))
430435
{
431-
stats.setMinWarn(hz_min_warn);
436+
stat.setMinWarn(min_warn);
432437
}
433-
if(param<int>(warn_max_key, hz_max_warn, hz_max_warn))
438+
if(param<int>(warn_max_key, max_warn, max_warn))
434439
{
435-
stats.setMaxWarn(hz_max_warn);
440+
stat.setMaxWarn(max_warn);
436441
}
437-
if(param<int>(error_max_key, hz_max_error, hz_max_error))
442+
if(param<int>(error_max_key, max_error, max_error))
438443
{
439-
stats.setMaxError(hz_max_error);
440-
}
444+
stat.setMaxError(max_error);
445+
}
446+
}
441447

442-
return stats;
448+
void AMLifeCycle::configureStat(AMStat& stat)
449+
{
450+
configureStat(stat,stat.getLongName());
443451
}
452+
453+
AMStatReset& AMLifeCycle::configureHzStat(AMStatReset& stat)
454+
{
455+
configureStat(stat, stat.getLongName(), "hz");
456+
}
457+
458+
AMStatReset& AMLifeCycle::configureHzStats(AMStatReset& stats)
459+
{
460+
configureStat(stats, stats.getShortName(), "hz");
461+
}
462+
444463
void AMLifeCycle::sendNodeUpdate()
445464
{
446465
brain_box_msgs::LifeCycleState msg;

0 commit comments

Comments
 (0)