curves
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
curves [2020/07/31 07:30] – [Select a curve to put in register] atolstov | curves [2022/05/24 08:52] – [Get value from the curve via Lua] emozolyak | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | {{ network: | ||
======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, | Curve is using a piecewise linear interpolation, | ||
Line 55: | Line 56: | ||
{{ : | {{ : | ||
=====Select a curve to put in register===== | =====Select a curve to put in register===== | ||
- | < | ||
- | |||
To get the value you need: | To get the value you need: | ||
- Have a curve | - Have a curve | ||
Line 90: | Line 89: | ||
{{ : | {{ : | ||
- | =====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 100: | Line 99: | ||
<code lua - curves.lib> | <code lua - curves.lib> | ||
- | function | + | --[[ |
- | --[[ | + | The function |
- | --Curve handler | + | Example of a curve, having multiple ranges of kx + b, set in a register of a String type with Json |
- | | + | [{" |
- | Put curve register as first argument and X coordinate as second. | + | --]] |
- | | + | function getCurveValue (curveReg, inputValue) |
- | Get as result status if it is inside curve range of outside(false) and the Y value as second output argument | + | |
+ | | ||
+ | local tab = cjson.decode( R(curveReg) ) | ||
+ | local outputValue, | ||
| | ||
- | | + | for _, sub_ in ipairs(tab) do |
- | curve_status, | + | if ( (sub_.range[s] <= inputValue) |
- | --]] | + | |
- | table = cjson.decode( R ( curve_register ) ) | + | |
- | -- inRangeCurve( table, x_to_find_y ) | + | |
- | | + | |
- | if ( value.range[1] <= x_to_find_y | + | |
- | | + | |
- | in_range_status, | + | |
- | return in_range_status, y | + | |
end | end | ||
end | end | ||
- | | + | -- edge cases |
- | -- in_range_status, | + | |
- | | + | |
- | | + | |
- | elseif | + | elseif |
- | | + | |
- | | + | |
- | return in_range_status, | + | |
end | end | ||
- | + | | |
- | | + | |
end | end | ||
</ | </ | ||
Example of usage: | Example of usage: | ||
<code lua - Getter> | <code lua - Getter> | ||
- | include " | + | include " |
function main (userId) | function main (userId) | ||
- | + | local SAFE_VALUE = 10 | |
- | local t = os.date(" | + | local curHour |
- | | + | |
- | | + | |
- | | + | |
+ | if (not valueOutOfRange) then | ||
+ | W("value_for_curHour", y) -- WriteReg | ||
+ | | ||
+ | W(" | ||
+ | end | ||
end | end | ||
</ | </ |
curves.txt · Last modified: 2024/06/03 08:45 by emozolyak