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 [2021/09/10 14:33] atolstovcurves [2024/06/03 08:45] (current) emozolyak
Line 1: Line 1:
-{{ :menu-icon-curves.png?nolink&60|}}+{{ network:menu-icon-curves.png?nolink&60|}}
 ======Curves====== ======Curves======
 =====Introduction===== =====Introduction=====
Line 99: Line 99:
  
 <code lua - curves.lib> <code lua - curves.lib>
-function GetCurveValue curve_register, x_to_find_y  +--[[ 
-    --[[    +The function does curve conversion f(v= 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, v) 
-        Get as result status if it is inside curve range of outside(falseand the Y value as second output argument+  
 +    local start, end_ = 1, 2                           -- start & end indexes of the subranges  
 +    local tab = cjson.decode(R(curveReg))
          
-    EXAMPLE: +    -- edge cases go first  
-        curve_status, value = GetCurveValue"curve_for_current_hour", current_hour +    if  (v < tab[start].range[start]then  
-    --]] +        return tab[start].range[start* tab[start].k + tab[start].b, true 
-    table = cjson.decodeR ( curve_register ) +    elseif (v > tab[#tab].range[end_]then  
-    -- inRangeCurve( tablex_to_find_y ) +        return tab[#tab].range[end_] * tab[#tab].k + tab[#tab].btrue  
-    for _, value in ipairs( table ) do --piecewise handler inside range of the curve +    end 
-        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, curveLinearCalcx_to_find_y, value.k, value.b ) +    for _, sub_ in ipairs(tab) do  
-            in_range_status, y = true, ( x_to_find_y value.k + value.b ) +        if ( (sub_.range[start] <= v) and (v sub_.range[end_]) then  
-            return in_range_statusy+            return (sub_.k + sub_.b), false                             -- normal sequence 
         end         end
     end     end
-    if  x_to_find_y < table[1].range[1] then -- behaivior for outside left-sided 
-        -- in_range_status, index_curve_piece, y = false, 1, curveLinearCalc( table[1].range[1], table[1].k*0, table[1].b ) --table[1].b 
-        in_range_status, index_curve_piece, y = false, 1, ( table[1].range[1] * table[1].k + table[1].b ) --table[1].b 
-        return in_range_status, y 
-    elseif x_to_find_y > table[#table].range[2] then -- behaivior for outside right-sided 
-        -- in_range_status, index_curve_piece, y = false,#table,curveLinearCalc( table[#table].range[2], table[#table].k, table[#table].b ) --table[#table].b 
-        in_range_status,  y = false,#table,( table[#table].range[2] * table[#table].k + table[#table].b) 
-        return in_range_status, y 
-    end 
- 
-    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" 
-SAFE_VALUE = 10+
  
 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) 
-    if curve_status then +     
-        WriteReg("value_for_current_hour", y)+    if (not valueOutOfRange) then 
 +        W("value_for_curHour", y)  -- WriteReg
     else     else
-        WriteReg("value_for_current_hour", SAFE_VALUE)+        W("value_for_curHour", SAFE_VALUE)
     end     end
-         
- 
 end end
 </code> </code>
curves.1631284432.txt.gz · Last modified: 2021/09/10 14:33 by atolstov

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki