fix(user/):Fix flac converter script
[BK-2020-03.git] / unitproc / bktemp-updateLoopPI
index d01e6b4b45b73fec7f9591a5accde4ee95cf0314..9054b90298b31df8373c76e89bae57b0dfd6bf97 100644 (file)
@@ -8,25 +8,27 @@ update_pi() {
     # Desc: Calculates control variable (CV) given setpoint (SP) and
     #   process variable (PV). Uses proportional integral (PI)
     #   control.
     # Desc: Calculates control variable (CV) given setpoint (SP) and
     #   process variable (PV). Uses proportional integral (PI)
     #   control.
-    # Usage: update_pi arg1 arg2 arg3 arg4 arg5
-    # Version: 0.0.1
+    # Usage: update_pi arg1 arg2 arg3 arg4 arg5 arg6
+    # Version: 0.1.3
     # Input: arg1: path_loop_name (control loop name path)
     #        arg2: var_pv (process variable)
     #        arg3: var_sp (set point)
     #        arg4: tune_p (proportional tuning factor)
     #        arg5: tune_i (integral tuning factor)
     # Input: arg1: path_loop_name (control loop name path)
     #        arg2: var_pv (process variable)
     #        arg3: var_sp (set point)
     #        arg4: tune_p (proportional tuning factor)
     #        arg5: tune_i (integral tuning factor)
+    #        arg6: var_cv_bias (control variable bias; prevents initial jerk)
     # Output: stdout: var_cv (control variable)
     #         file: path_var_pv
     #         file: path_var_sp
     #         file: path_tune_p
     #         file: path_tune_i
     # Output: stdout: var_cv (control variable)
     #         file: path_var_pv
     #         file: path_var_sp
     #         file: path_tune_p
     #         file: path_tune_i
+    #         file: path_var_cv
     #         file: path_sum (saves updated sum state)
     # Example: update_pi /dev/shm/DC1.AC 1.0 3.0 2.0 3.0 
     # Depends: bc, gnu coreutils 8.30, yell(), try()
     local var_sp var_pv var_cv;
     local tune_p tune_i;
     local term_p term_i;
     #         file: path_sum (saves updated sum state)
     # Example: update_pi /dev/shm/DC1.AC 1.0 3.0 2.0 3.0 
     # Depends: bc, gnu coreutils 8.30, yell(), try()
     local var_sp var_pv var_cv;
     local tune_p tune_i;
     local term_p term_i;
-    local error sum;
+    local error sum sum_cand;
     local path_sum path_var_sp path_var_pv path_tune_p path_tune_i;
 
     path_loop_name="$1";
     local path_sum path_var_sp path_var_pv path_tune_p path_tune_i;
 
     path_loop_name="$1";
@@ -55,6 +57,11 @@ update_pi() {
        tune_i="$5";
     fi;
 
        tune_i="$5";
     fi;
 
+    path_var_cv="$path_loop_name".cv
+
+    path_var_cv_bias="$path_loop_name".cv_bias
+    var_cv_bias="$6";
+    
     path_sum="$path_loop_name".sum
     if [[ -f "$path_sum" ]]; then
        sum="$(cat "$path_sum" | head -n1)";
     path_sum="$path_loop_name".sum
     if [[ -f "$path_sum" ]]; then
        sum="$(cat "$path_sum" | head -n1)";
@@ -65,18 +72,25 @@ update_pi() {
     #yell "DEBUG:path_loop_name:$path_loop_name";
     #yell "DEBUG:var_pv:$var_pv";
     #yell "DEBUG:var_sp:$var_sp";
     #yell "DEBUG:path_loop_name:$path_loop_name";
     #yell "DEBUG:var_pv:$var_pv";
     #yell "DEBUG:var_sp:$var_sp";
+    #yell "DEBUG:var_cv:$var_cv";
+    #yell "DEBUG:var_cv_bias:$var_cv_bias";
     #yell "DEBUG:tune_p:$tune_p";
     #yell "DEBUG:tune_i:$tune_i";
     
     error="$(try echo "$var_sp - $var_pv" | bc -l)";
     #yell "DEBUG:error:$error";
     #yell "DEBUG:tune_p:$tune_p";
     #yell "DEBUG:tune_i:$tune_i";
     
     error="$(try echo "$var_sp - $var_pv" | bc -l)";
     #yell "DEBUG:error:$error";
-    sum="$(try echo "$sum + $error" | bc -l)";
+    sum_cand="$(try echo "$sum + $error" | bc -l)";
     #yell "DEBUG:sum:$sum";
     #yell "DEBUG:sum:$sum";
+    if [[ "$(try echo "$sum_cand > 2 * $sum " | bc -l)" -eq 1 ]]; then
+       sum="$(try echo "$sum + l($error + 1)" | bc -l)"; # dampen integral sum spikes
+    else
+       sum="$sum_cand";
+    fi;
     term_p="$(try echo "$tune_p * $error" | bc -l)";
     #yell "DEBUG:term_p:$term_p";
     term_i="$(try echo "$tune_i * $sum" | bc -l)";
     #yell "DEBUG:term_i:$term_i";
     term_p="$(try echo "$tune_p * $error" | bc -l)";
     #yell "DEBUG:term_p:$term_p";
     term_i="$(try echo "$tune_i * $sum" | bc -l)";
     #yell "DEBUG:term_i:$term_i";
-    var_cv="$(try echo "$term_p + $term_i" | bc -l)";
+    var_cv="$(try echo "$term_p + $term_i + $var_cv_bias" | bc -l)";
     #yell "DEBUG:var_cv:$var_cv";
     
     # Write variables to memory
     #yell "DEBUG:var_cv:$var_cv";
     
     # Write variables to memory
@@ -85,12 +99,14 @@ update_pi() {
     echo "$var_pv" > "$path_var_pv";
     echo "$tune_p" > "$path_tune_p";
     echo "$tune_i" > "$path_tune_i";
     echo "$var_pv" > "$path_var_pv";
     echo "$tune_p" > "$path_tune_p";
     echo "$tune_i" > "$path_tune_i";
+    echo "$var_cv" > "$path_var_cv";
+    echo "$var_cv_bias" > "$path_var_cv_bias";
 
     # Output control variable to stdout
     echo "$var_cv";
 
     #yell "DEBUG:=============END_ROUND===============";
 
     # Output control variable to stdout
     echo "$var_cv";
 
     #yell "DEBUG:=============END_ROUND===============";
-}
+} # update specified PI loop
 #==END function definition==
 
 #==BEGIN Example code==
 #==END function definition==
 
 #==BEGIN Example code==
@@ -99,42 +115,43 @@ var_pv=1.0;
 var_sp=2.0;
 tune_p=1.0;
 tune_i=0.1;
 var_sp=2.0;
 tune_p=1.0;
 tune_i=0.1;
+var_cv_init=1000;
 try rm "$path_loop_name"*
 
 var_pv=4.0;
 try rm "$path_loop_name"*
 
 var_pv=4.0;
-output="$(update_pi "$path_loop_name" "$var_pv" "$var_sp" "$tune_p" "$tune_i" | tail -n1)";
+output="$(update_pi "$path_loop_name" "$var_pv" "$var_sp" "$tune_p" "$tune_i" "$var_cv_init" | tail -n1)";
 yell "DEBUG:sum:$(cat "$path_loop_name".sum | head -n1)";
 yell "DEBUG:output:$output";
 var_pv=4.0;
 yell "DEBUG:sum:$(cat "$path_loop_name".sum | head -n1)";
 yell "DEBUG:output:$output";
 var_pv=4.0;
-output="$(update_pi "$path_loop_name" "$var_pv" "$var_sp" "$tune_p" "$tune_i" | tail -n1)";
+output="$(update_pi "$path_loop_name" "$var_pv" "$var_sp" "$tune_p" "$tune_i" "$var_cv_init" | tail -n1)";
 yell "DEBUG:sum:$(cat "$path_loop_name".sum | head -n1)";
 yell "DEBUG:output:$output";
 var_pv=4.0;
 yell "DEBUG:sum:$(cat "$path_loop_name".sum | head -n1)";
 yell "DEBUG:output:$output";
 var_pv=4.0;
-output="$(update_pi "$path_loop_name" "$var_pv" "$var_sp" "$tune_p" "$tune_i" | tail -n1)";
+output="$(update_pi "$path_loop_name" "$var_pv" "$var_sp" "$tune_p" "$tune_i" "$var_cv_init" | tail -n1)";
 yell "DEBUG:sum:$(cat "$path_loop_name".sum | head -n1)";
 yell "DEBUG:output:$output";
 var_pv=4.0;
 yell "DEBUG:sum:$(cat "$path_loop_name".sum | head -n1)";
 yell "DEBUG:output:$output";
 var_pv=4.0;
-output="$(update_pi "$path_loop_name" "$var_pv" "$var_sp" "$tune_p" "$tune_i" | tail -n1)";
+output="$(update_pi "$path_loop_name" "$var_pv" "$var_sp" "$tune_p" "$tune_i" "$var_cv_init" | tail -n1)";
 yell "DEBUG:sum:$(cat "$path_loop_name".sum | head -n1)";
 yell "DEBUG:output:$output";
 var_pv=4.0;
 yell "DEBUG:sum:$(cat "$path_loop_name".sum | head -n1)";
 yell "DEBUG:output:$output";
 var_pv=4.0;
-output="$(update_pi "$path_loop_name" "$var_pv" "$var_sp" "$tune_p" "$tune_i" | tail -n1)";
+output="$(update_pi "$path_loop_name" "$var_pv" "$var_sp" "$tune_p" "$tune_i" "$var_cv_init" | tail -n1)";
 yell "DEBUG:sum:$(cat "$path_loop_name".sum | head -n1)";
 yell "DEBUG:output:$output";
 var_pv=1.0;
 yell "DEBUG:sum:$(cat "$path_loop_name".sum | head -n1)";
 yell "DEBUG:output:$output";
 var_pv=1.0;
-output="$(update_pi "$path_loop_name" "$var_pv" "$var_sp" "$tune_p" "$tune_i" | tail -n1)";
+output="$(update_pi "$path_loop_name" "$var_pv" "$var_sp" "$tune_p" "$tune_i" "$var_cv_init" | tail -n1)";
 yell "DEBUG:sum:$(cat "$path_loop_name".sum | head -n1)";
 yell "DEBUG:output:$output";
 var_pv=1.0;
 yell "DEBUG:sum:$(cat "$path_loop_name".sum | head -n1)";
 yell "DEBUG:output:$output";
 var_pv=1.0;
-output="$(update_pi "$path_loop_name" "$var_pv" "$var_sp" "$tune_p" "$tune_i" | tail -n1)";
+output="$(update_pi "$path_loop_name" "$var_pv" "$var_sp" "$tune_p" "$tune_i" "$var_cv_init" | tail -n1)";
 yell "DEBUG:sum:$(cat "$path_loop_name".sum | head -n1)";
 yell "DEBUG:output:$output";
 var_pv=1.0;
 yell "DEBUG:sum:$(cat "$path_loop_name".sum | head -n1)";
 yell "DEBUG:output:$output";
 var_pv=1.0;
-output="$(update_pi "$path_loop_name" "$var_pv" "$var_sp" "$tune_p" "$tune_i" | tail -n1)";
+output="$(update_pi "$path_loop_name" "$var_pv" "$var_sp" "$tune_p" "$tune_i" "$var_cv_init" | tail -n1)";
 yell "DEBUG:sum:$(cat "$path_loop_name".sum | head -n1)";
 yell "DEBUG:output:$output";
 var_pv=1.0;
 yell "DEBUG:sum:$(cat "$path_loop_name".sum | head -n1)";
 yell "DEBUG:output:$output";
 var_pv=1.0;
-output="$(update_pi "$path_loop_name" "$var_pv" "$var_sp" "$tune_p" "$tune_i" | tail -n1)";
+output="$(update_pi "$path_loop_name" "$var_pv" "$var_sp" "$tune_p" "$tune_i" "$var_cv_init" | tail -n1)";
 yell "DEBUG:sum:$(cat "$path_loop_name".sum | head -n1)";
 yell "DEBUG:output:$output";
 #==END Example code==
 yell "DEBUG:sum:$(cat "$path_loop_name".sum | head -n1)";
 yell "DEBUG:output:$output";
 #==END Example code==