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)
|
||||
Reference in New Issue
Block a user