Aegidius
 Plüss   Aplulogo
     
 www.aplu.ch      Print Text 
© 2021, V10.4
 
  RaspiBrickPi Library
 
EV3 and NXT Sensors
 

 

All sensors from the current Lego EV3 product line are supported. They are modelled by software in a class named by the sensor type, as there are:

  • ColorSensor
  • GyroSensor
  • IRDistanceSensor
  • IRRemoteSensor
  • IRSeekSensor
  • ColorSensor
  • LightSensor (ColorSensor in ambient light detection mode)
  • TouchSensor
  • UltrasonicSensor

All sensor use the same programming concept: An instance of the sensor is created using the constructor that specifies the sensor port. To read a sensor value, typically a read or get method is issued.

In many cases sensor values are requested in a repeating loop, we say, that "the sensor is polled periodically". To allow the program to terminate smoothly, the push buttons is clicked. In this function it is called Escape-Button.

 

Polling the touch sensor

In this basic program to poll the EV3 touch sensor at port S1 uses a data acquisition loop with a time interval of 100 ms and displays the state of the sensor at the OLED display. It is terminated by pressing the escape button.

from ev3brickpi import *

robot = LegoRobot()
oled = OLED1306()
ts = TouchSensor(SensorPort.S1)

while not robot.isEscapeHit():
    if ts.isPressed():
        oled.println("pressed")
    else:
        oled.println("released")
    Tools.delay(100)
oled.println("Done")
Tools.delay(2000)
robot.exit()

Since output from Python print commands are sent to stdout (standard out), you cannot see them unless you run the program in a console window. However if you are using the TigerJython IDE and you terminate the RaspiBrick firmware monitor by clicking Tools | Terminate Python on Target, the output to stdout is capture by the TigerJython message window. This is very convenient for debugging purposes.

 

Displaying ultrasonic sensor data in the console window

Since output from Python print commands are sent to stdout (standard out), you cannot see them unless you run the program in a console window. However if you are using the TigerJython IDE and you terminate the RaspiBrick firmware monitor by clicking Tools | Terminate Python on Target, the output to stdout is capture by the TigerJython message window. This is very convenient for debugging purposes.

from ev3brickpi import *

robot = LegoRobot()
us = UltrasonicSensor(SensorPort.S1)

while not robot.isEscapeHit():
    d = us.getDistance()
    print "d =", d
    Tools.delay(20)
print "done"
robot.exit()    


If you stop the RaspiBrick firmware, you can even use an endless loop, since a running program is automatically terminated by TigerJython when a new program is downloaded.

from ev3brickpi import *

robot = LegoRobot()
us = UltrasonicSensor(SensorPort.S1)

while True:
    d = us.getDistance()
    print "d = ", d
    Tools.delay(20)  

Because robot.exit() is not called, the BrickPi3 system is not resetted and it remains in a undefined state. But the constructor of LegoRobot will reset it at the next program run.

 

NXT sensors

Many sensors from the old Lego NXT product line are supported. They are modelled by software in a class named Nxt followed by the sensor type, as there are:

  • NxtLightSensor
  • NxtSoundSensor
  • NxtTouchSensor
  • NxtUltrasonicSensor

The programming concept remains the same as for the EV3 sensor.