Subrotines 1
Subrotines 1
Subroutine Declaration: Subroutines are declared using the SUBR keyword. The
structure of a subroutine typically consists of a name, formal parameters (optional),
and the body of the subroutine, which contains executable expressions and
commands.
Syntax Example:
2. Subroutine Calls:
Subroutines are called using their name followed by parentheses. Parameters (if any)
are passed as expressions. If no parameters are passed, the parentheses can be
omitted.
Example:
subr_name(expression1, expression2);
3. Return Values:
Subroutines return values to the calling environment using the RETURN command. The
value of the RETURN expression becomes the value of the subroutine call. If no value is
specified, the subroutine returns a null aggregate.
Example:
RETURN(expression);
4. Subroutine Types:
Subroutines can be user-written or system-defined. Both types are called in the same
manner. However, system-defined subroutines, such as MOVE and MONITOR, often have
low-level access to system primitives for greater efficiency.
Variables can be declared within subroutines using the NEW or STATIC keyword.
o NEW: Allocates a new copy of storage for each entry into the subroutine. The
type and initial value are determined by the expression provided.
o STATIC: Retains the value of the variable between multiple calls to the
subroutine.
Example:
var_name: NEW 0; -- New variable initialized to 0
6. Formal Parameters:
Value Parameters: These are passed by value. The actual parameter expression is
evaluated, and the result is stored in a temporary variable. The subroutine works on
this temporary copy.
Reference Parameters: These are passed by reference, meaning that changes made
to the parameter inside the subroutine affect the original variable in the calling
environment. To pass by reference, the ! operator is used in the parameter list.
Example:
7. Aggregate Parameters:
The RETURN command allows subroutines to return values to the calling environment.
If no RETURN command is used, the subroutine implicitly returns a null aggregate.
Example:
RETURN(value);
CLEANUP: This command is used to set a subroutine exit trap, allowing specific
actions to be taken when a subroutine exits.
ERRTRAP: This command sets an error handler for the subroutine, specifying
actions to take if an error occurs.
System-defined subroutines are implemented as part of the system itself and usually
handle critical low-level operations such as robot motion or sensor I/O.
User-defined subroutines extend the functionality of by allowing users to define
custom behavior, build reusable code, and modularize programs.
Example program
MAIN: SUBR();
-- Declare two positions as aggregates (lists of coordinates)
position1: NEW <10, 5, 0>;
position2: NEW <20, 10, 0>;
-- Move the robot to position 1
MOVE_TO_POSITION(position1);
-- Turn on the light at position 1
TURN_ON_LIGHT();
-- Move the robot to position 2
MOVE_TO_POSITION(position2);
-- Turn off the light at position 2
TURN_OFF_LIGHT();
END;
-- Subroutine to move the robot to a specified position
MOVE_TO_POSITION: SUBR(position);
-- Move the robot joints to the specified position
MOVE(<1, 2, 3>, position);
WAITMOVE; -- Wait until the move is complete
END;
-- Subroutine to turn on the light
TURN_ON_LIGHT: SUBR();
-- Simulate turning on the light
PRINT("Light turned ON");
END;
-- Subroutine to turn off the light
TURN_OFF_LIGHT: SUBR();
-- Simulate turning off the light
PRINT("Light turned OFF");
END;
Write a robot program to pick parts off a conveyor and load them into a pallet about 12 inches
from the pickup point. A Mechanical stop on the conveyor is used to locate the parts in a known
position for the pickup. The parts will be arranged in a 3 by 4 pattern, 40 mm apart in both
directions. The two directions of the pallet are assumed to be the robot's x and y world coordinate
axes, respectively. (Use AML Language only)
x_offset = 40 // in mm
y_offset = 40 // in mm
FOR i = 0 TO 2 DO
FOR j = 0 TO 3 DO
MOVE TO pickup
PICK_PART()
MOVE TO dropoff
DROP_PART()
END FOR
END FOR
END
SUBR PICK_PART()
SENSIO(1)
GRASP ON
END
SUBR DROP_PART()
SENSIO(1)
GRASP OFF
END