Commanding Impedance¶
This tutorial demonstrates how to implement impedance control with a Dephy actuator using the opensourceleg
library. You'll learn how to command position setpoints while maintaining compliant behavior through impedance control.
Overview¶
Impedance control allows for position control while maintaining a specified dynamic relationship between position and force. This example shows how to:
- Initialize a Dephy actuator in impedance control mode
- Command a position step input
- Monitor position tracking and motor current
Code Structure¶
The tutorial script is organized into four main sections:
1. Configuration¶
import time
import numpy as np
from opensourceleg.actuators.base import CONTROL_MODES
from opensourceleg.actuators.dephy import DephyActuator
from opensourceleg.logging.logger import Logger
from opensourceleg.utilities import SoftRealtimeLoop
TIME_TO_STEP = 1.0
FREQUENCY = 1000
DT = 1 / FREQUENCY
GEAR_RATIO = 1.0
Key parameters:
TIME_TO_STEP
: Delay before position step (1.0 second)FREQUENCY
: Control loop rate (1000 Hz)GEAR_RATIO
: Actuator gear ratio (1.0)
2. Initialization¶
def impedance_control():
impedance_logger = Logger(
log_path="./logs",
file_name="impedance_control",
)
actpack = DephyActuator(
port="/dev/ttyACM0",
gear_ratio=GEAR_RATIO,
frequency=FREQUENCY,
debug_level=0,
dephy_log=False,
This section:
- Creates a data logger for recording measurements
- Initializes the Dephy actuator with specified parameters
- Sets up a real-time loop for consistent timing
3. Control Setup¶
clock = SoftRealtimeLoop(dt=DT)
impedance_logger.set_stream_terminator("\r")
with actpack:
actpack.update()
actpack.set_control_mode(mode=CONTROL_MODES.IMPEDANCE)
actpack.set_impedance_gains()
current_position = actpack.output_position
command_position = current_position
Before the main loop, we:
- Configure the actuator for impedance control mode
- Initialize impedance control gains
- Get initial position and set up command position
- Configure logging variables for position and current
4. Control Loop¶
impedance_logger.track_variable(lambda: command_position, "Command Position")
impedance_logger.track_variable(lambda: actpack.motor_current, "Motor Current")
impedance_logger.track_variable(lambda: time.time(), "Time")
for t in clock:
actpack.update()
if t > TIME_TO_STEP:
command_position = current_position + np.pi / 2
actpack.set_output_position(value=command_position)
impedance_logger.info(
f"Time: {t}; "
f"Command Position: {command_position}; "
f"Output Position: {actpack.output_position}; "
The main loop:
- Starts at current position
- After
TIME_TO_STEP
, commands a π/2 radian (90 degree) position step - Updates actuator state
- Logs time, positions, and motor current
Running the Example¶
-
Navigate to the tutorial directory:
-
Run the script:
-
Expected behavior:
- t < 1.0s: Motor maintains initial position
- t ≥ 1.0s: Motor moves to position + π/2 radians
- Movement will be compliant due to impedance control
- Data is continuously logged to
./logs/commanding_impedance.csv
If you have any questions or need further assistance, please post on the Open Source Leg community forum.