0% found this document useful (0 votes)
16 views5 pages

Subrotines 1

Uploaded by

ajuthkumar3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views5 pages

Subrotines 1

Uploaded by

ajuthkumar3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

Subroutine Definition and Usage:

 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:

subr_name: SUBR(param1, param2);

-- body of the subroutine


END;

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.

5. Variable Declaration in Subroutines:

 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

var_name: STATIC 5; -- Static variable initialized to 5

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:

subr_name: SUBR(!param1, param2);


param1 = param2;

7. Aggregate Parameters:

 Subroutines can accept aggregates as parameters. Aggregates are ordered sets of


values, which may contain scalars, other aggregates, or both. supports aggregate
assignment and parallel assignment in subroutines.
 Example:

subr_name(<a, b, c>); -- Passing an aggregate to the subroutine

9. Returning Values with the RETURN Command:

 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);

10. Execution Process of Subroutines:

 When a subroutine is called, the following steps occur:


1. The PARMS variable is bound to an aggregate of all actual parameters.
2. Formal parameters are bound left-to-right.
3. Variable declarations, labels, and local subroutines are processed.
4. Executable statements are evaluated.
 When the subroutine finishes, the bindings are undone, and any temporary storage is
released.

11. Subroutine Cleanup and Error Handling:

 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.

12. System vs. User Subroutines:

 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.

13. Recursion in Subroutines:


 Recursive subroutine calls are permitted, meaning that a subroutine can call itself
within its body. This allows for complex operations such as traversing data structures
or performing iterative calculations.
14. Parallel Assignment in Subroutines:
 Parallel assignment allows multiple variables to be assigned values simultaneously
from an aggregate, reducing the need for temporary variables.
 Example: <var1, var2> = <expr1, expr2>;
15. Modularity and Reusability:
 Subroutines are an essential tool for modular programming in. They can be stored in
libraries and reused across different applications, reducing development time and
ensuring code consistency.

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)

// Define coordinates for the pickup and drop-off points

DEFINE pickup, dropoff

pickup = [X_pickup, Y_pickup, Z_pickup]

dropoff_base = [X_dropoff, Y_dropoff]

// Define the offsets for arranging parts in a 3x4 pattern

DEFINE x_offset, y_offset

x_offset = 40 // in mm

y_offset = 40 // in mm

FOR i = 0 TO 2 DO

FOR j = 0 TO 3 DO

// Calculate the dropoff position based on the offsets

dropoff = [dropoff_base[0] + i * x_offset, dropoff_base[1] + j * y_offset]

// Move to pickup position

MOVE TO pickup

WAIT 500 // Wait for 0.5 seconds

// Pick the part

PICK_PART()

// Move to dropoff position

MOVE TO dropoff

WAIT 500 // Wait for 0.5 seconds

// Drop the part

DROP_PART()

END FOR

END FOR
END

SUBR PICK_PART()

SENSIO(1)

GRASP ON

END

SUBR DROP_PART()

SENSIO(1)

GRASP OFF

END

You might also like