Advertisement
Guest User

Channels

a guest
Jul 11th, 2018
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
TCL 9.24 KB | None | 0 0
  1. proc startup {} {
  2.    
  3.    #lista zmiennych globalnych
  4.    global a
  5.    set a 0
  6.  
  7.    global b
  8.    set b 0
  9.    
  10.    global reference
  11.    set reference 1  
  12.  
  13.    global reference1
  14.    set reference1 1
  15.    
  16.    global reference2
  17.    set reference2 1
  18.    
  19.    global min_value
  20.    set min_value 0
  21.    
  22.    global max_value
  23.    set max_value 0
  24.    
  25.    global listoperator
  26.    set listoperator {}
  27.    
  28.    global c_value
  29.    set c_value 0
  30.    
  31.    global n_value
  32.    set n_value 0
  33.    
  34.    global integral  
  35.    set integral 0
  36.    
  37.    global c_time
  38.    set c_time 0
  39.  
  40.    global c_time1
  41.    set c_time1 0
  42.  
  43.    global n_time
  44.    set n_time 0
  45.    
  46.    global n_time1
  47.    set n_time1 0
  48.    
  49.    global integral_check
  50.    set integral_check 0
  51.    
  52.    #zmienna globalna w innym zapisie
  53.    set ::sum 0
  54.    
  55.    
  56.    #interwał czasowy
  57.    antInstallHook "5 s"
  58.    
  59.    #zainicjalizowanie/stworzenie kanałów, które wyświetlać będą konkretne wartości
  60.    antInitializeChannels "Test_channel_1_Callback_From_Sine" {Access: "Writable" ValueType: "Double" Format: "#0" Unit: "kV" Logging: "1" ReadingHook: "1m"}
  61.    antInitializeChannels "Test_channel_2_Callback_From_Square" {Access: "Writable" ValueType: "Double" Format: "#0" Unit: "kJ" Logging: "1" ReadingHook: "1m"}  
  62.    antInitializeChannels "Test_channel_3_Aritmetic_Function" {Access: "Writable" ValueType: "Double" Format: "#0" Unit: "MPH" Logging: "1" ReadingHook: "1m"}
  63.    antInitializeChannels "Test_channel_4_Sum_Of_Sine_And_Square" {Access: "Writable" ValueType: "Double" Format: "#0" Unit: "kg" Logging: "1" ReadingHook: "1m"}
  64.    antInitializeChannels "Test_channel_5_Product_Of_Sine_And_Square" {Access: "Writable" ValueType: "Double" Format: "#0" Unit: "oz" Logging: "1" ReadingHook: "1m"}
  65.    antInitializeChannels "Test_channel_6_Sine_Value_Powered_By_Module_Property" {Access: "Writable" ValueType: "Double" Format: "#0" Unit: "lux" Logging: "1" ReadingHook: "1m"}
  66.    antInitializeChannels "Test_channel_7_Minimal_Sine_Value" {Access: "Writable" ValueType: "Double" Format: "#0" Unit: "W" Logging: "1" ReadingHook: "1m"}
  67.    antInitializeChannels "Test_channel_8_Maximal_Sine_Value" {Access: "Writable" ValueType: "Double" Format: "#0" Unit: "mA" Logging: "1" ReadingHook: "1m"}
  68.    antInitializeChannels "Test_channel_9_Sum_Of_Last_10_Sine_Values" {Access: "Writable" ValueType: "Double" Format: "#0" Unit: "s" Logging: "1" ReadingHook: "1m"}
  69.    antInitializeChannels "Test_channel_10_Integral_Of_Sine" {Access: "Writable" ValueType: "Double" Format: "#0" Unit: "lbs" Logging: "1" ReadingHook: "1m"}
  70.    antInitializeChannels "Test_channel_11_Derivative_Of_Sine" {Access: "Writable" ValueType: "Double" Format: "#0" Unit: "km" Logging: "1" ReadingHook: "1m"}
  71.    
  72.    #zainstalowanie wywołań wartości kanałów: Sine i Square z Generatora
  73.    antInstallChannelCallback *Generator/Sine {} "callback"
  74.    antInstallChannelCallback *Generator/Square {} "callback1"
  75. }
  76.  
  77.  
  78. proc shutdown {} {
  79.    # Actions on module's shutdown
  80. }
  81.  
  82.  
  83. proc timerHook {hook_name timestamp} {
  84.    
  85.   if {$hook_name eq "5 s"} {
  86.       # Action on particular hook
  87.      
  88.       #ustalenie wartości kanału '3' na losową wartość podniesioną do potęgi 900
  89.       antSetChannelValue "Test_channel_3_Aritmetic_Function" [expr pow(rand(),900)]
  90.    }
  91. }
  92.  
  93.  
  94. #procedura dodawania elementów listy
  95. proc ladd {l} {
  96.    set total 0.0;
  97.    foreach nxt $l {
  98.       set total [expr {$total + $nxt}]};
  99.       return $total
  100.    }
  101.  
  102.    
  103. #procedura callback dla kanału Sine
  104. proc callback {channel_name timestamp channel_value status} {
  105.    
  106.    #odwolanie do zmiennych globalnych
  107.    global a
  108.    global b
  109.    global reference
  110.    global reference1
  111.    global reference2
  112.    global min_value
  113.    global max_value
  114.    global listoperator
  115.    global c_value
  116.    global n_value
  117.    global c_time
  118.    global c_time1
  119.    global n_time
  120.    global n_time1
  121.    global integral_check
  122.    
  123.    #zadeklarowanie zmiennej 'a' równiej wartości kanału Sine
  124.    set a $channel_value
  125.    
  126.    
  127.    #zadeklarowanie zmiennej 'b' równej propertiesowi modułu
  128.    set c [antGetSetting "ExponentOfPower"]
  129.    
  130.    
  131.    #porównywanie wartosci z kanalu z wykorzystaniem zmiennej 'reference'
  132.    #1 - wstępne przypisanie pierwszej wartości kanału zarówno jako wartość minimalna oraz maksymalna
  133.    if {$reference==1} {
  134.       set min_value $channel_value
  135.       set max_value $channel_value
  136.      
  137.    #wyzerowanie odniesienia, żeby nie przypisywana więcej była wartość wstępna  
  138.       set reference 0
  139.    }
  140.    
  141.    #2 - jeżeli kolejna wartość kanału jest mniejsza od dotychczasowej wartości minimalnej, wartość minimalna zostaje nadpisana tą wartością
  142.    if ($min_value>$channel_value) {
  143.       set min_value $channel_value
  144.    }
  145.    
  146.    #3 - jeżeli kolejna wartość kanału jest większa od dotyhczasowej wartości maksymalnej, wartośc maksymalna zostaje nadpisana tą wartością
  147.    if ($max_value<$channel_value) {
  148.       set max_value $channel_value
  149.    }
  150.  
  151.  
  152.    if {1 == $status} {
  153.      
  154.      #zagregowanie 10 najnowszych wartości kanału Sine
  155.      lappend listoperator $channel_value
  156.      
  157.      #jeżeli lista zawiera 10 elementów
  158.      if {[llength $listoperator]==10} {
  159.      
  160.      #następuje zsumowanie 10 wartości kanału w oparciu o utworzoną wcześniej procedurę listadditon
  161.      set ::sum [ladd $listoperator]
  162.  
  163.      #ustalenie wartości kanału '9' na sumę 10 wartości kanału Sine
  164.      antSetChannelValue "Test_channel_9_Sum_Of_Last_10_Sine_Values" $::sum
  165.      
  166.      #po wyciągnięciu sumy 10 wartości kanału, następuje wyzerowanie listoperatora oraz przechowującej sumę zmiennej suma
  167.      set listoperator [lreplace $listoperator 0 0]
  168.      set ::sum 0
  169.      }
  170.      
  171.    
  172. #     #obliczenie całki wartości kanału = suma[aktualna wartość*timestamp]
  173. #     if ($reference1==1) {
  174. #        
  175. #        set c_time $timestamp
  176. #        set reference1 0
  177. #        
  178. #     } else {
  179. #       set n_time1 $timestamp
  180. #      
  181. #       antSetChannelValue "Test_channel_10_Integral_Of_Sine" [expr {($n_value - $c_value)/($n_time1 - $c_time1)}]
  182. #       set c_time $n_time
  183.      
  184.      
  185.      #obliczanie różniczki wartości kanału = (aktualna wartość - poprzednia wartość)/(aktualny czas - poprzedni czas)
  186.      if ($reference1==1) {
  187.        
  188.         set c_value $channel_value
  189.         set c_time1 $timestamp
  190.         set reference1 0
  191.        
  192.      } else {
  193.        
  194.         set n_value $channel_value
  195.         set n_time1 $timestamp
  196.         antLog debug [expr ($n_time1 - $c_time1)]
  197.        
  198.         antSetChannelValue "Test_channel_11_Derivative_Of_Sine" [expr {($n_value - $c_value)/($n_time1 - $c_time1)}]
  199.        
  200.         set integral_check [expr $integral_check + [expr {($n_time1 - $c_time1)*$channel_value}]]
  201.  
  202.         antSetChannelValue "Test_channel_10_Integral_Of_Sine" $integral_check
  203.         set c_time1 $n_time1  
  204.         set c_value $n_value
  205.        
  206.      }
  207.      
  208.      
  209. #}
  210.    
  211.      #ustalenie wartości kanału '1' na wartość kanału Sine
  212.      antSetChannelValue "Test_channel_1_Callback_From_Sine" $channel_value
  213.      
  214.      #ustalenie wartości kanału '6' na wartość kanału Sine podniesiona do potęgi równej propertiesowi modułu (x^y)
  215.      antSetChannelValue "Test_channel_6_Sine_Value_Powered_By_Module_Property" [expr pow($channel_value, $c)]
  216.      
  217.      #ustalenie wartości kanału '7' na wartość minimalną kanału Sine
  218.      antSetChannelValue "Test_channel_7_Minimal_Sine_Value" $min_value
  219.      
  220.      #ustalenie wartości kanału '8' na wartość maksymalną kanału Sine
  221.      antSetChannelValue "Test_channel_8_Maximal_Sine_Value" $max_value
  222.    
  223.      #wyświetlenie logów informujących o pobranej wartości
  224.      antLog "Sine value is $channel_value"
  225.      
  226.      antLog "The value of channel $channel_name has been set to $channel_value at [antTimeToString $timestamp]"  
  227.  
  228.    } else {
  229.      
  230.      antLog "Failed to set channel $channel_name"
  231.      
  232.    }
  233. }
  234.  
  235.  
  236. #procedura callback1 dla kanału 'Square''
  237. proc callback1 {channel_name timestamp channel_value status} {
  238.    
  239.    #odwołanie do zmiennych globalnych
  240.    global a
  241.    global b
  242.    
  243.      
  244.    if {1 == $status} {
  245.      
  246.      #zadeklarowanie zmiennej 'b' równiej wartości kanału Square
  247.      set b $channel_value
  248.      
  249.      #ustalenie wartości kanału '2' na wartość kanału Square    
  250.      antSetChannelValue "Test_channel_2_Callback_From_Square" $channel_value      
  251.      
  252.      #ustalenie wartości kanału '4' równej sumie wartości kanałów Sine i Square
  253.      antSetChannelValue "Test_channel_4_Sum_Of_Sine_And_Square" [expr $a+$b]
  254.      
  255.      #ustalenie wartości kanału '5' równej iloczynowi wartości kanałów Sine i Square
  256.      antSetChannelValue "Test_channel_5_Product_Of_Sine_And_Square" [expr $a*$b]
  257.      
  258.      
  259.      
  260.      #wyświetlenie logów informujących o pobranych wartościach kanału, sumy oraz iloczynu
  261.      antLog "Square value is $channel_value"
  262.      antLog "The value of channel $channel_name has been set to $channel_value at [antTimeToString $timestamp]"  
  263.      antLog "The damned sum of Sine and Square channel values is: [expr $a+$b]"
  264.      antLog "A deprecated product of Sine and Square channel values is: [expr $a*$b]"
  265.    
  266.    } else {
  267.      
  268.      antLog "Failed to set channel $channel_name"
  269.      
  270.    }
  271. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement