brian.sensors.NXT package

brian.sensors.NXT.LightSensorNXT module

class brian.sensors.NXT.LightSensorNXT.LightSensorNXT(port: SensorPort)

Bases: Sensor

Class for interacting with NXT light sensor.

Sensor is automatically registered in the constructor of the base class and un-registered in its destructor. It can also be unregistered with the LightSensorNXT.close_sensor() function.

There can be at most one instance at any given time, of any sensor class per port in the entire program.

__init__(port: SensorPort)

Initialize an NXT light sensor at the given port.

Parameters:

port – Sensor port to which the sensor is attached.

set_led_on(led_on: bool) None

This function sets the sensor to the desired mode. While it’s not mandatory, it is recommended to call this function before accessing values from the sensor in a specific mode to prevent SensorIsNotReady exceptions.

Parameters:

led_on – whether the red LED should be set on or off

light_value(led_on: bool | None) float

Measures incoming light.

Parameters:

led_on – Indicates whether the red LED should be on or off. - If no value is provided or None is passed, the previously set LED state will be used.

Returns:

Value in range 0-1, with 0 being the darkest and 1 being the brightest.

Raises:

brian.sensors.SensorIsNotReadyError – If the sensor is not ready.

light_value_raw(led_on: bool | None) int

Measures incoming light. Raw measurement is inverted, meaning lower values correspond to brighter light.

Parameters:

led_on – Indicates whether the red LED should be on or off. - If no value is provided or None is passed, the previously set LED state will be used.

Returns:

Value in range 0-4095, with 4095 being the darkest and 0 being the brightest.

Raises:

brian.sensors.SensorIsNotReadyError – If the sensor is not ready.

brian.sensors.NXT.SoundSensorNXT module

class brian.sensors.NXT.SoundSensorNXT.SoundSensorNXT(port: SensorPort)

Bases: Sensor

Class for interacting with NXT sound sensor.

Sensor is automatically registered in the constructor of the base class and un-registered in its destructor. It can also be unregistered with the SoundSensorNXT.close_sensor() function.

There can be at most one instance at any given time, of any sensor class per port in the entire program.

__init__(port: SensorPort)

Initialize an NXT sound sensor at the given port.

Parameters:

port – Sensor port to which the sensor is attached.

sound_intensity() float

Measures incoming sound.

Returns:

Value in range 0-1, with 0 being the quietest and 1 being the loudest.

Raises:

brian.sensors.SensorIsNotReadyError – If the sensor is not ready.

sound_intensity_raw() int

Measures incoming sound. Raw measurement is inverted, meaning lower values correspond to louder sounds.

Returns:

Value in range 0-4095, with 4095 being the quietest and 0 being the loudest.

Raises:

brian.sensors.SensorIsNotReadyError – If the sensor is not ready.

brian.sensors.NXT.TouchSensorNXT module

class brian.sensors.NXT.TouchSensorNXT.TouchSensorNXT(port: SensorPort)

Bases: Sensor

Class for interacting with NXT touch sensor.

Sensor is automatically registered in the constructor of the base class and un-registered in its destructor. It can also be unregistered with the TouchSensorNXT.close_sensor() function.

There can be at most one instance at any given time, of any sensor class per port in the entire program.

__init__(port: SensorPort)

Initialize an NXT touch sensor at the given port.

Parameters:

port – Sensor port to which the sensor is attached.

is_pressed() bool

Measures the sensor state. Returns boolean of the last button pressed state. If the sensor is not ready, returns False.

Returns:

True if the sensor button is pressed, False otherwise. Or False if the sensor is not ready.

Raises:

brian.sensors.SensorIsNotReadyError – If the sensor is not ready.

wait_for_press(timeout_ms: int = -1) bool

Waits for next button press event. This function is blocking.

Parameters:

timeout_ms – Maximum number of milliseconds to wait. If the timeout is negative, the function will wait indefinitely.

Return success:
  • True: If the desired button event was caught.

  • False: If the timeout ran out.

wait_for_release(timeout_ms: int = -1) bool

Waits for next button release event. This function is blocking.

Parameters:

timeout_ms – Maximum number of milliseconds to wait. If the timeout is negative, the function will wait indefinitely.

Return success:
  • True: If the desired button event was caught.

  • False: If the timeout ran out.

wait_for_press_and_release(timeout_ms: int = -1) bool

Waits for next button press and release event. This function is blocking.

Parameters:

timeout_ms – Maximum number of milliseconds to wait. If the timeout is negative, the function will wait indefinitely.

Return success:
  • True: If the desired button event was caught.

  • False: If the timeout ran out.

brian.sensors.NXT.UltrasonicSensorNXT module

class brian.sensors.NXT.UltrasonicSensorNXT.UltrasonicSensorNXT(port: SensorPort)

Bases: Sensor

Class for interacting with NXT ultrasonic sensor.

Sensor is automatically registered in the constructor of the base class and un-registered in its destructor. It can also be unregistered with the UltrasonicSensorNXT.close_sensor() function.

There can be at most one instance at any given time, of any sensor class per port in the entire program.

__init__(port: SensorPort)

Initialize an NXT ultrasonic sensor at the given port.

Parameters:

port – Sensor port to which the sensor is attached.

distance_cm() int

Continuously measures the distance and returns the value in cm.

Returns:

distance in cm (0-255).

Raises:

brian.sensors.SensorIsNotReadyError – If the sensor is not ready.