【CoppeliaSim4.3】C#调用 remoteApi控制场景中UR5

8f801a5c4d7b35283466817d27468cd7.png

截图

视频演示

笔记:

一、场景脚本中添加代码,开启服务器:

function sysCall_init()
    -- Start remote API server
    local port=3000
    simRemoteApi.start(port)
  
    corout=coroutine.create(coroutineMain)
end

二、C#客户度连接服务器,并获取关节句柄

_id = Api.Connect(textBoxAddress.Text, (int)numericUpDownPort.Value);
  if (Api.IsConnected(_id))
  {
      for (int i = 0; i < _jointHandle.Length; i++)
      {
          _jointHandle[i] = Api.GetObjectHandle(_id, _jointPrefix + (i + 1));
      }
  }

三、从安装目录拷贝include和remoteApi到C#项目,生成dll、lib

7eead20bf9b6afb57087707f29d0547d.png

目录源位置

a07e195bc98bee4dcbdef9a04ad61f4c.png

目录目标位置

2bc5967eceb497dc0bbdd0d771773eb3.png

0a84bee410101116de17dde3e8762411.png

生成的库拷贝到C#项目编译输出目录

四、C#调用C++库

using System;
using System.Runtime.InteropServices;


namespace CoppeliaSimCSharpAPI.TestForm
{
    internal class Api
    {
        [DllImport("CoppeliaSimCSharpAPI.dll")]
        public static extern int Connect(string address, int port);


        [DllImport("CoppeliaSimCSharpAPI.dll")]
        public static extern int Disconnect(int id);


        [DllImport("CoppeliaSimCSharpAPI.dll")]
        public static extern bool IsConnected(int id);


        [DllImport("CoppeliaSimCSharpAPI.dll")]
        public static extern int GetJointPosition(int id, int[] jointHandle, float[] position, int jointCount);


        [DllImport("CoppeliaSimCSharpAPI.dll")]
        public static extern int GetObjectHandle(int id, string objectName);


        [DllImport("CoppeliaSimCSharpAPI.dll")]
        public static extern int MoveJoint(int id, int[] jointHandle, float[] position, bool inTorqueForceMode, int jointCount);


        [DllImport("CoppeliaSimCSharpAPI.dll")]
        public static extern int SendInfo(int id, string message, bool blocking);
    }
}

五、移动机械臂和获取位置

private void buttonMove_Click(object sender, EventArgs e)
        {
            var position = new float[]
            {
                (float)numericUpDownJ1.Value,
                (float)numericUpDownJ2.Value,
                (float)numericUpDownJ3.Value,
                (float)numericUpDownJ4.Value,
                (float)numericUpDownJ5.Value,
                (float)numericUpDownJ6.Value
            };
            Api.MoveJoint(_id, _jointHandle, position, true, _jointHandle.Length);
        }


        private void buttonGetPosition_Click(object sender, EventArgs e)
        {
            var position = new float[6];
            Api.GetJointPosition(_id, _jointHandle, position, _jointHandle.Length);
            MessageBox.Show($"{position[0]}\r\n" +
                            $"{position[1]}\r\n" +
                            $"{position[2]}\r\n" +
                            $"{position[3]}\r\n" +
                            $"{position[4]}\r\n" +
                            $"{position[5]}",
                            "Now position");
        }

The End

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值