工业4轴机器人C#控制

using rokae.clr;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace xCoreSDK_CSharp
{
    internal class PCB4RobotDemo
    {
        /// <summary>
        /// 工业4轴机型
        /// </summary>
        PCB4Robot robot = new PCB4Robot();

        // 工业三轴
        // PCB3Robot robot = new PCB3Robot();
        // 工业六轴
        // StandardRobot robot = new StandardRobot();
        
        /// <summary>
        /// 等待运动指令执行结束
        /// </summary>
        /// <param name="cmdID"></param>
        /// <param name="index"></param>
        void waitForFinish(String cmdID, int index)
        {
            ErrorCode ec;
            while (true)
            {
                var info = robot.queryEventInfo(Event.moveExecution, out ec);
                var _cmdID = (String)info["cmdID"];
                var _idx = (int)info["wayPointIndex"];
                var _err = (ErrorCode)info["error"];
                if (_err.value != 0)
                {
                    Console.WriteLine($"指令{_cmdID}:{_idx} 错误: {_err.message}");
                    return;
                }

                if (cmdID == _cmdID && _idx == index)
                {
                    Console.WriteLine($"指令 {cmdID}:{index} 已完成");
                    return;
                }

                Thread.Sleep(200);

            }
        }

        /// <summary>
        /// 示例 - 连接到机器人
        /// </summary>
        /// <param name="remoteIP">机器人地址</param>
        public void connect(string remoteIP)
        {
            robot.connectToRobot(remoteIP);
        }

        /// <summary>
        /// 示例 - 读取和设置软限位
        /// </summary>
        public void example_SoftLimit()
        {
            ErrorCode ec;
            double[,] limits = new double[4, 2] { { double.MaxValue, double.MaxValue }, { double.MaxValue, double.MaxValue },{ double.MaxValue, double.MaxValue },
                { double.MaxValue, double.MaxValue }};
           
            // 当limits为double类型数据最大值且enable为false时,代表关闭软限位
            robot.setSoftLimit(false, limits, out ec);
            PrintHelper.checkError("设置软限位", ec);

            bool v = robot.getSoftLimit(ref limits, out ec);
            Console.WriteLine($"软限位已打开 {v}");
            
            foreach (double i in limits)
            {
                Console.Write($"{i} ");
            }
            PrintHelper.checkError("读取软限位", ec);
            // 示例:设置2轴上限为 1.1
            // limits[1,1] = 1.1;
            robot.setSoftLimit(true, limits, out ec);
            
        }

        /// <summary>
        /// 示例 - 获取基本信息
        /// </summary>
        public void Example_BasicInfo()
        {
            ErrorCode ec;
            var info = robot.robotInfo(out ec);
            Console.WriteLine($"控制器版本 {info.version}, 机器人型号 {info.type}");
            var pos = robot.cartPosture(CoordinateType.flangeInBase, out ec);
            PrintHelper.checkError("位姿", ec);
            Console.Write("当前位姿 [");
            Array.ForEach(pos.trans, PrintHelper.printArray);
            Array.ForEach(pos.rpy, PrintHelper.printArray);
            Console.WriteLine("]");
        }

        /// <summary>
        /// 示例 - 运动指令
        /// </summary>
        public void Example_Move()
        {
            ErrorCode ec;
            robot.setOperateMode(OperateMode.automatic, out ec);
            robot.setPowerState(true, out ec);

            string id = "";
            // 设置工具工件坐标系
            Toolset defaultToolset = new Toolset();
            robot.setToolset(defaultToolset, out ec);
            MoveCommand movej1
                = new MoveCommand(), movej2 = new MoveCommand();

            // MoveJ指令的目标点
            // MoveJ1 X-0.448, Y-0, Z-0.4115, A-3.14, B-0.047, C-3.14
            movej1.cartTarget.trans[0] = 0.448;
            movej1.cartTarget.trans[1] = 0.0;
            movej1.cartTarget.trans[2] = 0.4115;
            movej1.cartTarget.rpy[0] = Math.PI;
            movej1.cartTarget.rpy[1] = 0.047;
            movej1.cartTarget.rpy[2] = Math.PI;

            // MoveJ2 X-0.502, Y-0, Z-0.63, A-3.14, B-1.416, C-3.14
            movej2.cartTarget.trans[0] = 0.502;
            movej2.cartTarget.trans[1] = 0.0;
            movej2.cartTarget.trans[2] = 0.63;
            movej2.cartTarget.rpy[0] = Math.PI;
            movej2.cartTarget.rpy[1] = 1.4167484;
            movej2.cartTarget.rpy[2] = Math.PI;
            // 沿外部工件坐标系X+偏移0.01
            movej2.cartTargetOffset.type = CartesianPosition.Offset.Type.offs;
            movej2.cartTargetOffset.frame.trans[0] = 0.01;

            var cmds = new List<MoveCommand> { movej1, movej2 };
            robot.moveAppend(MoveCommand.Type.MoveJ, cmds, ref id, out ec);
            robot.moveStart(out ec);
            waitForFinish(id, cmds.Count - 1);
        }
    }
}
 

相关推荐

  1. 工业4机器人C#控制

    2023-12-08 23:04:03       34 阅读
  2. C#与雷赛运动控制卡的使用(二) - 控制系统

    2023-12-08 23:04:03       31 阅读
  3. SEW多变频MOVIAXIS控制说明

    2023-12-08 23:04:03       35 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-08 23:04:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-08 23:04:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-08 23:04:03       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-08 23:04:03       20 阅读

热门阅读

  1. 举例说明自然语言处理(NLP)技术。

    2023-12-08 23:04:03       33 阅读
  2. go 爬虫 todo

    2023-12-08 23:04:03       44 阅读
  3. 在Redis中设置一个键值对并为其指定过期时间

    2023-12-08 23:04:03       32 阅读
  4. 集成学习(Ensemble Learning)

    2023-12-08 23:04:03       29 阅读
  5. GO设计模式——9、过滤器模式(结构型)

    2023-12-08 23:04:03       37 阅读
  6. 图像平滑、降噪、滤波、采样浅谈

    2023-12-08 23:04:03       39 阅读
  7. HTML程序大全(1):简易计算器

    2023-12-08 23:04:03       41 阅读
  8. vs qt widget application 槽函数和信号

    2023-12-08 23:04:03       31 阅读
  9. Ubuntu22.04 交叉编译fdk-aac for Rv1106

    2023-12-08 23:04:03       29 阅读
  10. 如何在Python中使用一行代码编写for循环

    2023-12-08 23:04:03       41 阅读
  11. C/C++ Linux 终端 高亮调试

    2023-12-08 23:04:03       39 阅读
  12. SpringMVC常用注解

    2023-12-08 23:04:03       37 阅读