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

@ -114,7 +114,7 @@ class Service(object):
time.sleep(0.12)#URX secondday port accepts command at 10Hz, no need to go faster time.sleep(0.12)#URX secondday port accepts command at 10Hz, no need to go faster
def cleanup(self): def close(self):
if self.joystick: if self.joystick:
self.joystick.quit() self.joystick.quit()
@ -135,5 +135,5 @@ if __name__ == "__main__":
try: try:
service.loop() service.loop()
finally: finally:
robot.cleanup() robot.close()
service.cleanup() service.close()

View File

@ -33,5 +33,5 @@ if __name__ == "__main__":
finally: finally:
rob.cleanup() rob.close()

View File

@ -23,5 +23,5 @@ if __name__ == "__main__":
rob.translate_tool((0, 0, -l), acc=a, vel=v) rob.translate_tool((0, 0, -l), acc=a, vel=v)
rob.translate_tool((0, 0, l), acc=a, vel=v) rob.translate_tool((0, 0, l), acc=a, vel=v)
finally: finally:
rob.cleanup() rob.close()

View File

@ -87,7 +87,7 @@ if __name__ == '__main__':
try: try:
service.loop() service.loop()
finally: finally:
robot.cleanup() robot.close()
spnav.spnav_close() spnav.spnav_close()

View File

@ -23,7 +23,7 @@ if __name__ == "__main__":
else: else:
do_wait = True do_wait = True
rob = urx.Robot("192.168.1.6", logLevel=logging.INFO) rob = urx.Robot("192.168.1.6")
rob.set_tcp((0, 0, 0, 0, 0, 0)) rob.set_tcp((0, 0, 0, 0, 0, 0))
rob.set_payload(0.5, (0, 0, 0)) rob.set_payload(0.5, (0, 0, 0))
try: try:
@ -99,5 +99,5 @@ if __name__ == "__main__":
finally: finally:
rob.cleanup() rob.close()

View File

@ -36,5 +36,5 @@ if __name__ == "__main__":
rob.movep(pose, acc=a, vel=v, radius=0, wait=True) rob.movep(pose, acc=a, vel=v, radius=0, wait=True)
finally: finally:
rob.cleanup() rob.close()

View File

@ -34,7 +34,7 @@ class Server(socketserver.ThreadingMixIn, socketserver.TCPServer):
""" """
self.handlers = [] self.handlers = []
def cleanup(self): def close(self):
for handler in self.handlers: for handler in self.handlers:
handler.request.shutdown(socket.SHUT_RDWR) handler.request.shutdown(socket.SHUT_RDWR)
handler.request.close() handler.request.close()
@ -63,4 +63,4 @@ if __name__ == "__main__":
handler.request.sendall(packet) handler.request.sendall(packet)
finally: finally:
print("Shutting down server") print("Shutting down server")
server.cleanup() server.close()

View File

@ -13,10 +13,10 @@ if __name__ == "__main__":
else: else:
host = 'localhost' host = 'localhost'
try: try:
robot = Robot( host )#, logLevel=logging.DEBUG, parserLogLevel=logging.DEBUG) robot = Robot(host)
r = robot r = robot
embed() embed()
finally: finally:
if "robot" in dir(): if "robot" in dir():
robot.cleanup() robot.close()

View File

@ -360,15 +360,14 @@ class URRobot(object):
def stop(self): def stop(self):
self.stopj() self.stopj()
def cleanup(self): def close(self):
""" """
close connection to robot and stop internal thread close connection to robot and stop internal thread
""" """
self.logger.info("Closing sockets to robot") self.logger.info("Closing sockets to robot")
self.secmon.cleanup() self.secmon.close()
if self.rtmon: if self.rtmon:
self.rtmon.stop() 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): def set_freedrive(self, val):
""" """

View File

@ -32,12 +32,9 @@ class URRTMonitor(threading.Thread):
# pose is not included! # pose is not included!
rtstruct540 = struct.Struct('>d6d6d6d6d6d6d6d6d18d') rtstruct540 = struct.Struct('>d6d6d6d6d6d6d6d6d18d')
def __init__(self, urHost, loglevel=logging.WARNING): def __init__(self, urHost):
threading.Thread.__init__(self) threading.Thread.__init__(self)
self.logger = logging.getLogger(self.__class__.__name__) 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.daemon = True
self._stop_event = True self._stop_event = True
self._dataEvent = threading.Condition() self._dataEvent = threading.Condition()
@ -141,7 +138,7 @@ class URRTMonitor(threading.Thread):
timestamp = self.__recvTime timestamp = self.__recvTime
pkgsize = struct.unpack('>i', head)[0] pkgsize = struct.unpack('>i', head)[0]
self.logger.debug( self.logger.debug(
'Received header telling that package is %d bytes long' % 'Received header telling that package is %s bytes long',
pkgsize) pkgsize)
payload = self.__recv_bytes(pkgsize - 4) payload = self.__recv_bytes(pkgsize - 4)
if pkgsize >= 692: if pkgsize >= 692:
@ -150,7 +147,7 @@ class URRTMonitor(threading.Thread):
unp = self.rtstruct540.unpack(payload[:self.rtstruct540.size]) unp = self.rtstruct540.unpack(payload[:self.rtstruct540.size])
else: else:
self.logger.warning( self.logger.warning(
'Error, Received packet of length smaller than 540: ', 'Error, Received packet of length smaller than 540: %s ',
pkgsize) pkgsize)
return return
@ -165,9 +162,8 @@ class URRTMonitor(threading.Thread):
self._ctrlTimestamp - self._ctrlTimestamp -
self._last_ctrl_ts) > 0.010: self._last_ctrl_ts) > 0.010:
self.logger.warning( self.logger.warning(
"Error the controller failed to send us a packet: time since last packet {}s ".format( "Error the controller failed to send us a packet: time since last packet %s s ",
self._ctrlTimestamp - self._ctrlTimestamp - self._last_ctrl_ts)
self._last_ctrl_ts))
self._last_ctrl_ts = self._ctrlTimestamp self._last_ctrl_ts = self._ctrlTimestamp
self._qActual = np.array(unp[31:37]) self._qActual = np.array(unp[31:37])
self._qTarget = np.array(unp[1:7]) self._qTarget = np.array(unp[1:7])
@ -246,7 +242,7 @@ class URRTMonitor(threading.Thread):
#print(self.__class__.__name__+': Stopping') #print(self.__class__.__name__+': Stopping')
self._stop_event = True self._stop_event = True
def cleanup(self): def close(self):
self.stop() self.stop()
self.join() self.join()
@ -258,35 +254,3 @@ class URRTMonitor(threading.Thread):
self._rtSock.close() 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: else:
self.logger.debug("Message type parser not implemented %s", tmp) self.logger.debug("Message type parser not implemented %s", tmp)
else: 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 return allData
@ -178,12 +178,12 @@ class ParserUtils(object):
counter += 1 counter += 1
if counter > limit: 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("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 limit = limit * 10
elif len(data) >= psize: elif len(data) >= psize:
self.logger.debug("Got packet with size %s and type %s", psize, ptype) self.logger.debug("Got packet with size %s and type %s", psize, ptype)
if counter: 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" # ok we we have somehting which looks like a packet"
return (data[:psize], data[psize:]) return (data[:psize], data[psize:])
else: else:
@ -389,7 +389,7 @@ class SecondaryMonitor(Thread):
with self._dictLock: with self._dictLock:
return self._dict["RobotModeData"]["isProgramRunning"] return self._dict["RobotModeData"]["isProgramRunning"]
def cleanup(self): def close(self):
self._trystop = True self._trystop = True
self.join() self.join()
# with self._dataEvent: #wake up any thread that may be waiting for data before we close. Should we do that? # with self._dataEvent: #wake up any thread that may be waiting for data before we close. Should we do that?