Page 1 of 1

C# API Controls

PostPosted: Wed Oct 25, 2023 4:09 pm
by jdg26
Hi Guys,

We recently purchased the UCCNC Software and the UC100 motion controller.
Our hopes are to update our old production machines with new motion control software.

I've been playing around with the C# API and can connect\open the UC100 controller.
My questions are:
[list=]
[*] How to do a general axis move after updating positions\index?
[*] How to move 2-axis simultaneously?
[/list]

Here's my sample code:
Code: Select all
using System.Runtime.InteropServices;

namespace DemoMotionControlForm_
{
    public partial class Form1 : Form
    {

        private int devices;
        private String helpStr;

        public Form1()
        {
            InitializeComponent();
            helpStr = "ERROR WITH DEVICE!\nFOLLOW STEPS BELOW\n\n";
            helpStr += "1. Hit 'List Devices'\n2. Hit 'Get Device Info'\n3. Hit 'Open Device'";
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void btnMoveRight_Click(object sender, EventArgs e)
        {
            // might need a call to AddLinearMoveRel()
            // for moving 2axis check AddSyncMove()
            int xPos = 0;
            int yPos = 0;
            int zPos = 0;
            int aPos = 0;
            int bPos = 0;
            int cPos = 0;

            int ret = UC100.GetAxisIndex(ref xPos, ref yPos, ref zPos, ref aPos, ref bPos, ref cPos);
            if (ret != 0) {
                MessageBox.Show(helpStr);
                return;
            }
            else
                MessageBox.Show("Current X-Axis Index -> " + xPos);

            xPos += 2000;   // add 2000 steps
            ret = UC100.SetAxisIndex(xPos, yPos, zPos, aPos, bPos, cPos);
            if (ret != 0) {
                MessageBox.Show(helpStr);
                return;
            }
            else
                MessageBox.Show("New X-Axis Index -> " + xPos);


            UC100.AddLinearMoveRel(0, 10.0, 2000, 1.0, true);
            if (ret != 0)
            {
                MessageBox.Show(helpStr);
                return;
            }
            else
                MessageBox.Show("Trying to do linear move -> " + xPos);

        }

        private void btnMoveLeft_Click(object sender, EventArgs e)
        {
            // copy the moveRight logic over
            // change the dir pin to move left
            MessageBox.Show("Moving Motor left <- ");
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Stopping Motor");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            UC100.ListDevices(ref devices);
            MessageBox.Show(devices + " device(s) connected");
        }

        private void btnGetDeviceInfo_Click(object sender, EventArgs e)
        {
            int boardID = 1;
            int type = 0;
            int serialNumber = 0;

            int ret = UC100.DeviceInfo(boardID, ref type, ref serialNumber);
            if (ret != 0)
                MessageBox.Show(helpStr);
            else
                MessageBox.Show("Board ID " + boardID + "\nType " + type + "\nSerial Number " + serialNumber + "\nRet = " + ret);
        }

        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }

        private void btnOpenDevice_Click(object sender, EventArgs e)
        {
            int ret = UC100.Open(1);
            if (ret != 0)
                MessageBox.Show(helpStr);
            else
                MessageBox.Show("Device Connected\nRet " + ret);
        }

        private void btnCloseDevice_Click(object sender, EventArgs e)
        {
            int ret = UC100.Close();
            if (ret != 0)
                MessageBox.Show(helpStr);
            else
                MessageBox.Show("Device Disconnected\nRet " + ret);
        }

        private void btnSetMotorAxis_Click(object sender, EventArgs e)
        {
            int ret = 0, stepRate = 0;
            UC100.AxisSetting axisSetting = new UC100.AxisSetting
            {
                Axis = 0,           // X-Axis
                Enable = true,
                StepPin = 2,
                DirPin = 3,
                StepNeg = false,
                DirNeg = false,
                MaxAccel = 0,       // change
                MaxVel = 0,          // change
                StepPer = 0,        // change might be 8000
                HomePin = 0,        // change
                HomeNeg = false,
                LimitPPin = 0,
                LimitNPin = 0,
                LimitNNeg = false,
                SoftLimitP = 0,
                SoftLimitN = 0,
                SlaveAxis = 0,
                BacklashOn = false,
                BacklashDist = 0,
                CompAccel = 0,
                EnablePin = 4,
                EnablePinNeg = false,
                EnableDelay = 0,
                CurrentHiLowPin = 0,
                CurrentHiLowPinNeg = false,
                HomeBackOff = 0,
                RotaryAxis = false,
                RotaryRollover = false,
            };
            ret = UC100.SetAxisSetting(ref axisSetting);
            UC100.GetStepRate(ref stepRate);
           
            if (ret != 0)
                MessageBox.Show(helpStr);
            else
                MessageBox.Show("X-Axis Setup!\nRet " + ret + "\nStepRate = " + stepRate);
        }

    }
}


Cheers!

Re: C# API Controls

PostPosted: Thu Oct 26, 2023 2:57 pm
by kig23
Hi,
you can use AddLinearMove () or AddLinearMoveP () or look at this post :
https://www.forum.cncdrive.com/viewtopic.php?f=2&t=4308&p=29950&hilit=software+development#p29950
Dezsoe wrote an example that you can download. Hope this helps you.