Dobot Magicial 用 DobotDll32.pas 2020/03/12~2020/07/28

・2020/03/18 使用例に Dobot 接続、切断を追加
・2020/07/28 DobotDll ラッパーのリンクを追加


■DobotDll を Delphi で使うユニットです。
 下の方に、コメントアウトしたコマンド終了待ちのサンプルコードがあります。


■ダウンロード
 dobotDll3.pas(拡張子が変わることがあります。)


※最近見つけた「DoboDllラッパー」。こちらの方が上級者っぽい。
 https://github.com/norgepaul/DelphiDobotAPI


{
  DobotDll32.pas + Delphi サンプル
  EXE と同じフォルダに下記 DLL が必要
    ・DobotDll.dll (32bit)
    ・Qt5Core.dll
    ・Qt5Network.dll
    ・Qt5SerialPort.dll
}

unit DobotDll32;

interface

uses
  Winapi.Windows;

// for Dobot *******************************************************************
type
  TKinematics = packed record
    velocity: single;
    acceleration: single;
  end;
  TPTPCommonParams = packed record
    velocityRatio     : single;
    accelerationRatio : single;
  end;
  TPTPCoordinateParams = packed record
    xyzVelocity     : single;
    rVelocity       : single;
    xyzAcceleration : single;
    rAcceleration   : single;
  end;
  TPTPJointParams = packed record
    velocity     : array [0..3] of single;
    acceleration : array [0..3] of single;
  end;
  TPTPJumpParams = packed record
    jumpHeight    : single;
    maxJumpHeight : single; // Z Limit
  end;
  TPTPCmd = packed record
    ptpMode : Byte;
    x : single;
    y : single;
    z : single;
    r : single;
  end;
  THOMEParams = packed record
    x : single;
    y : single;
    z : single;
    r : single;
  end;
  THOMECmd =  packed record
    rserved : Uint32; // = Cardinal
  end;
  TEndEffectorParams = packed record
    xBias : single;
    yBias : single;
    zBias : single;
  end;
  TWAITCmd = packed record
    timeout : Uint32;  // Unit ms
  end;

  TPose = packed record
    x : single;
    y : single;
    z : single;
    r : single;
    jointAngle : array [0..3] of single;
  end;
  TJOGJointParams = packed record
    velocity     : array [0..3] of single;
    acceleration : array [0..3] of single;
  end;
  TJOGCoordinateParams = packed record
    velocity     : array [0..3] of single;
    acceleration : array [0..3] of single;
  end;
  TJOGCommonParams = packed record
    velocityRatio     : single;
    accelerationRatio : single;
  end;
  TJOGCmd = packed record
    isJoint : Byte;
    cmd     : Byte;
  end;
  TCPParams = packed record
        planAcc: Single;
        juncitionVel: Single;
        acc: Single;
        realTimeTrack: Byte;
  end;
  TContinuousPathMode = (
        CPRelativeMode,
        CPAbsoluteMode
  );
  TCPCmd = packed record
        cpMode: Byte;
        x: Single;
        y: Single;
        z: Single;
        velocity: Single;
  end;
  TIOFunction = (
        IOFunctionDummy,
        IOFunctionDO,
        IOFunctionPWM,
        IOFunctionDI,
        IOFunctionADC
  );
  TIOMultiplexing = packed record
    address: Byte;
    multiplex: Byte;
  end;
  TIODO = packed record
    address : Byte;
    level : Byte;
  end;
  TIOPWM = packed record
    address: Byte;
    frequency: single;
    dutyCycle: single;
  end;
  TIODI = packed record
    address: Byte;
    level: Byte;
  end;

  TIOADC = packed record
    address: Byte;
    value: UInt16;
  end;
  {
  TPTPMode = (
    JUMP_XYZ,
    MOVJ_XYZ,
    MOVL_XYZ,
    JUMP_ANGLE,
    MOVJ_ANGLE,
    MOVL_ANGLE,
    MOVJ_INC,
    MOVL_INC,
    MOVJ_XYZ_INC,
    JUMP_MOVL_XYZ
  );
  }
  // Win32 はこちらが正解みたい
  TPTPMode = (
    JUMP_XYZ,
    MOVJ_XYZ,
    MOVL_XYZ,
    JUMP_ANGLE,
    MOVJ_ANGLE,
    MOVL_ANGLE,
    MOVJ_INC,
    MOVL_INC
  );
  TJOGMode = (
    IDEL,       //Invalid status
    AP_DOWN,    // X+/Joint1+
    AN_DOWN,    // X-/Joint1-
    BP_DOWN,    // Y+/Joint2+
    BN_DOWN,    // Y-/Joint2-
    CP_DOWN,    // Z+/Joint3+
    CN_DOWN,    // Z-/Joint3-
    DP_DOWN,    // R+/Joint4+
    DN_DOWN     // R-/Joint4-
             );
  TJOGModel = (COORDINATE_MODEL, JOINT_MODEL);

  TARCParams = packed record
    xyzVelocity : single;
    rVelocity : single;
    xyzAcceleration : single;
    rAcceleration : single;
  end;
  TARCCmd = packed record
    arcPointX : single;
    arcPointY : single;
    arcPointZ : single;
    arcPointR : single;
    toPointX  : single;
    toPointY  : single;
    toPointZ  : single;
    toPointR  : single;
  end;

// *****************************************************************************
// DobotDll.dll (32bit)

function ConnectDobot(portName: PAnsiChar ; baudrate: Uint32; fwType: PAnsiChar ; version: PAnsiChar ): Int32; stdcall; external 'DobotDll.dll';
function DisconnectDobot(): Uint32; stdcall; external 'DobotDll.dll';

// ????
function  SearchDobot(dobotList: PAnsiChar; maxLen: UInt32): Int32; stdcall; external 'DobotDll.dll';

function DobotExec(): Int32; stdcall; external 'DobotDll.dll';
function SetCmdTimeout(cmdTimeout: UInt32): Int32; stdcall; external 'DobotDll.dll';

//  Queued command control
function SetQueuedCmdStartExec(): Int32; stdcall; external 'DobotDll.dll';
function SetQueuedCmdStopExec(): Int32; stdcall; external 'DobotDll.dll';
function SetQueuedCmdForceStopExec(): Int32; stdcall; external 'DobotDll.dll';
function SetQueuedCmdStartDownload(totalLoop: UInt32; linePerLoop: UInt32): Int32; stdcall; external 'DobotDll.dll';
function SetQueuedCmdStopDownload(): Int32; stdcall; external 'DobotDll.dll';
function SetQueuedCmdClear(): Int32; stdcall; external 'DobotDll.dll';
function GetQueuedCmdCurrentIndex(var executedCmdIndex: Uint64): Int32; stdcall; external 'DobotDll.dll';

//  SN information
function GetDeviceSN(deviceSN: PAnsiChar; maxLen: UInt32): Int32; stdcall; external 'DobotDll.dll';

// Device information
function SetDeviceName(deviceName: PAnsiChar): Int32; stdcall; external 'DobotDll.dll';
function GetDeviceName(deviceName: PAnsiChar; maxLen: Uint32): Int32; stdcall; external 'DobotDll.dll';

// public static?extern functions
function SetEndEffectorParams(var endEffectorParams: TEndEffectorParams; var isQueued: Bool; queuedCmdIndex: UInt64): Int32; stdcall; external 'DobotDll.dll';
function GetEndEffectorParams(var endEffectorParams: TEndEffectorParams): Int32; stdcall; external 'DobotDll.dll';
function SetEndEffectorLaser(enableCtrl: Bool; isOn: Bool; isQueued: Bool; var queuedCmdIndex: UInt64): Int32; stdcall; external 'DobotDll.dll';
function GetEndEffectorLaser(var isCtrlEnabled: Bool; var isOn:Bool): Int32; stdcall; external 'DobotDll.dll';
function SetEndEffectorSuctionCup(enableCtrl: Bool; suck: Bool; isQueued: Bool; var queuedCmdIndex: Uint64): Int32; stdcall; external 'DobotDll.dll';
function GetEndEffectorSuctionCup(var isCtrlEnabled: Bool; var isSucked: Bool): Int32; stdcall; external 'DobotDll.dll';
function SetEndEffectorGripper(enableCtrl: Bool; grip: Bool; isQueued: Bool; var queuedCmdIndex: UInt64): Int32; stdcall; external 'DobotDll.dll';
function GetEndEffectorGripper(var isCtrlEnabled: Bool; var isGripped: Bool): Int32;  stdcall; external 'DobotDll.dll';

// InitialPose
function ResetPose(manual: Bool; rearArmAngle: Single;  frontArmAngle: Single): Int32; stdcall; external 'DobotDll.dll';

// Pose and Kinematics parameters are automatically get
function GetPose(var pose: TPose): Int32; stdcall; external 'DobotDll.dll';
function GetKinematics(var kinematics: TKinematics): Int32; stdcall; external 'DobotDll.dll';
function GetPoseL(var l: single): Int32; stdcall; external 'DobotDll.dll';

//  HOME
function SetHOMEParams(var homeParams: THOMEParams; isQueued: Bool; var queuedCmdIndex: UInt64): Int32; stdcall; external 'DobotDll.dll';
function GetHOMEParams(var homeParams: THOMEParams): Int32;stdcall; external 'DobotDll.dll';
function SetHOMECmd(var homeCmd: THOMECmd; isQueued: Bool; var queuedCmdIndex: UInt64): Int32; stdcall; external 'DobotDll.dll';

// Jog functions
function SetJOGJointParams(var jointJogParams: TJOGJointParams; isQueued: Bool; var queuedCmdIndex: UInt64): Int32; stdcall; external 'DobotDll.dll';
function GetJOGJointParams(var jointJogParams: TJOGJointParams): Int32; stdcall; external 'DobotDll.dll';
function SetJOGCoordinateParams(var coordinateJogParams: TJOGCoordinateParams; isQueued: Bool; var queuedCmdIndex: UInt64): Int32; stdcall; external 'DobotDll.dll';
function GetJOGCoordinateParams(var coordinateJogParams: TJOGCoordinateParams): Int32; stdcall; external 'DobotDll.dll';
function SetJOGCommonParams(var jogCommonParams: TJOGCommonParams; isQueued: Bool; var queuedCmdIndex: UInt64): Int32; stdcall; external 'DobotDll.dll';
function GetJOGCommonParams(var jogCommonParams: TJOGCommonParams): Int32; stdcall; external 'DobotDll.dll';
function SetJOGCmd(var jogCmd: TJogCmd; isQueued: Bool; var queuedCmdIndex: UInt64): Int32; stdcall; external 'DobotDll.dll';

// PTP functions
function SetPTPJointParams(var ptpJointParams : TPTPJointParams; isQueued: Bool; var queuedCmdIndex: UInt64): Int32; stdcall; external 'DobotDll.dll';
function GetPTPJointParams(var ptpJointParams: TPTPJointParams): Int32; stdcall; external 'DobotDll.dll';
function SetPTPCoordinateParams(var ptpCoordinateParams: TPTPCoordinateParams; isQueued: Bool; var queuedCmdIndex: UInt64): Int32; stdcall; external 'DobotDll.dll';
function GetPTPCoordinateParams(var ptpCoordinateParams: TPTPCoordinateParams): Int32; stdcall; external 'DobotDll.dll';
function SetPTPJumpParams(var ptpJumpParams: TPTPJumpParams; isQueued: Boolean; var queuedCmdIndex: UInt64): Int32; stdcall; external 'DobotDll.dll';
function GetPTPJumpParams(var ptpJumpParams: TPTPJumpParams): Int32; stdcall; external 'DobotDll.dll';
function SetPTPCommonParams(var ptpCommonParams: TPTPCommonParams; isQueued: Bool; var queuedCmdIndex: UInt64): Int32; stdcall; external 'DobotDll.dll';
function GetPTPCommonParams(var ptpCommonParams: TPTPCommonParams): Int32; stdcall; external 'DobotDll.dll';
function SetPTPCmd(var ptpCmd: TPTPCmd; isQueued: Bool; var queuedCmdIndex: UInt64): Int32; stdcall; external 'DobotDll.dll';

// CP functions
function SetCPParams(var cpParams: TCPParams; isQueued: Bool; var queuedCmdIndex: UInt64): Int32; stdcall; external 'DobotDll.dll';
function GetCPParams(var cpParams: TCPParams): Int32; stdcall; external 'DobotDll.dll';
function SetCPCmd(var cpCmd: TCPCmd; isQueued: Bool; var queuedCmdIndex: UInt64): Int32; stdcall; external 'DobotDll.dll';

// WAIT
function SetWAITCmd(var waitCmd: TWAITCmd; isQueued: Bool; var queuedCmdIndex: UInt64): Int32; stdcall; external 'DobotDll.dll';

// EIO
function SetIOMultiplexing(var ioMultiplexing: TIOMultiplexing; isQueued: Bool; var queuedCmdIndex: Uint64): Int32; stdcall; external 'DobotDll.dll';
function GetIOMultiplexing(var ioMultiplexing: TIOMultiplexing): Int32; stdcall; external 'DobotDll.dll';
function SetIODO(var ioDO: TIODO; isQueued: Bool; var queuedCmdIndex: uint64): Int32; stdcall; external 'DobotDll.dll';
function GetIODO(var ioDO: TIODO): Int32; stdcall; external 'DobotDll.dll';
function SetIOPWM(var ioPWM : TIOPWM; isQueued: Bool;var queuedCmdIndex:  uint64): Int32; stdcall; external 'DobotDll.dll';
function GetIOPWM(var ioPWM: TIOPWM): Int32; stdcall; external 'DobotDll.dll';
function GetIODI(var ioDI: TIODI): Int32; stdcall; external 'DobotDll.dll';
function GetIOADC(var ioADC: TIOADC): Int32; stdcall; external 'DobotDll.dll';

// Alarm
function GetAlarmsState(var alarmsState: Byte; var len: UInt32; maxLen: UInt32): Int32; stdcall; external 'DobotDll.dll';
function ClearAllAlarmsState: Int32; stdcall; external 'DobotDll.dll';

// Arc
function SetARCParams(var arcParams: TARCParams; isQueued: Bool; var queuedCmdIndex: UInt64): Int32; stdcall; external 'DobotDll.dll';
function GetARCParams(var arcParams: TARCParams): Int32; stdcall; external 'DobotDll.dll';
function SetARCCmd(var arcCmd: TARCCmd; isQueued: Bool; var queuedCmdIndex: UInt64): Int32; stdcall; external 'DobotDll.dll';

// *****************************************************************************

implementation

// -----------------------------------------------------------------------------
// Dobot Exsamples
// -----------------------------------------------------------------------------
{
procedure TForm5.SpeedButton11Click(Sender: TObject);
// Dobot 接続
var
  ret1 : integer;
  fwType, version, deviceSN: array [0..63] of AnsiChar;
  x, y, z, r: single;
begin
 IsDobotConnected := false;
  // fwType は省略不可, version は nil で省略可
  ret1 :=  ConnectDobot('', 115200, fwType, version);//nil);
  if ret1 = 0 then begin
    if GetDeviceSN(deviceSN, 64) = 0 then begin
      x := 200;
      y := 0;
      z := 0;
      r := 0;
      if dobotSetHomeParams(x, y, z, r) then begin
        IsDobotConnected := true;
      end;
    end;
  end;
end;

procedure TForm5.SpeedButton12Click(Sender: TObject);
// Dobot 切断
begin
  DisconnectDobot();
  IsDobotConnected := False;
end;

function movePTP(x, y, z, r: single): boolean;
var
  Pose: TPose;
  PTPCmd: TPTPCmd;
  queuedCmdIndex : Uint64;
  executedCmdIndex : UInt64;
  ret : integer;
begin
  result := False;
  executedCmdIndex := 0;
  if IsDobotConnected then begin
    PTPCmd.ptpMode := (Ord(MOVL_XYZ));

    PTPCmd.x := x;
    PTPCmd.y := y;
    PTPCmd.z := z;
    PTPCmd.r := r;
    ret := SetPTPCmd(PTPCmd, True, queuedCmdIndex);
    if ret = 0 then begin
      // 完了待ち
      while (executedCmdIndex < queuedCmdIndex) do begin
        GetQueuedCmdCurrentIndex(executedCmdIndex);
        GetPose(pose);
        //Form4.Memo1.Lines.Add(Format('x = %f, y = %f, z = %f, r = %f',[pose.x, pose.y, pose.z, Pose.r]));
        Form4.Edit31.Text := Format('%.2f',[pose.x]);
        Form4.Edit32.Text := Format('%.2f',[pose.y]);
        Form4.Edit33.Text := Format('%.2f',[pose.z]);
        Form4.Edit34.Text := Format('%.2f',[pose.jointAngle[0]]);

      end;
      GetPose(pose);
      //Form4.Memo1.Lines.Add(Format('x = %f, y = %f, z = %f, r = %f',[pose.x, pose.y, pose.z, Pose.r]));
      Form4.Edit31.Text := Format('%.2f',[pose.x]);
      Form4.Edit32.Text := Format('%.2f',[pose.y]);
      Form4.Edit33.Text := Format('%.2f',[pose.z]);
      Form4.Edit34.Text := Format('%.2f',[pose.jointAngle[0]]);

      Form4.Memo1.Lines.Add('>> move PTP End <<');
      result := True;
    end;
  end;
end;

function moveArc(arcX, arcY, arcZ, arcR, toX, toY, toZ, toR: single): boolean;
var
  pose : TPose;
  arcCmd : TARCCmd;
  queuedCmdIndex : UInt64;
  executedCmdIndex : UInt64;
  ret : integer;
begin
  result := False;
  executedCmdIndex := 0;
  if IsDobotConnected then begin

    // Z も円弧状態で動く
    arcCmd.arcPointX := arcX;
    arcCmd.arcPointY := arcY;
    arcCmd.arcPointZ := arcZ;
    arcCmd.arcPointR := arcR;

    arcCmd.toPointX := toX;
    arcCmd.toPointY := toY;
    arcCmd.toPointZ := toZ;
    arcCmd.toPointR := toR;

    ret := SetARCCmd(arcCmd, true, queuedCmdIndex);
    if ret = 0 then begin
      // 完了待ち
      while (executedCmdIndex < queuedCmdIndex) do begin
        GetQueuedCmdCurrentIndex(executedCmdIndex);
        GetPose(pose);
        Form4.Memo1.Lines.Add(Format('x = %f, y = %f, z = %f, r = %f', [pose.x, pose.y, pose.z, Pose.r]));
      end;
       Form4.Memo1.Lines.Add('>> move ARC End <<');
      result := True;
    end;
  end;
end;

function setSuctionCup(onFlag : boolean): boolean;
// SuctionCup ON / OFF
var
  suck : LongBool;
  queuedCmdIndex : Uint64;
  ret : integer;
begin
  result := False;
  suck := onFlag;
  queuedCmdIndex := 0;
  if IsDobotConnected then begin
    ret := SetEndEffectorSuctionCup(true, suck, true, queuedCmdIndex);
    result := ret = 0;
  end;
end;
}
end.