+ ### PID Control factors
+ K_P=1; # Gain for compensating buffer round lag
+ T_I="$(((4)*BUFFER_TTL_NS/(1)))"; # Consider this number of past nanoseconds to eliminate error
+ T_D="$(((1)*BUFFER_TTL_NS/(1)))"; # Predict value this number of nanoseconds into the future
+
+ # Calculate Error, errNS, in nanoseconds
+ ## Get current time
+ timeBufferStartNS="$(timeEpochNS)" && vbm "timeBufferStartNS :$timeBufferStartNS";
+ ## Calculate expected time (from start time, current buffer round number, nominal BUFFER_TTL)
+ timeBufferStartNSExp="$(( (timeBufferFirstNS) + (BUFFER_TTL_NS * bufferRound) ))" && vbm "timeBufferStartNSExp:$timeBufferStartNSExp";
+ ## Calculate error (diff between timeBufferStartNSExp and timeBufferStartNS; usually negative)
+ errNS="$(( timeBufferStartNSExp - timeBufferStartNS ))" && vbm "errNS:$errNS";
+# errNSx10e3="$((errNS*10**3))" && vbm "errNSx10e3:$errNSx10e3";
+ # ## Append error to errorHistory
+ # errorHistory+=("errNS");
+ # ### Trim errorHistory array if over errorHistorySize
+ # while [[ "${#errorHistory[@]}" -gt "errorHistorySize" ]]; then do
+ # unset "errorHistory[0]"; # remove oldest entry, creating sparse array
+ # errorHistory=("${errorHistory[@]}"); # reindex sparse array
+ # vbm "STATUS:Trimmed errorHistory array. Entry count:${#errorHistory[@]}";
+ # done;
+
+ # Calculate errReset in nanoseconds^2
+ ## errReset = int(errHistory(t),wrt(delta_BUFFER_TTL))
+ ## Integrate errorHistory with respect to time
+ # for value in "${errorHistory[@]}"; do
+ # errReset=$(( errReset + ( value*BUFFER_TTL_NS ) ));
+ # done;
+ vbm "errReset(orig):$errReset"
+ errReset="$(( (errReset + (errNS*BUFFER_TTL_NS)) ))" && vbm "errReset(post):$errReset";
+# errResetx10e3="$(( ( errResetx10e3 + ( errNSx10e3 * BUFFER_TTL_NS ) )*10**3 ))" && vbm "errResetx10e3:$errResetx10e3";
+
+ # Calculate errRate in nanoseconds per nanosecond
+ errRate="$(( errNS / BUFFER_TTL_NS ))" && vbm "errRate:$errRate";
+# errRatex10e3="$(( ( errNSx10e3 ) / BUFFER_TTL_NS ))" && vbm "errRatex10e3:$errRatex10e3";
+
+ # Debug
+ vbm "errNS :$errNS";
+ vbm "errResetTerm:$((errReset/T_I))";
+ vbm "errRateTerm :$((errRate*T_D))";
+
+ # Calculate PID control signal
+ ## ADJ = K_P * (errNS + errReset/T_I + errRate*T_D)
+ ADJ="$(( K_P*(errNS + errReset/T_I + errRate*T_D) ))" && vbm "ADJ:$ADJ";
+# ADJ="$((K_P*(errNSx10e3 + (errResetx10e3/T_I) + (errRatex10e3*T_D) )/(10**3)))" && vbm "ADJ:$ADJ";
+
+ # Calculate BUFFER_TTL_ADJ_FLOAT from ADJ (ns)
+ ## Calculate BUFFER_TTL_ADJ in nanoseconds (BUFFER_TTL_ADJ_NS = BUFFER_TTL_NS + ADJ)
+ BUFFER_TTL_ADJ_NS="$((BUFFER_TTL_NS + ADJ))" && vbm "BUFFER_TTL_ADJ_NS:$BUFFER_TTL_ADJ_NS";
+ ## Calculate integer seconds
+ BUFFER_TTL_ADJ_INT="$((BUFFER_TTL_ADJ_NS/(10**9)))" && vbm "BUFFER_TTL_ADJ_INT:$BUFFER_TTL_ADJ_INT";
+ ### Catch negative integer seconds, set minimum of BUFFER_TTL/10 seconds
+ if [[ "$BUFFER_TTL_ADJ_INT" -le "$((BUFFER_TTL/10))" ]]; then
+ BUFFER_TTL_ADJ_INT="$((BUFFER_TTL/10))";
+ yell "WARNING:Buffer lag adjustment yielded negative seconds.";
+ fi;
+ ## Calculate nanosecond remainder
+ ### Remove integer
+ BUFFER_TTL_ADJ_FLOATFRAC="$((BUFFER_TTL_ADJ_NS - (BUFFER_TTL_ADJ_INT*(10**9)) ))" && vbm "BUFFER_TTL_ADJ_FLOATFRAC:$BUFFER_TTL_ADJ_FLOATFRAC";
+ ### Calc absolute value of fraction (by removing '-' if present; see https://stackoverflow.com/a/47240327
+ BUFFER_TTL_ADJ_FLOATFRAC="${BUFFER_TTL_ADJ_FLOATFRAC#-}" && vbm "BUFFER_TTL_ADJ_FLOATFRAC:$BUFFER_TTL_ADJ_FLOATFRAC";
+ ## Form float BUFFER_TTL_ADJ_FLOAT
+ BUFFER_TTL_ADJ_FLOAT="$BUFFER_TTL_ADJ_INT"."$BUFFER_TTL_ADJ_FLOATFRAC" && vbm "BUFFER_TTL_ADJ_FLOAT:$BUFFER_TTL_ADJ_FLOAT";
+ vbm "STATUS:Calculated adjusted BUFFER_TTL (seconds):$BUFFER_TTL_ADJ_FLOAT";
+} # Calc BUFFER_TTL_ADJ_FLOAT so buffer starts every BUFFER_TTL seconds
+magicWriteVersion() {
+ # Desc: Appends time-stamped VERSION to PATHOUT_TAR
+ # Usage: magicWriteVersion
+ # Version: 0.1.0
+ # Input: CONTENT_VERSION, FILEOUT_VERSION, PATHOUT_TAR, DIR_TMP
+ # Input: SCRIPT_VERSION, SCRIPT_URL, AGE_VERSION, AGE_URL, SCRIPT_HOSTNAME
+ # Output: appends tar PATHOUT_TAR
+ # Depends: dateTimeShort, appendArgTar
+ local CONTENT_VERSION pubKeyIndex
+
+ # Set VERSION file name
+ FILEOUT_VERSION="$(dateTimeShort)..VERSION";
+
+ # Gather VERSION data in CONTENT_VERSION
+ CONTENT_VERSION="SCRIPT_VERSION=$SCRIPT_VERSION";
+ #CONTENT_VERSION="$CONTENT_VERSION""\\n";
+ CONTENT_VERSION="$CONTENT_VERSION""\\n""SCRIPT_NAME=$SCRIPT_NAME";
+ CONTENT_VERSION="$CONTENT_VERSION""\\n""SCRIPT_URL=$SCRIPT_URL";
+ CONTENT_VERSION="$CONTENT_VERSION""\\n""AGE_VERSION=$AGE_VERSION";
+ CONTENT_VERSION="$CONTENT_VERSION""\\n""AGE_URL=$AGE_URL";
+ CONTENT_VERSION="$CONTENT_VERSION""\\n""DATE=$(date --iso-8601=seconds)";
+ CONTENT_VERSION="$CONTENT_VERSION""\\n""HOSTNAME=$SCRIPT_HOSTNAME";
+ ## Add list of recipient pubkeys
+ for pubkey in "${recPubKeysValid[@]}"; do
+ ((pubKeyIndex++))
+ CONTENT_VERSION="$CONTENT_VERSION""\\n""PUBKEY_$pubKeyIndex=$pubkey";
+ done
+ ## Process newline escapes
+ CONTENT_VERSION="$(echo -e "$CONTENT_VERSION")"
+
+ # Write CONTENT_VERSION as file FILEOUT_VERSION and write-append to PATHOUT_TAR
+ appendArgTar "$CONTENT_VERSION" "$FILEOUT_VERSION" "$PATHOUT_TAR" "$DIR_TMP";
+
+} # bkgpslog: write version data to PATHOUT_TAR via appendArgTar()
+magicGatherWriteBuffer() {
+ # Desc: bkgpslog-specific meta function for writing data to DIR_TMP then appending each file to PATHOUT_TAR
+ # Inputs: vars: PATHOUT_TAR FILEOUT_{NMEA,GPX,KML} CMD_CONV_{NMEA,GPX,KML} CMD_{COMPRESS,ENCRYPT} DIR_TMP,
+ # Inputs: vars: BUFFER_TTL bufferTTL_STR SCRIPT_HOSTNAME CMD_COMPRESS_SUFFIX CMD_ENCRYPT_SUFFIX
+ # Output: file: (PATHOUT_TAR)
+ # Depends: yell(), try(), vbm(), appendArgTar(), tar 1, sleep 8, checkMakeTar()
+ # Depends: magicWriteVersion(), appendFileTar()
+ local FN
+
+ vbm "DEBUG:STATUS:$FN:Started magicGatherWriteBuffer().";
+ # Debug:Get function name
+ FN="${FUNCNAME[0]}";