Aegidius
 Plüss   Aplulogo
 
in examples
Use the asterisk (*) character for wildcard searches
Searches on more than one word will be treated "as a phrase"
 
serverstatus
 www.aplu.ch
 www.java-online.eu
     Print Text 
© 2018, V10.0
 
  Jython
 
 

.

In the last couple of years small and cheap moving robots containing a microprocessor brick are extensively used in the class room. They promote a modern aspect of our technical society and the engineering way of thinking for the constructing a concrete robot designed to perform a predefined task as well as for writing programs to make the robot accomplish that particular task. Using robots can be so much fun in the class room that kids are not even aware they are learning!

One of the most widespread educational robot is Lego NXT and the new Lego EV3. The NXT brick can be programmed to run autonomously using a few specific languages, among them graphical based languages (LabVIEW and the like), C and Java. The robot can also be controlled by sending commands over Bluetooth from a PC running any programming language. The NxtJLib/EV3Lib libraries developed for Java have a pure object-oriented design using the classical event callback model. The libraries are used in many Java programming courses and have been included in the TigerJython distribution.

Installation for NXT:
Copy into the Lib subdirectory of the directory where tigerjython2.jar resides:
under Windows: bluecove.2-1-1.jar (download)
under Linux/Mac: bluecove-gpl-2.1.1-SCNAPSHOT.jar (download)

This site emphasizes the Lego NXT brick used in direct mode. To learn how to use the new EV3 robot in direct or autonomous mode, consult the EV3JLIB Website here.

Example 1: Purpose: Learn the fundamental concept of NxtJLib.

A robot instance is created and the Bluetooth link established. Then two motors are created and assembled into to robot. Hereafter the robot is used. In the end the robot is disconnected and the program terminates.

# NxtEx1.py
  
from nxtrobot import *

# Create robot
robot = NxtRobot()
#robot = NxtRobot(0x16531B3EB1)

# Create two motors
motA = Motor(MotorPort.A)
motB = Motor(MotorPort.B)
    
# Assemble motors into robot
robot.addPart(motA)
robot.addPart(motB)

# Use motors
motA.forward()
motB.forward()

# Let them turn a moment
Tools.delay(5000)

# Stop motors and disconnect
robot.exit()
Select NxtEx1.py (Ctrl+C to copy, Ctrl+V to paste)

If you call the parameterless constructor of NxtRobot, a dialog box opens, where the NXT Bluetooth name has to be entered.

Jtut29

Press OK and the connection is engaged. The following dialog box is shown:

JTut30

If the connection process is successful, the following dialog is displayed and the robot emits a "connection melody".

JTut31

In this box you see the Bluetooth hexadecimal address. You may write it down and pass it to the NxtRobot constructor in your next program run.

robot = NxtRobot(0x16531B3EB1)

This will speed up the connection process considerably because no Bluetooth search is performed. If you need to stop the robot before the program ends you can hit the close button of the connection dialog box. More graceful you use the Escape key to terminate the program.

# NxtEx2.py

from nxtrobot import *

#robot = NxtRobot()
robot = NxtRobot(0x16531B3EB1)

motA = Motor(MotorPort.A)
robot.addPart(motA)
while not robot.isEscapeHit():
   motA.forward()
   Tools.delay(500)
   motA.stop()
   Tools.delay(500)
robot.exit()
Select NxtEx2.py (Ctrl+C to copy, Ctrl+V to paste)

 

Example 2: Purpose: Demonstrate how to use a touch sensor

The touch sensor is plugged into port S1. The robot moves forward until the touch sensor is hit. Then it moves back a while and turns to the left. Then it moves forward again.

# NxtEx3.py

from nxtrobot import *

#robot = NxtRobot()
robot = NxtRobot(0x16531B3EB1)

gear = Gear()
ts = TouchSensor(SensorPort.S1)
robot.addPart(gear)
robot.addPart(ts)
gear.forward()
while not robot.isEscapeHit():
  if ts.isPressed():
    gear.backward(1000);
    gear.left(500);
    gear.forward();
robot.exit()
Select NxtEx3.py (Ctrl+C to copy, Ctrl+V to paste)

Instead of polling the sensor you may register a callback that is fired when the touch sensor press event occurs. In Jython you use a named constructor parameter to register the callback function.

# NxtEx4.py

from nxtrobot import *

def onPressed(port):
   if ts.isPressed():
       gear.backward(1000);
       gear.left(500);
       gear.forward();

#robot = NxtRobot()
robot = NxtRobot(0x16531B3EB1)
gear = Gear()
ts = TouchSensor(SensorPort.S1)
ts = TouchSensor(pressed = onPressed)
robot.addPart(gear)
robot.addPart(ts)
gear.forward()
Select NxtEx4.py (Ctrl+C to copy, Ctrl+V to paste)

 

Example 3: Purpose: Demonstrate how to use a light sensor by implementing a border follower

The light sensor is plugged into port S1. It looks downwards and detects if the light is reflected from a black or white surface. The task of the robot is to follow a black/white border. We use the event model and register the onBright() and onDark() callback using named parameters.

# NxtEx5.py

from nxtrobot import *

def onBright(port, level):
  gear.leftArc(0.2)
  print "left"
   
def onDark(port, level):
   gear.rightArc(0.2)
   print "right"

#robot = NxtRobot()
robot = NxtRobot(0x16531B3EB1)

ls = LightSensor(bright = onBright, dark = onDark)
robot.addPart(ls)
ls.setTriggerLevel(400)
ls.activate(True)

gear = Gear()
gear.setSpeed(20)
robot.addPart(gear)
gear.forward()

while not robot.isEscapeHit():
    pass
robot.exit()
Select NxtEx5.py (Ctrl+C to copy, Ctrl+V to paste)

 

Example 4: Purpose: Construct a fire detecting system.

With NxtJLib the NXT robot can only be used in the so-called direct mode where a host computer sends commands and receives data at runtime. (In autonomous mode, a robot program is downloaded to the brick and executed there). In direct mode only a restricted part of the NXT sensors are supported, but the full power of a hosting system is available (GUI, TCP/IP, Sound, etc.). In the following example we construct a fire detecting system using the light sensor. When the luminosity exceeds a certain level, the host emits the sound of a fire horn.

The package ch.aplu.util provides a panoply of easy-to-use sound handling methods (Doc).

# NxtEx6.py

from nxtrobot import *
from ch.aplu.util import SoundPlayer

def bright(port, level):
  print "fire alarm"
  sp = SoundPlayer("firehorn.wav")
  sp.play()
   
#robot = NxtRobot()
robot = NxtRobot(0x16531B3EB1)

ls = LightSensor(bright = bright)  # Port S1
robot.addPart(ls)
ls.setTriggerLevel(400)
Select NxtEx6.py (Ctrl+C to copy, Ctrl+V to paste)
Download a sound archive file, unpack it and put firehorn.wav in the current program directory