Skip to content

Commit

Permalink
use whole mission estimate for new BMS
Browse files Browse the repository at this point in the history
  • Loading branch information
callumrollo committed Apr 30, 2024
1 parent 5343ae3 commit 42021b0
Showing 1 changed file with 45 additions and 41 deletions.
86 changes: 45 additions & 41 deletions pilot_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,66 +32,70 @@ def battery_plots(combined_nav_file, out_dir):
filename = f"{out_dir}/battery.png"
_log.info(f'writing figure to {filename}')
fig.savefig(filename, format='png', transparent=True)

# Prediction plot
df = df.dropna()
df_3day = df[df.index > df.index.max() - datetime.timedelta(days=3)]
regr = linear_model.LinearRegression()
regr.fit(df_3day.index.values.reshape(-1, 1), df_3day['Voltage'].values.reshape(-1, 1))
# Create time array of one point every hour for 60 days starting three days ago
datetime_pred = pd.date_range(df_3day.index[0], df_3day.index[0] + datetime.timedelta(days=60), 60*24)
y_forward = regr.predict(datetime_pred.values.astype(float).reshape(-1, 1))
v_per_ns = regr.coef_[0][0]
v_per_day_3 = v_per_ns * 24 * 60 * 60 * 1e9

df_5day = df[df.index > df.index.max() - datetime.timedelta(days=5)]
regr = linear_model.LinearRegression()
regr.fit(df_5day.index.values.reshape(-1, 1), df_5day['Voltage'].values.reshape(-1, 1))
# Create time array of one point every hour for 60 days starting three days ago
datetime_pred_5 = pd.date_range(df_5day.index[0], df_5day.index[0] + datetime.timedelta(days=60), 60*24)
y_forward_5 = regr.predict(datetime_pred_5.values.astype(float).reshape(-1, 1))
end = datetime_pred_5[y_forward_5[:, 0] > 23][-1]

recover = datetime_pred[y_forward[:, 0] > 24][-1]
v_per_ns = regr.coef_[0][0]
v_per_day = v_per_ns * 24 * 60 * 60 * 1e9
fig, ax = plt.subplots(figsize=(12, 8))
if df_mean.Voltage.min() > 28:
_log.info("voltage too high to make prediction plot")
ax.text(0.3, 0.5, "Voltage too high \nfor battery prediction", fontsize="24")
ax.axis("off")
else:
ax.scatter(df_5day.index, df_5day.Voltage, label="Voltage last 5 days", s=3)
ax.plot(datetime_pred, y_forward, label="last 3 days prediction")
ax.set(xlim=(df_5day.index[0], end), ylim=(22.9, df_5day.Voltage.max() + 0.1))
loss = np.round(np.abs(v_per_day), 2)
# Prediction plot
df = df.dropna()
df_3day = df[df.index > df.index.max() - datetime.timedelta(days=3)]
regr_3 = linear_model.LinearRegression()
regr_3.fit(df_3day.index.values.astype('datetime64[us]').astype(float).reshape(-1, 1), df_3day['Voltage'].values.reshape(-1, 1))
# Create time array of one point every hour for 60 days starting three days ago
datetime_pred_3 = pd.date_range(df_3day.index[0], df_3day.index[0] + datetime.timedelta(days=60), 60 * 24).values.astype('datetime64[us]')
y_forward_3 = regr_3.predict(datetime_pred_3.astype(float).reshape(-1, 1))
v_per_ns_3 = regr_3.coef_[0][0]
v_per_day_3 = v_per_ns_3 * 24 * 60 * 60 * 1e6

df_sub_28v = df[df.index > df_mean[df_mean.Voltage < 28].index.min()]
df_5day = df[df.index > df.index.max() - datetime.timedelta(days=5)]

regr_5 = linear_model.LinearRegression()
regr_5.fit(df_sub_28v.index.values.astype('datetime64[us]').astype(float).reshape(-1, 1), df_sub_28v['Voltage'].values.reshape(-1, 1))
# Create time array of one point every hour for 60 days starting three days ago
datetime_pred_5 = pd.date_range(df_sub_28v.index[0], df_sub_28v.index[0] + datetime.timedelta(days=60), 60 * 24).values.astype('datetime64[us]')
y_forward_5 = regr_5.predict(datetime_pred_5.astype(float).reshape(-1, 1))

end = datetime_pred_5[y_forward_5[:, 0] > 23][-1]

v_per_ns_5 = regr_5.coef_[0][0]
v_per_day_5 = v_per_ns_5 * 24 * 60 * 60 * 1e6
v_per_days = np.sort(np.array((v_per_day_3, v_per_day_5)))

loss_5 = np.round(np.abs(v_per_day_5), 2)
loss_3 = np.round(np.abs(v_per_day_3), 2)
recover_3 = datetime_pred[np.argmin(np.abs(24 - y_forward))]
ax.axvline(recover_3, color="red")
ax.plot(datetime_pred_5, y_forward_5, label="last 5 days prediction")
losses = np.sort(np.array((loss_3, loss_5)))
recover_3 = datetime_pred_3[np.argmin(np.abs(24 - y_forward_3))]
recover_5 = datetime_pred_5[np.argmin(np.abs(24 - y_forward_5))]
if recover_3 > recover_5:
r_string = f"{str(recover_5)[5:10]} - {str(recover_3)[5:10]}"
else:
r_string = f"{str(recover_3)[5:10]} - {str(recover_5)[5:10]}"
recoveries = np.sort(np.array((recover_3, recover_5)))

ax.scatter(df_sub_28v.index, df_sub_28v.Voltage, label="Voltage last 5 days", s=3)
ax.plot(datetime_pred_3, y_forward_3, label="last 3 days prediction")
ax.set(xlim=(df_5day.index[0], end), ylim=(22.9, df_5day.Voltage.max() + 0.1))
ax.axvline(recover_3, color="red")
ax.plot(datetime_pred_5, y_forward_5, label="Entire mission prediction")
r_string = f"{str(recoveries[0])[:10]} - {str(recoveries[1])[:10]}"
ax.axhline(24, color="red")
ax.axvline(recover, color="red")
ax.axvspan(recover_3, recover, alpha=0.1, color='red')
v_string = f"Voltage: {df['Voltage'].values[-1]} V\n{loss}-{loss_3} V/day\nrecover {r_string}"
ax.axvline(recover_3, color="red")
ax.axvline(recover_5, color="red")
ax.axvspan(recoveries[0], recoveries[1], alpha=0.1, color='red')
v_string = f"Voltage: {df['Voltage'].values[-1]} V\n{losses[0]}-{losses[1]} V/day\nrecover {r_string}"
ax.text(0.05, 0.08, v_string, transform=ax.transAxes)
ax.grid()
ax.set(ylabel="Voltage (v)", title=title)
plt.xticks(rotation=45)
ax.legend(loc=1)
dline = f"{datetime.datetime.now()},{glider},{mission},{v_per_days[1]},{recoveries[0]},{end}\n"
with open("/data/plots/nrt/battery_prediction.csv", "a") as file:
file.write(dline)
plt.tight_layout()
filename = f"{out_dir}/battery_prediction.png"
_log.info(f'writing figure to {filename}')
fig.savefig(filename, format='png', transparent=True)
dline = f"{datetime.datetime.now()},{glider},{mission},{v_per_day},{recover},{end}\n"
with open("/data/plots/nrt/battery_prediction.csv", "a") as file:
file.write(dline)


if __name__ == '__main__':
from pathlib import Path
battery_plots(Path('/data/data_l0_pyglider/nrt/SEA44/M85/rawnc/Martorn-rawgli.parquet'), '.')
battery_plots(Path('/data/data_l0_pyglider/nrt/SEA76/M25/rawnc/Fibbla-rawgli.parquet'), '.')

0 comments on commit 42021b0

Please sign in to comment.