专栏首页CSharp编程大全C# GTS四轴运动控制器实例(固高科技步进电机不带编码器) -V1

C# GTS四轴运动控制器实例(固高科技步进电机不带编码器) -V1

注:由于电机不带编码器,无法做home和当前位置信息读取!

功能:

三个轴的点位运动:前进+后退,并分别显示每个轴的移动脉冲数(可以换算为距离)!

开发环境:VS2017

硬件设备:固高科技三维移动平台

在上一版上添加了如下功能:

  1. 记录每次运行的距离,即使关闭软件,也能记录当前距离原点的距离(原点首次得在机台选定)
  2. 运行超出机台的最大距离后停止运动.
  
using System;
using System.Windows.Forms;
using gts;
using System.Xml;
using System.Collections.Generic;


namespace 三维平移台控制系统
{
    public partial class mainFrom : DevComponents.DotNetBar.OfficeForm
    {
        public mainFrom()
{
            this.EnableGlass = false;
            InitializeComponent();
        }
        short Rtn;
        int  vel = 0;
        int[] pos= {0,0,0 };
        int[] pos1 = { 0, 0, 0 };
        bool flag = false;
        uint clk;
        double prfpos, prfvel, encpos, encvel;
        short AXIS = 1;

        int X = 0;
        int Y = 0;
        int Z = 0;


        private void buttonX1_Click(object sender, EventArgs e)
{
          
            mc.GT_ZeroPos(AXIS, 1);
            //for (int i=0; i<3;i++)
            //{
            //    pos1[i] += pos[i];
            //}
            pos1[AXIS - 1] += pos[AXIS - 1];
            pos[AXIS-1] = 0;
           
        }

        private void buttonX2_Click(object sender, EventArgs e)
{
            //Rtn = mc.GT_Close();
            Rtn = mc.GT_PrfTrap(AXIS);
            mc.TTrapPrm trapprm;
            mc.GT_GetTrapPrm(AXIS, out trapprm);
            trapprm.acc = 0.1;//设置加速度
            trapprm.dec = 0.1;//设置减速度
            trapprm.smoothTime = 1;
            mc.GT_SetTrapPrm(AXIS, ref trapprm);
            pos[AXIS-1] += Convert.ToInt32(textBoxX1.Text)*500;
            vel = Convert.ToInt32(textBoxX2.Text)*2;
            mc.GT_SetPos(AXIS, pos[AXIS-1]);
            mc.GT_SetVel(AXIS, vel);
            // mc.GT_Update(AXIS);
            mc.GT_Update(1 << (AXIS - 1));
            //MessageBox.Show((1 << (AXIS - 1)).ToString());
        }

        private void buttonX3_Click(object sender, EventArgs e)
{
            //Rtn = mc.GT_Close();
            Rtn = mc.GT_PrfTrap(AXIS);
            mc.TTrapPrm trapprm;
            mc.GT_GetTrapPrm(AXIS, out trapprm);
            trapprm.acc = 0.1;
            trapprm.dec = 0.1;
            trapprm.smoothTime = 1;
            mc.GT_SetTrapPrm(AXIS, ref trapprm);
            pos[AXIS-1] -= Convert.ToInt32(textBoxX1.Text)*500;
            vel = Convert.ToInt32(textBoxX2.Text)*2;
            mc.GT_SetPos(AXIS, pos[AXIS-1]);
            mc.GT_SetVel(AXIS, vel);
            //mc.GT_Update(AXIS);
            mc.GT_Update(1 << (AXIS - 1));
        }

        private void timer1_Tick(object sender, EventArgs e)
{
            //if ()
            //{
            //    mc.GT_GetPrfPos(AXIS, out prfpos, 1, out clk);// profile 起始轴号
            //    mc.GT_GetEncPos(AXIS, out encpos, 1, out clk);// encoder 起始轴号
            //    textBox3.Text = prfpos.ToString();
            //    textBox5.Text = encpos.ToString();
            //    mc.GT_GetPrfVel(AXIS, out prfvel, 1, out clk);// 起始规划轴号
            //    mc.GT_GetEncVel(AXIS, out encvel, 1, out clk);// encoder 起始轴号
            //    textBox4.Text = prfvel.ToString();
            //    textBox6.Text = encvel.ToString();
            //}
            List<KeyValuePair<string, string>> settings = new List<KeyValuePair<string, string>>();
            KeyValuePair<string, string> kv = new KeyValuePair<string, string>("", "");

            mc.GT_GetPrfPos(AXIS, out prfpos, 1, out clk);// profile 起始轴号
            mc.GT_GetPrfVel(AXIS, out prfvel, 1, out clk);// 起始规划轴号
            if (AXIS == 1)
            {
                labelX4.Text = "规划位置 : " + (prfpos / 500).ToString() + " mm";
                labelX5.Text = "规划速度 : " + (prfvel / 2).ToString() + " mm/s";
                kv = new KeyValuePair<string, string>("X", ((prfpos + pos1[0]) / 500 +X).ToString());
                settings.Add(kv);
                labelX10.Text = "距离原点 : " + ((prfpos + pos1[0])/500 + X).ToString() + " mm";
                if ((prfpos + pos1[0]) / 500 + X > 850)
                    mc.GT_Stop(AXIS, 0);

            }
            else if (AXIS == 2)
            {
                labelX6.Text = "规划位置 : " + (prfpos / 500).ToString() + " mm";
                labelX7.Text = "规划速度 : " + (prfvel / 2).ToString() + " mm/s";
                kv = new KeyValuePair<string, string>("Y", ((prfpos + pos1[1]) / 500 + Y).ToString());
                settings.Add(kv);
                labelX11.Text = "距离原点 : " + ((prfpos + pos1[1]) / 500 + Y).ToString() + " mm";
                if ((prfpos + pos1[1]) / 500 + Y > 500)
                    mc.GT_Stop(AXIS, 0);
            }
            else if (AXIS == 3)
            {
                labelX8.Text = "规划位置 : " + (prfpos / 500).ToString() + " mm";
                labelX9.Text = "规划速度 : " + (prfvel / 2).ToString() + " mm/s";
                kv = new KeyValuePair<string, string>("Z", ((prfpos + pos1[2]) / 500 + Z).ToString());
                settings.Add(kv);
                labelX12.Text = "距离原点 : " + ((prfpos + pos1[2]) / 500 + Z).ToString() + " mm";
                if ((prfpos + pos1[2]) / 500 + Z > 500)
                    mc.GT_Stop(AXIS, 0);

            }
            SaveSettings(settings);

        }
        private void ReadConfigXML()
{
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(AppDomain.CurrentDomain.BaseDirectory + "\\config.xml");
            XmlNode settingNode = xmlDoc.DocumentElement;

            XmlElement e = settingNode.SelectSingleNode("X") as XmlElement;
            if (e == null)
            {
                X = 0;
            }
            else
            {
                int val = 0;
                if (!int.TryParse(e.InnerText, out val))
                {
                    X = 0;
                }
                else
                {
                    X = val;
                }
            }

            e = settingNode.SelectSingleNode("Y") as XmlElement;
            if (e == null)
            {
                Y = 0;
            }
            else
            {
                int val = 0;
                if (!int.TryParse(e.InnerText, out val))
                {
                    Y = 0;
                }
                else
                {
                    Y = val;
                }
            }

            e = settingNode.SelectSingleNode("Z") as XmlElement;
            if (e == null)
            {
                Z = 0;
            }
            else
            {
                int val = 0;
                if (!int.TryParse(e.InnerText, out val))
                {
                    Z = 0;
                }
                else
                {
                    Z = val;
                }
            }


        }

        private void mainFrom_Load(object sender, EventArgs e)
{
            comboBoxEx1.SelectedIndex = 0;

            textBoxX1.Text = "4";
            textBoxX2.Text = "5";
           
           
            labelX4.Text = "规划位置 : " + (0).ToString() + " mm";
            labelX5.Text = "规划速度 : " + (0).ToString() + " mm/s";
           
           
            labelX6.Text = "规划位置 : " + (0).ToString() + " mm";
            labelX7.Text = "规划速度 : " + (0).ToString() + " mm/s";
           
            labelX8.Text = "规划位置 : " + (0).ToString() + " mm";
            labelX9.Text = "规划速度 : " + (0).ToString() + " mm/s";

          
            /*初始化*/
            Rtn = mc.GT_Open(0, 1);
            Rtn = mc.GT_Reset();
            Rtn = mc.GT_LoadConfig("GTS800.cfg"); ;
            Rtn = mc.GT_ClrSts(1, 8); //axis 起始轴号,count 

            ReadConfigXML();
            labelX10.Text = "距离原点 : " + (X).ToString() + " mm";
            labelX11.Text = "距离原点 : " + (Y).ToString() + " mm";
            labelX12.Text = "距离原点 : " + (Z).ToString() + " mm";

            labelX13.Text = "最大距离 : " + (850).ToString() + " mm";
            labelX14.Text = "最大距离 : " + (500).ToString() + " mm";
            labelX15.Text = "最大距离 : " + (500).ToString() + " mm";
            timer1.Enabled = true;
        }

        private void buttonX4_Click(object sender, EventArgs e)
{
            mc.GT_Stop(AXIS, 0);
        }

        private void comboBoxEx1_SelectedIndexChanged(object sender, EventArgs e)
{
            switch (comboBoxEx1.SelectedItem.ToString())
            {
                case "X轴":
                    AXIS = 1; break;
                case "Y轴":
                    AXIS = 2; break;
                case "Z轴":
                    AXIS = 3; break;
                default:break;
            }
        }

        public static void SaveSettings(List<KeyValuePair<string, string>> settings)
{
            try
            {
                string docPath = AppDomain.CurrentDomain.BaseDirectory + "\\config.xml";
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(docPath);
                XmlNode settingNode = xmlDoc.DocumentElement;
                if (settingNode == null)
                    return;

                foreach (var kv in settings)
                {
                    SetNodeValue(xmlDoc, settingNode, kv.Key, kv.Value);
                }

                xmlDoc.Save(docPath);
            }
            catch { }
        }

        private static void SetNodeValue(XmlDocument XmlDoc, XmlNode rootnode, string key, string value)
{
            try
            {
                XmlElement e = rootnode.SelectSingleNode(key) as XmlElement;
                if (e == null)
                {
                    XmlNode node = XmlDoc.CreateNode(XmlNodeType.Element, key, "");
                    node.InnerText = value;
                    rootnode.AppendChild(node);
                }
                else
                {
                    e.InnerText = value;
                }
            }
            catch { }
        }

    }
}


本文分享自微信公众号 - CSharp编程大全(y438679770),作者:zls365

原文出处及转载信息见文内详细说明,如有侵权,请联系 yunjia_community@tencent.com 删除。

原始发表时间:2019-12-30

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • C# GTS四轴运动控制器实例(固高科技步进电机不带编码器)

    zls365
  • C# byte[]转换为字符串

    zls365
  • C#3种常见的定时器(多线程)

    zls365
  • DFS--POJ 1190 生日蛋糕

    Description 7月17日是Mr.W的生日,ACM-THU为此要制作一个体积为Nπ的M层生日蛋糕,每层都是一个圆柱体。 设从下往上数第i(1 <= ...

    风骨散人Chiam
  • iOS实践:一步步实现星级评分1. 创建星星2. 优化3. 灵异事件

    stanbai
  • C# GTS四轴运动控制器实例(固高科技步进电机不带编码器)

    zls365
  • 手把手带你用Java实现点灯游戏(下篇)

    前面几篇文章用Java带大家一起了解了几个游戏小项目,感兴趣的小伙伴可以点击文章观摩下,手把手教你用Java打造一款简单故事书(上篇)、手把手教你用Java打造...

    用户8211341
  • 1083 是否存在相等的差 (20 分)

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

    韩旭051
  • cloudflare 默认证书导致 Let’s Encrypt 证书失效及网站打不开的解决办法

    魏艾斯博客www.vpsss.net
  • 用Python爬虫获取自己感兴趣的博客文章

    数据科学社区

扫码关注云+社区

领取腾讯云代金券