arendst / Tasmota

Alternative firmware for ESP8266 and ESP32 based devices with easy configuration using webUI, OTA updates, automation using timers or rules, expandability and entirely local control over MQTT, HTTP, Serial or KNX. Full documentation at
https://tasmota.github.io/docs
GNU General Public License v3.0
21.98k stars 4.77k forks source link

Moving average (M:vname) for Scripting gives inadequate results #12528

Closed nikvostro closed 3 years ago

nikvostro commented 3 years ago

PROBLEM DESCRIPTION

Moving average (M:vname) for Scripting gives inadequate results. This actual at least for 2 independand builds (env): TTGO and EPS32CAM. Tested on master 9.5.0 and development on 01 Jul 2021 branches.

REQUESTED INFORMATION

TO REPRODUCE

  1. Input and enable following script and look at ave variable in Console:
    
    >D
    rand=0
    M:ave=0

S rand=rnd(100) ave=rand print %rand% - %ave%

  1. Specify the length of filter M:ave=0 to value 4, 6, 12 and other. Save. M:ave=0 4 or M:ave=0 6 or M:ave=0 1
  2. Look now at ave value - it will become inadequate.
  3. You can also try rebooting device with restart 1 or manually -> values for moving average will remain inadequate

EXPECTED BEHAVIOUR

Moving average should be calculated properly

SCREENSHOTS

image image

ADDITIONAL CONTEXT

I'm not a proffessional coder but it looks like an error with some pointer variables that are referencing to the moving average data array.

user_config_override.h:

#ifdef USE_RULES
#undef USE_RULES
#endif

#ifndef USE_SCRIPT
#define USE_SCRIPT
#endif

#define USE_SPI
#define USE_DISPLAY
#define USE_DISPLAY_ST7789
#define SHOW_SPLASH

(Please, remember to close the issue when the problem has been addressed)

gemu2015 commented 3 years ago

i checked your script with an older version and it worked. i then tried with most recent dev version and it fails like yours. however the bug is not in moving average but the print function somehow fails. if you spilt the print into 2 lines it works. print %rand% print movav = %ave%

since nothing was changed in the scripter printing function it must be a side effect of other changes in Tasmota. i am currently extremely busy with other things and have no time to check what causes this bug in the printing function. will check this if i have more time again.

nikvostro commented 3 years ago

Hello @gemu2015 !

I can't agree with you regarding supposed issue with printing function, because:

Nothing changed for me if I splitting print into two rows: image image

Declared length 12: image image

ESP32CAM with added HTML rows like in examle (docs/Scripting - ESP32 Webcam support): image image

gemu2015 commented 3 years ago

ok, recently memory allocation was changed from calloc to malloc. this caused an issue with moving average vars not set to zero before start. this should be fixed now. please test by replacing scripter source file.

xdrv_10_scripter.ino.zip

nikvostro commented 3 years ago

(I've replaced LOG_LEVEL with DEBUG to hide extra info intersecting with my "print") Now looks much better. I've tried to change legth -> result seems to adequeate now in all cases. I consider issue as resolved now :) Thanks a lot!

image image

nikvostro commented 3 years ago

By the way - abs() function is under USE_ANGLE_FUNC def as well as geometric functions. I think it's more logical to move it out OR modify the Docs by adding notice in brackets: image

gemu2015 commented 3 years ago

this was actually a mistake. will move abs() to standard functions.