NXT sensors

Brian allows you to work with five different types of sensors:

  • NXT Touch Sensor

  • NXT Color Sensor

  • NXT Ultrasonic Sensor

  • NXT Sound Sensor

  • HiTec Gyroscopic Sensor

Initialisation and Connection

First, you have to initialize the sensor. You must specify the sensor type and port (S1, S2, S3, S4). Here is an example of initialization:

import brian.sensors as sensors

TS = sensors.NXT.ColorSensorNXT(sensors.SensorPort.S1)
CS = sensors.NXT.TouchSensorNXT(sensors.SensorPort.S2)
US = sensors.NXT.UltrasonicSensorNXT(sensors.SensorPort.S3)
SO = sensors.NXT.SoundSensorNXT(sensors.SensorPort.S4)
import brian.sensors as sensors

GS = sensors.HiTec.GyroSensorHiTec(sensors.SensorPort.S1)

Now you can use your own names to control sensors, just separate method with dot. For example TS.is_connected().

After initialisation, you have to check, whether is sensor ready. These functions will help you:

is_connected

is_connected(self) bool
Returns:

True iff sensor is connected and not in the process of rebooting, False otherwise

is_ready

is_ready(self) bool

Ready-state indicates that the attempt to read values will give valid results. Example reasons for invalid results:

  • Sensor is not connected (is_connected returns False)

  • Sensor is rebooting or not initiated yet

  • Sensor is changing modes and the change is not finished yet

  • Connected sensor is incompatible with this handler (e.g. wrong type of sensor is connected)

In all of the above cases, this function will return False. :return: True iff values are ready for the next read, False otherwise

wait_until_ready

wait_until_ready(self, timeout_ms: int = -1) bool

Waits until the sensor is ready. This function is blocking. When changing modes, the sensor enters a “not ready” state for a short period (until the mode change is propagated). Therefore, it is recommended to first set the correct mode using set_mode() before the calling this function. This only applies to sensors with modes.

Parameters:

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

Return success:
  • True: The sensor is ready.

  • False: The sensor is not ready and time ran out.

reboot

reboot(self) None

Turn off power to the port and turn it back on. This will forcibly reboot the sensor.

The powered-down state lasts about 100ms. In case of some (mostly digital) sensors, there can be some additional time (~1s or more) to boot up and process connection handshake with Brian.

Touch Sensor

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

is_pressed

is_pressed(self) 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

wait_for_press(self, 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

wait_for_release(self, 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

wait_for_press_and_release(self, 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.

Light Sensor

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

set_led_on

set_led_on(self, 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

light_value(self, 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

light_value_raw(self, 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.

Ultrasonic Sensor

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

distance_cm

distance_cm(self) 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.

Sound Sensor

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

sound_intensity

sound_intensity(self) 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

sound_intensity_raw(self) 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.

Gyro Sensor

HiTec Gyro Sensor
class brian.sensors.HiTec.GyroSensorHiTec.GyroSensorHiTec(
   port: SensorPort
)

speed

speed(self) int

Measures the sensor state. Returns the angular speed in range -500 to 400

Returns:

angular speed in degrees/second (-500 to 400).

Raises:

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

Further Information

For further information about NXT sensors, go check API Reference.