5.4. Emulating the PID Loop - change_values

To emulate the PID loop, we first create a function to call the .calculate method of the PID_Controller class to write MV_001 values, and the change_procvar function to write PV_001 values.

   /********************************************************
    *               Emulating the PID Loop                 *
    ********************************************************/
   
   /*--------------------------------------------------------------------
    * Function:    change_values
    * Returns:     function
    * Description: Calls PID_Controller.calculate() and change_procvar()
    *------------------------------------------------------------------*/
   function change_values (controller)
   {
     controller.calculate(#SP_001, #MV_001, #PV_001);
     change_procvar (#SP_001, #MV_001, #PV_001);
     write_point (#CYCLE, ++cycle_count);
   }
   

Next, we create an instance of the PID_Controller class (PID1) and initialize it. Then we set an every timer to call the change_values function every 1/10 of a second. After that we call the auto_control function once to set the value to what already exists in the datahub. Finally, we set up a typical Gamma event loop using the while statement with the next_event function.

   /* Create a controller. */
   PID1 = new(PID_Controller);   
   PID1.init ("PID1");
   
   /* Start the controller running. */
   every (0.1, #change_values(PID1));
   
   write_point(#AUTO_001, 1);
   AUTO_TIMER = auto_control(#FREQ_001);
   
   /* Loop continuously. */
   while (t)
   {
     next_event();
   }