Commanding Position¶
This tutorial demonstrates how to command position to a Dephy actuator using the opensourceleg
library. You'll learn how to implement a basic step response test by commanding a position setpoint.
Overview¶
Position control allows direct control of the actuator's angular position. This example shows how to:
- Initialize a Dephy actuator in position control mode
- Command a position step input
- Log and monitor the actuator's response
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 position_control():
position_logger = Logger(
log_path="./logs",
file_name="position_control",
)
actpack = DephyActuator(
port="/dev/ttyACM0", gear_ratio=GEAR_RATIO, frequency=FREQUENCY, debug_level=0, dephy_log=False
)
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.POSITION)
actpack.set_position_gains()
actpack.update()
current_position = actpack.output_position
command_position = current_position
position_logger.track_variable(lambda: actpack.output_position, "Output Position")
position_logger.track_variable(lambda: command_position, "Command Position")
position_logger.track_variable(lambda: time.time(), "Time")
Before the main loop, we:
- Configure the actuator for position control mode
- Initialize position control gains
- Get initial position and set up command position
- Configure logging variables for tracking positions
4. Control Loop¶
for t in clock:
if t > TIME_TO_STEP:
command_position = current_position + (1 / 2) * np.pi
actpack.set_output_position(value=command_position)
actpack.update()
position_logger.info(f"Time: {t}; \
Command Position: {command_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 and position data
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 (90 degrees)
- Data is continuously logged to
./logs/commanding_position.csv
If you have any questions or need further assistance, please post on the Open Source Leg community forum.