# Automatic Material Alignment (AMA) Automation Example

This document describes how to start alignment automatically from a G-code program.
Load your G-code file in the CNC controller and press start; alignment is then triggered through controller variables.

## Control Variables

Communication between the CNC controller and the alignment tool uses the following variables:

- #999  --> here the #of markers to load into the material alignment tool need to be defined (0=off; 1=one markers,2...,3)
- #996  --> should always be zero (legacy)

The following variables provide marker positions in mm and marker diameter:

- #998 --> MARK1_X
- #997 --> MARK1_Y
- #994 --> MARK2_X
- #993 --> MARK2_Y
- #992 --> MARK3_X
- #991 --> MARK3_Y
- #990 --> MARK_DIAM

Only one circle diameter can be used per alignment cycle. For many workflows, this is sufficient because each cycle typically aligns to a marker, applies processing, and then continues to the next marker.

## Start Alignment Macros

To toggle #999 and #996, use dedicated subroutines for the selected marker count.
Include these subroutines in the Marco.cnc file of the EdingCNC machine control.

Create three routines for easy triggering from your G-code file:

- StartAlignment_1_marker
- StartAlignment_2_marker
- StartAlignment_3_marker

Call the appropriate routine after defining marker positions (maximum of 3 markers).

```text
sub StartAlignment_1_marker
  msg "AMA link macro"
  if  [#5397==0] ; and [#5380==0] - not simulation mode
    #999=1 ;flag to indicate to ama to start
    #996=0
    msg "Starting AMA and waiting for alignment to finish"
    M0 ; wait for the program to be continued by AMA
  endif
endsub

sub StartAlignment_2_marker
  msg "AMA link macro"
  if  [#5397==0] ; and [#5380==0] - not simulation mode
    #999=2 ;flag to indicate to ama to start
    #996=0
    msg "Starting AMA and waiting for alignment to finish"
    M0 ; wait for the program to be continued by AMA
  endif
endsub

sub StartAlignment_3_marker
  msg "AMA link macro"
  if  [#5397==0] ; and [#5380==0] - not simulation mode
    #999=3 ;flag to indicate to ama to start
    #996=0
    msg "Starting AMA and waiting for alignment to finish"
    M0 ; wait for the program to be continued by AMA
  endif
endsub
```

## G-code Example

The example below defines three markers, starts alignment, and then moves the tool above each marker position to illustrate correct alignment.

```text
marker definitions are given by filling variables all in [mm]:
;MARK1_X = #998
;MARK1_Y = #997
;MARK2_X = #994
;MARK2_Y = #993
;MARK3_X = #992
;MARK3_Y = #991
;MARK_DIAM = #990 

#990=9
#998=10
#997=20
#994=100
#993=25
#992=110
#991=200


gosub StartAlignment_3_marker

; circle move
; circle at 0,0
G0 X-5Y0
G02 i5 j0

;jog to 2nd circle 
G0 X-20 Y20 
; circle at -20,20
G0 X-25 Y20
G02 i5 j0

;jog to 3rd circle 
G0 X-20 Y40 
; circle at -20,40
G0 X-25Y40
G02 i5 j0
```
