Page 1 of 1

Trigonometric Functions

Posted: 04 Jun 2021, 11:35
by rira2005
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)

Re: Trigonometric Functions

Posted: 04 Jun 2021, 11:53
by TD-er
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.

Re: Trigonometric Functions

Posted: 08 Jun 2021, 11:29
by rira2005
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

Re: Trigonometric Functions

Posted: 08 Jun 2021, 12:18
by Micha_he
Maybe with a decimal-point (.) at the arcCos-parameter ?

Re: Trigonometric Functions

Posted: 08 Jun 2021, 13:01
by rira2005
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

Re: Trigonometric Functions

Posted: 08 Jun 2021, 13:08
by Micha_he
Test this:

Code: Select all

on Distance#Analog do
  Let,1,arcCos(0.7071068)
  TaskValueSet,4,1,[VAR#1]
endon
Edit: It doesn't work for me either...

Re: Trigonometric Functions

Posted: 08 Jun 2021, 13:51
by rira2005
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

Re: Trigonometric Functions

Posted: 08 Jun 2021, 13:56
by Micha_he
Same with INT like arcCos(1) !

Re: Trigonometric Functions

Posted: 08 Jun 2021, 14:01
by Micha_he
@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

Posted: 08 Jun 2021, 14:03
by Ath
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) :shock:

Re: Trigonometric Functions

Posted: 08 Jun 2021, 14:05
by Ath
Micha_he wrote: 08 Jun 2021, 14:01 @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) ?!
That would log a message "USE_TRIGONOMETRIC_FUNCTIONS_RULES not defined in build"

Re: Trigonometric Functions

Posted: 08 Jun 2021, 14:13
by Micha_he
Thanks for the explanation, but all trigonometric functions doesn't work, incl. 'acos'. All with same error 'Calculate: Unknown token'.

Re: Trigonometric Functions

Posted: 08 Jun 2021, 20:22
by Ath
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.

Re: Trigonometric Functions

Posted: 08 Jun 2021, 20:36
by Micha_he
But my test were performed with a normal 1M build !?

Re: Trigonometric Functions

Posted: 08 Jun 2021, 20:44
by Ath
Ah, yes, the 1M builds also have that LIMIT_BUILD_SIZE setting, so they exclude these functions too.

Re: Trigonometric Functions

Posted: 08 Jun 2021, 21:14
by Micha_he
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

Posted: 08 Jun 2021, 21:26
by Ath
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.

Re: Trigonometric Functions

Posted: 08 Jun 2021, 21:38
by Ath
Opened a PR to fix these issues here: https://github.com/letscontrolit/ESPEasy/pull/3681

Re: Trigonometric Functions

Posted: 15 Jun 2021, 13:55
by rira2005
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

Re: Trigonometric Functions

Posted: 15 Jun 2021, 15:00
by Ath
rira2005 wrote: 15 Jun 2021, 13:55 this is my working solution....
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
This way the measured value at the moment the event was triggered will be used, not the value at the moment the rule is processed (which can be delayed for several reasons). Any update in value will trigger a new event, so that will be processed in due course.

Re: Trigonometric Functions

Posted: 20 Oct 2022, 11:47
by Micha_he
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?

Re: Trigonometric Functions

Posted: 20 Oct 2022, 13:18
by TD-er
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.

Re: Trigonometric Functions

Posted: 20 Oct 2022, 13:45
by Micha_he
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:

Code: Select all

on Test do
  Let,20,acos(%eventvalue%)
endon
It didn't work! But it was late in the evening...

Re: Trigonometric Functions

Posted: 20 Oct 2022, 13:53
by TD-er
What exact build did you run on that 4M node?
Was it a collection build? (those have LIMIT_BUILD_SIZE defined)

Re: Trigonometric Functions

Posted: 20 Oct 2022, 14:06
by Micha_he
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...

Re: Trigonometric Functions

Posted: 20 Oct 2022, 14:12
by TD-er
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?

Re: Trigonometric Functions

Posted: 20 Oct 2022, 14:17
by Micha_he
No you missunderstood! I mean, I've used any free variable as follows:

Code: Select all

on Test do
  Let,19,%eventvalue%
  Let,20,acos(%var19%)
endon
New insights, then later.

Re: Trigonometric Functions

Posted: 20 Oct 2022, 14:33
by Ath
The syntax for using internal variables is either %v19%, [var#19] or [int#19] (for a 'rounded to 0 decimals' value)

Re: Trigonometric Functions

Posted: 20 Oct 2022, 14:45
by Micha_he
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

Posted: 20 Oct 2022, 16:13
by TD-er
How do you call your event?

Just made a quick test on my node here.

The command entered on the tools page:

Code: Select all

event,test=0.7
Rules:

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
Log results:

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

Posted: 20 Oct 2022, 17:26
by Micha_he
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...

Re: Trigonometric Functions

Posted: 20 Oct 2022, 18:40
by Micha_he
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.

Re: Trigonometric Functions

Posted: 20 Oct 2022, 19:04
by TD-er
Nope, there isn't a constant for pi... yet.
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

Posted: 20 Oct 2022, 19:16
by Micha_he
Nice idea.

But the uppercase problem is a bug, or?

Re: Trigonometric Functions

Posted: 20 Oct 2022, 19:30
by TD-er
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)

Re: Trigonometric Functions

Posted: 20 Oct 2022, 19:41
by TD-er
OK, looked into the code....
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);
}
I think it might be a bit more tricky then one might expect to make this case insensitive.

Re: Trigonometric Functions

Posted: 20 Oct 2022, 20:29
by TD-er
Hmm, looked into it a bit, without actually testing it.
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);
}
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.

Re: Trigonometric Functions

Posted: 20 Oct 2022, 20:48
by Micha_he
No problem from my side, on my device it works in lower case. I just thought, it was a bug.

Re: Trigonometric Functions

Posted: 20 Oct 2022, 22:45
by TD-er
Micha_he wrote: 20 Oct 2022, 20:48 No problem from my side, on my device it works in lower case. I just thought, it was a bug.
Moah, let's call it an undocumented code optimization ;)
A bug sounds so negative.