Using the Dephy Loadcell Amplifier¶
This tutorial demonstrates how to use the Dephy Loadcell Amplifier with the Open Source Leg platform to measure forces and moments. It includes examples for both standard I2C communication and custom callbacks for advanced use cases, including using the Dephy Actpack to interface with the amplifier directly instead of the Raspberry Pi's I2C bus.
Hardware Setup¶
- Connect the loadcell to the Dephy Loadcell Amplifier and the amplifier to the Raspberry Pi via I2C. Alternatively, raw amplifier readings can be read from the
genvars
property of theDephyActuator
class and fed into the amplifier update method as a custom callback (see example below). - Verify proper power supply connections.
- Ensure proper grounding.
- Mount the loadcell securely.
This example shows how to:
- Initialize and configure a Dephy Loadcell Amplifier.
- Read forces and moments (6-axis measurements).
- Log loadcell measurements.
- Use custom callbacks for advanced data handling.
Code Structure¶
The tutorial script for reading loadcell data has two main functions. The first shows the standard implementation where the I2C bus on a Raspberry Pi is used to communicate with the strain amplifier. The second shows an alternative use where raw ADC values are passed to the sensor in a custom data callback function. This second implementation is useful when the raw values are provided via a method other than I2C, such as reading them directly from a Dephy Actuator.
1. Initialization¶
import numpy as np
from opensourceleg.actuators.dephy import DephyActuator
from opensourceleg.logging.logger import Logger
from opensourceleg.sensors.loadcell import DephyLoadcellAmplifier
from opensourceleg.utilities import SoftRealtimeLoop
FREQUENCY = 200
DT = 1 / FREQUENCY
LOADCELL_CALIBRATION_MATRIX = np.array([
(-38.72600, -1817.74700, 9.84900, 43.37400, -44.54000, 1824.67000),
(-8.61600, 1041.14900, 18.86100, -2098.82200, 31.79400, 1058.6230),
(
-1047.16800,
8.63900,
-1047.28200,
-20.70000,
-1073.08800,
-8.92300,
),
(20.57600, -0.04000, -0.24600, 0.55400, -21.40800, -0.47600),
(-12.13400, -1.10800, 24.36100, 0.02300, -12.14100, 0.79200),
(-0.65100, -28.28700, 0.02200, -25.23000, 0.47300, -27.3070),
])
This section:
- Sets up constants and configuration parameters
- Defines the calibration matrix
2. Standard Setup with I2C Communication¶
This section demonstrates how to use the loadcell with standard I2C communication. It includes:
- Initializing the loadcell with I2C parameters.
- Calibrating the loadcell.
- Reading and logging force/torque data.
def demo_loadcell_i2c(loadcell_logger, clock):
loadcell = DephyLoadcellAmplifier(
calibration_matrix=LOADCELL_CALIBRATION_MATRIX,
tag="loadcell",
amp_gain=125,
exc=5,
bus=1,
i2c_address=102,
)
loadcell_logger.track_variable(lambda: loadcell.fx, "Fx")
loadcell_logger.track_variable(lambda: loadcell.fy, "Fy")
loadcell_logger.track_variable(lambda: loadcell.fz, "Fz")
loadcell_logger.track_variable(lambda: loadcell.mx, "Mx")
loadcell_logger.track_variable(lambda: loadcell.my, "My")
loadcell_logger.track_variable(lambda: loadcell.mz, "Mz")
with loadcell:
loadcell.calibrate(reset=True)
for t in clock:
loadcell.update()
loadcell_logger.info(
f"Time: {t}; Fx: {loadcell.fx}; Fy: {loadcell.fy}; Fz: {loadcell.fz};"
f"Mx: {loadcell.mx}; My: {loadcell.my}; Mz: {loadcell.mz};"
)
loadcell_logger.update()
3. Custom Callback Communication¶
This section demonstrates how to use a custom callback to provide raw data to the loadcell. It includes:
- Using a DephyActuator to retrieve raw amplifier readings.
- Passing the raw data to the loadcell using a callback function.
- Calibrating the loadcell with the custom callback.
- Reading and logging force/torque data.
def demo_loadcell_actpack(loadcell_logger, clock):
actpack = DephyActuator(
port="/dev/ttyACM1",
gear_ratio=9,
frequency=FREQUENCY,
debug_level=0,
dephy_log=False,
)
loadcell = DephyLoadcellAmplifier(
calibration_matrix=LOADCELL_CALIBRATION_MATRIX,
tag="loadcell",
amp_gain=125,
exc=5,
)
loadcell_logger.track_variable(lambda: loadcell.fx, "Fx")
loadcell_logger.track_variable(lambda: loadcell.fy, "Fy")
loadcell_logger.track_variable(lambda: loadcell.fz, "Fz")
loadcell_logger.track_variable(lambda: loadcell.mx, "Mx")
loadcell_logger.track_variable(lambda: loadcell.my, "My")
loadcell_logger.track_variable(lambda: loadcell.mz, "Mz")
def get_raw_loadcell_actpack():
actpack.update()
return actpack.genvars
with actpack, loadcell:
loadcell.calibrate(reset=True, data_callback=get_raw_loadcell_actpack)
for t in clock:
loadcell.update(data_callback=get_raw_loadcell_actpack)
loadcell_logger.info(
f"Time: {t}; Fx: {loadcell.fx}; Fy: {loadcell.fy}; Fz: {loadcell.fz};"
f"Mx: {loadcell.mx}; My: {loadcell.my}; Mz: {loadcell.mz};"
)
loadcell_logger.update()
Important Class Parameters¶
When initializing the DephyLoadcellAmplifier
, several important parameters can be configured:
loadcell = DephyLoadcellAmplifier(
calibration_matrix=LOADCELL_CALIBRATION_MATRIX,
tag="loadcell",
amp_gain=125,
exc=5,
bus=1,
i2c_address=102,
)
Parameter Details¶
-
calibration_matrix (np.array):
- 6x6 matrix that converts raw sensor values to physical units
- Specific to each loadcell and must be provided for accurate measurements
- Obtained from manufacturer after calibration procedure
-
tag (str):
- Unique identifier for the loadcell instance
- Useful when using multiple sensors
-
amp_gain (float):
- Amplifier gain setting
- Typically 125 for the Dephy amplifier
- Affects sensitivity and measurement range
-
exc (float):
- Excitation voltage in volts
- Typically 5V for the Dephy amplifier
- Must match the hardware configuration
-
bus (int):
- I2C bus number
- Typically 1 on Raspberry Pi
- Use
i2cdetect -y 1
to verify
-
i2c_address (int):
- Device address on the I2C bus
- Default is 102 (0x66 in hexadecimal)
- Can be configured on some amplifiers
Available Properties¶
The DephyLoadcellAmplifier
provides six measurement properties:
-
Forces (fx, fy, fz):
- Linear forces in Newtons (N)
- Three orthogonal directions
-
Moments (mx, my, mz):
- Torques/moments in Newton-meters (Nm)
- Rotation around three orthogonal axes
Running the Example¶
-
Navigate to the tutorial directory:
-
Run the script:
-
Expected behavior:
- Loadcell begins reading force/torque data continuously at 200Hz
- Data is logged to
./logs/reading_loadcell_data.csv
- Force and moment values update as you apply loads to the sensor
-
To change between I2C and using custom data callbacks, swap the
if __name__ == "__main__"
between the two demo function calls:
demo_loadcell_actpack(loadcell_logger=loadcell_logger, clock=clock)
# demo_loadcell_i2c(loadcell_logger=loadcell_logger, clock = clock)
Common Issues¶
- I2C Communication Errors: Verify connections with
i2cdetect -y 1
- Permission Denied: Add user to i2c group:
sudo usermod -a -G i2c $USER
- Incorrect Readings: Check calibration matrix and amplifier settings
- Noise in Measurements: Verify grounding and power supply stability
- Drift in Readings: The loadcell might have to be re-calibrated by the manufacturer
Calibration¶
The calibration matrix is crucial for accurate measurements. The provided matrix:
LOADCELL_CALIBRATION_MATRIX = np.array([
(-38.72600, -1817.74700, 9.84900, 43.37400, -44.54000, 1824.67000),
# ... additional rows ...
])
If you have any questions or need further assistance, please post on the Open Source Leg community forum.