unit uNmcIF;
// Inteface for SETI Net Rotor.Dll 2018-07-15
// Hello Big Jim...  The entry points are  C A S E   S E N S I T I V E

interface

uses System.Types;
{$REGION ' C O N S T A N T S '}

Const

{$IF Defined (WIN64)}
  RotorDLL = 'Rotor-64bit.dll';
{$ENDIF}
{$IF Defined (WIN32)}
  RotorDLL = 'NMCLIB04.dll';
{$ENDIF}

  AzPulsePerDeg = 628; // Rounded up to an integer
  AzVelocity = 50000; // 100000
  AzAcceleration = 100; // 20; // back to 20 2009-03-09
  // ElPulsePerDeg = 2494;
  ElPulsePerDeg = 667; // 113384 pulse / 170deg =  667 pulse/Deg, 90 deg = 53357 pulse 2018-05-31
  ElVelocity = 100000;
  // ElVelocity = 50000;
  ElAcceleration = 100; //

  CNumKerrModules = 3;
  FilterRelay = 2; // Channel on the Head End electronics Kerr module
  ElAddr = 2;
  AzAddr = 3;
  IOAddr = 1;
  AzPWM = 75; // For rotor homing (was 100)
  // ElPWM = 250; // For rotor homing
  ElPWM = 150; // For rotor homing  2018-06-19
  ElKp = 100;
  ElKd = 1000;
  ElKi = 0;
  ElIl = 0;
  ElEl = 20000;

  ElOl = 255;
  ElCl = 0;
  ElSr = 1;
  ElDc = 0; // Dead band compensation
  ElSm = 1;

  AzMax = 360;
  AzKp = 100;
  AzKd = 1000;
  AzKi = 0;
  AzIl = 0;
  AzEl = 16000;

  AzOl = 255;
  AzCl = 0;
  AzSr = 1;
  AzDc = 0; // Dead band compensaton was 45
  AzSm = 1;

const

  // Module type definitions:
  SERVOMODTYPE = 0;
  ADCMODTYPE = 1;
  IOMODTYPE = 2;
  STEPMODTYPE = 3;

  // Servo Module Command set:
  RESET_POS = $00; // Reset encode counter to 0
  SET_ADDR = $01; // Set address and group address
  DEF_STAT = $02; // Define status item to return
  READ_STAT = $03; // Read value of current status items
  LOAD_TRAJ = $04; // Load trajectory data
  START_MOVE = $05; // Start pre-loaded trajectory move
  SET_GAIN = $06; // Set servo gain and contro parameters
  STOP_MOTOR = $07; // Stop motor
  IO_CTRL = $08; // Define bit directions and set output
  SET_HOMING = $09; // Define homing mode
  SET_BAUD = $0A; // Set the baud rate
  CLEAR_BITS = $0B; // Save current position in the home position register
  SAVE_AS_HOME = $0C; // Store the input bytes and timer value
  // $ 0D   Not used
  NOP = $0E; // No Op - returnes previous defined status
  HARD_RESET = $0F; // RESET - no status is returned

  // IO modules STATUSITEMS bit definitions
  SEND_AD1 = $02;
  SEND_AD2 = $04;
  SEND_AD3 = $08;

  // Servo Module STATUSITEMS bit definitions
  SEND_POS = $01; // 4 bytes
  SEND_AD = $02; // 1 byte
  SEND_VEL = $04; // 2 bytes
  SEND_AUX = $08; // 1 byte
  SEND_HOME = $10; // 4 bytes
  SEND_ID = $20; // 2 bytes
  SEND_PERROR = $40; // 2 bytes
  SEND_NPOINTS = $80; // 1 byte

  // Servo Module LOAD_TRAJ control byte bit definitions
  LOAD_POS = $01;
  LOAD_VEL = $02;
  LOAD_ACC = $04;
  LOAD_PWM = $08;
  ENABLE_SERVO = $10;
  VEL_MODE = $20;
  REVERSE = $40;
  MOVE_REL = $40;
  START_NOW = $80;

  // Servo Module STOP_MOTOR control byte bit definitions:
  AMP_ENABLE = $01; // Enables the amplifier
  MOTOR_OFF = $02; // Turns the motor off
  STOP_ABRUPT = $04; // Slam on the brakes.
  STOP_SMOOTH = $08; // Decellerate smoothly
  STOP_HERE = $10; // Stop at position

  // Servo Module IO_CTRL control byte bit definitions:
  SET_OUT1 = $01; // Set limit1 output (clear other wise)
  SET_OUT2 = $02; // Set limit2 output (clear other wise)
  IO1_IN = $04; // Limit 1 is an input (otherwise its an output)
  IO2_IN = $08; // Limit 2 is an input (otherwise its an output)

  LIMSTOP_OFF = $04; // turn off motor on limit
  LIMSTOP_ABRUPT = $08; // stop abruptly on limit
  THREE_PHASE = $10; // 1 = 3-phase mode, 0 = single PWM channel
  ANTIPHASE = $20; // 1 = antiphase (0 = 50% duty cycle), 0 = PWM & dir
  FAST_PATH = $408; // 0 = 30 or 60 Hz path execution, 1 = 60 or 120 Hz
  STEP_MODE = $808; // 0 = normal operation, 1 = Step & Direction enabled

  // Servo Module SET_HOMING control byte bit definitions:
  ON_LIMIT1 = $01; // Home on change in limit1
  ON_LIMIT2 = $02; // Home on change in limit2
  HOME_MOTOR_OFF = $04; // Turn motor off when homed
  ON_INDEX = $08; // Home on change in index
  HOME_STOP_ABRUPT = $10; // Stop abruptlyh when homed
  HOME_STOP_SMOOTH = $20; // Stop smoothly when homed
  ON_POS_ERR = $40; // home on excessive position error
  ON_CUR_ERR = $80; // home on overcurrent error

  // Servo Module STATUS byte bit definitions:
  MOVE_DONE = $01; // set when move done (in Trap) when goal reached (in VEL0
  CKSUM_ERROR = $02; // checksum error received in command
  OVERCURRENT = $04; // Overcurrent detected (sticky bit)
  POWER_ON = $08; // Motor power is on
  POS_ERR = $10; // Set on excess positon error (sticky bit)
  LIMIT1 = $20; // Limit Switch 1 (on or off)
  LIMIT2 = $40; // Limit Switch 2 (on or off)
  HOME_IN_PROG = $80; // Set if searching for home.  Reset when found

  // Servo Module  Auxiliary STATUS byte bit definitions:
  INDEX = $01; // Value of the encoder index signal
  POS_WRAP = $02; // Set when 32 bit position counter wraps (sticky bit)
  SERVO_ON = $04; // Set when positon servo is operating
  ACCEL_DONE = $08; // Set when ecceleration portion of a move is done
  SLEW_DONE = $10; // Set whe slew portion of a move is done
  SERVO_OVERRUN = $20; // Set of servo takes longer than the specified servo period
  PATH_MODE = $40; // Set when path mode motion is in progress.
 // RotorDLL = 'NMCLIB04.DLL';
{$ENDREGION}
  //

function IoGetADCVal(portname: Byte; Chan: Byte): Byte; stdcall;
function IoGetPWMVal(portname: Byte; Chan: Byte): Byte; stdCall;
function IoSetPWMVal(portname: Byte; Pwm1: Byte; Pwm2: Byte): integer; stdCall;
function IoBitDirIn(portname: Byte; bitnum: integer): integer; stdCall;
function IoBitDirOut(portname: Byte; bitnum: integer): integer; stdCall;
function IoSetOutBit(portname: Byte; bitnum: integer): integer; stdCall;
function IoClrOutBit(portname: Byte; bitnum: integer): integer; stdCall;
function IoGetBitDir(portname: Byte; bitnum: integer): integer; stdCall;
function IoOutBitVal(portname: Byte; bitnum: integer): integer; stdCall;

function NmcInit(portname: pAnsichar; baudrate: integer): integer; stdCall;
function NmcHardReset(addr: Byte): boolean; stdCall;
function NmcSendcmd(addr: Byte; cmd: Byte; datastr: pchar; n: Byte; stataddr: Byte): boolean; stdCall;
procedure NmcShutdown; stdCall;
function NmcReadStatus(addr: Byte; statusitems: Byte): boolean; stdCall;
function NmcDefineStatus(addr: Byte; statusitems: Byte): boolean; stdCall;
function NmcGetStat(addr: Byte): Byte; stdCall;
function NmcGetModType(addr: Byte): Byte; stdCall;
function NmcGetModVer(addr: Byte): Byte; stdCall;
function NmcNoOp(addr: Byte): boolean; stdCall;
function NmcGetStatItems(addr: Byte): Byte; stdCall;

function ServoGetCmdPos(addr: Byte): integer; stdCall;
function ServoSetPos(addr: Byte; pos: integer): boolean; stdCall;
function ServoClearBits(addr: Byte): boolean; // Does not seem to work
function ServoGetPos(addr: Byte): LongInt; stdCall;
function ServoGetPError(addr: Byte): integer; stdCall;
function ServoGetIoCtrl(addr: Byte): Byte; stdCall;
function ServoGetStat(addr: Byte): boolean; stdCall;
function ServoGetAux(addr: Byte): Byte; stdCall;
function ServoLoadTraj(addr: Byte; mode: Byte; pos: LongInt; vel: LongInt; acc: LongInt; pwm: Byte): boolean; stdCall;

function ServoResetPos(addr: Byte): boolean; stdCall;
function ServoSetHoming(addr: Byte; mode: Byte): boolean; stdCall;
function ServoSetIoCtrl(addr, mode: Byte): boolean; stdCall;
function ServoSetGain(addr: Byte; kp: smallInt; kd: smallInt; ki: smallInt; il: smallInt; ol: Byte; cl: Byte; el: smallInt; sr: Byte; dc: Byte): Byte; stdCall;
function ServoSetGain2(addr: Byte; kp: smallInt; kd: smallInt; ki: smallInt; il: smallInt; ol: Byte; cl: Byte; el: smallInt; sr: Byte; dc: Byte; sm: Byte)
  : Byte; stdCall;
function ServoStopMotor(addr: Byte; mode: Byte): boolean; stdCall;

// Simple Test
function PowerOf2(value: Double): Double; stdCall;
function PowerOf3(value: Double): Double; stdCall;
function CircleArea(value: Double): Double; stdCall;
function CircleCircum(value: Double): Double; stdCall;

// SIO

function ErrorPrinting(value: integer): integer; stdcall;
function ErrorMsgBox(Text: pAnsichar): integer; stdCall;
function SimpleMsgBox(Text: String): integer; stdCall;

function SioOpen(portname: pAnsichar; baudrate: integer): THandle; stdCall; // Returns a handle to the PIC card
function SioChangeBaud(Handle: THandle; baud: integer): boolean; stdCall;
function SioClrInbuf(Handle: THandle): boolean; stdCall;
function SioPutChars(Handle: THandle; stuff: pAnsichar; n: DWORD): boolean; stdCall;
function SioGetChars(Handle: THandle; stuff: pAnsichar; n: DWORD): DWORD; stdCall;
function SioClose(Handle: THandle): integer; stdCall;
function SioTest(Handle: THandle): DWORD;

implementation

// SIO
function SioTest; external RotorDLL name 'SioTest';
function SioOpen; external RotorDLL name 'SioOpen';
function SioChangeBaud; external RotorDLL name 'SioChangeBaud';
function SioClrInbuf; external RotorDLL name 'SioClrInbuf';
function SioClose; external RotorDLL name 'SioClose';
function SioPutChars; external RotorDLL name 'SioPutChars';
function SioGetChars; external RotorDLL name 'SioGetChars';

Function ErrorPrinting; external RotorDLL name 'ErrorPrinting';
function ErrorMsgBox; external RotorDLL name 'ErrorMsgBox';
function SimpleMsgBox; external RotorDLL name 'SimpleMsgBox';

// Simple Test
function PowerOf2; external RotorDLL name 'PowerOf2'; // index 3;
function PowerOf3; external RotorDLL name 'PowerOf3'; // index 4;
function CircleArea; external RotorDLL name 'CircleArea'; // index 1;
function CircleCircum; external RotorDLL name 'CircleCircum'; // index 2;

// function IoGetADCVal(portname: Byte; Chan: Byte): Byte; stdcall;   // From the interface section

function IoGetADCVal; external RotorDLL name 'IoGetADCVal';
function IoGetPWMVal; external RotorDLL name 'IoGetPWMVal';
function IoSetPWMVal; external RotorDLL name 'IoSetPWMVal';
function IoBitDirIn; external RotorDLL name 'IoBitDirIn';
function IoBitDirOut; external RotorDLL name 'IoBitDirOut';
function IoSetOutBit; external RotorDLL name 'IoSetOutBit';
function IoClrOutBit; external RotorDLL name 'IoClrOutBit';
function IoGetBitDir; external RotorDLL name 'IoGetBitDir';
function IoOutBitVal; external RotorDLL name 'IoOutBitVal';

function NmcGetModType; external RotorDLL name 'NmcGetModType';
function NmcGetModVer; external RotorDLL name 'NmcGetModVer';
function ServoSetGain2; external RotorDLL name 'ServoSetGain2';
function ServoStopMotor; external RotorDLL name 'ServoStopMotor';
function NmcInit; external RotorDLL name 'NmcInit';
function NmcHardReset; external RotorDLL name 'NmcHardReset';
// -------------------------tested to here ----------------------

function ServoGetCmdPos; external RotorDLL name 'ServoGetCmdPos';
function ServoLoadTraj; external RotorDLL name 'ServoLoadTraj';
function ServoGetPError; external RotorDLL name 'ServoGetPError';
function ServoGetPos; external RotorDLL name 'ServoGetPos';
function ServoSetHoming; external RotorDLL name 'ServoSetHoming';
function ServoResetPos; external RotorDLL name 'ServoResetPos';

function NmcSendcmd; external RotorDLL name 'NmcSendcmd';

procedure NmcShutdown; external RotorDLL name 'NmcShutdown';
function NmcReadStatus; external RotorDLL name 'NmcReadStatus';

function NmcDefineStatus; external RotorDLL name 'NmcDefineStatus';

function NmcGetStat; external RotorDLL name 'NmcGetStat';

function NmcNoOp; external RotorDLL name 'NmcNoOp';

function NmcGetStatItems; external RotorDLL name 'NmcGetStatItems';

function ServoGetIoCtrl; external RotorDLL name 'ServoGetIoCtrl';
function ServoSetIoCtrl; external RotorDLL name 'ServoSetIoCtrl';
function ServoSetGain; external RotorDLL name 'ServoSetGain';
function ServoSetPos; external RotorDLL name 'ServoSetPos';
function ServoClearBits; external RotorDLL name 'ServoClearBits';
function ServoGetStat; external RotorDLL name 'ServoGetStat';
function ServoGetAux; external RotorDLL name 'ServoGetAux';

end.
