Refactor connection lifecycle for asynchronous sessions contexts#27
Refactor connection lifecycle for asynchronous sessions contexts#27michaelosthege wants to merge 6 commits intoCommonplaceRobotics:masterfrom
Conversation
This way one can create a client with only non-dangerous read methods.
|
While testing today I already learned that entering the context should definitely await until the first |
|
I can confirm that the code from this PR works. My two biggest concerns at this point:
What do you think? |
| raise CRICommandError("Kinematics not ready.") | ||
| yield controller | ||
| # Graceful context exit: give up control and disable robot, then disconnect in finally block. | ||
| controller.disable() |
There was a problem hiding this comment.
TODO (MO): don't do this by default
| if not controller.enable(): | ||
| raise CRICommandError("Failed to enable robot.") |
There was a problem hiding this comment.
TODO (@cpr-olt): does this automatically do reset under the hood?
Bei unserer Anlage müssen wir immer damit rechnen, dass der Mensch eine Tür öffnet: Wäre es dafür nicht besser wenn disable() automatisch gemacht wird, damit die Motoren dann schon stromlos sind?
There was a problem hiding this comment.
In Kapitel 4.6. sind diese Kommandos nicht genauer beschrieben:
CRISTART 1234 CMD Connect CRIEND
CRISTART 1234 CMD Disconnect CRIEND
CRISTART 1234 CMD Reset CRIEND
CRISTART 1234 CMD Enable CRIEND
CRISTART 1234 CMD Disable CRIEND
This replaces #26 where I tried to work around an issue that I ran into due to unclean connect/disconnect/dispose resource management when connections failed.
Essentially the threads must be regarded as "single use", but this requires them to be recreated if the same
CRIControllershould be re-used.Even more convenient would be a context manager that deals with proper disconnects and resource disposal, regardless of user level implementation.
Based on my comment here I refactored in the following way:
connectis changed toraiseon errors -- this is a breaking change, but it's much easier to work with as it enables passing contextual information up the stack (through exception types and messages) which is not the case withreturn False.CRIControllerclass is split intoCRIClientandCRIController(CRIClient)which makes it possible to separate observational and actuating code based on type information.CRIConnectoris introduced: This is a factory-like creator of connection session contexts.asynccontext managers.I also added an example that uses the
CRIConnectorcontexts.This asynchronous context manager style connection is relevant for us for multiple reasons:
CRIClientcan be long-lived, auto-reconnectiing to monitor whileCRIControlleris only invoked for movementWhat do you think, @cpr-bar ?