Skip to content

Reading Sensors

This tutorial demonstrates how to read sensor data from a Dephy actuator using the opensourceleg library. You'll learn how to continuously monitor and log actuator sensor values.

Overview

This example shows how to:

  • Initialize a Dephy actuator for sensor reading
  • Continuously monitor sensor values
  • Log sensor data for analysis

Code Structure

The tutorial script is organized into three main sections:

1. Configuration

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

FREQUENCY = 1000
DT = 1 / FREQUENCY
GEAR_RATIO = 1.0

Key parameters:

  • FREQUENCY: Sensor reading rate (1000 Hz)
  • GEAR_RATIO: Actuator gear ratio (1.0)

2. Initialization

if __name__ == "__main__":
    sensor_logger = Logger(
        log_path="./logs",
        file_name="reading_sensor_data",
    )
    clock = SoftRealtimeLoop(dt=DT)
    actpack = DephyActuator(
        port="/dev/ttyACM0",
        gear_ratio=GEAR_RATIO,
        frequency=FREQUENCY,
        debug_level=0,
        dephy_log=False,
    )
    sensor_logger.track_variable(lambda: actpack.motor_position, "Motor Position")
    sensor_logger.track_variable(lambda: actpack.motor_current, "Motor Current")

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
  • Configures which sensor variables to track

3. Main Loop

    with actpack:
        for t in clock:
            actpack.update()
            sensor_logger.info(f"Time: {t}; Motor Position: {actpack.motor_position};")
            sensor_logger.update()

The main loop:

  1. Updates actuator state to get fresh sensor readings
  2. Logs time and sensor values
  3. Updates the logger

Running the Example

  1. Navigate to the tutorial directory:

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

    python reading_sensor_data.py
    
  3. Expected behavior:

    • Continuous reading of sensor values
    • Data logged to ./logs/reading_sensor_data.csv
    • No active control (read-only operation)

Additional Notes

  • This script operates in read-only mode
  • No control commands are sent to the actuator, feel free to move the actuator output and see the sensor values change

Available Sensor Readings

The Dephy actuator provides several sensor values that can be tracked, you can find the list of all the available sensor readings in the Actuator API documentation.

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