Using the Lord Microstrain IMU¶
This tutorial demonstrates how to use the Lord Microstrain IMU with the Open Source Leg platform.
Hardware Setup¶
- Connect the Lord Microstrain IMU to your computer via USB
- Note the device port (typically
/dev/ttyUSB0
) - Mount the IMU securely with proper orientation
- Verify the LED indicators are active
This example shows how to:
- Initialize and configure a Lord Microstrain IMU
- Read orientation, angular velocity, and acceleration data
- Log IMU measurements
Code Structure¶
The tutorial script is organized into several main sections:
1. Initialization¶
from opensourceleg.logging.logger import Logger
from opensourceleg.sensors.imu import LordMicrostrainIMU
from opensourceleg.utilities import SoftRealtimeLoop
FREQUENCY = 200
DT = 1 / FREQUENCY
if __name__ == "__main__":
imu_logger = Logger(
log_path="./logs",
file_name="reading_imu_data",
)
clock = SoftRealtimeLoop(dt=DT)
imu = LordMicrostrainIMU(
tag="LordMicrostrainIMU",
port=r"/dev/ttyS0",
baud_rate=921600,
frequency=FREQUENCY,
update_timeout=500,
max_packets=1,
This section:
- Creates a data logger for recording measurements
- Sets up a real-time loop for consistent timing
- Initializes the LordMicrostrainIMU with specified parameters
- Configures variable tracking for roll, pitch, and yaw angles
2. Main Loop¶
offline=False,
)
imu_logger.track_variable(lambda: imu.roll, "Roll")
imu_logger.track_variable(lambda: imu.pitch, "Pitch")
imu_logger.track_variable(lambda: imu.yaw, "Yaw")
with imu:
for t in clock:
imu.update()
imu_logger.info(f"Time: {t}; Roll: {imu.roll}; Pitch: {imu.pitch}; Yaw: {imu.yaw};")
The main loop:
- Updates the IMU to get the latest reading
- Logs the time and current orientation data
- Updates the logger
IMU Parameters¶
When initializing the LordMicrostrainIMU, several important parameters can be configured:
imu = LordMicrostrainIMU(
tag="LordMicrostrainIMU",
port=r"/dev/ttyS0",
baud_rate=921600,
frequency=FREQUENCY,
update_timeout=500,
max_packets=1,
return_packets=False,
offline=False,
)
Parameter Details¶
-
tag (str):
- Unique identifier for the IMU instance
- Useful when using multiple sensors
- Defaults to "LordMicrostrainIMU"
-
port (str):
- Specifies the serial port where the IMU is connected
- Default is
/dev/ttyUSB0
- Use
ls /dev/ttyUSB*
to see available devices
-
baud_rate (int):
- Communication speed for serial connection
- Default is 921600 baud
- Must match IMU's configured baud rate
-
frequency (int):
- Data streaming frequency in Hz
- Default is 200 Hz
- Higher rates provide more data but increase processing load
-
update_timeout (int):
- Timeout for data packet retrieval in milliseconds
- Default is 500ms
- Increase if data retrieval is unreliable
-
max_packets (int):
- Maximum number of data packets to retrieve per update
- Default is 1
- Higher values can buffer more data
-
return_packets (bool):
- If True, returns the raw data packets in the update() method
- Default is False
- Useful for advanced processing
Available Properties¶
The LordMicrostrainIMU provides several useful properties:
-
Orientation (roll, pitch, yaw):
- Current angular orientation in radians
- Returned as Euler angles
-
Angular Velocity (vel_x, vel_y, vel_z):
- Angular rates in radians per second
-
Linear Acceleration (acc_x, acc_y, acc_z):
- Linear accelerations in m/s²
-
Raw Data:
- Access to complete data packet
Running the Example¶
-
Navigate to the tutorial directory:
-
Run the script:
-
Expected behavior:
- IMU begins reading orientation data continuously at 200Hz
- Data is logged to
./logs/reading_imu_data.csv
- Roll, pitch, and yaw values update as you rotate the IMU
Common Issues¶
- Device Not Found: Verify connections and run
ls /dev/ttyUSB*
- Permission Denied: Add user to dialout group:
sudo usermod -a -G dialout $USER
- Incorrect Readings: Check IMU orientation and mounting
- Missing Data: Verify baud rate and connection quality
- MSCL Import Error: Install the MSCL library from Lord Microstrain
Additional Resources¶
If you have any questions or need further assistance, please post on the Open Source Leg community forum.