/*
 * For calb step script
 *
 * Description of the script: 
 *  In the script, the user units are configured, after which there is a departure to the leftmost position and then a certain number of shifts occur until the right position is reached. Each position is recorded in a .csv file.
 * 
 * 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 uniaxial mDrive Direct Control mode!
 *  To run the script, upload it to the mDrive Direct Control software
 */

command_home(); // send HOME command (find home position)
command_wait_for_stop(100); 
command_zero();
command_wait_for_stop(100); 

// Setting the value to convert to user unit
var mm_per_step = 0.00125; // steps to distance translation coefficient
var calb = new_calibration(mm_per_step, get_engine_settings().MicrostepMode); // create calibration structure

// Setting boundaries and movement step
// Boundaries can be set manually or taken from limit constraints
var edge = get_edges_settings_calb(calb);
var first_border = edge.LeftBorder; //0;
var second_board = edge.RightBorder;//23;
var shift = 5;//step move;
var delay = 2000;// The delay of movement

var f = new_file("file.csv"); // Choose a file name and path
f.open(); // Open a file
f.resize(0);
f.seek( 0 ); // Seek to the beginning of the file

command_move_calb(first_border, calb); // Move to the starting position
command_wait_for_stop(delay); // Wait until controller stops moving
var i = 1;
var time = 0;
var step = (second_board - first_border)/shift;
 f.write(0+ ","  +  get_status().CurPosition + "," +  get_status().uCurPosition+ "," + "\n" ); // Get current position, potentiometer value and date and write them to file
do {
  time = i*delay;
  command_movr_calb(shift, calb); // Move to the next position
  command_wait_for_stop(delay); // Wait until controller stops moving
  f.write(time + ","  +  get_position_calb(calb).Position + "," +  get_position_calb(calb).EncPosition+ "," + "\n" ); // Get current position, potentiometer value and date and write them to file
  i = i+1;
} while (  get_position_calb(calb).Position+shift < second_board )
f.close(); // Close the file

