MaSH Environment rcx2threads

Contents

Purpose

This environment supports programming a Lego Mindstorms RCX robot, via the Lejos system, for programs that require two execution threads.

Rewrites

mandatory
void main()

Purpose: A program that is organised into methods must have a main method (a procedure with no arguments). This will be the first method to execute. mashc automatically rewrites this method to conform to standard Java.

Setting up sensors

Constants

final int TOUCH

Purpose: Constant to select sensor type touch.

final int LIGHT

Purpose: Constant to select sensor type light.

final int ROTATION

Purpose: Constant to select sensor type rotation.

Methods

void setUpSensor(int port, int type)

Purpose: Sets up the port to be sensor of the given type.

Precondition: port is 1, 2, or 3.

Precondition: type is TOUCH, LIGHT, or ROTATION.

Using touch sensors

Methods

void waitForPush(int port)

Purpose: Makes the program wait until the touch sensor on port is pushed.

Precondition: port is 1, 2, or 3.

Precondition: The port has been set up as a touch sensor.

void waitForLetGo(int port)

Purpose: Makes the program wait until the touch sensor on port is let go.

Precondition: port is 1, 2, or 3.

Precondition: The port has been set up as a touch sensor.

boolean isPushed(int port)

Purpose: Returns true if and only if the button on port is currently pushed.

Precondition: port is 1, 2, or 3.

Precondition: The port has been set up as a touch sensor.

Using light sensors

Methods

void waitForLighter(int port, int dif)

Purpose: Makes the program wait until the light sensor reading on port is increased by dif.

Precondition: port is 1, 2, or 3.

Precondition: The port has been set up as a light sensor.

Precondition: dif is between 0 and 100, inclusive. 0 is no wait at all. Real light levels never really change by anything like 100.

void waitForLight(int port, int light)

Purpose: Makes the program wait until the light sensor reading on port is at least the desired light level.

Precondition: port is 1, 2, or 3.

Precondition: The port has been set up as a light sensor.

Precondition: light is between 0 and 100, inclusive.

void waitForDarker(int port, int dif)

Purpose: Makes the program wait until the light sensor reading on port is decreased by dif.

Precondition: port is 1, 2, or 3.

Precondition: The port has been set up as a light sensor.

Precondition: dif is between 0 and 100, inclusive. 0 is no wait at all. Real light levels never really change by anything like 100.

void waitForDark(int port, int light)

Purpose: Makes the program wait until the light sensor reading on port is at most the desired light level.

Precondition: port is 1, 2, or 3.

Precondition: The port has been set up as a light sensor.

Precondition: light is between 0 and 100, inclusive.

int getLight(int port)

Purpose: Returns the current light sensor reading on port.

Precondition: port is 1, 2, or 3.

Precondition: The port has been set up as a light sensor.

Using rotation sensors

Methods

void waitForRotation(int port, int rotation)

Purpose: Makes the program wait until the counter in the rotation sensor on port has changed by at least the absolute value of rotation.

Precondition: port is 1, 2, or 3.

Precondition: The port has been set up as a rotation sensor.

int getRotation(int port)

Purpose: Returns the current rotation sensor reading on port.

Precondition: port is 1, 2, or 3.

Precondition: The port has been set up as a rotation sensor.

void resetRotation(int port)

Purpose: Sets the counter in the rotation sensor on port to zero.

Precondition: port is 1, 2, or 3.

Precondition: The port has been set up as a rotation sensor.

Output port constants

Constants

final int A

Purpose: Constant to select port A.

final int B

Purpose: Constant to select port B.

final int C

Purpose: Constant to select port C.

Using motors

Methods

void motorForward(int port, int power)

Purpose: Make the motor on port go forwards at the given power.

Precondition: port is A, B, or C.

Precondition: power is betweeen 0 and 7, inclusive.

void motorBackward(int port, int power)

Purpose: Make the motor on port go backwards at the given power.

Precondition: port is A, B, or C.

Precondition: power is betweeen 0 and 7, inclusive.

void motorStop(int port)

Purpose: Stop the motor on port.

Precondition: port is A, B, or C.

void motorFloat(int port)

Purpose: Float the motor on port.

Precondition: port is A, B, or C.

Using lamps

Methods

void lampOn(int port, int power)

Purpose: Make the lamp on port go on at the given power.

Precondition: port is A, B, or C.

Precondition: power is betweeen 0 and 7, inclusive.

void lampOff(int port)

Purpose: Turns the lamp on port off.

Precondition: port is A, B, or C.

Waiting for fixed times

Methods

void sleep(int ms)

Purpose: Makes the program wait for a requested number of ms (miliseconds).

Making sounds

Methods

void systemSound(int i)

Purpose: Play system sound number i. The system sounds are as follows.

i description
0 short beep
1 double beep
2 descending arpeggio
3 ascending arpeggio
4 long, low beep
5 quick ascending arpeggio

Precondition: 0 ≤ i ≤ 5.

void playTone(int frequency, int duration)

Purpose: Plays a tone, given its frequency (Hertz) and duration (centiseconds).

Precondition: 31 ≤ frequency ≤ 2100.

Precondition: 0 ≤ duration ≤ 256.

Using the LCD

Methods

void showNumber(int a)

Purpose: Displays a number in the LCD. Does not require refresh().

Precondition: 0 ≤ a ≤ 9999.

void clear()

Purpose: Clears the LCD, but the effect will not show until refresh() is called.

void refresh()

Purpose: Refreshes the LCD, causing the changes to be displayed.

void putChar(char c, int i)

Purpose: Puts a character c into the LCD at position i.

Precondition: 0 ≤ i ≤ 4.

Using infra-red communications

Methods

void sendByte(int i)

Purpose: Send one byte of information to another RCX, by infra-red transmission. The value to send, i, will be truncated if it can't fit in a byte.

int receiveByte()

Purpose: Waits for and returns a new byte sent by another RCX via infra-red transmission.

Math

Purpose

The following are some commonly used numeric constants and functions.

Constants

final int MAX_INT

Purpose: A constant holding the maximum value an int can have, 231 − 1.

final int MIN_INT

Purpose: A constant holding the minimum value an int can have, − 231.

final double PI

Purpose: The closest double approximation to π.

final double E

Purpose: The closest double approximation to e.

Methods

double abs(double a)

Purpose: Returns the absolute value of a.

int abs(int a)

Purpose: Returns the absolute value of a.

double ceil(double a)

Purpose: Returns the least double value that is greater than or equal to a and equal to an integer.

double exp(double x)

Purpose: Returns ex, that is Euler's constant e raised to power x.

double floor(double a)

Purpose: Returns the greatest double value that is less than or equal to a and equal to an integer.

double log(double x)

Purpose: Returns the natural logarithm of x.

double rint(double x)

Purpose: Returns the closest mathematical integer to x.

int round(float a)

Purpose: Returns the closest int to a.

double sqrt(double a)

Purpose: Returns the square root of a.

Precondition: a ≥ 0.0.

double pow(double a, double b)

Purpose: Returns a raised to the power b, ab.

double sin(double a)

Purpose: Returns the trigonometric sine of a radians.

double cos(double a)

Purpose: Returns the trigonometric cosine of a radians.

double tan(double a)

Purpose: Returns the trigonometric tangent of a radians.

double asin(double a)

Purpose: Returns the trigonometric arc sine of a in radians.

double acos(double a)

Purpose: Returns the trigonometric arc cosine of a in radians.

double atan(double a)

Purpose: Returns the trigonometric arc tangent of a in radians.

double atan2(double y, double x)

Purpose: Returns the angle theta from the conversion of rectangular coordinates (x, y) to polar coordinates (r, theta).

double max(double a, double b)

Purpose: Returns the greater of a and b.

int max(int a, int b)

Purpose: Returns the greater of a and b.

double min(double a, double b)

Purpose: Returns the lesser of a and b.

int min(int a, int b)

Purpose: Returns the lesser of a and b.

double random()

Purpose: Returns a random value x such that 0.0 ≤ x < 1.0.

Strings

Purpose

The following are methods for working with Strings.

Methods

int length(String s)

Purpose: Returns the length of s.

char charAt(String s, int i)

Purpose: Returns the character at position i in s.

Precondition: 0 ≤ i < length(s).

boolean equals(String a, String b)

Purpose: Returns true if and only if a contains the same sequence of characters as in b.

boolean parseBoolean(String s)

Purpose: Returns s converted to a boolean.

int parseInt(String s)

Purpose: Returns s converted to an int.

long parseLong(String s)

Purpose: Returns s converted to a long.

float parseFloat(String s)

Purpose: Returns s converted to a float.

double parseDouble(String s)

Purpose: Returns s converted to a double.

Threads

Rewrites

void run()

Purpose: A program may have a second thread of execution. The run() method contains the statements that perform the actions of the second thread.

Methods

void start()

Purpose: Start the extra thread and call run().