Class InfraredSensor
LEGO EV3 infrared sensor.
Declaration
class InfraredSensor(
Sensor, ButtonBase)
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.sensor.Sensor.__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.
▷ def beacon(self, channel=1) Checks if ``beacon`` button is pressed.
▷ def bottom_left(self, channel=1) Checks if ``bottom_left`` button is pressed.
▷ def bottom_right(self, channel=1) Checks if ``bottom_right`` button is pressed.
▶ def buttons_pressed(self, channel=1) Returns list of currently pressed buttons.
▷ def distance(self, channel=1) Returns distance (0, 100) to the beacon on the given channel. Returns None when beacon is not found.
▷ def heading(self, channel=1) Returns heading (-25, 25) to the beacon on the given channel.
▷ def heading_and_distance(self, channel=1) Returns heading and distance to the beacon on the given channel as a tuple.
▶ def process(self) Check for currenly pressed buttons. If the new state differs from the old state, call the appropriate button event handlers.
To use the on_channel1_top_left, etc handlers your program would do something like:
def top_left_channel_1_action(state):
print("top left on channel 1: %s" % state)
def bottom_right_channel_4_action(state):
print("bottom right on channel 4: %s" % state)
ir = InfraredSensor()
ir.on_channel1_top_left = top_left_channel_1_action
ir.on_channel4_bottom_right = bottom_right_channel_4_action
while True:
ir.process()
time.sleep(0.01)
▷ def proximity(self) @property An estimate of the distance between the sensor and objects in front of it, as a percentage. 100% is approximately 70cm/27in.
@property
def proximity(self)
▷ def top_left(self, channel=1) Checks if ``top_left`` button is pressed.
▷ def top_right(self, channel=1) Checks if ``top_right`` button is pressed.