The Component Library
Introduction
The component library is a set of useful components that we prepared for you in advance.
It contains all kinds of components: components that read information from sensors, components that do certain actions like halt the program etc.
For each component we will introduce the cmp code.
To use a component you should import it first to your own component (examples are in "Getting started with componenet language") and initialize it with the right argument (usually a SensorPort or a MotorPort).
Then, you need to feed its in ports, or read its out ports.
Note: Most library components are implemented in java, but the cmp code is enough to learn how to use them.
Back to top
Sensors
These components wrap the NXT sensors. Their goal is to sample the relevant sensor each cycle, and output the sample in an out port.
Touch Sensor
This component reads the touch sensor and outputs in 'pressed' port 'true' if it is pressed, 'false' else.
Argument is the lejos.nxt.SensorPort that the sensor is connected to.
TouchSensor.cmp code:
package il.ac.tau.cs.smlab.model2lejos.library.wrappers.touchsensor;
component TouchSensor[lejos.nxt.SensorPort sensorPort] {
port
out Boolean pressed;
}
Light Sensor
This component reads the light sensor and outputs the brightness in 'brightness' port as an integer between 0-1023. (0=dark, 1023=bright)
Argument is the lejos.nxt.SensorPort that the sensor is connected to.
LightSensor.cmp code:
package il.ac.tau.cs.smlab.model2lejos.library.wrappers.lightsensor;
component LightSensor[lejos.nxt.SensorPort sensorPort] {
port
out Integer brightness;
}
Ultrasonic Sensor
This component reads the ultrasonic sensor and outputs the range in 'range' port as a Double between 0 cm to 255 cm.
Argument is the lejos.nxt.SensorPort that the sensor is connected to.
UltrasonicSensor.cmp code:
package il.ac.tau.cs.smlab.model2lejos.library.wrappers.ultrasonicsensor;
component UltrasonicSensor[lejos.nxt.SensorPort sensorPort] {
port
out Double range;
}
Color Sensor
This component reads the color sensor and outputs the color in 'color' port as a Color.
More information about Color type is here.
Our recommendation is to use this component along with the Color Distinguisher component, that can evaluate the color RGB to a name.
Argument is the lejos.nxt.SensorPort that the sensor is connected to.
ColorSensor.cmp code:
package il.ac.tau.cs.smlab.model2lejos.library.wrappers.colorsensor;
import lejos.nxt.ColorSensor.Color;
component ColorSensor[lejos.nxt.SensorPort sensorPort] {
port
out Color color;
}
Sound Sensor
This component reads noise rate from sound sensor, as a percentage from 0 to 100.
It has one out port 'volume' for that cause.
SoundSensor.cmp code:
package il.ac.tau.cs.smlab.model2lejos.library.wrappers.soundsensor;
component SoundSensor[lejos.nxt.SensorPort sensorPort] {
port
out Integer volume;
}
Gyro Sensor
This component is used to communicate with the gyro sensor.
gyroValue out port is the gyro raw value with offset applied.
angularVelocity out port holds the current angular velocity. When integrating for a heading, values less than 1.0 can be ignored to minimize perceived drift since the resolution of the Gyroscope sensor is 1 deg/sec.
offset in port is used to set the offset used by gyroValue. Default at instantiation is zero.
GyroSensor.cmp code:
package il.ac.tau.cs.smlab.model2lejos.library.wrappers.soundsensor;
component GyroSensor[lejos.nxt.SensorPort sensorPort] {
port
out Integer gyroValue,
out Float angularVelocity,
in Integer offset;
}
Compass Sensor
This component is used to communicate with the compass sensor.
Degrees out port reads the directional heading in degrees (0 to 359.9) while 0 is North.
DegreesCartesian out port reads cartesian degrees from 0 to 360, but -
resetCartesianZero in port can be used to designate any direction as zero, rather than relying on North as being zero.
CompassSensor.cmp code:
package il.ac.tau.cs.smlab.model2lejos.library.wrappers.compasssensor;
component CompassSensor[lejos.nxt.SensorPort sensorPort] {
port
out Float Degrees,
out Float DegreesCartesian,
in Boolean resetCartesianZero;
}
Back to top
Differential Pilot
This component wraps the DifferentialPilot class of the NXT.
The DifferentialPilot class is a software abstraction of the Pilot mechanism of a NXT robot. It contains methods to control robot movements: travel forward or backward in a straight line or a circular path or rotate to a new direction.
Arguments are left wheel diameter, right wheel diameter,MotorPort of left motor and MotorPort of right motor.
Input ports:
travelSpeed port - robot speed in cm/s or inch/s (depends on the unit you entered for the wheels).
discrete_speed port - robot speed out of a bank of speeds:
MotorSpeed.SPEED1 = 0.1*(max speed)
MotorSpeed.SPEED2 = 0.2*(max speed)
MotorSpeed.SPEED3 = 0.3*(max speed)
MotorSpeed.SPEED4 = 0.4*(max speed)
MotorSpeed.SPEED5 = 0.5*(max speed)
MotorSpeed.SPEED6 = 0.6*(max speed)
MotorSpeed.SPEED7 = 0.7*(max speed)
MotorSpeed.SPEED8 = 0.8*(max speed)
MotorSpeed.SPEED9 = 0.9*(max speed)
MotorSpeed.SPEED10 = max speed
rotateSpeed port - robot rotation speed in .
bTurn90Right port - if 'true' robot travels about 13cm and turns right about 90 degrees.
bTurn90Left port - if 'true' robot travels about 13cm and turns left about 90 degrees.
bTurn180 port - if 'true' robot turns about 180 degrees.
distance port - travels the inserted distance.
cmd port - command for the pilot:
MotorCmd.FORWARD - drives the motors forward.
MotorCmd.STOP - stopps the robot.
MotorCmd.ROTATELEFTINPLACE - turns left in place.
MotorCmd.ROTATERIGHTINPLACE - turns right in place.
MotorCmd.STEERLEFT - drives the robot to the left in an arc path.
MotorCmd.STEERRIGHT - drives the robot to the right in an arc path.
DifferentialPilot.cmp code:
package il.ac.tau.cs.smlab.model2lejos.library.wrappers.differentialpilot;
import il.ac.tau.cs.smlab.model2lejos.library.wrappers.differentialpilot.types.*;
component DifferentialPilot[Double lWheelDiameter, Double rWheelDiameter, lejos.nxt.MotorPort left, lejos.nxt.MotorPort right] {
port
/** speed in distance (wheel diameter) units/sec */
in Double travelSpeed,
/** rotation speed in degrees per second*/
in Double rotateSpeed,
in Boolean bTurn90Right,
in Boolean bTurn90Left,
in Boolean bTurn180,
/** distance in cm*/
in Double distance,
/** see MotorCmd */
in MotorCmd cmd,
/** see MotorSpeed */
in MotorSpeed discrete_speed;
}
Back to top
LCD
This component receives a message as a String in 'message' port and displays it on the LCD screen.
LCD.cmp code:
package il.ac.tau.cs.smlab.model2lejos.library.wrappers.lcd;
component LCD {
port
in String message;
}
Back to top
Button
This component is responsible for a specific button. It outputs 'true' in 'isPressed' if button is pressed, 'false' else.
Argument is the button number as an Integer whereas:
1=ENTER button
2=LEFT button
3=RIGHT button
4=ESCAPE button
Button.cmp code:
package il.ac.tau.cs.smlab.model2lejos.library.wrappers.buttons;
component Button[Integer buttonNr] {
port
out Boolean isPressed;
}
Back to top
Color Distinguisher
This component is used to evaluate the color (of type lejos.nxt.ColorSensor.Color) it receives in 'color' port.
Output ports:
'colorValue' is the color received.
'redValue' is amount of RED between 0-255.
'greenValue' is amount of GREEN between 0-255.
'blueValue' is amount of BLUE between 0-255.
'rgbValues' is a string "R;G;B;" where R,G,B are RED,GREEN,BLUE values.
'colorName' is color name in a string.
'colorNumber' is enum of the color:
Color.BLACK
Color.BLUE
Color.CYAN
Color.DARK_GRAY
Color.GRAY
Color.GREEN
Color.LIGHT_GRAY
Color.MAGENTA
Color.NONE
Color.PINK
Color.RED
Color.WHITE
Color.YELLOW
ColorDistinguisher.cmp code:
package il.ac.tau.cs.smlab.model2lejos.library.wrappers.colordistinguisher;
import lejos.nxt.ColorSensor.Color;
component ColorDistinguisher{
port
in Color color,
out Color colorValue,
out Integer redValue,
out Integer greenValue,
out Integer blueValue,
out String rgbValues,
out String colorName,
out Integer colorNumber;
}
Back to top
System Exit
This component is resposible for halting the program.
If 'bExitSystem' is 'true' the system will exit.
SystemExit.cmp code:
package il.ac.tau.cs.smlab.model2lejos.library.components.systemexit;
component SystemExit{
port
in Boolean bExitSystem;
}
LED
This component is responsible for operating a LED that is connected to a MotorPort.
If 'led_on' is 'true' then LED will be on, and off if 'false'.
Argument is lejos.nxt.MotorPort that the LED is connected to.
LED.cmp code:
package il.ac.tau.cs.smlab.model2lejos.library.wrappers.led;
component LED[lejos.nxt.MotorPort motorport] {
port
in Boolean led_on;
}
Back to top
Sound on Brick
SoundOnBrick.cmp code:
package il.ac.tau.cs.smlab.model2lejos.library.wrappers.soundonbrick;
component SoundOnBrick {
port
in Boolean bPlaySingleBeep,
in Boolean bPlayImperialMarch;
}
Back to top
Download Library
You can download the library files from this link for your own convenience.
Back to top
|