Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 import threading
00036 import time
00037
00038 import rospy
00039
00040 from . import logging
00041
00042
00043 class ParamUpdater(threading.Thread):
00044 '''
00045 Using dynamic_reconfigure that is passed in __init__, this thread updates
00046 the Dynamic Reconfigure server's value.
00047
00048 This class works for a single element in a single parameter.
00049 '''
00050
00051
00052
00053 def __init__(self, reconf):
00054 """
00055 :type reconf: dynamic_reconfigure
00056 """
00057 super(ParamUpdater, self).__init__()
00058 self.setDaemon(True)
00059
00060 self._reconf = reconf
00061 self._condition_variable = threading.Condition()
00062 self._configs_pending = {}
00063 self._timestamp_last_pending = None
00064 self._stop_flag = False
00065
00066 def run(self):
00067 _timestamp_last_commit = None
00068
00069 logging.debug(' ParamUpdater started')
00070
00071 while not self._stop_flag:
00072 if self._timestamp_last_pending is None or _timestamp_last_commit >= self._timestamp_last_pending:
00073 with self._condition_variable:
00074 logging.debug(' ParamUpdater loop 1.1')
00075 self._condition_variable.wait()
00076 logging.debug(' ParamUpdater loop 1.2')
00077 logging.debug(' ParamUpdater loop 2')
00078
00079 if self._stop_flag:
00080 return
00081
00082 _timestamp_last_commit = time.time()
00083 configs_tobe_updated = self._configs_pending.copy()
00084 self._configs_pending = {}
00085
00086 logging.debug(' run last_commit={}, last_pend={}'.format(
00087 _timestamp_last_commit, self._timestamp_last_pending))
00088
00089 try:
00090 self._reconf.update_configuration(configs_tobe_updated)
00091 except rospy.ServiceException as ex:
00092 logging.debug('Could not update configs due to {}'.format(
00093 ex.value))
00094 except Exception as exc:
00095 raise exc
00096
00097 def update(self, config):
00098 with self._condition_variable:
00099 for name, value in config.items():
00100 self._configs_pending[name] = value
00101
00102 self._timestamp_last_pending = time.time()
00103
00104 self._condition_variable.notify()
00105
00106 def stop(self):
00107 self._stop_flag = True
00108 with self._condition_variable:
00109 self._condition_variable.notify()