feat(unitproc):Add PI loop updater
[BK-2020-03.git] / unitproc / bktemp-updateLoopPI
... / ...
CommitLineData
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.
11 # Usage: update_pi arg1 arg2 arg3 arg4 arg5
12 # Version: 0.0.1
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 # Output: stdout: var_cv (control variable)
19 # file: path_var_pv
20 # file: path_var_sp
21 # file: path_tune_p
22 # file: path_tune_i
23 # file: path_sum (saves updated sum state)
24 # Example: update_pi /dev/shm/DC1.AC 1.0 3.0 2.0 3.0
25 # Depends: bc, gnu coreutils 8.30, yell(), try()
26 local var_sp var_pv var_cv;
27 local tune_p tune_i;
28 local term_p term_i;
29 local error sum;
30 local path_sum path_var_sp path_var_pv path_tune_p path_tune_i;
31
32 path_loop_name="$1";
33
34 path_var_pv="$path_loop_name".pv
35 var_pv="$2"; # read provided process variable
36
37 path_var_sp="$path_loop_name".sp
38 if [[ -f "$path_var_sp" ]]; then
39 var_sp="$(cat "$path_var_sp" | head -n1)";
40 else
41 var_sp="$3";
42 fi;
43
44 path_tune_p="$path_loop_name".tune_p
45 if [[ -f "$path_tune_p" ]]; then
46 tune_p="$(cat "$path_tune_p" | head -n1)";
47 else
48 tune_p="$4";
49 fi;
50
51 path_tune_i="$path_loop_name".tune_i
52 if [[ -f "$path_tune_i" ]]; then
53 tune_i="$(cat "$path_tune_i" | head -n1)";
54 else
55 tune_i="$5";
56 fi;
57
58 path_sum="$path_loop_name".sum
59 if [[ -f "$path_sum" ]]; then
60 sum="$(cat "$path_sum" | head -n1)";
61 else
62 sum=0;
63 fi;
64
65 #yell "DEBUG:path_loop_name:$path_loop_name";
66 #yell "DEBUG:var_pv:$var_pv";
67 #yell "DEBUG:var_sp:$var_sp";
68 #yell "DEBUG:tune_p:$tune_p";
69 #yell "DEBUG:tune_i:$tune_i";
70
71 error="$(try echo "$var_sp - $var_pv" | bc -l)";
72 #yell "DEBUG:error:$error";
73 sum="$(try echo "$sum + $error" | bc -l)";
74 #yell "DEBUG:sum:$sum";
75 term_p="$(try echo "$tune_p * $error" | bc -l)";
76 #yell "DEBUG:term_p:$term_p";
77 term_i="$(try echo "$tune_i * $sum" | bc -l)";
78 #yell "DEBUG:term_i:$term_i";
79 var_cv="$(try echo "$term_p + $term_i" | bc -l)";
80 #yell "DEBUG:var_cv:$var_cv";
81
82 # Write variables to memory
83 echo "$sum" > "$path_sum";
84 echo "$var_sp" > "$path_var_sp";
85 echo "$var_pv" > "$path_var_pv";
86 echo "$tune_p" > "$path_tune_p";
87 echo "$tune_i" > "$path_tune_i";
88
89 # Output control variable to stdout
90 echo "$var_cv";
91
92 #yell "DEBUG:=============END_ROUND===============";
93}
94#==END function definition==
95
96#==BEGIN Example code==
97path_loop_name="/tmp/DC1.AC";
98var_pv=1.0;
99var_sp=2.0;
100tune_p=1.0;
101tune_i=0.1;
102try rm "$path_loop_name"*
103
104var_pv=4.0;
105output="$(update_pi "$path_loop_name" "$var_pv" "$var_sp" "$tune_p" "$tune_i" | tail -n1)";
106yell "DEBUG:sum:$(cat "$path_loop_name".sum | head -n1)";
107yell "DEBUG:output:$output";
108var_pv=4.0;
109output="$(update_pi "$path_loop_name" "$var_pv" "$var_sp" "$tune_p" "$tune_i" | tail -n1)";
110yell "DEBUG:sum:$(cat "$path_loop_name".sum | head -n1)";
111yell "DEBUG:output:$output";
112var_pv=4.0;
113output="$(update_pi "$path_loop_name" "$var_pv" "$var_sp" "$tune_p" "$tune_i" | tail -n1)";
114yell "DEBUG:sum:$(cat "$path_loop_name".sum | head -n1)";
115yell "DEBUG:output:$output";
116var_pv=4.0;
117output="$(update_pi "$path_loop_name" "$var_pv" "$var_sp" "$tune_p" "$tune_i" | tail -n1)";
118yell "DEBUG:sum:$(cat "$path_loop_name".sum | head -n1)";
119yell "DEBUG:output:$output";
120var_pv=4.0;
121output="$(update_pi "$path_loop_name" "$var_pv" "$var_sp" "$tune_p" "$tune_i" | tail -n1)";
122yell "DEBUG:sum:$(cat "$path_loop_name".sum | head -n1)";
123yell "DEBUG:output:$output";
124var_pv=1.0;
125output="$(update_pi "$path_loop_name" "$var_pv" "$var_sp" "$tune_p" "$tune_i" | tail -n1)";
126yell "DEBUG:sum:$(cat "$path_loop_name".sum | head -n1)";
127yell "DEBUG:output:$output";
128var_pv=1.0;
129output="$(update_pi "$path_loop_name" "$var_pv" "$var_sp" "$tune_p" "$tune_i" | tail -n1)";
130yell "DEBUG:sum:$(cat "$path_loop_name".sum | head -n1)";
131yell "DEBUG:output:$output";
132var_pv=1.0;
133output="$(update_pi "$path_loop_name" "$var_pv" "$var_sp" "$tune_p" "$tune_i" | tail -n1)";
134yell "DEBUG:sum:$(cat "$path_loop_name".sum | head -n1)";
135yell "DEBUG:output:$output";
136var_pv=1.0;
137output="$(update_pi "$path_loop_name" "$var_pv" "$var_sp" "$tune_p" "$tune_i" | tail -n1)";
138yell "DEBUG:sum:$(cat "$path_loop_name".sum | head -n1)";
139yell "DEBUG:output:$output";
140#==END Example code==
141
142# Author: Steven Baltakatei Sandoval
143# License: GPLv3+