Initial commit
This commit is contained in:
96
src/algo1.py
Normal file
96
src/algo1.py
Normal file
@@ -0,0 +1,96 @@
|
||||
# Copyright (C) 2026 Hector van der Aa <hector@h3cx.dev>
|
||||
# Copyright (C) 2026 Pierre Barbier <pierrebarbier741@gmail.com>
|
||||
# Copyright (C) 2026 Association Exergie <association.exergie@gmail.com>
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
import time
|
||||
import pandas as pd
|
||||
from pathlib import Path
|
||||
import argparse
|
||||
from tqdm import tqdm
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("file", type=Path, help="Source data")
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
run_data_path: Path = args.file
|
||||
|
||||
df = pd.read_csv(run_data_path).set_index("time_us", drop=False)
|
||||
|
||||
sync_ok = False
|
||||
cycle_ctr = 0
|
||||
cam_ctr = 0
|
||||
exhaust_timestamp = 0
|
||||
intake_timestamp = 0
|
||||
spark_release_time_1 = 0
|
||||
spark_release_time_2 = 0
|
||||
spark_release_time_3 = 0
|
||||
spark_adv = 25
|
||||
|
||||
rows = []
|
||||
|
||||
for _, row in tqdm(df.iterrows(), total=len(df)):
|
||||
time_us: int = row["time_us"]
|
||||
crank: int = row["crank"]
|
||||
cam: int = row["cam"]
|
||||
|
||||
if cam == 1:
|
||||
if cycle_ctr < 4:
|
||||
sync_ok = False
|
||||
if not sync_ok and cycle_ctr == 4:
|
||||
cam_ctr += 1
|
||||
if cam_ctr >= 2:
|
||||
sync_ok = True
|
||||
cycle_ctr = 0
|
||||
|
||||
if crank == 1:
|
||||
cycle_ctr += 1
|
||||
if cycle_ctr >= 5:
|
||||
sync_ok = False
|
||||
|
||||
if cycle_ctr == 2:
|
||||
exhaust_timestamp = time_us
|
||||
|
||||
if cycle_ctr == 3:
|
||||
intake_timestamp = time_us
|
||||
|
||||
if cycle_ctr == 4 and sync_ok:
|
||||
d1a = time_us - intake_timestamp
|
||||
d1b = intake_timestamp - exhaust_timestamp
|
||||
spark_release_time_1 = int(
|
||||
time_us + (2 * d1a - d1b) * (45 - spark_adv) / 180
|
||||
)
|
||||
|
||||
t_s = (
|
||||
d1a * (135 + spark_adv) / 180 + (2 * d1a - d1b) * (45 - spark_adv) / 180
|
||||
)
|
||||
|
||||
t_a = d1a / 2 + t_s / 2
|
||||
|
||||
spark_release_time_2 = int(t_a * (45 - spark_adv) / 180 + time_us)
|
||||
|
||||
f_a = (315 + spark_adv) / (d1a) + (45 - spark_adv) / (2 * d1a - d1b)
|
||||
|
||||
t_a = 1 / f_a
|
||||
|
||||
spark_release_time_3 = int(2 * (t_a * (45 - spark_adv)) + time_us)
|
||||
|
||||
spark_1 = 1 if spark_release_time_1 == time_us and sync_ok else 0
|
||||
spark_2 = 1 if spark_release_time_2 == time_us and sync_ok else 0
|
||||
spark_3 = 1 if spark_release_time_3 == time_us and sync_ok else 0
|
||||
rows.append(
|
||||
{
|
||||
"time_us": time_us,
|
||||
"crank": crank,
|
||||
"cam": cam,
|
||||
"spark_1": spark_1,
|
||||
"spark_2": spark_2,
|
||||
"spark_3": spark_3,
|
||||
}
|
||||
)
|
||||
|
||||
out_df = pd.DataFrame(rows)
|
||||
base_name, _ = run_data_path.stem.rsplit("_", 1)
|
||||
output = run_data_path.parent / f"{base_name}_output.csv"
|
||||
out_df.to_csv(output)
|
||||
74
src/plot.py
Normal file
74
src/plot.py
Normal file
@@ -0,0 +1,74 @@
|
||||
# Copyright (C) 2026 Hector van der Aa <hector@h3cx.dev>
|
||||
# Copyright (C) 2026 Association Exergie <association.exergie@gmail.com>
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
import pandas as pd
|
||||
from pathlib import Path
|
||||
import argparse
|
||||
from tqdm import tqdm
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("directory", type=Path, help="Source data directory")
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
directory: Path = args.directory
|
||||
|
||||
if not directory.is_dir():
|
||||
parser.error(f"{directory} is not a valid directory")
|
||||
|
||||
print(f"Processing data in: {directory}")
|
||||
|
||||
files: list[Path] = []
|
||||
|
||||
for path in directory.glob("*.csv"):
|
||||
stem = path.stem
|
||||
|
||||
try:
|
||||
base_name, channel = stem.rsplit("_", 1)
|
||||
except ValueError:
|
||||
print(f"Skipping badly named file: {path}")
|
||||
continue
|
||||
|
||||
if channel != "output":
|
||||
print(f"Skipping unknown file: {path}")
|
||||
continue
|
||||
|
||||
files.append(path)
|
||||
|
||||
|
||||
for file in files:
|
||||
output_df = pd.read_csv(file).set_index("time_us", drop=False)
|
||||
fig, ax = plt.subplots(figsize=(12, 6))
|
||||
|
||||
# RPM
|
||||
ax.plot(output_df["time_us"] / 1_000_000, output_df["crank"], label="crank")
|
||||
ax.plot(
|
||||
output_df["time_us"] / 1_000_000, output_df["cam"], label="cam", color="red"
|
||||
)
|
||||
ax.plot(
|
||||
output_df["time_us"] / 1_000_000,
|
||||
output_df["spark_1"],
|
||||
label="spark_1",
|
||||
color="green",
|
||||
)
|
||||
ax.plot(
|
||||
output_df["time_us"] / 1_000_000,
|
||||
output_df["spark_2"],
|
||||
label="spark_2",
|
||||
color="yellow",
|
||||
)
|
||||
ax.plot(
|
||||
output_df["time_us"] / 1_000_000,
|
||||
output_df["spark_3"],
|
||||
label="spark_3",
|
||||
color="orange",
|
||||
)
|
||||
ax.set_xlabel("Time (s)")
|
||||
ax.set_ylim(0, 1)
|
||||
ax.grid(True)
|
||||
|
||||
fig.tight_layout()
|
||||
plt.show()
|
||||
plt.close(fig)
|
||||
Reference in New Issue
Block a user