Trigonometric Functions
Moderators: grovkillen, Stuntteam, TD-er
Trigonometric Functions
hi Guys found this in the doku....
https://espeasy.readthedocs.io/en/lates ... Rules.html
Has any one tried to calculate the high of a liquid an lying zylinder with this function, i i am note sure they are allready compiled with the standard 8266 images!?
there are some question on it like this...
viewtopic.php?f=4&t=5957&p=37402&hilit= ... der#p32494
the formular for the calc. were this :
Formula: V = r²*l*(arccos(r-h/r) - (r-h)*√(2*r*h - h²)/r²)
many thanks.
rira
-----------------------------------------------
Trigonometric Functions
Since the trigonometric functions add quite a bit to the compiled binary, these functions are not included in builds which have a flag defined to limit their build size.
All trigonometric functions are present in 2 versions, for angles in radian and with the _d suffix for angles in degree.
Radian Angle:
sin(x) Sine of x (radian)
cos(x) Cosine of x (radian)
tan(x) Tangent of x (radian)
arcSin(x) Arc Sine of x (radian)
arcCos(x) Arc Cosine of x (radian)
arcTan(x) Arc Tangent of x (radian)
Degree Angle:
sin_d(x) Sine of x (degree)
cos_d(x) Cosine of x (degree)
tan_d(x) Tangent of x (degree)
arcSin_d(x) Arc Sine of x (degree)
arcCos_d(x) Arc Cosine of x (degree)
arcTan_d(x) Arc Tangent of x (degree)
https://espeasy.readthedocs.io/en/lates ... Rules.html
Has any one tried to calculate the high of a liquid an lying zylinder with this function, i i am note sure they are allready compiled with the standard 8266 images!?
there are some question on it like this...
viewtopic.php?f=4&t=5957&p=37402&hilit= ... der#p32494
the formular for the calc. were this :
Formula: V = r²*l*(arccos(r-h/r) - (r-h)*√(2*r*h - h²)/r²)
many thanks.
rira
-----------------------------------------------
Trigonometric Functions
Since the trigonometric functions add quite a bit to the compiled binary, these functions are not included in builds which have a flag defined to limit their build size.
All trigonometric functions are present in 2 versions, for angles in radian and with the _d suffix for angles in degree.
Radian Angle:
sin(x) Sine of x (radian)
cos(x) Cosine of x (radian)
tan(x) Tangent of x (radian)
arcSin(x) Arc Sine of x (radian)
arcCos(x) Arc Cosine of x (radian)
arcTan(x) Arc Tangent of x (radian)
Degree Angle:
sin_d(x) Sine of x (degree)
cos_d(x) Cosine of x (degree)
tan_d(x) Tangent of x (degree)
arcSin_d(x) Arc Sine of x (degree)
arcCos_d(x) Arc Cosine of x (degree)
arcTan_d(x) Arc Tangent of x (degree)
Re: Trigonometric Functions
I don't see why this can't be computed.
However, I do expect you need to compute this in parts, storing intermediate results in variables using the "let" command.
These functions should be included in the "Normal" and "Custom" builds.
However, I do expect you need to compute this in parts, storing intermediate results in variables using the "let" command.
These functions should be included in the "Normal" and "Custom" builds.
Re: Trigonometric Functions
Hi
Any Idea?
Rule (test)
on Distance#Analog do
TaskValueSet 4,1,arcCos(0,7071068)
endon
Log:
816018: ACT : let,10,1000
816022: ACT : let,11,1000
816027: ACT : TaskValueSet 4,1,arcCos(0,7071068)
816030: Too many arguments: cmd=TaskValueSet Arg1=4 Arg2=1 Arg3=arcCos(0 ExtraArg4=7071068) lineLength=34
816031: Line: _TaskValueSet 4,1,arcCos(0,7071068)_
816032: Command not executed! See: https://github.com/letscontrolit/ESPEasy/issues/2724
816035: Command unknown: TaskValueSet 4,1,arcCos(0,7071068)
tried it without and with quotes....
tia
RIRA
Any Idea?
Rule (test)
on Distance#Analog do
TaskValueSet 4,1,arcCos(0,7071068)
endon
Log:
816018: ACT : let,10,1000
816022: ACT : let,11,1000
816027: ACT : TaskValueSet 4,1,arcCos(0,7071068)
816030: Too many arguments: cmd=TaskValueSet Arg1=4 Arg2=1 Arg3=arcCos(0 ExtraArg4=7071068) lineLength=34
816031: Line: _TaskValueSet 4,1,arcCos(0,7071068)_
816032: Command not executed! See: https://github.com/letscontrolit/ESPEasy/issues/2724
816035: Command unknown: TaskValueSet 4,1,arcCos(0,7071068)
tried it without and with quotes....
tia
RIRA
Re: Trigonometric Functions
Maybe with a decimal-point (.) at the arcCos-parameter ?
Re: Trigonometric Functions
not better with the . point....
1835142: EVENT: Distance#Analog=663.45
1835148: ACT : TaskValueSet 4,1,arcCos(0.7071068)
1835152: Calculate: Unknown token input: arcCos(0.7071068) = 0
rira
1835142: EVENT: Distance#Analog=663.45
1835148: ACT : TaskValueSet 4,1,arcCos(0.7071068)
1835152: Calculate: Unknown token input: arcCos(0.7071068) = 0
rira
Re: Trigonometric Functions
Test this:
Edit: It doesn't work for me either...
Code: Select all
on Distance#Analog do
Let,1,arcCos(0.7071068)
TaskValueSet,4,1,[VAR#1]
endon
Re: Trigonometric Functions
Me too....
4835162: ACT : Let,1,arcCos(0.7071068)
4835165: Calculate: Unknown token input: arcCos(0.7071068) = 0
4835168: ACT : TaskValueSet,4,1,0
...Sad...
Rira
4835162: ACT : Let,1,arcCos(0.7071068)
4835165: Calculate: Unknown token input: arcCos(0.7071068) = 0
4835168: ACT : TaskValueSet,4,1,0
...Sad...
Rira
Re: Trigonometric Functions
Same with INT like arcCos(1) !
Re: Trigonometric Functions
@rira2005: Have you tested it with a firmware 4M or greater? Maybe in my 1MB ESP the trigonometric functions are not available (limit_build_size) ?!
Re: Trigonometric Functions
That function is actually called acos, though it's name is written all over the source as ArcCos, similar for asin (ArcSin) and atan (ArcTan)
/Ton (PayPal.me)
Re: Trigonometric Functions
That would log a message "USE_TRIGONOMETRIC_FUNCTIONS_RULES not defined in build"
/Ton (PayPal.me)
Re: Trigonometric Functions
Thanks for the explanation, but all trigonometric functions doesn't work, incl. 'acos'. All with same error 'Calculate: Unknown token'.
Re: Trigonometric Functions
As TD-er already wrote, you will need to use a Custom or Normal build to have the trigonometric functions included. In any other build even the log message that it is not available is not in the build... resulting in the generic "Calculate: unknown token input" error message.
Besides that, there's also a mismatch in the documentation, as the function names are as I wrote above, not using 'arc' as a prefix, but just an 'a' like 'acos', 'asin' and 'atan', with the optional '_b' suffix for the degrees, instead of radians, variants.
Besides that, there's also a mismatch in the documentation, as the function names are as I wrote above, not using 'arc' as a prefix, but just an 'a' like 'acos', 'asin' and 'atan', with the optional '_b' suffix for the degrees, instead of radians, variants.
/Ton (PayPal.me)
Re: Trigonometric Functions
But my test were performed with a normal 1M build !?
Re: Trigonometric Functions
Ah, yes, the 1M builds also have that LIMIT_BUILD_SIZE setting, so they exclude these functions too.
/Ton (PayPal.me)
Re: Trigonometric Functions
Maybe we need a info in the documentation, with commands are in the different builds or a better error-message, when a command is not available.
Re: Trigonometric Functions
That info is a bit fluent, so quite hard to document.
I'm checking if it's possible to keep these functions in all 'normal' builds, also the 1M ones, and it is looking quite good so far.
I'm checking if it's possible to keep these functions in all 'normal' builds, also the 1M ones, and it is looking quite good so far.
/Ton (PayPal.me)
Re: Trigonometric Functions
Opened a PR to fix these issues here: https://github.com/letscontrolit/ESPEasy/pull/3681
/Ton (PayPal.me)
Re: Trigonometric Functions
this is my working solution....
-------------------------------------------------------------
on Distance#Analog do
let 14,80 //radius
let 15,200 //lengh
let 16,[Distance#Analog] //high
if [Distance#Analog]<=160 //diameter (2xradius)
let 1,2*%v14%*%v16%-%v16%^2
//logentry,%v1%
let 2,sqrt(%v1%)
//logentry,%v2%
let 3,%v2%/(%v14%*%v14%)
//logentry,%v3%
let 4,((%v14%-%v16%)/%v14%)
//logentry,%v4%
let 5,acos(%v4%)
//logentry,%v5%
let 6,(%v14%-%v16%)*%v3%
//logentry,%v6%
let 7,(%v5%-%v6%)
//logentry,%v7%
let 8,(%v14%*%v14%)*%v15%
//logentry,%v8%
let 9,(%v5%-%v6%)*%v8%
//logentry,%v9%
let 10,%v9%/1000
//logentry,%v10%
TaskValueSet,4,1,%v10%
Publish domoticz/in,'{"idx":157,"nvalue":0,"svalue":"[dummy#liter]"}'
else
Publish domoticz/in,'{"idx":157,"nvalue":0,"svalue":"Error"}'
endif
endon
---------------------------------------------------------------------------------------------------
thx for your help.
rira
-------------------------------------------------------------
on Distance#Analog do
let 14,80 //radius
let 15,200 //lengh
let 16,[Distance#Analog] //high
if [Distance#Analog]<=160 //diameter (2xradius)
let 1,2*%v14%*%v16%-%v16%^2
//logentry,%v1%
let 2,sqrt(%v1%)
//logentry,%v2%
let 3,%v2%/(%v14%*%v14%)
//logentry,%v3%
let 4,((%v14%-%v16%)/%v14%)
//logentry,%v4%
let 5,acos(%v4%)
//logentry,%v5%
let 6,(%v14%-%v16%)*%v3%
//logentry,%v6%
let 7,(%v5%-%v6%)
//logentry,%v7%
let 8,(%v14%*%v14%)*%v15%
//logentry,%v8%
let 9,(%v5%-%v6%)*%v8%
//logentry,%v9%
let 10,%v9%/1000
//logentry,%v10%
TaskValueSet,4,1,%v10%
Publish domoticz/in,'{"idx":157,"nvalue":0,"svalue":"[dummy#liter]"}'
else
Publish domoticz/in,'{"idx":157,"nvalue":0,"svalue":"Error"}'
endif
endon
---------------------------------------------------------------------------------------------------
thx for your help.
rira
Re: Trigonometric Functions
Improvement suggestion:
Code: Select all
on Distance#Analog do
let 14,80 //radius
let 15,200 //lengh
let 16,%eventvalue1% //high <-- changed
if %v16%<=160 //diameter (2xradius) <-- changed
let 1,2*%v14%*%v16%-%v16%^2
/Ton (PayPal.me)
Re: Trigonometric Functions
Yesterday I've done a test with my 'normal WROOM2 2M256' device, with the last firmware from 20220809.
The command 'acos' seems not include there. Is the '2M256' a LIMIT_BUILD_SIZE firmware too?
The command 'acos' seems not include there. Is the '2M256' a LIMIT_BUILD_SIZE firmware too?
Re: Trigonometric Functions
Made a quick scan through the source code.
The only thing I can think of is that either "PLUGIN_BUILD_NORMAL" isn't set for that build, or for whatever reason "ESP8266_1M" is defined for that WROOM2 2M256 build.
This is the board definition used (not present in our boards folder): https://github.com/platformio/platform- ... om_02.json
So maybe this should be copied there and add these extra flags: -DESP8266_2M -DESP8266_2M256
Also the "variant" variable differs from "generic" to "nodemcu" No idea what difference that makes.
TL;DR No idea yet why it shouldn't be included...
Have to check later, thus so far these are my notes to continue later.
The only thing I can think of is that either "PLUGIN_BUILD_NORMAL" isn't set for that build, or for whatever reason "ESP8266_1M" is defined for that WROOM2 2M256 build.
This is the board definition used (not present in our boards folder): https://github.com/platformio/platform- ... om_02.json
So maybe this should be copied there and add these extra flags: -DESP8266_2M -DESP8266_2M256
Also the "variant" variable differs from "generic" to "nodemcu" No idea what difference that makes.
TL;DR No idea yet why it shouldn't be included...
Have to check later, thus so far these are my notes to continue later.
Re: Trigonometric Functions
Ok, thanks. I'll check again this afternoon, that I've tested all correctly yesterday.
I'll test with rules and not only via web-command.
Yesterday evening I've problems with a rule like this, on another 4M1M-device:
It didn't work! But it was late in the evening...
I'll test with rules and not only via web-command.
Yesterday evening I've problems with a rule like this, on another 4M1M-device:
Code: Select all
on Test do
Let,20,acos(%eventvalue%)
endon
Re: Trigonometric Functions
What exact build did you run on that 4M node?
Was it a collection build? (those have LIMIT_BUILD_SIZE defined)
Was it a collection build? (those have LIMIT_BUILD_SIZE defined)
Re: Trigonometric Functions
No, a 'normal_ESP8266_4M1M'. But possibly I did something wrong.
It only work, when I use a %var% to save the %eventvalue%. If I use the %eventvalue% direct to the acos-function, the log no action was logged for this event !?!?
Then it was time to go to bed
I'll test it later with a fresh/empty device...
It only work, when I use a %var% to save the %eventvalue%. If I use the %eventvalue% direct to the acos-function, the log no action was logged for this event !?!?
Then it was time to go to bed
I'll test it later with a fresh/empty device...
Re: Trigonometric Functions
OK, there is no %var% so just to be sure we're not looking into build issues where it might be a syntax error on the rules.
Can you post the rules you try to test?
Can you post the rules you try to test?
Re: Trigonometric Functions
No you missunderstood! I mean, I've used any free variable as follows:
New insights, then later.
Code: Select all
on Test do
Let,19,%eventvalue%
Let,20,acos(%var19%)
endon
Re: Trigonometric Functions
The syntax for using internal variables is either %v19%, [var#19] or [int#19] (for a 'rounded to 0 decimals' value)
/Ton (PayPal.me)
Re: Trigonometric Functions
That's my mistake, I wrote it from the road But the version with an additional var works, problems are with the other one.
Re: Trigonometric Functions
How do you call your event?
Just made a quick test on my node here.
The command entered on the tools page:
Rules:
Log results:
Just made a quick test on my node here.
The command entered on the tools page:
Code: Select all
event,test=0.7
Code: Select all
on Test do
Let,19,%eventvalue%
Let,20,acos([var#19])
Let,21,acos(%eventvalue%)
LogEntry,"v19: [var#19] v20: [var#20] v21: [var#21] "
endon
Code: Select all
7247192 : Info : HTTP: event,test=0.7
7247288 : Info : EVENT: test=0.7
7247378 : Info : ACT : Let,19,0.7
7247386 : Info : ACT : Let,20,acos(0.7)
7247391 : Info : ACT : Let,21,acos(0.7)
7247398 : Info : ACT : LogEntry,"v19: 0.7 v20: 0.79539883018414 v21: 0.79539883018414 "
7247401 : Info : v19: 0.7 v20: 0.79539883018414 v21: 0.79539883018414
Re: Trigonometric Functions
Damn, the command is 'event,test=[x]' and not 'event,test,x'.
With the second one the result is OK, but nothing is done. My mistake.
Now I look at the WROOM2_2M256...
With the second one the result is OK, but nothing is done. My mistake.
Now I look at the WROOM2_2M256...
Re: Trigonometric Functions
First: acos() is available at the 2M256. Either I called the event wrong or ...
... write the functions with uppercase characters
because the functions like acos() and sqrt() must be lowercase! Otherwise it leads in an error!
Last Question:
Exists a variable like %pi% or a funtion pi() ? Otherwise I will use 3.1415927 to calc.
... write the functions with uppercase characters
because the functions like acos() and sqrt() must be lowercase! Otherwise it leads in an error!
Last Question:
Exists a variable like %pi% or a funtion pi() ? Otherwise I will use 3.1415927 to calc.
Re: Trigonometric Functions
Nope, there isn't a constant for pi... yet.
Thus so far you must define it yourself.
For example:
Thus so far you must define it yourself.
For example:
Code: Select all
on system#boot do
let,314,atan(1)*4
endon
Re: Trigonometric Functions
Nice idea.
But the uppercase problem is a bug, or?
But the uppercase problem is a bug, or?
Re: Trigonometric Functions
Not sure yet.
All other commands (at least the commands themselves) are parsed case-insensitive.
So considering how others are used, it could be considered a bug.
However, I never would have considered to be using those functions in capitals.
Not sure how simple it is to fix. (without adding a lot of complexity)
All other commands (at least the commands themselves) are parsed case-insensitive.
So considering how others are used, it could be considered a bug.
However, I never would have considered to be using those functions in capitals.
Not sure how simple it is to fix. (without adding a lot of complexity)
Re: Trigonometric Functions
OK, looked into the code....
This is the rules preprocessor function, trying to match these functions and replacing them by a single char token.
I think it might be a bit more tricky then one might expect to make this case insensitive.
This is the rules preprocessor function, trying to match these functions and replacing them by a single char token.
Code: Select all
void preProcessReplace(String& input, UnaryOperator op) {
String find = toString(op);
if (find.isEmpty()) { return; }
find += '('; // Add opening parenthesis.
const String replace = String(static_cast<char>(op)) + '(';
input.replace(find, replace);
}
Re: Trigonometric Functions
Hmm, looked into it a bit, without actually testing it.
This is what I came up with
This is already becoming quite complex, as you need to keep 2 strings in sync:
- The input string
- The input string turned to lower case
Also any optimization like checking if "sin" occurs in the line and if it doesn't you can skip 4 checks at once do get a bit more complex.
If this preprocessing was only done once at load of the file, then it wouldn't really be an issue.
But on ESP8266 we don't keep all rules lines in memory.
Have to think about this a bit more, whether it is worth the extra resources needed to parse.
This is what I came up with
Code: Select all
void preProcessReplace(String& input,
String& input_lc,
UnaryOperator op) {
String find = toString(op);
if (find.isEmpty()) { return; }
find += '('; // Add opening parenthesis.
const String replace = String(static_cast<char>(op)) + '(';
int index_start = input_lc.indexOf(find);
if (index_start == -1) { return; }
int pos = 0;
while (pos < input_lc.length() && index_start != -1) {
// First replace all occurences by their lower case variant
const int index_end = index_start + find.length();
input.replace(input.substring(index_start, index_end), find);
pos = index_end;
index_start = input_lc.indexOf(find, pos);
}
input.replace(find, replace);
input_lc.replace(find, replace);
}
- The input string
- The input string turned to lower case
Also any optimization like checking if "sin" occurs in the line and if it doesn't you can skip 4 checks at once do get a bit more complex.
If this preprocessing was only done once at load of the file, then it wouldn't really be an issue.
But on ESP8266 we don't keep all rules lines in memory.
Have to think about this a bit more, whether it is worth the extra resources needed to parse.
Re: Trigonometric Functions
No problem from my side, on my device it works in lower case. I just thought, it was a bug.
Re: Trigonometric Functions
Moah, let's call it an undocumented code optimization
A bug sounds so negative.
Who is online
Users browsing this forum: No registered users and 1 guest