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

[FEATURE REQUEST] Context manager for dynamixel bus #92

Open
ntjess opened this issue Jul 1, 2020 · 2 comments
Open

[FEATURE REQUEST] Context manager for dynamixel bus #92

ntjess opened this issue Jul 1, 2020 · 2 comments
Labels
enhancement New feature or request

Comments

@ntjess
Copy link

ntjess commented Jul 1, 2020

Is your feature request related to a problem? Please describe.

Currently, manual interactions with a device will entail something like:

bus = DynamixelBus(...)
# Make some servos
# Do some operations

# De-torque servos
bus.close()

However, if any of the operations result in an exception, the bus will never be closed and the servos may still have torque enabled. This may cause errors restarting the code.

Describe the solution you'd like
A context manager might solve some of these issues quite nicely:

with DynamixelBus(...) as bus:
  # Make some servos
  # Do some operations
# If an error occurred, the __exit__ method will safely close the bus

The tricky part would be determining the best way to clean up the devices constructed within the context manager scope. I was thinking something like

def close_servo(_servo):
  _servo.torque_enable.value = False

with DynamixelBus(...) as bus:
  servo = ...
  bus.exit_callbacks.append(partial(close_servo, servo))
  # Do the things

Then, the __exit__ function of BaseBus will dispatch each callback in exit_callbacks when the context manager is out of scope.

Describe alternatives you've considered
I have been placing some tentative clean-up code in a try-catch block so far.

Additional context
Add any other context or screenshots about the feature request here.

@ntjess ntjess added the enhancement New feature or request label Jul 1, 2020
@ntjess
Copy link
Author

ntjess commented Jul 1, 2020

Alternatively, the YAML file for a device could contain instructions on how to safely shut it down. This sequence of specified operations could be executed by the bus for each connected component.

@sonelu
Copy link
Owner

sonelu commented Jul 1, 2020

I don't think it is a problem to add context management for DynamixelBus or even to the parent class BaseBus to support that scenario.

But be aware that the thinking about activating the torque is the following:

  • BaseRobot constructor only creates the buses, devices, joints, syncs, and the JointManager
  • when the BaseRobot's start is called it opens the buses, and the devices (which practically reads the current registers of all devices); devices' torque is not affected
  • the BaseRobot calls the JointManager to start(); this triggers the activation of all joints associated with the JointManager; it is at this moment that the torque is activated provided that:
    • joints have an activate register
    • joints have auto set to True (which is default)

Stopping the torques happen when:

  • the BaseRobot stop is called; it will first stop the JointManager, that deactivates the joints if they have an activate register and auto is True
  • then stops the syncs and closes the buses

The complication arises from the fact that there are many threads using the bus: the JointManager handles the joints and all sync threads that replicate read and write values to devices. So it is physically impossible to put everything under one context manager.

All IO operations are under try block and they are intercepted and logged to the logging instead of raising exceptions.

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

No branches or pull requests

2 participants