Trigonometric Functions

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
rira2005
Normal user
Posts: 30
Joined: 25 Jun 2017, 21:09

Trigonometric Functions

#1 Post by rira2005 » 04 Jun 2021, 11:35

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)

TD-er
Core team member
Posts: 9266
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Trigonometric Functions

#2 Post by TD-er » 04 Jun 2021, 11:53

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.

rira2005
Normal user
Posts: 30
Joined: 25 Jun 2017, 21:09

Re: Trigonometric Functions

#3 Post by rira2005 » 08 Jun 2021, 11:29

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

Micha_he
Normal user
Posts: 372
Joined: 07 Feb 2018, 19:14
Location: Helmstedt, Germany

Re: Trigonometric Functions

#4 Post by Micha_he » 08 Jun 2021, 12:18

Maybe with a decimal-point (.) at the arcCos-parameter ?

rira2005
Normal user
Posts: 30
Joined: 25 Jun 2017, 21:09

Re: Trigonometric Functions

#5 Post by rira2005 » 08 Jun 2021, 13:01

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

Micha_he
Normal user
Posts: 372
Joined: 07 Feb 2018, 19:14
Location: Helmstedt, Germany

Re: Trigonometric Functions

#6 Post by Micha_he » 08 Jun 2021, 13:08

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...

rira2005
Normal user
Posts: 30
Joined: 25 Jun 2017, 21:09

Re: Trigonometric Functions

#7 Post by rira2005 » 08 Jun 2021, 13:51

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

Micha_he
Normal user
Posts: 372
Joined: 07 Feb 2018, 19:14
Location: Helmstedt, Germany

Re: Trigonometric Functions

#8 Post by Micha_he » 08 Jun 2021, 13:56

Same with INT like arcCos(1) !

Micha_he
Normal user
Posts: 372
Joined: 07 Feb 2018, 19:14
Location: Helmstedt, Germany

Re: Trigonometric Functions

#9 Post by Micha_he » 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) ?!

User avatar
Ath
Normal user
Posts: 3950
Joined: 10 Jun 2018, 12:06
Location: NL

Re: Trigonometric Functions

#10 Post by Ath » 08 Jun 2021, 14:03

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:
/Ton (PayPal.me)

User avatar
Ath
Normal user
Posts: 3950
Joined: 10 Jun 2018, 12:06
Location: NL

Re: Trigonometric Functions

#11 Post by Ath » 08 Jun 2021, 14:05

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"
/Ton (PayPal.me)

Micha_he
Normal user
Posts: 372
Joined: 07 Feb 2018, 19:14
Location: Helmstedt, Germany

Re: Trigonometric Functions

#12 Post by Micha_he » 08 Jun 2021, 14:13

Thanks for the explanation, but all trigonometric functions doesn't work, incl. 'acos'. All with same error 'Calculate: Unknown token'.

User avatar
Ath
Normal user
Posts: 3950
Joined: 10 Jun 2018, 12:06
Location: NL

Re: Trigonometric Functions

#13 Post by Ath » 08 Jun 2021, 20:22

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.
/Ton (PayPal.me)

Micha_he
Normal user
Posts: 372
Joined: 07 Feb 2018, 19:14
Location: Helmstedt, Germany

Re: Trigonometric Functions

#14 Post by Micha_he » 08 Jun 2021, 20:36

But my test were performed with a normal 1M build !?

User avatar
Ath
Normal user
Posts: 3950
Joined: 10 Jun 2018, 12:06
Location: NL

Re: Trigonometric Functions

#15 Post by Ath » 08 Jun 2021, 20:44

Ah, yes, the 1M builds also have that LIMIT_BUILD_SIZE setting, so they exclude these functions too.
/Ton (PayPal.me)

Micha_he
Normal user
Posts: 372
Joined: 07 Feb 2018, 19:14
Location: Helmstedt, Germany

Re: Trigonometric Functions

#16 Post by Micha_he » 08 Jun 2021, 21:14

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.

User avatar
Ath
Normal user
Posts: 3950
Joined: 10 Jun 2018, 12:06
Location: NL

Re: Trigonometric Functions

#17 Post by Ath » 08 Jun 2021, 21:26

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.
/Ton (PayPal.me)

User avatar
Ath
Normal user
Posts: 3950
Joined: 10 Jun 2018, 12:06
Location: NL

Re: Trigonometric Functions

#18 Post by Ath » 08 Jun 2021, 21:38

Opened a PR to fix these issues here: https://github.com/letscontrolit/ESPEasy/pull/3681
/Ton (PayPal.me)

rira2005
Normal user
Posts: 30
Joined: 25 Jun 2017, 21:09

Re: Trigonometric Functions

#19 Post by rira2005 » 15 Jun 2021, 13:55

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

User avatar
Ath
Normal user
Posts: 3950
Joined: 10 Jun 2018, 12:06
Location: NL

Re: Trigonometric Functions

#20 Post by Ath » 15 Jun 2021, 15:00

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.
/Ton (PayPal.me)

Micha_he
Normal user
Posts: 372
Joined: 07 Feb 2018, 19:14
Location: Helmstedt, Germany

Re: Trigonometric Functions

#21 Post by Micha_he » 20 Oct 2022, 11:47

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?

TD-er
Core team member
Posts: 9266
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Trigonometric Functions

#22 Post by TD-er » 20 Oct 2022, 13:18

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.

Micha_he
Normal user
Posts: 372
Joined: 07 Feb 2018, 19:14
Location: Helmstedt, Germany

Re: Trigonometric Functions

#23 Post by Micha_he » 20 Oct 2022, 13:45

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...

TD-er
Core team member
Posts: 9266
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Trigonometric Functions

#24 Post by TD-er » 20 Oct 2022, 13:53

What exact build did you run on that 4M node?
Was it a collection build? (those have LIMIT_BUILD_SIZE defined)

Micha_he
Normal user
Posts: 372
Joined: 07 Feb 2018, 19:14
Location: Helmstedt, Germany

Re: Trigonometric Functions

#25 Post by Micha_he » 20 Oct 2022, 14:06

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...

TD-er
Core team member
Posts: 9266
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Trigonometric Functions

#26 Post by TD-er » 20 Oct 2022, 14:12

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?

Micha_he
Normal user
Posts: 372
Joined: 07 Feb 2018, 19:14
Location: Helmstedt, Germany

Re: Trigonometric Functions

#27 Post by Micha_he » 20 Oct 2022, 14:17

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.

User avatar
Ath
Normal user
Posts: 3950
Joined: 10 Jun 2018, 12:06
Location: NL

Re: Trigonometric Functions

#28 Post by Ath » 20 Oct 2022, 14:33

The syntax for using internal variables is either %v19%, [var#19] or [int#19] (for a 'rounded to 0 decimals' value)
/Ton (PayPal.me)

Micha_he
Normal user
Posts: 372
Joined: 07 Feb 2018, 19:14
Location: Helmstedt, Germany

Re: Trigonometric Functions

#29 Post by Micha_he » 20 Oct 2022, 14:45

That's my mistake, I wrote it from the road :? But the version with an additional var works, problems are with the other one.

TD-er
Core team member
Posts: 9266
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Trigonometric Functions

#30 Post by TD-er » 20 Oct 2022, 16:13

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

Micha_he
Normal user
Posts: 372
Joined: 07 Feb 2018, 19:14
Location: Helmstedt, Germany

Re: Trigonometric Functions

#31 Post by Micha_he » 20 Oct 2022, 17:26

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...

Micha_he
Normal user
Posts: 372
Joined: 07 Feb 2018, 19:14
Location: Helmstedt, Germany

Re: Trigonometric Functions

#32 Post by Micha_he » 20 Oct 2022, 18:40

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.

TD-er
Core team member
Posts: 9266
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Trigonometric Functions

#33 Post by TD-er » 20 Oct 2022, 19:04

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

Micha_he
Normal user
Posts: 372
Joined: 07 Feb 2018, 19:14
Location: Helmstedt, Germany

Re: Trigonometric Functions

#34 Post by Micha_he » 20 Oct 2022, 19:16

Nice idea.

But the uppercase problem is a bug, or?

TD-er
Core team member
Posts: 9266
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Trigonometric Functions

#35 Post by TD-er » 20 Oct 2022, 19:30

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)

TD-er
Core team member
Posts: 9266
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Trigonometric Functions

#36 Post by TD-er » 20 Oct 2022, 19:41

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.

TD-er
Core team member
Posts: 9266
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Trigonometric Functions

#37 Post by TD-er » 20 Oct 2022, 20:29

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.

Micha_he
Normal user
Posts: 372
Joined: 07 Feb 2018, 19:14
Location: Helmstedt, Germany

Re: Trigonometric Functions

#38 Post by Micha_he » 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.

TD-er
Core team member
Posts: 9266
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Trigonometric Functions

#39 Post by TD-er » 20 Oct 2022, 22:45

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.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest