User Tools

Site Tools


curves

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
curves [2020/07/30 16:06] atolstovcurves [2022/05/24 08:52] (current) – [Get value from the curve via Lua] emozolyak
Line 1: Line 1:
 +{{ network:menu-icon-curves.png?nolink&60|}}
 ======Curves====== ======Curves======
 =====Introduction===== =====Introduction=====
Line 4: Line 5:
 Curve is a way to define output value from a pre-defined pair table of a discrete set of known data points. Such as standard or desired value. Curve is a way to define output value from a pre-defined pair table of a discrete set of known data points. Such as standard or desired value.
  
-It can be represented as a black-box, where is some **INPUT x** have corresponding **OUTPUT y** = f(x)+Curve can be represented as a function f, which is a blackbox, where is some **INPUT x** have corresponding **OUTPUT y** = f(x)
  
 Curve is using a piecewise linear interpolation, is a curve fitting which is a allow to using linear polynomials to construct new data points within the range of a discrete set of known data points. Curve is using a piecewise linear interpolation, is a curve fitting which is a allow to using linear polynomials to construct new data points within the range of a discrete set of known data points.
Line 55: Line 56:
 {{ :curve:c6.png?direct&600 |}}\\  {{ :curve:c6.png?direct&600 |}}\\ 
 =====Select a curve to put in register===== =====Select a curve to put in register=====
-First, to get value from curve you need the curves and the register pre-created. +To get the value you need
-Now, it is time to create a register with value type "String".+  Have a curve 
 +  - Have string register for dashboard element to put selected curve in it 
 +  - Optionally, have yet another string register for store that curve all the time
  
 +Now, it is time to create a register with value type "String" for dashboard element.
  
 Then, create a new dashboard.  Then, create a new dashboard. 
Line 85: Line 89:
 {{ :curve:reg_filled_with_equat.png?direct&600 |}} {{ :curve:reg_filled_with_equat.png?direct&600 |}}
  
-=====Get value from the curve=====+=====Get value from the curve via Lua =====
  
 To get the **OUTPUT y value** from existing(pre-created) curve, you need: To get the **OUTPUT y value** from existing(pre-created) curve, you need:
Line 95: Line 99:
  
 <code lua - curves.lib> <code lua - curves.lib>
-function GetCurveValue curve_register, x_to_find_y  +--[[ 
-    --[[    +The function does curve conversion f(inputValue= outputValue 
-    --Curve handler  +Example of a curve, having multiple ranges of kx + b, set in a register of a String type with Json  
-    INPUT: +[{"k":4.167,"b":0,"range":[0,3]},{"k":0,"b":12.5,"range":[3,27]},{"k":-4.167,"b":125,"range":[27,30]}] 
-        Put curve register as first argument and X coordinate as second. +--]] 
-    OUTPUT: +function getCurveValue (curveReg, inputValue) 
-        Get as result status if it is inside curve range of outside(falseand the Y value as second output argument+ 
 +    local s, e = 1, 2                           -- start & end indexes of the subranges  
 +    local tab = cjson.decodeR(curveReg
 +    local outputValue, stickOutflag
          
-    EXAMPLE: +    for _, sub_ in ipairs(tab) do  
-        curve_status, value = GetCurveValue( "curve_for_current_hour", current_hour ) +        if ( (sub_.range[s] <= inputValue) and (inputValue sub_.range[e]) then  
-    --]] +            return (inputValue sub_.k + sub_.b), nil                             -- normal sequence 
-    table = cjson.decode( R ( curve_register ) ) +
-    -- inRangeCurve( table, x_to_find_y ) +
-    for _, value in ipairs( table ) do --piecewise handler inside range of the curve +
-        if ( value.range[1] <= x_to_find_y and x_to_find_y value.range[2] ) then  +
-            -- in_range_status, index_curve_piece, y = true, index, curveLinearCalc( x_to_find_y, value.k, value.b ) +
-            in_range_status, y = true, x_to_find_y value.k + value.b ) +
-            return in_range_statusy+
         end         end
     end     end
-    if  x_to_find_y < table[1].range[1] then -- behaivior for outside left-sided +    -- edge cases  
-        -- in_range_status, index_curve_piece, y = false, 1, curveLinearCalctable[1].range[1], table[1].k*0, table[1].b --table[1].b +    if  (inputValue < tab[s].range[s]) then  
-        in_range_status, index_curve_piece,false, 1, ( table[1].range[1] * table[1].k + table[1].b ) --table[1].b +        outputValue tab[s].range[s] * tab[s].k + tab[s].b  
-        return in_range_status, y +        stickOutflag = 1  
-    elseif x_to_find_y table[#table].range[2] then -- behaivior for outside right-sided +    elseif (inputValue tab[#tab].range[e]then  
-        -- in_range_status, index_curve_piece,false,#table,curveLinearCalc( table[#table].range[2], table[#table].k, table[#table].b ) --table[#table].b +        outputValue tab[#tab].range[e* tab[#tab].k + tab[#tab].b 
-        in_range_status, false,#table,( table[#table].range[2] * table[#table].k + table[#table].b) +        stickOutflag = #tab 
-        return in_range_status, y+
     end     end
- +    return outputValuestickOutflag
-    return in_range_status+
 end end
 </code> </code>
 Example of usage: Example of usage:
 <code lua - Getter> <code lua - Getter>
-include "curves.lib"+include "curves.lib" 
  
 function main (userId) function main (userId)
- +    local SAFE_VALUE = 10  
-    local = os.date("*t", os.time())  +    local curHour tonumber( os.date("*t", os.time()).hour )  
-    current_hour = tonumber(t.hour+    INFO("curHour "  .. curHour
-    curve_status, y = GetCurveValue( "curve_for_current_hour", current_hour +    local y, valueOutOfRange getCurveValue("curve_for_curHour", curHour
-    WriteReg("value_for_current_hour", y)  +     
 +    if (not valueOutOfRange) then 
 +        W("value_for_curHour", y)  -- WriteReg 
 +    else 
 +        W("value_for_curHour", SAFE_VALUE) 
 +    end
 end end
 </code> </code>
curves.1596125199.txt.gz · Last modified: 2020/07/30 16:06 by atolstov

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki