Skip to content
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

Handle failures on port out commands #79

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

finik
Copy link
Contributor

@finik finik commented Sep 14, 2020

Port out commands can fail (e.x obstruction of the movement), and it
will get the while executon stuck. The diff handles failures and
reports errors to the caller while at it, its useful to know when
the command fails.

Port out commands can fail (e.x obstruction of the movement), and it
will get the while executon stuck. The diff handles failures and
reports errors to the caller while at it, its useful to know when
the command fails.
@finik
Copy link
Contributor Author

finik commented Sep 19, 2020

any objections?

@codecov-commenter
Copy link

Codecov Report

Merging #79 into master will decrease coverage by 0.55%.
The diff coverage is 72.40%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #79      +/-   ##
==========================================
- Coverage   75.76%   75.20%   -0.56%     
==========================================
  Files          18       20       +2     
  Lines        2080     2267     +187     
==========================================
+ Hits         1576     1705     +129     
- Misses        504      562      +58     
Impacted Files Coverage Δ
pylgbst/comms/cgattlib.py 0.00% <0.00%> (ø)
setup.py 0.00% <ø> (ø)
pylgbst/comms/cbluepy.py 67.81% <25.00%> (ø)
pylgbst/comms/cgatt.py 42.55% <50.00%> (-2.13%) ⬇️
pylgbst/comms/cbleak.py 51.96% <51.96%> (ø)
pylgbst/__init__.py 34.09% <62.50%> (+6.59%) ⬆️
pylgbst/hub.py 93.00% <84.61%> (-1.71%) ⬇️
pylgbst/peripherals.py 75.66% <95.65%> (+0.21%) ⬆️
tests/test_cbleak.py 97.82% <97.82%> (ø)
pylgbst/comms/__init__.py 38.81% <100.00%> (+1.72%) ⬆️
... and 13 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 17ce398...adf5c09. Read the comment docs.

@@ -704,7 +704,7 @@ def bytes(self):

def is_reply(self, msg):
return isinstance(msg, MsgPortOutputFeedback) and msg.port == self.port \
and (msg.is_completed() or self.is_buffered)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to understand how self.is_buffered is not needed anymore. It were used for buffered commands that were just put into buffer, waiting the execution.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it is dead code for now, where is it being set? It is always false for now, all I could find is a note "support buffering" in another place. Did I miss anything?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not dead code. If peripheral is set to use buffered commands, this flag has to be respected. You are free to change the buffering flag anytime on peripheral, since it is a part of LEGO protocol.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Today none of them do?
  2. Before enabling them to do so, a proper SDK side state machine has to be implemented (the one that keeps track of the 2 pending commands inside the controller, rejects or buffers the rest on the SDK side (4.2 in the protocol). If I was implementing an asynchronous method that is what I would do, probably through in another command buffer on the SDK side as well, a callback function, to notify about execution of the specific command. But it sounded like you idea is to keep it simple(synchronous)? So what is the value of these buffered commands?
  3. (btw somewhat related to the sync/simple model). While initially experimenting with the lib I had a problem that took me while to figure out. I did a simple test "detect a press on a blue button, twist the motor as a result", It works once, then it gets into a deadlock, took me a while to figure out. Basically it seems users can't call the SDK from an SDK callback?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Probably no. But I want to keep the intent to support it. For example, you know it's there, you know it lacks good implementation. This is a positive thing IMO, somebody might contribute the implementation.
  2. I'm not "all or nothing" guy. I wish I had time to make proper implementation of buffered commands. Indeed, I would keep it as simple as possible, leaving complex state handling to the user of those buffered modes. Complicated and asynchronous is out of scope for this lib, unless we find simple wrapping.
  3. I am sure there can be bugs leading to deadlocks. It is hard to catch those, especially when multiple BLE backends have to be supported, each with own specifics of processing. In general, calling library functions from callbacks are fine, unless it logically leads to deadlock.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with keeping it simple and being against all or nothing, your library is awesome in that sense. It provides a very useful and simplified sync API. The fact that Lego protocol allows keeping the next bullet in the chamber, that's cool, but why? Anyway, I can bring back the "or self.is_buffered", but I think it will just create confusion for now, the contract of the SDK right now is "motor commands are blocking", they do not return until they either succeed or fail. With this flag, it will return immediately for some, and that return will sometimes be triggered by the engine starting that command, sometimes by finishing a previous command!? So my approach to "all or nothing" is introduce the functionality incrementally, but keep the API consistent and keep behavior well defined at every phase. Anyhow, here is a the latest demo: https://www.youtube.com/watch?v=myctUKspBio I will cleanup and put out the latest version of the code, hopefully tonight

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The video is amazing! I'm gonna show it to my daughter!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants