Skip to content
Snippets Groups Projects
Commit 60b05b8c authored by bav6096's avatar bav6096
Browse files

updated monitor

parent edd80a9d
No related branches found
No related tags found
No related merge requests found
......@@ -49,7 +49,7 @@ find_package(catkin REQUIRED COMPONENTS
## Generate messages in the 'msg' folder
add_message_files(
FILES
Monitoring.msg
Metric.msg
)
## Generate services in the 'srv' folder
......
string key # Label of the value.
string val # Value to track.
string unit # Unit of the value.
float32 err # Error level of the value.
\ No newline at end of file
float32 err_lvl # Error level of the value.
\ No newline at end of file
string name # Name and origin of the monitor message.
string desc # Text describing the monitor message.
Metric metric # Values to monitor.
\ No newline at end of file
#!/usr/bin/env python
import socket
import rospy
from monitoring.msg import Monitoring
from monitoring.msg import Metic
from monitoring.msg import Metric
class Monitor:
def __init__(self, desc, freq=1, auto=True):
self.host = socket.gethostname()
self.node = rospy.get_name()
self.name = self.host + self.node
self.desc = desc
def __init__(self, mode, freq):
self.log = {} # Logging dictionary. Is used to accomplish different publishing modes.
self.pub = rospy.Publisher('metric', Metric, queue_size=1)
self.mode = mode
self.metric = Metric()
if not freq > 0:
print("Frequency must be greater than 0!")
quit()
rospy.logwarn("Frequency must be greater then 0! Using 1 as frequency!")
freq = 1
if auto:
self.timer = rospy.Timer(rospy.Duration(1.0 / freq), self.pub_monitoring)
self.timer = rospy.Timer(rospy.Duration(1.0 / freq), self.publish_metric)
self.log = {}
self.pub = rospy.Publisher('monitoring', Monitoring, queue_size=1)
self.pub_times = 0
def reset_metric(self):
self.metric = Metric()
self.monitoring = Monitoring()
self.monitoring.name = self.name
self.monitoring.desc = self.desc
def publish_metric(self):
self.pub.publish(self.metric)
self.reset_metric()
def rst_log_key(self, key):
def update_metric(self, key, val, unit, err_lvl):
def set_metric():
self.metric.key = key
self.metric.val = val
self.metric.unit = unit
self.metric.err_lvl = err_lvl
def rst_key(): # Reset the value of a key in the logging dictionary.
self.log[key] = {'num': 0, 'val': 0, 'sum': 0, 'dur': rospy.get_rostime()}
def add_log_val(self, key, val, unit, err, mode=2):
if " " in key: # A key cannot contain whitespace!
rospy.logwarn("[%s] whitespaces are not allowed in monitoring keys!", self.node)
rospy.logwarn("Whitespaces are not allowed in metric keys!")
else:
if key in self.log:
# Mode 1: Write the first obtained value into the
# published message.
if mode == 1 and self.pub_times == 0:
self.set_metric(key, val, unit, err)
self.pub_times = 1
self.reset_metric() # Reset metric.
# Mode 2: Write the last obtained value into the
# published message.
elif mode == 2:
self.set_metric(key, val, unit, err)
if key in self.log:
# Mode 1: Set the last obtained value.
if self.mode == 1:
set_metric()
# Mode 3: Remember and write the minimum obtained
# value, after mode selection, into the
# published message.
elif mode == 3:
# Mode 2: Remember and set the minimum value,
# obtained after mode selection.
elif self.mode == 2:
if val < self.log[key]['val']:
self.log[key]['val'] = val
else:
val = self.log[key]['val']
self.set_metric(key, self.log[key]['val'], unit, err)
set_metric()
# Mode 4: Remember and write the maximum obtained
# value, after mode selection, into the
# published message.
elif mode == 4:
# Mode 3: Remember and set the maximum value,
# obtained after mode selection.
elif self.mode == 3:
if val > self.log[key]['val']:
self.log[key]['val'] = val
else:
val = self.log[key]['val']
self.set_metric(key, self.log[key]['val'], unit, err)
set_metric()
# Mode 5: Write the average value over five seconds
# into the published message.
elif mode == 5:
# Mode 4: Set the average obtained value over five
# seconds.
elif self.mode == 4:
dur = rospy.get_rostime() - self.log[key]['dur']
if dur < rospy.Duration(5):
self.log[self.name][key]['num'] += 1
self.log[self.name][key]['sum'] += val
self.log[key]['num'] += 1
self.log[key]['sum'] += val
else:
quotient = self.log[key]['sum'] / (self.log[key]['num'] + 0.001)
self.set_metric(key, quotient, unit, err)
self.rst_log_key(key)
val = self.log[key]['sum'] / (self.log[key]['num'] + 0.001)
set_metric()
rst_key()
else:
self.rst_log_key(key) # Add key to the logging dictionary.
self.set_metric(key, val, unit, err)
def set_metric(self, key, val, unit, err):
metric = Metic()
metric.key = str(key)
metric.val = str(val)
metric.unit = str(unit)
metric.err = err
self.monitoring.metric = metric
def rst_monitoring(self):
self.monitoring = Monitoring()
self.monitoring.name = self.name
self.monitoring.desc = self.desc
def pub_monitoring(self):
self.pub.publish(self.monitoring)
self.rst_monitoring()
rst_key() # Add key to the logging dictionary.
set_metric()
#!/usr/bin/env python
import rospy
from monitoring.msg import Metric
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment