Bot'n Roll ONE A+ - Raspberry Pi 5 library
 
Loading...
Searching...
No Matches
onepi.utils.line_detector.LineDetector Class Reference

Public Member Functions

 compute_line (self, readings)
 

Protected Member Functions

 _normalise (self, reading, minimum, scale)
 
 _normalise_readings (self, sensor_reading)
 
 _calculate_factors (self, ref, min, max)
 
 _load_if_necessary (self)
 
 _compute_line_value (self, readings)
 
 _cap_value (self, value, lower_limit, upper_limit)
 
 _convert_range (self, x_value, x_min, x_max, y_min, y_max)
 
 _normalise_line_value (self, line_value, size)
 
 _filter_line_value (self, line_value, ref_value, max_value)
 
 _compute_mean_gaussian (self, reading)
 
 _get_max_value_and_index (self, lst)
 
 _prune (self, readings)
 

Static Protected Attributes

int _ref_max = 1000
 
tuple _cfg_loaded
 
list _scaling_factor
 
int _previous_line_value = 0
 
 _config = LineSensorConfig()
 

Detailed Description

Converts line sensor readings into a relative line position on the sensor.
It assumes that the array of sensors of a sensor peripheral are arranged in a straight line and equally spaced.
It provides methods to compute the location of the line relative to the device.
It outputs values in a range [-100, 100] where 0 (zero) corresponds to the centre of the sensor peripheral.

Member Function Documentation

◆ _calculate_factors()

onepi.utils.line_detector.LineDetector._calculate_factors ( self,
ref,
min,
max )
protected
Calculates the scaling factor given a reference value\
and a range defined by min and max values
This scaling factor is useful in normalising values

◆ _cap_value()

onepi.utils.line_detector.LineDetector._cap_value ( self,
value,
lower_limit,
upper_limit )
protected
Caps the value to lower and upper limits

◆ _compute_line_value()

onepi.utils.line_detector.LineDetector._compute_line_value ( self,
readings )
protected
Computes a line value in the range [0, ref_max]

◆ _compute_mean_gaussian()

onepi.utils.line_detector.LineDetector._compute_mean_gaussian ( self,
reading )
protected
Lets assume the line detected gives us a discrete gaussian
where the probabilities are given by each sensor reading and
the values are pre-determined based on each sensor location:
 |sensor id | value  | probability |
 |----------|--------|-------------|
 |    0     |   1    |  reading[0] |
 |    1     |   2    |  reading[1] |
 |  (...)   | (...)  |    (...)    |
 |    7     |   8    |  reading[7] |

We can compute the mean of the gaussian (location of line) by:
 1) computing the product for each sensor: product = value * probability
 2) computing sum_products = product[0] + product[1] + ... + product[7]
 3) computing sum_probabilities = reading[0] + reading[1] + ... + reading[7]
 4) computing mean = sum_products / sum_probabilities

◆ _convert_range()

onepi.utils.line_detector.LineDetector._convert_range ( self,
x_value,
x_min,
x_max,
y_min,
y_max )
protected
Converts a value x given in the range [x_min : x_max]
to a new value in the range [y_min : y_max]

◆ _filter_line_value()

onepi.utils.line_detector.LineDetector._filter_line_value ( self,
line_value,
ref_value,
max_value )
protected
Filters the line value to handle edge cases
such as no line detected or reading errors

:param line_value: the computed line value
:param ref_value: the reference value to help determining which side
is the line in case it's no longer detected (e.g. middle of the range)
:param max_value: the maximum value of the range

◆ _get_max_value_and_index()

onepi.utils.line_detector.LineDetector._get_max_value_and_index ( self,
lst )
protected
Returns the max value and its corresponding index from a list

◆ _load_if_necessary()

onepi.utils.line_detector.LineDetector._load_if_necessary ( self)
protected
Loads values from config if they weren't loaded before

◆ _normalise()

onepi.utils.line_detector.LineDetector._normalise ( self,
reading,
minimum,
scale )
protected
normalise a reading taking the minimum value of the range and a scale factor

◆ _normalise_line_value()

onepi.utils.line_detector.LineDetector._normalise_line_value ( self,
line_value,
size )
protected
Converts a line value in the range of [0, 8000] to a new range e.g. [-106, 106]
This method involves two steps:
1)  Converting the value in the range of [0, 8000] to a new range using a correction factor
    e.g. for a correction factor of 6% the new range is [-106, 106]
    The new range extends beyond 100 by a little margin specified by the correction factor in the config.
    The purpose of this is to enable adjusting the sensitivity of the readings near the extremities.
    For a sensor that is typically higher (away from the floor) this correction factor should be higher
    to prevent large jumps in the readings close to the extremities.
    By increasing the correction factor we are narrowing the sensitivity of the sensors and by reducing it
    we are extending the sensitivity out.
    There is a balance to achieve by tuning this value correctly in order to get the best of both worlds:
    - Smooth transitions in sensor value near the extremeties
    - Maximum sensitivity region of the sensor
2) Capping the values to the desired range [-100, 100]

◆ _normalise_readings()

onepi.utils.line_detector.LineDetector._normalise_readings ( self,
sensor_reading )
protected
Normalize values for each sensor reading

◆ _prune()

onepi.utils.line_detector.LineDetector._prune ( self,
readings )
protected
Deals with edge cases such when the max readings is on one of the sensors at the extremety
In such cases it bumps up the value of the sensor to be at the threshold level

◆ compute_line()

onepi.utils.line_detector.LineDetector.compute_line ( self,
readings )
Given an input as a set of sensors readings it computes a relative location of a line
along the length of the sensor and expresses the output in a range [-100, 100]
where 0 (zero) corresponds to the line being at the centre of the sensor.

Member Data Documentation

◆ _cfg_loaded

tuple onepi.utils.line_detector.LineDetector._cfg_loaded
staticprotected
Initial value:
= (
False # flag to signal whether or not the config values have been read already
)

◆ _scaling_factor

list onepi.utils.line_detector.LineDetector._scaling_factor
staticprotected
Initial value:
= [
0
] * 8

The documentation for this class was generated from the following file: