First steps

Connection test

Opening

$ pl
?- [plnxt].
?- nxt_open.

Pay attention to the established connection symbol on the brick display.

Command

Checking the battery level (the simplest command, which writes and reads from the device):

?- nxt_voltage_millivolt(Voltage).

You should receive the battery voltage, if not, there is a problem!

Closing

?- nxt_close.

Pay attention to the established connection symbol on the brick display. It has gone.

Using SWIPL shell

Please run the SWIPL shell and load the plnxt.pl file ([plnxt].). Then:

  • Open connection: nxt_open.
  • Perform a series of selected commands (movement, sensors readings). For example:
    • nxt_go(300). % Moving forward at a speed of 300 degrees/second.
    • nxt_stop.
    • nxt_go_cm(400,80). % Moving 80 cm forward at a speed of 400 degrees/second.
    • nxt_touch(Value). % Reading touch sensor.
    • nxt_sound(Value). % Reading sound sensor.
    • nxt_light(Value). % Reading light sensor.
    • nxt_light_LED(activate). % Turning a light sensor diode on.
    • nxt_light(Value).
    • nxt_light_LED(passivate).
    • nxt_ultrasonic(Value).
    • nxt_rotate(350,360). % Rotating 360 degrees to the right at a speed of 350 degrees/second.
    • nxt_play_tone(500,2000). % Playing tone at frequency 500 Hz for 2000 ms.
  • Close connection: nxt_close.

File programs

Most of predicates have their version with a force option (e.g. nxt_go(400, force).), which causes the immediate execution of the command. The lack of this option makes the command execution waiting till the robot is stopped.
This allows to write sequential programs, such as the following example.

Please copy the code below into a file and consult it in SWIPL.

:- consult('plnxt.pl_path').
 
start :-
	nxt_open,
	nxt_go_cm(400,80), % Moving 80 cm forward at a speed of 400 degrees/second.
	nxt_go_cm(-400,80), % Moving 80 cm backward at a speed of 400 degrees/second.
	nxt_close.
 
:- start.

What in this case is the 'force' option needed for?

Please test the simple examples of using the trigger mechanism (FIXME documentation).

Example 1

:- consult('plnxt.pl_path').
 
start :-
	nxt_open,
	trigger_create(_,check_distance,[nxt_stop,nxt_close]),
	nxt_go(300).
 
check_distance :-
	nxt_ultrasonic(Distance,force),
	Distance < 15.

The trigger will be fired, when check_distance is true. Then the robot will be stopped and the connection closed.
Therefore, it is a simple program: robot moves forward till encounters an obstacle.
The measurement of distance is cyclic. Without the 'force' option it would take place after the engines stopped (in this case “never”).
The robot would painfully encounter an obstacle.

Example 2

:- consult('../plnxt.pl').
 
start:-
	nxt_open,
	go_on_buddy,
	trigger_create(_,pushed,[nxt_stop,nxt_close]).
 
go_on_buddy :-
	nxt_go(200),
        sleep(1), % Short break, so one clap would not be recognized as two.
	trigger_create(_,clap,wait_a_second_buddy).
 
wait_a_second_buddy :-
	nxt_stop,
        sleep(1),
	trigger_create(_,clap,go_on_buddy).
 
% Verifying if the sound intensity exceeds the threshold value.
clap :-
	nxt_sound(Value,force),
	Value > 15.
 
% Verifying if the touch sensor is pressed.
pushed :-
	nxt_touch(Value,force),
	Value=1.

The robot moves forward. Stops after a clap. Moves farther after an another clap.
It stops and closes the connection after pressing touch sensor.
Choose a threshold for detecting a clap sound.
Too low value can cause a loop.
Too high will make you will have to punch the sensor instead of clapping.

mindstorms/plnxt/first_steps.txt · Last modified: 2019/06/27 15:50 (external edit)
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0