summaryrefslogtreecommitdiffstats
path: root/planner.c
diff options
context:
space:
mode:
authorGravatar Robert Helling <helling@lmu.de>2014-04-17 10:54:55 +0200
committerGravatar Dirk Hohndel <dirk@hohndel.org>2014-04-17 07:29:44 -0700
commit9ddef367b5120e630af9334553dc4b320a09590b (patch)
tree0afadb4086ddc733999a671c0b1b25b88245633e /planner.c
parent8380f096199d4da1e89789c9bd67974e550f2bf3 (diff)
downloadsubsurface-9ddef367b5120e630af9334553dc4b320a09590b.tar.gz
Improvement for various bits of the planner
Rewrite of the actual planner logic. Now ascend to the next potential stop depth. There the state is cached and we try to ascend to the next stop depth. If we hit the ceiling while doing that we go back to the cached state and wait there for a minute. Then we try again. Then loop. Converted all depth related variables from unsigned int to int. During planning, in a time step the current depth can temporarily be negative and comparisons of a negative int with an unsigned it have not the result I expected ( (int) -2 < (unsigned int) 3 turns out to be false). And we don’t really need the 32nd bit that unsigned buys us for depths. Deco stops are now shown in the same table as manually entered stops in boldface (I removed the second table to save screen estate). The gas shown in the table is still misleading as it means the gas used on the segment leading up to that event. The update of the profile only works partially upon changes in the list of available gases. Treatment of various gases is basically there but needs some more love. The ascent velocity is now provided by a function that takes the current depth as argument. Currently it always returns 10m/min but that will later be variable (and hopefully user configurable). The profile is not redrawn while deco is computed (avoiding an infinite recursion). The table got a new column for the duration of a segment while the old “duration” column was renamed “Runtime” to reflect what it actually shows. Currently, only the run time but not the duration are editable. All deco gases are used from the depth where their pO2 is 1.4bar. This should become more flexible. Calculation of the pressure drop in cylinders without configured volumes is suppressed. This solves a problem with the planner crashing when saving a dive where not all cylinders had been manually given a volume. [Short rant break: Treating 0/0 as air bites back at so many places. E.g. Cylinder data is initialized with memsetting the whole structures to 0. Then later suddenly this totally unconfigured cylinder is being treated as it would contain air. Maybe at some point this was a feature. But it lead to a naughty bug which took me over an hour to resolve. We should seriously reconsider this choice and better move to 209/0 being air if changing this everywhere is not too much trouble] Signed-off-by: Robert C. Helling <helling@atdotde.de> Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'planner.c')
-rw-r--r--planner.c223
1 files changed, 117 insertions, 106 deletions
diff --git a/planner.c b/planner.c
index 9df5c894c..95ac0251f 100644
--- a/planner.c
+++ b/planner.c
@@ -13,7 +13,7 @@
#include "planner.h"
#include "gettext.h"
-unsigned int decostoplevels[] = { 0, 3000, 6000, 9000, 12000, 15000, 18000, 21000, 24000, 27000,
+int decostoplevels[] = { 0, 3000, 6000, 9000, 12000, 15000, 18000, 21000, 24000, 27000,
30000, 33000, 36000, 39000, 42000, 45000, 48000, 51000, 54000, 57000,
60000, 63000, 66000, 69000, 72000, 75000, 78000, 81000, 84000, 87000,
90000, 100000, 110000, 120000, 130000, 140000, 150000, 160000, 170000,
@@ -99,6 +99,18 @@ void get_gas_string(int o2, int he, char *text, int len)
snprintf(text, len, "(%d/%d)", (o2 + 5) / 10, (he + 5) / 10);
}
+double interpolate_transition(struct dive *dive, int t0, int t1, int d0, int d1, const struct gasmix *gasmix, int ppo2)
+{
+ int j;
+ double tissue_tolerance;
+
+ for (j = t0; j < t1; j++) {
+ int depth = interpolate(d0, d1, j - t0, t1 - t0);
+ tissue_tolerance = add_segment(depth_to_mbar(depth, dive) / 1000.0, gasmix, 1, ppo2, dive);
+ }
+ return tissue_tolerance;
+}
+
/* returns the tissue tolerance at the end of this (partial) dive */
double tissue_at_end(struct dive *dive, char **cached_datap)
{
@@ -133,43 +145,13 @@ double tissue_at_end(struct dive *dive, char **cached_datap)
}
if (i > 0)
lastdepth = psample->depth.mm;
- for (j = t0; j < t1; j++) {
- int depth = interpolate(lastdepth, sample->depth.mm, j - t0, t1 - t0);
- tissue_tolerance = add_segment(depth_to_mbar(depth, dive) / 1000.0,
- &dive->cylinder[gasidx].gasmix, 1, sample->po2, dive);
- }
+ tissue_tolerance = interpolate_transition(dive, t0, t1, lastdepth, sample->depth.mm, &dive->cylinder[gasidx].gasmix, sample->po2);
psample = sample;
t0 = t1;
}
return tissue_tolerance;
}
-/* how many seconds until we can ascend to the next stop? */
-static int time_at_last_depth(struct dive *dive, int o2, int he, unsigned int next_stop, char **cached_data_p)
-{
- int depth, gasidx;
- double surface_pressure, tissue_tolerance;
- int wait = 0;
- struct sample *sample;
-
- if (!dive)
- return 0;
- surface_pressure = dive->dc.surface_pressure.mbar / 1000.0;
- tissue_tolerance = tissue_at_end(dive, cached_data_p);
- sample = &dive->dc.sample[dive->dc.samples - 1];
- depth = sample->depth.mm;
- gasidx = get_gasidx(dive, o2, he);
- if (gasidx == -1) {
- fprintf(stderr, "cannot find gas (%d/%d), using first gas\n", o2, he);
- gasidx = 0;
- }
- while (deco_allowed_depth(tissue_tolerance, surface_pressure, dive, 1) > next_stop) {
- wait++;
- tissue_tolerance = add_segment(depth_to_mbar(depth, dive) / 1000.0,
- &dive->cylinder[gasidx].gasmix, 1, sample->po2, dive);
- }
- return wait;
-}
/* if a default cylinder is set, use that */
void fill_default_cylinder(cylinder_t *cyl)
@@ -196,6 +178,7 @@ void fill_default_cylinder(cylinder_t *cyl)
if (ti->psi)
cyl->type.size.mliter = cuft_to_l(ti->cuft) * 1000 / bar_to_atm(psi_to_bar(ti->psi));
}
+ cyl->depth.mm = 1600 * 1000 / O2_IN_AIR * 10 - 10000; // MOD of air
}
static int add_gas(struct dive *dive, int o2, int he)
@@ -389,18 +372,18 @@ struct divedatapoint *plan_add_segment(struct diveplan *diveplan, int duration,
}
struct gaschanges {
- unsigned int depth;
+ int depth;
int gasidx;
};
-static struct gaschanges *analyze_gaslist(struct diveplan *diveplan, struct dive *dive, int *gaschangenr)
+static struct gaschanges *analyze_gaslist(struct diveplan *diveplan, struct dive *dive, int *gaschangenr, int depth)
{
int nr = 0;
struct gaschanges *gaschanges = NULL;
struct divedatapoint *dp = diveplan->dp;
while (dp) {
- if (dp->time == 0) {
+ if (dp->time == 0 && dp->depth <= depth) {
int i = 0, j = 0;
nr++;
gaschanges = realloc(gaschanges, nr * sizeof(struct gaschanges));
@@ -436,15 +419,15 @@ static struct gaschanges *analyze_gaslist(struct diveplan *diveplan, struct dive
}
/* sort all the stops into one ordered list */
-static unsigned int *sort_stops(unsigned int *dstops, int dnr, struct gaschanges *gstops, int gnr)
+static unsigned int *sort_stops(int *dstops, int dnr, struct gaschanges *gstops, int gnr)
{
int i, gi, di;
int total = dnr + gnr;
- unsigned int *stoplevels = malloc(total * sizeof(unsigned int));
+ int *stoplevels = malloc(total * sizeof(int));
/* no gaschanges */
if (gnr == 0) {
- memcpy(stoplevels, dstops, dnr * sizeof(unsigned int));
+ memcpy(stoplevels, dstops, dnr * sizeof(int));
return stoplevels;
}
i = total - 1;
@@ -586,17 +569,29 @@ static void add_plan_to_notes(struct diveplan *diveplan, struct dive *dive)
}
#endif
+int ascend_velocity(int depth)
+{
+ /* We need to make this configurable */
+ return 10000/60;
+}
+
void plan(struct diveplan *diveplan, char **cached_datap, struct dive **divep, bool add_deco)
{
struct dive *dive;
struct sample *sample;
int wait_time, o2, he, po2;
int transitiontime, gi;
- unsigned int stopidx, depth, ceiling;
+ int current_cylinder;
+ unsigned int stopidx;
+ int depth, ceiling;
double tissue_tolerance;
struct gaschanges *gaschanges = NULL;
int gaschangenr;
- unsigned int *stoplevels = NULL;
+ int *stoplevels = NULL;
+ char *trial_cache = NULL;
+ bool stopping = false;
+ bool clear_to_ascend;
+ int clock, previous_point_time;
set_gf(diveplan->gflow, diveplan->gfhigh, default_prefs.gf_low_at_maxdepth);
if (!diveplan->surface_pressure)
@@ -614,6 +609,10 @@ void plan(struct diveplan *diveplan, char **cached_datap, struct dive **divep, b
he = dive->cylinder[0].gasmix.he.permille;
get_gas_from_events(&dive->dc, sample->time.seconds, &o2, &he);
po2 = dive->dc.sample[dive->dc.samples - 1].po2;
+ if ((current_cylinder = get_gasidx(dive, o2, he)) == -1) {
+ report_error(translate("gettextFromC", "Can't find gas %d/%d"), (o2 + 5) / 10, (he + 5) / 10);
+ current_cylinder = 0;
+ }
depth = dive->dc.sample[dive->dc.samples - 1].depth.mm;
/* if all we wanted was the dive just get us back to the surface */
@@ -629,90 +628,102 @@ void plan(struct diveplan *diveplan, char **cached_datap, struct dive **divep, b
}
tissue_tolerance = tissue_at_end(dive, cached_datap);
- ceiling = deco_allowed_depth(tissue_tolerance, diveplan->surface_pressure / 1000.0, dive, 1);
+
#if DEBUG_PLAN & 4
printf("gas %d/%d\n", o2, he);
printf("depth %5.2lfm ceiling %5.2lfm\n", depth / 1000.0, ceiling / 1000.0);
#endif
- if (depth < ceiling) /* that's not good... */
- depth = ceiling;
- if (depth == 0 && ceiling == 0) /* we are done here */
- goto done;
+
+ gaschanges = analyze_gaslist(diveplan, dive, &gaschangenr, depth);
for (stopidx = 0; stopidx < sizeof(decostoplevels) / sizeof(int); stopidx++)
- if (decostoplevels[stopidx] >= ceiling)
+ if (decostoplevels[stopidx] >= depth)
break;
if (stopidx > 0)
stopidx--;
-
- /* so now we know the first decostop level above us
- * NOTE, this could be the surface or a long list of potential stops
- * we do NOT start only at the ceiling, as the ceiling may come down
- * further during the ascent.
- * Next we need to figure out if there are better gases available
- * and at which depths we are supposed to switch to them */
- gaschanges = analyze_gaslist(diveplan, dive, &gaschangenr);
stoplevels = sort_stops(decostoplevels, stopidx + 1, gaschanges, gaschangenr);
+ stopidx += gaschangenr;
+ clock = previous_point_time = dive->dc.sample[dive->dc.samples - 1].time.seconds;
gi = gaschangenr - 1;
- stopidx += gaschangenr;
- if (depth > stoplevels[stopidx]) {
- /* right now all the transitions are at 30ft/min - this needs to be configurable */
- transitiontime = (depth - stoplevels[stopidx]) / 150;
-#if DEBUG_PLAN & 2
- printf("transitiontime %d:%02d to depth %5.2lfm\n", FRACTION(transitiontime, 60), stoplevels[stopidx] / 1000.0);
-#endif
- plan_add_segment(diveplan, transitiontime, stoplevels[stopidx], o2, he, po2, false);
- /* re-create the dive */
- delete_single_dive(dive_table.nr - 1);
- *divep = dive = create_dive_from_plan(diveplan);
- if (!dive)
- goto error_exit;
- record_dive(dive);
- }
- while (stopidx > 0) { /* this indicates that we aren't surfacing directly */
- /* if we are in a double-step, eg, when 3m/10ft stop is disabled,
- * just skip the first stop at that depth */
- if (stoplevels[stopidx] == stoplevels[stopidx - 1]) {
- stopidx--;
- continue;
- }
+
+ while (1) {
+ /* We will break out when we hit the surface */
+ do {
+ /* Ascend in one second steps to next stop depth */
+ int deltad = ascend_velocity(depth);
+ if (depth - deltad < stoplevels[stopidx])
+ deltad = depth - stoplevels[stopidx];
+
+ tissue_tolerance = add_segment(depth_to_mbar(depth, dive) / 1000.0, &dive->cylinder[current_cylinder].gasmix, 1, po2, dive);
+ ++clock;
+ depth -= deltad;
+ } while (depth > stoplevels[stopidx]);
+
+ if (depth <= 0)
+ break; /* We are at the surface */
+
+
if (gi >= 0 && stoplevels[stopidx] == gaschanges[gi].depth) {
- o2 = dive->cylinder[gaschanges[gi].gasidx].gasmix.o2.permille;
- he = dive->cylinder[gaschanges[gi].gasidx].gasmix.he.permille;
+ plan_add_segment(diveplan, clock - previous_point_time, depth, o2, he, po2, false);
+ previous_point_time = clock;
+ stopping = true;
+
+ current_cylinder = gaschanges[gi].gasidx;
+ o2 = dive->cylinder[current_cylinder].gasmix.o2.permille;
+ he = dive->cylinder[current_cylinder].gasmix.he.permille;
#if DEBUG_PLAN & 16
printf("switch to gas %d (%d/%d) @ %5.2lfm\n", gaschanges[gi].gasidx,
- (o2 + 5) / 10, (he + 5) / 10, gaschanges[gi].depth / 1000.0);
+ (o2 + 5) / 10, (he + 5) / 10, gaschanges[gi].depth / 1000.0);
#endif
gi--;
}
- wait_time = time_at_last_depth(dive, o2, he, stoplevels[stopidx - 1], cached_datap);
- /* typically deco plans are done in one minute increments; we may want to
- * make this configurable at some point */
- wait_time = ((wait_time + 59) / 60) * 60;
-#if DEBUG_PLAN & 2
- tissue_tolerance = tissue_at_end(dive, cached_datap);
- ceiling = deco_allowed_depth(tissue_tolerance, diveplan->surface_pressure / 1000.0, dive, 1);
- printf("waittime %d:%02d at depth %5.2lfm; ceiling %5.2lfm\n", FRACTION(wait_time, 60),
- stoplevels[stopidx] / 1000.0, ceiling / 1000.0);
-#endif
- if (wait_time)
- plan_add_segment(diveplan, wait_time, stoplevels[stopidx], o2, he, po2, false);
- /* right now all the transitions are at 30ft/min - this needs to be configurable */
- transitiontime = (stoplevels[stopidx] - stoplevels[stopidx - 1]) / 150;
-#if DEBUG_PLAN & 2
- printf("transitiontime %d:%02d to depth %5.2lfm\n", FRACTION(transitiontime, 60), stoplevels[stopidx - 1] / 1000.0);
-#endif
- plan_add_segment(diveplan, transitiontime, stoplevels[stopidx - 1], o2, he, po2, false);
- /* re-create the dive */
- delete_single_dive(dive_table.nr - 1);
- *divep = dive = create_dive_from_plan(diveplan);
- if (!dive)
- goto error_exit;
- record_dive(dive);
- stopidx--;
- }
-done:
+ /* trial ascend */
+ --stopidx;
+ int trial_depth = depth;
+ cache_deco_state(tissue_tolerance, &trial_cache);
+ while(1) {
+ /* Try to ascend to next stop, go back and wait if we hit the ceiling on the way */
+ clear_to_ascend = true;
+ while (trial_depth > stoplevels[stopidx]) {
+ int deltad = ascend_velocity(trial_depth);
+ tissue_tolerance = add_segment(depth_to_mbar(trial_depth, dive) / 1000.0, &dive->cylinder[current_cylinder].gasmix, 1, po2, dive);
+ if (deco_allowed_depth(tissue_tolerance, diveplan->surface_pressure / 1000.0, dive, 1) > trial_depth - deltad){
+ /* We should have stopped */
+ clear_to_ascend = false;
+ break;
+ }
+ trial_depth -= deltad;
+ }
+ restore_deco_state(trial_cache);
+ /* The next stop is clear */
+ if(clear_to_ascend)
+ break;
+
+ /* Wait a minute */
+ if (!stopping) {
+ plan_add_segment(diveplan, clock - previous_point_time, depth, o2, he, po2, false);
+ previous_point_time = clock;
+ stopping = true;
+ }
+ tissue_tolerance = add_segment(depth_to_mbar(depth, dive) / 1000.0, &dive->cylinder[current_cylinder].gasmix, 60, po2, dive);
+ cache_deco_state(tissue_tolerance, &trial_cache);
+ clock += 60;
+ trial_depth = depth;
+ }
+ if (stopping) {
+ plan_add_segment(diveplan, clock - previous_point_time, depth, o2, he, po2, false);
+ previous_point_time = clock;
+ stopping = false;
+ }
+
+ }
+ plan_add_segment(diveplan, clock - previous_point_time, 0, o2, he, po2, false);
+ delete_single_dive(dive_table.nr - 1);
+ *divep = dive = create_dive_from_plan(diveplan);
+ if (!dive)
+ goto error_exit;
+ record_dive(dive);
#if DO_WE_WANT_THIS_IN_QT
add_plan_to_notes(diveplan, dive);