-
Notifications
You must be signed in to change notification settings - Fork 1
Map and Constrain
###Map
Values from a node can be "mapped" to another range. In the README file there is an example of mapping a PPM signal to a servo. The PPM input produces values in the range of 1000 to 2000. You can set the position of a servo in degrees of angle in the range of +/- 90. So we want to "compress" the input range of 1000 to 2000 down to 90 to -90. Likewise you could stretch a range or even invert a range. In this case we make it so 1000 changes to -90 and 2000 changes to 90, and of course 1500 changes to 0.
n1.map(in_start, in_end, out_start, out_end)
The value of n1 is made to conform to the range described by the arguments to the method. in_start to in_end is the input range. out_start to out_end is the output range. Note that the names "low" and "high" are not used since the values could be in either order.
map() by default will constrain the values to the output range. You can supply constrain=False to the method to skip the constrain. The value of n1 does not have to be between in_start and in_end, sometimes it's useful to get values outside the range out_start to out_end, but usually you want to constrain.
###Constrain
n1.constrain(n2, n3)
Values of n1 outside the range of values n2 to n3 are changed to the nearest value. Note that it does not matter if the n2 is greater or less than n3. This is similar to retain_between but instead of changing values outside the range to zero, the values are changed to the closest in the range.