From 6b7a9faf8c56ea7c9fc46f7e64d8e72b762122b9 Mon Sep 17 00:00:00 2001 From: useyourshadow <74160173+useyourshadow@users.noreply.github.com> Date: Sat, 27 Jul 2024 14:54:49 -0400 Subject: [PATCH] Refined alarm handlers got rid of if main == name --- .../ros_alarms/src/ros_alarms/alarm_node.py | 39 +++++++++++++++++ .../src/ros_alarms/cpu_temp_alarm.py | 42 +++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 mil_common/ros_alarms/src/ros_alarms/alarm_node.py create mode 100644 mil_common/ros_alarms/src/ros_alarms/cpu_temp_alarm.py diff --git a/mil_common/ros_alarms/src/ros_alarms/alarm_node.py b/mil_common/ros_alarms/src/ros_alarms/alarm_node.py new file mode 100644 index 000000000..97941d9f5 --- /dev/null +++ b/mil_common/ros_alarms/src/ros_alarms/alarm_node.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python3 + +import rospy + +from std_msgs.msg import Float32 + +global alarm_pub + +def cpu_temp_callback(temp): + + # Sets of cpu temp alarm + if float(temp.data) > 75: + rospy.logwarn(f"CPU TEMPERATURE EXCEEDS: {temp.data}") + alarm_pub.publish(True) + + else: + alarm_pub.publish(False) + + + +def cpu_temp_alarm(): + global alarm_pub + + # Initializing node + rospy.init_node('cpu_temp_alarm', anonymous=True) + + rospy.Subscriber('cpu_temperature', Float32, cpu_temp_callback) + alarm_pub = rospy.Publisher('cpu_alarm', bool, queue_size=10) + + rospy.spin() + + + +if __name__ == '__main__': + + try: + cpu_temp_alarm() + except rospy.InterruptedError: + pass \ No newline at end of file diff --git a/mil_common/ros_alarms/src/ros_alarms/cpu_temp_alarm.py b/mil_common/ros_alarms/src/ros_alarms/cpu_temp_alarm.py new file mode 100644 index 000000000..d67266d9c --- /dev/null +++ b/mil_common/ros_alarms/src/ros_alarms/cpu_temp_alarm.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python3 + +import psutil +import rospy +from std_msgs.msg import Float32, Bool + +def get_cpu_temp(): + # Checks computer sensors + temp = psutil.sensors_temperatures() + if 'coretemp' in temp: + return temp['coretemp'][0].current + else: + raise ValueError("Core temp not found!") + +def cpu_temp_callback(alarm): + if alarm.data: + rospy.logwarn("ALARM: CPU temperature exceeds the threshold!") + +def cpu_temp_ROS(): + # ROS initializing + rospy.init_node('cpu_temp_node', anonymous=True) + temp_pub = rospy.Publisher('cpu_temperature', Float32, queue_size=10) + rospy.Subscriber('cpu_alarm', Bool, cpu_temp_callback) + rate = rospy.Rate(1) + + # Loops through and actively gets CPU temp + while not rospy.is_shutdown(): + try: + cpu_temp = get_cpu_temp() + rospy.loginfo("CPU TEMP: %f", cpu_temp) + temp_pub.publish(cpu_temp) + except ValueError as e: + rospy.logwarn(str(e)) + rate.sleep() + +# if __name__ == '__main__': +# try: +# cpu_temp_ROS() +# except rospy.ROSInterruptException: +# pass + +