/*
 * Sync bug test script
 *
 * This script does synchroniztion test to find sync bug in firmware 3.9.9, 3.8.7, 3.9.10.
 * This test only for hardwarer from ver. 2.2.0 to ver. 2.2.4.
 * To begin the test, the first what you need to do is connect synchronization pin 15 to pin 16 on CN1 jack.
 * The scripts automaticly set all neccessary settings for test, and automaticly begining the test.
 * As a result you will see a log message with current position of motor and text OK! (green text) or ERROR! (red text).
 */

var axes = [];
var number_of_axes = 0;
var last_serial = 0;

while (serial = get_next_serial(last_serial)) // get next serial number and repeat for each axes.
{
  axes[number_of_axes] = new_axis(serial);
  number_of_axes++;
  last_serial = serial;
  log("Found axis " + number_of_axes + " with serial number " + serial);
}

// SSNI settings
var SSNI = get_sync_in_settings;
SSNI.SyncInFlags = SYNCIN_ENABLED | SYNCIN_GOTOPOSITION; //set flags for synchronization in for enabled and absolute position
SSNI.ClutterTime = 4;// set sync in Clutter Time 
SSNI.Position = 0;// set sync in absolute position 
SSNI.Speed = 500;// set sync in speed
set_sync_in_settings(SSNI);// set synchronization in settings

// GSNO settings
var GSNO = get_sync_out_settings;
GSNO.SyncOutFlags = SYNCOUT_ENABLED | SYNCOUT_ONPERIOD;  // set flags for synchronization out for enabled out and period
GSNO.SyncOutPeriod = 200;// set sync out period
GSNO.SyncOutPulseSteps = 2000;// set sync out Pusle width
set_sync_out_settings(GSNO);	// set synchronization out settings

for (i = 0; i < 10; i++)  // testing cycle 
{
  GETS = get_status();// get information about device
  command_movr(201, 0);  // shifting position for 201 steps 
  command_wait_for_stop(100); // Wait until the movement is complete

  if (GETS.CurPosition != 0)// get device current position and compare it with 0
  {
     log(">>> Error! GETS.CurPosition = " + GETS.CurPosition, 1);// log ERROR, bug is exist
  }
  else
  {
     log(">>> OK! GETS.CurPosition = " + GETS.CurPosition, 3); // log OK, bug does not exist
  }
}
