Skip to content

Commanding Voltage

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

Overview

Voltage control is the most basic form of motor control. This example shows how to:

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

Code Structure

The tutorial script is organized into four main sections:

1. Configuration

import time

from opensourceleg.actuators.base import CONTROL_MODES
from opensourceleg.actuators.dephy import DephyActuator
from opensourceleg.logging.logger import Logger
from opensourceleg.utilities import SoftRealtimeLoop

FREQUENCY = 1000
TIME_TO_STEP = 1.0
DT = 1 / FREQUENCY
GEAR_RATIO = 1.0

Key parameters:

  • TIME_TO_STEP: Delay before applying voltage (1.0 second)
  • FREQUENCY: Control loop rate (1000 Hz)
  • GEAR_RATIO: Actuator gear ratio (1.0)

2. Initialization

def voltage_control():
    voltage_logger = Logger(
        log_path="./logs",
        file_name="voltage_control",
    )

    actpack = DephyActuator(
        port="/dev/ttyACM0",
        gear_ratio=GEAR_RATIO,
        frequency=FREQUENCY,
        debug_level=0,
        dephy_log=False,
    )

    command_voltage = 0

    voltage_logger.track_variable(lambda: command_voltage, "Command Voltage")
    voltage_logger.track_variable(lambda: actpack.motor_voltage, "Motor Voltage")
    voltage_logger.track_variable(lambda: time.time(), "Time")

    clock = SoftRealtimeLoop(dt=DT)

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.VOLTAGE)

Before the main loop, we:

  • Initialize command voltage to zero
  • Set up logging variables for voltage and time
  • Configure the real-time loop

4. Control Loop

        for t in clock:
            actpack.update()

            if t > TIME_TO_STEP:
                command_voltage = 1000

            actpack.set_motor_voltage(value=command_voltage)

            voltage_logger.info(
                f"Time: {t}; "
                f"Command Voltage: {command_voltage}; "
                f"Motor Voltage: {actpack.motor_voltage}; "

The main loop:

  1. Starts with zero voltage
  2. After TIME_TO_STEP, commands 1000 mV
  3. Updates actuator state
  4. Logs time, motor voltage, and motor current

Running the Example

  1. Navigate to the tutorial directory:

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

    python commanding_voltage.py
    
  3. Expected behavior:

    • t < 1.0s: Motor maintains 0 mV
    • t ≥ 1.0s: Motor steps to 1000 mV
    • Data is continuously logged to ./logs/commanding_voltage.csv

Additional Notes

  • Voltage control provides no feedback regulation
  • Motor speed will vary with load under constant voltage
  • This mode is useful for basic testing and characterization of the motor

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