-
Notifications
You must be signed in to change notification settings - Fork 197
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bug fixes, send/receive debug logs and Unsigned 24 support #267
base: master
Are you sure you want to change the base?
Changes from 6 commits
5141b19
3ce8ab4
bc06df4
3fb146e
659899d
9b9efb7
a8fcd87
d0d9bba
b14f43e
56b206a
a108eee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
__pycache__/ | ||
*.py[cod] | ||
*$py.class | ||
.idea/ | ||
|
||
# C extensions | ||
*.so | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -79,7 +79,8 @@ def unsubscribe(self, can_id, callback=None): | |
if callback is None: | ||
del self.subscribers[can_id] | ||
else: | ||
self.subscribers[can_id].remove(callback) | ||
if callback in self.subscribers[can_id]: | ||
self.subscribers[can_id].remove(callback) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you describe the circumstances where raising a If a non-registered callback is passed, that is clearly the caller's fault and I think it's correct to raise an exception in response. The main place where this function is used is in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're right. I removed this change. |
||
|
||
def connect(self, *args, **kwargs): | ||
"""Connect to CAN bus using python-can. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -219,6 +219,17 @@ def add_member(self, variable): | |
self.subindices[variable.subindex] = variable | ||
self.names[variable.name] = variable | ||
|
||
class Unsigned24(struct.Struct): | ||
def __init__(self, *args, **kwargs): | ||
super(Unsigned24, self).__init__("<I", *args, **kwargs) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are we still trying to support Python 2? If not, this can be shortened to just Also, why does this use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed the super thing. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had to look it up in the Python library docs, but as I see it, the |
||
|
||
def unpack(self, data, *args, **kwargs): | ||
if isinstance(data, bytearray): | ||
while len(data) < 4: | ||
data += b'\x00' | ||
else: | ||
logger.error(f"Unsigned24.unpack received wrong type - {type(data)}") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use |
||
return super(Unsigned24, self).unpack(data, *args, **kwargs) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another |
||
|
||
class Variable(object): | ||
"""Simple variable.""" | ||
|
@@ -232,6 +243,7 @@ class Variable(object): | |
UNSIGNED8: struct.Struct("B"), | ||
UNSIGNED16: struct.Struct("<H"), | ||
UNSIGNED32: struct.Struct("<L"), | ||
UNSIGNED24: Unsigned24(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be ordered by size? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
UNSIGNED64: struct.Struct("<Q"), | ||
REAL32: struct.Struct("<f"), | ||
REAL64: struct.Struct("<d") | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -562,7 +562,7 @@ def get_data(self): | |
data = data | (~((1 << self.length) - 1)) | ||
data = od_struct.pack(data) | ||
else: | ||
data = self.pdo_parent.data[byte_offset:byte_offset + len(self.od) // 8] | ||
data = self.pdo_parent.data[byte_offset:byte_offset + self.length // 8] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using the cached value here seems like a good idea, but it does change the semantics. That should at least be explained in the commit message. Why not apply the same treatment in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I couldn't think of any reason len (self. OD) is used here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, I agree there are some corner cases in the code that don't deal well with mapping only some bits of an object. But to fix that properly, each use within By the way, my (hacky) solution so far was to adjust the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @acolomb This one |
||
|
||
return data | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,9 @@ class SdoClient(SdoBase): | |
#: Seconds to wait before sending a request, for rate limiting | ||
PAUSE_BEFORE_SEND = 0.0 | ||
|
||
# Seconds to wait before next read attempt for response in queue. For delayed responses. | ||
PAUSE_BEFORE_READ = 0.2 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps RETRY_DELAY or something would be more easily understandable. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the benefit of this mechanism? If there is a special case that requires such behavior, we should maybe make it optional by defaulting the constant to zero? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @acolomb If it fails to respond to the send_request that may be a sign that the device isn't ready and not necessarily a communication error. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a very slight notion, but |
||
|
||
def __init__(self, rx_cobid, tx_cobid, od): | ||
""" | ||
:param int rx_cobid: | ||
|
@@ -42,6 +45,7 @@ def __init__(self, rx_cobid, tx_cobid, od): | |
self.responses = queue.Queue() | ||
|
||
def on_response(self, can_id, data, timestamp): | ||
logger.debug(f"received response in {can_id} data {data}.") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As I understand it, it is preferable to use old-style string interpolation for logging because then the final message will only be constructed if needed. https://docs.python.org/3/howto/logging.html#logging-variable-data There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree, we should keep a consistent style here, meaning |
||
self.responses.put(bytes(data)) | ||
|
||
def send_request(self, request): | ||
|
@@ -50,6 +54,7 @@ def send_request(self, request): | |
try: | ||
if self.PAUSE_BEFORE_SEND: | ||
time.sleep(self.PAUSE_BEFORE_SEND) | ||
logger.debug(f"sending to {self.rx_cobid} data {request} ") | ||
self.network.send_message(self.rx_cobid, request) | ||
except CanError as e: | ||
# Could be a buffer overflow. Wait some time before trying again | ||
|
@@ -88,6 +93,7 @@ def request_response(self, sdo_request): | |
if not retries_left: | ||
self.abort(0x5040000) | ||
raise | ||
time.sleep(self.PAUSE_BEFORE_READ) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If an additional delay is appropriate here (see above), it should wait after logging, not before. |
||
logger.warning(str(e)) | ||
|
||
def abort(self, abort_code=0x08000000): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -172,6 +172,7 @@ def segmented_download(self, command, request): | |
self.send_response(response) | ||
|
||
def send_response(self, response): | ||
logger.debug(f"Sending to {self.tx_cobid} data {response}.") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another overly verbose log message. |
||
self.network.send_message(self.tx_cobid, response) | ||
|
||
def abort(self, abort_code=0x08000000): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a separate section for IDE stuff down below, if such things must be collected here.
This change seems unrelated though, so let's keep it in a separate commit / PR.