[PATCH v2] Add a simple table-based cns calculations

Anton Lundin glance at acc.umu.se
Tue Apr 9 10:25:21 PDT 2013


For dives where divecomputers haven't provided us with a cns, we
calculate our cns accumulated during that dive based on a simple table.

We also check if we did a dive in the prior 12 ours and grab the cns
from it and calculate how much of that still affects us.

Signed-off-by: Anton Lundin <glance at acc.umu.se>
---
 divelist.c |   64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 63 insertions(+), 1 deletion(-)

diff --git a/divelist.c b/divelist.c
index 61b9116..e97d2d5 100644
--- a/divelist.c
+++ b/divelist.c
@@ -273,7 +273,7 @@ static int active_o2(struct dive *dive, struct divecomputer *dc, duration_t time
 	return o2permille;
 }
 
-/* calculate OTU for a dive - this only takes the first diveomputer into account */
+/* calculate OTU for a dive - this only takes the first divecomputer into account */
 static int calculate_otu(struct dive *dive)
 {
 	int i;
@@ -297,6 +297,66 @@ static int calculate_otu(struct dive *dive)
 	}
 	return otu + 0.5;
 }
+/* calculate CNS for a dive - this only takes the first divecomputer into account */
+int const cns_table[][3] = {
+/* po2, Maximum Single Exposure, Maximum 24 hour Exposure */
+	{1600,  45 * 60, 150 * 60},
+	{1500, 120 * 60, 180 * 60},
+	{1400, 150 * 60, 180 * 60},
+	{1300, 180 * 60, 210 * 60},
+	{1200, 210 * 60, 240 * 60},
+	{1100, 240 * 60, 270 * 60},
+	{1000, 300 * 60, 300 * 60},
+	{ 900, 360 * 60, 360 * 60},
+	{ 800, 450 * 60, 450 * 60},
+	{ 700, 570 * 60, 570 * 60},
+	{ 600, 720 * 60, 720 * 60}
+};
+static int calculate_cns(struct dive *dive)
+{
+	int i, j;
+	double cns = 0.0;
+	struct divecomputer *dc = &dive->dc;
+
+	/* shortcut */
+	if (dive->cns)
+		return dive->cns;
+	/* or if we have a divecomputer that already tracked cns for us */
+	if ((dc->sample + dc->samples - 1)->cns)
+		return (dc->sample + dc->samples - 1)->cns;
+	/*
+	 * Do we start with a cns loading from a privious dive?
+	 * Check if we did a dive 12 hours prior, and what cns we had from that.
+	 * Then apply ha 90min halftime to see whats left.
+	 */
+	if (dive->next && dive->when < (dive->next->when + 3600 * 12)) {
+		cns = calculate_cns(dive->next);
+		cns = cns * 1/pow(2, (dive->when - dive->next->when) / (90.0 * 60.0));
+	}
+	/* Caclulate the cns for each sample in this dive and sum them */
+	for (i = 1; i < dc->samples; i++) {
+		int t;
+		int po2;
+		struct sample *sample = dc->sample + i;
+		struct sample *psample = sample - 1;
+		t = sample->time.seconds - psample->time.seconds;
+		if (sample->po2) {
+			po2 = sample->po2;
+		} else {
+			int o2 = active_o2(dive, dc, sample->time);
+			po2 = o2 / 1000.0 * depth_to_mbar(sample->depth.mm, dive);
+		}
+		/* Find what table-row we should calculate % for */
+		for (j = 1; j < sizeof(cns_table)/(sizeof(int) * 3); j++)
+			if (po2 > cns_table[j][0])
+				break;
+		j--;
+		cns += ((double)t)/((double)cns_table[j][1]) * 100;
+	}
+	/* save calculated cns in dive struct */
+	dive->cns = cns;
+	return dive->cns;
+}
 /*
  * Return air usage (in liters).
  */
@@ -456,6 +516,8 @@ void update_cylinder_related_info(struct dive *dive)
 	if (dive != NULL) {
 		dive->sac = calculate_sac(dive);
 		dive->otu = calculate_otu(dive);
+		if (dive->maxcns == 0)
+			dive->maxcns = calculate_cns(dive);
 	}
 }
 
-- 
1.7.10.4



More information about the subsurface mailing list