Page 1 of 1

dotNet GetUserDataDouble problem

Posted: Fri Nov 16, 2018 9:14 pm
by Moray
I'm sure I'm doing something stupid here, but I'm unable to read a double from userdata via C#.

The snippet of code in the KFlop loading the data to KFlop is this -

Code: Select all

double *d2 = (double *)&persist.UserData[TRIGGER_POS_VAR];
	if(ReadBit(PROBE_OFFSETS_REQUEST))
	{
		GetFixtureIndex(&FixtureIndex);
		persist.UserData[PROBE_OFFSET] = FixtureIndex;
		printf("Fixture:%d\n", persist.UserData[PROBE_OFFSET]);
		
		GetOriginOffset(&OriginOffsetX, FixtureIndex, X);
		d2[0] = OriginOffsetX;
		GetOriginOffset(&OriginOffsetY, FixtureIndex, Y);
		d2[1] = OriginOffsetY;
		GetOriginOffset(&OriginOffsetZ, FixtureIndex, Z);
		d2[2] = OriginOffsetZ;
		printf("OriginOffset:X%f, Y%f, Z%f\n", d2[0], d2[1], d2[2]);
		
		GetAxisOffset(&AxisOffsetX, X);
		d2[3] = AxisOffsetX;
		GetAxisOffset(&AxisOffsetY, Y);
		d2[4] = AxisOffsetY;
		GetAxisOffset(&AxisOffsetZ, Z);
		d2[5] = AxisOffsetZ;
		printf("AxisOffset:X%f, Y%f, Z%f\n", d2[3], d2[4], d2[5]);
		
		ClearBit(PROBE_OFFSETS_REQUEST);
		SetBit(PROBE_OFFSETS_COMPLETE);
	}
Which works perfectly, and displays this in the console-

Code: Select all

Fixture:2
OriginOffset:X-0.500000, Y0.500000, Z0.500000
AxisOffset:X0.250000, Y0.000000, Z0.000000
However when I use this code in visual studio -

Code: Select all

OffsetInUse = KM.GetUserData(UserDataProbeOffset);
                        OffsetFixtureX = KM.GetUserDataDouble(UserDataProbeOffsetFixture / 2);
                        textBox_debug.AppendText(Convert.ToString(KM.GetUserDataDouble(UserDataProbeOffsetFixture / 2)));
                        textBox_debug.AppendText(Convert.ToString(KM.GetUserDataDouble((UserDataProbeOffsetFixture / 2) + 1)));
                        OffsetFixtureY = 99.99;
                        OffsetGlobalX = KM.GetUserDataDouble(UserDataProbeOffsetGlobal / 2);
                        RefreshOffsetsText();
The values I get in my debug textbox are 1.54905692776038E-312 and 1.59149684358956E-312
The fixture offset displays ok. The RefreshOffsetsText() function takes the values loaded into the relevant variables and updates them in my app, I've only added the output to the textbox to try and debug things.

(as a side note, it's probably worth adding a note to the documentation that when using doubles in UserData with dotNet, you have to divide the index by two compared with the index you use in the KFlop)

Re: dotNet GetUserDataDouble problem

Posted: Fri Nov 16, 2018 9:21 pm
by Moray
Something I never checked were the values returned when using different fixtures in KMCNC.
Regardless of the what fixture is selected, the values returned via dotNet remain the same, even though the values from the KFlop/console change.

Also just realised I never mentioned the general data flow between things.
My program sets a virtual bit in the KFlop to request the offsets information.
The KFlop then requests the offset information from the KMCNC, and places that information in the various UserData locations (that's the first code snippet above).
Once the KFlop has stored all the offsets info, it sets another virtual bit.
Once my program sees the bit set, it then reads the information from UserData.

I know this is the long way around, but it'll hopefully get simplified in future.

Re: dotNet GetUserDataDouble problem

Posted: Sat Nov 17, 2018 4:07 am
by TomKerekes
Hi Moray,

You left out a lot of details, but it seems to work for me. The code I used is below.

After executing the string s contains "0.5 2.5" which was what was the offsets for my selected fixture.

Note the the doubles must reside on even persist boundaries.

Code: Select all

#include "KMotionDef.h"
#define TMP 10                    // which spare persist to use to transfer data
#include "KflopToKMotionCNCFunctions.c"

#define TRIGGER_POS_VAR 50
#define PROBE_OFFSET 49
#define X 0
#define Y 1
#define Z 2

void main()
{
    int FixtureIndex;
    double OriginOffsetX, OriginOffsetY, OriginOffsetZ, AxisOffsetX, AxisOffsetY, AxisOffsetZ;

    double *d2 = (double *)&persist.UserData[TRIGGER_POS_VAR];

    GetFixtureIndex(&FixtureIndex);
    persist.UserData[PROBE_OFFSET] = FixtureIndex;
    printf("Fixture:%d\n", persist.UserData[PROBE_OFFSET]);

    GetOriginOffset(&OriginOffsetX, FixtureIndex, X);
    d2[0] = OriginOffsetX;
    GetOriginOffset(&OriginOffsetY, FixtureIndex, Y);
    d2[1] = OriginOffsetY;
    GetOriginOffset(&OriginOffsetZ, FixtureIndex, Z);
    d2[2] = OriginOffsetZ;
    printf("OriginOffset:X%f, Y%f, Z%f\n", d2[0], d2[1], d2[2]);

    GetAxisOffset(&AxisOffsetX, X);
    d2[3] = AxisOffsetX;
    GetAxisOffset(&AxisOffsetY, Y);
    d2[4] = AxisOffsetY;
    GetAxisOffset(&AxisOffsetZ, Z);
    d2[5] = AxisOffsetZ;
    printf("AxisOffset:X%f, Y%f, Z%f\n", d2[3], d2[4], d2[5]);
}

Code: Select all

            string s;
            int UserDataProbeOffset = 49;
            int UserDataProbeOffsetFixture = 50;
            int OffsetInUse;
            OffsetInUse = KM.GetUserData(UserDataProbeOffset);
            s = Convert.ToString(KM.GetUserDataDouble(UserDataProbeOffsetFixture / 2));
            s+= " " + Convert.ToString(KM.GetUserDataDouble((UserDataProbeOffsetFixture / 2) + 1));
Something I never checked were the values returned when using different fixtures in KMCNC.
Regardless of the what fixture is selected, the values returned via dotNet remain the same, even though the values from the KFlop/console change.
I don't understand what you mean by this.

Re: dotNet GetUserDataDouble problem

Posted: Sat Nov 17, 2018 6:54 pm
by Moray
I know I left out a lot of details. I would share the complete code, however it's needing a major tidy up, which I'm hoping to leave until I have everything fully functioning, then ask you nicely to add the required functionality to KMCNC, so everything can be modified/cleared out in one go (or just mostly re-written now I've got a better understanding of how things need to work!). :)

I finally figured it out, and it was something stupid!
When I've copied and pasted the userdata double code in the KFlop C, I'd forgotten to change the index constant from TRIGGER_POS_VAR to PROBE_OFFSET_FIXTURE, so the values were getting loaded to the wrong user vars. I only realised after directly entering the user var number, and things started to work.
It's amazing what a break does to finding bugs :-)

I'm now getting all the data I need into my program, so all I need now is to write the mechanism for calculating and setting offsets, and I'll have functional basic probing, albeit only for one corner so far!.
TomKerekes wrote:
Sat Nov 17, 2018 4:07 am

Something I never checked were the values returned when using different fixtures in KMCNC.
Regardless of the what fixture is selected, the values returned via dotNet remain the same, even though the values from the KFlop/console change.
I don't understand what you mean by this.
What I meant was when I selected different fixtures in KMCNC, with different offset values, the values returned to the debug box never changed. They always remained the same.

Re: dotNet GetUserDataDouble problem

Posted: Sat Nov 17, 2018 6:59 pm
by TomKerekes
Hi Moray,
What I meant was when I selected different fixtures in KMCNC, with different offset values, the values returned to the debug box never changed. They always remained the same.
I'm afraid I still don't understand. Is this still an issue after fixing the bug? What are you doing in .NET to get the values and from where?

Re: dotNet GetUserDataDouble problem

Posted: Sat Nov 17, 2018 10:50 pm
by Moray
TomKerekes wrote:
Sat Nov 17, 2018 6:59 pm
Hi Moray,
What I meant was when I selected different fixtures in KMCNC, with different offset values, the values returned to the debug box never changed. They always remained the same.
I'm afraid I still don't understand. Is this still an issue after fixing the bug? What are you doing in .NET to get the values and from where?
Everything is working perfectly now.
It was the 1.54905692776038E-312 and 1.59149684358956E-312 values that were never changing, which I thought would help point me towards the problem, which they should of done, as they were the values of some unrelated user vars.


I've finally managed to probe the corner of a box, and update the fixture offsets so the corner of the box becomes the fixture origin.
Next step is to push the various configuration options out to a config file, and add the ability to generate the required KFlop C code from those options so it's a simple case of copy and pasting into an existing init.c file to get probing functionality. At which point I'll look at an alpha release for anybody interesting in testing.