Skip to content

Commit 5db9a02

Browse files
authored
Example to print IMU data (#727)
* Print IMU data * Modified to print IMU data * Changed the sleep * Removed delay * Changed style
1 parent 1b7b49d commit 5db9a02

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

examples/highres_imu.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import asyncio
2+
from mavsdk import System
3+
4+
5+
async def get_imu_data():
6+
# Connect to the drone
7+
drone = System()
8+
await drone.connect(system_address="udp://:14540")
9+
10+
# Wait for the drone to connect
11+
print("Waiting for drone to connect...")
12+
async for state in drone.core.connection_state():
13+
if state.is_connected:
14+
print("Drone is connected!")
15+
break
16+
17+
telemetry = drone.telemetry
18+
19+
# Set the rate at which IMU data is updated (in Hz)
20+
await telemetry.set_rate_imu(200.0)
21+
22+
# Fetch and print IMU data
23+
print("Fetching IMU data...")
24+
async for imu in telemetry.imu():
25+
# Print data in HIGHRES_IMU format
26+
print(f"HIGHRES_IMU (105)")
27+
print(f"Time (us): {imu.timestamp_us}")
28+
print(f"X Acceleration (m/s^2): {imu.acceleration_frd.forward_m_s2}")
29+
print(f"Y Acceleration (m/s^2): {imu.acceleration_frd.right_m_s2}")
30+
print(f"Z Acceleration (m/s^2): {imu.acceleration_frd.down_m_s2}")
31+
print(f"X Gyro (rad/s): {imu.angular_velocity_frd.forward_rad_s}")
32+
print(f"Y Gyro (rad/s): {imu.angular_velocity_frd.right_rad_s}")
33+
print(f"Z Gyro (rad/s): {imu.angular_velocity_frd.down_rad_s}")
34+
print(f"X Mag (gauss): {imu.magnetic_field_frd.forward_gauss}")
35+
print(f"Y Mag (gauss): {imu.magnetic_field_frd.right_gauss}")
36+
print(f"Z Mag (gauss): {imu.magnetic_field_frd.down_gauss}")
37+
print(f"Temperature (°C): {imu.temperature_degc}")
38+
print("-----------------------------------------")
39+
40+
if __name__ == "__main__":
41+
loop = asyncio.get_event_loop()
42+
loop.run_until_complete(get_imu_data())

0 commit comments

Comments
 (0)