Help with the c++ API

Post anything you want to discuss with others about the software.

Re: Help with the c++ API

Postby TheButcher » Wed Aug 21, 2024 9:22 am

sure, my bad:
I put the header file into the workspace in a folder called source and tried around a bit.
Got the list devices Command working. I cant figure out how to move the machine tho

Code: Select all
#include <cstdio>
#include <windows.h>
#include <iostream>
#include "source/UC100.h"

typedef int (__stdcall *AddLinMoveFP)(double, double, double, double, double, double, double, double);
typedef int (*ListDevicesFP)(int*);
typedef int (*SetEstopSettingFP)(int, bool);
typedef int (*SetEstopStateFP)();
typedef int(* DeviceInfoFP)(int, int*, int*);
typedef int(* OpenFP)(int);
typedef int(*GetStatusFP)(Stat *SStat);
typedef int(*StopFP)();

int main() {
    // load the UC100 DLL
    HINSTANCE hGetProcLib = LoadLibrary(R"(C:\UCCNC\API\DLL\UC100.dll)");
    if (!hGetProcLib) {
        printf("could not load the dynamic library\n");
        return 1;
    }

    // get the function we want, ListDevices in this example
    AddLinMoveFP linMove = (AddLinMoveFP)GetProcAddress(hGetProcLib, "AddLinearMoveRel");
    ListDevicesFP listDevices = (ListDevicesFP) GetProcAddress(hGetProcLib, "ListDevices");
    SetEstopSettingFP SetEstopSetting = (SetEstopSettingFP) GetProcAddress(hGetProcLib, "SetEstopSetting");
    OpenFP Open = (OpenFP) GetProcAddress(hGetProcLib, "Open");
    GetStatusFP GetStatus = (GetStatusFP) GetProcAddress(hGetProcLib, "GetStatus");
    StopFP Stop = (StopFP) GetProcAddress(hGetProcLib, "Stop");
    SetEstopStateFP SetEstopState = (SetEstopStateFP) GetProcAddress(hGetProcLib, "SetEstopState");
    if (!linMove) {
        printf("could not locate the function\n");
        return 1;
    }

    // call the DLL function
    int x = 0;
    Stat res_stat;
    int r2 = listDevices(&x);
    int r4 = Open(1);
    //int r6 = Stop();
    //int r3 = SetEstopSetting(11, false);
    int r5 = GetStatus(&res_stat);
    //int r = linMove(1000, 1000, 1000, 0, 0, 0, 1000, 8);
    int id1 = linMove(0, 0, 0, 0, 0, 0, 500, 9);
    int id2 = linMove(50, 0, 0, 0, 0, 0, 500, 9);

    std::cout << "ID1: " << id1 << " ID2: " << id2 << std::endl;
    FreeLibrary(hGetProcLib);
    std::cout << "Device count: " << x << std::endl;
    std::cout << "estop: " << res_stat.Estop << std::endl;
    std::cout << "Idle: " << res_stat.Idle << std::endl;
    //printf("%d\n", r4);

    return 0;
}
TheButcher
 
Posts: 11
Joined: Mon Jul 22, 2024 11:12 am

Re: Help with the c++ API

Postby jdg26 » Wed Aug 21, 2024 12:30 pm

This looks OK, however the machine is not moving because you are a missing a call to SetAxisSetting().
You need to set your stepper motor axis with the STEP, DIR, ENA, etc.. pins with the SetAxisSetting() function.

I would call this after you open communication with the UC100, i.e after Open(), and before you start calling the AddLinearMove functions.
And definitely check the return values for all the UC100 calls!
jdg26
 
Posts: 11
Joined: Wed Oct 25, 2023 1:58 pm
Location: USA

Re: Help with the c++ API

Postby ger21 » Wed Aug 21, 2024 5:02 pm

ok, ive played around a bit and came to the conclusion, that the program somehow isnt connected to the UC100 driver/the software.
What is the general workflow for using the machine via c++?


The API is for those that want to write their own control software.
Gerry
UCCNC 2022 Screenset - http://www.thecncwoodworker.com/2022.html
ger21
 
Posts: 2714
Joined: Sat Sep 03, 2016 2:17 am

Re: Help with the c++ API

Postby TheButcher » Fri Aug 23, 2024 1:36 pm

Thanks jdg26!
The machine started making noises but doesnt move. Can you help me?
Code: Select all
#include <cstdio>
#include <windows.h>
#include <iostream>
#include "source/UC100.h"

typedef int (__stdcall *AddLinMoveFP)(double, double, double, double, double, double, double, double);
typedef int (*ListDevicesFP)(int*);
typedef int (*SetEstopSettingFP)(int, bool);
typedef int (*SetEstopStateFP)();
typedef int(* DeviceInfoFP)(int, int*, int*);
typedef int(* OpenFP)(int);
typedef int(*GetStatusFP)(Stat *SStat);
typedef int(*StopFP)();
typedef int(*SetMotionProgressStateFP)(bool);
typedef int(*SetAxisSettingFP)(AxisSetting);
typedef int(*AddLinearMoveRelFP)(int, double, int, double, bool);

int main() {
    // load the UC100 DLL
    HINSTANCE hGetProcLib = LoadLibrary(R"(C:\UCCNC\API\DLL\UC100.dll)");
    if (!hGetProcLib) {
        printf("could not load the dynamic library\n");
        return 1;
    }

    // get the function we want, ListDevices in this example
    auto AddLinMove = (AddLinMoveFP)GetProcAddress(hGetProcLib, "AddLinearMoveRel");
    auto listDevices = (ListDevicesFP) GetProcAddress(hGetProcLib, "ListDevices");
    auto SetEstopSetting = (SetEstopSettingFP) GetProcAddress(hGetProcLib, "SetEstopSetting");
    auto Open = (OpenFP) GetProcAddress(hGetProcLib, "Open");
    auto GetStatus = (GetStatusFP) GetProcAddress(hGetProcLib, "GetStatus");
    auto Stop = (StopFP) GetProcAddress(hGetProcLib, "Stop");
    auto SetEstopState = (SetEstopStateFP) GetProcAddress(hGetProcLib, "SetEstopState");
    auto SetMotionProgressState = (SetMotionProgressStateFP) GetProcAddress(hGetProcLib, "SetMotionProgressState");
    auto SetAxisSetting = (SetAxisSettingFP) GetProcAddress(hGetProcLib, "SetAxisSetting");
    auto AddLinearMoveRel = (AddLinearMoveRelFP) GetProcAddress(hGetProcLib, "AddLinearMoveRel");


    /*
     * typedef struct AxisSetting {
   int      Axis;         //Axis number (X=0,Y=1,Z=2,A=3,B=4,C=5).
   bool   Enable;         //Enables the axis.
   int      ;      //Step output pin.
   int      DirPin;         //Direction output pin.
   bool   StepNeg;      //Inverts the step pin.
   bool   DirNeg;         //Inverts the dir pin.
   double   MaxAccel;      //Acceleration parameter of the axis in Units/sec^2.
   double   MaxVel;         //Velocity parameter of the axis in Units/sec.
   double   StepPer;      //Steps per Units parameter.
   int      HomePin;      //Home input pin of the axis.
   bool   HomeNeg;      //Inverts the home input.
   int      LimitPPin;      //Positive side end-limit input pin.
   bool   LimitPNeg;      //Inverts the positive side end-limit input.
   int      LimitNPin;      //Negative side end-limit input pin.
   bool   LimitNNeg;      //Inverts the negative side end-limit input.
   double   SoftLimitP;      //Positive side software end-limit.
   double   SoftLimitN;      //Negative side software end-limit.
   int      SlaveAxis;      //Slave axis, only for the XYZ axes. (0=No slave, 3=A slave, 4=B slave, 5=C slave).
   bool   BacklashOn;      //Enables the backlash compensation for the axis.
   double   BacklashDist;   //Backlash distance in Units.
   double   CompAccel;      //Compensation acceleration for backlash and thread cutting in Units/sec^2.
   int      EnablePin;      //Axis enable output pin.
   bool   EnablePinNeg;   //Inverts the axis enable output.
   int      EnableDelay;   //Delays the enable output 0-255 value (x10msec).
   int      CurrentHiLowPin;   //Current hi/low output pin.
   bool   CurrentHiLowPinNeg;   //Inverts the current hi/low output.
   double   HomeBackOff;   //Home back off distance in Units.
   bool   RotaryAxis;      //Enables the rotary axis function for the axis. Works for the A,B,C axes only.
   bool   RotaryRollover;   //Enables the rollover for rotary axis on 360 degrees. Works for the A,B,C axes only and if the rotary axis function is enabled.
}AxisSetting;
     */
    AxisSetting x_axis, y_axis, z_axis, a_axis;
    x_axis.Axis = 0;
    x_axis.Enable = true;
    x_axis.StepPin = 3;
    x_axis.DirPin = 2;
    x_axis.MaxAccel = 400;
    x_axis.MaxVel = 4200;
    x_axis.StepPer = 533;
    x_axis.HomePin = 12;
    x_axis.LimitPPin = 0;
    x_axis.LimitNPin = 12;
    x_axis.SoftLimitP = 298;
    x_axis.SoftLimitN = 0;
    x_axis.EnablePin = 0;
    x_axis.CurrentHiLowPin = 0;

    y_axis.Axis = 1;
    y_axis.Enable = true;
    y_axis.StepPin = 5;
    y_axis.DirPin = 4;
    y_axis.MaxAccel = 400;
    y_axis.MaxVel = 4200;
    y_axis.StepPer = 533;
    y_axis.HomePin = 12;
    y_axis.LimitPPin = 0;
    y_axis.LimitNPin = 12;
    y_axis.SoftLimitP = 415;
    y_axis.SoftLimitN = 0;
    y_axis.EnablePin = 0;
    y_axis.CurrentHiLowPin = 0;

    z_axis.Axis = 2;
    z_axis.Enable = true;
    z_axis.StepPin = 7;
    z_axis.DirPin = 6;
    z_axis.MaxAccel = 400;
    z_axis.MaxVel = 4200;
    z_axis.StepPer = 533;
    z_axis.HomePin = 12;
    z_axis.LimitPPin = 0;
    z_axis.LimitNPin = 12;
    z_axis.SoftLimitP = -132;
    z_axis.SoftLimitN = 0;
    z_axis.EnablePin = 0;
    z_axis.CurrentHiLowPin = 0;

    a_axis.Axis = 3;
    a_axis.Enable = true;
    a_axis.StepPin = 9;
    a_axis.DirPin = 8;
    a_axis.MaxAccel = 200;
    a_axis.MaxVel = 3600;
    a_axis.StepPer = 480;
    a_axis.HomePin = 12;
    a_axis.LimitPPin = 0;
    a_axis.LimitNPin = 12;
    a_axis.SoftLimitP = -1000000;
    a_axis.SoftLimitN = 1000000;
    a_axis.EnablePin = 0;
    a_axis.CurrentHiLowPin = 0;

    // call the DLL function
    int x = 0;
    Stat res_stat;
    int r2 = listDevices(&x);
    std::cout << "R2: " << r2 << std::endl;
    int r4 = Open(1);

    SetAxisSetting(x_axis);
    SetAxisSetting(y_axis);
    SetAxisSetting(z_axis);
    SetAxisSetting(a_axis);

    SetMotionProgressState(false);
    //int r6 = Stop();
    //int r3 = SetEstopSetting(11, false);
    int r5 = GetStatus(&res_stat);
    //int r = linMove(1000, 1000, 1000, 0, 0, 0, 1000, 8);
    int id1 = AddLinMove(0, 0, 0, 0, 0, 0, 0, 0); //  AddLinearMove(double x,double y,double z,double a,double b,double c, double Feed, int ID);
    //int id2 = AddLinearMoveRel(0, 1, 1, 1, true);    //AddLinearMoveRel(int Axis,double Step,int StepCount,double Speed,bool Dir);
    int id3 = AddLinMove(-10000, -10000, -10000, 0, 0, 0, 0, 0);
    AddLinMove(0, 0, 0, 0, 0, 0, 0, 0);

    //std::cout << "ID1: " << id1 << " ID2: " << id2 << std::endl;
    FreeLibrary(hGetProcLib);
    std::cout << "Device count: " << x << std::endl;
    std::cout << "estop: " << res_stat.Estop << std::endl;
    std::cout << "Idle: " << res_stat.Idle << std::endl;
    //printf("%d\n", r4);

    return 0;
}

Maybe I forgot an axissetting?
TheButcher
 
Posts: 11
Joined: Mon Jul 22, 2024 11:12 am

Re: Help with the c++ API

Postby jdg26 » Fri Aug 23, 2024 3:05 pm

Woo-hoo! Glad you made some progress with the machine.

Here's what you should take a look at:

SetAxisSetting() is required to be passed a pointer to an AxisSetting struct.
See below taken from the UC100.h header file:
Code: Select all
extern "C" UC100API int SetAxisSetting(AxisSetting *_AxisSetting);  //Sets the axes parameters.


However, you are defining your SetAxisSettingFP function pointer to be passed an AxisSetting struct value, not the pointer.
This:
Code: Select all
typedef int(*SetAxisSettingFP)(AxisSetting);

Should be changed to this:
Code: Select all
typedef int(*SetAxisSettingFP)(AxisSetting*);

And then when you go ahead and use the SetAxis() function, you want to pass the AxisSetting pointer, like this:
Code: Select all
    SetAxisSetting(&x_axis);
    SetAxisSetting(&y_axis);
    SetAxisSetting(&z_axis);
    SetAxisSetting(&a_axis);


I highly suggest checking every return value from a UC100 DLL function call.
You can do so like this:
Code: Select all
    // See ReturnVal enum in UC100.h for the error return values
    int error_check = SetAxisSetting(&x_axis);
    if (error_check != 0){
        cout << "error!\nUC100 error-code: " << error_check << endl;
        return;
    }
jdg26
 
Posts: 11
Joined: Wed Oct 25, 2023 1:58 pm
Location: USA

Re: Help with the c++ API

Postby TheButcher » Sat Aug 24, 2024 12:57 pm

Thanks :), still doesnt work tho.
Code: Select all
#include <cstdio>
#include <windows.h>
#include <iostream>
#include "source/UC100.h"

typedef int (__stdcall *AddLinMoveFP)(double, double, double, double, double, double, double, double);
typedef int (*ListDevicesFP)(int*);
typedef int (*SetEstopSettingFP)(int, bool);
typedef int (*SetEstopStateFP)();
typedef int(* DeviceInfoFP)(int, int*, int*);
typedef int(* OpenFP)(int);
typedef int(*GetStatusFP)(Stat *SStat);
typedef int(*StopFP)();
typedef int(*SetMotionProgressStateFP)(bool);
typedef int(*SetAxisSettingFP)(AxisSetting*);
typedef int(*AddLinearMoveRelFP)(int, double, int, double, bool);
typedef int(*SetSpindleSettingFP)(SPSetting*);
//SET SPINDEL SETTOING!!!!
//SetSpindleSetting(SPSetting *_SPSetting)

int main() {
    // load the UC100 DLL
    HINSTANCE hGetProcLib = LoadLibrary(R"(C:\UCCNC\API\DLL\UC100.dll)");
    if (!hGetProcLib) {
        printf("could not load the dynamic library\n");
        return 1;
    }

    // get the function we want, ListDevices in this example
    auto AddLinMove = (AddLinMoveFP)GetProcAddress(hGetProcLib, "AddLinearMoveRel");
    auto listDevices = (ListDevicesFP) GetProcAddress(hGetProcLib, "ListDevices");
    auto SetEstopSetting = (SetEstopSettingFP) GetProcAddress(hGetProcLib, "SetEstopSetting");
    auto Open = (OpenFP) GetProcAddress(hGetProcLib, "Open");
    auto GetStatus = (GetStatusFP) GetProcAddress(hGetProcLib, "GetStatus");
    auto Stop = (StopFP) GetProcAddress(hGetProcLib, "Stop");
    auto SetEstopState = (SetEstopStateFP) GetProcAddress(hGetProcLib, "SetEstopState");
    auto SetMotionProgressState = (SetMotionProgressStateFP) GetProcAddress(hGetProcLib, "SetMotionProgressState");
    auto SetAxisSetting = (SetAxisSettingFP) GetProcAddress(hGetProcLib, "SetAxisSetting");
    auto AddLinearMoveRel = (AddLinearMoveRelFP) GetProcAddress(hGetProcLib, "AddLinearMoveRel");
    auto SetSpindleSetting = (SetSpindleSettingFP) GetProcAddress(hGetProcLib, "SetSpindleSetting");

    /*
     * typedef struct AxisSetting {
   int      Axis;         //Axis number (X=0,Y=1,Z=2,A=3,B=4,C=5).
   bool   Enable;         //Enables the axis.
   int      ;      //Step output pin.
   int      DirPin;         //Direction output pin.
   bool   StepNeg;      //Inverts the step pin.
   bool   DirNeg;         //Inverts the dir pin.
   double   MaxAccel;      //Acceleration parameter of the axis in Units/sec^2.
   double   MaxVel;         //Velocity parameter of the axis in Units/sec.
   double   StepPer;      //Steps per Units parameter.
   int      HomePin;      //Home input pin of the axis.
   bool   HomeNeg;      //Inverts the home input.
   int      LimitPPin;      //Positive side end-limit input pin.
   bool   LimitPNeg;      //Inverts the positive side end-limit input.
   int      LimitNPin;      //Negative side end-limit input pin.
   bool   LimitNNeg;      //Inverts the negative side end-limit input.
   double   SoftLimitP;      //Positive side software end-limit.
   double   SoftLimitN;      //Negative side software end-limit.
   int      SlaveAxis;      //Slave axis, only for the XYZ axes. (0=No slave, 3=A slave, 4=B slave, 5=C slave).
   bool   BacklashOn;      //Enables the backlash compensation for the axis.
   double   BacklashDist;   //Backlash distance in Units.
   double   CompAccel;      //Compensation acceleration for backlash and thread cutting in Units/sec^2.
   int      EnablePin;      //Axis enable output pin.
   bool   EnablePinNeg;   //Inverts the axis enable output.
   int      EnableDelay;   //Delays the enable output 0-255 value (x10msec).
   int      CurrentHiLowPin;   //Current hi/low output pin.
   bool   CurrentHiLowPinNeg;   //Inverts the current hi/low output.
   double   HomeBackOff;   //Home back off distance in Units.
   bool   RotaryAxis;      //Enables the rotary axis function for the axis. Works for the A,B,C axes only.
   bool   RotaryRollover;   //Enables the rollover for rotary axis on 360 degrees. Works for the A,B,C axes only and if the rotary axis function is enabled.
}AxisSetting;
     */
    AxisSetting x_axis, y_axis, z_axis, a_axis;
    x_axis.Axis = 0;
    x_axis.Enable = true;
    x_axis.StepPin = 3;
    x_axis.DirPin = 2;
    x_axis.MaxAccel = 400;
    x_axis.MaxVel = 4200;
    x_axis.StepPer = 533;
    x_axis.HomePin = 12;
    x_axis.LimitPPin = 0;
    x_axis.LimitNPin = 12;
    x_axis.SoftLimitP = 298;
    x_axis.SoftLimitN = 0;
    x_axis.EnablePin = 0;
    x_axis.CurrentHiLowPin = 0;

    y_axis.Axis = 1;
    y_axis.Enable = true;
    y_axis.StepPin = 5;
    y_axis.DirPin = 4;
    y_axis.MaxAccel = 400;
    y_axis.MaxVel = 4200;
    y_axis.StepPer = 533;
    y_axis.HomePin = 12;
    y_axis.LimitPPin = 0;
    y_axis.LimitNPin = 12;
    y_axis.SoftLimitP = 415;
    y_axis.SoftLimitN = 0;
    y_axis.EnablePin = 0;
    y_axis.CurrentHiLowPin = 0;

    z_axis.Axis = 2;
    z_axis.Enable = true;
    z_axis.StepPin = 7;
    z_axis.DirPin = 6;
    z_axis.MaxAccel = 400;
    z_axis.MaxVel = 4200;
    z_axis.StepPer = 533;
    z_axis.HomePin = 12;
    z_axis.LimitPPin = 0;
    z_axis.LimitNPin = 12;
    z_axis.SoftLimitP = -132;
    z_axis.SoftLimitN = 0;
    z_axis.EnablePin = 0;
    z_axis.CurrentHiLowPin = 0;

    a_axis.Axis = 3;
    a_axis.Enable = true;
    a_axis.StepPin = 9;
    a_axis.DirPin = 8;
    a_axis.MaxAccel = 200;
    a_axis.MaxVel = 3600;
    a_axis.StepPer = 480;
    a_axis.HomePin = 12;
    a_axis.LimitPPin = 0;
    a_axis.LimitNPin = 12;
    a_axis.SoftLimitP = -1000000;
    a_axis.SoftLimitN = 1000000;
    a_axis.EnablePin = 0;
    a_axis.CurrentHiLowPin = 0;

    SPSetting s_set;
    s_set.Mode = 0;
    SetSpindleSetting(&s_set);
    // call the DLL function
    int x = 0;
    Stat res_stat;
    int r2 = listDevices(&x);
    std::cout << "R2: " << r2 << std::endl;
    int r4 = Open(1);
    Stop();

    int error_checkX = SetAxisSetting(&x_axis);
    if (error_checkX!= 0){
        std::cout << "X: error!\nUC100 error-code: " << error_checkX << std::endl;
    }

    Stop();
    int error_checkY = SetAxisSetting(&y_axis);
    if (error_checkY != 0){
        std::cout << "Y: error!\nUC100 error-code: " << error_checkY << std::endl;
    }

    Stop();
    int error_checkZ = SetAxisSetting(&z_axis);
    if (error_checkZ != 0){
        std::cout << "Z: error!\nUC100 error-code: " << error_checkZ << std::endl;
    }

    Stop();
    int error_checkA = SetAxisSetting(&a_axis);
    if (error_checkA != 0){
        std::cout << "A: error!\nUC100 error-code: " << error_checkA << std::endl;
    }

    SetMotionProgressState(false);
    //int r6 = Stop();
    //int r3 = SetEstopSetting(11, false);
    int r5 = GetStatus(&res_stat);
    //int r = linMove(1000, 1000, 1000, 0, 0, 0, 1000, 8);
    int id1 = AddLinMove(0, 0, 0, 0, 0, 0, 10, 9); //  AddLinearMove(double x,double y,double z,double a,double b,double c, double Feed, int ID);
    //int id2 = AddLinearMoveRel(0, 1, 1, 1, true);    //AddLinearMoveRel(int Axis,double Step,int StepCount,double Speed,bool Dir);
    int id3 = AddLinMove(100, 100, 0, 0, 0, 0, 0, 9);
    AddLinMove(0, 0, 0, 0, 0, 0, 0, 9);

    std::cout << "ID1: " << id1 << " ID2: " << id3 << std::endl;
    FreeLibrary(hGetProcLib);
    std::cout << "Device count: " << x << std::endl;
    std::cout << "estop: " << res_stat.Estop << std::endl;
    std::cout << "Idle: " << res_stat.Idle << std::endl;
    //printf("%d\n", r4);

    return 0;
}


I get error code 7:
7 Controller is in estop state. Can be cleared with the Stop() function.
thats why i added all of the Stop() s.
Do you have any idea?
I also tried
Code: Select all
SetEstopSetting(11, false);

But that diesnt do anything either :/
TheButcher
 
Posts: 11
Joined: Mon Jul 22, 2024 11:12 am

Re: Help with the c++ API

Postby TheButcher » Mon Aug 26, 2024 11:26 am

I totally forgot to say that the error only occured for the Y-axis
TheButcher
 
Posts: 11
Joined: Mon Jul 22, 2024 11:12 am

Re: Help with the c++ API

Postby jdg26 » Tue Aug 27, 2024 2:49 pm

The UC100_ESTOP error code is surprising, I had the same error pop up with my C++ test code.
It does look like calling the UC100 Stop() function is a quick fix to get the controller working nicely.

I will note the C# wrapper is very user friendly and it may be worth taking a look at.

To get the C++ code working please see the sample below:
Code: Select all
#include <cstdio>
#include <windows.h>
#include <iostream>
#include "source/UC100.h"

// typedef some UC100 function pointers
typedef int (__stdcall *AddLinMoveFP)(double, double, double, double, double, double, double, double);
typedef int (*ListDevicesFP)(int*);
typedef int (*SetEstopSettingFP)(int, bool);
typedef int (*SetEstopStateFP)();
typedef int(*DeviceInfoFP)(int, int*, int*);
typedef int(*OpenFP)(int);
typedef int(*GetStatusFP)(Stat *SStat);
typedef int(*StopFP)(void);
typedef int(*SetMotionProgressStateFP)(bool);
typedef int(*SetAxisSettingFP)(AxisSetting*);
typedef int(*AddLinearMoveRelFP)(int, double, int, double, bool);
typedef int(*SetSpindleSettingFP)(SPSetting*);
typedef int (*GetAxisPositionFP)(double*, double*, double*, double*, double*, double*);
//SET SPINDEL SETTOING!!!!
//SetSpindleSetting(SPSetting *_SPSetting);

// get the functions we want and make them global variables
auto AddLinMove_ = (AddLinMoveFP)GetProcAddress(hGetProcLib, "AddLinearMoveRel");
auto listDevices_ = (ListDevicesFP)GetProcAddress(hGetProcLib, "ListDevices");
auto SetEstopSetting_ = (SetEstopSettingFP)GetProcAddress(hGetProcLib, "SetEstopSetting");
auto Open_ = (OpenFP)GetProcAddress(hGetProcLib, "Open");
auto GetStatus_ = (GetStatusFP)GetProcAddress(hGetProcLib, "GetStatus");
auto Stop_ = (StopFP)GetProcAddress(hGetProcLib, "Stop");
auto SetEstopState_ = (SetEstopStateFP)GetProcAddress(hGetProcLib, "SetEstopState");
auto SetMotionProgressState_ = (SetMotionProgressStateFP)GetProcAddress(hGetProcLib, "SetMotionProgressState");
auto SetAxisSetting_ = (SetAxisSettingFP)GetProcAddress(hGetProcLib, "SetAxisSetting");
auto AddLinearMoveRel_ = (AddLinearMoveRelFP)GetProcAddress(hGetProcLib, "AddLinearMoveRel");
auto SetSpindleSetting_ = (SetSpindleSettingFP)GetProcAddress(hGetProcLib, "SetSpindleSetting");
auto GetAxisPosition_ = (GetAxisPositionFP)GetProcAddress(hGetProcLib, "GetAxisPosition");

int device_count;

static bool open_device() {
    // call the DLL function
    device_count = 0;
    int r1 = listDevices_(&device_count);
    std::cout << "R1: " << r1 << std::endl;
    int r2 = Open_(1);

    if (r1 != 0 || r2 != 0)
        return false;

    Stop_();

    SPSetting s_set;
    s_set.Mode = 0;
    SetSpindleSetting_(&s_set);

    return true;
}

// set your axes and enable them
static bool set_axes() {
    AxisSetting x_axis, y_axis, z_axis, a_axis;

    x_axis.Axis = 0;
    x_axis.Enable = true;
    x_axis.StepPin = 3;
    x_axis.DirPin = 2;
    x_axis.MaxAccel = 400;
    x_axis.MaxVel = 4200;
    x_axis.StepPer = 533;
    x_axis.HomePin = 12;
    x_axis.LimitPPin = 0;
    x_axis.LimitNPin = 12;
    x_axis.SoftLimitP = 298;
    x_axis.SoftLimitN = 0;
    x_axis.EnablePin = 0;
    x_axis.CurrentHiLowPin = 0;

    y_axis.Axis = 1;
    y_axis.Enable = true;
    y_axis.StepPin = 5;
    y_axis.DirPin = 4;
    y_axis.MaxAccel = 400;
    y_axis.MaxVel = 4200;
    y_axis.StepPer = 533;
    y_axis.HomePin = 12;
    y_axis.LimitPPin = 0;
    y_axis.LimitNPin = 12;
    y_axis.SoftLimitP = 415;
    y_axis.SoftLimitN = 0;
    y_axis.EnablePin = 0;
    y_axis.CurrentHiLowPin = 0;

    z_axis.Axis = 2;
    z_axis.Enable = true;
    z_axis.StepPin = 7;
    z_axis.DirPin = 6;
    z_axis.MaxAccel = 400;
    z_axis.MaxVel = 4200;
    z_axis.StepPer = 533;
    z_axis.HomePin = 12;
    z_axis.LimitPPin = 0;
    z_axis.LimitNPin = 12;
    z_axis.SoftLimitP = -132;
    z_axis.SoftLimitN = 0;
    z_axis.EnablePin = 0;
    z_axis.CurrentHiLowPin = 0;

    a_axis.Axis = 3;
    a_axis.Enable = true;
    a_axis.StepPin = 9;
    a_axis.DirPin = 8;
    a_axis.MaxAccel = 200;
    a_axis.MaxVel = 3600;
    a_axis.StepPer = 480;
    a_axis.HomePin = 12;
    a_axis.LimitPPin = 0;
    a_axis.LimitNPin = 12;
    a_axis.SoftLimitP = -1000000;
    a_axis.SoftLimitN = 1000000;
    a_axis.EnablePin = 0;
    a_axis.CurrentHiLowPin = 0;

    int error_checkX = SetAxisSetting_(&x_axis);
    if (error_checkX != 0) {
        std::cout << "X: error!\nUC100 error-code: " << error_checkX << std::endl;
        return false;
    }

    Stop_();
    int error_checkY = SetAxisSetting_(&y_axis);
    if (error_checkY != 0) {
        std::cout << "Y: error!\nUC100 error-code: " << error_checkY << std::endl;
        return false;
    }

    Stop_();
    int error_checkZ = SetAxisSetting_(&z_axis);
    if (error_checkZ != 0) {
        std::cout << "Z: error!\nUC100 error-code: " << error_checkZ << std::endl;
        return false;
    }

    Stop_();
    int error_checkA = SetAxisSetting_(&a_axis);
    if (error_checkA != 0) {
        std::cout << "A: error!\nUC100 error-code: " << error_checkA << std::endl;
        return false;
    }

    return true;
}

static void test_motor_move() {
    // this may or may not be needed
    // haven't been able to test
    SetMotionProgressState_(false);

    // test with one axis first to verify
    // this will move the x-axis 5units, you might hit a limit
    // play around with speed parameter
    AddLinearMoveRel_(0, 5, 1, 1.0, true); // AddLinearMoveRel(int Axis, double Step, int StepCount, double Speed, bool Dir);

    // test a multi-axis move
    int move_id = 0; // can use a var for your move_id, and just add to it for subsequent moves

    // get the current axis_position
    double x_pos, y_pos, z_pos, a_pos, b_pos, c_pos;
    GetAxisPosition_(&x_pos, &y_pos, &z_pos, &a_pos, &b_pos, &c_pos);

    AddLinMove_(x_pos+1, y_pos+1, z_pos, a_pos, b_pos, c_pos, 0.5, move_id++);

    // can do other moves here...
}

static void print_controller_info() {
    Stat res_stat;

    GetStatus_(&res_stat);
    std::cout << "Device count: " << device_count << std::endl;
    std::cout << "estop: " << res_stat.Estop << std::endl;
    std::cout << "Idle: " << res_stat.Idle << std::endl;
}

int main() {
    // load the UC100 DLL
    HINSTANCE hGetProcLib = LoadLibrary(R"(C:\UCCNC\API\DLL\UC100.dll)");
    if (!hGetProcLib) {
        printf("could not load the dynamic library\n");
        return 1;
    }

    // open the UC100
    if (!open_device())
        return 1;

    // set your axes
    if (!set_axes())
        return 1;

    // test some motor movements
    test_motor_move();

    // print some info
    print_controller_info();

    while (1) {
        // this may be needed to allow the controller
        // to complete all moves in the motion buffer
    }

    return 0;
}

** note I haven't been able to fully test this sample **

I would take a look at the parameters needed for the AddLinearMove family of functions.

Also, how are you running this, through the command line?
jdg26
 
Posts: 11
Joined: Wed Oct 25, 2023 1:58 pm
Location: USA

Re: Help with the c++ API

Postby TheButcher » Wed Aug 28, 2024 7:27 am

ok thank you so much! This worked. I run it through Clion IDE. This seems to be the right order to set stuff up. Also thanks for cleaning the code up!! :)
TheButcher
 
Posts: 11
Joined: Mon Jul 22, 2024 11:12 am

Re: Help with the c++ API

Postby jdg26 » Wed Aug 28, 2024 12:03 pm

Nice! Glad to hear it's working.
jdg26
 
Posts: 11
Joined: Wed Oct 25, 2023 1:58 pm
Location: USA

Previous

Return to General discussion about the UCCNC software

Who is online

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