I am currently trying to read voltage and current values from the power sensor on my PX4 drone. I am able to see these values in real-time and was also able to write a python script to extract those values in real time. However, I would like to use the data that is saved on the log file instead. I have written my own script and used the “Flight Review” analyzer. Both these just show me zeros for voltage and current, and any battery_statuse parameters. Attached is the python script, please help.
import pyulog
import pandas as pd
from datetime import datetime, timedelta, timezone
def extract_voltage_current(ulog_file_path):
# Load the ULog file
ulog = pyulog.ULog(ulog_file_path)
# Extract data from the 'battery_status' topic
battery_status_data = ulog.get_dataset('battery_status')
# Check if data is available
if not battery_status_data:
print("No 'battery_status' data found in the log file.")
return
# Debug: Print the structure of battery_status_data
print("Battery status data keys:", battery_status_data.data.keys())
# Extract data lists
timestamps = battery_status_data.data['timestamp']
voltages = battery_status_data.data['voltage_v']
currents = battery_status_data.data['current_a']
# Convert timestamps to datetime
start_time = datetime.fromtimestamp(ulog.start_timestamp / 1e6, tz=timezone.utc)
times = [start_time + timedelta(microseconds=int(ts)) for ts in timestamps]
# Create a DataFrame to store voltage, current, and time data
voltage_current_df = pd.DataFrame({
'timestamp': timestamps,
'time': times,
'voltage': voltages,
'current': currents
})
# Print the extracted data
print(voltage_current_df)
# Save the extracted data to a CSV file
output_csv = 'voltage_current_data.csv'
voltage_current_df.to_csv(output_csv, index=False)
print(f"Data saved to {output_csv}")
Use the correct path for your local file
ulog_file_path = r"C:\Users\chadi\OneDrive - University of New Mexico\Desktop\log_155_UnknownDate.ulg"
extract_voltage_current(ulog_file_path)