Skip to content

Commanding Current

This tutorial demonstrates how to command current to a Dephy actuator using the opensourceleg library. You'll learn how to implement a basic step response test by commanding a current setpoint.

Overview

Current control is fundamental for motor control applications. This example shows how to:

  • Initialize a Dephy actuator in current control mode
  • Command a current step input
  • Log and monitor the actuator's response

Code Structure

The tutorial script is organized into four main sections:

1. Configuration

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 applying current (1.0 second)
  • FREQUENCY: Control loop rate (1000 Hz)
  • CURRENT_SETPOINT: Target current (600 mA)

2. Initialization

def current_control():
    current_logger = Logger(
        log_path="./logs",
        file_name="current_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

    with actpack:
        actpack.set_control_mode(mode=CONTROL_MODES.CURRENT)
        actpack.set_current_gains()

        command_current = 0

        current_logger.track_variable(lambda: actpack.motor_current, "Motor Current")

Before the main loop, we:

  • Configure the actuator for current control mode
  • Initialize current control gains
  • Set up variables for tracking commanded and measured current

4. Control Loop

        for t in clock:
            if t > TIME_TO_STEP:
                command_current = CURRENT_SETPOINT
                actpack.set_motor_current(value=command_current)  # in mA

            actpack.update()

            current_logger.info(
                f"Time: {t}; " f"Command Current: {command_current}; " f"Motor Current: {actpack.motor_current}",
            )
            current_logger.update()

The main loop:

  1. Starts with zero current
  2. After TIME_TO_STEP, commands CURRENT_SETPOINT
  3. Updates actuator state
  4. Logs time, commanded current, and measured current

Running the Example

  1. Navigate to the tutorial directory:

    cd tutorials/actuators/dephy
    
  2. Run the script:

    python commanding_current.py
    
  3. Expected behavior:

    • t < 1.0s: Motor maintains 0 mA
    • t ≥ 1.0s: Motor steps to 600 mA
    • Data is continuously logged to ./logs/commanding_current.csv

If you have any questions or need further assistance, please post on the Open Source Leg community forum.