Class Sensor
The sensor class provides a uniform interface for using most of the sensors available for the EV3.
Declaration
source linkDocumentation
Methods
▶ def __init__(self, address=None, name_pattern=SYSTEM_DEVICE_NAME_CONVENTION, ...) overrideinherited doc Spin through the Linux sysfs class for the device type and find a device that matches the provided name pattern and attributes (if any).
def __init__(
self,
address=None,
name_pattern=SYSTEM_DEVICE_NAME_CONVENTION,
name_exact=False,
**kwargs,
)
This method overrides ev3dev2.Device.__init__.
- Parameters:
- class_name: class name of the device, a subdirectory of /sys/class.
For example, 'tacho-motor'.
- name_pattern: pattern that device name should match.
For example, 'sensor*' or 'motor*'. Default value: '*'.
- name_exact: when True, assume that the name_pattern provided is the
exact device name and use it directly.
- keyword arguments: used for matching the corresponding device
attributes. For example, address='outA', or driver_name=['lego-ev3-us', 'lego-nxt-us']. When argument value is a list, then a match against any entry of the list is enough.
Example:
d = ev3dev.Device('tacho-motor', address='outA')
s = ev3dev.Device('lego-sensor', driver_name=['lego-ev3-us', 'lego-nxt-us'])
If there was no valid connected device, an error is thrown.
Overrides
This method is overriden in:
▷ def address(self) @property Returns the name of the port that the sensor is connected to, e.g. ``ev3:in1``. I2C sensors also include the I2C address (decimal), e.g. ``ev3:in1:i2c8``.
@property
def address(self)
▶ def bin_data(self, fmt=None) Returns the unscaled raw values in the ``value<N>`` attributes as raw byte array. Use ``bin_data_format``, ``num_values`` and the individual sensor documentation to determine how to interpret the data.
Use fmt to unpack the raw bytes into a struct.
Example:
>>> from ev3dev2.sensor.lego import InfraredSensor
>>> ir = InfraredSensor()
>>> ir.value()
28
>>> ir.bin_data('<b')
(28,)
▶ def bin_data_format(self) @property Returns the format of the values in ``bin_data`` for the current mode. Possible values are:
▷ def command(self) @property Sends a command to the sensor.
@property
def command(self)
▷ def command(self, value) @command.setter @command.setter
def command(
self,
value,
)
▷ def commands(self) @property Returns a list of the valid commands for the sensor. Returns -EOPNOTSUPP if no commands are supported.
@property
def commands(self)
▷ def decimals(self) @property Returns the number of decimal places for the values in the ``value<N>`` attributes of the current mode.
@property
def decimals(self)
▷ def driver_name(self) @property Returns the name of the sensor device/driver. See the list of [supported sensors] for a complete list of drivers.
@property
def driver_name(self)
▷ def mode(self) @property Returns the current mode. Writing one of the values returned by ``modes`` sets the sensor to that mode.
▷ def mode(self, value) @mode.setter @mode.setter
def mode(
self,
value,
)
▷ def modes(self) @property Returns a list of the valid modes for the sensor.
▷ def num_values(self) @property Returns the number of ``value<N>`` attributes that will return a valid value for the current mode.
@property
def num_values(self)
▷ def units(self) @property Returns the units of the measured value for the current mode. May return empty string
▶ def value(self, n=0) Returns the value or values measured by the sensor. Check num_values to
see how many values there are. Values with N >= num_values will return an error. The values are fixed point numbers, so check decimals to see if you need to divide to get the actual value.