mirror of
https://github.com/huggingface/lerobot.git
synced 2026-05-26 14:09:47 +00:00
feat(robots): add earth rover robot support (#2575)
Co-authored-by: somthecoder <sbaner64@gmail.com> Co-authored-by: randomSmarts <Aarshsmittal@gmail.com> Co-authored-by: Hassoonu <halsae2@illinois.edu> Co-authored-by: Saketh06 <saketh.kantipudi@gmail.com> Co-authored-by: sairajshetye <sairajshetye2@gmail.com> Co-authored-by: Khalil Meftah <kmeftah.khalil@gmail.com>
This commit is contained in:
@@ -25,7 +25,11 @@ from lerobot.utils.errors import DeviceAlreadyConnectedError, DeviceNotConnected
|
||||
|
||||
from ..teleoperator import Teleoperator
|
||||
from ..utils import TeleopEvents
|
||||
from .configuration_keyboard import KeyboardEndEffectorTeleopConfig, KeyboardTeleopConfig
|
||||
from .configuration_keyboard import (
|
||||
KeyboardEndEffectorTeleopConfig,
|
||||
KeyboardRoverTeleopConfig,
|
||||
KeyboardTeleopConfig,
|
||||
)
|
||||
|
||||
PYNPUT_AVAILABLE = True
|
||||
try:
|
||||
@@ -289,3 +293,158 @@ class KeyboardEndEffectorTeleop(KeyboardTeleop):
|
||||
TeleopEvents.SUCCESS: success,
|
||||
TeleopEvents.RERECORD_EPISODE: rerecord_episode,
|
||||
}
|
||||
|
||||
|
||||
class KeyboardRoverTeleop(KeyboardTeleop):
|
||||
"""
|
||||
Keyboard teleoperator for mobile robots like EarthRover Mini Plus.
|
||||
|
||||
Provides intuitive WASD-style controls for driving a mobile robot:
|
||||
- Linear movement (forward/backward)
|
||||
- Angular movement (turning/rotation)
|
||||
- Speed adjustment
|
||||
- Emergency stop
|
||||
|
||||
Keyboard Controls:
|
||||
Movement:
|
||||
- W: Move forward
|
||||
- S: Move backward
|
||||
- A: Turn left (with forward motion)
|
||||
- D: Turn right (with forward motion)
|
||||
- Q: Rotate left in place
|
||||
- E: Rotate right in place
|
||||
- X: Emergency stop
|
||||
|
||||
Speed Control:
|
||||
- +/=: Increase speed
|
||||
- -: Decrease speed
|
||||
|
||||
System:
|
||||
- ESC: Disconnect teleoperator
|
||||
|
||||
Attributes:
|
||||
config: Teleoperator configuration
|
||||
current_linear_speed: Current linear velocity magnitude
|
||||
current_angular_speed: Current angular velocity magnitude
|
||||
|
||||
Example:
|
||||
```python
|
||||
from lerobot.teleoperators.keyboard import KeyboardRoverTeleop, KeyboardRoverTeleopConfig
|
||||
|
||||
teleop = KeyboardRoverTeleop(
|
||||
KeyboardRoverTeleopConfig(linear_speed=1.0, angular_speed=1.0, speed_increment=0.1)
|
||||
)
|
||||
teleop.connect()
|
||||
|
||||
while teleop.is_connected:
|
||||
action = teleop.get_action()
|
||||
robot.send_action(action)
|
||||
```
|
||||
"""
|
||||
|
||||
config_class = KeyboardRoverTeleopConfig
|
||||
name = "keyboard_rover"
|
||||
|
||||
def __init__(self, config: KeyboardRoverTeleopConfig):
|
||||
super().__init__(config)
|
||||
# Add rover-specific speed settings
|
||||
self.current_linear_speed = config.linear_speed
|
||||
self.current_angular_speed = config.angular_speed
|
||||
|
||||
@property
|
||||
def action_features(self) -> dict:
|
||||
"""Return action format for rover (linear and angular velocities)."""
|
||||
return {
|
||||
"linear.vel": float,
|
||||
"angular.vel": float,
|
||||
}
|
||||
|
||||
@property
|
||||
def is_calibrated(self) -> bool:
|
||||
"""Rover teleop doesn't require calibration."""
|
||||
return True
|
||||
|
||||
def _drain_pressed_keys(self):
|
||||
"""Update current_pressed state from event queue without clearing held keys"""
|
||||
while not self.event_queue.empty():
|
||||
key_char, is_pressed = self.event_queue.get_nowait()
|
||||
if is_pressed:
|
||||
self.current_pressed[key_char] = True
|
||||
else:
|
||||
# Only remove key if it's being released
|
||||
self.current_pressed.pop(key_char, None)
|
||||
|
||||
def get_action(self) -> dict[str, Any]:
|
||||
"""
|
||||
Get the current action based on pressed keys.
|
||||
|
||||
Returns:
|
||||
dict with 'linear.vel' and 'angular.vel' keys
|
||||
"""
|
||||
before_read_t = time.perf_counter()
|
||||
|
||||
if not self.is_connected:
|
||||
raise DeviceNotConnectedError(
|
||||
"KeyboardRoverTeleop is not connected. You need to run `connect()` before `get_action()`."
|
||||
)
|
||||
|
||||
self._drain_pressed_keys()
|
||||
|
||||
linear_velocity = 0.0
|
||||
angular_velocity = 0.0
|
||||
|
||||
# Check which keys are currently pressed (not released)
|
||||
active_keys = {key for key, is_pressed in self.current_pressed.items() if is_pressed}
|
||||
|
||||
# Linear movement (W/S) - these take priority
|
||||
if "w" in active_keys:
|
||||
linear_velocity = self.current_linear_speed
|
||||
elif "s" in active_keys:
|
||||
linear_velocity = -self.current_linear_speed
|
||||
|
||||
# Turning (A/D/Q/E)
|
||||
if "d" in active_keys:
|
||||
angular_velocity = -self.current_angular_speed
|
||||
if linear_velocity == 0: # If not moving forward/back, add slight forward motion
|
||||
linear_velocity = self.current_linear_speed * self.config.turn_assist_ratio
|
||||
elif "a" in active_keys:
|
||||
angular_velocity = self.current_angular_speed
|
||||
if linear_velocity == 0: # If not moving forward/back, add slight forward motion
|
||||
linear_velocity = self.current_linear_speed * self.config.turn_assist_ratio
|
||||
elif "q" in active_keys:
|
||||
angular_velocity = self.current_angular_speed
|
||||
linear_velocity = 0 # Rotate in place
|
||||
elif "e" in active_keys:
|
||||
angular_velocity = -self.current_angular_speed
|
||||
linear_velocity = 0 # Rotate in place
|
||||
|
||||
# Stop (X) - overrides everything
|
||||
if "x" in active_keys:
|
||||
linear_velocity = 0
|
||||
angular_velocity = 0
|
||||
|
||||
# Speed adjustment
|
||||
if "+" in active_keys or "=" in active_keys:
|
||||
self.current_linear_speed += self.config.speed_increment
|
||||
self.current_angular_speed += self.config.speed_increment * self.config.angular_speed_ratio
|
||||
logging.info(
|
||||
f"Speed increased: linear={self.current_linear_speed:.2f}, angular={self.current_angular_speed:.2f}"
|
||||
)
|
||||
if "-" in active_keys:
|
||||
self.current_linear_speed = max(
|
||||
self.config.min_linear_speed, self.current_linear_speed - self.config.speed_increment
|
||||
)
|
||||
self.current_angular_speed = max(
|
||||
self.config.min_angular_speed,
|
||||
self.current_angular_speed - self.config.speed_increment * self.config.angular_speed_ratio,
|
||||
)
|
||||
logging.info(
|
||||
f"Speed decreased: linear={self.current_linear_speed:.2f}, angular={self.current_angular_speed:.2f}"
|
||||
)
|
||||
|
||||
self.logs["read_pos_dt_s"] = time.perf_counter() - before_read_t
|
||||
|
||||
return {
|
||||
"linear.vel": linear_velocity,
|
||||
"angular.vel": angular_velocity,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user