From 0585a8c3674ad4efd8e0425b809f5ae9c13dba0f Mon Sep 17 00:00:00 2001 From: steffen Date: Sun, 31 Jan 2021 19:07:24 +0000 Subject: [PATCH] auto mode optimized.. --- server/locomotive.cc | 66 ++++++++++++++++++++++---------------- server/locomotive.h | 4 ++- server/railway.cc | 4 --- webinterface/locomotive.js | 2 +- 4 files changed, 42 insertions(+), 34 deletions(-) diff --git a/server/locomotive.cc b/server/locomotive.cc index c624a6f..415db07 100644 --- a/server/locomotive.cc +++ b/server/locomotive.cc @@ -569,7 +569,7 @@ int Locomotives::Loop() { // // only in automate do anything alone // - if (loco->blockassign[0] == 0 || loco->blockdest[0] == 0) continue; + if (loco->blockassign[0] == 0) continue; if (loco->auto_onroute == LOCO_OR_NOTHING) { timer_start(&loco->auto_timenext); @@ -577,7 +577,7 @@ int Locomotives::Loop() { } else if (loco->auto_onroute == LOCO_OR_SEARCH) { - if (loco->auto_timenext.tv_sec == 0 || timer_get(&loco->auto_timenext) > 1000) { + if (loco->auto_timenext.tv_sec == 0 || timer_get(&loco->auto_timenext) > LOCO_TO_TRYAGAIN) { if (server->railways.FindWay(loco->blockassign, loco->blockdest, loco->name, &way)) { size_t pos; @@ -596,20 +596,17 @@ int Locomotives::Loop() { } else if (loco->auto_onroute == LOCO_OR_PREPARE) { - if (loco->auto_timenext.tv_sec == 0 || timer_get(&loco->auto_timenext) > 1000) { - if (AutoCheckWaySingleStep(loco->auto_way, loco->name, &loco->auto_data) == 1) + if (loco->auto_timenext.tv_sec == 0 || timer_get(&loco->auto_timenext) > LOCO_TO_TURNOUT) { + if (AutoCheckWaySingleStep(loco->auto_way, loco->name, &loco->auto_data) == 1) { + if (loco->flags & LOCO_F_CARGO) SetSpeed(loco->name, loco->vmid); + else SetSpeed(loco->name, loco->vfast); loco->auto_onroute = LOCO_OR_ONTHEWAY; + } else timer_start(&loco->auto_timenext); } } - else if (loco->auto_onroute == LOCO_OR_ONTHEWAY) { - if (loco->flags & LOCO_F_CARGO) SetSpeed(loco->name, loco->vmid); - else SetSpeed(loco->name, loco->vfast); - - loco->auto_onroute = LOCO_OR_ONTHEWAYPREPARE; - } - else if (loco->auto_onroute == LOCO_OR_ONTHEWAYPREPARE) { + else if (loco->auto_onroute == LOCO_OR_ONTHEWAY) { // check enter block block = loco->blocknext+2; @@ -617,20 +614,38 @@ int Locomotives::Loop() { sensor = server->blocks.GetSensorMinus(block); else sensor = server->blocks.GetSensorPlus(block); - if (server->sensors.GetActive(sensor) == 1) { - SetSpeed(loco->name, loco->vslow); - loco->auto_onroute = LOCO_OR_ENTERBLOCK; - if (loco->blockassign != 0) { + if (server->sensors.GetActive(sensor) == 1) { // entering block? + debug (0, "* Locomotive '%s' Enter Block '%s'", loco->name, loco->blocknext); + // unlock old assigned block + if (loco->blockassign[0] != 0) { server->blocks.SetLockedby((string)(loco->blockassign+2), loco->name, 0); } + + // assignment <-- next + // check if we need to clear dest + strncpy (loco->blockprev, loco->blockassign, REFERENCENAME_LEN); + strncpy (loco->blockassign, loco->blocknext, REFERENCENAME_LEN); + loco->blocknext[0] = 0; + if (strncmp(loco->blockassign, loco->blockdest, REFERENCENAME_LEN) == 0) { + loco->blockdest[0] = 0; + } + + // slow down + SetSpeed(loco->name, loco->vslow); + + jp.Clear(); + jp.AddObject("locomotive",_GetJSON(lnum)); + if(network) network->ChangeListPushToAll(jp.ToString()); + + loco->auto_onroute = LOCO_OR_ENTERBLOCK; } } + else if (loco->auto_onroute == LOCO_OR_ENTERBLOCK) { // check enter block - block = loco->blocknext+2; - - if (loco->blocknext[0] == '-') + block = loco->blockassign+2; + if (loco->blockassign[0] == '-') sensor = server->blocks.GetSensorPlus(block); else sensor = server->blocks.GetSensorMinus(block); @@ -640,21 +655,16 @@ int Locomotives::Loop() { SetSpeed(loco->name, 0); loco->auto_onroute = LOCO_OR_STOPWAIT; server->railways.UnLockWay(loco->auto_way, loco->name); - strncpy (loco->blockassign, loco->blocknext, REFERENCENAME_LEN); - if (strncmp (loco->blockassign, loco->blockdest, REFERENCENAME_LEN) == 0) { - loco->blockdest[0] = 0; - jp.Clear(); - jp.AddObject("locomotive",_GetJSON(lnum)); - if(network) network->ChangeListPushToAll(jp.ToString()); - - } - loco->blocknext[0] = 0; + jp.Clear(); + jp.AddObject("locomotive",_GetJSON(lnum)); + if(network) network->ChangeListPushToAll(jp.ToString()); timer_start(&loco->auto_timenext); } } else if (loco->auto_onroute == LOCO_OR_STOPWAIT) { if (loco->auto_timenext.tv_sec == 0 || timer_get(&loco->auto_timenext) > 5000) { - loco->auto_onroute = LOCO_OR_SEARCH; + if (loco->blockdest[0] != 0) loco->auto_onroute = LOCO_OR_SEARCH; + else loco->auto_onroute = LOCO_OR_NOTHING; } } } diff --git a/server/locomotive.h b/server/locomotive.h index 61f32f1..ae6f9eb 100644 --- a/server/locomotive.h +++ b/server/locomotive.h @@ -13,6 +13,9 @@ #define LOCO_F_RANDOM 0x0200 #define LOCO_F_AUTOSTOP 0x0400 +#define LOCO_TO_TURNOUT (50+TURNOUT_DEFAULT_ACTIVETIMEOUT) +#define LOCO_TO_TRYAGAIN (1000) + #define WAYDATA_LEN (16*REFERENCENAME_LEN) enum { @@ -30,7 +33,6 @@ enum { LOCO_OR_ONTHEWAY, // locomotive is on the way // maybe prepare next block? // ASSIGN -> PREV, NEXT -> ASSIGN, NEXT <- (empty) // propabely searching next block (if DEST is set)? - LOCO_OR_ONTHEWAYPREPARE, // prepare way as long as on the way LOCO_OR_ENTERBLOCK, // got new block ready? // if NEXT is empty and way not AutoPrepareWay not finished... slow down LOCO_OR_STOPWAIT // stopping diff --git a/server/railway.cc b/server/railway.cc index ce90296..cacbce7 100644 --- a/server/railway.cc +++ b/server/railway.cc @@ -404,8 +404,6 @@ int Railways::_FindReference(int *x, int *y, string name) { *y = 0; } - printf ("%s:%d find reference (%d,%d,%s)\n", __FILE__, __LINE__, *x, *y, name.c_str()); - for (; found == 0 && *y < height; (*y)++) { if (*x >= width) *x = 0; else (*x)++; @@ -419,8 +417,6 @@ int Railways::_FindReference(int *x, int *y, string name) { if (found) break; } - printf ("%s:%d found (%d,%d)\n", __FILE__, __LINE__, *x, *y); - if (*x < width && *y < height) return 1; else return 0; }; diff --git a/webinterface/locomotive.js b/webinterface/locomotive.js index 4e8a4c7..6b1b4d5 100644 --- a/webinterface/locomotive.js +++ b/webinterface/locomotive.js @@ -574,7 +574,7 @@ function locolist_show() { // // clear list - + ul.innerHTML = ""; // // append all locomotives