5.2. The Plant Model - change_procvar

This part of the program performs the calculations for changing the value of PV_001. It starts by registering the two points related to the process variable: PROP_001(the proportion) and INT_001 (the integral). Then it checks to make sure they are within the correct range, and if not it sets them to the values corresponding to Poor in the Monitor's PID Loop options, using the Gamma write_point function.

   /********************************************************
    *                   The Plant Model                    *
    ********************************************************/
   
   /* The proportion and integral for PV. */
   PROP_001 = register_point(#PROP_001); 
   INT_001 = register_point(#INT_001);   
   
   /* Check and correct out-of-range values,
    * by writing default "Poor" values.*/
   if (PROP_001 < .01 || PROP_001 > 10)  
        write_point(#PROP_001, .7);      
   if (INT_001 <= 0 || INT_001 > 10)
        write_point(#INT_001, 5);
   

The change_procvar function calculates and assigns a new value to the process variable. Using the proportion and integral, it modifies the difference between the control output (mvtag) and the process variable (pvtag) to calculate a new value for the the process variable. That value then gets written to the datahub.

   /*--------------------------------------------------------------------
    * Function:    change_procvar
    * Returns:     t on success, nil on failure
    * Description: Sets and writes a new PV value.
    *------------------------------------------------------------------*/
   function change_procvar (sptag, mvtag, pvtag) // 
   {
     local diff = eval(mvtag) * PROP_001 - eval (pvtag),
           newval = eval(pvtag) + diff / INT_001;
       

Before writing the value to the datahub, we manipulate it a little. First, we use the floor function to help cut newval to a manageable size—two decimal places. This reduces the amount of minor fluctuations in MV_001 and PV_001 when they reach a stable state. Then we multiply newval by demo_send, an adjustment factor that the tutorial uses to demonstrate lsend and gsend. Finally we write the new value (via pvtag) to the datahub point PV_001.

     newval = floor (newval * 100) / 100;
     newval = newval * demo_send;
     write_point (pvtag, newval);
   }