Plotting
This example shows how one can pull a log file from the drone and use pandas and matplotlib to plot it.
We'll start by picking a log file from the drone, connecting as an observer since we do not need control:
from blueye.sdk import Drone
myDrone = Drone(connect_as_observer=True)
log = myDrone.logs[0]
Next we'll parse the log to a stream and create a pandas dataframe from the records. We'll also specify the column names to make it easier to work with the dataframe later.
import pandas as pd
import blueye.protocol as bp
columns = ["rt", "delta", "meta", "message"]
log_stream = log.parse_to_stream()
divelog = pd.DataFrame.from_records(log_stream, columns=columns)
The four columns are the record tuple described in Streaming the records: the wall clock time, the time since the first record, the message type and the message.
We'll now filter out all entries that are not depth telemetry messages and messages that were logged before the start of the dive.
depth_log = divelog[divelog.meta == bp.DepthTel]
depth_log = depth_log[depth_log.rt > log.start_time]
We can now extract the depth value from the remaining entries
depth_log["depth"] = depth_log["message"].apply(lambda x: x.depth.value)
We'll prepare our axes for plotting
x = depth_log["rt"]
y = depth_log["depth"]
Next we will plot depth vs time with matplotlib:
import matplotlib.pyplot as plt
# Instantiate our figure and axes to plot on
figure, axes = plt.subplots()
# Plot the depth values against time
axes.plot(x, y, label="depth")
# Set title, labels, and legend
plt.title("Depth chart")
plt.xlabel("Time")
plt.ylabel("Depth [m]")
plt.legend()
# Save the figure
figure.savefig("depth_plot.svg")
This should yield us a plot that looks something like this:
See the matplotlib documentation for more ways to plot your data.
Blunux older than 3.0 (CSV logs)
Drones on Blunux older than 3.0 store CSV logs instead. Download one with the legacy index and read it into pandas:
from blueye.sdk import Drone
myDrone = Drone(connect_as_observer=True)
myDrone.legacy_logs[0].download(output_name="log0.csv")
import pandas as pd
divelog = pd.read_csv("log0.csv")
Convert the unix timestamp in rt_clock into a more readable format, and prepare the
axes. Depth is logged in millimeters:
divelog["rt_clock"] = pd.to_datetime(divelog["rt_clock"], unit="s")
x = divelog["rt_clock"]
y = divelog["depth"] / 1000 # Dividing by 1000 to get depth in meters
The matplotlib part above is the same from here.