C# API Controls

If you have a question about the software please ask it here.

C# API Controls

Postby jdg26 » Wed Oct 25, 2023 4:09 pm

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!
jdg26
 
Posts: 12
Joined: Wed Oct 25, 2023 1:58 pm
Location: USA

Re: C# API Controls

Postby kig23 » Thu Oct 26, 2023 2:57 pm

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.
kig23
 
Posts: 161
Joined: Sat Mar 31, 2018 6:58 am
Location: Italy


Return to Ask a question from support here

Who is online

Users browsing this forum: Google [Bot] and 30 guests