News:

  • October 13, 2025, 07:23:50 PM

Login with username, password and session length

Author Topic: Filtering and rounding FLOATING numbers  (Read 2629 times)

JoshChapman

  • Newbie
  • *
  • Posts: 5
Filtering and rounding FLOATING numbers
« on: January 14, 2021, 03:35:21 PM »
Hello, newbie to PLC programming here.
I'm receiving a dimension from a laser triangulation device into my BRX (BX-DM1E-18ED23-D) as an analog value. I am outputting it to a Cmore HMI as a dimension. The Cmore is set to display 4 decimal places and 1 whole number place, like this: 1.2345. All this works reasonably well but because the analog signal is just a little unstable it makes my 4th place after the decimal bounce up and down a lot. This gets quite annoying.
I added a First Order Filter with the time constant at .1 and the bouncing almost disappears, but then it takes a longer time to settle the dimension when I put a part under my laser sensor. I changed the time constant to .03 and the setting time is acceptable but the 4th place digit still moves more than I want it to.
Is there a better way to get good data output?
I think leaving the filter and then rounding the number off to .0002 tolerance would probably make my output pretty clean. But I can't find a good way to round off like that. How is that supposed to be done?

Controls Guy

  • Internal Dev
  • Hero Member
  • ****
  • Posts: 3601
  • Darth Ladder
Re: Filtering and rounding FLOATING numbers
« Reply #1 on: January 14, 2021, 04:06:16 PM »
Multiply by 5000, place in an integer, multiply again by 2, and have the HMI use the number in implied decimal mode.   HMI displays 1.2345 if the register value is 12345.  That could be the sole storage location unless you're also using the value in logic.   In that case, keep the FP value and add the integer as a second representation.
I retract my earlier statement that half of all politicians are crooks.  Half of all politicians are NOT crooks.  There.

BobO

  • Host Moderator
  • Hero Member
  • *****
  • Posts: 6126
  • Yes Pinky, Do-more will control the world!
Re: Filtering and rounding FLOATING numbers
« Reply #2 on: January 15, 2021, 11:18:48 AM »
To get to a minimized visual jitter, you'll have to use combination of bucketing and DEADBAND.

Filtering slows the rate of change, but not frequency of update. It will actually increase frequency of update.

Bucketing via multiply/round/divide gets you part of the way there, but will still jitter around the edges of the buckets.

Bucket plus a deadband slightly larger than the bucket eliminates the jitter.

In the attached example, I have a temperature value updating at about 5Hz (WHITE). I filter at about 2 seconds (RED), then bucket at 0.1 degrees (GREEN) and then deadband at 0.2 degrees (CYAN).

« Last Edit: January 15, 2021, 12:57:21 PM by BobO »
"It has recently come to our attention that users spend 95% of their time using 5% of the available features. That might be relevant." -BobO

Bolt

  • Hero Member
  • *****
  • Posts: 591
Re: Filtering and rounding FLOATING numbers
« Reply #3 on: January 15, 2021, 11:38:32 AM »
In the attached example, I have a temperature value updating at about 2Hz (WHITE). I filter at about 2 seconds (RED), then bucket at 0.1 degrees (GREEN) and then deadband at 0.2 degrees (CYAN).

Neat.  Care to share the code for that?

BobO

  • Host Moderator
  • Hero Member
  • *****
  • Posts: 6126
  • Yes Pinky, Do-more will control the world!
Re: Filtering and rounding FLOATING numbers
« Reply #4 on: January 15, 2021, 11:41:24 AM »
Neat.  Care to share the code for that?

"It has recently come to our attention that users spend 95% of their time using 5% of the available features. That might be relevant." -BobO

BobO

  • Host Moderator
  • Hero Member
  • *****
  • Posts: 6126
  • Yes Pinky, Do-more will control the world!
Re: Filtering and rounding FLOATING numbers
« Reply #5 on: January 15, 2021, 11:43:45 AM »
And upon further reflection, the TOINT() is redundant.
"It has recently come to our attention that users spend 95% of their time using 5% of the available features. That might be relevant." -BobO

Controls Guy

  • Internal Dev
  • Hero Member
  • ****
  • Posts: 3601
  • Darth Ladder
Re: Filtering and rounding FLOATING numbers
« Reply #6 on: January 15, 2021, 12:48:34 PM »
To get to a minimized visual jitter, you'll have to use combination of bucketing and DEADBAND.

Nice!    8)
I retract my earlier statement that half of all politicians are crooks.  Half of all politicians are NOT crooks.  There.

JoshChapman

  • Newbie
  • *
  • Posts: 5
Re: Filtering and rounding FLOATING numbers
« Reply #7 on: January 15, 2021, 02:30:52 PM »
Thanks.
BOBOs solution looks pretty good.

This is what we did which at first is quite like Bobo's solution. But then we deviated to round off the last digit (using the greater than/less than inputs) then added the last digit back in as either a 0 or 5.
Would the deadband method be better or is it just different?

BobO

  • Host Moderator
  • Hero Member
  • *****
  • Posts: 6126
  • Yes Pinky, Do-more will control the world!
Re: Filtering and rounding FLOATING numbers
« Reply #8 on: January 15, 2021, 02:37:22 PM »
Thanks.
BOBOs solution looks pretty good.

This is what we did which at first is quite like Bobo's solution. But then we deviated to round off the last digit (using the greater than/less than inputs) then added the last digit back in as either a 0 or 5.
Would the deadband method be better or is it just different?

Reducing precision reduces jitter while in the center of a bucket, but it doesn't reduce jitter at the bucket boundary. A deadband reduces jitter everywhere.
"It has recently come to our attention that users spend 95% of their time using 5% of the available features. That might be relevant." -BobO