Advanced Motors, Sensors and Third Party Hardware
|
Advanced Motors, Sensors and Third Party Hardware
Motor classesBasicMotorAll leJOS NXJ Motor classes extend the abstract BasicMotor class. BasicMotor defines the methods that any type of leJOS NXJ motor must support and provides implementations for some of them. The methods are:
RCXMotorThe RCX motors do not have an in-built tachometer and so cannot support the advanced functions of the NXT motors such as the rotate and rotateTo methods and the speed regulation. A simpler class is used to support the RCX motors. It has similar methods to the Motor class in the RCX version of leJOS (which are the same as those supported by the BasicMotor class). To use an RCX motor you create an instance of the RCXMotor class using the constructor:
Example:
You can use RCX Motors with leJOS NXJ by connecting them with the conversion cables that can be purchased from LEGO (and are bundled with the LEGO MINDSTORMS NXT educational kits). You can also use the RCXMotor class to control RCX motors connected to a remote RCX - see the "Communications" tutorial. MotorThis class controls the NXT motors which have a built in tachometer, and is described in Controlling the Motors MotorPort classesTo use the NXT motors it is not necessary to explicitly use the MotorPort class: you can just use the variables Motor.A, Motor.B and Motor.C. So if you are only using NXT motors, you can skip this section. However it is useful to understand how motor ports work as they are used by:
There is a hierarchy of interfaces defined for motor ports:
All motor ports support the BasicMotorPort interface which allows control of a motors power and mode (forward, backward, stop, float). Ports that supports this include:
The implementations of BasicMotorPort include:
The tachometers that are built in to the NXT motors support the Tachometer interface. NXT motor ports support the TachoMotorPort interface which includes the BasicMotorPort and Tachometer interfaces. Implementations of TachoMotorPort include:
All this sounds rather complicated, but is simple to use:
Sensor PortsIf you are using a sensor connected directly to a NXT sensor port, you can use the SensorPort class and you can probably skip this section. But if you are using a port splitter, or a remote NXT or RCX, it may be of interest. The NXT sensor ports support three different types of sensor:
Corresponding to each of the different types of sensor, there is a corresponding interface:
At the top of the interface hierarchy is the BasicSensorPort. All sensor port classes implement this interface. This interface allows the type and mode of a sensor to be set. These type and mode constants are defined by the interface SensorConstants, which is inherited by the BasicSensorPort interface. The types of sensors are:
and the modes are:
The BasicSensorPort interface defines the methods:
Most of the time, with leJOS NXJ, these types and modes do not need to be set explicitly as it is done by the constructor for the sensor class being used, e.g. TouchSensor, LightSensor and UltrasonicSensor. The implementation of the NXT sensor port – SensorPort – supports all these interfaces. The reason for separating out the different interfaces is that there are other implementations of sensor ports that only support a subset of these interfaces, and different types of sensors only require particular interfaces to be implemented:
Port splitters like the Mindsensors Split-Nx only support I2C sensors and thus, effectively, only support the I2CPort interface. There are other implementations that only support the other interfaces. For example the current implementation of remote sensor ports – RemoteSensorPort – currently only supports the ADSensorPort interface. The classes for RCX Sensors multiplexers – such as the forthcoming Mindsensors version – will only support the LegacySensorPort interface. SensorsEach sensor supported by leJOS NXJ has a specific class that is used to access the sensor. Each of these sensor classes has, as a parameter, a sensor port that supports the required interface. Any sensor port class that implements the interface can be specified as the parameter. As the SensorPort class supports all the interfaces, if the sensor being accessed is directly connected to the NXT, the parameter should be one of SensorPort.S1, SensorPort.S2, SensorPort.S3 or SensorPort.S4. If a port splitter is used the parameter again should be one of SensorPort.S1, SensorPort.S2, SensorPort.S3 or SensorPort.S4. This specifies the port that the splitter is connected to. If multiple sensors are connected to the splitter they must each have different I2C addresses. Most I2C sensors can have their address changed – see the manufacturers instructions. To specify the address that a sensor uses, if it is not the default, the setAddress method of I2CSensor should be used. The sensor ports supported by leJOS NXJ together with the class that supports them and the type of sensor port they require is given in the following table: RCX SensorsRCX sensors, other than the touch sensor, are active sensors that have voltage applied for all but the short period every three milliseconds when the measurement is taken. RCX Light SensorThe RCX light sensor is supported by the RCXLightSensor class. The constructor is:
For example:
The RCX light sensor is automatically activated, so current is applied to it and the LED comes on. It can be passivated and activated explicitly. The methods are:
RCX Touch SensorAs the RCX touch sensor is a passive sensor similar to the NXT version, it is supported by the standard TouchSensor class. RCX Rotation SensorThe RCX rotation sensor is not currently supported by leJOS NXJ. RCX Temperature SensorThe constructor is:
Third party sensors and other devicesleJOS NXJ supports many third party sensors. The two main vendors of third party sensors are Mindsensors and HiTechnic. Most of the third party sensors and I2C sensors and extend the I2CSensor class but there are also Analog/Digital sensors such as the HiTechnic Gyro sensor and the IR Seeker. There are also other I2C devices supplied by the third parties, that are not sensors, but are multiplexers or adapters. The RCX Motor Multiplexer from Mindsensors is an example of a multiplexer. It allows up to 4 RCX motors to be connected to a NXT sensor port and to be independently controlled. The Mindstorms NRLink-Nx infra-red communications adapter is an example of an adapter. It allows two-way communication between the NXT and RCXs. It also allows control of Power Function motors. I2CSensorThe I2CSensor class implements the basic methods for accessing I2C sensors including getData and SendData. It also includes methods that are implemented by all the I2C sensors, including getVersion, getProductID and getSensorType. The method signatures are:
leJOS NXJ uses 7-bit I2C addresses, whereas I2C device specification often give them as 8-bit addresses with the least significant bit set to zero. Most I2C sensors use a default address of 0x01, which such specifications give as 0x02. Most I2C devices allow the address to be changed. The setAddress method can be used to set the address used to talk to the I2C device – it defaults to 0x01. Individual I2C devices have registers that can be read and written and registers that can be used to execute commands. Each I2C device has a class that extends I2CSensor and has methods to access the registers and execute the commands specific to that sensor. The I2CSensor class can be used to implement an I2C device explorer that reports what devices are connected to which sensor sing which address – see the I2CDevices sample. This is possible as all the NXT I2C sensors and other devices support the getVersion, getProductID and getSensorType methods. See the table above for the complete list of sensors and other third party devices. |