Approximately in code

how can you tell the control hub to do something when a sensor reading (a number) is approximatly around the needed number.
Thanks all

Greetings, @AdamNguyen!

This is an incredibly common software pattern, and it’s traditionally handled in one of two ways.

  1. Checking within a single bounded range (as is traditionally done with REV Distance Sensor values).
  2. Checking within multiple bounded ranges (as is traditionally done with REV Color Sensor values).

Single Bounded Range

A single bounded range is managed by using logic structures to see if a value is between a minimum AND a maximum value, so you define the acceptable minimum and maximum value that you’re willing to accept and the logic structure will accept the value if it’s between the minimum and maximum values.

This requires several blocks:

  • LOGICAL OPERATOR block (Found in LOGIC drawer, I like to right-click the block and select “External Inputs” to put the inputs on top of each other instead of right/left)
    image
    image <== after selecting “External Inputs” from right-click menu

  • COMPARISON block (found in LOGIC drawer, drop-down allows selecting other comparisons)
    image
    image <== Possible Options

  • NUMBER block (found in MATH drawer)
    image

Using these blocks, you can create a conditional statement like:

image

Multiple Bounded non-overlapping ranges

There’s also a trick used if you want to do different things based on different possible ranges, and this trick involves using if/elseif logic. This trick is used a lot when using sensors like the REV Color Sensor where different color sensor ranges represent different colors. The idea is the same as the simple range min/max check, but the logic ends up being a lot simpler thanks to the “only one of these executes” property of the if/elseif structure (if you have questions about how this works just ask for me to explain!).

Let’s say you use the Control Hub’s battery voltage sensor and you want to do different things based on the value of the voltage of the battery. You would likely do something like:

image

Using in a program

I created an example of how to use both of these ranged checks, each in its own function (creating functions is fun - if you want to learn how just ask!).

-Danny

Here’s another approach our team uses frequently in code for approximate comparison, especially when we want to find if some value we have is “close enough” to another value.

As an example, suppose we want to know if a heading (e.g. from the IMU) is “approximately” 30 (degrees), +/- 2 degrees. We can subtract 30 from our heading value, take the absolute value of that, and make sure that the result is less than or equal to 2.

You can adjust the tolerance range (the amount of approximation) by changing the “2” in this pattern – the smaller the number, the closer the tested value needs to be to its target.

We use this pattern frequently when we want to know things like “is a motor encoder value approximately equal to this target value” or “is the robot pointed at approximately this heading”.

You can create a function to do approximate comparisons, such as:

The approach @ddiaz gives works fine, of course – we just find this pattern to be more useful when we want to know if something is “close enough” to a target value.

Pm

1 Like