Skip to content
Snippets Groups Projects
Commit 54e2477d authored by bav6096's avatar bav6096
Browse files

added topic for visualisation

parent 59dcd246
No related branches found
No related tags found
No related merge requests found
...@@ -51,6 +51,8 @@ add_message_files( ...@@ -51,6 +51,8 @@ add_message_files(
FILES FILES
Monitoring.msg Monitoring.msg
Metric.msg Metric.msg
States.msg
State.msg
) )
## Generate services in the 'srv' folder ## Generate services in the 'srv' folder
...@@ -161,6 +163,8 @@ include_directories( ...@@ -161,6 +163,8 @@ include_directories(
## Mark executable scripts (Python etc.) for installation ## Mark executable scripts (Python etc.) for installation
## in contrast to setup.py, you can choose the destination ## in contrast to setup.py, you can choose the destination
catkin_install_python(PROGRAMS catkin_install_python(PROGRAMS
nodes/watchdog
nodes/monitor_test2
nodes/monitor_test nodes/monitor_test
DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION} DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
) )
......
string component # A metric is affiliated with a roboter component.
string label # Label of the metric. string label # Label of the metric.
string value # Value of the metric. string value # Value of the metric.
string unit # Unit of the metric. string unit # Unit of the metric.
......
string family # Affiliation of the monitored metric.
string origin # Origin of the monitored metric. string origin # Origin of the monitored metric.
Metric metric # Monitored metric. Metric metric # Monitored metric.
\ No newline at end of file
string component # Name of the component, whose
# critical level is monitored.
float32 critical # Critical level of the family.
\ No newline at end of file
State[] states # Array of states.
...@@ -6,11 +6,11 @@ from monitoring.monitor import Monitor ...@@ -6,11 +6,11 @@ from monitoring.monitor import Monitor
def monitor_test(): def monitor_test():
rospy.init_node("monitor_test") rospy.init_node("monitor_test")
monitor = Monitor(1, 1, "test") monitor = Monitor(1, 1)
rate = rospy.Rate(10) rate = rospy.Rate(10)
while not rospy.is_shutdown(): while not rospy.is_shutdown():
monitor.update_metric("test", 10, "test", 0.5) monitor.update_metric("family", "label1", 10, "unit", 0.5)
rate.sleep() rate.sleep()
......
#!/usr/bin/env python
import rospy
from monitoring.monitor import Monitor
def monitor_test():
rospy.init_node("monitor_test2")
monitor = Monitor(1, 1)
rate = rospy.Rate(10)
value = 0
while not rospy.is_shutdown():
monitor.update_metric("family", "label2", 10, "unit", value)
value = value + 0.1
if value > 1.0:
value = 0
rate.sleep()
if __name__ == '__main__':
try:
monitor_test()
except rospy.ROSInterruptException:
pass
...@@ -6,11 +6,10 @@ from monitoring.watchdog import Watchdog ...@@ -6,11 +6,10 @@ from monitoring.watchdog import Watchdog
def watchdog(): def watchdog():
rospy.init_node("watchdog") rospy.init_node("watchdog")
watching = Watchdog(1.0) watching = Watchdog(1, 1.0)
rate = rospy.Rate(10) rate = rospy.Rate(10)
while not rospy.is_shutdown(): while not rospy.is_shutdown():
print(watching.states)
rate.sleep() rate.sleep()
......
...@@ -7,7 +7,7 @@ from monitoring.msg import Metric ...@@ -7,7 +7,7 @@ from monitoring.msg import Metric
class Monitor: class Monitor:
def __init__(self, mode=1, freq=1.0, family="generic"): def __init__(self, mode=1, freq=1.0):
self.log = {} # Logging dictionary. Is used to accomplish different publishing modes. self.log = {} # Logging dictionary. Is used to accomplish different publishing modes.
self.pub = rospy.Publisher('monitoring', Monitoring, queue_size=1) self.pub = rospy.Publisher('monitoring', Monitoring, queue_size=1)
self.mode = mode self.mode = mode
...@@ -19,16 +19,16 @@ class Monitor: ...@@ -19,16 +19,16 @@ class Monitor:
self.timer = rospy.Timer(rospy.Duration(1.0 / freq), self.publish_monitoring) self.timer = rospy.Timer(rospy.Duration(1.0 / freq), self.publish_monitoring)
self.monitoring = Monitoring() self.monitoring = Monitoring()
self.monitoring.family = family
self.monitoring.origin = socket.gethostname() + rospy.get_name() self.monitoring.origin = socket.gethostname() + rospy.get_name()
# At the moment the last metric update ist published over and over. # At the moment the last metric update ist published over and over.
def publish_monitoring(self, event): def publish_monitoring(self, event):
self.pub.publish(self.monitoring) self.pub.publish(self.monitoring)
def update_metric(self, label, value, unit, critical): def update_metric(self, component, label, value, unit, critical):
def set_metric(): def set_metric():
metric = Metric() metric = Metric()
metric.component = str(component)
metric.label = str(label) metric.label = str(label)
metric.value = str(value) metric.value = str(value)
metric.unit = str(unit) metric.unit = str(unit)
......
...@@ -2,54 +2,70 @@ ...@@ -2,54 +2,70 @@
import rospy import rospy
from monitoring.msg import Monitoring from monitoring.msg import Monitoring
from monitoring.msg import State
from monitoring.msg import States
class Watchdog: class Watchdog:
def __init__(self, freq): def __init__(self, mode=1, freq=1.0):
self.states = {} self.mode = mode
self.pub = rospy.Publisher('states', States, queue_size=1)
self.component_states = {}
self.observing = {} self.observing = {}
if not freq > 0.0: if not freq > 0.0:
rospy.logwarn("Frequency must be greater then 0! Using 1 as frequency!") rospy.logwarn("Frequency must be greater then 0! Using 1 as frequency!")
freq = 1.0 freq = 1.0
self.timer = rospy.Timer(rospy.Duration(1.0 / freq), self.update_state) self.timer = rospy.Timer(rospy.Duration(1.0 / freq), self.update_states)
rospy.Subscriber("monitoring", Monitoring, self.logging) rospy.Subscriber("monitoring", Monitoring, self.logging)
def logging(self, monitoring): def logging(self, monitoring):
family = monitoring.family
origin = monitoring.origin origin = monitoring.origin
metric = monitoring.metric metric = monitoring.metric
component = metric.component
label = metric.label label = metric.label
value = metric.value value = metric.value
unit = metric.unit unit = metric.unit
critical = metric.critical critical = metric.critical
if family in self.observing: if component in self.observing:
if origin in self.observing[family]: if origin in self.observing[component]:
self.observing[family][origin].update({label: {"value": value, "unit": unit, "critical": critical}}) self.observing[component][origin].update({label: {"value": value, "unit": unit, "critical": critical}})
else: else:
self.observing[family][origin] = {} self.observing[component][origin] = {}
self.observing[family][origin][label] = {"value": value, "unit": unit, "critical": critical} self.observing[component][origin][label] = {"value": value, "unit": unit, "critical": critical}
else: else:
self.observing[family] = {} self.observing[component] = {}
self.observing[family][origin] = {} self.observing[component][origin] = {}
self.observing[family][origin][label] = {"value": value, "unit": unit, "critical": critical} self.observing[component][origin][label] = {"value": value, "unit": unit, "critical": critical}
def update_state(self, event): def update_states(self, event):
for family in self.observing: for component in self.observing:
# Mode 1: Take the average critical level for a component
if self.mode == 1:
aggregation = 0 aggregation = 0
number = 0 number = 0
for origin in self.observing[family]: for origin in self.observing[component]:
for label in self.observing[family][origin]: for label in self.observing[component][origin]:
number = number + 1 number = number + 1
aggregation = aggregation + self.observing[family][origin][label]["critical"] aggregation = aggregation + self.observing[component][origin][label]["critical"]
quotient = aggregation / number quotient = aggregation / number
self.states.update({family: quotient}) self.component_states.update({component: quotient})
self.publish_states()
def publish_states(self):
topic = States()
for component in self.component_states:
state = State()
state.component = component
state.critical = self.component_states[component]
topic.states.append(state)
self.pub.publish(topic)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment