style(unitproc/bkt-bkunzip):Treat bkunzip function as template
[BK-2020-03.git] / unitproc / bkt-updateLoopPI
CommitLineData
5113c9ac
SBS
1#!/bin/bash
2
3#==BEGIN function definition==
4yell() { echo "$0: $*" >&2; } # print script path and all args to stderr
5die() { yell "$*"; exit 111; } # same as yell() but non-zero exit status
6try() { "$@" || die "cannot $*"; } # runs args as command, reports args if command fails
7update_pi() {
8 # Desc: Calculates control variable (CV) given setpoint (SP) and
9 # process variable (PV). Uses proportional integral (PI)
10 # control.
77c52629 11 # Usage: update_pi arg1 arg2 arg3 arg4 arg5 arg6
12674731 12 # Version: 0.1.3
5113c9ac
SBS
13 # Input: arg1: path_loop_name (control loop name path)
14 # arg2: var_pv (process variable)
15 # arg3: var_sp (set point)
16 # arg4: tune_p (proportional tuning factor)
17 # arg5: tune_i (integral tuning factor)
77c52629 18 # arg6: var_cv_bias (control variable bias; prevents initial jerk)
5113c9ac
SBS
19 # Output: stdout: var_cv (control variable)
20 # file: path_var_pv
21 # file: path_var_sp
22 # file: path_tune_p
23 # file: path_tune_i
77c52629 24 # file: path_var_cv
5113c9ac
SBS
25 # file: path_sum (saves updated sum state)
26 # Example: update_pi /dev/shm/DC1.AC 1.0 3.0 2.0 3.0
27 # Depends: bc, gnu coreutils 8.30, yell(), try()
28 local var_sp var_pv var_cv;
29 local tune_p tune_i;
30 local term_p term_i;
8be2ec95 31 local error sum sum_cand;
5113c9ac
SBS
32 local path_sum path_var_sp path_var_pv path_tune_p path_tune_i;
33
34 path_loop_name="$1";
35
36 path_var_pv="$path_loop_name".pv
37 var_pv="$2"; # read provided process variable
38
39 path_var_sp="$path_loop_name".sp
40 if [[ -f "$path_var_sp" ]]; then
41 var_sp="$(cat "$path_var_sp" | head -n1)";
42 else
43 var_sp="$3";
44 fi;
45
46 path_tune_p="$path_loop_name".tune_p
47 if [[ -f "$path_tune_p" ]]; then
48 tune_p="$(cat "$path_tune_p" | head -n1)";
49 else
50 tune_p="$4";
51 fi;
52
53 path_tune_i="$path_loop_name".tune_i
54 if [[ -f "$path_tune_i" ]]; then
55 tune_i="$(cat "$path_tune_i" | head -n1)";
56 else
57 tune_i="$5";
58 fi;
59
77c52629
SBS
60 path_var_cv="$path_loop_name".cv
61
a47be58a 62 path_var_cv_bias="$path_loop_name".cv_bias
77c52629
SBS
63 var_cv_bias="$6";
64
5113c9ac
SBS
65 path_sum="$path_loop_name".sum
66 if [[ -f "$path_sum" ]]; then
67 sum="$(cat "$path_sum" | head -n1)";
68 else
69 sum=0;
70 fi;
71
72 #yell "DEBUG:path_loop_name:$path_loop_name";
73 #yell "DEBUG:var_pv:$var_pv";
74 #yell "DEBUG:var_sp:$var_sp";
77c52629 75 #yell "DEBUG:var_cv:$var_cv";
a47be58a 76 #yell "DEBUG:var_cv_bias:$var_cv_bias";
5113c9ac
SBS
77 #yell "DEBUG:tune_p:$tune_p";
78 #yell "DEBUG:tune_i:$tune_i";
79
80 error="$(try echo "$var_sp - $var_pv" | bc -l)";
81 #yell "DEBUG:error:$error";
8be2ec95 82 sum_cand="$(try echo "$sum + $error" | bc -l)";
5113c9ac 83 #yell "DEBUG:sum:$sum";
8be2ec95 84 if [[ "$(try echo "$sum_cand > 2 * $sum " | bc -l)" -eq 1 ]]; then
12674731 85 sum="$(try echo "$sum + l($error + 1)" | bc -l)"; # dampen integral sum spikes
8be2ec95
SBS
86 else
87 sum="$sum_cand";
88 fi;
5113c9ac
SBS
89 term_p="$(try echo "$tune_p * $error" | bc -l)";
90 #yell "DEBUG:term_p:$term_p";
91 term_i="$(try echo "$tune_i * $sum" | bc -l)";
92 #yell "DEBUG:term_i:$term_i";
77c52629 93 var_cv="$(try echo "$term_p + $term_i + $var_cv_bias" | bc -l)";
5113c9ac
SBS
94 #yell "DEBUG:var_cv:$var_cv";
95
96 # Write variables to memory
97 echo "$sum" > "$path_sum";
98 echo "$var_sp" > "$path_var_sp";
99 echo "$var_pv" > "$path_var_pv";
100 echo "$tune_p" > "$path_tune_p";
101 echo "$tune_i" > "$path_tune_i";
77c52629 102 echo "$var_cv" > "$path_var_cv";
a47be58a 103 echo "$var_cv_bias" > "$path_var_cv_bias";
5113c9ac
SBS
104
105 # Output control variable to stdout
106 echo "$var_cv";
107
108 #yell "DEBUG:=============END_ROUND===============";
37bc30fd 109} # update specified PI loop
5113c9ac
SBS
110#==END function definition==
111
112#==BEGIN Example code==
113path_loop_name="/tmp/DC1.AC";
114var_pv=1.0;
115var_sp=2.0;
116tune_p=1.0;
117tune_i=0.1;
77c52629 118var_cv_init=1000;
5113c9ac
SBS
119try rm "$path_loop_name"*
120
121var_pv=4.0;
77c52629 122output="$(update_pi "$path_loop_name" "$var_pv" "$var_sp" "$tune_p" "$tune_i" "$var_cv_init" | tail -n1)";
5113c9ac
SBS
123yell "DEBUG:sum:$(cat "$path_loop_name".sum | head -n1)";
124yell "DEBUG:output:$output";
125var_pv=4.0;
77c52629 126output="$(update_pi "$path_loop_name" "$var_pv" "$var_sp" "$tune_p" "$tune_i" "$var_cv_init" | tail -n1)";
5113c9ac
SBS
127yell "DEBUG:sum:$(cat "$path_loop_name".sum | head -n1)";
128yell "DEBUG:output:$output";
129var_pv=4.0;
77c52629 130output="$(update_pi "$path_loop_name" "$var_pv" "$var_sp" "$tune_p" "$tune_i" "$var_cv_init" | tail -n1)";
5113c9ac
SBS
131yell "DEBUG:sum:$(cat "$path_loop_name".sum | head -n1)";
132yell "DEBUG:output:$output";
133var_pv=4.0;
77c52629 134output="$(update_pi "$path_loop_name" "$var_pv" "$var_sp" "$tune_p" "$tune_i" "$var_cv_init" | tail -n1)";
5113c9ac
SBS
135yell "DEBUG:sum:$(cat "$path_loop_name".sum | head -n1)";
136yell "DEBUG:output:$output";
137var_pv=4.0;
77c52629 138output="$(update_pi "$path_loop_name" "$var_pv" "$var_sp" "$tune_p" "$tune_i" "$var_cv_init" | tail -n1)";
5113c9ac
SBS
139yell "DEBUG:sum:$(cat "$path_loop_name".sum | head -n1)";
140yell "DEBUG:output:$output";
141var_pv=1.0;
77c52629 142output="$(update_pi "$path_loop_name" "$var_pv" "$var_sp" "$tune_p" "$tune_i" "$var_cv_init" | tail -n1)";
5113c9ac
SBS
143yell "DEBUG:sum:$(cat "$path_loop_name".sum | head -n1)";
144yell "DEBUG:output:$output";
145var_pv=1.0;
77c52629 146output="$(update_pi "$path_loop_name" "$var_pv" "$var_sp" "$tune_p" "$tune_i" "$var_cv_init" | tail -n1)";
5113c9ac
SBS
147yell "DEBUG:sum:$(cat "$path_loop_name".sum | head -n1)";
148yell "DEBUG:output:$output";
149var_pv=1.0;
77c52629 150output="$(update_pi "$path_loop_name" "$var_pv" "$var_sp" "$tune_p" "$tune_i" "$var_cv_init" | tail -n1)";
5113c9ac
SBS
151yell "DEBUG:sum:$(cat "$path_loop_name".sum | head -n1)";
152yell "DEBUG:output:$output";
153var_pv=1.0;
77c52629 154output="$(update_pi "$path_loop_name" "$var_pv" "$var_sp" "$tune_p" "$tune_i" "$var_cv_init" | tail -n1)";
5113c9ac
SBS
155yell "DEBUG:sum:$(cat "$path_loop_name".sum | head -n1)";
156yell "DEBUG:output:$output";
157#==END Example code==
158
159# Author: Steven Baltakatei Sandoval
160# License: GPLv3+