/*
 * Multi axis discrete motion script
 *
 * Description of the script: 
 *  The script opens two axes by serial numbers. Moves along the X axis to a certain coordinate. Then it begins to shift discretely along the Y axis, with a programmable pause after each offset.
 *  The script can be useful if you are using the system to scan an area and/or capture frames
 * 
 * Warning: enter the serial numbers of your axes!
 *
 * Note:
 *  This is a rather difficult script to learn, since it uses a large number of commands and structures.
 *
 * Important: 
 *  This script will only be executed in multiaxial mDrive Direct Control mode! 
 *  To run the script, upload it to the mDrive Direct Control software. For multi-controller operation, open them in a single multi-axis mDrive Direct Control window
 */

// Enter the serial numbers of your axes.
serial_number_x = 14889;
serial_number_y = 14888;

var x = new_axis(serial_number_x);
var y = new_axis(serial_number_y);

// Installing the source data
var x_target_coordinate = 5000; // first border coordinate
var delay = 100; // delay in milliseconds

var y_first_border = 0; // first border coordinate
var y_second_border = 5000; // second border coordinate 
var y_step = 100
var y_direct = 1

// Calibration and positioning of the axes to their original positions.
x.command_stop(); // send STOP command (does immediate stop)
x.command_zero(); // send ZERO command (sets current position and encoder value to zero)
y.command_stop(); // send STOP command (does immediate stop)
y.command_zero(); // send ZERO command (sets current position and encoder value to zero)

x.command_move(x_target_coordinate); // move to wards one border
x.command_wait_for_stop(10); // wait until controller stops moving
y.command_move(y_first_border); // move towards one border
y.command_wait_for_stop(10); // wait until controller stops moving

// Movement in discrete samples along one axis from the end to the end
// with a delay after each movement.
while (1) { // infinite loop

 // Choosing the direction of travel
 if (y.get_position() >= y_second_border)
 {
  y_direct = -1
 }
 if (y.get_status().CurPosition <= y_first_border)
 {
  y_direct = 1
 }

 // Movement in a given direction
 y.command_movr(y_step*y_direct); // move towards another border
 y.command_wait_for_stop(10); // wait until controller stops moving
 msleep(delay);
}