Skip to content

[FEATURE REQUEST] Context manager for dynamixel bus #92

@ntjess

Description

@ntjess

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions