From a7d18a9fa649e1f6e9b7d553e286abce8a430b9a Mon Sep 17 00:00:00 2001 From: "Robert C. Helling" Date: Mon, 6 Jul 2015 00:07:39 +0200 Subject: [PATCH] Round MOD of gas rather than truncate For the proper calculation, we need to take salinity and surface pressure into account (rather than depth = bar * 10 - 10) Signed-off-by: Robert C. Helling Signed-off-by: Dirk Hohndel --- dive.h | 26 +++++++++++++++++++------- equipment.c | 2 +- planner.c | 2 +- profile.c | 2 +- qt-models/cylindermodel.cpp | 2 +- qt-models/diveplannermodel.cpp | 2 +- 6 files changed, 24 insertions(+), 12 deletions(-) diff --git a/dive.h b/dive.h index 9019511d1..fcc01a8e4 100644 --- a/dive.h +++ b/dive.h @@ -167,13 +167,6 @@ static inline int interpolate(int a, int b, int part, int whole) return rint(x / whole); } -/* MOD rounded to multiples of roundto mm */ -static inline depth_t gas_mod(struct gasmix *mix, pressure_t po2_limit, int roundto) { - depth_t depth; - depth.mm = ((po2_limit.mbar * 1000 / get_o2(mix) * 10 - 10000) / roundto) * roundto; - return depth; -} - void get_gas_string(const struct gasmix *gasmix, char *text, int len); const char *gasname(const struct gasmix *gasmix); @@ -445,6 +438,25 @@ static inline int rel_mbar_to_depth(int mbar, struct dive *dive) return cm * 10; } +static inline int mbar_to_depth(int mbar, struct dive *dive) +{ + pressure_t surface_pressure; + if (dive->surface_pressure.mbar) + surface_pressure = dive->surface_pressure; + else + surface_pressure.mbar = SURFACE_PRESSURE; + return rel_mbar_to_depth(mbar - surface_pressure.mbar, dive); +} + +/* MOD rounded to multiples of roundto mm */ +static inline depth_t gas_mod(struct gasmix *mix, pressure_t po2_limit, struct dive *dive, int roundto) { + depth_t rounded_depth; + + double depth = (double) mbar_to_depth(po2_limit.mbar * 1000 / get_o2(mix), dive); + rounded_depth.mm = rint(depth / roundto) * roundto; + return rounded_depth; +} + #define SURFACE_THRESHOLD 750 /* somewhat arbitrary: only below 75cm is it really diving */ /* this is a global spot for a temporary dive structure that we use to diff --git a/equipment.c b/equipment.c index aebac5120..47c439735 100644 --- a/equipment.c +++ b/equipment.c @@ -226,7 +226,7 @@ void reset_cylinders(struct dive *dive, bool track_gas) if (cylinder_none(cyl)) continue; if (cyl->depth.mm == 0) /* if the gas doesn't give a mod, calculate based on prefs */ - cyl->depth = gas_mod(&cyl->gasmix, decopo2, M_OR_FT(3,10)); + cyl->depth = gas_mod(&cyl->gasmix, decopo2, dive, M_OR_FT(3,10)); if (track_gas) cyl->start.mbar = cyl->end.mbar = cyl->type.workingpressure.mbar; cyl->gas_used.mliter = 0; diff --git a/planner.c b/planner.c index fcca0d78e..b04c835ef 100644 --- a/planner.c +++ b/planner.c @@ -176,7 +176,7 @@ void fill_default_cylinder(cylinder_t *cyl) cyl->type.size.mliter = cuft_to_l(ti->cuft) * 1000 / bar_to_atm(psi_to_bar(ti->psi)); } // MOD of air - cyl->depth = gas_mod(&cyl->gasmix, pO2, 1); + cyl->depth = gas_mod(&cyl->gasmix, pO2, &displayed_dive, 1); } /* make sure that the gas we are switching to is represented in our diff --git a/profile.c b/profile.c index 8deff29b5..74382b074 100644 --- a/profile.c +++ b/profile.c @@ -963,7 +963,7 @@ static void calculate_gas_information_new(struct dive *dive, struct plot_info *p * END takes O₂ + N₂ (air) into account ("Narcotic" for trimix dives) * EAD just uses N₂ ("Air" for nitrox dives) */ pressure_t modpO2 = { .mbar = (int)(prefs.modpO2 * 1000) }; - entry->mod = (double)gas_mod(&dive->cylinder[cylinderindex].gasmix, modpO2, 1).mm; + entry->mod = (double)gas_mod(&dive->cylinder[cylinderindex].gasmix, modpO2, dive, 1).mm; entry->end = (entry->depth + 10000) * (1000 - fhe) / 1000.0 - 10000; entry->ead = (entry->depth + 10000) * fn2 / (double)N2_IN_AIR - 10000; entry->eadd = (entry->depth + 10000) * diff --git a/qt-models/cylindermodel.cpp b/qt-models/cylindermodel.cpp index a3ab34513..8786f7889 100644 --- a/qt-models/cylindermodel.cpp +++ b/qt-models/cylindermodel.cpp @@ -218,7 +218,7 @@ bool CylindersModel::setData(const QModelIndex &index, const QVariant &value, in prefs.o2consumption / prefs.decosac / prefs.pscr_ratio; else modpO2.mbar = prefs.decopo2; - cyl->depth = gas_mod(&cyl->gasmix, modpO2, M_OR_FT(3, 10)); + cyl->depth = gas_mod(&cyl->gasmix, modpO2, &displayed_dive, M_OR_FT(3, 10)); changed = true; } break; diff --git a/qt-models/diveplannermodel.cpp b/qt-models/diveplannermodel.cpp index 439e7a4de..e4cff38ec 100644 --- a/qt-models/diveplannermodel.cpp +++ b/qt-models/diveplannermodel.cpp @@ -536,7 +536,7 @@ bool DivePlannerPointsModel::addGas(struct gasmix mix) prefs.o2consumption / prefs.decosac / prefs.pscr_ratio; else modpO2.mbar = prefs.decopo2; - cyl->depth = gas_mod(&mix, modpO2, M_OR_FT(3,10)); + cyl->depth = gas_mod(&mix, modpO2, &displayed_dive, M_OR_FT(3,10));