renamed cleanup to close, this is more common

This commit is contained in:
Olivier R-D
2015-04-10 12:39:19 +02:00
parent 747f07287d
commit 92a7c1812e
11 changed files with 25 additions and 62 deletions

View File

@ -360,15 +360,14 @@ class URRobot(object):
def stop(self):
self.stopj()
def cleanup(self):
def close(self):
"""
close connection to robot and stop internal thread
"""
self.logger.info("Closing sockets to robot")
self.secmon.cleanup()
self.secmon.close()
if self.rtmon:
self.rtmon.stop()
shutdown = cleanup # this might be wrong since we could also shutdown the robot hardware from this script
def set_freedrive(self, val):
"""

View File

@ -32,12 +32,9 @@ class URRTMonitor(threading.Thread):
# pose is not included!
rtstruct540 = struct.Struct('>d6d6d6d6d6d6d6d6d18d')
def __init__(self, urHost, loglevel=logging.WARNING):
def __init__(self, urHost):
threading.Thread.__init__(self)
self.logger = logging.getLogger(self.__class__.__name__)
if len(logging.root.handlers) == 0: # dirty hack
logging.basicConfig()
self.logger.setLevel(loglevel)
self.daemon = True
self._stop_event = True
self._dataEvent = threading.Condition()
@ -141,7 +138,7 @@ class URRTMonitor(threading.Thread):
timestamp = self.__recvTime
pkgsize = struct.unpack('>i', head)[0]
self.logger.debug(
'Received header telling that package is %d bytes long' %
'Received header telling that package is %s bytes long',
pkgsize)
payload = self.__recv_bytes(pkgsize - 4)
if pkgsize >= 692:
@ -150,7 +147,7 @@ class URRTMonitor(threading.Thread):
unp = self.rtstruct540.unpack(payload[:self.rtstruct540.size])
else:
self.logger.warning(
'Error, Received packet of length smaller than 540: ',
'Error, Received packet of length smaller than 540: %s ',
pkgsize)
return
@ -165,9 +162,8 @@ class URRTMonitor(threading.Thread):
self._ctrlTimestamp -
self._last_ctrl_ts) > 0.010:
self.logger.warning(
"Error the controller failed to send us a packet: time since last packet {}s ".format(
self._ctrlTimestamp -
self._last_ctrl_ts))
"Error the controller failed to send us a packet: time since last packet %s s ",
self._ctrlTimestamp - self._last_ctrl_ts)
self._last_ctrl_ts = self._ctrlTimestamp
self._qActual = np.array(unp[31:37])
self._qTarget = np.array(unp[1:7])
@ -246,7 +242,7 @@ class URRTMonitor(threading.Thread):
#print(self.__class__.__name__+': Stopping')
self._stop_event = True
def cleanup(self):
def close(self):
self.stop()
self.join()
@ -258,35 +254,3 @@ class URRTMonitor(threading.Thread):
self._rtSock.close()
def startupInteractive():
from optparse import OptionParser
from IPython import embed
# Require the urhost arg.
parser = OptionParser()
parser.add_option(
'--debug',
action='store_true',
default=False,
dest='debug')
parser.add_option(
'--start',
action='store_true',
default=False,
dest='start')
opts, args = parser.parse_args()
if len(args) != 1:
raise Exception('Must have argument with ur-host name or ip!')
urHost = args[0]
print('Connecting to UR real-time socket inteface on "%s"' % urHost)
# # Start the connectors
urRTMon = URRTMonitor(urHost, debug=opts.debug)
# # Register for hard shutdown
try:
if opts.start:
urRTMon.start()
embed()
finally:
urRTMon.stop()
if __name__ == '__main__':
startupInteractive()

View File

@ -99,7 +99,7 @@ class ParserUtils(object):
else:
self.logger.debug("Message type parser not implemented %s", tmp)
else:
self.logger.debug("Unknown packet type %s with size %s" % (ptype, psize))
self.logger.debug("Unknown packet type %s with size %s", ptype, psize)
return allData
@ -178,12 +178,12 @@ class ParserUtils(object):
counter += 1
if counter > limit:
self.logger.warn("tried %s times to find a packet in data, advertised packet size: %s, type: %s", counter, psize, ptype)
self.logger.warn("Data length: {}".format(len(data)))
self.logger.warn("Data length: %s", len(data))
limit = limit * 10
elif len(data) >= psize:
self.logger.debug("Got packet with size %s and type %s", psize, ptype)
if counter:
self.logger.info("Remove {0} bytes of garbage at begining of packet".format(counter))
self.logger.info("Remove %s bytes of garbage at begining of packet", counter)
# ok we we have somehting which looks like a packet"
return (data[:psize], data[psize:])
else:
@ -389,7 +389,7 @@ class SecondaryMonitor(Thread):
with self._dictLock:
return self._dict["RobotModeData"]["isProgramRunning"]
def cleanup(self):
def close(self):
self._trystop = True
self.join()
# with self._dataEvent: #wake up any thread that may be waiting for data before we close. Should we do that?