Backlash amount/direction variable type and persistence...

Moderators: TomKerekes, dynomotion

Post Reply
SamMarrocco
Posts: 85
Joined: Fri Apr 27, 2018 12:44 pm

Backlash amount/direction variable type and persistence...

Post by SamMarrocco » Sun Jul 24, 2022 8:49 pm

I'm currently using persist variables to move backlash amount and direction variables about my code for various reasons (such as being able to keep BL amount and direction after an axis has been .enabled and when my app is started and stopped. My code appears to be working but I'd like some clarity on the types I should be using and if I'm casting them correctly or just 'lucking out'.


As kept in the KFlop, should ch->Backlash be a float and ch->BacklashDirection be an int?

I'm currently using the following code to copy the values from the KFlop into the persist variables, which seems to work, but doesn't seem like it should.....

float Axis0_Backlash;
float Axis0_BacklashDirection;

Axis0_BacklashDirection=ch0->BacklashDirection;
Axis0_Backlash=ch0->Backlash;

persist.UserData[GPV__AXIS_0_BACKLASH] = *(int *) & Axis0_Backlash;
persist.UserData[GPV__AXIS_0_BACKLASHDIRECTION] = *(int *) & Axis0_BacklashDirection;


.....and this code to copy the values from the persist variables back into the KFlop......

float Axis0_Backlash;
float Axis0_BacklashDirection;

Axis0_Backlash = *(float *) &persist.UserData[GPV__AXIS_0_BACKLASH];
Axis0_BacklashDirection = *(float *) &persist.UserData[GPV__AXIS_0_BACKLASHDIRECTION];

ch0->Backlash=Axis0_Backlash;
ch0->BacklashDirection=Axis0_BacklashDirection;

I feel like I've got 'two wrongs making a right' somewhere here. I'd appreciate any suggestions.

User avatar
TomKerekes
Posts: 2540
Joined: Mon Dec 04, 2017 1:49 am

Re: Backlash amount/direction variable type and persistence...

Post by TomKerekes » Mon Jul 25, 2022 4:38 pm

H Sam,
As kept in the KFlop, should ch->Backlash be a float and ch->BacklashDirection be an int?
yes. For definitions see KMotion_def.h

Code: Select all

	int BacklashMode;				// Type of correction:  Currently only BACKLASH_OFF, BACKLASH_LINEAR
	float BacklashAmount;			// Amount of Backlash to be applied
	float BacklashRate;				// Rate Backlash shoule be applied, counts/sec
	int BacklashDirection;			// Last non zero direction moved
	float PrevBacklashDest;			// Prev Destination where backlash was determined to allow small hysteresis
	float Backlash;					// current amount of compensation being applied
What you have is not exactly logical but should work. This line:

Code: Select all

Axis0_BacklashDirection=ch0->BacklashDirection;
converts the small integer to floating point format which can be represented exactly in floating point.

The value is then saved as a float and retrieved as a float.

The float is then reconverted to an integer here:

Code: Select all

ch0->BacklashDirection=Axis0_BacklashDirection;
HTH
Regards,

Tom Kerekes
Dynomotion, Inc.

Post Reply