Reads data from Dynamixel motors. This wraps a GroupBulkRead from the DynamixelSDK.
| 347 | |
| 348 | |
| 349 | class DynamixelReader: |
| 350 | """Reads data from Dynamixel motors. |
| 351 | |
| 352 | This wraps a GroupBulkRead from the DynamixelSDK. |
| 353 | """ |
| 354 | |
| 355 | def __init__(self, client: DynamixelClient, motor_ids: Sequence[int], |
| 356 | address: int, size: int): |
| 357 | """Initializes a new reader.""" |
| 358 | self.client = client |
| 359 | self.motor_ids = motor_ids |
| 360 | self.address = address |
| 361 | self.size = size |
| 362 | self._initialize_data() |
| 363 | |
| 364 | self.operation = self.client.dxl.GroupBulkRead(client.port_handler, |
| 365 | client.packet_handler) |
| 366 | |
| 367 | for motor_id in motor_ids: |
| 368 | success = self.operation.addParam(motor_id, address, size) |
| 369 | if not success: |
| 370 | raise OSError( |
| 371 | '[Motor ID: {}] Could not add parameter to bulk read.' |
| 372 | .format(motor_id)) |
| 373 | |
| 374 | def read(self, retries: int = 1): |
| 375 | """Reads data from the motors.""" |
| 376 | self.client.check_connected() |
| 377 | success = False |
| 378 | while not success and retries >= 0: |
| 379 | comm_result = self.operation.txRxPacket() |
| 380 | success = self.client.handle_packet_result( |
| 381 | comm_result, context='read') |
| 382 | retries -= 1 |
| 383 | |
| 384 | # If we failed, send a copy of the previous data. |
| 385 | if not success: |
| 386 | return self._get_data() |
| 387 | |
| 388 | errored_ids = [] |
| 389 | for i, motor_id in enumerate(self.motor_ids): |
| 390 | # Check if the data is available. |
| 391 | available = self.operation.isAvailable(motor_id, self.address, |
| 392 | self.size) |
| 393 | if not available: |
| 394 | errored_ids.append(motor_id) |
| 395 | continue |
| 396 | |
| 397 | self._update_data(i, motor_id) |
| 398 | |
| 399 | if errored_ids: |
| 400 | logging.error('Bulk read data is unavailable for: %s', |
| 401 | str(errored_ids)) |
| 402 | |
| 403 | return self._get_data() |
| 404 | |
| 405 | def _initialize_data(self): |
| 406 | """Initializes the cached data.""" |
nothing calls this directly
no outgoing calls
no test coverage detected