I have a callback function in a file "color.py" that constantly recieves incoming data but updates a global variable every 3 seconds:
last_color = ""
last_calltime = datetime.strptime("00:00:00","%H:%M:%S")
def callback(data):
global last_calltime, last_color
cube = data.data
t = time.localtime()
tf = time.strftime("%H:%M:%S",t)
current_time = datetime.strptime(tf, "%H:%M:%S")
delta_time = current_time - last_calltime
if abs(delta_time.total_seconds()) >= 3:
if "Red" in cube:
last_color = "Red"
elif "Green" in cube:
last_color = "Green"
else:
last_color = "Blue"
t = time.localtime()
tf = time.strftime("%H:%M:%S",t)
last_calltime = datetime.strptime(tf, "%H:%M:%S")
return last_color
if __name__ == '__main__':
try:
rospy.init_node('color')
rospy.Subscriber ('/topic',String, callback)
rospy.spin()
In another file, I need to reference this "last_color" variable that updates. So far the function I have in this file:
from color import last_color
a = 0
def observe_color(a)
if a == 0:
return last_color
else:
return "None"
I need this to return the updated variable from the "color.py" file but it only returns the initialized value for last_color which is "". How can I do this?