From 185a88e219bacb047ae6eaa1a63894111152b632 Mon Sep 17 00:00:00 2001 From: Christian Weickhmann Date: Thu, 20 Jul 2017 09:51:04 +0200 Subject: [PATCH 1/2] Added deprecation flag to TMCL.bus.Bus.get_motor(). Cleaned up some comments. --- tmcl/bus.py | 135 ++++++++++++++++++++++++++++++++++---------------- tmcl/info.txt | 4 -- 2 files changed, 93 insertions(+), 46 deletions(-) delete mode 100644 tmcl/info.txt diff --git a/tmcl/bus.py b/tmcl/bus.py index eb34c45..ae24412 100644 --- a/tmcl/bus.py +++ b/tmcl/bus.py @@ -1,6 +1,45 @@ import struct from .motor import Motor from .reply import Reply, TrinamicException +import functools +import inspect +import warnings + +# NOTE(kgriffs): We don't want our deprecations to be ignored by default, +# so create our own type. +class DeprecatedWarning(UserWarning): + pass + +def deprecated(instructions): + """Flags a method as deprecated. + Args: + instructions: A human-friendly string of instructions, such + as: 'Please migrate to add_proxy() ASAP.' + By: Kurt Griffiths (kgriffs), https://gist.github.com/kgriffs/8202106 + """ + def decorator(func): + '''This is a decorator which can be used to mark functions + as deprecated. It will result in a warning being emitted + when the function is used.''' + @functools.wraps(func) + def wrapper(*args, **kwargs): + message = 'Call to deprecated function {}. {}'.format( + func.__name__, + instructions) + + frame = inspect.currentframe().f_back + + warnings.warn_explicit(message, + category=DeprecatedWarning, + filename=inspect.getfile(frame.f_code), + lineno=frame.f_lineno) + + return func(*args, **kwargs) + + return wrapper + + return decorator + # MSG_STRUCTURE = ">BBBBIB" @@ -17,46 +56,58 @@ REPLY_LENGTH_IIC = 8 -class Bus (object): - def __init__( self, serial, CAN = False ): - self.CAN = CAN - self.serial = serial - - def binaryadd(self, address, command, type, motorbank, value): - checksum_struct = struct.pack(MSG_STRUCTURE[:-1], address, command, type, motorbank, value) - checksum = 0 - for s in checksum_struct: - checksum += int(s) % 256 - checksum = checksum % 256 - return checksum - - def send ( self, address, command, type, motorbank, value ): - if self.CAN: - msg = struct.pack(MSG_STRUCTURE_CAN, command, type, motorbank,value) - self.serial.write(msg) - resp = [0] - data = self.serial.read(REPLY_LENGTH_CAN) - resp.extend(struct.unpack(REPLY_STRUCTURE_CAN, data)) - reply = Reply(resp) - return self._handle_reply(reply) - else: - checksum = self.binaryadd(address, command, type, motorbank, value) - msg = struct.pack(MSG_STRUCTURE, address, command, type, motorbank, value, checksum) # max_current gets applied wrong! Some internal rounding!? - self.serial.write(msg) - rep = self.serial.read(REPLY_LENGTH) - reply = Reply(struct.unpack(REPLY_STRUCTURE, rep)) - return self._handle_reply(reply) - - def _handle_reply (self, reply): - if reply.status < Reply.Status.SUCCESS: - raise TrinamicException(reply) - return reply - - def get_module (self, module_address = 1, motor = 0): - """ - Returns object addressing motor number 'motor' on module 'module_address'. - module_address defaults to 1 (doc for TMCM310 starts counting addresses at 1). - motor defaults to 0 (1st axis). - """ - return Motor(self, module_address, motor) +class Bus (object): + + def __init__( self, serial, CAN = False ): + self.CAN = CAN + self.serial = serial + + def binaryadd(self, address, command, type, motorbank, value): + checksum_struct = struct.pack(MSG_STRUCTURE[:-1], address, command, type, motorbank, value) + checksum = 0 + for s in checksum_struct: + checksum += int(s) % 256 + checksum = checksum % 256 + return checksum + + def send ( self, address, command, type, motorbank, value ): + if self.CAN: + msg = struct.pack(MSG_STRUCTURE_CAN, command, type, motorbank,value) + self.serial.write(msg) + resp = [0] + data = self.serial.read(REPLY_LENGTH_CAN) + resp.extend(struct.unpack(REPLY_STRUCTURE_CAN, data)) + reply = Reply(resp) + return self._handle_reply(reply) + else: + checksum = self.binaryadd(address, command, type, motorbank, value) + msg = struct.pack(MSG_STRUCTURE, address, command, type, motorbank, value, checksum) # max_current gets applied wrong! Some internal rounding!? + self.serial.write(msg) + rep = self.serial.read(REPLY_LENGTH) + reply = Reply(struct.unpack(REPLY_STRUCTURE, rep)) + return self._handle_reply(reply) + + def _handle_reply (self, reply): + if reply.status < Reply.Status.SUCCESS: + raise TrinamicException(reply) + return reply + + @deprecated("Please use `get_module` in the future.") + def get_motor (self, address): + """ + Deprecated! + Although the name suggests otherwise, this function retuns a handle + for a driver module (rather than a motor). + Use `get_module` instead, which also allows to select the default motor + driver on the module. + """ + return Motor(self, address) + + def get_module (self, module_address = 1, motor = 0): + """ + Returns object addressing motor number 'motor' on module 'module_address'. + module_address defaults to 1 (doc for TMCM310 starts counting addresses at 1). + motor defaults to 0 (1st axis). + """ + return Motor(self, module_address, motor) diff --git a/tmcl/info.txt b/tmcl/info.txt deleted file mode 100644 index 2a02ea2..0000000 --- a/tmcl/info.txt +++ /dev/null @@ -1,4 +0,0 @@ -Big fuckup: address! -It is the address of the module, should be set when connecting to it -rather than every time when doing something on the bus... -Also, the motor cannot be addressed directly. From 850b739fe6dfb3707f457584ced8dbf7323b99dd Mon Sep 17 00:00:00 2001 From: Christian Weickhmann Date: Thu, 20 Jul 2017 09:51:04 +0200 Subject: [PATCH 2/2] Added deprecation flag to TMCL.bus.Bus.get_motor(). Cleaned up some comments. --- tmcl/bus.py | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++- tmcl/info.txt | 4 ---- 2 files changed, 52 insertions(+), 5 deletions(-) delete mode 100644 tmcl/info.txt diff --git a/tmcl/bus.py b/tmcl/bus.py index eb34c45..e3e15ef 100644 --- a/tmcl/bus.py +++ b/tmcl/bus.py @@ -1,6 +1,45 @@ import struct from .motor import Motor from .reply import Reply, TrinamicException +import functools +import inspect +import warnings + +# NOTE(kgriffs): We don't want our deprecations to be ignored by default, +# so create our own type. +class DeprecatedWarning(UserWarning): + pass + +def deprecated(instructions): + """Flags a method as deprecated. + Args: + instructions: A human-friendly string of instructions, such + as: 'Please migrate to add_proxy() ASAP.' + By: Kurt Griffiths (kgriffs), https://gist.github.com/kgriffs/8202106 + """ + def decorator(func): + '''This is a decorator which can be used to mark functions + as deprecated. It will result in a warning being emitted + when the function is used.''' + @functools.wraps(func) + def wrapper(*args, **kwargs): + message = 'Call to deprecated function {}. {}'.format( + func.__name__, + instructions) + + frame = inspect.currentframe().f_back + + warnings.warn_explicit(message, + category=DeprecatedWarning, + filename=inspect.getfile(frame.f_code), + lineno=frame.f_lineno) + + return func(*args, **kwargs) + + return wrapper + + return decorator + # MSG_STRUCTURE = ">BBBBIB" @@ -17,8 +56,9 @@ REPLY_LENGTH_IIC = 8 -class Bus (object): +class Bus (object): + def __init__( self, serial, CAN = False ): self.CAN = CAN self.serial = serial @@ -53,6 +93,17 @@ def _handle_reply (self, reply): raise TrinamicException(reply) return reply + @deprecated("Please use `get_module` in the future.") + def get_motor (self, address): + """ + Deprecated! + Although the name suggests otherwise, this function retuns a handle + for a driver module (rather than a motor). + Use `get_module` instead, which also allows to select the default motor + driver on the module. + """ + return Motor(self, address) + def get_module (self, module_address = 1, motor = 0): """ Returns object addressing motor number 'motor' on module 'module_address'. diff --git a/tmcl/info.txt b/tmcl/info.txt deleted file mode 100644 index 2a02ea2..0000000 --- a/tmcl/info.txt +++ /dev/null @@ -1,4 +0,0 @@ -Big fuckup: address! -It is the address of the module, should be set when connecting to it -rather than every time when doing something on the bus... -Also, the motor cannot be addressed directly.