Skip to content

Conversation

whats2000
Copy link

What this does

This pull request improves camera compatibility, error handling, and motor position reading robustness.

Title Label
Improve camera backend & error handling (⚙️ Hardware)
Add retry logic for motor position reads (⚡️ Performance)

Camera compatibility and error handling

  • Switched the default OpenCV backend for Windows from cv2.CAP_MSMF to cv2.CAP_DSHOW in get_cv2_backend, improving camera support and stability.

  • Updated _validate_width_and_height in camera_opencv.py:

    • Logs warnings instead of raising errors when dimensions don’t match.
    • Automatically adjusts to the camera’s actual dimensions.
    • Correctly sets output dimensions based on rotation.

Motor position reading robustness

  • Added a num_retry=5 parameter to sync_read("Present_Position") calls in:

    • so101_follower.py
    • so101_leader.py
  • This adds retries for more reliable motor position reads.
    (diff 1, diff 2)


How it was tested

  • Verified camera behavior on Windows setup:

    • Confirmed DirectShow backend initializes successfully where MSMF failed.
    • Checked that resolution adjustments are logged correctly and video streams remain functional.
  • Tested new retry logic by simulating communication drops:

    • Observed stable reads after transient failures.
  • Confirmed that no regressions occurred in existing camera or motor tests.


How to checkout & try?

To verify the lerobot-teleoperate connection retry, please use the following command.
The sync will be much smoother and will successfully connect most of the time

lerobot-teleoperate `
    --robot.type=so101_follower `
    --robot.port=<Rebot Port> `
    --robot.id=my_awesome_follower_arm `
    --teleop.type=so101_leader `
    --teleop.port=<Teleop Port> `
    --teleop.id=my_awesome_leader_arm

The sync will be much smoother and will successfully connect most of the time. I have mostly connected and failed without retry (I think that maybe some latency for some hardware)

For camera, please verify the camera

lerobot-teleoperate `
    --robot.type=koch_follower `
    --robot.port=<Rebot Port> `
    --robot.id=my_awesome_follower_arm `
    --robot.cameras="{ front: {type: opencv, index_or_path: 0, width: 1920, height: 1080, fps: 30}}" `
    --teleop.type=koch_leader `
    --teleop.port=<Teleop Port> `
    --teleop.id=my_awesome_leader_arm `
    --display_data=true

@Copilot Copilot AI review requested due to automatic review settings October 9, 2025 12:22
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR improves camera compatibility on Windows and adds retry logic for motor position reads to increase robustness. The changes focus on enhancing hardware reliability and error handling.

  • Switch Windows camera backend from MSMF to DirectShow for better compatibility
  • Replace camera dimension validation errors with warnings and automatic adjustment
  • Add retry logic (5 attempts) to motor position reads for SO101 leader and follower robots

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
src/lerobot/cameras/utils.py Changes Windows OpenCV backend from MSMF to DirectShow
src/lerobot/cameras/opencv/camera_opencv.py Replaces dimension validation errors with warnings and auto-adjustment
src/lerobot/teleoperators/so101_leader/so101_leader.py Adds retry logic to motor position reads
src/lerobot/robots/so101_follower/so101_follower.py Adds retry logic to motor position reads

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines +251 to +254
if self.rotation in [cv2.ROTATE_90_CLOCKWISE, cv2.ROTATE_90_COUNTERCLOCKWISE]:
self.width, self.height = self.capture_height, self.capture_width
else:
self.width, self.height = self.capture_width, self.capture_height
Copy link

Copilot AI Oct 9, 2025

Choose a reason for hiding this comment

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

[nitpick] This dimension update logic should be extracted into a separate method to improve code organization and reusability. The logic for determining output dimensions based on rotation could be used elsewhere in the camera handling code.

Suggested change
if self.rotation in [cv2.ROTATE_90_CLOCKWISE, cv2.ROTATE_90_COUNTERCLOCKWISE]:
self.width, self.height = self.capture_height, self.capture_width
else:
self.width, self.height = self.capture_width, self.capture_height
self.width, self.height = self._get_output_dimensions(self.capture_width, self.capture_height, self.rotation)
Returns the output dimensions (width, height) based on the capture dimensions and rotation.
"""
if rotation in [cv2.ROTATE_90_CLOCKWISE, cv2.ROTATE_90_COUNTERCLOCKWISE]:
return capture_height, capture_width
else:
return capture_width, capture_height

Copilot uses AI. Check for mistakes.

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.

1 participant