3 #==BEGIN function definition==
4 yell
() { echo "$0: $*" >&2; } # print script path and all args to stderr
5 die
() { yell
"$*"; exit 111; } # same as yell() but non-zero exit status
6 try
() { "$@" || die
"cannot $*"; } # runs args as command, reports args if command fails
8 # Desc: Calculates control variable (CV) given setpoint (SP) and
9 # process variable (PV). Uses proportional integral (PI)
11 # Usage: update_pi arg1 arg2 arg3 arg4 arg5 arg6
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)
18 # arg6: var_cv_bias (control variable bias; prevents initial jerk)
19 # Output: stdout: var_cv (control variable)
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
;
32 local path_sum path_var_sp path_var_pv path_tune_p path_tune_i
;
36 path_var_pv
="$path_loop_name".pv
37 var_pv
="$2"; # read provided process variable
39 path_var_sp
="$path_loop_name".sp
40 if [[ -f "$path_var_sp" ]]; then
41 var_sp
="$(cat "$path_var_sp" | head -n1)";
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)";
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)";
60 path_var_cv
="$path_loop_name".cv
64 path_sum
="$path_loop_name".
sum
65 if [[ -f "$path_sum" ]]; then
66 sum="$(cat "$path_sum" | head -n1)";
71 #yell "DEBUG:path_loop_name:$path_loop_name";
72 #yell "DEBUG:var_pv:$var_pv";
73 #yell "DEBUG:var_sp:$var_sp";
74 #yell "DEBUG:var_cv:$var_cv";
75 #yell "DEBUG:tune_p:$tune_p";
76 #yell "DEBUG:tune_i:$tune_i";
78 error
="$(try echo "$var_sp - $var_pv" | bc -l)";
79 #yell "DEBUG:error:$error";
80 sum="$(try echo "$sum + $error" | bc -l)";
81 #yell "DEBUG:sum:$sum";
82 term_p
="$(try echo "$tune_p * $error" | bc -l)";
83 #yell "DEBUG:term_p:$term_p";
84 term_i
="$(try echo "$tune_i * $sum" | bc -l)";
85 #yell "DEBUG:term_i:$term_i";
86 var_cv
="$(try echo "$term_p + $term_i + $var_cv_bias" | bc -l)";
87 #yell "DEBUG:var_cv:$var_cv";
89 # Write variables to memory
90 echo "$sum" > "$path_sum";
91 echo "$var_sp" > "$path_var_sp";
92 echo "$var_pv" > "$path_var_pv";
93 echo "$tune_p" > "$path_tune_p";
94 echo "$tune_i" > "$path_tune_i";
95 echo "$var_cv" > "$path_var_cv";
97 # Output control variable to stdout
100 #yell "DEBUG:=============END_ROUND===============";
102 #==END function definition==
104 #==BEGIN Example code==
105 path_loop_name
="/tmp/DC1.AC";
111 try
rm "$path_loop_name"*
114 output
="$(update_pi "$path_loop_name" "$var_pv" "$var_sp" "$tune_p" "$tune_i" "$var_cv_init" | tail -n1)";
115 yell
"DEBUG:sum:$(cat "$path_loop_name".sum | head -n1)";
116 yell
"DEBUG:output:$output";
118 output
="$(update_pi "$path_loop_name" "$var_pv" "$var_sp" "$tune_p" "$tune_i" "$var_cv_init" | tail -n1)";
119 yell
"DEBUG:sum:$(cat "$path_loop_name".sum | head -n1)";
120 yell
"DEBUG:output:$output";
122 output
="$(update_pi "$path_loop_name" "$var_pv" "$var_sp" "$tune_p" "$tune_i" "$var_cv_init" | tail -n1)";
123 yell
"DEBUG:sum:$(cat "$path_loop_name".sum | head -n1)";
124 yell
"DEBUG:output:$output";
126 output
="$(update_pi "$path_loop_name" "$var_pv" "$var_sp" "$tune_p" "$tune_i" "$var_cv_init" | tail -n1)";
127 yell
"DEBUG:sum:$(cat "$path_loop_name".sum | head -n1)";
128 yell
"DEBUG:output:$output";
130 output
="$(update_pi "$path_loop_name" "$var_pv" "$var_sp" "$tune_p" "$tune_i" "$var_cv_init" | tail -n1)";
131 yell
"DEBUG:sum:$(cat "$path_loop_name".sum | head -n1)";
132 yell
"DEBUG:output:$output";
134 output
="$(update_pi "$path_loop_name" "$var_pv" "$var_sp" "$tune_p" "$tune_i" "$var_cv_init" | tail -n1)";
135 yell
"DEBUG:sum:$(cat "$path_loop_name".sum | head -n1)";
136 yell
"DEBUG:output:$output";
138 output
="$(update_pi "$path_loop_name" "$var_pv" "$var_sp" "$tune_p" "$tune_i" "$var_cv_init" | tail -n1)";
139 yell
"DEBUG:sum:$(cat "$path_loop_name".sum | head -n1)";
140 yell
"DEBUG:output:$output";
142 output
="$(update_pi "$path_loop_name" "$var_pv" "$var_sp" "$tune_p" "$tune_i" "$var_cv_init" | tail -n1)";
143 yell
"DEBUG:sum:$(cat "$path_loop_name".sum | head -n1)";
144 yell
"DEBUG:output:$output";
146 output
="$(update_pi "$path_loop_name" "$var_pv" "$var_sp" "$tune_p" "$tune_i" "$var_cv_init" | tail -n1)";
147 yell
"DEBUG:sum:$(cat "$path_loop_name".sum | head -n1)";
148 yell
"DEBUG:output:$output";
149 #==END Example code==
151 # Author: Steven Baltakatei Sandoval