8.5. Trend Functions - accumulate_trends, update_trends

The QNX versions of the Monitor each use a Photon trend widget to plot trends of the three main variables: SP_001, MV_001, and PV_001. The values of these variables have to be collected over time and put into an array, which is used by the trend widget display. This breaks down to three tasks: associate an array with each variable, put the data into the array, and pass the array to the trend widget.

   /* Assign each DataHub point a property value, which in this case is an
    * array.  These arrays will be used to hold a short history of each point,
    * allowing them to be plotted by the trend widget.
    */
   setprop (#SP_001, #tdata, make_array (0));
   setprop (#MV_001, #tdata, make_array (0));
   setprop (#PV_001, #tdata, make_array (0));
   

The first task is to associate an array with each variable. This is done by using a feature in Gamma called properties. A property is a (name . value) pair that can be associated with any symbol in Gamma. The value can be any data type, including (as in this case) an array. The Gamma functions for working with properties include set_prop and get_prop. In these three lines, then, we use the setprop function to assign a new property (named tdata) to each point, and give the property the value of an array. The array is created using the Gamma function make_array and initially consists of zero (0) elements.

The next task is to accumulate data to put into the arrays. The following function is called every .1 seconds from within the create_monitor function. This timing is the same as the PID Emulator, so that every time the 3 values change in the Cascade DataHub, this function expands each array by one element, and puts in the new value.

   /*--------------------------------------------------------------------
    * Function:    accumulate_trends
    * Returns:     t or nil
    * Description: Adds new data to property value arrays.  These arrays
    *              are property values for the variables that correspond
    *              to the DataHub points, and are used to update the trend
    *              widget.
    *------------------------------------------------------------------*/
   function accumulate_trends (syms...)
   {
     local data;
     with sym in syms do
       {
         data = getprop (sym, #tdata);
         data[length(data)] = eval (sym);
       }
   }
   

In the final step, the following function is also called every .1 seconds, and it assigns the latest arrays to the trend widget.

[Note]

This function is an adaptation of the Tutorial Two function update_trends. That function uses the abstracted method .put_data to access trend data, rather than an if statement.

   /*--------------------------------------------------------------------
    * Function:    update_trends
    * Returns:     t or nil
    * Description: Assigns the most recent arrays to the trend widget.
    *------------------------------------------------------------------*/
   function update_trends (widget, syms...)
   {
     local        tarray = make_array (length(syms)), i = 0, sym;
     
     with sym in syms do
       tarray[i++] = getprop (sym, #tdata);
     if(_os_ == "QNX4")
       widget.rttrend_data = tarray;
     else
       widget.trend_data = tarray;
     
     with sym in syms do
       shorten_array (getprop (sym, #tdata), 0);
   }
   

The trend widget in QNX 4 (using Photon 1.14) is RtTrend, and in QNX 6 (using Photon 2) it's PtTrend. The trend data resource for these are named .rttrend_data and .trend_data respectively, which thus must be treated separately.