+timeEpochNS() {
+ # Desc: Get epoch nanoseconds
+ # Usage: timeEpochNS
+ # Version 0.2.2
+ # Input: arg1: 'date'-parsable timestamp string (optional)
+ # Output: Nanoseconds since 1970-01-01
+ # Depends: date 8, yell()
+ # Ref/Attrib: Force base 10 Bash arith with '10#'. https://stackoverflow.com/a/24777667
+ local TIME_CURRENT TIME_INPUT TIME_EPOCH_FLOAT TIME_EPOCH_NSFRAC
+ local TIME_EPOCH_NS
+
+ argTime="$1";
+
+ # Get Current Time
+ TIME_CURRENT="$(date --iso-8601=ns)"; # Produce `date`-parsable current timestamp with resolution of 1 nanosecond.
+
+ # Decide to parse current or supplied time
+ ## Check if time argument empty
+ if [[ -z "$argTime" ]]; then
+ ## T: Time argument empty, use current time
+ TIME_INPUT="$TIME_CURRENT";
+ else
+ ## F: Time argument exists, validate time
+ if date --date="$argTime" 1>/dev/null 2>&1; then
+ ### T: Time argument is valid; use it
+ TIME_INPUT="$argTime";
+ else
+ ### F: Time argument not valid; exit
+ yell "ERROR:Invalid time argument supplied. Exiting."; exit 1;
+ fi;
+ fi;
+ # Construct and deliver nanoseconds since 1970-01-01
+ TIME_EPOCH_FLOAT="$(date --date="$TIME_INPUT" +%s.%N)"; # Save ssss.NNNNNNNNN
+ TIME_EPOCH_INT="$(echo "$TIME_EPOCH_FLOAT" | cut -d. -f1)"; # Get ssss
+ TIME_EPOCH_NSFRAC="$(echo "$TIME_EPOCH_FLOAT" | cut -d. -f2)"; # Get NNNNNNNNN
+ TIME_EPOCH_NS="$(( (10#"$TIME_EPOCH_INT" * 10**9) + (10#"$TIME_EPOCH_NSFRAC") ))";
+ echo "$TIME_EPOCH_NS";
+} # Nanoseconds since 1970-01-01
+magicBufferSleepPID() {
+ # Desc: Compensates for lag so buffer rounds start every BUFFER_TTL seconds
+ # Input: vars: BUFFER_TTL, errResetx10e3, K_P, T_I, T_D
+ # # Input: array: errorHistory
+ # Output: vars: BUFFER_TTL_ADJ_FLOAT
+ # Re/Attrib: https://en.wikipedia.org/wiki/PID_controller#Standard_versus_parallel_(ideal)_form
+ local BUFFER_TTL_NS
+ local timeBufferStartNS timeBufferStartNSExp errNS errNSx10e3
+ local errResetx10e3 errRatex10e3 ADJ BUFFER_TTL_ADJ_NS BUFFER_TTL_ADJ_INT
+ local BUFFER_TTL_ADJ_FLOATFRAC
+ # local errorHistorySize
+
+ # ## Define errorHistorySize
+ # errorHistorySize=100;
+ ## Define BUFFER_TTL in nanoseconds
+ BUFFER_TTL_NS=$((BUFFER_TTL * 10**9));
+
+ # Calculate Error, errNS, in nanoseconds
+ ## Get current time
+ timeBufferStartNS="$(timeEpochNS)";
+ ## Calculate expected time (from start time, current buffer round number, nominal BUFFER_TTL)
+ timeBufferStartNSExp="$(( (timeBufferFirstNS) + (BUFFER_TTL_NS * bufferRound) ))";
+ ## Calculate error (diff between timeBufferStartNSExp and timeBufferStartNS; usually negative)
+ errNS="$(( timeBufferStartNSExp - timeBufferStartNS ))";
+ errNSx10e3="$((errNS*10**3))";
+ # ## 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;
+ errResetx10e3="$(( ( errResetx10e3 + ( errNS * BUFFER_TTL_NS ) )*10**3 ))";
+
+ # Calculate errRate in nanoseconds per 1000 nanoseconds
+ errRatex10e3="$(( ( errNSx10e3 ) / BUFFER_TTL_NS ))";
+
+ # Calculate PID control signal
+ ## ADJ = K_P * (errNS + errReset/T_I + errRate*T_D)
+ ADJ="$((K_P*(errNSx10e3 + (errResetx10e3/T_I) + (errRatex10e3*T_D) )/10**3))";
+
+ # 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))";
+ ## Calculate integer seconds
+ BUFFER_TTL_ADJ_INT="$((BUFFER_TTL_ADJ_NS/(10**9)))";
+ ## Calculate nanosecond remainder
+ BUFFER_TTL_ADJ_FLOATFRAC="$((BUFFER_TTL_NS - (BUFFER_TTL_ADJ_INT*(10**9)) ))";
+ ## Form float BUFFER_TTL_ADJ_FLOAT
+ BUFFER_TTL_ADJ_FLOAT="$BUFFER_TTL_ADJ_INT"."$BUFFER_TTL_ADJ_FLOATFRAC";
+ vbm "STATUS:Calculated adjusted BUFFER_TTL (seconds):$BUFFER_TTL_ADJ_FLOAT";
+} # Calc BUFFER_TTL_ADJ_FLOAT so buffer starts every BUFFER_TTL seconds