The leJOS Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail

Trail: Essential leJOS classes
Lesson: Controlling the Hardware

Controlling the Hardware: Motors


The static Motor class is used to control actuators, i.e., devices connected to one of the three black output ports mounted beneath the RCX's display:
Most of its methods are intented to be used with actual LEGO® motors, but may also be used in connection with, say, a light bulb.

Not very surprisingly, the three ports are implemented as static fields of the Motor class:

Hence a typical call would be

            Motor.A.forward();
        
Controlling one of these motors is rather simple: for forcing the motor turning clock- or counterclockwise, floating (which means that the motor is idling) or stopping it, for setting the motor power from 0 to 7 (which has only a noticeable effect on the motor when it's heavy loaded!)
and the associated information methods and public static char getID(), which delivers 'A', 'B' or 'C'.

Note that the actual direction the motor is turning to depends on the way the connectors are mounted on the ports and the motors, resp. - so if your robot is running backwards when it shouldn't it may suffice to turn one of the connectors for 90.

A complete example which uses the Motor class

is Rotator.java which may be found in the examples/rotator section of the leJOS tree.

The Motor API

may be found here.
The leJOS Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail