User Tools

Site Tools


curves

Differences

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

Link to this comparison view

Next revision
Previous revision
curves [2020/07/30 12:50] – created 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===== 
 +{{ :curve:function_machine2.png?nolink&200|}}
 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 have corresponding OUTPUT.(svg)+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 21: Line 23:
   * There is strict algebraic function and value should be calculated.   * There is strict algebraic function and value should be calculated.
   * The function should be sequentually or time-to-time changed.   * The function should be sequentually or time-to-time changed.
-<del> 
-To define curve algebraically, it is linear equation of a line that passes through two given points used. It is also known as Linear interpolation between two known points.</del> 
  
-To define curve algebraically, it is linear interpolation between two known points for every piece used. Its string reprentation is a JSON formatted linear polynomial factor "**k**", syllable "**b**" and function definition domain "**range**" values for every piece of a curve, so that inside the **range**  of INPUT value OUTPUT value can be calculated as\\  ''y = **k** × x + **b**''+To define curve algebraically, it is linear interpolation between two known points for every piece used. Its string reprentation is a JSON formatted linear polynomial factor "**k**", syllable "**b**" and function definition domain "**range**" values for every piece of a curve, so that inside the **range**  of **INPUT x** value **OUTPUT y** value can be calculated as\\  ''y = **k**x + **b**''
  
-For example: +For example:\\  
-(0 to 100 line; 0 1 0 1 / 24 hour ; temperature citoterm falling curve; sinus)+At minimum it can be two points.\\  
 +{{ :curve:c1.png?direct&600 |}}\\  
 +Binary logic function.\\  
 +{{ :curve:c2.png?direct&600 |}}\\  
 +Table parameter definition\\  
 +{{ :curve:c3.png?direct&600 |}}\\  
 +Saturation function\\  
 +{{ :curve:c4.png?direct&600 |}}\\  
 +Algebraic defined calculated function. For example sin(xwith 6 points approximation.\\  
 +{{ :curve:c5.png?direct&600 |}}\\ 
  
-To get the OUTPUT value **y** from existing(pre-created) curve, you need:+=====New curve creation===== 
 +Let's create a new curve. \\  
 +Click on "New curve" button.\\  
 +{{ :curve:curve_new.png?direct&600 |}}\\  
 +Type in a Title and go on the next tab ''Curve''\\  
 +{{ :curve:curve_tab.png?direct&600 |}}\\  
 +Optionally, you can check "Use the existing String register to store the curve parameters" to store it all the time in this register if you will. 
 +{{ :curve:c1_start.png?direct&600 |}} 
 + 
 +Here is a curve editor. There is a domain of a function definition, range of a function and every point X and Y coordinates.\\  
 +{{ :curve:curve_new_start.png?direct&600 |}}\\  
 +To create a new point click twice on a line. To remove it click with holding Alt button.\\  
 +{{ :curve:curve_new_click.png?direct&600 |}}\\  
 +Then you can both move point or specify its value using form in the right.\\  
 +{{ :curve:curve_new_move.png?direct&600 |}}\\  
 +At the end, when all the rest of parameters set let's look at the result . In our example it would more precise approximation of sin(x) with 7 point defined\\  
 +{{ :curve:c6.png?direct&600 |}}\\  
 +=====Select a curve to put in register===== 
 +To get the value you need: 
 +  - Have a curve 
 +  - Have a 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.  
 +{{ :curve:no_dashb.png?direct&600 |}} 
 +Click the "Add dashboard"
 +{{ :curve:dash.png?direct&600 |}} 
 +Type in the Title and go on. 
 +{{ :curve:dash_vis_editor.png?direct&600 |}} 
 +Go to a visual editor. 
 + 
 + 
 +To do this, there is a dashboard element "Curve"
 +{{ :curve:dash_element.png?direct&600 |}} 
 +Drag'n'drop it to create a new one. 
 +{{ :curve:dash_element_created.png?direct&600 |}} 
 + 
 +Pick a register to be used later in Lua script to get value. 
 +{{ :curve:dash_element_reg.png?direct&600 |}} 
 +Let's test it. 
 +Now, you need to put the curve to string register via dashboard. 
 +{{ :curve:dash_element_view_no_select.png?direct&600 |}} 
 +Click and select the one you want to pick. There is a icon for open curve editor. 
 +{{ :curve:dash_element_view_selector.png?direct&600 |}} 
 +There is a picked one. 
 +{{ :curve:dash_element_view_curve_1.png?direct&600 |}} 
 + Let's check the register. 
 +{{ :curve:reg_filled_with_equat.png?direct&600 |}} 
 + 
 +=====Get value from the curve via Lua ===== 
 + 
 +To get the **OUTPUT value** from existing(pre-created) curve, you need:
   - a Lua script to work with string register and    - a Lua script to work with string register and 
   - include a Lua lib, which will provide access to that function.   - include a Lua lib, which will provide access to that function.
Line 36: Line 98:
   - Call a GetCurveValue function and pass to it this ''**register**'' and INPUT ''**x**''   - Call a GetCurveValue function and pass to it this ''**register**'' and INPUT ''**x**''
  
 +<code lua - curves.lib>
 +--[[
 +The function does curve conversion f(inputValue) = outputValue
 +Example of a curve, having multiple ranges of kx + b, set in a register of a String type with Json 
 +[{"k":4.167,"b":0,"range":[0,3]},{"k":0,"b":12.5,"range":[3,27]},{"k":-4.167,"b":125,"range":[27,30]}]
 +--]]
 +function getCurveValue (curveReg, inputValue)
  
- +    local s, e = 12                           -- start & end indexes of the subranges  
-<code lua - curves.lib> +    local tab = cjson.decodeR(curveReg) 
-function GetCurveValue ( curve_registerx_to_find_y +    local outputValuestickOutflag
-    --[[ +
-     +
-    --Curve handler  +
-    INPUT: +
-        Put curve register as first argument and X coordinate as second. +
-    OUTPUT: +
-        Get as result status if it is inside curve range of outside(falseand the Y value as second output argument +
-     +
-    EXAMPLE: +
-        curve_statusvalue = GetCurveValue( "curve_for_current_hour", current_hour )+
          
-    --]] +    for _, sub_ in ipairs(tab) do  
-    table = cjson.decode( R ( curve_register ) ) +        if ( (sub_.range[s] <= inputValue) and (inputValue sub_.range[e]) then  
-    -- inRangeCurve( table, x_to_find_y ) +            return (inputValue sub_.k + sub_.b), nil                             -- normal sequence 
-    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.1596113417.txt.gz · Last modified: 2020/07/30 12:50 by atolstov

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki